org.el 956 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  85. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  86. ;;
  87. ;; In Org buffers, the value of `outline-regexp' is that of
  88. ;; `org-outline-regexp'. The only function still directly relying on
  89. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  90. ;; job when `orgstruct-mode' is active.
  91. (defvar org-outline-regexp "\\*+ "
  92. "Regexp to match Org headlines.")
  93. (defvar org-outline-regexp-bol "^\\*+ "
  94. "Regexp to match Org headlines.
  95. This is similar to `org-outline-regexp' but additionally makes
  96. sure that we are at the beginning of the line.")
  97. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  98. "Matches a headline, putting stars and text into groups.
  99. Stars are put in group 1 and the trimmed body in group 2.")
  100. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  101. (unless (boundp 'calendar-view-holidays-initially-flag)
  102. (org-defvaralias 'calendar-view-holidays-initially-flag
  103. 'view-calendar-holidays-initially))
  104. (unless (boundp 'calendar-view-diary-initially-flag)
  105. (org-defvaralias 'calendar-view-diary-initially-flag
  106. 'view-diary-entries-initially))
  107. (unless (boundp 'diary-fancy-buffer)
  108. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  109. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  112. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  113. (declare-function org-agenda-redo "org-agenda" (&optional all))
  114. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  115. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  116. (declare-function org-beamer-mode "ox-beamer" ())
  117. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  118. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  119. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  120. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  121. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  122. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  123. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  124. (declare-function org-clock-update-time-maybe "org-clock" ())
  125. (declare-function org-clocktable-shift "org-clock" (dir n))
  126. (declare-function org-element-at-point "org-element" ())
  127. (declare-function org-element-cache-refresh "org-element" (pos))
  128. (declare-function org-element-cache-reset "org-element" (&optional all))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-context "org-element" (&optional element))
  131. (declare-function org-element-copy "org-element" (datum))
  132. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  133. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element" (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element-type "org-element" (element))
  140. (declare-function org-element-update-syntax "org-element" ())
  141. (declare-function org-id-find-id-file "org-id" (id))
  142. (declare-function org-id-get-create "org-id" (&optional force))
  143. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  144. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  145. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  146. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  147. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  148. (declare-function org-table-align "org-table" ())
  149. (declare-function org-table-begin "org-table" (&optional table-type))
  150. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  151. (declare-function org-table-blank-field "org-table" ())
  152. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  153. (declare-function org-table-edit-field "org-table" (arg))
  154. (declare-function org-table-end "org-table" (&optional table-type))
  155. (declare-function org-table-end-of-field "org-table" (&optional n))
  156. (declare-function org-table-insert-row "org-table" (&optional arg))
  157. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  158. (declare-function org-table-maybe-eval-formula "org-table" ())
  159. (declare-function org-table-maybe-recalculate-line "org-table" ())
  160. (declare-function org-table-next-row "org-table" ())
  161. (declare-function org-table-paste-rectangle "org-table" ())
  162. (declare-function org-table-wrap-region "org-table" (arg))
  163. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  164. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  165. (declare-function orgtbl-mode "org-table" (&optional arg))
  166. (defsubst org-uniquify (list)
  167. "Non-destructively remove duplicate elements from LIST."
  168. (let ((res (copy-sequence list))) (delete-dups res)))
  169. (defsubst org-get-at-bol (property)
  170. "Get text property PROPERTY at the beginning of line."
  171. (get-text-property (point-at-bol) property))
  172. (defsubst org-trim (s)
  173. "Remove whitespace at the beginning and the end of string S."
  174. (replace-regexp-in-string
  175. "\\`[ \t\n\r]+" ""
  176. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  177. ;; load languages based on value of `org-babel-load-languages'
  178. (defvar org-babel-load-languages)
  179. ;;;###autoload
  180. (defun org-babel-do-load-languages (sym value)
  181. "Load the languages defined in `org-babel-load-languages'."
  182. (set-default sym value)
  183. (mapc (lambda (pair)
  184. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  185. (if active
  186. (progn
  187. (require (intern (concat "ob-" lang))))
  188. (progn
  189. (funcall 'fmakunbound
  190. (intern (concat "org-babel-execute:" lang)))
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-expand-body:" lang)))))))
  193. org-babel-load-languages))
  194. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  195. ;;;###autoload
  196. (defun org-babel-load-file (file &optional compile)
  197. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  198. This function exports the source code using `org-babel-tangle'
  199. and then loads the resulting file using `load-file'. With prefix
  200. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  201. file to byte-code before it is loaded."
  202. (interactive "fFile to load: \nP")
  203. (let* ((age (lambda (file)
  204. (float-time
  205. (time-subtract (current-time)
  206. (nth 5 (or (file-attributes (file-truename file))
  207. (file-attributes file)))))))
  208. (base-name (file-name-sans-extension file))
  209. (exported-file (concat base-name ".el")))
  210. ;; tangle if the org-mode file is newer than the elisp file
  211. (unless (and (file-exists-p exported-file)
  212. (> (funcall age file) (funcall age exported-file)))
  213. ;; Tangle-file traversal returns reversed list of tangled files
  214. ;; and we want to evaluate the first target.
  215. (setq exported-file
  216. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "ebnf2ps" ebnf2ps))
  283. :value-type (boolean :tag "Activate" :value t)))
  284. ;;;; Customization variables
  285. (defcustom org-clone-delete-id nil
  286. "Remove ID property of clones of a subtree.
  287. When non-nil, clones of a subtree don't inherit the ID property.
  288. Otherwise they inherit the ID property with a new unique
  289. identifier."
  290. :type 'boolean
  291. :version "24.1"
  292. :group 'org-id)
  293. ;;; Version
  294. (org-check-version)
  295. ;;;###autoload
  296. (defun org-version (&optional here full message)
  297. "Show the org-mode version.
  298. Interactively, or when MESSAGE is non-nil, show it in echo area.
  299. With prefix argument, or when HERE is non-nil, insert it at point.
  300. In non-interactive uses, a reduced version string is output unless
  301. FULL is given."
  302. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  303. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  304. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  305. (load-suffixes (list ".el"))
  306. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  307. (org-trash (or
  308. (and (fboundp 'org-release) (fboundp 'org-git-version))
  309. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  310. (load-suffixes save-load-suffixes)
  311. (org-version (org-release))
  312. (git-version (org-git-version))
  313. (version (format "Org-mode version %s (%s @ %s)"
  314. org-version
  315. git-version
  316. (if org-install-dir
  317. (if (string= org-dir org-install-dir)
  318. org-install-dir
  319. (concat "mixed installation! " org-install-dir " and " org-dir))
  320. "org-loaddefs.el can not be found!")))
  321. (version1 (if full version org-version)))
  322. (when here (insert version1))
  323. (when message (message "%s" version1))
  324. version1))
  325. (defconst org-version (org-version))
  326. ;;; Syntax Constants
  327. ;;;; Block
  328. (defconst org-block-regexp
  329. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  330. "Regular expression for hiding blocks.")
  331. (defconst org-dblock-start-re
  332. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  333. "Matches the start line of a dynamic block, with parameters.")
  334. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  335. "Matches the end of a dynamic block.")
  336. ;;;; Clock and Planning
  337. (defconst org-clock-string "CLOCK:"
  338. "String used as prefix for timestamps clocking work hours on an item.")
  339. (defvar org-closed-string "CLOSED:"
  340. "String used as the prefix for timestamps logging closing a TODO entry.")
  341. (defvar org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].")
  346. (defvar org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].")
  351. (defconst org-ds-keyword-length
  352. (+ 2
  353. (apply #'max
  354. (mapcar #'length
  355. (list org-deadline-string org-scheduled-string
  356. org-clock-string org-closed-string))))
  357. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  358. (defconst org-planning-line-re
  359. (concat "^[ \t]*"
  360. (regexp-opt
  361. (list org-closed-string org-deadline-string org-scheduled-string)
  362. t))
  363. "Matches a line with planning info.
  364. Matched keyword is in group 1.")
  365. (defconst org-clock-line-re
  366. (concat "^[ \t]*" org-clock-string)
  367. "Matches a line with clock info.")
  368. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  369. "Matches the DEADLINE keyword.")
  370. (defconst org-deadline-time-regexp
  371. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  372. "Matches the DEADLINE keyword together with a time stamp.")
  373. (defconst org-deadline-time-hour-regexp
  374. (concat "\\<" org-deadline-string
  375. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  376. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  377. (defconst org-deadline-line-regexp
  378. (concat "\\<\\(" org-deadline-string "\\).*")
  379. "Matches the DEADLINE keyword and the rest of the line.")
  380. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  381. "Matches the SCHEDULED keyword.")
  382. (defconst org-scheduled-time-regexp
  383. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  384. "Matches the SCHEDULED keyword together with a time stamp.")
  385. (defconst org-scheduled-time-hour-regexp
  386. (concat "\\<" org-scheduled-string
  387. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  388. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  389. (defconst org-closed-time-regexp
  390. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  391. "Matches the CLOSED keyword together with a time stamp.")
  392. (defconst org-keyword-time-regexp
  393. (concat "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string
  396. org-clock-string)
  397. t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 4 keywords, together with the time stamp.")
  400. (defconst org-keyword-time-not-clock-regexp
  401. (concat
  402. "\\<"
  403. (regexp-opt
  404. (list org-scheduled-string org-deadline-string org-closed-string) t)
  405. " *[[<]\\([^]>]+\\)[]>]")
  406. "Matches any of the 3 keywords, together with the time stamp.")
  407. (defconst org-maybe-keyword-time-regexp
  408. (concat "\\(\\<"
  409. (regexp-opt
  410. (list org-scheduled-string org-deadline-string org-closed-string
  411. org-clock-string)
  412. t)
  413. "\\)?"
  414. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  415. "\\|"
  416. "<%%([^\r\n>]*>\\)")
  417. "Matches a timestamp, possibly preceded by a keyword.")
  418. (defconst org-all-time-keywords
  419. (mapcar (lambda (w) (substring w 0 -1))
  420. (list org-scheduled-string org-deadline-string
  421. org-clock-string org-closed-string))
  422. "List of time keywords.")
  423. ;;;; Drawer
  424. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  425. "Matches first or last line of a hidden block.
  426. Group 1 contains drawer's name or \"END\".")
  427. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  428. "Regular expression matching the first line of a property drawer.")
  429. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  430. "Regular expression matching the last line of a property drawer.")
  431. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  432. "Regular expression matching the first line of a clock drawer.")
  433. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  434. "Regular expression matching the last line of a clock drawer.")
  435. (defconst org-property-drawer-re
  436. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  437. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  438. "[ \t]*:END:[ \t]*$")
  439. "Matches an entire property drawer.")
  440. (defconst org-clock-drawer-re
  441. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  442. org-clock-drawer-end-re "\\)\n?")
  443. "Matches an entire clock drawer.")
  444. ;;;; Headline
  445. (defconst org-heading-keyword-regexp-format
  446. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  447. "Printf format for a regexp matching a headline with some keyword.
  448. This regexp will match the headline of any node which has the
  449. exact keyword that is put into the format. The keyword isn't in
  450. any group by default, but the stars and the body are.")
  451. (defconst org-heading-keyword-maybe-regexp-format
  452. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  453. "Printf format for a regexp matching a headline, possibly with some keyword.
  454. This regexp can match any headline with the specified keyword, or
  455. without a keyword. The keyword isn't in any group by default,
  456. but the stars and the body are.")
  457. (defconst org-archive-tag "ARCHIVE"
  458. "The tag that marks a subtree as archived.
  459. An archived subtree does not open during visibility cycling, and does
  460. not contribute to the agenda listings.")
  461. (defconst org-comment-string "COMMENT"
  462. "Entries starting with this keyword will never be exported.
  463. An entry can be toggled between COMMENT and normal with
  464. \\[org-toggle-comment].")
  465. ;;;; LaTeX Environments and Fragments
  466. (defconst org-latex-regexps
  467. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  468. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  469. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  470. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  471. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  472. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  473. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  474. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  475. "Regular expressions for matching embedded LaTeX.")
  476. ;;;; Node Property
  477. (defconst org-effort-property "Effort"
  478. "The property that is being used to keep track of effort estimates.
  479. Effort estimates given in this property need to have the format H:MM.")
  480. ;;;; Table
  481. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  482. "Detect an org-type or table-type table.")
  483. (defconst org-table-line-regexp "^[ \t]*|"
  484. "Detect an org-type table line.")
  485. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  486. "Detect an org-type table line.")
  487. (defconst org-table-hline-regexp "^[ \t]*|-"
  488. "Detect an org-type table hline.")
  489. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  490. "Detect a table-type table hline.")
  491. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  492. "Detect the first line outside a table when searching from within it.
  493. This works for both table types.")
  494. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  495. "Detect a #+TBLFM line.")
  496. ;;;; Timestamp
  497. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  498. "Regular expression for fast time stamp matching.")
  499. (defconst org-ts-regexp-inactive
  500. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  501. "Regular expression for fast inactive time stamp matching.")
  502. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  503. "Regular expression for fast time stamp matching.")
  504. (defconst org-ts-regexp0
  505. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  506. "Regular expression matching time strings for analysis.
  507. This one does not require the space after the date, so it can be used
  508. on a string that terminates immediately after the date.")
  509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  510. "Regular expression matching time strings for analysis.")
  511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  512. "Regular expression matching time stamps, with groups.")
  513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  514. "Regular expression matching time stamps (also [..]), with groups.")
  515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  516. "Regular expression matching a time stamp range.")
  517. (defconst org-tr-regexp-both
  518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  519. "Regular expression matching a time stamp range.")
  520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  521. org-ts-regexp "\\)?")
  522. "Regular expression matching a time stamp or time stamp range.")
  523. (defconst org-tsr-regexp-both
  524. (concat org-ts-regexp-both "\\(--?-?"
  525. org-ts-regexp-both "\\)?")
  526. "Regular expression matching a time stamp or time stamp range.
  527. The time stamps may be either active or inactive.")
  528. (defconst org-repeat-re
  529. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  530. "Regular expression for specifying repeated events.
  531. After a match, group 1 contains the repeat expression.")
  532. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  533. "Formats for `format-time-string' which are used for time stamps.")
  534. ;;; The custom variables
  535. (defgroup org nil
  536. "Outline-based notes management and organizer."
  537. :tag "Org"
  538. :group 'outlines
  539. :group 'calendar)
  540. (defcustom org-mode-hook nil
  541. "Mode hook for Org-mode, run after the mode was turned on."
  542. :group 'org
  543. :type 'hook)
  544. (defcustom org-load-hook nil
  545. "Hook that is run after org.el has been loaded."
  546. :group 'org
  547. :type 'hook)
  548. (defcustom org-log-buffer-setup-hook nil
  549. "Hook that is run after an Org log buffer is created."
  550. :group 'org
  551. :version "24.1"
  552. :type 'hook)
  553. (defvar org-modules) ; defined below
  554. (defvar org-modules-loaded nil
  555. "Have the modules been loaded already?")
  556. (defun org-load-modules-maybe (&optional force)
  557. "Load all extensions listed in `org-modules'."
  558. (when (or force (not org-modules-loaded))
  559. (mapc (lambda (ext)
  560. (condition-case nil (require ext)
  561. (error (message "Problems while trying to load feature `%s'" ext))))
  562. org-modules)
  563. (setq org-modules-loaded t)))
  564. (defun org-set-modules (var value)
  565. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  566. (set var value)
  567. (when (featurep 'org)
  568. (org-load-modules-maybe 'force)
  569. (org-element-cache-reset 'all)))
  570. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  571. "Modules that should always be loaded together with org.el.
  572. If a description starts with <C>, the file is not part of Emacs
  573. and loading it will require that you have downloaded and properly
  574. installed the Org mode distribution.
  575. You can also use this system to load external packages (i.e. neither Org
  576. core modules, nor modules from the CONTRIB directory). Just add symbols
  577. to the end of the list. If the package is called org-xyz.el, then you need
  578. to add the symbol `xyz', and the package must have a call to:
  579. \(provide 'org-xyz)
  580. For export specific modules, see also `org-export-backends'."
  581. :group 'org
  582. :set 'org-set-modules
  583. :version "24.4"
  584. :package-version '(Org . "8.0")
  585. :type
  586. '(set :greedy t
  587. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  588. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  589. (const :tag " crypt: Encryption of subtrees" org-crypt)
  590. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  591. (const :tag " docview: Links to doc-view buffers" org-docview)
  592. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  593. (const :tag " habit: Track your consistency with habits" org-habit)
  594. (const :tag " id: Global IDs for identifying entries" org-id)
  595. (const :tag " info: Links to Info nodes" org-info)
  596. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  597. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  598. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  599. (const :tag " mouse: Additional mouse support" org-mouse)
  600. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  601. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  602. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  603. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  604. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  605. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  606. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  607. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  608. (const :tag "C collector: Collect properties into tables" org-collector)
  609. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  610. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  611. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  612. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  613. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  614. (const :tag "C eval: Include command output as text" org-eval)
  615. (const :tag "C eww: Store link to url of eww" org-eww)
  616. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  617. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  618. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  619. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  620. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  621. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  622. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  623. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  624. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  625. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  626. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  627. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  628. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  629. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  630. (const :tag "C registry: A registry for Org-mode links" org-registry)
  631. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  632. (const :tag "C secretary: Team management with org-mode" org-secretary)
  633. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  634. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  635. (const :tag "C track: Keep up with Org-mode development" org-track)
  636. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  637. (const :tag "C vm: Links to VM folders/messages" org-vm)
  638. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  639. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  640. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  641. (defvar org-export-registered-backends) ; From ox.el.
  642. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  643. (declare-function org-export-backend-name "ox" (backend))
  644. (defcustom org-export-backends '(ascii html icalendar latex)
  645. "List of export back-ends that should be always available.
  646. If a description starts with <C>, the file is not part of Emacs
  647. and loading it will require that you have downloaded and properly
  648. installed the Org mode distribution.
  649. Unlike to `org-modules', libraries in this list will not be
  650. loaded along with Org, but only once the export framework is
  651. needed.
  652. This variable needs to be set before org.el is loaded. If you
  653. need to make a change while Emacs is running, use the customize
  654. interface or run the following code, where VAL stands for the new
  655. value of the variable, after updating it:
  656. \(progn
  657. \(setq org-export-registered-backends
  658. \(org-remove-if-not
  659. \(lambda (backend)
  660. \(let ((name (org-export-backend-name backend)))
  661. \(or (memq name val)
  662. \(catch 'parentp
  663. \(dolist (b val)
  664. \(and (org-export-derived-backend-p b name)
  665. \(throw 'parentp t)))))))
  666. org-export-registered-backends))
  667. \(let ((new-list (mapcar #'org-export-backend-name
  668. org-export-registered-backends)))
  669. \(dolist (backend val)
  670. \(cond
  671. \((not (load (format \"ox-%s\" backend) t t))
  672. \(message \"Problems while trying to load export back-end `%s'\"
  673. backend))
  674. \((not (memq backend new-list)) (push backend new-list))))
  675. \(set-default 'org-export-backends new-list)))
  676. Adding a back-end to this list will also pull the back-end it
  677. depends on, if any."
  678. :group 'org
  679. :group 'org-export
  680. :version "24.4"
  681. :package-version '(Org . "8.0")
  682. :initialize 'custom-initialize-set
  683. :set (lambda (var val)
  684. (if (not (featurep 'ox)) (set-default var val)
  685. ;; Any back-end not required anymore (not present in VAL and not
  686. ;; a parent of any back-end in the new value) is removed from the
  687. ;; list of registered back-ends.
  688. (setq org-export-registered-backends
  689. (org-remove-if-not
  690. (lambda (backend)
  691. (let ((name (org-export-backend-name backend)))
  692. (or (memq name val)
  693. (catch 'parentp
  694. (dolist (b val)
  695. (and (org-export-derived-backend-p b name)
  696. (throw 'parentp t)))))))
  697. org-export-registered-backends))
  698. ;; Now build NEW-LIST of both new back-ends and required
  699. ;; parents.
  700. (let ((new-list (mapcar #'org-export-backend-name
  701. org-export-registered-backends)))
  702. (dolist (backend val)
  703. (cond
  704. ((not (load (format "ox-%s" backend) t t))
  705. (message "Problems while trying to load export back-end `%s'"
  706. backend))
  707. ((not (memq backend new-list)) (push backend new-list))))
  708. ;; Set VAR to that list with fixed dependencies.
  709. (set-default var new-list))))
  710. :type '(set :greedy t
  711. (const :tag " ascii Export buffer to ASCII format" ascii)
  712. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  713. (const :tag " html Export buffer to HTML format" html)
  714. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  715. (const :tag " latex Export buffer to LaTeX format" latex)
  716. (const :tag " man Export buffer to MAN format" man)
  717. (const :tag " md Export buffer to Markdown format" md)
  718. (const :tag " odt Export buffer to ODT format" odt)
  719. (const :tag " org Export buffer to Org format" org)
  720. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  721. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  722. (const :tag "C deck Export buffer to deck.js presentations" deck)
  723. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  724. (const :tag "C groff Export buffer to Groff format" groff)
  725. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  726. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  727. (const :tag "C s5 Export buffer to s5 presentations" s5)
  728. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  729. (eval-after-load 'ox
  730. '(mapc
  731. (lambda (backend)
  732. (condition-case nil (require (intern (format "ox-%s" backend)))
  733. (error (message "Problems while trying to load export back-end `%s'"
  734. backend))))
  735. org-export-backends))
  736. (defcustom org-support-shift-select nil
  737. "Non-nil means make shift-cursor commands select text when possible.
  738. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  739. start selecting a region, or enlarge regions started in this way.
  740. In Org-mode, in special contexts, these same keys are used for
  741. other purposes, important enough to compete with shift selection.
  742. Org tries to balance these needs by supporting `shift-select-mode'
  743. outside these special contexts, under control of this variable.
  744. The default of this variable is nil, to avoid confusing behavior. Shifted
  745. cursor keys will then execute Org commands in the following contexts:
  746. - on a headline, changing TODO state (left/right) and priority (up/down)
  747. - on a time stamp, changing the time
  748. - in a plain list item, changing the bullet type
  749. - in a property definition line, switching between allowed values
  750. - in the BEGIN line of a clock table (changing the time block).
  751. Outside these contexts, the commands will throw an error.
  752. When this variable is t and the cursor is not in a special
  753. context, Org-mode will support shift-selection for making and
  754. enlarging regions. To make this more effective, the bullet
  755. cycling will no longer happen anywhere in an item line, but only
  756. if the cursor is exactly on the bullet.
  757. If you set this variable to the symbol `always', then the keys
  758. will not be special in headlines, property lines, and item lines,
  759. to make shift selection work there as well. If this is what you
  760. want, you can use the following alternative commands: `C-c C-t'
  761. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  762. can be used to switch TODO sets, `C-c -' to cycle item bullet
  763. types, and properties can be edited by hand or in column view.
  764. However, when the cursor is on a timestamp, shift-cursor commands
  765. will still edit the time stamp - this is just too good to give up.
  766. XEmacs user should have this variable set to nil, because
  767. `shift-select-mode' is in Emacs 23 or later only."
  768. :group 'org
  769. :type '(choice
  770. (const :tag "Never" nil)
  771. (const :tag "When outside special context" t)
  772. (const :tag "Everywhere except timestamps" always)))
  773. (defcustom org-loop-over-headlines-in-active-region nil
  774. "Shall some commands act upon headlines in the active region?
  775. When set to t, some commands will be performed in all headlines
  776. within the active region.
  777. When set to `start-level', some commands will be performed in all
  778. headlines within the active region, provided that these headlines
  779. are of the same level than the first one.
  780. When set to a string, those commands will be performed on the
  781. matching headlines within the active region. Such string must be
  782. a tags/property/todo match as it is used in the agenda tags view.
  783. The list of commands is: `org-schedule', `org-deadline',
  784. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  785. `org-archive-to-archive-sibling'. The archiving commands skip
  786. already archived entries."
  787. :type '(choice (const :tag "Don't loop" nil)
  788. (const :tag "All headlines in active region" t)
  789. (const :tag "In active region, headlines at the same level than the first one" start-level)
  790. (string :tag "Tags/Property/Todo matcher"))
  791. :version "24.1"
  792. :group 'org-todo
  793. :group 'org-archive)
  794. (defgroup org-startup nil
  795. "Options concerning startup of Org-mode."
  796. :tag "Org Startup"
  797. :group 'org)
  798. (defcustom org-startup-folded t
  799. "Non-nil means entering Org-mode will switch to OVERVIEW.
  800. This can also be configured on a per-file basis by adding one of
  801. the following lines anywhere in the buffer:
  802. #+STARTUP: fold (or `overview', this is equivalent)
  803. #+STARTUP: nofold (or `showall', this is equivalent)
  804. #+STARTUP: content
  805. #+STARTUP: showeverything
  806. By default, this option is ignored when Org opens agenda files
  807. for the first time. If you want the agenda to honor the startup
  808. option, set `org-agenda-inhibit-startup' to nil."
  809. :group 'org-startup
  810. :type '(choice
  811. (const :tag "nofold: show all" nil)
  812. (const :tag "fold: overview" t)
  813. (const :tag "content: all headlines" content)
  814. (const :tag "show everything, even drawers" showeverything)))
  815. (defcustom org-startup-truncated t
  816. "Non-nil means entering Org-mode will set `truncate-lines'.
  817. This is useful since some lines containing links can be very long and
  818. uninteresting. Also tables look terrible when wrapped."
  819. :group 'org-startup
  820. :type 'boolean)
  821. (defcustom org-startup-indented nil
  822. "Non-nil means turn on `org-indent-mode' on startup.
  823. This can also be configured on a per-file basis by adding one of
  824. the following lines anywhere in the buffer:
  825. #+STARTUP: indent
  826. #+STARTUP: noindent"
  827. :group 'org-structure
  828. :type '(choice
  829. (const :tag "Not" nil)
  830. (const :tag "Globally (slow on startup in large files)" t)))
  831. (defcustom org-use-sub-superscripts t
  832. "Non-nil means interpret \"_\" and \"^\" for display.
  833. If you want to control how Org exports those characters, see
  834. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  835. used to be an alias for `org-export-with-sub-superscripts' in
  836. Org <8.0, it is not anymore.
  837. When this option is turned on, you can use TeX-like syntax for
  838. sub- and superscripts within the buffer. Several characters after
  839. \"_\" or \"^\" will be considered as a single item - so grouping
  840. with {} is normally not needed. For example, the following things
  841. will be parsed as single sub- or superscripts:
  842. 10^24 or 10^tau several digits will be considered 1 item.
  843. 10^-12 or 10^-tau a leading sign with digits or a word
  844. x^2-y^3 will be read as x^2 - y^3, because items are
  845. terminated by almost any nonword/nondigit char.
  846. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  847. Still, ambiguity is possible. So when in doubt, use {} to enclose
  848. the sub/superscript. If you set this variable to the symbol `{}',
  849. the braces are *required* in order to trigger interpretations as
  850. sub/superscript. This can be helpful in documents that need \"_\"
  851. frequently in plain text."
  852. :group 'org-startup
  853. :version "24.4"
  854. :package-version '(Org . "8.0")
  855. :type '(choice
  856. (const :tag "Always interpret" t)
  857. (const :tag "Only with braces" {})
  858. (const :tag "Never interpret" nil)))
  859. (defcustom org-startup-with-beamer-mode nil
  860. "Non-nil means turn on `org-beamer-mode' on startup.
  861. This can also be configured on a per-file basis by adding one of
  862. the following lines anywhere in the buffer:
  863. #+STARTUP: beamer"
  864. :group 'org-startup
  865. :version "24.1"
  866. :type 'boolean)
  867. (defcustom org-startup-align-all-tables nil
  868. "Non-nil means align all tables when visiting a file.
  869. This is useful when the column width in tables is forced with <N> cookies
  870. in table fields. Such tables will look correct only after the first re-align.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: align
  874. #+STARTUP: noalign"
  875. :group 'org-startup
  876. :type 'boolean)
  877. (defcustom org-startup-with-inline-images nil
  878. "Non-nil means show inline images when loading a new Org file.
  879. This can also be configured on a per-file basis by adding one of
  880. the following lines anywhere in the buffer:
  881. #+STARTUP: inlineimages
  882. #+STARTUP: noinlineimages"
  883. :group 'org-startup
  884. :version "24.1"
  885. :type 'boolean)
  886. (defcustom org-startup-with-latex-preview nil
  887. "Non-nil means preview LaTeX fragments when loading a new Org file.
  888. This can also be configured on a per-file basis by adding one of
  889. the following lines anywhere in the buffer:
  890. #+STARTUP: latexpreview
  891. #+STARTUP: nolatexpreview"
  892. :group 'org-startup
  893. :version "24.4"
  894. :package-version '(Org . "8.0")
  895. :type 'boolean)
  896. (defcustom org-insert-mode-line-in-empty-file nil
  897. "Non-nil means insert the first line setting Org-mode in empty files.
  898. When the function `org-mode' is called interactively in an empty file, this
  899. normally means that the file name does not automatically trigger Org-mode.
  900. To ensure that the file will always be in Org-mode in the future, a
  901. line enforcing Org-mode will be inserted into the buffer, if this option
  902. has been set."
  903. :group 'org-startup
  904. :type 'boolean)
  905. (defcustom org-replace-disputed-keys nil
  906. "Non-nil means use alternative key bindings for some keys.
  907. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  908. These keys are also used by other packages like shift-selection-mode'
  909. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  910. If you want to use Org-mode together with one of these other modes,
  911. or more generally if you would like to move some Org-mode commands to
  912. other keys, set this variable and configure the keys with the variable
  913. `org-disputed-keys'.
  914. This option is only relevant at load-time of Org-mode, and must be set
  915. *before* org.el is loaded. Changing it requires a restart of Emacs to
  916. become effective."
  917. :group 'org-startup
  918. :type 'boolean)
  919. (defcustom org-use-extra-keys nil
  920. "Non-nil means use extra key sequence definitions for certain commands.
  921. This happens automatically if you run XEmacs or if `window-system'
  922. is nil. This variable lets you do the same manually. You must
  923. set it before loading org.
  924. Example: on Carbon Emacs 22 running graphically, with an external
  925. keyboard on a Powerbook, the default way of setting M-left might
  926. not work for either Alt or ESC. Setting this variable will make
  927. it work for ESC."
  928. :group 'org-startup
  929. :type 'boolean)
  930. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  931. (defcustom org-disputed-keys
  932. '(([(shift up)] . [(meta p)])
  933. ([(shift down)] . [(meta n)])
  934. ([(shift left)] . [(meta -)])
  935. ([(shift right)] . [(meta +)])
  936. ([(control shift right)] . [(meta shift +)])
  937. ([(control shift left)] . [(meta shift -)]))
  938. "Keys for which Org-mode and other modes compete.
  939. This is an alist, cars are the default keys, second element specifies
  940. the alternative to use when `org-replace-disputed-keys' is t.
  941. Keys can be specified in any syntax supported by `define-key'.
  942. The value of this option takes effect only at Org-mode's startup,
  943. therefore you'll have to restart Emacs to apply it after changing."
  944. :group 'org-startup
  945. :type 'alist)
  946. (defun org-key (key)
  947. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  948. Or return the original if not disputed.
  949. Also apply the translations defined in `org-xemacs-key-equivalents'."
  950. (when org-replace-disputed-keys
  951. (let* ((nkey (key-description key))
  952. (x (org-find-if (lambda (x)
  953. (equal (key-description (car x)) nkey))
  954. org-disputed-keys)))
  955. (setq key (if x (cdr x) key))))
  956. (when (featurep 'xemacs)
  957. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  958. key)
  959. (defun org-find-if (predicate seq)
  960. (catch 'exit
  961. (while seq
  962. (if (funcall predicate (car seq))
  963. (throw 'exit (car seq))
  964. (pop seq)))))
  965. (defun org-defkey (keymap key def)
  966. "Define a key, possibly translated, as returned by `org-key'."
  967. (define-key keymap (org-key key) def))
  968. (defcustom org-ellipsis nil
  969. "The ellipsis to use in the Org-mode outline.
  970. When nil, just use the standard three dots.
  971. When a string, use that string instead.
  972. When a face, use the standard 3 dots, but with the specified face.
  973. The change affects only Org-mode (which will then use its own display table).
  974. Changing this requires executing \\[org-mode] in a buffer to become
  975. effective."
  976. :group 'org-startup
  977. :type '(choice (const :tag "Default" nil)
  978. (face :tag "Face" :value org-warning)
  979. (string :tag "String" :value "...#")))
  980. (defvar org-display-table nil
  981. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  982. (defgroup org-keywords nil
  983. "Keywords in Org-mode."
  984. :tag "Org Keywords"
  985. :group 'org)
  986. (defcustom org-closed-keep-when-no-todo nil
  987. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  988. :group 'org-todo
  989. :group 'org-keywords
  990. :version "24.4"
  991. :package-version '(Org . "8.0")
  992. :type 'boolean)
  993. (defgroup org-structure nil
  994. "Options concerning the general structure of Org-mode files."
  995. :tag "Org Structure"
  996. :group 'org)
  997. (defgroup org-reveal-location nil
  998. "Options about how to make context of a location visible."
  999. :tag "Org Reveal Location"
  1000. :group 'org-structure)
  1001. (defcustom org-show-context-detail '((isearch . lineage)
  1002. (bookmark-jump . lineage)
  1003. (default . ancestors))
  1004. "Alist between context and visibility span when revealing a location.
  1005. \\<org-mode-map>Some actions may move point into invisible
  1006. locations. As a consequence, Org always expose a neighborhood
  1007. around point. How much is shown depends on the initial action,
  1008. or context. Valid contexts are
  1009. agenda when exposing an entry from the agenda
  1010. org-goto when using the command `org-goto' (\\[org-goto])
  1011. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1012. tags-tree when constructing a sparse tree based on tags matches
  1013. link-search when exposing search matches associated with a link
  1014. mark-goto when exposing the jump goal of a mark
  1015. bookmark-jump when exposing a bookmark location
  1016. isearch when exiting from an incremental search
  1017. default default for all contexts not set explicitly
  1018. Allowed visibility spans are
  1019. minimal show current headline; if point is not on headline,
  1020. also show entry
  1021. local show current headline, entry and next headline
  1022. ancestors show current headline and its direct ancestors; if
  1023. point is not on headline, also show entry
  1024. lineage show current headline, its direct ancestors and all
  1025. their children; if point is not on headline, also show
  1026. entry and first child
  1027. tree show current headline, its direct ancestors and all
  1028. their children; if point is not on headline, also show
  1029. entry and all children
  1030. canonical show current headline, its direct ancestors along with
  1031. their entries and children; if point is not located on
  1032. the headline, also show current entry and all children
  1033. As special cases, a nil or t value means show all contexts in
  1034. `minimal' or `canonical' view, respectively.
  1035. Some views can make displayed information very compact, but also
  1036. make it harder to edit the location of the match. In such
  1037. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1038. more context."
  1039. :group 'org-reveal-location
  1040. :version "25.1"
  1041. :package-version '(Org . "8.3")
  1042. :type '(choice
  1043. (const :tag "Canonical" t)
  1044. (const :tag "Minimal" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const agenda)
  1049. (const org-goto)
  1050. (const occur-tree)
  1051. (const tags-tree)
  1052. (const link-search)
  1053. (const mark-goto)
  1054. (const bookmark-jump)
  1055. (const isearch)
  1056. (const default))
  1057. (choice :tag "Detail level"
  1058. (const minimal)
  1059. (const local)
  1060. (const ancestors)
  1061. (const lineage)
  1062. (const tree)
  1063. (const canonical))))))
  1064. (defcustom org-indirect-buffer-display 'other-window
  1065. "How should indirect tree buffers be displayed?
  1066. This applies to indirect buffers created with the commands
  1067. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1068. Valid values are:
  1069. current-window Display in the current window
  1070. other-window Just display in another window.
  1071. dedicated-frame Create one new frame, and re-use it each time.
  1072. new-frame Make a new frame each time. Note that in this case
  1073. previously-made indirect buffers are kept, and you need to
  1074. kill these buffers yourself."
  1075. :group 'org-structure
  1076. :group 'org-agenda-windows
  1077. :type '(choice
  1078. (const :tag "In current window" current-window)
  1079. (const :tag "In current frame, other window" other-window)
  1080. (const :tag "Each time a new frame" new-frame)
  1081. (const :tag "One dedicated frame" dedicated-frame)))
  1082. (defcustom org-use-speed-commands nil
  1083. "Non-nil means activate single letter commands at beginning of a headline.
  1084. This may also be a function to test for appropriate locations where speed
  1085. commands should be active.
  1086. For example, to activate speed commands when the point is on any
  1087. star at the beginning of the headline, you can do this:
  1088. (setq org-use-speed-commands
  1089. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1090. :group 'org-structure
  1091. :type '(choice
  1092. (const :tag "Never" nil)
  1093. (const :tag "At beginning of headline stars" t)
  1094. (function)))
  1095. (defcustom org-speed-commands-user nil
  1096. "Alist of additional speed commands.
  1097. This list will be checked before `org-speed-commands-default'
  1098. when the variable `org-use-speed-commands' is non-nil
  1099. and when the cursor is at the beginning of a headline.
  1100. The car if each entry is a string with a single letter, which must
  1101. be assigned to `self-insert-command' in the global map.
  1102. The cdr is either a command to be called interactively, a function
  1103. to be called, or a form to be evaluated.
  1104. An entry that is just a list with a single string will be interpreted
  1105. as a descriptive headline that will be added when listing the speed
  1106. commands in the Help buffer using the `?' speed command."
  1107. :group 'org-structure
  1108. :type '(repeat :value ("k" . ignore)
  1109. (choice :value ("k" . ignore)
  1110. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1111. (cons :tag "Letter and Command"
  1112. (string :tag "Command letter")
  1113. (choice
  1114. (function)
  1115. (sexp))))))
  1116. (defcustom org-bookmark-names-plist
  1117. '(:last-capture "org-capture-last-stored"
  1118. :last-refile "org-refile-last-stored"
  1119. :last-capture-marker "org-capture-last-stored-marker")
  1120. "Names for bookmarks automatically set by some Org commands.
  1121. This can provide strings as names for a number of bookmarks Org sets
  1122. automatically. The following keys are currently implemented:
  1123. :last-capture
  1124. :last-capture-marker
  1125. :last-refile
  1126. When a key does not show up in the property list, the corresponding bookmark
  1127. is not set."
  1128. :group 'org-structure
  1129. :type 'plist)
  1130. (defgroup org-cycle nil
  1131. "Options concerning visibility cycling in Org-mode."
  1132. :tag "Org Cycle"
  1133. :group 'org-structure)
  1134. (defcustom org-cycle-skip-children-state-if-no-children t
  1135. "Non-nil means skip CHILDREN state in entries that don't have any."
  1136. :group 'org-cycle
  1137. :type 'boolean)
  1138. (defcustom org-cycle-max-level nil
  1139. "Maximum level which should still be subject to visibility cycling.
  1140. Levels higher than this will, for cycling, be treated as text, not a headline.
  1141. When `org-odd-levels-only' is set, a value of N in this variable actually
  1142. means 2N-1 stars as the limiting headline.
  1143. When nil, cycle all levels.
  1144. Note that the limiting level of cycling is also influenced by
  1145. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1146. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1147. than its value."
  1148. :group 'org-cycle
  1149. :type '(choice
  1150. (const :tag "No limit" nil)
  1151. (integer :tag "Maximum level")))
  1152. (defcustom org-hide-block-startup nil
  1153. "Non-nil means entering Org-mode will fold all blocks.
  1154. This can also be set in on a per-file basis with
  1155. #+STARTUP: hideblocks
  1156. #+STARTUP: showblocks"
  1157. :group 'org-startup
  1158. :group 'org-cycle
  1159. :type 'boolean)
  1160. (defcustom org-cycle-global-at-bob nil
  1161. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1162. This makes it possible to do global cycling without having to use S-TAB or
  1163. \\[universal-argument] TAB. For this special case to work, the first line
  1164. of the buffer must not be a headline -- it may be empty or some other text.
  1165. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1166. sure the cursor stays at the beginning of the buffer. When this option is
  1167. nil, don't do anything special at the beginning of the buffer."
  1168. :group 'org-cycle
  1169. :type 'boolean)
  1170. (defcustom org-cycle-level-after-item/entry-creation t
  1171. "Non-nil means cycle entry level or item indentation in new empty entries.
  1172. When the cursor is at the end of an empty headline, i.e., with only stars
  1173. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1174. and then all possible ancestor states, before returning to the original state.
  1175. This makes data entry extremely fast: M-RET to create a new headline,
  1176. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1177. When the cursor is at the end of an empty plain list item, one TAB will
  1178. make it a subitem, two or more tabs will back up to make this an item
  1179. higher up in the item hierarchy."
  1180. :group 'org-cycle
  1181. :type 'boolean)
  1182. (defcustom org-cycle-emulate-tab t
  1183. "Where should `org-cycle' emulate TAB.
  1184. nil Never
  1185. white Only in completely white lines
  1186. whitestart Only at the beginning of lines, before the first non-white char
  1187. t Everywhere except in headlines
  1188. exc-hl-bol Everywhere except at the start of a headline
  1189. If TAB is used in a place where it does not emulate TAB, the current subtree
  1190. visibility is cycled."
  1191. :group 'org-cycle
  1192. :type '(choice (const :tag "Never" nil)
  1193. (const :tag "Only in completely white lines" white)
  1194. (const :tag "Before first char in a line" whitestart)
  1195. (const :tag "Everywhere except in headlines" t)
  1196. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1197. (defcustom org-cycle-separator-lines 2
  1198. "Number of empty lines needed to keep an empty line between collapsed trees.
  1199. If you leave an empty line between the end of a subtree and the following
  1200. headline, this empty line is hidden when the subtree is folded.
  1201. Org-mode will leave (exactly) one empty line visible if the number of
  1202. empty lines is equal or larger to the number given in this variable.
  1203. So the default 2 means at least 2 empty lines after the end of a subtree
  1204. are needed to produce free space between a collapsed subtree and the
  1205. following headline.
  1206. If the number is negative, and the number of empty lines is at least -N,
  1207. all empty lines are shown.
  1208. Special case: when 0, never leave empty lines in collapsed view."
  1209. :group 'org-cycle
  1210. :type 'integer)
  1211. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1212. (defcustom org-pre-cycle-hook nil
  1213. "Hook that is run before visibility cycling is happening.
  1214. The function(s) in this hook must accept a single argument which indicates
  1215. the new state that will be set right after running this hook. The
  1216. argument is a symbol. Before a global state change, it can have the values
  1217. `overview', `content', or `all'. Before a local state change, it can have
  1218. the values `folded', `children', or `subtree'."
  1219. :group 'org-cycle
  1220. :type 'hook)
  1221. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1222. org-cycle-hide-drawers
  1223. org-cycle-show-empty-lines
  1224. org-optimize-window-after-visibility-change)
  1225. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1226. The function(s) in this hook must accept a single argument which indicates
  1227. the new state that was set by the most recent `org-cycle' command. The
  1228. argument is a symbol. After a global state change, it can have the values
  1229. `overview', `contents', or `all'. After a local state change, it can have
  1230. the values `folded', `children', or `subtree'."
  1231. :group 'org-cycle
  1232. :type 'hook
  1233. :version "25.1"
  1234. :package-version '(Org . "8.3"))
  1235. (defgroup org-edit-structure nil
  1236. "Options concerning structure editing in Org-mode."
  1237. :tag "Org Edit Structure"
  1238. :group 'org-structure)
  1239. (defcustom org-odd-levels-only nil
  1240. "Non-nil means skip even levels and only use odd levels for the outline.
  1241. This has the effect that two stars are being added/taken away in
  1242. promotion/demotion commands. It also influences how levels are
  1243. handled by the exporters.
  1244. Changing it requires restart of `font-lock-mode' to become effective
  1245. for fontification also in regions already fontified.
  1246. You may also set this on a per-file basis by adding one of the following
  1247. lines to the buffer:
  1248. #+STARTUP: odd
  1249. #+STARTUP: oddeven"
  1250. :group 'org-edit-structure
  1251. :group 'org-appearance
  1252. :type 'boolean)
  1253. (defcustom org-adapt-indentation t
  1254. "Non-nil means adapt indentation to outline node level.
  1255. When this variable is set, Org assumes that you write outlines by
  1256. indenting text in each node to align with the headline (after the
  1257. stars). The following issues are influenced by this variable:
  1258. - The indentation is increased by one space in a demotion
  1259. command, and decreased by one in a promotion command. However,
  1260. in the latter case, if shifting some line in the entry body
  1261. would alter document structure (e.g., insert a new headline),
  1262. indentation is not changed at all.
  1263. - Property drawers and planning information is inserted indented
  1264. when this variable is set. When nil, they will not be indented.
  1265. - TAB indents a line relative to current level. The lines below
  1266. a headline will be indented when this variable is set.
  1267. Note that this is all about true indentation, by adding and
  1268. removing space characters. See also `org-indent.el' which does
  1269. level-dependent indentation in a virtual way, i.e. at display
  1270. time in Emacs."
  1271. :group 'org-edit-structure
  1272. :type 'boolean)
  1273. (defcustom org-special-ctrl-a/e nil
  1274. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1275. When t, `C-a' will bring back the cursor to the beginning of the
  1276. headline text, i.e. after the stars and after a possible TODO
  1277. keyword. In an item, this will be the position after bullet and
  1278. check-box, if any. When the cursor is already at that position,
  1279. another `C-a' will bring it to the beginning of the line.
  1280. `C-e' will jump to the end of the headline, ignoring the presence
  1281. of tags in the headline. A second `C-e' will then jump to the
  1282. true end of the line, after any tags. This also means that, when
  1283. this variable is non-nil, `C-e' also will never jump beyond the
  1284. end of the heading of a folded section, i.e. not after the
  1285. ellipses.
  1286. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1287. normally, going to the true line boundary first. Only a directly
  1288. following, identical keypress will bring the cursor to the
  1289. special positions.
  1290. This may also be a cons cell where the behavior for `C-a' and
  1291. `C-e' is set separately."
  1292. :group 'org-edit-structure
  1293. :type '(choice
  1294. (const :tag "off" nil)
  1295. (const :tag "on: after stars/bullet and before tags first" t)
  1296. (const :tag "reversed: true line boundary first" reversed)
  1297. (cons :tag "Set C-a and C-e separately"
  1298. (choice :tag "Special C-a"
  1299. (const :tag "off" nil)
  1300. (const :tag "on: after stars/bullet first" t)
  1301. (const :tag "reversed: before stars/bullet first" reversed))
  1302. (choice :tag "Special C-e"
  1303. (const :tag "off" nil)
  1304. (const :tag "on: before tags first" t)
  1305. (const :tag "reversed: after tags first" reversed)))))
  1306. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1307. (defcustom org-special-ctrl-k nil
  1308. "Non-nil means `C-k' will behave specially in headlines.
  1309. When nil, `C-k' will call the default `kill-line' command.
  1310. When t, the following will happen while the cursor is in the headline:
  1311. - When the cursor is at the beginning of a headline, kill the entire
  1312. line and possible the folded subtree below the line.
  1313. - When in the middle of the headline text, kill the headline up to the tags.
  1314. - When after the headline text, kill the tags."
  1315. :group 'org-edit-structure
  1316. :type 'boolean)
  1317. (defcustom org-ctrl-k-protect-subtree nil
  1318. "Non-nil means, do not delete a hidden subtree with C-k.
  1319. When set to the symbol `error', simply throw an error when C-k is
  1320. used to kill (part-of) a headline that has hidden text behind it.
  1321. Any other non-nil value will result in a query to the user, if it is
  1322. OK to kill that hidden subtree. When nil, kill without remorse."
  1323. :group 'org-edit-structure
  1324. :version "24.1"
  1325. :type '(choice
  1326. (const :tag "Do not protect hidden subtrees" nil)
  1327. (const :tag "Protect hidden subtrees with a security query" t)
  1328. (const :tag "Never kill a hidden subtree with C-k" error)))
  1329. (defcustom org-special-ctrl-o t
  1330. "Non-nil means, make `C-o' insert a row in tables."
  1331. :group 'org-edit-structure
  1332. :type 'boolean)
  1333. (defcustom org-catch-invisible-edits nil
  1334. "Check if in invisible region before inserting or deleting a character.
  1335. Valid values are:
  1336. nil Do not check, so just do invisible edits.
  1337. error Throw an error and do nothing.
  1338. show Make point visible, and do the requested edit.
  1339. show-and-error Make point visible, then throw an error and abort the edit.
  1340. smart Make point visible, and do insertion/deletion if it is
  1341. adjacent to visible text and the change feels predictable.
  1342. Never delete a previously invisible character or add in the
  1343. middle or right after an invisible region. Basically, this
  1344. allows insertion and backward-delete right before ellipses.
  1345. FIXME: maybe in this case we should not even show?"
  1346. :group 'org-edit-structure
  1347. :version "24.1"
  1348. :type '(choice
  1349. (const :tag "Do not check" nil)
  1350. (const :tag "Throw error when trying to edit" error)
  1351. (const :tag "Unhide, but do not do the edit" show-and-error)
  1352. (const :tag "Show invisible part and do the edit" show)
  1353. (const :tag "Be smart and do the right thing" smart)))
  1354. (defcustom org-yank-folded-subtrees t
  1355. "Non-nil means when yanking subtrees, fold them.
  1356. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1357. it starts with a heading and all other headings in it are either children
  1358. or siblings, then fold all the subtrees. However, do this only if no
  1359. text after the yank would be swallowed into a folded tree by this action."
  1360. :group 'org-edit-structure
  1361. :type 'boolean)
  1362. (defcustom org-yank-adjusted-subtrees nil
  1363. "Non-nil means when yanking subtrees, adjust the level.
  1364. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1365. this function for details."
  1366. :group 'org-edit-structure
  1367. :type 'boolean)
  1368. (defcustom org-M-RET-may-split-line '((default . t))
  1369. "Non-nil means M-RET will split the line at the cursor position.
  1370. When nil, it will go to the end of the line before making a
  1371. new line.
  1372. You may also set this option in a different way for different
  1373. contexts. Valid contexts are:
  1374. headline when creating a new headline
  1375. item when creating a new item
  1376. table in a table field
  1377. default the value to be used for all contexts not explicitly
  1378. customized"
  1379. :group 'org-structure
  1380. :group 'org-table
  1381. :type '(choice
  1382. (const :tag "Always" t)
  1383. (const :tag "Never" nil)
  1384. (repeat :greedy t :tag "Individual contexts"
  1385. (cons
  1386. (choice :tag "Context"
  1387. (const headline)
  1388. (const item)
  1389. (const table)
  1390. (const default))
  1391. (boolean)))))
  1392. (defcustom org-insert-heading-respect-content nil
  1393. "Non-nil means insert new headings after the current subtree.
  1394. When nil, the new heading is created directly after the current line.
  1395. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1396. this variable on for the duration of the command."
  1397. :group 'org-structure
  1398. :type 'boolean)
  1399. (defcustom org-blank-before-new-entry '((heading . auto)
  1400. (plain-list-item . auto))
  1401. "Should `org-insert-heading' leave a blank line before new heading/item?
  1402. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1403. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1404. which case Org will look at the surrounding headings/items and try to
  1405. make an intelligent decision whether to insert a blank line or not.
  1406. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1407. the setting here is ignored and no empty line is inserted to avoid breaking
  1408. the list structure."
  1409. :group 'org-edit-structure
  1410. :type '(list
  1411. (cons (const heading)
  1412. (choice (const :tag "Never" nil)
  1413. (const :tag "Always" t)
  1414. (const :tag "Auto" auto)))
  1415. (cons (const plain-list-item)
  1416. (choice (const :tag "Never" nil)
  1417. (const :tag "Always" t)
  1418. (const :tag "Auto" auto)))))
  1419. (defcustom org-insert-heading-hook nil
  1420. "Hook being run after inserting a new heading."
  1421. :group 'org-edit-structure
  1422. :type 'hook)
  1423. (defcustom org-enable-fixed-width-editor t
  1424. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1425. This currently only means they are never auto-wrapped.
  1426. When nil, such lines will be treated like ordinary lines."
  1427. :group 'org-edit-structure
  1428. :type 'boolean)
  1429. (defcustom org-goto-auto-isearch t
  1430. "Non-nil means typing characters in `org-goto' starts incremental search.
  1431. When nil, you can use these keybindings to navigate the buffer:
  1432. q Quit the org-goto interface
  1433. n Go to the next visible heading
  1434. p Go to the previous visible heading
  1435. f Go one heading forward on same level
  1436. b Go one heading backward on same level
  1437. u Go one heading up"
  1438. :group 'org-edit-structure
  1439. :type 'boolean)
  1440. (defgroup org-sparse-trees nil
  1441. "Options concerning sparse trees in Org-mode."
  1442. :tag "Org Sparse Trees"
  1443. :group 'org-structure)
  1444. (defcustom org-highlight-sparse-tree-matches t
  1445. "Non-nil means highlight all matches that define a sparse tree.
  1446. The highlights will automatically disappear the next time the buffer is
  1447. changed by an edit command."
  1448. :group 'org-sparse-trees
  1449. :type 'boolean)
  1450. (defcustom org-remove-highlights-with-change t
  1451. "Non-nil means any change to the buffer will remove temporary highlights.
  1452. Such highlights are created by `org-occur' and `org-clock-display'.
  1453. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1454. The highlights created by `org-toggle-latex-fragment' always need
  1455. `C-c C-x C-l' to be removed."
  1456. :group 'org-sparse-trees
  1457. :group 'org-time
  1458. :type 'boolean)
  1459. (defcustom org-occur-hook '(org-first-headline-recenter)
  1460. "Hook that is run after `org-occur' has constructed a sparse tree.
  1461. This can be used to recenter the window to show as much of the structure
  1462. as possible."
  1463. :group 'org-sparse-trees
  1464. :type 'hook)
  1465. (defgroup org-imenu-and-speedbar nil
  1466. "Options concerning imenu and speedbar in Org-mode."
  1467. :tag "Org Imenu and Speedbar"
  1468. :group 'org-structure)
  1469. (defcustom org-imenu-depth 2
  1470. "The maximum level for Imenu access to Org-mode headlines.
  1471. This also applied for speedbar access."
  1472. :group 'org-imenu-and-speedbar
  1473. :type 'integer)
  1474. (defgroup org-table nil
  1475. "Options concerning tables in Org-mode."
  1476. :tag "Org Table"
  1477. :group 'org)
  1478. (defcustom org-enable-table-editor 'optimized
  1479. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1480. When nil, such lines will be treated like ordinary lines.
  1481. When equal to the symbol `optimized', the table editor will be optimized to
  1482. do the following:
  1483. - Automatic overwrite mode in front of whitespace in table fields.
  1484. This makes the structure of the table stay in tact as long as the edited
  1485. field does not exceed the column width.
  1486. - Minimize the number of realigns. Normally, the table is aligned each time
  1487. TAB or RET are pressed to move to another field. With optimization this
  1488. happens only if changes to a field might have changed the column width.
  1489. Optimization requires replacing the functions `self-insert-command',
  1490. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1491. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1492. very good at guessing when a re-align will be necessary, but you can always
  1493. force one with \\[org-ctrl-c-ctrl-c].
  1494. If you would like to use the optimized version in Org-mode, but the
  1495. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1496. This variable can be used to turn on and off the table editor during a session,
  1497. but in order to toggle optimization, a restart is required.
  1498. See also the variable `org-table-auto-blank-field'."
  1499. :group 'org-table
  1500. :type '(choice
  1501. (const :tag "off" nil)
  1502. (const :tag "on" t)
  1503. (const :tag "on, optimized" optimized)))
  1504. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1505. (version<= emacs-version "24.1"))
  1506. "Non-nil means cluster self-insert commands for undo when possible.
  1507. If this is set, then, like in the Emacs command loop, 20 consecutive
  1508. characters will be undone together.
  1509. This is configurable, because there is some impact on typing performance."
  1510. :group 'org-table
  1511. :type 'boolean)
  1512. (defcustom org-table-tab-recognizes-table.el t
  1513. "Non-nil means TAB will automatically notice a table.el table.
  1514. When it sees such a table, it moves point into it and - if necessary -
  1515. calls `table-recognize-table'."
  1516. :group 'org-table-editing
  1517. :type 'boolean)
  1518. (defgroup org-link nil
  1519. "Options concerning links in Org-mode."
  1520. :tag "Org Link"
  1521. :group 'org)
  1522. (defvar org-link-abbrev-alist-local nil
  1523. "Buffer-local version of `org-link-abbrev-alist', which see.
  1524. The value of this is taken from the #+LINK lines.")
  1525. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1526. (defcustom org-link-abbrev-alist nil
  1527. "Alist of link abbreviations.
  1528. The car of each element is a string, to be replaced at the start of a link.
  1529. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1530. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1531. [[linkkey:tag][description]]
  1532. The 'linkkey' must be a word word, starting with a letter, followed
  1533. by letters, numbers, '-' or '_'.
  1534. If REPLACE is a string, the tag will simply be appended to create the link.
  1535. If the string contains \"%s\", the tag will be inserted there. If the string
  1536. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1537. at that point (see the function `url-hexify-string'). If the string contains
  1538. the specifier \"%(my-function)\", then the custom function `my-function' will
  1539. be invoked: this function takes the tag as its only argument and must return
  1540. a string.
  1541. REPLACE may also be a function that will be called with the tag as the
  1542. only argument to create the link, which should be returned as a string.
  1543. See the manual for examples."
  1544. :group 'org-link
  1545. :type '(repeat
  1546. (cons
  1547. (string :tag "Protocol")
  1548. (choice
  1549. (string :tag "Format")
  1550. (function)))))
  1551. (defcustom org-descriptive-links t
  1552. "Non-nil means Org will display descriptive links.
  1553. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1554. \"Org Website\", hiding the link itself and just displaying its
  1555. description. When set to nil, Org will display the full links
  1556. literally.
  1557. You can interactively set the value of this variable by calling
  1558. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1559. :group 'org-link
  1560. :type 'boolean)
  1561. (defcustom org-link-file-path-type 'adaptive
  1562. "How the path name in file links should be stored.
  1563. Valid values are:
  1564. relative Relative to the current directory, i.e. the directory of the file
  1565. into which the link is being inserted.
  1566. absolute Absolute path, if possible with ~ for home directory.
  1567. noabbrev Absolute path, no abbreviation of home directory.
  1568. adaptive Use relative path for files in the current directory and sub-
  1569. directories of it. For other files, use an absolute path."
  1570. :group 'org-link
  1571. :type '(choice
  1572. (const relative)
  1573. (const absolute)
  1574. (const noabbrev)
  1575. (const adaptive)))
  1576. (defvaralias 'org-activate-links 'org-highlight-links)
  1577. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1578. "Types of links that should be highlighted in Org-mode files.
  1579. This is a list of symbols, each one of them leading to the
  1580. highlighting of a certain link type.
  1581. You can still open links that are not highlighted.
  1582. In principle, it does not hurt to turn on highlighting for all
  1583. link types. There may be a small gain when turning off unused
  1584. link types. The types are:
  1585. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1586. angle Links in angular brackets that may contain whitespace like
  1587. <bbdb:Carsten Dominik>.
  1588. plain Plain links in normal text, no whitespace, like http://google.com.
  1589. radio Text that is matched by a radio target, see manual for details.
  1590. tag Tag settings in a headline (link to tag search).
  1591. date Time stamps (link to calendar).
  1592. footnote Footnote labels.
  1593. If you set this variable during an Emacs session, use `org-mode-restart'
  1594. in the Org buffer so that the change takes effect."
  1595. :group 'org-link
  1596. :group 'org-appearance
  1597. :type '(set :greedy t
  1598. (const :tag "Double bracket links" bracket)
  1599. (const :tag "Angular bracket links" angle)
  1600. (const :tag "Plain text links" plain)
  1601. (const :tag "Radio target matches" radio)
  1602. (const :tag "Tags" tag)
  1603. (const :tag "Timestamps" date)
  1604. (const :tag "Footnotes" footnote)))
  1605. (defcustom org-make-link-description-function nil
  1606. "Function to use for generating link descriptions from links.
  1607. When nil, the link location will be used. This function must take
  1608. two parameters: the first one is the link, the second one is the
  1609. description generated by `org-insert-link'. The function should
  1610. return the description to use."
  1611. :group 'org-link
  1612. :type '(choice (const nil) (function)))
  1613. (defgroup org-link-store nil
  1614. "Options concerning storing links in Org-mode."
  1615. :tag "Org Store Link"
  1616. :group 'org-link)
  1617. (defcustom org-url-hexify-p t
  1618. "When non-nil, hexify URL when creating a link."
  1619. :type 'boolean
  1620. :version "24.3"
  1621. :group 'org-link-store)
  1622. (defcustom org-email-link-description-format "Email %c: %.30s"
  1623. "Format of the description part of a link to an email or usenet message.
  1624. The following %-escapes will be replaced by corresponding information:
  1625. %F full \"From\" field
  1626. %f name, taken from \"From\" field, address if no name
  1627. %T full \"To\" field
  1628. %t first name in \"To\" field, address if no name
  1629. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1630. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1631. %s subject
  1632. %d date
  1633. %m message-id.
  1634. You may use normal field width specification between the % and the letter.
  1635. This is for example useful to limit the length of the subject.
  1636. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1637. :group 'org-link-store
  1638. :type 'string)
  1639. (defcustom org-from-is-user-regexp
  1640. (let (r1 r2)
  1641. (when (and user-mail-address (not (string= user-mail-address "")))
  1642. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1643. (when (and user-full-name (not (string= user-full-name "")))
  1644. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1645. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1646. "Regexp matched against the \"From:\" header of an email or usenet message.
  1647. It should match if the message is from the user him/herself."
  1648. :group 'org-link-store
  1649. :type 'regexp)
  1650. (defcustom org-context-in-file-links t
  1651. "Non-nil means file links from `org-store-link' contain context.
  1652. A search string will be added to the file name with :: as separator and
  1653. used to find the context when the link is activated by the command
  1654. `org-open-at-point'. When this option is t, the entire active region
  1655. will be placed in the search string of the file link. If set to a
  1656. positive integer, only the first n lines of context will be stored.
  1657. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1658. negates this setting for the duration of the command."
  1659. :group 'org-link-store
  1660. :type '(choice boolean integer))
  1661. (defcustom org-keep-stored-link-after-insertion nil
  1662. "Non-nil means keep link in list for entire session.
  1663. The command `org-store-link' adds a link pointing to the current
  1664. location to an internal list. These links accumulate during a session.
  1665. The command `org-insert-link' can be used to insert links into any
  1666. Org-mode file (offering completion for all stored links). When this
  1667. option is nil, every link which has been inserted once using \\[org-insert-link]
  1668. will be removed from the list, to make completing the unused links
  1669. more efficient."
  1670. :group 'org-link-store
  1671. :type 'boolean)
  1672. (defgroup org-link-follow nil
  1673. "Options concerning following links in Org-mode."
  1674. :tag "Org Follow Link"
  1675. :group 'org-link)
  1676. (defcustom org-link-translation-function nil
  1677. "Function to translate links with different syntax to Org syntax.
  1678. This can be used to translate links created for example by the Planner
  1679. or emacs-wiki packages to Org syntax.
  1680. The function must accept two parameters, a TYPE containing the link
  1681. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1682. which is everything after the link protocol. It should return a cons
  1683. with possibly modified values of type and path.
  1684. Org contains a function for this, so if you set this variable to
  1685. `org-translate-link-from-planner', you should be able follow many
  1686. links created by planner."
  1687. :group 'org-link-follow
  1688. :type '(choice (const nil) (function)))
  1689. (defcustom org-follow-link-hook nil
  1690. "Hook that is run after a link has been followed."
  1691. :group 'org-link-follow
  1692. :type 'hook)
  1693. (defcustom org-tab-follows-link nil
  1694. "Non-nil means on links TAB will follow the link.
  1695. Needs to be set before org.el is loaded.
  1696. This really should not be used, it does not make sense, and the
  1697. implementation is bad."
  1698. :group 'org-link-follow
  1699. :type 'boolean)
  1700. (defcustom org-return-follows-link nil
  1701. "Non-nil means on links RET will follow the link.
  1702. In tables, the special behavior of RET has precedence."
  1703. :group 'org-link-follow
  1704. :type 'boolean)
  1705. (defcustom org-mouse-1-follows-link
  1706. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1707. "Non-nil means mouse-1 on a link will follow the link.
  1708. A longer mouse click will still set point. Does not work on XEmacs.
  1709. Needs to be set before org.el is loaded."
  1710. :group 'org-link-follow
  1711. :version "24.4"
  1712. :package-version '(Org . "8.3")
  1713. :type '(choice
  1714. (const :tag "A double click follows the link" double)
  1715. (const :tag "Unconditionally follow the link with mouse-1" t)
  1716. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1717. (defcustom org-mark-ring-length 4
  1718. "Number of different positions to be recorded in the ring.
  1719. Changing this requires a restart of Emacs to work correctly."
  1720. :group 'org-link-follow
  1721. :type 'integer)
  1722. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1723. "Non-nil means internal links in Org files must exactly match a headline.
  1724. When nil, the link search tries to match a phrase with all words
  1725. in the search text."
  1726. :group 'org-link-follow
  1727. :version "24.1"
  1728. :type '(choice
  1729. (const :tag "Use fuzzy text search" nil)
  1730. (const :tag "Match only exact headline" t)
  1731. (const :tag "Match exact headline or query to create it"
  1732. query-to-create)))
  1733. (defcustom org-link-frame-setup
  1734. '((vm . vm-visit-folder-other-frame)
  1735. (vm-imap . vm-visit-imap-folder-other-frame)
  1736. (gnus . org-gnus-no-new-news)
  1737. (file . find-file-other-window)
  1738. (wl . wl-other-frame))
  1739. "Setup the frame configuration for following links.
  1740. When following a link with Emacs, it may often be useful to display
  1741. this link in another window or frame. This variable can be used to
  1742. set this up for the different types of links.
  1743. For VM, use any of
  1744. `vm-visit-folder'
  1745. `vm-visit-folder-other-window'
  1746. `vm-visit-folder-other-frame'
  1747. For Gnus, use any of
  1748. `gnus'
  1749. `gnus-other-frame'
  1750. `org-gnus-no-new-news'
  1751. For FILE, use any of
  1752. `find-file'
  1753. `find-file-other-window'
  1754. `find-file-other-frame'
  1755. For Wanderlust use any of
  1756. `wl'
  1757. `wl-other-frame'
  1758. For the calendar, use the variable `calendar-setup'.
  1759. For BBDB, it is currently only possible to display the matches in
  1760. another window."
  1761. :group 'org-link-follow
  1762. :type '(list
  1763. (cons (const vm)
  1764. (choice
  1765. (const vm-visit-folder)
  1766. (const vm-visit-folder-other-window)
  1767. (const vm-visit-folder-other-frame)))
  1768. (cons (const vm-imap)
  1769. (choice
  1770. (const vm-visit-imap-folder)
  1771. (const vm-visit-imap-folder-other-window)
  1772. (const vm-visit-imap-folder-other-frame)))
  1773. (cons (const gnus)
  1774. (choice
  1775. (const gnus)
  1776. (const gnus-other-frame)
  1777. (const org-gnus-no-new-news)))
  1778. (cons (const file)
  1779. (choice
  1780. (const find-file)
  1781. (const find-file-other-window)
  1782. (const find-file-other-frame)))
  1783. (cons (const wl)
  1784. (choice
  1785. (const wl)
  1786. (const wl-other-frame)))))
  1787. (defcustom org-display-internal-link-with-indirect-buffer nil
  1788. "Non-nil means use indirect buffer to display infile links.
  1789. Activating internal links (from one location in a file to another location
  1790. in the same file) normally just jumps to the location. When the link is
  1791. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1792. is displayed in
  1793. another window. When this option is set, the other window actually displays
  1794. an indirect buffer clone of the current buffer, to avoid any visibility
  1795. changes to the current buffer."
  1796. :group 'org-link-follow
  1797. :type 'boolean)
  1798. (defcustom org-open-non-existing-files nil
  1799. "Non-nil means `org-open-file' will open non-existing files.
  1800. When nil, an error will be generated.
  1801. This variable applies only to external applications because they
  1802. might choke on non-existing files. If the link is to a file that
  1803. will be opened in Emacs, the variable is ignored."
  1804. :group 'org-link-follow
  1805. :type 'boolean)
  1806. (defcustom org-open-directory-means-index-dot-org nil
  1807. "Non-nil means a link to a directory really means to index.org.
  1808. When nil, following a directory link will run dired or open a finder/explorer
  1809. window on that directory."
  1810. :group 'org-link-follow
  1811. :type 'boolean)
  1812. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1813. "Non-nil means ask for confirmation before executing shell links.
  1814. Shell links can be dangerous: just think about a link
  1815. [[shell:rm -rf ~/*][Google Search]]
  1816. This link would show up in your Org-mode document as \"Google Search\",
  1817. but really it would remove your entire home directory.
  1818. Therefore we advise against setting this variable to nil.
  1819. Just change it to `y-or-n-p' if you want to confirm with a
  1820. single keystroke rather than having to type \"yes\"."
  1821. :group 'org-link-follow
  1822. :type '(choice
  1823. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1824. (const :tag "with y-or-n (faster)" y-or-n-p)
  1825. (const :tag "no confirmation (dangerous)" nil)))
  1826. (put 'org-confirm-shell-link-function
  1827. 'safe-local-variable
  1828. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1829. (defcustom org-confirm-shell-link-not-regexp ""
  1830. "A regexp to skip confirmation for shell links."
  1831. :group 'org-link-follow
  1832. :version "24.1"
  1833. :type 'regexp)
  1834. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1835. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1836. Elisp links can be dangerous: just think about a link
  1837. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1838. This link would show up in your Org-mode document as \"Google Search\",
  1839. but really it would remove your entire home directory.
  1840. Therefore we advise against setting this variable to nil.
  1841. Just change it to `y-or-n-p' if you want to confirm with a
  1842. single keystroke rather than having to type \"yes\"."
  1843. :group 'org-link-follow
  1844. :type '(choice
  1845. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1846. (const :tag "with y-or-n (faster)" y-or-n-p)
  1847. (const :tag "no confirmation (dangerous)" nil)))
  1848. (put 'org-confirm-shell-link-function
  1849. 'safe-local-variable
  1850. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1851. (defcustom org-confirm-elisp-link-not-regexp ""
  1852. "A regexp to skip confirmation for Elisp links."
  1853. :group 'org-link-follow
  1854. :version "24.1"
  1855. :type 'regexp)
  1856. (defconst org-file-apps-defaults-gnu
  1857. '((remote . emacs)
  1858. (system . mailcap)
  1859. (t . mailcap))
  1860. "Default file applications on a UNIX or GNU/Linux system.
  1861. See `org-file-apps'.")
  1862. (defconst org-file-apps-defaults-macosx
  1863. '((remote . emacs)
  1864. (t . "open %s")
  1865. (system . "open %s")
  1866. ("ps.gz" . "gv %s")
  1867. ("eps.gz" . "gv %s")
  1868. ("dvi" . "xdvi %s")
  1869. ("fig" . "xfig %s"))
  1870. "Default file applications on a MacOS X system.
  1871. The system \"open\" is known as a default, but we use X11 applications
  1872. for some files for which the OS does not have a good default.
  1873. See `org-file-apps'.")
  1874. (defconst org-file-apps-defaults-windowsnt
  1875. (list
  1876. '(remote . emacs)
  1877. (cons t
  1878. (list (if (featurep 'xemacs)
  1879. 'mswindows-shell-execute
  1880. 'w32-shell-execute)
  1881. "open" 'file))
  1882. (cons 'system
  1883. (list (if (featurep 'xemacs)
  1884. 'mswindows-shell-execute
  1885. 'w32-shell-execute)
  1886. "open" 'file)))
  1887. "Default file applications on a Windows NT system.
  1888. The system \"open\" is used for most files.
  1889. See `org-file-apps'.")
  1890. (defcustom org-file-apps
  1891. '((auto-mode . emacs)
  1892. ("\\.mm\\'" . default)
  1893. ("\\.x?html?\\'" . default)
  1894. ("\\.pdf\\'" . default))
  1895. "External applications for opening `file:path' items in a document.
  1896. Org-mode uses system defaults for different file types, but
  1897. you can use this variable to set the application for a given file
  1898. extension. The entries in this list are cons cells where the car identifies
  1899. files and the cdr the corresponding command. Possible values for the
  1900. file identifier are
  1901. \"string\" A string as a file identifier can be interpreted in different
  1902. ways, depending on its contents:
  1903. - Alphanumeric characters only:
  1904. Match links with this file extension.
  1905. Example: (\"pdf\" . \"evince %s\")
  1906. to open PDFs with evince.
  1907. - Regular expression: Match links where the
  1908. filename matches the regexp. If you want to
  1909. use groups here, use shy groups.
  1910. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1911. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1912. to open *.html and *.xhtml with firefox.
  1913. - Regular expression which contains (non-shy) groups:
  1914. Match links where the whole link, including \"::\", and
  1915. anything after that, matches the regexp.
  1916. In a custom command string, %1, %2, etc. are replaced with
  1917. the parts of the link that were matched by the groups.
  1918. For backwards compatibility, if a command string is given
  1919. that does not use any of the group matches, this case is
  1920. handled identically to the second one (i.e. match against
  1921. file name only).
  1922. In a custom lisp form, you can access the group matches with
  1923. (match-string n link).
  1924. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1925. to open [[file:document.pdf::5]] with evince at page 5.
  1926. `directory' Matches a directory
  1927. `remote' Matches a remote file, accessible through tramp or efs.
  1928. Remote files most likely should be visited through Emacs
  1929. because external applications cannot handle such paths.
  1930. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1931. so all files Emacs knows how to handle. Using this with
  1932. command `emacs' will open most files in Emacs. Beware that this
  1933. will also open html files inside Emacs, unless you add
  1934. (\"html\" . default) to the list as well.
  1935. t Default for files not matched by any of the other options.
  1936. `system' The system command to open files, like `open' on Windows
  1937. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1938. that will be selected if you call `C-c C-o' with a double
  1939. \\[universal-argument] \\[universal-argument] prefix.
  1940. Possible values for the command are:
  1941. `emacs' The file will be visited by the current Emacs process.
  1942. `default' Use the default application for this file type, which is the
  1943. association for t in the list, most likely in the system-specific
  1944. part.
  1945. This can be used to overrule an unwanted setting in the
  1946. system-specific variable.
  1947. `system' Use the system command for opening files, like \"open\".
  1948. This command is specified by the entry whose car is `system'.
  1949. Most likely, the system-specific version of this variable
  1950. does define this command, but you can overrule/replace it
  1951. here.
  1952. string A command to be executed by a shell; %s will be replaced
  1953. by the path to the file.
  1954. sexp A Lisp form which will be evaluated. The file path will
  1955. be available in the Lisp variable `file'.
  1956. For more examples, see the system specific constants
  1957. `org-file-apps-defaults-macosx'
  1958. `org-file-apps-defaults-windowsnt'
  1959. `org-file-apps-defaults-gnu'."
  1960. :group 'org-link-follow
  1961. :type '(repeat
  1962. (cons (choice :value ""
  1963. (string :tag "Extension")
  1964. (const :tag "System command to open files" system)
  1965. (const :tag "Default for unrecognized files" t)
  1966. (const :tag "Remote file" remote)
  1967. (const :tag "Links to a directory" directory)
  1968. (const :tag "Any files that have Emacs modes"
  1969. auto-mode))
  1970. (choice :value ""
  1971. (const :tag "Visit with Emacs" emacs)
  1972. (const :tag "Use default" default)
  1973. (const :tag "Use the system command" system)
  1974. (string :tag "Command")
  1975. (sexp :tag "Lisp form")))))
  1976. (defcustom org-doi-server-url "http://dx.doi.org/"
  1977. "The URL of the DOI server."
  1978. :type 'string
  1979. :version "24.3"
  1980. :group 'org-link-follow)
  1981. (defgroup org-refile nil
  1982. "Options concerning refiling entries in Org-mode."
  1983. :tag "Org Refile"
  1984. :group 'org)
  1985. (defcustom org-directory "~/org"
  1986. "Directory with org files.
  1987. This is just a default location to look for Org files. There is no need
  1988. at all to put your files into this directory. It is only used in the
  1989. following situations:
  1990. 1. When a capture template specifies a target file that is not an
  1991. absolute path. The path will then be interpreted relative to
  1992. `org-directory'
  1993. 2. When a capture note is filed away in an interactive way (when exiting the
  1994. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1995. with `org-directory' as the default path."
  1996. :group 'org-refile
  1997. :group 'org-capture
  1998. :type 'directory)
  1999. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2000. "Default target for storing notes.
  2001. Used as a fall back file for org-capture.el, for templates that
  2002. do not specify a target file."
  2003. :group 'org-refile
  2004. :group 'org-capture
  2005. :type 'file)
  2006. (defcustom org-goto-interface 'outline
  2007. "The default interface to be used for `org-goto'.
  2008. Allowed values are:
  2009. outline The interface shows an outline of the relevant file
  2010. and the correct heading is found by moving through
  2011. the outline or by searching with incremental search.
  2012. outline-path-completion Headlines in the current buffer are offered via
  2013. completion. This is the interface also used by
  2014. the refile command."
  2015. :group 'org-refile
  2016. :type '(choice
  2017. (const :tag "Outline" outline)
  2018. (const :tag "Outline-path-completion" outline-path-completion)))
  2019. (defcustom org-goto-max-level 5
  2020. "Maximum target level when running `org-goto' with refile interface."
  2021. :group 'org-refile
  2022. :type 'integer)
  2023. (defcustom org-reverse-note-order nil
  2024. "Non-nil means store new notes at the beginning of a file or entry.
  2025. When nil, new notes will be filed to the end of a file or entry.
  2026. This can also be a list with cons cells of regular expressions that
  2027. are matched against file names, and values."
  2028. :group 'org-capture
  2029. :group 'org-refile
  2030. :type '(choice
  2031. (const :tag "Reverse always" t)
  2032. (const :tag "Reverse never" nil)
  2033. (repeat :tag "By file name regexp"
  2034. (cons regexp boolean))))
  2035. (defcustom org-log-refile nil
  2036. "Information to record when a task is refiled.
  2037. Possible values are:
  2038. nil Don't add anything
  2039. time Add a time stamp to the task
  2040. note Prompt for a note and add it with template `org-log-note-headings'
  2041. This option can also be set with on a per-file-basis with
  2042. #+STARTUP: nologrefile
  2043. #+STARTUP: logrefile
  2044. #+STARTUP: lognoterefile
  2045. You can have local logging settings for a subtree by setting the LOGGING
  2046. property to one or more of these keywords.
  2047. When bulk-refiling from the agenda, the value `note' is forbidden and
  2048. will temporarily be changed to `time'."
  2049. :group 'org-refile
  2050. :group 'org-progress
  2051. :version "24.1"
  2052. :type '(choice
  2053. (const :tag "No logging" nil)
  2054. (const :tag "Record timestamp" time)
  2055. (const :tag "Record timestamp with note." note)))
  2056. (defcustom org-refile-targets nil
  2057. "Targets for refiling entries with \\[org-refile].
  2058. This is a list of cons cells. Each cell contains:
  2059. - a specification of the files to be considered, either a list of files,
  2060. or a symbol whose function or variable value will be used to retrieve
  2061. a file name or a list of file names. If you use `org-agenda-files' for
  2062. that, all agenda files will be scanned for targets. Nil means consider
  2063. headings in the current buffer.
  2064. - A specification of how to find candidate refile targets. This may be
  2065. any of:
  2066. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2067. This tag has to be present in all target headlines, inheritance will
  2068. not be considered.
  2069. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2070. todo keyword.
  2071. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2072. headlines that are refiling targets.
  2073. - a cons cell (:level . N). Any headline of level N is considered a target.
  2074. Note that, when `org-odd-levels-only' is set, level corresponds to
  2075. order in hierarchy, not to the number of stars.
  2076. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2077. Note that, when `org-odd-levels-only' is set, level corresponds to
  2078. order in hierarchy, not to the number of stars.
  2079. Each element of this list generates a set of possible targets.
  2080. The union of these sets is presented (with completion) to
  2081. the user by `org-refile'.
  2082. You can set the variable `org-refile-target-verify-function' to a function
  2083. to verify each headline found by the simple criteria above.
  2084. When this variable is nil, all top-level headlines in the current buffer
  2085. are used, equivalent to the value `((nil . (:level . 1))'."
  2086. :group 'org-refile
  2087. :type '(repeat
  2088. (cons
  2089. (choice :value org-agenda-files
  2090. (const :tag "All agenda files" org-agenda-files)
  2091. (const :tag "Current buffer" nil)
  2092. (function) (variable) (file))
  2093. (choice :tag "Identify target headline by"
  2094. (cons :tag "Specific tag" (const :value :tag) (string))
  2095. (cons :tag "TODO keyword" (const :value :todo) (string))
  2096. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2097. (cons :tag "Level number" (const :value :level) (integer))
  2098. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2099. (defcustom org-refile-target-verify-function nil
  2100. "Function to verify if the headline at point should be a refile target.
  2101. The function will be called without arguments, with point at the
  2102. beginning of the headline. It should return t and leave point
  2103. where it is if the headline is a valid target for refiling.
  2104. If the target should not be selected, the function must return nil.
  2105. In addition to this, it may move point to a place from where the search
  2106. should be continued. For example, the function may decide that the entire
  2107. subtree of the current entry should be excluded and move point to the end
  2108. of the subtree."
  2109. :group 'org-refile
  2110. :type '(choice
  2111. (const nil)
  2112. (function)))
  2113. (defcustom org-refile-use-cache nil
  2114. "Non-nil means cache refile targets to speed up the process.
  2115. The cache for a particular file will be updated automatically when
  2116. the buffer has been killed, or when any of the marker used for flagging
  2117. refile targets no longer points at a live buffer.
  2118. If you have added new entries to a buffer that might themselves be targets,
  2119. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2120. find that easier, `C-u C-u C-u C-c C-w'."
  2121. :group 'org-refile
  2122. :version "24.1"
  2123. :type 'boolean)
  2124. (defcustom org-refile-use-outline-path nil
  2125. "Non-nil means provide refile targets as paths.
  2126. So a level 3 headline will be available as level1/level2/level3.
  2127. When the value is `file', also include the file name (without directory)
  2128. into the path. In this case, you can also stop the completion after
  2129. the file name, to get entries inserted as top level in the file.
  2130. When `full-file-path', include the full file path."
  2131. :group 'org-refile
  2132. :type '(choice
  2133. (const :tag "Not" nil)
  2134. (const :tag "Yes" t)
  2135. (const :tag "Start with file name" file)
  2136. (const :tag "Start with full file path" full-file-path)))
  2137. (defcustom org-outline-path-complete-in-steps t
  2138. "Non-nil means complete the outline path in hierarchical steps.
  2139. When Org-mode uses the refile interface to select an outline path
  2140. \(see variable `org-refile-use-outline-path'), the completion of
  2141. the path can be done in a single go, or it can be done in steps down
  2142. the headline hierarchy. Going in steps is probably the best if you
  2143. do not use a special completion package like `ido' or `icicles'.
  2144. However, when using these packages, going in one step can be very
  2145. fast, while still showing the whole path to the entry."
  2146. :group 'org-refile
  2147. :type 'boolean)
  2148. (defcustom org-refile-allow-creating-parent-nodes nil
  2149. "Non-nil means allow to create new nodes as refile targets.
  2150. New nodes are then created by adding \"/new node name\" to the completion
  2151. of an existing node. When the value of this variable is `confirm',
  2152. new node creation must be confirmed by the user (recommended).
  2153. When nil, the completion must match an existing entry.
  2154. Note that, if the new heading is not seen by the criteria
  2155. listed in `org-refile-targets', multiple instances of the same
  2156. heading would be created by trying again to file under the new
  2157. heading."
  2158. :group 'org-refile
  2159. :type '(choice
  2160. (const :tag "Never" nil)
  2161. (const :tag "Always" t)
  2162. (const :tag "Prompt for confirmation" confirm)))
  2163. (defcustom org-refile-active-region-within-subtree nil
  2164. "Non-nil means also refile active region within a subtree.
  2165. By default `org-refile' doesn't allow refiling regions if they
  2166. don't contain a set of subtrees, but it might be convenient to
  2167. do so sometimes: in that case, the first line of the region is
  2168. converted to a headline before refiling."
  2169. :group 'org-refile
  2170. :version "24.1"
  2171. :type 'boolean)
  2172. (defgroup org-todo nil
  2173. "Options concerning TODO items in Org-mode."
  2174. :tag "Org TODO"
  2175. :group 'org)
  2176. (defgroup org-progress nil
  2177. "Options concerning Progress logging in Org-mode."
  2178. :tag "Org Progress"
  2179. :group 'org-time)
  2180. (defvar org-todo-interpretation-widgets
  2181. '((:tag "Sequence (cycling hits every state)" sequence)
  2182. (:tag "Type (cycling directly to DONE)" type))
  2183. "The available interpretation symbols for customizing `org-todo-keywords'.
  2184. Interested libraries should add to this list.")
  2185. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2186. "List of TODO entry keyword sequences and their interpretation.
  2187. \\<org-mode-map>This is a list of sequences.
  2188. Each sequence starts with a symbol, either `sequence' or `type',
  2189. indicating if the keywords should be interpreted as a sequence of
  2190. action steps, or as different types of TODO items. The first
  2191. keywords are states requiring action - these states will select a headline
  2192. for inclusion into the global TODO list Org-mode produces. If one of
  2193. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2194. signify that no further action is necessary. If \"|\" is not found,
  2195. the last keyword is treated as the only DONE state of the sequence.
  2196. The command \\[org-todo] cycles an entry through these states, and one
  2197. additional state where no keyword is present. For details about this
  2198. cycling, see the manual.
  2199. TODO keywords and interpretation can also be set on a per-file basis with
  2200. the special #+SEQ_TODO and #+TYP_TODO lines.
  2201. Each keyword can optionally specify a character for fast state selection
  2202. \(in combination with the variable `org-use-fast-todo-selection')
  2203. and specifiers for state change logging, using the same syntax that
  2204. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2205. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2206. indicates to record a time stamp each time this state is selected.
  2207. Each keyword may also specify if a timestamp or a note should be
  2208. recorded when entering or leaving the state, by adding additional
  2209. characters in the parenthesis after the keyword. This looks like this:
  2210. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2211. record only the time of the state change. With X and Y being either
  2212. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2213. Y when leaving the state if and only if the *target* state does not
  2214. define X. You may omit any of the fast-selection key or X or /Y,
  2215. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2216. For backward compatibility, this variable may also be just a list
  2217. of keywords. In this case the interpretation (sequence or type) will be
  2218. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2219. :group 'org-todo
  2220. :group 'org-keywords
  2221. :type '(choice
  2222. (repeat :tag "Old syntax, just keywords"
  2223. (string :tag "Keyword"))
  2224. (repeat :tag "New syntax"
  2225. (cons
  2226. (choice
  2227. :tag "Interpretation"
  2228. ;;Quick and dirty way to see
  2229. ;;`org-todo-interpretations'. This takes the
  2230. ;;place of item arguments
  2231. :convert-widget
  2232. (lambda (widget)
  2233. (widget-put widget
  2234. :args (mapcar
  2235. (lambda (x)
  2236. (widget-convert
  2237. (cons 'const x)))
  2238. org-todo-interpretation-widgets))
  2239. widget))
  2240. (repeat
  2241. (string :tag "Keyword"))))))
  2242. (defvar org-todo-keywords-1 nil
  2243. "All TODO and DONE keywords active in a buffer.")
  2244. (make-variable-buffer-local 'org-todo-keywords-1)
  2245. (defvar org-todo-keywords-for-agenda nil)
  2246. (defvar org-done-keywords-for-agenda nil)
  2247. (defvar org-todo-keyword-alist-for-agenda nil)
  2248. (defvar org-tag-alist-for-agenda nil
  2249. "Alist of all tags from all agenda files.")
  2250. (defvar org-tag-groups-alist-for-agenda nil
  2251. "Alist of all groups tags from all current agenda files.")
  2252. (defvar org-tag-groups-alist nil)
  2253. (make-variable-buffer-local 'org-tag-groups-alist)
  2254. (defvar org-agenda-contributing-files nil)
  2255. (defvar org-not-done-keywords nil)
  2256. (make-variable-buffer-local 'org-not-done-keywords)
  2257. (defvar org-done-keywords nil)
  2258. (make-variable-buffer-local 'org-done-keywords)
  2259. (defvar org-todo-heads nil)
  2260. (make-variable-buffer-local 'org-todo-heads)
  2261. (defvar org-todo-sets nil)
  2262. (make-variable-buffer-local 'org-todo-sets)
  2263. (defvar org-todo-log-states nil)
  2264. (make-variable-buffer-local 'org-todo-log-states)
  2265. (defvar org-todo-kwd-alist nil)
  2266. (make-variable-buffer-local 'org-todo-kwd-alist)
  2267. (defvar org-todo-key-alist nil)
  2268. (make-variable-buffer-local 'org-todo-key-alist)
  2269. (defvar org-todo-key-trigger nil)
  2270. (make-variable-buffer-local 'org-todo-key-trigger)
  2271. (defcustom org-todo-interpretation 'sequence
  2272. "Controls how TODO keywords are interpreted.
  2273. This variable is in principle obsolete and is only used for
  2274. backward compatibility, if the interpretation of todo keywords is
  2275. not given already in `org-todo-keywords'. See that variable for
  2276. more information."
  2277. :group 'org-todo
  2278. :group 'org-keywords
  2279. :type '(choice (const sequence)
  2280. (const type)))
  2281. (defcustom org-use-fast-todo-selection t
  2282. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2283. This variable describes if and under what circumstances the cycling
  2284. mechanism for TODO keywords will be replaced by a single-key, direct
  2285. selection scheme.
  2286. When nil, fast selection is never used.
  2287. When the symbol `prefix', it will be used when `org-todo' is called
  2288. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2289. `C-u t' in an agenda buffer.
  2290. When t, fast selection is used by default. In this case, the prefix
  2291. argument forces cycling instead.
  2292. In all cases, the special interface is only used if access keys have
  2293. actually been assigned by the user, i.e. if keywords in the configuration
  2294. are followed by a letter in parenthesis, like TODO(t)."
  2295. :group 'org-todo
  2296. :type '(choice
  2297. (const :tag "Never" nil)
  2298. (const :tag "By default" t)
  2299. (const :tag "Only with C-u C-c C-t" prefix)))
  2300. (defcustom org-provide-todo-statistics t
  2301. "Non-nil means update todo statistics after insert and toggle.
  2302. ALL-HEADLINES means update todo statistics by including headlines
  2303. with no TODO keyword as well, counting them as not done.
  2304. A list of TODO keywords means the same, but skip keywords that are
  2305. not in this list.
  2306. When set to a list of two lists, the first list contains keywords
  2307. to consider as TODO keywords, the second list contains keywords
  2308. to consider as DONE keywords.
  2309. When this is set, todo statistics is updated in the parent of the
  2310. current entry each time a todo state is changed."
  2311. :group 'org-todo
  2312. :type '(choice
  2313. (const :tag "Yes, only for TODO entries" t)
  2314. (const :tag "Yes, including all entries" all-headlines)
  2315. (repeat :tag "Yes, for TODOs in this list"
  2316. (string :tag "TODO keyword"))
  2317. (list :tag "Yes, for TODOs and DONEs in these lists"
  2318. (repeat (string :tag "TODO keyword"))
  2319. (repeat (string :tag "DONE keyword")))
  2320. (other :tag "No TODO statistics" nil)))
  2321. (defcustom org-hierarchical-todo-statistics t
  2322. "Non-nil means TODO statistics covers just direct children.
  2323. When nil, all entries in the subtree are considered.
  2324. This has only an effect if `org-provide-todo-statistics' is set.
  2325. To set this to nil for only a single subtree, use a COOKIE_DATA
  2326. property and include the word \"recursive\" into the value."
  2327. :group 'org-todo
  2328. :type 'boolean)
  2329. (defcustom org-after-todo-state-change-hook nil
  2330. "Hook which is run after the state of a TODO item was changed.
  2331. The new state (a string with a TODO keyword, or nil) is available in the
  2332. Lisp variable `org-state'."
  2333. :group 'org-todo
  2334. :type 'hook)
  2335. (defvar org-blocker-hook nil
  2336. "Hook for functions that are allowed to block a state change.
  2337. Functions in this hook should not modify the buffer.
  2338. Each function gets as its single argument a property list,
  2339. see `org-trigger-hook' for more information about this list.
  2340. If any of the functions in this hook returns nil, the state change
  2341. is blocked.")
  2342. (defvar org-trigger-hook nil
  2343. "Hook for functions that are triggered by a state change.
  2344. Each function gets as its single argument a property list with at
  2345. least the following elements:
  2346. (:type type-of-change :position pos-at-entry-start
  2347. :from old-state :to new-state)
  2348. Depending on the type, more properties may be present.
  2349. This mechanism is currently implemented for:
  2350. TODO state changes
  2351. ------------------
  2352. :type todo-state-change
  2353. :from previous state (keyword as a string), or nil, or a symbol
  2354. 'todo' or 'done', to indicate the general type of state.
  2355. :to new state, like in :from")
  2356. (defcustom org-enforce-todo-dependencies nil
  2357. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2358. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2359. be blocked if any prior sibling is not yet done.
  2360. Finally, if the parent is blocked because of ordered siblings of its own,
  2361. the child will also be blocked."
  2362. :set (lambda (var val)
  2363. (set var val)
  2364. (if val
  2365. (add-hook 'org-blocker-hook
  2366. 'org-block-todo-from-children-or-siblings-or-parent)
  2367. (remove-hook 'org-blocker-hook
  2368. 'org-block-todo-from-children-or-siblings-or-parent)))
  2369. :group 'org-todo
  2370. :type 'boolean)
  2371. (defcustom org-enforce-todo-checkbox-dependencies nil
  2372. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2373. When this is nil, checkboxes have no influence on switching TODO states.
  2374. When non-nil, you first need to check off all check boxes before the TODO
  2375. entry can be switched to DONE.
  2376. This variable needs to be set before org.el is loaded, and you need to
  2377. restart Emacs after a change to make the change effective. The only way
  2378. to change is while Emacs is running is through the customize interface."
  2379. :set (lambda (var val)
  2380. (set var val)
  2381. (if val
  2382. (add-hook 'org-blocker-hook
  2383. 'org-block-todo-from-checkboxes)
  2384. (remove-hook 'org-blocker-hook
  2385. 'org-block-todo-from-checkboxes)))
  2386. :group 'org-todo
  2387. :type 'boolean)
  2388. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2389. "Non-nil means inserting a TODO heading is treated as state change.
  2390. So when the command \\[org-insert-todo-heading] is used, state change
  2391. logging will apply if appropriate. When nil, the new TODO item will
  2392. be inserted directly, and no logging will take place."
  2393. :group 'org-todo
  2394. :type 'boolean)
  2395. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2396. "Non-nil means switching TODO states with S-cursor counts as state change.
  2397. This is the default behavior. However, setting this to nil allows a
  2398. convenient way to select a TODO state and bypass any logging associated
  2399. with that."
  2400. :group 'org-todo
  2401. :type 'boolean)
  2402. (defcustom org-todo-state-tags-triggers nil
  2403. "Tag changes that should be triggered by TODO state changes.
  2404. This is a list. Each entry is
  2405. (state-change (tag . flag) .......)
  2406. State-change can be a string with a state, and empty string to indicate the
  2407. state that has no TODO keyword, or it can be one of the symbols `todo'
  2408. or `done', meaning any not-done or done state, respectively."
  2409. :group 'org-todo
  2410. :group 'org-tags
  2411. :type '(repeat
  2412. (cons (choice :tag "When changing to"
  2413. (const :tag "Not-done state" todo)
  2414. (const :tag "Done state" done)
  2415. (string :tag "State"))
  2416. (repeat
  2417. (cons :tag "Tag action"
  2418. (string :tag "Tag")
  2419. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2420. (defcustom org-log-done nil
  2421. "Information to record when a task moves to the DONE state.
  2422. Possible values are:
  2423. nil Don't add anything, just change the keyword
  2424. time Add a time stamp to the task
  2425. note Prompt for a note and add it with template `org-log-note-headings'
  2426. This option can also be set with on a per-file-basis with
  2427. #+STARTUP: nologdone
  2428. #+STARTUP: logdone
  2429. #+STARTUP: lognotedone
  2430. You can have local logging settings for a subtree by setting the LOGGING
  2431. property to one or more of these keywords."
  2432. :group 'org-todo
  2433. :group 'org-progress
  2434. :type '(choice
  2435. (const :tag "No logging" nil)
  2436. (const :tag "Record CLOSED timestamp" time)
  2437. (const :tag "Record CLOSED timestamp with note." note)))
  2438. ;; Normalize old uses of org-log-done.
  2439. (cond
  2440. ((eq org-log-done t) (setq org-log-done 'time))
  2441. ((and (listp org-log-done) (memq 'done org-log-done))
  2442. (setq org-log-done 'note)))
  2443. (defcustom org-log-reschedule nil
  2444. "Information to record when the scheduling date of a tasks is modified.
  2445. Possible values are:
  2446. nil Don't add anything, just change the date
  2447. time Add a time stamp to the task
  2448. note Prompt for a note and add it with template `org-log-note-headings'
  2449. This option can also be set with on a per-file-basis with
  2450. #+STARTUP: nologreschedule
  2451. #+STARTUP: logreschedule
  2452. #+STARTUP: lognotereschedule"
  2453. :group 'org-todo
  2454. :group 'org-progress
  2455. :type '(choice
  2456. (const :tag "No logging" nil)
  2457. (const :tag "Record timestamp" time)
  2458. (const :tag "Record timestamp with note." note)))
  2459. (defcustom org-log-redeadline nil
  2460. "Information to record when the deadline date of a tasks is modified.
  2461. Possible values are:
  2462. nil Don't add anything, just change the date
  2463. time Add a time stamp to the task
  2464. note Prompt for a note and add it with template `org-log-note-headings'
  2465. This option can also be set with on a per-file-basis with
  2466. #+STARTUP: nologredeadline
  2467. #+STARTUP: logredeadline
  2468. #+STARTUP: lognoteredeadline
  2469. You can have local logging settings for a subtree by setting the LOGGING
  2470. property to one or more of these keywords."
  2471. :group 'org-todo
  2472. :group 'org-progress
  2473. :type '(choice
  2474. (const :tag "No logging" nil)
  2475. (const :tag "Record timestamp" time)
  2476. (const :tag "Record timestamp with note." note)))
  2477. (defcustom org-log-note-clock-out nil
  2478. "Non-nil means record a note when clocking out of an item.
  2479. This can also be configured on a per-file basis by adding one of
  2480. the following lines anywhere in the buffer:
  2481. #+STARTUP: lognoteclock-out
  2482. #+STARTUP: nolognoteclock-out"
  2483. :group 'org-todo
  2484. :group 'org-progress
  2485. :type 'boolean)
  2486. (defcustom org-log-done-with-time t
  2487. "Non-nil means the CLOSED time stamp will contain date and time.
  2488. When nil, only the date will be recorded."
  2489. :group 'org-progress
  2490. :type 'boolean)
  2491. (defcustom org-log-note-headings
  2492. '((done . "CLOSING NOTE %t")
  2493. (state . "State %-12s from %-12S %t")
  2494. (note . "Note taken on %t")
  2495. (reschedule . "Rescheduled from %S on %t")
  2496. (delschedule . "Not scheduled, was %S on %t")
  2497. (redeadline . "New deadline from %S on %t")
  2498. (deldeadline . "Removed deadline, was %S on %t")
  2499. (refile . "Refiled on %t")
  2500. (clock-out . ""))
  2501. "Headings for notes added to entries.
  2502. The value is an alist, with the car being a symbol indicating the
  2503. note context, and the cdr is the heading to be used. The heading
  2504. may also be the empty string. The following placeholders can be
  2505. used:
  2506. %t a time stamp.
  2507. %T an active time stamp instead the default inactive one
  2508. %d a short-format time stamp.
  2509. %D an active short-format time stamp.
  2510. %s the new TODO state or time stamp (inactive), in double quotes.
  2511. %S the old TODO state or time stamp (inactive), in double quotes.
  2512. %u the user name.
  2513. %U full user name.
  2514. In fact, it is not a good idea to change the `state' entry,
  2515. because Agenda Log mode depends on the format of these entries."
  2516. :group 'org-todo
  2517. :group 'org-progress
  2518. :type '(list :greedy t
  2519. (cons (const :tag "Heading when closing an item" done) string)
  2520. (cons (const :tag
  2521. "Heading when changing todo state (todo sequence only)"
  2522. state) string)
  2523. (cons (const :tag "Heading when just taking a note" note) string)
  2524. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2525. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2526. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2527. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2528. (cons (const :tag "Heading when refiling" refile) string)
  2529. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2530. (unless (assq 'note org-log-note-headings)
  2531. (push '(note . "%t") org-log-note-headings))
  2532. (defcustom org-log-into-drawer nil
  2533. "Non-nil means insert state change notes and time stamps into a drawer.
  2534. When nil, state changes notes will be inserted after the headline and
  2535. any scheduling and clock lines, but not inside a drawer.
  2536. The value of this variable should be the name of the drawer to use.
  2537. LOGBOOK is proposed as the default drawer for this purpose, you can
  2538. also set this to a string to define the drawer of your choice.
  2539. A value of t is also allowed, representing \"LOGBOOK\".
  2540. A value of t or nil can also be set with on a per-file-basis with
  2541. #+STARTUP: logdrawer
  2542. #+STARTUP: nologdrawer
  2543. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2544. will be ignored.
  2545. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2546. a subtree.
  2547. Do not check directly this variable in a Lisp program. Call
  2548. function `org-log-into-drawer' instead."
  2549. :group 'org-todo
  2550. :group 'org-progress
  2551. :type '(choice
  2552. (const :tag "Not into a drawer" nil)
  2553. (const :tag "LOGBOOK" t)
  2554. (string :tag "Other")))
  2555. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2556. (defun org-log-into-drawer ()
  2557. "Name of the log drawer, as a string, or nil.
  2558. This is the value of `org-log-into-drawer'. However, if the
  2559. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2560. be used instead of the default value."
  2561. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2562. (cond ((equal p "nil") nil)
  2563. ((equal p "t") "LOGBOOK")
  2564. ((stringp p) p)
  2565. (p "LOGBOOK")
  2566. ((stringp org-log-into-drawer) org-log-into-drawer)
  2567. (org-log-into-drawer "LOGBOOK"))))
  2568. (defcustom org-log-state-notes-insert-after-drawers nil
  2569. "Non-nil means insert state change notes after any drawers in entry.
  2570. Only the drawers that *immediately* follow the headline and the
  2571. deadline/scheduled line are skipped.
  2572. When nil, insert notes right after the heading and perhaps the line
  2573. with deadline/scheduling if present.
  2574. This variable will have no effect if `org-log-into-drawer' is
  2575. set."
  2576. :group 'org-todo
  2577. :group 'org-progress
  2578. :type 'boolean)
  2579. (defcustom org-log-states-order-reversed t
  2580. "Non-nil means the latest state note will be directly after heading.
  2581. When nil, the state change notes will be ordered according to time.
  2582. This option can also be set with on a per-file-basis with
  2583. #+STARTUP: logstatesreversed
  2584. #+STARTUP: nologstatesreversed"
  2585. :group 'org-todo
  2586. :group 'org-progress
  2587. :type 'boolean)
  2588. (defcustom org-todo-repeat-to-state nil
  2589. "The TODO state to which a repeater should return the repeating task.
  2590. By default this is the first task in a TODO sequence, or the previous state
  2591. in a TODO_TYP set. But you can specify another task here.
  2592. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2593. :group 'org-todo
  2594. :version "24.1"
  2595. :type '(choice (const :tag "Head of sequence" nil)
  2596. (string :tag "Specific state")))
  2597. (defcustom org-log-repeat 'time
  2598. "Non-nil means record moving through the DONE state when triggering repeat.
  2599. An auto-repeating task is immediately switched back to TODO when
  2600. marked DONE. If you are not logging state changes (by adding \"@\"
  2601. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2602. record a closing note, there will be no record of the task moving
  2603. through DONE. This variable forces taking a note anyway.
  2604. nil Don't force a record
  2605. time Record a time stamp
  2606. note Prompt for a note and add it with template `org-log-note-headings'
  2607. This option can also be set with on a per-file-basis with
  2608. #+STARTUP: nologrepeat
  2609. #+STARTUP: logrepeat
  2610. #+STARTUP: lognoterepeat
  2611. You can have local logging settings for a subtree by setting the LOGGING
  2612. property to one or more of these keywords."
  2613. :group 'org-todo
  2614. :group 'org-progress
  2615. :type '(choice
  2616. (const :tag "Don't force a record" nil)
  2617. (const :tag "Force recording the DONE state" time)
  2618. (const :tag "Force recording a note with the DONE state" note)))
  2619. (defgroup org-priorities nil
  2620. "Priorities in Org-mode."
  2621. :tag "Org Priorities"
  2622. :group 'org-todo)
  2623. (defcustom org-enable-priority-commands t
  2624. "Non-nil means priority commands are active.
  2625. When nil, these commands will be disabled, so that you never accidentally
  2626. set a priority."
  2627. :group 'org-priorities
  2628. :type 'boolean)
  2629. (defcustom org-highest-priority ?A
  2630. "The highest priority of TODO items. A character like ?A, ?B etc.
  2631. Must have a smaller ASCII number than `org-lowest-priority'."
  2632. :group 'org-priorities
  2633. :type 'character)
  2634. (defcustom org-lowest-priority ?C
  2635. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2636. Must have a larger ASCII number than `org-highest-priority'."
  2637. :group 'org-priorities
  2638. :type 'character)
  2639. (defcustom org-default-priority ?B
  2640. "The default priority of TODO items.
  2641. This is the priority an item gets if no explicit priority is given.
  2642. When starting to cycle on an empty priority the first step in the cycle
  2643. depends on `org-priority-start-cycle-with-default'. The resulting first
  2644. step priority must not exceed the range from `org-highest-priority' to
  2645. `org-lowest-priority' which means that `org-default-priority' has to be
  2646. in this range exclusive or inclusive the range boundaries. Else the
  2647. first step refuses to set the default and the second will fall back
  2648. to (depending on the command used) the highest or lowest priority."
  2649. :group 'org-priorities
  2650. :type 'character)
  2651. (defcustom org-priority-start-cycle-with-default t
  2652. "Non-nil means start with default priority when starting to cycle.
  2653. When this is nil, the first step in the cycle will be (depending on the
  2654. command used) one higher or lower than the default priority.
  2655. See also `org-default-priority'."
  2656. :group 'org-priorities
  2657. :type 'boolean)
  2658. (defcustom org-get-priority-function nil
  2659. "Function to extract the priority from a string.
  2660. The string is normally the headline. If this is nil Org computes the
  2661. priority from the priority cookie like [#A] in the headline. It returns
  2662. an integer, increasing by 1000 for each priority level.
  2663. The user can set a different function here, which should take a string
  2664. as an argument and return the numeric priority."
  2665. :group 'org-priorities
  2666. :version "24.1"
  2667. :type '(choice
  2668. (const nil)
  2669. (function)))
  2670. (defgroup org-time nil
  2671. "Options concerning time stamps and deadlines in Org-mode."
  2672. :tag "Org Time"
  2673. :group 'org)
  2674. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2675. "Number of minutes to round time stamps to.
  2676. These are two values, the first applies when first creating a time stamp.
  2677. The second applies when changing it with the commands `S-up' and `S-down'.
  2678. When changing the time stamp, this means that it will change in steps
  2679. of N minutes, as given by the second value.
  2680. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2681. numbers should be factors of 60, so for example 5, 10, 15.
  2682. When this is larger than 1, you can still force an exact time stamp by using
  2683. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2684. and by using a prefix arg to `S-up/down' to specify the exact number
  2685. of minutes to shift."
  2686. :group 'org-time
  2687. :get (lambda (var) ; Make sure both elements are there
  2688. (if (integerp (default-value var))
  2689. (list (default-value var) 5)
  2690. (default-value var)))
  2691. :type '(list
  2692. (integer :tag "when inserting times")
  2693. (integer :tag "when modifying times")))
  2694. ;; Normalize old customizations of this variable.
  2695. (when (integerp org-time-stamp-rounding-minutes)
  2696. (setq org-time-stamp-rounding-minutes
  2697. (list org-time-stamp-rounding-minutes
  2698. org-time-stamp-rounding-minutes)))
  2699. (defcustom org-display-custom-times nil
  2700. "Non-nil means overlay custom formats over all time stamps.
  2701. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2702. To turn this on on a per-file basis, insert anywhere in the file:
  2703. #+STARTUP: customtime"
  2704. :group 'org-time
  2705. :set 'set-default
  2706. :type 'sexp)
  2707. (make-variable-buffer-local 'org-display-custom-times)
  2708. (defcustom org-time-stamp-custom-formats
  2709. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2710. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2711. These are overlaid over the default ISO format if the variable
  2712. `org-display-custom-times' is set. Time like %H:%M should be at the
  2713. end of the second format. The custom formats are also honored by export
  2714. commands, if custom time display is turned on at the time of export."
  2715. :group 'org-time
  2716. :type 'sexp)
  2717. (defun org-time-stamp-format (&optional long inactive)
  2718. "Get the right format for a time string."
  2719. (let ((f (if long (cdr org-time-stamp-formats)
  2720. (car org-time-stamp-formats))))
  2721. (if inactive
  2722. (concat "[" (substring f 1 -1) "]")
  2723. f)))
  2724. (defcustom org-time-clocksum-format
  2725. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2726. "The format string used when creating CLOCKSUM lines.
  2727. This is also used when Org mode generates a time duration.
  2728. The value can be a single format string containing two
  2729. %-sequences, which will be filled with the number of hours and
  2730. minutes in that order.
  2731. Alternatively, the value can be a plist associating any of the
  2732. keys :years, :months, :weeks, :days, :hours or :minutes with
  2733. format strings. The time duration is formatted using only the
  2734. time components that are needed and concatenating the results.
  2735. If a time unit in absent, it falls back to the next smallest
  2736. unit.
  2737. The keys :require-years, :require-months, :require-days,
  2738. :require-weeks, :require-hours, :require-minutes are also
  2739. meaningful. A non-nil value for these keys indicates that the
  2740. corresponding time component should always be included, even if
  2741. its value is 0.
  2742. For example,
  2743. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2744. :require-minutes t)
  2745. means durations longer than a day will be expressed in days,
  2746. hours and minutes, and durations less than a day will always be
  2747. expressed in hours and minutes (even for durations less than an
  2748. hour).
  2749. The value
  2750. \(:days \"%dd\" :minutes \"%dm\")
  2751. means durations longer than a day will be expressed in days and
  2752. minutes, and durations less than a day will be expressed entirely
  2753. in minutes (even for durations longer than an hour)."
  2754. :group 'org-time
  2755. :group 'org-clock
  2756. :version "24.4"
  2757. :package-version '(Org . "8.0")
  2758. :type '(choice (string :tag "Format string")
  2759. (set :tag "Plist"
  2760. (group :inline t (const :tag "Years" :years)
  2761. (string :tag "Format string"))
  2762. (group :inline t
  2763. (const :tag "Always show years" :require-years)
  2764. (const t))
  2765. (group :inline t (const :tag "Months" :months)
  2766. (string :tag "Format string"))
  2767. (group :inline t
  2768. (const :tag "Always show months" :require-months)
  2769. (const t))
  2770. (group :inline t (const :tag "Weeks" :weeks)
  2771. (string :tag "Format string"))
  2772. (group :inline t
  2773. (const :tag "Always show weeks" :require-weeks)
  2774. (const t))
  2775. (group :inline t (const :tag "Days" :days)
  2776. (string :tag "Format string"))
  2777. (group :inline t
  2778. (const :tag "Always show days" :require-days)
  2779. (const t))
  2780. (group :inline t (const :tag "Hours" :hours)
  2781. (string :tag "Format string"))
  2782. (group :inline t
  2783. (const :tag "Always show hours" :require-hours)
  2784. (const t))
  2785. (group :inline t (const :tag "Minutes" :minutes)
  2786. (string :tag "Format string"))
  2787. (group :inline t
  2788. (const :tag "Always show minutes" :require-minutes)
  2789. (const t)))))
  2790. (defcustom org-time-clocksum-use-fractional nil
  2791. "When non-nil, \\[org-clock-display] uses fractional times.
  2792. See `org-time-clocksum-format' for more on time clock formats."
  2793. :group 'org-time
  2794. :group 'org-clock
  2795. :version "24.3"
  2796. :type 'boolean)
  2797. (defcustom org-time-clocksum-use-effort-durations nil
  2798. "When non-nil, \\[org-clock-display] uses effort durations.
  2799. E.g. by default, one day is considered to be a 8 hours effort,
  2800. so a task that has been clocked for 16 hours will be displayed
  2801. as during 2 days in the clock display or in the clocktable.
  2802. See `org-effort-durations' on how to set effort durations
  2803. and `org-time-clocksum-format' for more on time clock formats."
  2804. :group 'org-time
  2805. :group 'org-clock
  2806. :version "24.4"
  2807. :package-version '(Org . "8.0")
  2808. :type 'boolean)
  2809. (defcustom org-time-clocksum-fractional-format "%.2f"
  2810. "The format string used when creating CLOCKSUM lines,
  2811. or when Org mode generates a time duration, if
  2812. `org-time-clocksum-use-fractional' is enabled.
  2813. The value can be a single format string containing one
  2814. %-sequence, which will be filled with the number of hours as
  2815. a float.
  2816. Alternatively, the value can be a plist associating any of the
  2817. keys :years, :months, :weeks, :days, :hours or :minutes with
  2818. a format string. The time duration is formatted using the
  2819. largest time unit which gives a non-zero integer part. If all
  2820. specified formats have zero integer part, the smallest time unit
  2821. is used."
  2822. :group 'org-time
  2823. :type '(choice (string :tag "Format string")
  2824. (set (group :inline t (const :tag "Years" :years)
  2825. (string :tag "Format string"))
  2826. (group :inline t (const :tag "Months" :months)
  2827. (string :tag "Format string"))
  2828. (group :inline t (const :tag "Weeks" :weeks)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Days" :days)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Hours" :hours)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Minutes" :minutes)
  2835. (string :tag "Format string")))))
  2836. (defcustom org-deadline-warning-days 14
  2837. "Number of days before expiration during which a deadline becomes active.
  2838. This variable governs the display in sparse trees and in the agenda.
  2839. When 0 or negative, it means use this number (the absolute value of it)
  2840. even if a deadline has a different individual lead time specified.
  2841. Custom commands can set this variable in the options section."
  2842. :group 'org-time
  2843. :group 'org-agenda-daily/weekly
  2844. :type 'integer)
  2845. (defcustom org-scheduled-delay-days 0
  2846. "Number of days before a scheduled item becomes active.
  2847. This variable governs the display in sparse trees and in the agenda.
  2848. The default value (i.e. 0) means: don't delay scheduled item.
  2849. When negative, it means use this number (the absolute value of it)
  2850. even if a scheduled item has a different individual delay time
  2851. specified.
  2852. Custom commands can set this variable in the options section."
  2853. :group 'org-time
  2854. :group 'org-agenda-daily/weekly
  2855. :version "24.4"
  2856. :package-version '(Org . "8.0")
  2857. :type 'integer)
  2858. (defcustom org-read-date-prefer-future t
  2859. "Non-nil means assume future for incomplete date input from user.
  2860. This affects the following situations:
  2861. 1. The user gives a month but not a year.
  2862. For example, if it is April and you enter \"feb 2\", this will be read
  2863. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2864. 2. The user gives a day, but no month.
  2865. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2866. read this as the third of *next* month. However, if you enter \"17\",
  2867. it will be considered as *this* month.
  2868. If you set this variable to the symbol `time', then also the following
  2869. will work:
  2870. 3. If the user gives a time.
  2871. If the time is before now, it will be interpreted as tomorrow.
  2872. Currently none of this works for ISO week specifications.
  2873. When this option is nil, the current day, month and year will always be
  2874. used as defaults.
  2875. See also `org-agenda-jump-prefer-future'."
  2876. :group 'org-time
  2877. :type '(choice
  2878. (const :tag "Never" nil)
  2879. (const :tag "Check month and day" t)
  2880. (const :tag "Check month, day, and time" time)))
  2881. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2882. "Should the agenda jump command prefer the future for incomplete dates?
  2883. The default is to do the same as configured in `org-read-date-prefer-future'.
  2884. But you can also set a deviating value here.
  2885. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2886. :group 'org-agenda
  2887. :group 'org-time
  2888. :version "24.1"
  2889. :type '(choice
  2890. (const :tag "Use org-read-date-prefer-future"
  2891. org-read-date-prefer-future)
  2892. (const :tag "Never" nil)
  2893. (const :tag "Always" t)))
  2894. (defcustom org-read-date-force-compatible-dates t
  2895. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2896. Depending on the system Emacs is running on, certain dates cannot
  2897. be represented with the type used internally to represent time.
  2898. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2899. correctly. Some systems allow for earlier dates, some for later,
  2900. some for both. One way to find out it to insert any date into an
  2901. Org buffer, putting the cursor on the year and hitting S-up and
  2902. S-down to test the range.
  2903. When this variable is set to t, the date/time prompt will not let
  2904. you specify dates outside the 1970-2037 range, so it is certain that
  2905. these dates will work in whatever version of Emacs you are
  2906. running, and also that you can move a file from one Emacs implementation
  2907. to another. WHenever Org is forcing the year for you, it will display
  2908. a message and beep.
  2909. When this variable is nil, Org will check if the date is
  2910. representable in the specific Emacs implementation you are using.
  2911. If not, it will force a year, usually the current year, and beep
  2912. to remind you. Currently this setting is not recommended because
  2913. the likelihood that you will open your Org files in an Emacs that
  2914. has limited date range is not negligible.
  2915. A workaround for this problem is to use diary sexp dates for time
  2916. stamps outside of this range."
  2917. :group 'org-time
  2918. :version "24.1"
  2919. :type 'boolean)
  2920. (defcustom org-read-date-display-live t
  2921. "Non-nil means display current interpretation of date prompt live.
  2922. This display will be in an overlay, in the minibuffer."
  2923. :group 'org-time
  2924. :type 'boolean)
  2925. (defcustom org-read-date-popup-calendar t
  2926. "Non-nil means pop up a calendar when prompting for a date.
  2927. In the calendar, the date can be selected with mouse-1. However, the
  2928. minibuffer will also be active, and you can simply enter the date as well.
  2929. When nil, only the minibuffer will be available."
  2930. :group 'org-time
  2931. :type 'boolean)
  2932. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2933. 'org-read-date-popup-calendar)
  2934. (make-obsolete-variable
  2935. 'org-read-date-minibuffer-setup-hook
  2936. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2937. (defcustom org-read-date-minibuffer-setup-hook nil
  2938. "Hook to be used to set up keys for the date/time interface.
  2939. Add key definitions to `minibuffer-local-map', which will be a
  2940. temporary copy.
  2941. WARNING: This option is obsolete, you should use
  2942. `org-read-date-minibuffer-local-map' to set up keys."
  2943. :group 'org-time
  2944. :type 'hook)
  2945. (defcustom org-extend-today-until 0
  2946. "The hour when your day really ends. Must be an integer.
  2947. This has influence for the following applications:
  2948. - When switching the agenda to \"today\". It it is still earlier than
  2949. the time given here, the day recognized as TODAY is actually yesterday.
  2950. - When a date is read from the user and it is still before the time given
  2951. here, the current date and time will be assumed to be yesterday, 23:59.
  2952. Also, timestamps inserted in capture templates follow this rule.
  2953. IMPORTANT: This is a feature whose implementation is and likely will
  2954. remain incomplete. Really, it is only here because past midnight seems to
  2955. be the favorite working time of John Wiegley :-)"
  2956. :group 'org-time
  2957. :type 'integer)
  2958. (defcustom org-use-effective-time nil
  2959. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2960. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2961. \"effective time\" of any timestamps between midnight and 8am will be
  2962. 23:59 of the previous day."
  2963. :group 'org-time
  2964. :version "24.1"
  2965. :type 'boolean)
  2966. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2967. "When non-nil, use the last clock out time for `org-todo'.
  2968. Note that this option has precedence over the combined use of
  2969. `org-use-effective-time' and `org-extend-today-until'."
  2970. :group 'org-time
  2971. :version "24.4"
  2972. :package-version '(Org . "8.0")
  2973. :type 'boolean)
  2974. (defcustom org-edit-timestamp-down-means-later nil
  2975. "Non-nil means S-down will increase the time in a time stamp.
  2976. When nil, S-up will increase."
  2977. :group 'org-time
  2978. :type 'boolean)
  2979. (defcustom org-calendar-follow-timestamp-change t
  2980. "Non-nil means make the calendar window follow timestamp changes.
  2981. When a timestamp is modified and the calendar window is visible, it will be
  2982. moved to the new date."
  2983. :group 'org-time
  2984. :type 'boolean)
  2985. (defgroup org-tags nil
  2986. "Options concerning tags in Org-mode."
  2987. :tag "Org Tags"
  2988. :group 'org)
  2989. (defcustom org-tag-alist nil
  2990. "List of tags allowed in Org-mode files.
  2991. When this list is nil, Org-mode will base TAG input on what is already in the
  2992. buffer.
  2993. The value of this variable is an alist, the car of each entry must be a
  2994. keyword as a string, the cdr may be a character that is used to select
  2995. that tag through the fast-tag-selection interface.
  2996. See the manual for details."
  2997. :group 'org-tags
  2998. :type '(repeat
  2999. (choice
  3000. (cons (string :tag "Tag name")
  3001. (character :tag "Access char"))
  3002. (list :tag "Start radio group"
  3003. (const :startgroup)
  3004. (option (string :tag "Group description")))
  3005. (list :tag "Start tag group, non distinct"
  3006. (const :startgrouptag)
  3007. (option (string :tag "Group description")))
  3008. (list :tag "Group tags delimiter"
  3009. (const :grouptags))
  3010. (list :tag "End radio group"
  3011. (const :endgroup)
  3012. (option (string :tag "Group description")))
  3013. (list :tag "End tag group, non distinct"
  3014. (const :endgrouptag)
  3015. (option (string :tag "Group description")))
  3016. (const :tag "New line" (:newline)))))
  3017. (defcustom org-tag-persistent-alist nil
  3018. "List of tags that will always appear in all Org-mode files.
  3019. This is in addition to any in buffer settings or customizations
  3020. of `org-tag-alist'.
  3021. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3022. The value of this variable is an alist, the car of each entry must be a
  3023. keyword as a string, the cdr may be a character that is used to select
  3024. that tag through the fast-tag-selection interface.
  3025. See the manual for details.
  3026. To disable these tags on a per-file basis, insert anywhere in the file:
  3027. #+STARTUP: noptag"
  3028. :group 'org-tags
  3029. :type '(repeat
  3030. (choice
  3031. (cons (string :tag "Tag name")
  3032. (character :tag "Access char"))
  3033. (const :tag "Start radio group" (:startgroup))
  3034. (const :tag "Group tags delimiter" (:grouptags))
  3035. (const :tag "End radio group" (:endgroup))
  3036. (const :tag "New line" (:newline)))))
  3037. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3038. "If non-nil, always offer completion for all tags of all agenda files.
  3039. Instead of customizing this variable directly, you might want to
  3040. set it locally for capture buffers, because there no list of
  3041. tags in that file can be created dynamically (there are none).
  3042. (add-hook 'org-capture-mode-hook
  3043. (lambda ()
  3044. (set (make-local-variable
  3045. 'org-complete-tags-always-offer-all-agenda-tags)
  3046. t)))"
  3047. :group 'org-tags
  3048. :version "24.1"
  3049. :type 'boolean)
  3050. (defvar org-file-tags nil
  3051. "List of tags that can be inherited by all entries in the file.
  3052. The tags will be inherited if the variable `org-use-tag-inheritance'
  3053. says they should be.
  3054. This variable is populated from #+FILETAGS lines.")
  3055. (defcustom org-use-fast-tag-selection 'auto
  3056. "Non-nil means use fast tag selection scheme.
  3057. This is a special interface to select and deselect tags with single keys.
  3058. When nil, fast selection is never used.
  3059. When the symbol `auto', fast selection is used if and only if selection
  3060. characters for tags have been configured, either through the variable
  3061. `org-tag-alist' or through a #+TAGS line in the buffer.
  3062. When t, fast selection is always used and selection keys are assigned
  3063. automatically if necessary."
  3064. :group 'org-tags
  3065. :type '(choice
  3066. (const :tag "Always" t)
  3067. (const :tag "Never" nil)
  3068. (const :tag "When selection characters are configured" auto)))
  3069. (defcustom org-fast-tag-selection-single-key nil
  3070. "Non-nil means fast tag selection exits after first change.
  3071. When nil, you have to press RET to exit it.
  3072. During fast tag selection, you can toggle this flag with `C-c'.
  3073. This variable can also have the value `expert'. In this case, the window
  3074. displaying the tags menu is not even shown, until you press C-c again."
  3075. :group 'org-tags
  3076. :type '(choice
  3077. (const :tag "No" nil)
  3078. (const :tag "Yes" t)
  3079. (const :tag "Expert" expert)))
  3080. (defvar org-fast-tag-selection-include-todo nil
  3081. "Non-nil means fast tags selection interface will also offer TODO states.
  3082. This is an undocumented feature, you should not rely on it.")
  3083. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3084. "The column to which tags should be indented in a headline.
  3085. If this number is positive, it specifies the column. If it is negative,
  3086. it means that the tags should be flushright to that column. For example,
  3087. -80 works well for a normal 80 character screen.
  3088. When 0, place tags directly after headline text, with only one space in
  3089. between."
  3090. :group 'org-tags
  3091. :type 'integer)
  3092. (defcustom org-auto-align-tags t
  3093. "Non-nil keeps tags aligned when modifying headlines.
  3094. Some operations (i.e. demoting) change the length of a headline and
  3095. therefore shift the tags around. With this option turned on, after
  3096. each such operation the tags are again aligned to `org-tags-column'."
  3097. :group 'org-tags
  3098. :type 'boolean)
  3099. (defcustom org-use-tag-inheritance t
  3100. "Non-nil means tags in levels apply also for sublevels.
  3101. When nil, only the tags directly given in a specific line apply there.
  3102. This may also be a list of tags that should be inherited, or a regexp that
  3103. matches tags that should be inherited. Additional control is possible
  3104. with the variable `org-tags-exclude-from-inheritance' which gives an
  3105. explicit list of tags to be excluded from inheritance, even if the value of
  3106. `org-use-tag-inheritance' would select it for inheritance.
  3107. If this option is t, a match early-on in a tree can lead to a large
  3108. number of matches in the subtree when constructing the agenda or creating
  3109. a sparse tree. If you only want to see the first match in a tree during
  3110. a search, check out the variable `org-tags-match-list-sublevels'."
  3111. :group 'org-tags
  3112. :type '(choice
  3113. (const :tag "Not" nil)
  3114. (const :tag "Always" t)
  3115. (repeat :tag "Specific tags" (string :tag "Tag"))
  3116. (regexp :tag "Tags matched by regexp")))
  3117. (defcustom org-tags-exclude-from-inheritance nil
  3118. "List of tags that should never be inherited.
  3119. This is a way to exclude a few tags from inheritance. For way to do
  3120. the opposite, to actively allow inheritance for selected tags,
  3121. see the variable `org-use-tag-inheritance'."
  3122. :group 'org-tags
  3123. :type '(repeat (string :tag "Tag")))
  3124. (defun org-tag-inherit-p (tag)
  3125. "Check if TAG is one that should be inherited."
  3126. (cond
  3127. ((member tag org-tags-exclude-from-inheritance) nil)
  3128. ((eq org-use-tag-inheritance t) t)
  3129. ((not org-use-tag-inheritance) nil)
  3130. ((stringp org-use-tag-inheritance)
  3131. (string-match org-use-tag-inheritance tag))
  3132. ((listp org-use-tag-inheritance)
  3133. (member tag org-use-tag-inheritance))
  3134. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3135. (defcustom org-tags-match-list-sublevels t
  3136. "Non-nil means list also sublevels of headlines matching a search.
  3137. This variable applies to tags/property searches, and also to stuck
  3138. projects because this search is based on a tags match as well.
  3139. When set to the symbol `indented', sublevels are indented with
  3140. leading dots.
  3141. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3142. the sublevels of a headline matching a tag search often also match
  3143. the same search. Listing all of them can create very long lists.
  3144. Setting this variable to nil causes subtrees of a match to be skipped.
  3145. This variable is semi-obsolete and probably should always be true. It
  3146. is better to limit inheritance to certain tags using the variables
  3147. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3148. :group 'org-tags
  3149. :type '(choice
  3150. (const :tag "No, don't list them" nil)
  3151. (const :tag "Yes, do list them" t)
  3152. (const :tag "List them, indented with leading dots" indented)))
  3153. (defcustom org-tags-sort-function nil
  3154. "When set, tags are sorted using this function as a comparator."
  3155. :group 'org-tags
  3156. :type '(choice
  3157. (const :tag "No sorting" nil)
  3158. (const :tag "Alphabetical" string<)
  3159. (const :tag "Reverse alphabetical" string>)
  3160. (function :tag "Custom function" nil)))
  3161. (defvar org-tags-history nil
  3162. "History of minibuffer reads for tags.")
  3163. (defvar org-last-tags-completion-table nil
  3164. "The last used completion table for tags.")
  3165. (defvar org-after-tags-change-hook nil
  3166. "Hook that is run after the tags in a line have changed.")
  3167. (defgroup org-properties nil
  3168. "Options concerning properties in Org-mode."
  3169. :tag "Org Properties"
  3170. :group 'org)
  3171. (defcustom org-property-format "%-10s %s"
  3172. "How property key/value pairs should be formatted by `indent-line'.
  3173. When `indent-line' hits a property definition, it will format the line
  3174. according to this format, mainly to make sure that the values are
  3175. lined-up with respect to each other."
  3176. :group 'org-properties
  3177. :type 'string)
  3178. (defcustom org-properties-postprocess-alist nil
  3179. "Alist of properties and functions to adjust inserted values.
  3180. Elements of this alist must be of the form
  3181. ([string] [function])
  3182. where [string] must be a property name and [function] must be a
  3183. lambda expression: this lambda expression must take one argument,
  3184. the value to adjust, and return the new value as a string.
  3185. For example, this element will allow the property \"Remaining\"
  3186. to be updated wrt the relation between the \"Effort\" property
  3187. and the clock summary:
  3188. ((\"Remaining\" (lambda(value)
  3189. (let ((clocksum (org-clock-sum-current-item))
  3190. (effort (org-duration-string-to-minutes
  3191. (org-entry-get (point) \"Effort\"))))
  3192. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3193. :group 'org-properties
  3194. :version "24.1"
  3195. :type '(alist :key-type (string :tag "Property")
  3196. :value-type (function :tag "Function")))
  3197. (defcustom org-use-property-inheritance nil
  3198. "Non-nil means properties apply also for sublevels.
  3199. This setting is chiefly used during property searches. Turning it on can
  3200. cause significant overhead when doing a search, which is why it is not
  3201. on by default.
  3202. When nil, only the properties directly given in the current entry count.
  3203. When t, every property is inherited. The value may also be a list of
  3204. properties that should have inheritance, or a regular expression matching
  3205. properties that should be inherited.
  3206. However, note that some special properties use inheritance under special
  3207. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3208. and the properties ending in \"_ALL\" when they are used as descriptor
  3209. for valid values of a property.
  3210. Note for programmers:
  3211. When querying an entry with `org-entry-get', you can control if inheritance
  3212. should be used. By default, `org-entry-get' looks only at the local
  3213. properties. You can request inheritance by setting the inherit argument
  3214. to t (to force inheritance) or to `selective' (to respect the setting
  3215. in this variable)."
  3216. :group 'org-properties
  3217. :type '(choice
  3218. (const :tag "Not" nil)
  3219. (const :tag "Always" t)
  3220. (repeat :tag "Specific properties" (string :tag "Property"))
  3221. (regexp :tag "Properties matched by regexp")))
  3222. (defun org-property-inherit-p (property)
  3223. "Check if PROPERTY is one that should be inherited."
  3224. (cond
  3225. ((eq org-use-property-inheritance t) t)
  3226. ((not org-use-property-inheritance) nil)
  3227. ((stringp org-use-property-inheritance)
  3228. (string-match org-use-property-inheritance property))
  3229. ((listp org-use-property-inheritance)
  3230. (member property org-use-property-inheritance))
  3231. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3232. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3233. "The default column format, if no other format has been defined.
  3234. This variable can be set on the per-file basis by inserting a line
  3235. #+COLUMNS: %25ITEM ....."
  3236. :group 'org-properties
  3237. :type 'string)
  3238. (defcustom org-columns-ellipses ".."
  3239. "The ellipses to be used when a field in column view is truncated.
  3240. When this is the empty string, as many characters as possible are shown,
  3241. but then there will be no visual indication that the field has been truncated.
  3242. When this is a string of length N, the last N characters of a truncated
  3243. field are replaced by this string. If the column is narrower than the
  3244. ellipses string, only part of the ellipses string will be shown."
  3245. :group 'org-properties
  3246. :type 'string)
  3247. (defcustom org-columns-modify-value-for-display-function nil
  3248. "Function that modifies values for display in column view.
  3249. For example, it can be used to cut out a certain part from a time stamp.
  3250. The function must take 2 arguments:
  3251. column-title The title of the column (*not* the property name)
  3252. value The value that should be modified.
  3253. The function should return the value that should be displayed,
  3254. or nil if the normal value should be used."
  3255. :group 'org-properties
  3256. :type '(choice (const nil) (function)))
  3257. (defconst org-global-properties-fixed
  3258. '(("VISIBILITY_ALL" . "folded children content all")
  3259. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3260. "List of property/value pairs that can be inherited by any entry.
  3261. These are fixed values, for the preset properties. The user variable
  3262. that can be used to add to this list is `org-global-properties'.
  3263. The entries in this list are cons cells where the car is a property
  3264. name and cdr is a string with the value. If the value represents
  3265. multiple items like an \"_ALL\" property, separate the items by
  3266. spaces.")
  3267. (defcustom org-global-properties nil
  3268. "List of property/value pairs that can be inherited by any entry.
  3269. This list will be combined with the constant `org-global-properties-fixed'.
  3270. The entries in this list are cons cells where the car is a property
  3271. name and cdr is a string with the value.
  3272. You can set buffer-local values for the same purpose in the variable
  3273. `org-file-properties' this by adding lines like
  3274. #+PROPERTY: NAME VALUE"
  3275. :group 'org-properties
  3276. :type '(repeat
  3277. (cons (string :tag "Property")
  3278. (string :tag "Value"))))
  3279. (defvar org-file-properties nil
  3280. "List of property/value pairs that can be inherited by any entry.
  3281. Valid for the current buffer.
  3282. This variable is populated from #+PROPERTY lines.")
  3283. (make-variable-buffer-local 'org-file-properties)
  3284. (defgroup org-agenda nil
  3285. "Options concerning agenda views in Org-mode."
  3286. :tag "Org Agenda"
  3287. :group 'org)
  3288. (defvar org-category nil
  3289. "Variable used by org files to set a category for agenda display.
  3290. Such files should use a file variable to set it, for example
  3291. # -*- mode: org; org-category: \"ELisp\"
  3292. or contain a special line
  3293. #+CATEGORY: ELisp
  3294. If the file does not specify a category, then file's base name
  3295. is used instead.")
  3296. (make-variable-buffer-local 'org-category)
  3297. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3298. (defcustom org-agenda-files nil
  3299. "The files to be used for agenda display.
  3300. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3301. \\[org-remove-file]. You can also use customize to edit the list.
  3302. If an entry is a directory, all files in that directory that are matched by
  3303. `org-agenda-file-regexp' will be part of the file list.
  3304. If the value of the variable is not a list but a single file name, then
  3305. the list of agenda files is actually stored and maintained in that file, one
  3306. agenda file per line. In this file paths can be given relative to
  3307. `org-directory'. Tilde expansion and environment variable substitution
  3308. are also made."
  3309. :group 'org-agenda
  3310. :type '(choice
  3311. (repeat :tag "List of files and directories" file)
  3312. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3313. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3314. "Regular expression to match files for `org-agenda-files'.
  3315. If any element in the list in that variable contains a directory instead
  3316. of a normal file, all files in that directory that are matched by this
  3317. regular expression will be included."
  3318. :group 'org-agenda
  3319. :type 'regexp)
  3320. (defcustom org-agenda-text-search-extra-files nil
  3321. "List of extra files to be searched by text search commands.
  3322. These files will be searched in addition to the agenda files by the
  3323. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3324. Note that these files will only be searched for text search commands,
  3325. not for the other agenda views like todo lists, tag searches or the weekly
  3326. agenda. This variable is intended to list notes and possibly archive files
  3327. that should also be searched by these two commands.
  3328. In fact, if the first element in the list is the symbol `agenda-archives',
  3329. then all archive files of all agenda files will be added to the search
  3330. scope."
  3331. :group 'org-agenda
  3332. :type '(set :greedy t
  3333. (const :tag "Agenda Archives" agenda-archives)
  3334. (repeat :inline t (file))))
  3335. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3336. 'org-agenda-text-search-extra-files)
  3337. (defcustom org-agenda-skip-unavailable-files nil
  3338. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3339. A nil value means to remove them, after a query, from the list."
  3340. :group 'org-agenda
  3341. :type 'boolean)
  3342. (defcustom org-calendar-to-agenda-key [?c]
  3343. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3344. The command `org-calendar-goto-agenda' will be bound to this key. The
  3345. default is the character `c' because then `c' can be used to switch back and
  3346. forth between agenda and calendar."
  3347. :group 'org-agenda
  3348. :type 'sexp)
  3349. (defcustom org-calendar-insert-diary-entry-key [?i]
  3350. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3351. This option is irrelevant until `org-agenda-diary-file' has been configured
  3352. to point to an Org-mode file. When that is the case, the command
  3353. `org-agenda-diary-entry' will be bound to the key given here, by default
  3354. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3355. if you want to continue doing this, you need to change this to a different
  3356. key."
  3357. :group 'org-agenda
  3358. :type 'sexp)
  3359. (defcustom org-agenda-diary-file 'diary-file
  3360. "File to which to add new entries with the `i' key in agenda and calendar.
  3361. When this is the symbol `diary-file', the functionality in the Emacs
  3362. calendar will be used to add entries to the `diary-file'. But when this
  3363. points to a file, `org-agenda-diary-entry' will be used instead."
  3364. :group 'org-agenda
  3365. :type '(choice
  3366. (const :tag "The standard Emacs diary file" diary-file)
  3367. (file :tag "Special Org file diary entries")))
  3368. (eval-after-load "calendar"
  3369. '(progn
  3370. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3371. 'org-calendar-goto-agenda)
  3372. (add-hook 'calendar-mode-hook
  3373. (lambda ()
  3374. (unless (eq org-agenda-diary-file 'diary-file)
  3375. (define-key calendar-mode-map
  3376. org-calendar-insert-diary-entry-key
  3377. 'org-agenda-diary-entry))))))
  3378. (defgroup org-latex nil
  3379. "Options for embedding LaTeX code into Org-mode."
  3380. :tag "Org LaTeX"
  3381. :group 'org)
  3382. (defcustom org-format-latex-options
  3383. '(:foreground default :background default :scale 1.0
  3384. :html-foreground "Black" :html-background "Transparent"
  3385. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3386. "Options for creating images from LaTeX fragments.
  3387. This is a property list with the following properties:
  3388. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3389. `default' means use the foreground of the default face.
  3390. `auto' means use the foreground from the text face.
  3391. :background the background color, or \"Transparent\".
  3392. `default' means use the background of the default face.
  3393. `auto' means use the background from the text face.
  3394. :scale a scaling factor for the size of the images, to get more pixels
  3395. :html-foreground, :html-background, :html-scale
  3396. the same numbers for HTML export.
  3397. :matchers a list indicating which matchers should be used to
  3398. find LaTeX fragments. Valid members of this list are:
  3399. \"begin\" find environments
  3400. \"$1\" find single characters surrounded by $.$
  3401. \"$\" find math expressions surrounded by $...$
  3402. \"$$\" find math expressions surrounded by $$....$$
  3403. \"\\(\" find math expressions surrounded by \\(...\\)
  3404. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3405. :group 'org-latex
  3406. :type 'plist)
  3407. (defcustom org-format-latex-signal-error t
  3408. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3409. When nil, just push out a message."
  3410. :group 'org-latex
  3411. :version "24.1"
  3412. :type 'boolean)
  3413. (defcustom org-latex-to-mathml-jar-file nil
  3414. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3415. Use this to specify additional executable file say a jar file.
  3416. When using MathToWeb as the converter, specify the full-path to
  3417. your mathtoweb.jar file."
  3418. :group 'org-latex
  3419. :version "24.1"
  3420. :type '(choice
  3421. (const :tag "None" nil)
  3422. (file :tag "JAR file" :must-match t)))
  3423. (defcustom org-latex-to-mathml-convert-command nil
  3424. "Command to convert LaTeX fragments to MathML.
  3425. Replace format-specifiers in the command as noted below and use
  3426. `shell-command' to convert LaTeX to MathML.
  3427. %j: Executable file in fully expanded form as specified by
  3428. `org-latex-to-mathml-jar-file'.
  3429. %I: Input LaTeX file in fully expanded form.
  3430. %i: The latex fragment to be converted.
  3431. %o: Output MathML file.
  3432. This command is used by `org-create-math-formula'.
  3433. When using MathToWeb as the converter, set this option to
  3434. \"java -jar %j -unicode -force -df %o %I\".
  3435. When using LaTeXML set this option to
  3436. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3437. :group 'org-latex
  3438. :version "24.1"
  3439. :type '(choice
  3440. (const :tag "None" nil)
  3441. (string :tag "\nShell command")))
  3442. (defcustom org-latex-create-formula-image-program 'dvipng
  3443. "Program to convert LaTeX fragments with.
  3444. dvipng Process the LaTeX fragments to dvi file, then convert
  3445. dvi files to png files using dvipng.
  3446. This will also include processing of non-math environments.
  3447. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3448. to convert pdf files to png files"
  3449. :group 'org-latex
  3450. :version "24.1"
  3451. :type '(choice
  3452. (const :tag "dvipng" dvipng)
  3453. (const :tag "imagemagick" imagemagick)))
  3454. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3455. "Path to store latex preview images.
  3456. A relative path here creates many directories relative to the
  3457. processed org files paths. An absolute path puts all preview
  3458. images at the same place."
  3459. :group 'org-latex
  3460. :version "24.3"
  3461. :type 'string)
  3462. (defun org-format-latex-mathml-available-p ()
  3463. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3464. (save-match-data
  3465. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3466. org-latex-to-mathml-convert-command)
  3467. (let ((executable (car (split-string
  3468. org-latex-to-mathml-convert-command))))
  3469. (when (executable-find executable)
  3470. (if (string-match
  3471. "%j" org-latex-to-mathml-convert-command)
  3472. (file-readable-p org-latex-to-mathml-jar-file)
  3473. t))))))
  3474. (defcustom org-format-latex-header "\\documentclass{article}
  3475. \\usepackage[usenames]{color}
  3476. \[PACKAGES]
  3477. \[DEFAULT-PACKAGES]
  3478. \\pagestyle{empty} % do not remove
  3479. % The settings below are copied from fullpage.sty
  3480. \\setlength{\\textwidth}{\\paperwidth}
  3481. \\addtolength{\\textwidth}{-3cm}
  3482. \\setlength{\\oddsidemargin}{1.5cm}
  3483. \\addtolength{\\oddsidemargin}{-2.54cm}
  3484. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3485. \\setlength{\\textheight}{\\paperheight}
  3486. \\addtolength{\\textheight}{-\\headheight}
  3487. \\addtolength{\\textheight}{-\\headsep}
  3488. \\addtolength{\\textheight}{-\\footskip}
  3489. \\addtolength{\\textheight}{-3cm}
  3490. \\setlength{\\topmargin}{1.5cm}
  3491. \\addtolength{\\topmargin}{-2.54cm}"
  3492. "The document header used for processing LaTeX fragments.
  3493. It is imperative that this header make sure that no page number
  3494. appears on the page. The package defined in the variables
  3495. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3496. will either replace the placeholder \"[PACKAGES]\" in this
  3497. header, or they will be appended."
  3498. :group 'org-latex
  3499. :type 'string)
  3500. (defun org-set-packages-alist (var val)
  3501. "Set the packages alist and make sure it has 3 elements per entry."
  3502. (set var (mapcar (lambda (x)
  3503. (if (and (consp x) (= (length x) 2))
  3504. (list (car x) (nth 1 x) t)
  3505. x))
  3506. val)))
  3507. (defun org-get-packages-alist (var)
  3508. "Get the packages alist and make sure it has 3 elements per entry."
  3509. (mapcar (lambda (x)
  3510. (if (and (consp x) (= (length x) 2))
  3511. (list (car x) (nth 1 x) t)
  3512. x))
  3513. (default-value var)))
  3514. (defcustom org-latex-default-packages-alist
  3515. '(("AUTO" "inputenc" t)
  3516. ("T1" "fontenc" t)
  3517. ("" "fixltx2e" nil)
  3518. ("" "graphicx" t)
  3519. ("" "grffile" t)
  3520. ("" "longtable" nil)
  3521. ("" "wrapfig" nil)
  3522. ("" "rotating" nil)
  3523. ("normalem" "ulem" t)
  3524. ("" "amsmath" t)
  3525. ("" "textcomp" t)
  3526. ("" "amssymb" t)
  3527. ("" "capt-of" nil)
  3528. ("" "hyperref" nil))
  3529. "Alist of default packages to be inserted in the header.
  3530. Change this only if one of the packages here causes an
  3531. incompatibility with another package you are using.
  3532. The packages in this list are needed by one part or another of
  3533. Org mode to function properly:
  3534. - inputenc, fontenc: for basic font and character selection
  3535. - fixltx2e: Important patches of LaTeX itself
  3536. - graphicx: for including images
  3537. - grffile: allow periods and spaces in graphics file names
  3538. - longtable: For multipage tables
  3539. - wrapfig: for figure placement
  3540. - rotating: for sideways figures and tables
  3541. - ulem: for underline and strike-through
  3542. - amsmath: for subscript and superscript and math environments
  3543. - textcomp, amssymb: for various symbols used
  3544. for interpreting the entities in `org-entities'. You can skip
  3545. some of these packages if you don't use any of their symbols.
  3546. - capt-of: for captions outside of floats
  3547. - hyperref: for cross references
  3548. Therefore you should not modify this variable unless you know
  3549. what you are doing. The one reason to change it anyway is that
  3550. you might be loading some other package that conflicts with one
  3551. of the default packages. Each element is either a cell or
  3552. a string.
  3553. A cell is of the format
  3554. \(\"options\" \"package\" SNIPPET-FLAG)
  3555. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3556. when compiling LaTeX snippets into images for inclusion into
  3557. non-LaTeX output.
  3558. A string will be inserted as-is in the header of the document."
  3559. :group 'org-latex
  3560. :group 'org-export-latex
  3561. :set 'org-set-packages-alist
  3562. :get 'org-get-packages-alist
  3563. :version "25.1"
  3564. :package-version '(Org . "8.3")
  3565. :type '(repeat
  3566. (choice
  3567. (list :tag "options/package pair"
  3568. (string :tag "options")
  3569. (string :tag "package")
  3570. (boolean :tag "Snippet"))
  3571. (string :tag "A line of LaTeX"))))
  3572. (defcustom org-latex-packages-alist nil
  3573. "Alist of packages to be inserted in every LaTeX header.
  3574. These will be inserted after `org-latex-default-packages-alist'.
  3575. Each element is either a cell or a string.
  3576. A cell is of the format:
  3577. \(\"options\" \"package\" SNIPPET-FLAG)
  3578. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3579. needed when turning LaTeX snippets into images for inclusion into
  3580. non-LaTeX output.
  3581. A string will be inserted as-is in the header of the document.
  3582. Make sure that you only list packages here which:
  3583. - you want in every file;
  3584. - do not conflict with the setup in `org-format-latex-header';
  3585. - do not conflict with the default packages in
  3586. `org-latex-default-packages-alist'."
  3587. :group 'org-latex
  3588. :group 'org-export-latex
  3589. :set 'org-set-packages-alist
  3590. :get 'org-get-packages-alist
  3591. :type '(repeat
  3592. (choice
  3593. (list :tag "options/package pair"
  3594. (string :tag "options")
  3595. (string :tag "package")
  3596. (boolean :tag "Snippet"))
  3597. (string :tag "A line of LaTeX"))))
  3598. (defgroup org-appearance nil
  3599. "Settings for Org-mode appearance."
  3600. :tag "Org Appearance"
  3601. :group 'org)
  3602. (defcustom org-level-color-stars-only nil
  3603. "Non-nil means fontify only the stars in each headline.
  3604. When nil, the entire headline is fontified.
  3605. Changing it requires restart of `font-lock-mode' to become effective
  3606. also in regions already fontified."
  3607. :group 'org-appearance
  3608. :type 'boolean)
  3609. (defcustom org-hide-leading-stars nil
  3610. "Non-nil means hide the first N-1 stars in a headline.
  3611. This works by using the face `org-hide' for these stars. This
  3612. face is white for a light background, and black for a dark
  3613. background. You may have to customize the face `org-hide' to
  3614. make this work.
  3615. Changing it requires restart of `font-lock-mode' to become effective
  3616. also in regions already fontified.
  3617. You may also set this on a per-file basis by adding one of the following
  3618. lines to the buffer:
  3619. #+STARTUP: hidestars
  3620. #+STARTUP: showstars"
  3621. :group 'org-appearance
  3622. :type 'boolean)
  3623. (defcustom org-hidden-keywords nil
  3624. "List of symbols corresponding to keywords to be hidden the org buffer.
  3625. For example, a value '(title) for this list will make the document's title
  3626. appear in the buffer without the initial #+TITLE: keyword."
  3627. :group 'org-appearance
  3628. :version "24.1"
  3629. :type '(set (const :tag "#+AUTHOR" author)
  3630. (const :tag "#+DATE" date)
  3631. (const :tag "#+EMAIL" email)
  3632. (const :tag "#+TITLE" title)))
  3633. (defcustom org-custom-properties nil
  3634. "List of properties (as strings) with a special meaning.
  3635. The default use of these custom properties is to let the user
  3636. hide them with `org-toggle-custom-properties-visibility'."
  3637. :group 'org-properties
  3638. :group 'org-appearance
  3639. :version "24.3"
  3640. :type '(repeat (string :tag "Property Name")))
  3641. (defcustom org-fontify-done-headline nil
  3642. "Non-nil means change the face of a headline if it is marked DONE.
  3643. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3644. When this is non-nil, the headline after the keyword is set to the
  3645. `org-headline-done' as an additional indication."
  3646. :group 'org-appearance
  3647. :type 'boolean)
  3648. (defcustom org-fontify-emphasized-text t
  3649. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3650. Changing this variable requires a restart of Emacs to take effect."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-fontify-whole-heading-line nil
  3654. "Non-nil means fontify the whole line for headings.
  3655. This is useful when setting a background color for the
  3656. org-level-* faces."
  3657. :group 'org-appearance
  3658. :type 'boolean)
  3659. (defcustom org-highlight-latex-and-related nil
  3660. "Non-nil means highlight LaTeX related syntax in the buffer.
  3661. When non nil, the value should be a list containing any of the
  3662. following symbols:
  3663. `latex' Highlight LaTeX snippets and environments.
  3664. `script' Highlight subscript and superscript.
  3665. `entities' Highlight entities."
  3666. :group 'org-appearance
  3667. :version "24.4"
  3668. :package-version '(Org . "8.0")
  3669. :type '(choice
  3670. (const :tag "No highlighting" nil)
  3671. (set :greedy t :tag "Highlight"
  3672. (const :tag "LaTeX snippets and environments" latex)
  3673. (const :tag "Subscript and superscript" script)
  3674. (const :tag "Entities" entities))))
  3675. (defcustom org-hide-emphasis-markers nil
  3676. "Non-nil mean font-lock should hide the emphasis marker characters."
  3677. :group 'org-appearance
  3678. :type 'boolean)
  3679. (defcustom org-hide-macro-markers nil
  3680. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3681. :group 'org-appearance
  3682. :type 'boolean)
  3683. (defcustom org-pretty-entities nil
  3684. "Non-nil means show entities as UTF8 characters.
  3685. When nil, the \\name form remains in the buffer."
  3686. :group 'org-appearance
  3687. :version "24.1"
  3688. :type 'boolean)
  3689. (defcustom org-pretty-entities-include-sub-superscripts t
  3690. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3691. :group 'org-appearance
  3692. :version "24.1"
  3693. :type 'boolean)
  3694. (defvar org-emph-re nil
  3695. "Regular expression for matching emphasis.
  3696. After a match, the match groups contain these elements:
  3697. 0 The match of the full regular expression, including the characters
  3698. before and after the proper match
  3699. 1 The character before the proper match, or empty at beginning of line
  3700. 2 The proper match, including the leading and trailing markers
  3701. 3 The leading marker like * or /, indicating the type of highlighting
  3702. 4 The text between the emphasis markers, not including the markers
  3703. 5 The character after the match, empty at the end of a line")
  3704. (defvar org-verbatim-re nil
  3705. "Regular expression for matching verbatim text.")
  3706. (defvar org-emphasis-regexp-components) ; defined just below
  3707. (defvar org-emphasis-alist) ; defined just below
  3708. (defun org-set-emph-re (var val)
  3709. "Set variable and compute the emphasis regular expression."
  3710. (set var val)
  3711. (when (and (boundp 'org-emphasis-alist)
  3712. (boundp 'org-emphasis-regexp-components)
  3713. org-emphasis-alist org-emphasis-regexp-components)
  3714. (let* ((e org-emphasis-regexp-components)
  3715. (pre (car e))
  3716. (post (nth 1 e))
  3717. (border (nth 2 e))
  3718. (body (nth 3 e))
  3719. (nl (nth 4 e))
  3720. (body1 (concat body "*?"))
  3721. (markers (mapconcat 'car org-emphasis-alist ""))
  3722. (vmarkers (mapconcat
  3723. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3724. org-emphasis-alist "")))
  3725. ;; make sure special characters appear at the right position in the class
  3726. (if (string-match "\\^" markers)
  3727. (setq markers (concat (replace-match "" t t markers) "^")))
  3728. (if (string-match "-" markers)
  3729. (setq markers (concat (replace-match "" t t markers) "-")))
  3730. (if (string-match "\\^" vmarkers)
  3731. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3732. (if (string-match "-" vmarkers)
  3733. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3734. (if (> nl 0)
  3735. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3736. (int-to-string nl) "\\}")))
  3737. ;; Make the regexp
  3738. (setq org-emph-re
  3739. (concat "\\([" pre "]\\|^\\)"
  3740. "\\("
  3741. "\\([" markers "]\\)"
  3742. "\\("
  3743. "[^" border "]\\|"
  3744. "[^" border "]"
  3745. body1
  3746. "[^" border "]"
  3747. "\\)"
  3748. "\\3\\)"
  3749. "\\([" post "]\\|$\\)"))
  3750. (setq org-verbatim-re
  3751. (concat "\\([" pre "]\\|^\\)"
  3752. "\\("
  3753. "\\([" vmarkers "]\\)"
  3754. "\\("
  3755. "[^" border "]\\|"
  3756. "[^" border "]"
  3757. body1
  3758. "[^" border "]"
  3759. "\\)"
  3760. "\\3\\)"
  3761. "\\([" post "]\\|$\\)")))))
  3762. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3763. ;; set this option proved cumbersome. See this message/thread:
  3764. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3765. (defvar org-emphasis-regexp-components
  3766. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3767. "Components used to build the regular expression for emphasis.
  3768. This is a list with five entries. Terminology: In an emphasis string
  3769. like \" *strong word* \", we call the initial space PREMATCH, the final
  3770. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3771. and \"trong wor\" is the body. The different components in this variable
  3772. specify what is allowed/forbidden in each part:
  3773. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3774. post Chars allowed as postmatch. End of line will be allowed too.
  3775. border The chars *forbidden* as border characters.
  3776. body-regexp A regexp like \".\" to match a body character. Don't use
  3777. non-shy groups here, and don't allow newline here.
  3778. newline The maximum number of newlines allowed in an emphasis exp.
  3779. You need to reload Org or to restart Emacs after customizing this.")
  3780. (defcustom org-emphasis-alist
  3781. `(("*" bold)
  3782. ("/" italic)
  3783. ("_" underline)
  3784. ("=" org-verbatim verbatim)
  3785. ("~" org-code verbatim)
  3786. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3787. "Alist of characters and faces to emphasize text.
  3788. Text starting and ending with a special character will be emphasized,
  3789. for example *bold*, _underlined_ and /italic/. This variable sets the
  3790. marker characters and the face to be used by font-lock for highlighting
  3791. in Org-mode Emacs buffers.
  3792. You need to reload Org or to restart Emacs after customizing this."
  3793. :group 'org-appearance
  3794. :set 'org-set-emph-re
  3795. :version "24.4"
  3796. :package-version '(Org . "8.0")
  3797. :type '(repeat
  3798. (list
  3799. (string :tag "Marker character")
  3800. (choice
  3801. (face :tag "Font-lock-face")
  3802. (plist :tag "Face property list"))
  3803. (option (const verbatim)))))
  3804. (defvar org-protecting-blocks
  3805. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3806. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3807. This is needed for font-lock setup.")
  3808. ;;; Miscellaneous options
  3809. (defgroup org-completion nil
  3810. "Completion in Org-mode."
  3811. :tag "Org Completion"
  3812. :group 'org)
  3813. (defcustom org-completion-use-ido nil
  3814. "Non-nil means use ido completion wherever possible.
  3815. Note that `ido-mode' must be active for this variable to be relevant.
  3816. If you decide to turn this variable on, you might well want to turn off
  3817. `org-outline-path-complete-in-steps'.
  3818. See also `org-completion-use-iswitchb'."
  3819. :group 'org-completion
  3820. :type 'boolean)
  3821. (defcustom org-completion-use-iswitchb nil
  3822. "Non-nil means use iswitchb completion wherever possible.
  3823. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3824. If you decide to turn this variable on, you might well want to turn off
  3825. `org-outline-path-complete-in-steps'.
  3826. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3827. :group 'org-completion
  3828. :type 'boolean)
  3829. (defcustom org-completion-fallback-command 'hippie-expand
  3830. "The expansion command called by \\[pcomplete] in normal context.
  3831. Normal means, no org-mode-specific context."
  3832. :group 'org-completion
  3833. :type 'function)
  3834. ;;; Functions and variables from their packages
  3835. ;; Declared here to avoid compiler warnings
  3836. ;; XEmacs only
  3837. (defvar outline-mode-menu-heading)
  3838. (defvar outline-mode-menu-show)
  3839. (defvar outline-mode-menu-hide)
  3840. (defvar zmacs-regions) ; XEmacs regions
  3841. ;; Emacs only
  3842. (defvar mark-active)
  3843. ;; Various packages
  3844. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3845. (declare-function calendar-forward-day "cal-move" (arg))
  3846. (declare-function calendar-goto-date "cal-move" (date))
  3847. (declare-function calendar-goto-today "cal-move" ())
  3848. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3849. (defvar calc-embedded-close-formula)
  3850. (defvar calc-embedded-open-formula)
  3851. (declare-function cdlatex-tab "ext:cdlatex" ())
  3852. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3853. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3854. (defvar font-lock-unfontify-region-function)
  3855. (declare-function iswitchb-read-buffer "iswitchb"
  3856. (prompt &optional default require-match start matches-set))
  3857. (defvar iswitchb-temp-buflist)
  3858. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3859. (defvar org-agenda-tags-todo-honor-ignore-options)
  3860. (declare-function org-agenda-skip "org-agenda" ())
  3861. (declare-function
  3862. org-agenda-format-item "org-agenda"
  3863. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3864. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3865. (declare-function org-agenda-change-all-lines "org-agenda"
  3866. (newhead hdmarker &optional fixface just-this))
  3867. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3868. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3869. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3870. (beg end))
  3871. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3872. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3873. "org-agenda" (&optional end))
  3874. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3875. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3876. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3877. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3878. (declare-function org-indent-mode "org-indent" (&optional arg))
  3879. (declare-function parse-time-string "parse-time" (string))
  3880. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3881. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3882. (defvar remember-data-file)
  3883. (defvar texmathp-why)
  3884. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3885. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3886. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3887. ;;;###autoload
  3888. (defun turn-on-orgtbl ()
  3889. "Unconditionally turn on `orgtbl-mode'."
  3890. (require 'org-table)
  3891. (orgtbl-mode 1))
  3892. (defun org-at-table-p (&optional table-type)
  3893. "Non-nil if the cursor is inside an Org table.
  3894. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3895. If `org-enable-table-editor' is nil, return nil unconditionally."
  3896. (and org-enable-table-editor
  3897. (save-excursion
  3898. (beginning-of-line)
  3899. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3900. (let ((element (org-element-lineage (org-element-at-point) '(table) t)))
  3901. (and element
  3902. (or table-type (eq (org-element-property :type element) '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-comment-string) '(0 'org-special-keyword t))
  5623. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5624. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5625. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5626. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5627. ;; Emphasis
  5628. (if em
  5629. (if (featurep 'xemacs)
  5630. '(org-do-emphasis-faces (0 nil append))
  5631. '(org-do-emphasis-faces)))
  5632. ;; Checkboxes
  5633. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5634. 1 'org-checkbox prepend)
  5635. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5636. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5637. (0 (org-get-checkbox-statistics-face) t)))
  5638. ;; Description list items
  5639. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5640. 1 'org-list-dt prepend)
  5641. ;; ARCHIVEd headings
  5642. (list (concat
  5643. org-outline-regexp-bol
  5644. "\\(.*:" org-archive-tag ":.*\\)")
  5645. '(1 'org-archived prepend))
  5646. ;; Specials
  5647. '(org-do-latex-and-related)
  5648. '(org-fontify-entities)
  5649. '(org-raise-scripts)
  5650. ;; Code
  5651. '(org-activate-code (1 'org-code t))
  5652. ;; COMMENT
  5653. (list (format
  5654. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5655. org-todo-regexp
  5656. org-comment-string)
  5657. '(9 'org-special-keyword t))
  5658. ;; Blocks and meta lines
  5659. '(org-fontify-meta-lines-and-blocks))))
  5660. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5661. (run-hooks 'org-font-lock-set-keywords-hook)
  5662. ;; Now set the full font-lock-keywords
  5663. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5664. (org-set-local 'font-lock-defaults
  5665. '(org-font-lock-keywords t nil nil backward-paragraph))
  5666. (kill-local-variable 'font-lock-keywords) nil))
  5667. (defun org-toggle-pretty-entities ()
  5668. "Toggle the composition display of entities as UTF8 characters."
  5669. (interactive)
  5670. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5671. (org-restart-font-lock)
  5672. (if org-pretty-entities
  5673. (message "Entities are now displayed as UTF8 characters")
  5674. (save-restriction
  5675. (widen)
  5676. (org-decompose-region (point-min) (point-max))
  5677. (message "Entities are now displayed as plain text"))))
  5678. (defvar org-custom-properties-overlays nil
  5679. "List of overlays used for custom properties.")
  5680. (make-variable-buffer-local 'org-custom-properties-overlays)
  5681. (defun org-toggle-custom-properties-visibility ()
  5682. "Display or hide properties in `org-custom-properties'."
  5683. (interactive)
  5684. (if org-custom-properties-overlays
  5685. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5686. (setq org-custom-properties-overlays nil))
  5687. (when org-custom-properties
  5688. (org-with-wide-buffer
  5689. (goto-char (point-min))
  5690. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5691. (while (re-search-forward regexp nil t)
  5692. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5693. (when (and end (< (point) end))
  5694. ;; Hide first custom property in current drawer.
  5695. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5696. (overlay-put o 'invisible t)
  5697. (overlay-put o 'org-custom-property t)
  5698. (push o org-custom-properties-overlays))
  5699. ;; Hide additional custom properties in the same drawer.
  5700. (while (re-search-forward regexp end t)
  5701. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5702. (overlay-put o 'invisible t)
  5703. (overlay-put o 'org-custom-property t)
  5704. (push o org-custom-properties-overlays)))))
  5705. ;; Each entry is limited to a single property drawer.
  5706. (outline-next-heading)))))))
  5707. (defun org-fontify-entities (limit)
  5708. "Find an entity to fontify."
  5709. (let (ee)
  5710. (when org-pretty-entities
  5711. (catch 'match
  5712. ;; "\_ "-family is left out on purpose. Only the first one,
  5713. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5714. ;; confusing when adding a second white space character.
  5715. (while (re-search-forward
  5716. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5717. limit t)
  5718. (if (and (not (org-at-comment-p))
  5719. (setq ee (org-entity-get (match-string 1)))
  5720. (= (length (nth 6 ee)) 1))
  5721. (let*
  5722. ((end (if (equal (match-string 2) "{}")
  5723. (match-end 2)
  5724. (match-end 1))))
  5725. (add-text-properties
  5726. (match-beginning 0) end
  5727. (list 'font-lock-fontified t))
  5728. (compose-region (match-beginning 0) end
  5729. (nth 6 ee) nil)
  5730. (backward-char 1)
  5731. (throw 'match t))))
  5732. nil))))
  5733. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5734. "Fontify string S like in Org-mode."
  5735. (with-temp-buffer
  5736. (insert s)
  5737. (let ((org-odd-levels-only odd-levels))
  5738. (org-mode)
  5739. (font-lock-ensure)
  5740. (buffer-string))))
  5741. (defvar org-m nil)
  5742. (defvar org-l nil)
  5743. (defvar org-f nil)
  5744. (defun org-get-level-face (n)
  5745. "Get the right face for match N in font-lock matching of headlines."
  5746. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5747. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5748. (if org-cycle-level-faces
  5749. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5750. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5751. (cond
  5752. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5753. ((eq n 2) org-f)
  5754. (t (if org-level-color-stars-only nil org-f))))
  5755. (defun org-face-from-face-or-color (context inherit face-or-color)
  5756. "Create a face list that inherits INHERIT, but sets the foreground color.
  5757. When FACE-OR-COLOR is not a string, just return it."
  5758. (if (stringp face-or-color)
  5759. (list :inherit inherit
  5760. (cdr (assoc context org-faces-easy-properties))
  5761. face-or-color)
  5762. face-or-color))
  5763. (defun org-get-todo-face (kwd)
  5764. "Get the right face for a TODO keyword KWD.
  5765. If KWD is a number, get the corresponding match group."
  5766. (if (numberp kwd) (setq kwd (match-string kwd)))
  5767. (or (org-face-from-face-or-color
  5768. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5769. (and (member kwd org-done-keywords) 'org-done)
  5770. 'org-todo))
  5771. (defun org-get-priority-face (priority)
  5772. "Get the right face for PRIORITY.
  5773. PRIORITY is a character."
  5774. (or (org-face-from-face-or-color
  5775. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5776. 'org-priority))
  5777. (defun org-get-tag-face (tag)
  5778. "Get the right face for TAG.
  5779. If TAG is a number, get the corresponding match group."
  5780. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5781. (or (org-face-from-face-or-color
  5782. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5783. 'org-tag)))
  5784. (defun org-font-lock-add-priority-faces (limit)
  5785. "Add the special priority faces."
  5786. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5787. (add-text-properties
  5788. (match-beginning 1) (match-end 1)
  5789. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5790. 'font-lock-fontified t))))
  5791. (defun org-font-lock-add-tag-faces (limit)
  5792. "Add the special tag faces."
  5793. (when (and org-tag-faces org-tags-special-faces-re)
  5794. (while (re-search-forward org-tags-special-faces-re limit t)
  5795. (add-text-properties (match-beginning 1) (match-end 1)
  5796. (list 'face (org-get-tag-face 1)
  5797. 'font-lock-fontified t))
  5798. (backward-char 1))))
  5799. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5800. "Remove fontification and activation overlays from links."
  5801. (font-lock-default-unfontify-region beg end)
  5802. (let* ((buffer-undo-list t)
  5803. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5804. (inhibit-modification-hooks t)
  5805. deactivate-mark buffer-file-name buffer-file-truename)
  5806. (org-decompose-region beg end)
  5807. (remove-text-properties beg end
  5808. '(mouse-face t keymap t org-linked-text t
  5809. invisible t intangible t
  5810. org-emphasis t))
  5811. (org-remove-font-lock-display-properties beg end)))
  5812. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5813. ((raise 0.3) (height 0.7))
  5814. ((raise -0.5))
  5815. ((raise 0.5)))
  5816. "Display properties for showing superscripts and subscripts.")
  5817. (defun org-remove-font-lock-display-properties (beg end)
  5818. "Remove specific display properties that have been added by font lock.
  5819. The will remove the raise properties that are used to show superscripts
  5820. and subscripts."
  5821. (let (next prop)
  5822. (while (< beg end)
  5823. (setq next (next-single-property-change beg 'display nil end)
  5824. prop (get-text-property beg 'display))
  5825. (if (member prop org-script-display)
  5826. (put-text-property beg next 'display nil))
  5827. (setq beg next))))
  5828. (defun org-raise-scripts (limit)
  5829. "Add raise properties to sub/superscripts."
  5830. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5831. (if (re-search-forward
  5832. (if (eq org-use-sub-superscripts t)
  5833. org-match-substring-regexp
  5834. org-match-substring-with-braces-regexp)
  5835. limit t)
  5836. (let* ((pos (point)) table-p comment-p
  5837. (mpos (match-beginning 3))
  5838. (emph-p (get-text-property mpos 'org-emphasis))
  5839. (link-p (get-text-property mpos 'mouse-face))
  5840. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5841. (goto-char (point-at-bol))
  5842. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5843. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5844. (goto-char pos)
  5845. ;; Handle a_b^c
  5846. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5847. (if (or comment-p emph-p link-p keyw-p)
  5848. t
  5849. (put-text-property (match-beginning 3) (match-end 0)
  5850. 'display
  5851. (if (equal (char-after (match-beginning 2)) ?^)
  5852. (nth (if table-p 3 1) org-script-display)
  5853. (nth (if table-p 2 0) org-script-display)))
  5854. (add-text-properties (match-beginning 2) (match-end 2)
  5855. (list 'invisible t
  5856. 'org-dwidth t 'org-dwidth-n 1))
  5857. (if (and (eq (char-after (match-beginning 3)) ?{)
  5858. (eq (char-before (match-end 3)) ?}))
  5859. (progn
  5860. (add-text-properties
  5861. (match-beginning 3) (1+ (match-beginning 3))
  5862. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5863. (add-text-properties
  5864. (1- (match-end 3)) (match-end 3)
  5865. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5866. t)))))
  5867. ;;;; Visibility cycling, including org-goto and indirect buffer
  5868. ;;; Cycling
  5869. (defvar org-cycle-global-status nil)
  5870. (make-variable-buffer-local 'org-cycle-global-status)
  5871. (put 'org-cycle-global-status 'org-state t)
  5872. (defvar org-cycle-subtree-status nil)
  5873. (make-variable-buffer-local 'org-cycle-subtree-status)
  5874. (put 'org-cycle-subtree-status 'org-state t)
  5875. (defvar org-inlinetask-min-level)
  5876. (defun org-unlogged-message (&rest args)
  5877. "Display a message, but avoid logging it in the *Messages* buffer."
  5878. (let ((message-log-max nil))
  5879. (apply 'message args)))
  5880. ;;;###autoload
  5881. (defun org-cycle (&optional arg)
  5882. "TAB-action and visibility cycling for Org-mode.
  5883. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5884. is outline visibility cycling, but it also invokes other actions
  5885. in special contexts.
  5886. - When this function is called with a prefix argument, rotate the entire
  5887. buffer through 3 states (global cycling)
  5888. 1. OVERVIEW: Show only top-level headlines.
  5889. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5890. 3. SHOW ALL: Show everything.
  5891. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5892. determined by the variable `org-startup-folded', and by any VISIBILITY
  5893. properties in the buffer.
  5894. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5895. including any drawers.
  5896. - When inside a table, re-align the table and move to the next field.
  5897. - When point is at the beginning of a headline, rotate the subtree started
  5898. by this line through 3 different states (local cycling)
  5899. 1. FOLDED: Only the main headline is shown.
  5900. 2. CHILDREN: The main headline and the direct children are shown.
  5901. From this state, you can move to one of the children
  5902. and zoom in further.
  5903. 3. SUBTREE: Show the entire subtree, including body text.
  5904. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5905. - When point is at the beginning of an empty headline and the variable
  5906. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5907. of the headline by demoting and promoting it to likely levels. This
  5908. speeds up creation document structure by pressing TAB once or several
  5909. times right after creating a new headline.
  5910. - When there is a numeric prefix, go up to a heading with level ARG, do
  5911. a `show-subtree' and return to the previous cursor position. If ARG
  5912. is negative, go up that many levels.
  5913. - When point is not at the beginning of a headline, execute the global
  5914. binding for TAB, which is re-indenting the line. See the option
  5915. `org-cycle-emulate-tab' for details.
  5916. - Special case: if point is at the beginning of the buffer and there is
  5917. no headline in line 1, this function will act as if called with prefix arg
  5918. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5919. But only if also the variable `org-cycle-global-at-bob' is t."
  5920. (interactive "P")
  5921. (org-load-modules-maybe)
  5922. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5923. (and org-cycle-level-after-item/entry-creation
  5924. (or (org-cycle-level)
  5925. (org-cycle-item-indentation))))
  5926. (let* ((limit-level
  5927. (or org-cycle-max-level
  5928. (and (boundp 'org-inlinetask-min-level)
  5929. org-inlinetask-min-level
  5930. (1- org-inlinetask-min-level))))
  5931. (nstars (and limit-level
  5932. (if org-odd-levels-only
  5933. (and limit-level (1- (* limit-level 2)))
  5934. limit-level)))
  5935. (org-outline-regexp
  5936. (if (not (derived-mode-p 'org-mode))
  5937. outline-regexp
  5938. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5939. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5940. (not (looking-at org-outline-regexp))))
  5941. (org-cycle-hook
  5942. (if bob-special
  5943. (delq 'org-optimize-window-after-visibility-change
  5944. (copy-sequence org-cycle-hook))
  5945. org-cycle-hook))
  5946. (pos (point)))
  5947. (if (or bob-special (equal arg '(4)))
  5948. ;; special case: use global cycling
  5949. (setq arg t))
  5950. (cond
  5951. ((equal arg '(16))
  5952. (setq last-command 'dummy)
  5953. (org-set-startup-visibility)
  5954. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5955. ((equal arg '(64))
  5956. (show-all)
  5957. (org-unlogged-message "Entire buffer visible, including drawers"))
  5958. ;; Try cdlatex TAB completion
  5959. ((org-try-cdlatex-tab))
  5960. ;; Table: enter it or move to the next field.
  5961. ((org-at-table-p 'any)
  5962. (if (org-at-table.el-p)
  5963. (message "Use C-c ' to edit table.el tables")
  5964. (if arg (org-table-edit-field t)
  5965. (org-table-justify-field-maybe)
  5966. (call-interactively 'org-table-next-field))))
  5967. ((run-hook-with-args-until-success
  5968. 'org-tab-after-check-for-table-hook))
  5969. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5970. ((eq arg t) (org-cycle-internal-global))
  5971. ;; Drawers: delegate to `org-flag-drawer'.
  5972. ((save-excursion
  5973. (beginning-of-line 1)
  5974. (looking-at org-drawer-regexp))
  5975. (org-flag-drawer ; toggle block visibility
  5976. (not (get-char-property (match-end 0) 'invisible))))
  5977. ;; Show-subtree, ARG levels up from here.
  5978. ((integerp arg)
  5979. (save-excursion
  5980. (org-back-to-heading)
  5981. (outline-up-heading (if (< arg 0) (- arg)
  5982. (- (funcall outline-level) arg)))
  5983. (org-show-subtree)))
  5984. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5985. ((and (featurep 'org-inlinetask)
  5986. (org-inlinetask-at-task-p)
  5987. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5988. (org-inlinetask-toggle-visibility))
  5989. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5990. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5991. (save-excursion (move-beginning-of-line 1)
  5992. (looking-at org-outline-regexp)))
  5993. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5994. (org-cycle-internal-local))
  5995. ;; From there: TAB emulation and template completion.
  5996. (buffer-read-only (org-back-to-heading))
  5997. ((run-hook-with-args-until-success
  5998. 'org-tab-after-check-for-cycling-hook))
  5999. ((org-try-structure-completion))
  6000. ((run-hook-with-args-until-success
  6001. 'org-tab-before-tab-emulation-hook))
  6002. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6003. (or (not (bolp))
  6004. (not (looking-at org-outline-regexp))))
  6005. (call-interactively (global-key-binding "\t")))
  6006. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6007. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6008. (or (and (eq org-cycle-emulate-tab 'white)
  6009. (= (match-end 0) (point-at-eol)))
  6010. (and (eq org-cycle-emulate-tab 'whitestart)
  6011. (>= (match-end 0) pos))))
  6012. t
  6013. (eq org-cycle-emulate-tab t))
  6014. (call-interactively (global-key-binding "\t")))
  6015. (t (save-excursion
  6016. (org-back-to-heading)
  6017. (org-cycle)))))))
  6018. (defun org-cycle-internal-global ()
  6019. "Do the global cycling action."
  6020. ;; Hack to avoid display of messages for .org attachments in Gnus
  6021. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6022. (cond
  6023. ((and (eq last-command this-command)
  6024. (eq org-cycle-global-status 'overview))
  6025. ;; We just created the overview - now do table of contents
  6026. ;; This can be slow in very large buffers, so indicate action
  6027. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6028. (unless ga (org-unlogged-message "CONTENTS..."))
  6029. (org-content)
  6030. (unless ga (org-unlogged-message "CONTENTS...done"))
  6031. (setq org-cycle-global-status 'contents)
  6032. (run-hook-with-args 'org-cycle-hook 'contents))
  6033. ((and (eq last-command this-command)
  6034. (eq org-cycle-global-status 'contents))
  6035. ;; We just showed the table of contents - now show everything
  6036. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6037. (show-all)
  6038. (unless ga (org-unlogged-message "SHOW ALL"))
  6039. (setq org-cycle-global-status 'all)
  6040. (run-hook-with-args 'org-cycle-hook 'all))
  6041. (t
  6042. ;; Default action: go to overview
  6043. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6044. (org-overview)
  6045. (unless ga (org-unlogged-message "OVERVIEW"))
  6046. (setq org-cycle-global-status 'overview)
  6047. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6048. (defvar org-called-with-limited-levels nil
  6049. "Non-nil when `org-with-limited-levels' is currently active.")
  6050. (defun org-cycle-internal-local ()
  6051. "Do the local cycling action."
  6052. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6053. ;; First, determine end of headline (EOH), end of subtree or item
  6054. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6055. (save-excursion
  6056. (if (org-at-item-p)
  6057. (progn
  6058. (beginning-of-line)
  6059. (setq struct (org-list-struct))
  6060. (setq eoh (point-at-eol))
  6061. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6062. (setq has-children (org-list-has-child-p (point) struct)))
  6063. (org-back-to-heading)
  6064. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6065. (setq eos (save-excursion (org-end-of-subtree t t)
  6066. (when (bolp) (backward-char)) (point)))
  6067. (setq has-children
  6068. (or (save-excursion
  6069. (let ((level (funcall outline-level)))
  6070. (outline-next-heading)
  6071. (and (org-at-heading-p t)
  6072. (> (funcall outline-level) level))))
  6073. (save-excursion
  6074. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6075. ;; Determine end invisible part of buffer (EOL)
  6076. (beginning-of-line 2)
  6077. ;; XEmacs doesn't have `next-single-char-property-change'
  6078. (if (featurep 'xemacs)
  6079. (while (and (not (eobp)) ;; this is like `next-line'
  6080. (get-char-property (1- (point)) 'invisible))
  6081. (beginning-of-line 2))
  6082. (while (and (not (eobp)) ;; this is like `next-line'
  6083. (get-char-property (1- (point)) 'invisible))
  6084. (goto-char (next-single-char-property-change (point) 'invisible))
  6085. (and (eolp) (beginning-of-line 2))))
  6086. (setq eol (point)))
  6087. ;; Find out what to do next and set `this-command'
  6088. (cond
  6089. ((= eos eoh)
  6090. ;; Nothing is hidden behind this heading
  6091. (unless (org-before-first-heading-p)
  6092. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6093. (org-unlogged-message "EMPTY ENTRY")
  6094. (setq org-cycle-subtree-status nil)
  6095. (save-excursion
  6096. (goto-char eos)
  6097. (outline-next-heading)
  6098. (if (outline-invisible-p) (org-flag-heading nil))))
  6099. ((and (or (>= eol eos)
  6100. (not (string-match "\\S-" (buffer-substring eol eos))))
  6101. (or has-children
  6102. (not (setq children-skipped
  6103. org-cycle-skip-children-state-if-no-children))))
  6104. ;; Entire subtree is hidden in one line: children view
  6105. (unless (org-before-first-heading-p)
  6106. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6107. (if (org-at-item-p)
  6108. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6109. (org-show-entry)
  6110. (org-with-limited-levels (show-children))
  6111. ;; FIXME: This slows down the func way too much.
  6112. ;; How keep drawers hidden in subtree anyway?
  6113. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6114. ;; (org-cycle-hide-drawers 'subtree))
  6115. ;; Fold every list in subtree to top-level items.
  6116. (when (eq org-cycle-include-plain-lists 'integrate)
  6117. (save-excursion
  6118. (org-back-to-heading)
  6119. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6120. (beginning-of-line 1)
  6121. (let* ((struct (org-list-struct))
  6122. (prevs (org-list-prevs-alist struct))
  6123. (end (org-list-get-bottom-point struct)))
  6124. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6125. (org-list-get-all-items (point) struct prevs))
  6126. (goto-char (if (< end eos) end eos)))))))
  6127. (org-unlogged-message "CHILDREN")
  6128. (save-excursion
  6129. (goto-char eos)
  6130. (outline-next-heading)
  6131. (if (outline-invisible-p) (org-flag-heading nil)))
  6132. (setq org-cycle-subtree-status 'children)
  6133. (unless (org-before-first-heading-p)
  6134. (run-hook-with-args 'org-cycle-hook 'children)))
  6135. ((or children-skipped
  6136. (and (eq last-command this-command)
  6137. (eq org-cycle-subtree-status 'children)))
  6138. ;; We just showed the children, or no children are there,
  6139. ;; now show everything.
  6140. (unless (org-before-first-heading-p)
  6141. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6142. (outline-flag-region eoh eos nil)
  6143. (org-unlogged-message
  6144. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6145. (setq org-cycle-subtree-status 'subtree)
  6146. (unless (org-before-first-heading-p)
  6147. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6148. (t
  6149. ;; Default action: hide the subtree.
  6150. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6151. (outline-flag-region eoh eos t)
  6152. (org-unlogged-message "FOLDED")
  6153. (setq org-cycle-subtree-status 'folded)
  6154. (unless (org-before-first-heading-p)
  6155. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6156. ;;;###autoload
  6157. (defun org-global-cycle (&optional arg)
  6158. "Cycle the global visibility. For details see `org-cycle'.
  6159. With \\[universal-argument] prefix arg, switch to startup visibility.
  6160. With a numeric prefix, show all headlines up to that level."
  6161. (interactive "P")
  6162. (let ((org-cycle-include-plain-lists
  6163. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6164. (cond
  6165. ((integerp arg)
  6166. (show-all)
  6167. (hide-sublevels arg)
  6168. (setq org-cycle-global-status 'contents))
  6169. ((equal arg '(4))
  6170. (org-set-startup-visibility)
  6171. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6172. (t
  6173. (org-cycle '(4))))))
  6174. (defun org-set-startup-visibility ()
  6175. "Set the visibility required by startup options and properties."
  6176. (cond
  6177. ((eq org-startup-folded t)
  6178. (org-overview))
  6179. ((eq org-startup-folded 'content)
  6180. (org-content))
  6181. ((or (eq org-startup-folded 'showeverything)
  6182. (eq org-startup-folded nil))
  6183. (show-all)))
  6184. (unless (eq org-startup-folded 'showeverything)
  6185. (if org-hide-block-startup (org-hide-block-all))
  6186. (org-set-visibility-according-to-property 'no-cleanup)
  6187. (org-cycle-hide-archived-subtrees 'all)
  6188. (org-cycle-hide-drawers 'all)
  6189. (org-cycle-show-empty-lines t)))
  6190. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6191. "Switch subtree visibilities according to :VISIBILITY: property."
  6192. (interactive)
  6193. (let (org-show-entry-below)
  6194. (org-with-wide-buffer
  6195. (goto-char (point-min))
  6196. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6197. (if (not (org-at-property-p)) (outline-next-heading)
  6198. (let ((state (match-string 3)))
  6199. (save-excursion
  6200. (org-back-to-heading t)
  6201. (hide-subtree)
  6202. (org-reveal)
  6203. (cond
  6204. ((equal state "folded")
  6205. (hide-subtree))
  6206. ((equal state "children")
  6207. (org-show-hidden-entry)
  6208. (show-children))
  6209. ((equal state "content")
  6210. (save-excursion
  6211. (save-restriction
  6212. (org-narrow-to-subtree)
  6213. (org-content))))
  6214. ((member state '("all" "showall"))
  6215. (show-subtree)))))))
  6216. (unless no-cleanup
  6217. (org-cycle-hide-archived-subtrees 'all)
  6218. (org-cycle-hide-drawers 'all)
  6219. (org-cycle-show-empty-lines 'all)))))
  6220. ;; This function uses outline-regexp instead of the more fundamental
  6221. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6222. ;; buffers, where outline-regexp is needed.
  6223. (defun org-overview ()
  6224. "Switch to overview mode, showing only top-level headlines.
  6225. This shows all headlines with a level equal or greater than the level
  6226. of the first headline in the buffer. This is important, because if the
  6227. first headline is not level one, then (hide-sublevels 1) gives confusing
  6228. results."
  6229. (interactive)
  6230. (save-excursion
  6231. (let ((level
  6232. (save-excursion
  6233. (goto-char (point-min))
  6234. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6235. (progn
  6236. (goto-char (match-beginning 0))
  6237. (funcall outline-level))))))
  6238. (and level (hide-sublevels level)))))
  6239. (defun org-content (&optional arg)
  6240. "Show all headlines in the buffer, like a table of contents.
  6241. With numerical argument N, show content up to level N."
  6242. (interactive "P")
  6243. (org-overview)
  6244. (save-excursion
  6245. ;; Visit all headings and show their offspring
  6246. (and (integerp arg) (org-overview))
  6247. (goto-char (point-max))
  6248. (catch 'exit
  6249. (while (and (progn (condition-case nil
  6250. (outline-previous-visible-heading 1)
  6251. (error (goto-char (point-min))))
  6252. t)
  6253. (looking-at org-outline-regexp))
  6254. (if (integerp arg)
  6255. (show-children (1- arg))
  6256. (show-branches))
  6257. (if (bobp) (throw 'exit nil))))))
  6258. (defun org-optimize-window-after-visibility-change (state)
  6259. "Adjust the window after a change in outline visibility.
  6260. This function is the default value of the hook `org-cycle-hook'."
  6261. (when (get-buffer-window (current-buffer))
  6262. (cond
  6263. ((eq state 'content) nil)
  6264. ((eq state 'all) nil)
  6265. ((eq state 'folded) nil)
  6266. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6267. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6268. (defun org-remove-empty-overlays-at (pos)
  6269. "Remove outline overlays that do not contain non-white stuff."
  6270. (mapc
  6271. (lambda (o)
  6272. (and (eq 'outline (overlay-get o 'invisible))
  6273. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6274. (overlay-end o))))
  6275. (delete-overlay o)))
  6276. (overlays-at pos)))
  6277. (defun org-clean-visibility-after-subtree-move ()
  6278. "Fix visibility issues after moving a subtree."
  6279. ;; First, find a reasonable region to look at:
  6280. ;; Start two siblings above, end three below
  6281. (let* ((beg (save-excursion
  6282. (and (org-get-last-sibling)
  6283. (org-get-last-sibling))
  6284. (point)))
  6285. (end (save-excursion
  6286. (and (org-get-next-sibling)
  6287. (org-get-next-sibling)
  6288. (org-get-next-sibling))
  6289. (if (org-at-heading-p)
  6290. (point-at-eol)
  6291. (point))))
  6292. (level (looking-at "\\*+"))
  6293. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6294. (save-excursion
  6295. (save-restriction
  6296. (narrow-to-region beg end)
  6297. (when re
  6298. ;; Properly fold already folded siblings
  6299. (goto-char (point-min))
  6300. (while (re-search-forward re nil t)
  6301. (if (and (not (outline-invisible-p))
  6302. (save-excursion
  6303. (goto-char (point-at-eol)) (outline-invisible-p)))
  6304. (hide-entry))))
  6305. (org-cycle-show-empty-lines 'overview)
  6306. (org-cycle-hide-drawers 'overview)))))
  6307. (defun org-cycle-show-empty-lines (state)
  6308. "Show empty lines above all visible headlines.
  6309. The region to be covered depends on STATE when called through
  6310. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6311. entire buffer covered. Note that an empty line is only shown if there
  6312. are at least `org-cycle-separator-lines' empty lines before the headline."
  6313. (when (/= org-cycle-separator-lines 0)
  6314. (save-excursion
  6315. (let* ((n (abs org-cycle-separator-lines))
  6316. (re (cond
  6317. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6318. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6319. (t (let ((ns (number-to-string (- n 2))))
  6320. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6321. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6322. beg end)
  6323. (cond
  6324. ((memq state '(overview contents t))
  6325. (setq beg (point-min) end (point-max)))
  6326. ((memq state '(children folded))
  6327. (setq beg (point)
  6328. end (progn (org-end-of-subtree t t)
  6329. (line-beginning-position 2)))))
  6330. (when beg
  6331. (goto-char beg)
  6332. (while (re-search-forward re end t)
  6333. (unless (get-char-property (match-end 1) 'invisible)
  6334. (let ((e (match-end 1))
  6335. (b (if (>= org-cycle-separator-lines 0)
  6336. (match-beginning 1)
  6337. (save-excursion
  6338. (goto-char (match-beginning 0))
  6339. (skip-chars-backward " \t\n")
  6340. (line-end-position)))))
  6341. (outline-flag-region b e nil))))))))
  6342. ;; Never hide empty lines at the end of the file.
  6343. (save-excursion
  6344. (goto-char (point-max))
  6345. (outline-previous-heading)
  6346. (outline-end-of-heading)
  6347. (if (and (looking-at "[ \t\n]+")
  6348. (= (match-end 0) (point-max)))
  6349. (outline-flag-region (point) (match-end 0) nil))))
  6350. (defun org-show-empty-lines-in-parent ()
  6351. "Move to the parent and re-show empty lines before visible headlines."
  6352. (save-excursion
  6353. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6354. (org-cycle-show-empty-lines context))))
  6355. (defun org-files-list ()
  6356. "Return `org-agenda-files' list, plus all open org-mode files.
  6357. This is useful for operations that need to scan all of a user's
  6358. open and agenda-wise Org files."
  6359. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6360. (dolist (buf (buffer-list))
  6361. (with-current-buffer buf
  6362. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6363. (let ((file (expand-file-name (buffer-file-name))))
  6364. (unless (member file files)
  6365. (push file files))))))
  6366. files))
  6367. (defsubst org-entry-beginning-position ()
  6368. "Return the beginning position of the current entry."
  6369. (save-excursion (outline-back-to-heading t) (point)))
  6370. (defsubst org-entry-end-position ()
  6371. "Return the end position of the current entry."
  6372. (save-excursion (outline-next-heading) (point)))
  6373. (defun org-cycle-hide-drawers (state &optional exceptions)
  6374. "Re-hide all drawers after a visibility state change.
  6375. When non-nil, optional argument EXCEPTIONS is a list of strings
  6376. specifying which drawers should not be hidden."
  6377. (when (and (derived-mode-p 'org-mode)
  6378. (not (memq state '(overview folded contents))))
  6379. (save-excursion
  6380. (let* ((globalp (memq state '(contents all)))
  6381. (beg (if globalp (point-min) (point)))
  6382. (end (if globalp (point-max)
  6383. (if (eq state 'children)
  6384. (save-excursion (outline-next-heading) (point))
  6385. (org-end-of-subtree t)))))
  6386. (goto-char beg)
  6387. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6388. (unless (member-ignore-case (match-string 1) exceptions)
  6389. (let ((drawer (org-element-at-point)))
  6390. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6391. (org-flag-drawer t drawer)
  6392. ;; Make sure to skip drawer entirely or we might flag
  6393. ;; it another time when matching its ending line with
  6394. ;; `org-drawer-regexp'.
  6395. (goto-char (org-element-property :end drawer))))))))))
  6396. (defun org-flag-drawer (flag &optional element)
  6397. "When FLAG is non-nil, hide the drawer we are at.
  6398. Otherwise make it visible. When optional argument ELEMENT is
  6399. a parsed drawer, as returned by `org-element-at-point', hide or
  6400. show that drawer instead."
  6401. (when (save-excursion
  6402. (beginning-of-line)
  6403. (org-looking-at-p org-drawer-regexp))
  6404. (let ((drawer (or element (org-element-at-point))))
  6405. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6406. (let ((post (org-element-property :post-affiliated drawer)))
  6407. (save-excursion
  6408. (outline-flag-region
  6409. (progn (goto-char post) (line-end-position))
  6410. (progn (goto-char (org-element-property :end drawer))
  6411. (skip-chars-backward " \r\t\n")
  6412. (line-end-position))
  6413. flag))
  6414. ;; When the drawer is hidden away, make sure point lies in
  6415. ;; a visible part of the buffer.
  6416. (when (and flag (> (line-beginning-position) post))
  6417. (goto-char post)))))))
  6418. (defun org-subtree-end-visible-p ()
  6419. "Is the end of the current subtree visible?"
  6420. (pos-visible-in-window-p
  6421. (save-excursion (org-end-of-subtree t) (point))))
  6422. (defun org-first-headline-recenter ()
  6423. "Move cursor to the first headline and recenter the headline."
  6424. (goto-char (point-min))
  6425. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6426. (set-window-start (selected-window) (point-at-bol))))
  6427. ;;; Saving and restoring visibility
  6428. (defun org-outline-overlay-data (&optional use-markers)
  6429. "Return a list of the locations of all outline overlays.
  6430. These are overlays with the `invisible' property value `outline'.
  6431. The return value is a list of cons cells, with start and stop
  6432. positions for each overlay.
  6433. If USE-MARKERS is set, return the positions as markers."
  6434. (let (beg end)
  6435. (save-excursion
  6436. (save-restriction
  6437. (widen)
  6438. (delq nil
  6439. (mapcar (lambda (o)
  6440. (when (eq (overlay-get o 'invisible) 'outline)
  6441. (setq beg (overlay-start o)
  6442. end (overlay-end o))
  6443. (and beg end (> end beg)
  6444. (if use-markers
  6445. (cons (copy-marker beg)
  6446. (copy-marker end t))
  6447. (cons beg end)))))
  6448. (overlays-in (point-min) (point-max))))))))
  6449. (defun org-set-outline-overlay-data (data)
  6450. "Create visibility overlays for all positions in DATA.
  6451. DATA should have been made by `org-outline-overlay-data'."
  6452. (let (o)
  6453. (save-excursion
  6454. (save-restriction
  6455. (widen)
  6456. (show-all)
  6457. (mapc (lambda (c)
  6458. (outline-flag-region (car c) (cdr c) t))
  6459. data)))))
  6460. ;;; Folding of blocks
  6461. (defvar org-hide-block-overlays nil
  6462. "Overlays hiding blocks.")
  6463. (make-variable-buffer-local 'org-hide-block-overlays)
  6464. (defun org-block-map (function &optional start end)
  6465. "Call FUNCTION at the head of all source blocks in the current buffer.
  6466. Optional arguments START and END can be used to limit the range."
  6467. (let ((start (or start (point-min)))
  6468. (end (or end (point-max))))
  6469. (save-excursion
  6470. (goto-char start)
  6471. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6472. (save-excursion
  6473. (save-match-data
  6474. (goto-char (match-beginning 0))
  6475. (funcall function)))))))
  6476. (defun org-hide-block-toggle-all ()
  6477. "Toggle the visibility of all blocks in the current buffer."
  6478. (org-block-map 'org-hide-block-toggle))
  6479. (defun org-hide-block-all ()
  6480. "Fold all blocks in the current buffer."
  6481. (interactive)
  6482. (org-show-block-all)
  6483. (org-block-map 'org-hide-block-toggle-maybe))
  6484. (defun org-show-block-all ()
  6485. "Unfold all blocks in the current buffer."
  6486. (interactive)
  6487. (mapc 'delete-overlay org-hide-block-overlays)
  6488. (setq org-hide-block-overlays nil))
  6489. (defun org-hide-block-toggle-maybe ()
  6490. "Toggle visibility of block at point.
  6491. Unlike to `org-hide-block-toggle', this function does not throw
  6492. an error. Return a non-nil value when toggling is successful."
  6493. (interactive)
  6494. (ignore-errors (org-hide-block-toggle)))
  6495. (defun org-hide-block-toggle (&optional force)
  6496. "Toggle the visibility of the current block.
  6497. When optional argument FORCE is `off', make block visible. If it
  6498. is non-nil, hide it unconditionally. Throw an error when not at
  6499. a block. Return a non-nil value when toggling is successful."
  6500. (interactive)
  6501. (let ((element (org-element-at-point)))
  6502. (unless (memq (org-element-type element)
  6503. '(center-block comment-block dynamic-block example-block
  6504. export-block quote-block special-block
  6505. src-block verse-block))
  6506. (user-error "Not at a block"))
  6507. (let* ((start (save-excursion
  6508. (goto-char (org-element-property :post-affiliated element))
  6509. (line-end-position)))
  6510. (end (save-excursion
  6511. (goto-char (org-element-property :end element))
  6512. (skip-chars-backward " \r\t\n")
  6513. (line-end-position)))
  6514. (overlays (overlays-at start)))
  6515. (cond
  6516. ;; Do nothing when not before or at the block opening line or
  6517. ;; at the block closing line.
  6518. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6519. ((and (not (eq force 'off))
  6520. (not (memq t (mapcar
  6521. (lambda (o)
  6522. (eq (overlay-get o 'invisible) 'org-hide-block))
  6523. overlays))))
  6524. (let ((ov (make-overlay start end)))
  6525. (overlay-put ov 'invisible 'org-hide-block)
  6526. ;; Make the block accessible to `isearch'.
  6527. (overlay-put
  6528. ov 'isearch-open-invisible
  6529. (lambda (ov)
  6530. (when (memq ov org-hide-block-overlays)
  6531. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6532. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6533. (delete-overlay ov))))
  6534. (push ov org-hide-block-overlays)
  6535. ;; When the block is hidden away, make sure point is left in
  6536. ;; a visible part of the buffer.
  6537. (when (> (line-beginning-position) start)
  6538. (goto-char start)
  6539. (beginning-of-line))
  6540. ;; Signal successful toggling.
  6541. t))
  6542. ((or (not force) (eq force 'off))
  6543. (dolist (ov overlays t)
  6544. (when (memq ov org-hide-block-overlays)
  6545. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6546. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6547. (delete-overlay ov))))))))
  6548. ;; org-tab-after-check-for-cycling-hook
  6549. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6550. ;; Remove overlays when changing major mode
  6551. (add-hook 'org-mode-hook
  6552. (lambda () (org-add-hook 'change-major-mode-hook
  6553. 'org-show-block-all 'append 'local)))
  6554. ;;; Org-goto
  6555. (defvar org-goto-window-configuration nil)
  6556. (defvar org-goto-marker nil)
  6557. (defvar org-goto-map)
  6558. (defun org-goto-map ()
  6559. "Set the keymap `org-goto'."
  6560. (setq org-goto-map
  6561. (let ((map (make-sparse-keymap)))
  6562. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6563. mouse-drag-region universal-argument org-occur)))
  6564. (dolist (cmd cmds)
  6565. (substitute-key-definition cmd cmd map global-map)))
  6566. (suppress-keymap map)
  6567. (org-defkey map "\C-m" 'org-goto-ret)
  6568. (org-defkey map [(return)] 'org-goto-ret)
  6569. (org-defkey map [(left)] 'org-goto-left)
  6570. (org-defkey map [(right)] 'org-goto-right)
  6571. (org-defkey map [(control ?g)] 'org-goto-quit)
  6572. (org-defkey map "\C-i" 'org-cycle)
  6573. (org-defkey map [(tab)] 'org-cycle)
  6574. (org-defkey map [(down)] 'outline-next-visible-heading)
  6575. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6576. (if org-goto-auto-isearch
  6577. (if (fboundp 'define-key-after)
  6578. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6579. nil)
  6580. (org-defkey map "q" 'org-goto-quit)
  6581. (org-defkey map "n" 'outline-next-visible-heading)
  6582. (org-defkey map "p" 'outline-previous-visible-heading)
  6583. (org-defkey map "f" 'outline-forward-same-level)
  6584. (org-defkey map "b" 'outline-backward-same-level)
  6585. (org-defkey map "u" 'outline-up-heading))
  6586. (org-defkey map "/" 'org-occur)
  6587. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6588. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6589. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6590. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6591. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6592. map)))
  6593. (defconst org-goto-help
  6594. "Browse buffer copy, to find location or copy text.%s
  6595. RET=jump to location C-g=quit and return to previous location
  6596. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6597. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6598. (defun org-goto (&optional alternative-interface)
  6599. "Look up a different location in the current file, keeping current visibility.
  6600. When you want look-up or go to a different location in a
  6601. document, the fastest way is often to fold the entire buffer and
  6602. then dive into the tree. This method has the disadvantage, that
  6603. the previous location will be folded, which may not be what you
  6604. want.
  6605. This command works around this by showing a copy of the current
  6606. buffer in an indirect buffer, in overview mode. You can dive
  6607. into the tree in that copy, use org-occur and incremental search
  6608. to find a location. When pressing RET or `Q', the command
  6609. returns to the original buffer in which the visibility is still
  6610. unchanged. After RET it will also jump to the location selected
  6611. in the indirect buffer and expose the headline hierarchy above.
  6612. With a prefix argument, use the alternative interface: e.g. if
  6613. `org-goto-interface' is 'outline use 'outline-path-completion."
  6614. (interactive "P")
  6615. (org-goto-map)
  6616. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6617. (org-refile-use-outline-path t)
  6618. (org-refile-target-verify-function nil)
  6619. (interface
  6620. (if (not alternative-interface)
  6621. org-goto-interface
  6622. (if (eq org-goto-interface 'outline)
  6623. 'outline-path-completion
  6624. 'outline)))
  6625. (org-goto-start-pos (point))
  6626. (selected-point
  6627. (if (eq interface 'outline)
  6628. (car (org-get-location (current-buffer) org-goto-help))
  6629. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6630. (org-refile-check-position pa)
  6631. (nth 3 pa)))))
  6632. (if selected-point
  6633. (progn
  6634. (org-mark-ring-push org-goto-start-pos)
  6635. (goto-char selected-point)
  6636. (if (or (outline-invisible-p) (org-invisible-p2))
  6637. (org-show-context 'org-goto)))
  6638. (message "Quit"))))
  6639. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6640. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6641. (defvar org-goto-local-auto-isearch-map) ; defined below
  6642. (defun org-get-location (buf help)
  6643. "Let the user select a location in the Org-mode buffer BUF.
  6644. This function uses a recursive edit. It returns the selected position
  6645. or nil."
  6646. (org-no-popups
  6647. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6648. (isearch-hide-immediately nil)
  6649. (isearch-search-fun-function
  6650. (lambda () 'org-goto-local-search-headings))
  6651. (org-goto-selected-point org-goto-exit-command))
  6652. (save-excursion
  6653. (save-window-excursion
  6654. (delete-other-windows)
  6655. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6656. (org-pop-to-buffer-same-window
  6657. (condition-case nil
  6658. (make-indirect-buffer (current-buffer) "*org-goto*")
  6659. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6660. (with-output-to-temp-buffer "*Org Help*"
  6661. (princ (format help (if org-goto-auto-isearch
  6662. " Just type for auto-isearch."
  6663. " n/p/f/b/u to navigate, q to quit."))))
  6664. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6665. (setq buffer-read-only nil)
  6666. (let ((org-startup-truncated t)
  6667. (org-startup-folded nil)
  6668. (org-startup-align-all-tables nil))
  6669. (org-mode)
  6670. (org-overview))
  6671. (setq buffer-read-only t)
  6672. (if (and (boundp 'org-goto-start-pos)
  6673. (integer-or-marker-p org-goto-start-pos))
  6674. (progn (goto-char org-goto-start-pos)
  6675. (when (outline-invisible-p)
  6676. (org-show-set-visibility 'lineage)))
  6677. (goto-char (point-min)))
  6678. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6679. (message "Select location and press RET")
  6680. (use-local-map org-goto-map)
  6681. (recursive-edit)))
  6682. (kill-buffer "*org-goto*")
  6683. (cons org-goto-selected-point org-goto-exit-command))))
  6684. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6685. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6686. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6687. (if (fboundp 'isearch-other-control-char)
  6688. (progn
  6689. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6690. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6691. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6692. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6693. (define-key org-goto-local-auto-isearch-map [return] nil))
  6694. (defun org-goto-local-search-headings (string bound noerror)
  6695. "Search and make sure that any matches are in headlines."
  6696. (catch 'return
  6697. (while (if isearch-forward
  6698. (search-forward string bound noerror)
  6699. (search-backward string bound noerror))
  6700. (when (save-match-data
  6701. (and (save-excursion
  6702. (beginning-of-line)
  6703. (looking-at org-complex-heading-regexp))
  6704. (or (not (match-beginning 5))
  6705. (< (point) (match-beginning 5)))))
  6706. (throw 'return (point))))))
  6707. (defun org-goto-local-auto-isearch ()
  6708. "Start isearch."
  6709. (interactive)
  6710. (goto-char (point-min))
  6711. (let ((keys (this-command-keys)))
  6712. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6713. (isearch-mode t)
  6714. (isearch-process-search-char (string-to-char keys)))))
  6715. (defun org-goto-ret (&optional arg)
  6716. "Finish `org-goto' by going to the new location."
  6717. (interactive "P")
  6718. (setq org-goto-selected-point (point)
  6719. org-goto-exit-command 'return)
  6720. (throw 'exit nil))
  6721. (defun org-goto-left ()
  6722. "Finish `org-goto' by going to the new location."
  6723. (interactive)
  6724. (if (org-at-heading-p)
  6725. (progn
  6726. (beginning-of-line 1)
  6727. (setq org-goto-selected-point (point)
  6728. org-goto-exit-command 'left)
  6729. (throw 'exit nil))
  6730. (user-error "Not on a heading")))
  6731. (defun org-goto-right ()
  6732. "Finish `org-goto' by going to the new location."
  6733. (interactive)
  6734. (if (org-at-heading-p)
  6735. (progn
  6736. (setq org-goto-selected-point (point)
  6737. org-goto-exit-command 'right)
  6738. (throw 'exit nil))
  6739. (user-error "Not on a heading")))
  6740. (defun org-goto-quit ()
  6741. "Finish `org-goto' without cursor motion."
  6742. (interactive)
  6743. (setq org-goto-selected-point nil)
  6744. (setq org-goto-exit-command 'quit)
  6745. (throw 'exit nil))
  6746. ;;; Indirect buffer display of subtrees
  6747. (defvar org-indirect-dedicated-frame nil
  6748. "This is the frame being used for indirect tree display.")
  6749. (defvar org-last-indirect-buffer nil)
  6750. (defun org-tree-to-indirect-buffer (&optional arg)
  6751. "Create indirect buffer and narrow it to current subtree.
  6752. With a numerical prefix ARG, go up to this level and then take that tree.
  6753. If ARG is negative, go up that many levels.
  6754. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6755. indirect buffer previously made with this command, to avoid proliferation of
  6756. indirect buffers. However, when you call the command with a \
  6757. \\[universal-argument] prefix, or
  6758. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6759. is kept so that you can work with several indirect buffers at the same time.
  6760. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6761. \\[universal-argument] prefix also
  6762. requests that a new frame be made for the new buffer, so that the dedicated
  6763. frame is not changed."
  6764. (interactive "P")
  6765. (let ((cbuf (current-buffer))
  6766. (cwin (selected-window))
  6767. (pos (point))
  6768. beg end level heading ibuf)
  6769. (save-excursion
  6770. (org-back-to-heading t)
  6771. (when (numberp arg)
  6772. (setq level (org-outline-level))
  6773. (if (< arg 0) (setq arg (+ level arg)))
  6774. (while (> (setq level (org-outline-level)) arg)
  6775. (org-up-heading-safe)))
  6776. (setq beg (point)
  6777. heading (org-get-heading))
  6778. (org-end-of-subtree t t)
  6779. (if (org-at-heading-p) (backward-char 1))
  6780. (setq end (point)))
  6781. (if (and (buffer-live-p org-last-indirect-buffer)
  6782. (not (eq org-indirect-buffer-display 'new-frame))
  6783. (not arg))
  6784. (kill-buffer org-last-indirect-buffer))
  6785. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6786. org-last-indirect-buffer ibuf)
  6787. (cond
  6788. ((or (eq org-indirect-buffer-display 'new-frame)
  6789. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6790. (select-frame (make-frame))
  6791. (delete-other-windows)
  6792. (org-pop-to-buffer-same-window ibuf)
  6793. (org-set-frame-title heading))
  6794. ((eq org-indirect-buffer-display 'dedicated-frame)
  6795. (raise-frame
  6796. (select-frame (or (and org-indirect-dedicated-frame
  6797. (frame-live-p org-indirect-dedicated-frame)
  6798. org-indirect-dedicated-frame)
  6799. (setq org-indirect-dedicated-frame (make-frame)))))
  6800. (delete-other-windows)
  6801. (org-pop-to-buffer-same-window ibuf)
  6802. (org-set-frame-title (concat "Indirect: " heading)))
  6803. ((eq org-indirect-buffer-display 'current-window)
  6804. (org-pop-to-buffer-same-window ibuf))
  6805. ((eq org-indirect-buffer-display 'other-window)
  6806. (pop-to-buffer ibuf))
  6807. (t (error "Invalid value")))
  6808. (if (featurep 'xemacs)
  6809. (save-excursion (org-mode) (turn-on-font-lock)))
  6810. (narrow-to-region beg end)
  6811. (show-all)
  6812. (goto-char pos)
  6813. (run-hook-with-args 'org-cycle-hook 'all)
  6814. (and (window-live-p cwin) (select-window cwin))))
  6815. (defun org-get-indirect-buffer (&optional buffer heading)
  6816. (setq buffer (or buffer (current-buffer)))
  6817. (let ((n 1) (base (buffer-name buffer)) bname)
  6818. (while (buffer-live-p
  6819. (get-buffer
  6820. (setq bname
  6821. (concat base "-"
  6822. (if heading (concat heading "-" (number-to-string n))
  6823. (number-to-string n))))))
  6824. (setq n (1+ n)))
  6825. (condition-case nil
  6826. (make-indirect-buffer buffer bname 'clone)
  6827. (error (make-indirect-buffer buffer bname)))))
  6828. (defun org-set-frame-title (title)
  6829. "Set the title of the current frame to the string TITLE."
  6830. ;; FIXME: how to name a single frame in XEmacs???
  6831. (unless (featurep 'xemacs)
  6832. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6833. ;;;; Structure editing
  6834. ;;; Inserting headlines
  6835. (defun org-previous-line-empty-p (&optional next)
  6836. "Is the previous line a blank line?
  6837. When NEXT is non-nil, check the next line instead."
  6838. (save-excursion
  6839. (and (not (bobp))
  6840. (or (beginning-of-line (if next 2 0)) t)
  6841. (save-match-data
  6842. (looking-at "[ \t]*$")))))
  6843. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6844. "Insert a new heading or an item with the same depth at point.
  6845. If point is at the beginning of a heading or a list item, insert
  6846. a new heading or a new item above the current one. If point is
  6847. at the beginning of a normal line, turn the line into a heading.
  6848. If point is in the middle of a headline or a list item, split the
  6849. headline or the item and create a new headline/item with the text
  6850. in the current line after point \(see `org-M-RET-may-split-line'
  6851. on how to modify this behavior).
  6852. With one universal prefix argument, set the user option
  6853. `org-insert-heading-respect-content' to t for the duration of
  6854. the command. This modifies the behavior described above in this
  6855. ways: on list items and at the beginning of normal lines, force
  6856. the insertion of a heading after the current subtree.
  6857. With two universal prefix arguments, insert the heading at the
  6858. end of the grandparent subtree. For example, if point is within
  6859. a 2nd-level heading, then it will insert a 2nd-level heading at
  6860. the end of the 1st-level parent heading.
  6861. If point is at the beginning of a headline, insert a sibling
  6862. before the current headline. If point is not at the beginning,
  6863. split the line and create a new headline with the text in the
  6864. current line after point \(see `org-M-RET-may-split-line' on how
  6865. to modify this behavior).
  6866. If point is at the beginning of a normal line, turn this line
  6867. into a heading.
  6868. When INVISIBLE-OK is set, stop at invisible headlines when going
  6869. back. This is important for non-interactive uses of the
  6870. command.
  6871. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6872. heading, unconditionally."
  6873. (interactive "P")
  6874. (if (org-called-interactively-p 'any) (org-reveal))
  6875. (let ((itemp (and (not top-level) (org-in-item-p)))
  6876. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6877. (respect-content (or org-insert-heading-respect-content
  6878. (equal arg '(4))))
  6879. (initial-content ""))
  6880. (cond
  6881. ((or (= (buffer-size) 0)
  6882. (and (not (save-excursion
  6883. (and (ignore-errors (org-back-to-heading invisible-ok))
  6884. (org-at-heading-p))))
  6885. (or arg (not itemp))))
  6886. ;; At beginning of buffer or so high up that only a heading
  6887. ;; makes sense.
  6888. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6889. ((not respect-content)
  6890. (unless may-split (end-of-line))
  6891. (insert "\n* "))
  6892. ((re-search-forward org-outline-regexp-bol nil t)
  6893. (beginning-of-line)
  6894. (insert "* \n")
  6895. (backward-char))
  6896. (t (goto-char (point-max))
  6897. (insert "\n* ")))
  6898. (run-hooks 'org-insert-heading-hook))
  6899. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6900. (t
  6901. ;; Maybe move at the end of the subtree
  6902. (when (equal arg '(16))
  6903. (org-up-heading-safe)
  6904. (org-end-of-subtree t))
  6905. ;; Insert a heading
  6906. (save-restriction
  6907. (widen)
  6908. (let* ((level nil)
  6909. (on-heading (org-at-heading-p))
  6910. (empty-line-p (if on-heading
  6911. (org-previous-line-empty-p)
  6912. ;; We will decide later
  6913. nil))
  6914. ;; Get a level string to fall back on.
  6915. (fix-level
  6916. (if (org-before-first-heading-p) "*"
  6917. (save-excursion
  6918. (org-back-to-heading t)
  6919. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6920. (looking-at org-outline-regexp)
  6921. (make-string (1- (length (match-string 0))) ?*))))
  6922. (stars
  6923. (save-excursion
  6924. (condition-case nil
  6925. (if top-level "* "
  6926. (org-back-to-heading invisible-ok)
  6927. (when (and (not on-heading)
  6928. (featurep 'org-inlinetask)
  6929. (integerp org-inlinetask-min-level)
  6930. (>= (length (match-string 0))
  6931. org-inlinetask-min-level))
  6932. ;; Find a heading level before the inline
  6933. ;; task.
  6934. (while (and (setq level (org-up-heading-safe))
  6935. (>= level org-inlinetask-min-level)))
  6936. (if (org-at-heading-p)
  6937. (org-back-to-heading invisible-ok)
  6938. (error "This should not happen")))
  6939. (unless (and (save-excursion
  6940. (save-match-data
  6941. (org-backward-heading-same-level
  6942. 1 invisible-ok))
  6943. (= (point) (match-beginning 0)))
  6944. (not (org-previous-line-empty-p t)))
  6945. (setq empty-line-p (or empty-line-p
  6946. (org-previous-line-empty-p))))
  6947. (match-string 0))
  6948. (error (or fix-level "* ")))))
  6949. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6950. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6951. pos hide-previous previous-pos)
  6952. ;; If we insert after content, move there and clean up
  6953. ;; whitespace.
  6954. (when (and respect-content
  6955. (not (org-looking-at-p org-outline-regexp-bol)))
  6956. (if (not (org-before-first-heading-p))
  6957. (org-end-of-subtree nil t)
  6958. (re-search-forward org-outline-regexp-bol)
  6959. (beginning-of-line 0))
  6960. (skip-chars-backward " \r\t\n")
  6961. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6962. (looking-at "[ \t]+") (replace-match ""))
  6963. (unless (eobp) (forward-char 1))
  6964. (when (looking-at "^\\*")
  6965. (unless (bobp) (backward-char 1))
  6966. (insert "\n")))
  6967. ;; If we are splitting, grab the text that should be moved
  6968. ;; to the new headline.
  6969. (when may-split
  6970. (if (org-on-heading-p)
  6971. ;; This is a heading: split intelligently (keeping
  6972. ;; tags).
  6973. (let ((pos (point)))
  6974. (beginning-of-line)
  6975. (unless (looking-at org-complex-heading-regexp)
  6976. (error "This should not happen"))
  6977. (when (and (match-beginning 4)
  6978. (> pos (match-beginning 4))
  6979. (< pos (match-end 4)))
  6980. (setq initial-content (buffer-substring pos (match-end 4)))
  6981. (goto-char pos)
  6982. (delete-region (point) (match-end 4))
  6983. (if (looking-at "[ \t]*$")
  6984. (replace-match "")
  6985. (insert (make-string (length initial-content) ?\s)))
  6986. (setq initial-content (org-trim initial-content)))
  6987. (goto-char pos))
  6988. ;; A normal line.
  6989. (setq initial-content
  6990. (org-trim
  6991. (delete-and-extract-region (point) (line-end-position))))))
  6992. ;; If we are at the beginning of the line, insert before it.
  6993. ;; Otherwise, after it.
  6994. (cond
  6995. ((and (bolp) (looking-at "[ \t]*$")))
  6996. ((bolp) (save-excursion (insert "\n")))
  6997. (t (end-of-line)
  6998. (insert "\n")))
  6999. ;; Insert the new heading
  7000. (insert stars)
  7001. (just-one-space)
  7002. (insert initial-content)
  7003. (unless (and blank (org-previous-line-empty-p))
  7004. (org-N-empty-lines-before-current (if blank 1 0)))
  7005. ;; Adjust visibility, which may be messed up if we removed
  7006. ;; blank lines while previous entry was hidden.
  7007. (let ((bol (line-beginning-position)))
  7008. (dolist (o (overlays-at (1- bol)))
  7009. (when (and (eq (overlay-get o 'invisible) 'outline)
  7010. (eq (overlay-end o) bol))
  7011. (move-overlay o (overlay-start o) (1- bol)))))
  7012. (run-hooks 'org-insert-heading-hook)))))))
  7013. (defun org-N-empty-lines-before-current (N)
  7014. "Make the number of empty lines before current exactly N.
  7015. So this will delete or add empty lines."
  7016. (save-excursion
  7017. (beginning-of-line)
  7018. (let ((p (point)))
  7019. (skip-chars-backward " \r\t\n")
  7020. (unless (bolp) (forward-line))
  7021. (delete-region (point) p))
  7022. (when (> N 0) (insert (make-string N ?\n)))))
  7023. (defun org-get-heading (&optional no-tags no-todo)
  7024. "Return the heading of the current entry, without the stars.
  7025. When NO-TAGS is non-nil, don't include tags.
  7026. When NO-TODO is non-nil, don't include TODO keywords."
  7027. (save-excursion
  7028. (org-back-to-heading t)
  7029. (cond
  7030. ((and no-tags no-todo)
  7031. (looking-at org-complex-heading-regexp)
  7032. (match-string 4))
  7033. (no-tags
  7034. (looking-at (concat org-outline-regexp
  7035. "\\(.*?\\)"
  7036. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7037. (match-string 1))
  7038. (no-todo
  7039. (looking-at org-todo-line-regexp)
  7040. (match-string 3))
  7041. (t (looking-at org-heading-regexp)
  7042. (match-string 2)))))
  7043. (defvar orgstruct-mode) ; defined below
  7044. (defun org-heading-components ()
  7045. "Return the components of the current heading.
  7046. This is a list with the following elements:
  7047. - the level as an integer
  7048. - the reduced level, different if `org-odd-levels-only' is set.
  7049. - the TODO keyword, or nil
  7050. - the priority character, like ?A, or nil if no priority is given
  7051. - the headline text itself, or the tags string if no headline text
  7052. - the tags string, or nil."
  7053. (save-excursion
  7054. (org-back-to-heading t)
  7055. (if (let (case-fold-search)
  7056. (looking-at
  7057. (if orgstruct-mode
  7058. org-heading-regexp
  7059. org-complex-heading-regexp)))
  7060. (if orgstruct-mode
  7061. (list (length (match-string 1))
  7062. (org-reduced-level (length (match-string 1)))
  7063. nil
  7064. nil
  7065. (match-string 2)
  7066. nil)
  7067. (list (length (match-string 1))
  7068. (org-reduced-level (length (match-string 1)))
  7069. (org-match-string-no-properties 2)
  7070. (and (match-end 3) (aref (match-string 3) 2))
  7071. (org-match-string-no-properties 4)
  7072. (org-match-string-no-properties 5))))))
  7073. (defun org-get-entry ()
  7074. "Get the entry text, after heading, entire subtree."
  7075. (save-excursion
  7076. (org-back-to-heading t)
  7077. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7078. (defun org-insert-heading-after-current ()
  7079. "Insert a new heading with same level as current, after current subtree."
  7080. (interactive)
  7081. (org-back-to-heading)
  7082. (org-insert-heading)
  7083. (org-move-subtree-down)
  7084. (end-of-line 1))
  7085. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7086. "Insert heading with `org-insert-heading-respect-content' set to t."
  7087. (interactive)
  7088. (org-insert-heading '(4) invisible-ok))
  7089. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7090. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7091. (interactive)
  7092. (org-insert-todo-heading force-state '(4)))
  7093. (defun org-insert-todo-heading (arg &optional force-heading)
  7094. "Insert a new heading with the same level and TODO state as current heading.
  7095. If the heading has no TODO state, or if the state is DONE, use the first
  7096. state (TODO by default). Also with one prefix arg, force first state. With
  7097. two prefix args, force inserting at the end of the parent subtree."
  7098. (interactive "P")
  7099. (when (or force-heading (not (org-insert-item 'checkbox)))
  7100. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7101. force-heading))
  7102. (save-excursion
  7103. (org-back-to-heading)
  7104. (outline-previous-heading)
  7105. (looking-at org-todo-line-regexp))
  7106. (let*
  7107. ((new-mark-x
  7108. (if (or (equal arg '(4))
  7109. (not (match-beginning 2))
  7110. (member (match-string 2) org-done-keywords))
  7111. (car org-todo-keywords-1)
  7112. (match-string 2)))
  7113. (new-mark
  7114. (or
  7115. (run-hook-with-args-until-success
  7116. 'org-todo-get-default-hook new-mark-x nil)
  7117. new-mark-x)))
  7118. (beginning-of-line 1)
  7119. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7120. (if org-treat-insert-todo-heading-as-state-change
  7121. (org-todo new-mark)
  7122. (insert new-mark " "))))
  7123. (when org-provide-todo-statistics
  7124. (org-update-parent-todo-statistics))))
  7125. (defun org-insert-subheading (arg)
  7126. "Insert a new subheading and demote it.
  7127. Works for outline headings and for plain lists alike."
  7128. (interactive "P")
  7129. (org-insert-heading arg)
  7130. (cond
  7131. ((org-at-heading-p) (org-do-demote))
  7132. ((org-at-item-p) (org-indent-item))))
  7133. (defun org-insert-todo-subheading (arg)
  7134. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7135. Works for outline headings and for plain lists alike."
  7136. (interactive "P")
  7137. (org-insert-todo-heading arg)
  7138. (cond
  7139. ((org-at-heading-p) (org-do-demote))
  7140. ((org-at-item-p) (org-indent-item))))
  7141. ;;; Promotion and Demotion
  7142. (defvar org-after-demote-entry-hook nil
  7143. "Hook run after an entry has been demoted.
  7144. The cursor will be at the beginning of the entry.
  7145. When a subtree is being demoted, the hook will be called for each node.")
  7146. (defvar org-after-promote-entry-hook nil
  7147. "Hook run after an entry has been promoted.
  7148. The cursor will be at the beginning of the entry.
  7149. When a subtree is being promoted, the hook will be called for each node.")
  7150. (defun org-promote-subtree ()
  7151. "Promote the entire subtree.
  7152. See also `org-promote'."
  7153. (interactive)
  7154. (save-excursion
  7155. (org-with-limited-levels (org-map-tree 'org-promote)))
  7156. (org-fix-position-after-promote))
  7157. (defun org-demote-subtree ()
  7158. "Demote the entire subtree. See `org-demote'.
  7159. See also `org-promote'."
  7160. (interactive)
  7161. (save-excursion
  7162. (org-with-limited-levels (org-map-tree 'org-demote)))
  7163. (org-fix-position-after-promote))
  7164. (defun org-do-promote ()
  7165. "Promote the current heading higher up the tree.
  7166. If the region is active in `transient-mark-mode', promote all headings
  7167. in the region."
  7168. (interactive)
  7169. (save-excursion
  7170. (if (org-region-active-p)
  7171. (org-map-region 'org-promote (region-beginning) (region-end))
  7172. (org-promote)))
  7173. (org-fix-position-after-promote))
  7174. (defun org-do-demote ()
  7175. "Demote the current heading lower down the tree.
  7176. If the region is active in `transient-mark-mode', demote all headings
  7177. in the region."
  7178. (interactive)
  7179. (save-excursion
  7180. (if (org-region-active-p)
  7181. (org-map-region 'org-demote (region-beginning) (region-end))
  7182. (org-demote)))
  7183. (org-fix-position-after-promote))
  7184. (defun org-fix-position-after-promote ()
  7185. "Make sure that after pro/demotion cursor position is right."
  7186. (let ((pos (point)))
  7187. (when (save-excursion
  7188. (beginning-of-line 1)
  7189. (looking-at org-todo-line-regexp)
  7190. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7191. (cond ((eobp) (insert " "))
  7192. ((eolp) (insert " "))
  7193. ((equal (char-after) ?\ ) (forward-char 1))))))
  7194. (defun org-current-level ()
  7195. "Return the level of the current entry, or nil if before the first headline.
  7196. The level is the number of stars at the beginning of the
  7197. headline. Use `org-reduced-level' to remove the effect of
  7198. `org-odd-levels'. Unlike to `org-outline-level', this function
  7199. ignores inlinetasks."
  7200. (let ((level (org-with-limited-levels (org-outline-level))))
  7201. (and (> level 0) level)))
  7202. (defun org-get-previous-line-level ()
  7203. "Return the outline depth of the last headline before the current line.
  7204. Returns 0 for the first headline in the buffer, and nil if before the
  7205. first headline."
  7206. (and (org-current-level)
  7207. (or (and (/= (line-beginning-position) (point-min))
  7208. (save-excursion (beginning-of-line 0) (org-current-level)))
  7209. 0)))
  7210. (defun org-reduced-level (l)
  7211. "Compute the effective level of a heading.
  7212. This takes into account the setting of `org-odd-levels-only'."
  7213. (cond
  7214. ((zerop l) 0)
  7215. (org-odd-levels-only (1+ (floor (/ l 2))))
  7216. (t l)))
  7217. (defun org-level-increment ()
  7218. "Return the number of stars that will be added or removed at a
  7219. time to headlines when structure editing, based on the value of
  7220. `org-odd-levels-only'."
  7221. (if org-odd-levels-only 2 1))
  7222. (defun org-get-valid-level (level &optional change)
  7223. "Rectify a level change under the influence of `org-odd-levels-only'
  7224. LEVEL is a current level, CHANGE is by how much the level should be
  7225. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7226. even level numbers will become the next higher odd number."
  7227. (if org-odd-levels-only
  7228. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7229. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7230. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7231. (max 1 (+ level (or change 0)))))
  7232. (if (boundp 'define-obsolete-function-alias)
  7233. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7234. (define-obsolete-function-alias 'org-get-legal-level
  7235. 'org-get-valid-level)
  7236. (define-obsolete-function-alias 'org-get-legal-level
  7237. 'org-get-valid-level "23.1")))
  7238. (defun org-promote ()
  7239. "Promote the current heading higher up the tree."
  7240. (org-with-wide-buffer
  7241. (org-back-to-heading t)
  7242. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7243. after-change-functions))
  7244. (level (save-match-data (funcall outline-level)))
  7245. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7246. (diff (abs (- level (length up-head) -1))))
  7247. (cond
  7248. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7249. (replace-match "# " nil t))
  7250. ((= level 1)
  7251. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7252. (t (replace-match up-head nil t)))
  7253. (unless (= level 1)
  7254. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7255. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7256. (run-hooks 'org-after-promote-entry-hook))))
  7257. (defun org-demote ()
  7258. "Demote the current heading lower down the tree."
  7259. (org-with-wide-buffer
  7260. (org-back-to-heading t)
  7261. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7262. after-change-functions))
  7263. (level (save-match-data (funcall outline-level)))
  7264. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7265. (diff (abs (- level (length down-head) -1))))
  7266. (replace-match down-head nil t)
  7267. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7268. (when org-adapt-indentation (org-fixup-indentation diff))
  7269. (run-hooks 'org-after-demote-entry-hook))))
  7270. (defun org-cycle-level ()
  7271. "Cycle the level of an empty headline through possible states.
  7272. This goes first to child, then to parent, level, then up the hierarchy.
  7273. After top level, it switches back to sibling level."
  7274. (interactive)
  7275. (let ((org-adapt-indentation nil))
  7276. (when (org-point-at-end-of-empty-headline)
  7277. (setq this-command 'org-cycle-level) ; Only needed for caching
  7278. (let ((cur-level (org-current-level))
  7279. (prev-level (org-get-previous-line-level)))
  7280. (cond
  7281. ;; If first headline in file, promote to top-level.
  7282. ((= prev-level 0)
  7283. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7284. do (org-do-promote)))
  7285. ;; If same level as prev, demote one.
  7286. ((= prev-level cur-level)
  7287. (org-do-demote))
  7288. ;; If parent is top-level, promote to top level if not already.
  7289. ((= prev-level 1)
  7290. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7291. do (org-do-promote)))
  7292. ;; If top-level, return to prev-level.
  7293. ((= cur-level 1)
  7294. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7295. do (org-do-demote)))
  7296. ;; If less than prev-level, promote one.
  7297. ((< cur-level prev-level)
  7298. (org-do-promote))
  7299. ;; If deeper than prev-level, promote until higher than
  7300. ;; prev-level.
  7301. ((> cur-level prev-level)
  7302. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7303. do (org-do-promote))))
  7304. t))))
  7305. (defun org-map-tree (fun)
  7306. "Call FUN for every heading underneath the current one."
  7307. (org-back-to-heading)
  7308. (let ((level (funcall outline-level)))
  7309. (save-excursion
  7310. (funcall fun)
  7311. (while (and (progn
  7312. (outline-next-heading)
  7313. (> (funcall outline-level) level))
  7314. (not (eobp)))
  7315. (funcall fun)))))
  7316. (defun org-map-region (fun beg end)
  7317. "Call FUN for every heading between BEG and END."
  7318. (let ((org-ignore-region t))
  7319. (save-excursion
  7320. (setq end (copy-marker end))
  7321. (goto-char beg)
  7322. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7323. (< (point) end))
  7324. (funcall fun))
  7325. (while (and (progn
  7326. (outline-next-heading)
  7327. (< (point) end))
  7328. (not (eobp)))
  7329. (funcall fun)))))
  7330. (defun org-fixup-indentation (diff)
  7331. "Change the indentation in the current entry by DIFF.
  7332. DIFF is an integer. Indentation is done according to the
  7333. following rules:
  7334. - Planning information and property drawers are always indented
  7335. according to the new level of the headline;
  7336. - Footnote definitions and their contents are ignored;
  7337. - Inlinetasks' boundaries are not shifted;
  7338. - Empty lines are ignored;
  7339. - Other lines' indentation are shifted by DIFF columns, unless
  7340. it would introduce a structural change in the document, in
  7341. which case no shifting is done at all.
  7342. Assume point is at a heading or an inlinetask beginning."
  7343. (org-with-wide-buffer
  7344. (narrow-to-region (line-beginning-position)
  7345. (save-excursion
  7346. (if (org-with-limited-levels (org-at-heading-p))
  7347. (org-with-limited-levels (outline-next-heading))
  7348. (org-inlinetask-goto-end))
  7349. (point)))
  7350. (forward-line)
  7351. ;; Indent properly planning info and property drawer.
  7352. (when (org-looking-at-p org-planning-line-re)
  7353. (org-indent-line)
  7354. (forward-line))
  7355. (when (looking-at org-property-drawer-re)
  7356. (goto-char (match-end 0))
  7357. (forward-line)
  7358. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7359. (catch 'no-shift
  7360. (when (zerop diff) (throw 'no-shift nil))
  7361. ;; If DIFF is negative, first check if a shift is possible at all
  7362. ;; (e.g., it doesn't break structure). This can only happen if
  7363. ;; some contents are not properly indented.
  7364. (let ((case-fold-search t))
  7365. (when (< diff 0)
  7366. (let ((diff (- diff))
  7367. (forbidden-re (concat org-outline-regexp
  7368. "\\|"
  7369. (substring org-footnote-definition-re 1))))
  7370. (save-excursion
  7371. (while (not (eobp))
  7372. (cond
  7373. ((org-looking-at-p "[ \t]*$") (forward-line))
  7374. ((and (org-looking-at-p org-footnote-definition-re)
  7375. (let ((e (org-element-at-point)))
  7376. (and (eq (org-element-type e) 'footnote-definition)
  7377. (goto-char (org-element-property :end e))))))
  7378. ((org-looking-at-p org-outline-regexp) (forward-line))
  7379. ;; Give up if shifting would move before column 0 or
  7380. ;; if it would introduce a headline or a footnote
  7381. ;; definition.
  7382. (t
  7383. (skip-chars-forward " \t")
  7384. (let ((ind (current-column)))
  7385. (when (or (< ind diff)
  7386. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7387. (throw 'no-shift nil)))
  7388. ;; Ignore contents of example blocks and source
  7389. ;; blocks if their indentation is meant to be
  7390. ;; preserved. Jump to block's closing line.
  7391. (beginning-of-line)
  7392. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7393. (let ((e (org-element-at-point)))
  7394. (and (memq (org-element-type e)
  7395. '(example-block src-block))
  7396. (or org-src-preserve-indentation
  7397. (org-element-property :preserve-indent e))
  7398. (goto-char (org-element-property :end e))
  7399. (progn (skip-chars-backward " \r\t\n")
  7400. (beginning-of-line)
  7401. t))))
  7402. (forward-line))))))))
  7403. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7404. ;; by DIFF. Also skip contents of source or example blocks
  7405. ;; when indentation is meant to be preserved.
  7406. (while (not (eobp))
  7407. (cond
  7408. ((and (org-looking-at-p org-footnote-definition-re)
  7409. (let ((e (org-element-at-point)))
  7410. (and (eq (org-element-type e) 'footnote-definition)
  7411. (goto-char (org-element-property :end e))))))
  7412. ((org-looking-at-p org-outline-regexp) (forward-line))
  7413. ((org-looking-at-p "[ \t]*$") (forward-line))
  7414. (t
  7415. (org-indent-line-to (+ (org-get-indentation) diff))
  7416. (beginning-of-line)
  7417. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7418. (let ((e (org-element-at-point)))
  7419. (and (memq (org-element-type e)
  7420. '(example-block src-block))
  7421. (or org-src-preserve-indentation
  7422. (org-element-property :preserve-indent e))
  7423. (goto-char (org-element-property :end e))
  7424. (progn (skip-chars-backward " \r\t\n")
  7425. (beginning-of-line)
  7426. t))))
  7427. (forward-line)))))))))
  7428. (defun org-convert-to-odd-levels ()
  7429. "Convert an org-mode file with all levels allowed to one with odd levels.
  7430. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7431. level 5 etc."
  7432. (interactive)
  7433. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7434. (let ((outline-level 'org-outline-level)
  7435. (org-odd-levels-only nil) n)
  7436. (save-excursion
  7437. (goto-char (point-min))
  7438. (while (re-search-forward "^\\*\\*+ " nil t)
  7439. (setq n (- (length (match-string 0)) 2))
  7440. (while (>= (setq n (1- n)) 0)
  7441. (org-demote))
  7442. (end-of-line 1))))))
  7443. (defun org-convert-to-oddeven-levels ()
  7444. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7445. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7446. file contains a section with an even level, conversion would
  7447. destroy the structure of the file. An error is signaled in this
  7448. case."
  7449. (interactive)
  7450. (goto-char (point-min))
  7451. ;; First check if there are no even levels
  7452. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7453. (org-show-set-visibility 'canonical)
  7454. (error "Not all levels are odd in this file. Conversion not possible"))
  7455. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7456. (let ((outline-regexp org-outline-regexp)
  7457. (outline-level 'org-outline-level)
  7458. (org-odd-levels-only nil) n)
  7459. (save-excursion
  7460. (goto-char (point-min))
  7461. (while (re-search-forward "^\\*\\*+ " nil t)
  7462. (setq n (/ (1- (length (match-string 0))) 2))
  7463. (while (>= (setq n (1- n)) 0)
  7464. (org-promote))
  7465. (end-of-line 1))))))
  7466. (defun org-tr-level (n)
  7467. "Make N odd if required."
  7468. (if org-odd-levels-only (1+ (/ n 2)) n))
  7469. ;;; Vertical tree motion, cutting and pasting of subtrees
  7470. (defun org-move-subtree-up (&optional arg)
  7471. "Move the current subtree up past ARG headlines of the same level."
  7472. (interactive "p")
  7473. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7474. (defun org-move-subtree-down (&optional arg)
  7475. "Move the current subtree down past ARG headlines of the same level."
  7476. (interactive "p")
  7477. (setq arg (prefix-numeric-value arg))
  7478. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7479. 'org-get-last-sibling))
  7480. (ins-point (make-marker))
  7481. (cnt (abs arg))
  7482. (col (current-column))
  7483. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7484. ;; Select the tree
  7485. (org-back-to-heading)
  7486. (setq beg0 (point))
  7487. (save-excursion
  7488. (setq ne-beg (org-back-over-empty-lines))
  7489. (setq beg (point)))
  7490. (save-match-data
  7491. (save-excursion (outline-end-of-heading)
  7492. (setq folded (outline-invisible-p)))
  7493. (progn (org-end-of-subtree nil t)
  7494. (unless (eobp) (backward-char))))
  7495. (outline-next-heading)
  7496. (setq ne-end (org-back-over-empty-lines))
  7497. (setq end (point))
  7498. (goto-char beg0)
  7499. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7500. ;; include less whitespace
  7501. (save-excursion
  7502. (goto-char beg)
  7503. (forward-line (- ne-beg ne-end))
  7504. (setq beg (point))))
  7505. ;; Find insertion point, with error handling
  7506. (while (> cnt 0)
  7507. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7508. (progn (goto-char beg0)
  7509. (user-error "Cannot move past superior level or buffer limit")))
  7510. (setq cnt (1- cnt)))
  7511. (if (> arg 0)
  7512. ;; Moving forward - still need to move over subtree
  7513. (progn (org-end-of-subtree t t)
  7514. (save-excursion
  7515. (org-back-over-empty-lines)
  7516. (or (bolp) (newline)))))
  7517. (setq ne-ins (org-back-over-empty-lines))
  7518. (move-marker ins-point (point))
  7519. (setq txt (buffer-substring beg end))
  7520. (org-save-markers-in-region beg end)
  7521. (delete-region beg end)
  7522. (org-remove-empty-overlays-at beg)
  7523. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7524. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7525. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7526. (let ((bbb (point)))
  7527. (insert-before-markers txt)
  7528. (org-reinstall-markers-in-region bbb)
  7529. (move-marker ins-point bbb))
  7530. (or (bolp) (insert "\n"))
  7531. (setq ins-end (point))
  7532. (goto-char ins-point)
  7533. (org-skip-whitespace)
  7534. (when (and (< arg 0)
  7535. (org-first-sibling-p)
  7536. (> ne-ins ne-beg))
  7537. ;; Move whitespace back to beginning
  7538. (save-excursion
  7539. (goto-char ins-end)
  7540. (let ((kill-whole-line t))
  7541. (kill-line (- ne-ins ne-beg)) (point)))
  7542. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7543. (move-marker ins-point nil)
  7544. (if folded
  7545. (hide-subtree)
  7546. (org-show-entry)
  7547. (show-children)
  7548. (org-cycle-hide-drawers 'children))
  7549. (org-clean-visibility-after-subtree-move)
  7550. ;; move back to the initial column we were at
  7551. (move-to-column col)))
  7552. (defvar org-subtree-clip ""
  7553. "Clipboard for cut and paste of subtrees.
  7554. This is actually only a copy of the kill, because we use the normal kill
  7555. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7556. (defvar org-subtree-clip-folded nil
  7557. "Was the last copied subtree folded?
  7558. This is used to fold the tree back after pasting.")
  7559. (defun org-cut-subtree (&optional n)
  7560. "Cut the current subtree into the clipboard.
  7561. With prefix arg N, cut this many sequential subtrees.
  7562. This is a short-hand for marking the subtree and then cutting it."
  7563. (interactive "p")
  7564. (org-copy-subtree n 'cut))
  7565. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7566. "Copy the current subtree it in the clipboard.
  7567. With prefix arg N, copy this many sequential subtrees.
  7568. This is a short-hand for marking the subtree and then copying it.
  7569. If CUT is non-nil, actually cut the subtree.
  7570. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7571. of some markers in the region, even if CUT is non-nil. This is
  7572. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7573. (interactive "p")
  7574. (let (beg end folded (beg0 (point)))
  7575. (if (org-called-interactively-p 'any)
  7576. (org-back-to-heading nil) ; take what looks like a subtree
  7577. (org-back-to-heading t)) ; take what is really there
  7578. (setq beg (point))
  7579. (skip-chars-forward " \t\r\n")
  7580. (save-match-data
  7581. (if nosubtrees
  7582. (outline-next-heading)
  7583. (save-excursion (outline-end-of-heading)
  7584. (setq folded (outline-invisible-p)))
  7585. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7586. (org-end-of-subtree t t)))
  7587. ;; Include the end of an inlinetask
  7588. (when (and (featurep 'org-inlinetask)
  7589. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7590. "END[ \t]*$")))
  7591. (end-of-line))
  7592. (setq end (point))
  7593. (goto-char beg0)
  7594. (when (> end beg)
  7595. (setq org-subtree-clip-folded folded)
  7596. (when (or cut force-store-markers)
  7597. (org-save-markers-in-region beg end))
  7598. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7599. (setq org-subtree-clip (current-kill 0))
  7600. (message "%s: Subtree(s) with %d characters"
  7601. (if cut "Cut" "Copied")
  7602. (length org-subtree-clip)))))
  7603. (defun org-paste-subtree (&optional level tree for-yank remove)
  7604. "Paste the clipboard as a subtree, with modification of headline level.
  7605. The entire subtree is promoted or demoted in order to match a new headline
  7606. level.
  7607. If the cursor is at the beginning of a headline, the same level as
  7608. that headline is used to paste the tree.
  7609. If not, the new level is derived from the *visible* headings
  7610. before and after the insertion point, and taken to be the inferior headline
  7611. level of the two. So if the previous visible heading is level 3 and the
  7612. next is level 4 (or vice versa), level 4 will be used for insertion.
  7613. This makes sure that the subtree remains an independent subtree and does
  7614. not swallow low level entries.
  7615. You can also force a different level, either by using a numeric prefix
  7616. argument, or by inserting the heading marker by hand. For example, if the
  7617. cursor is after \"*****\", then the tree will be shifted to level 5.
  7618. If optional TREE is given, use this text instead of the kill ring.
  7619. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7620. move back over whitespace before inserting, and move point to the end of
  7621. the inserted text when done.
  7622. When REMOVE is non-nil, remove the subtree from the clipboard."
  7623. (interactive "P")
  7624. (setq tree (or tree (and kill-ring (current-kill 0))))
  7625. (unless (org-kill-is-subtree-p tree)
  7626. (user-error "%s"
  7627. (substitute-command-keys
  7628. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7629. (org-with-limited-levels
  7630. (let* ((visp (not (outline-invisible-p)))
  7631. (txt tree)
  7632. (^re_ "\\(\\*+\\)[ \t]*")
  7633. (old-level (if (string-match org-outline-regexp-bol txt)
  7634. (- (match-end 0) (match-beginning 0) 1)
  7635. -1))
  7636. (force-level (cond (level (prefix-numeric-value level))
  7637. ((and (looking-at "[ \t]*$")
  7638. (string-match
  7639. "^\\*+$" (buffer-substring
  7640. (point-at-bol) (point))))
  7641. (- (match-end 0) (match-beginning 0)))
  7642. ((and (bolp)
  7643. (looking-at org-outline-regexp))
  7644. (- (match-end 0) (point) 1))))
  7645. (previous-level (save-excursion
  7646. (condition-case nil
  7647. (progn
  7648. (outline-previous-visible-heading 1)
  7649. (if (looking-at ^re_)
  7650. (- (match-end 0) (match-beginning 0) 1)
  7651. 1))
  7652. (error 1))))
  7653. (next-level (save-excursion
  7654. (condition-case nil
  7655. (progn
  7656. (or (looking-at org-outline-regexp)
  7657. (outline-next-visible-heading 1))
  7658. (if (looking-at ^re_)
  7659. (- (match-end 0) (match-beginning 0) 1)
  7660. 1))
  7661. (error 1))))
  7662. (new-level (or force-level (max previous-level next-level)))
  7663. (shift (if (or (= old-level -1)
  7664. (= new-level -1)
  7665. (= old-level new-level))
  7666. 0
  7667. (- new-level old-level)))
  7668. (delta (if (> shift 0) -1 1))
  7669. (func (if (> shift 0) 'org-demote 'org-promote))
  7670. (org-odd-levels-only nil)
  7671. beg end newend)
  7672. ;; Remove the forced level indicator
  7673. (if force-level
  7674. (delete-region (point-at-bol) (point)))
  7675. ;; Paste
  7676. (beginning-of-line (if (bolp) 1 2))
  7677. (setq beg (point))
  7678. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7679. (insert-before-markers txt)
  7680. (unless (string-match "\n\\'" txt) (insert "\n"))
  7681. (setq newend (point))
  7682. (org-reinstall-markers-in-region beg)
  7683. (setq end (point))
  7684. (goto-char beg)
  7685. (skip-chars-forward " \t\n\r")
  7686. (setq beg (point))
  7687. (if (and (outline-invisible-p) visp)
  7688. (save-excursion (outline-show-heading)))
  7689. ;; Shift if necessary
  7690. (unless (= shift 0)
  7691. (save-restriction
  7692. (narrow-to-region beg end)
  7693. (while (not (= shift 0))
  7694. (org-map-region func (point-min) (point-max))
  7695. (setq shift (+ delta shift)))
  7696. (goto-char (point-min))
  7697. (setq newend (point-max))))
  7698. (when (or (org-called-interactively-p 'interactive) for-yank)
  7699. (message "Clipboard pasted as level %d subtree" new-level))
  7700. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7701. kill-ring
  7702. (eq org-subtree-clip (current-kill 0))
  7703. org-subtree-clip-folded)
  7704. ;; The tree was folded before it was killed/copied
  7705. (hide-subtree))
  7706. (and for-yank (goto-char newend))
  7707. (and remove (setq kill-ring (cdr kill-ring))))))
  7708. (defun org-kill-is-subtree-p (&optional txt)
  7709. "Check if the current kill is an outline subtree, or a set of trees.
  7710. Returns nil if kill does not start with a headline, or if the first
  7711. headline level is not the largest headline level in the tree.
  7712. So this will actually accept several entries of equal levels as well,
  7713. which is OK for `org-paste-subtree'.
  7714. If optional TXT is given, check this string instead of the current kill."
  7715. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7716. (re (org-get-limited-outline-regexp))
  7717. (^re (concat "^" re))
  7718. (start-level (and kill
  7719. (string-match
  7720. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7721. kill)
  7722. (- (match-end 2) (match-beginning 2) 1)))
  7723. (start (1+ (or (match-beginning 2) -1))))
  7724. (if (not start-level)
  7725. (progn
  7726. nil) ;; does not even start with a heading
  7727. (catch 'exit
  7728. (while (setq start (string-match ^re kill (1+ start)))
  7729. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7730. (throw 'exit nil)))
  7731. t))))
  7732. (defvar org-markers-to-move nil
  7733. "Markers that should be moved with a cut-and-paste operation.
  7734. Those markers are stored together with their positions relative to
  7735. the start of the region.")
  7736. (defun org-save-markers-in-region (beg end)
  7737. "Check markers in region.
  7738. If these markers are between BEG and END, record their position relative
  7739. to BEG, so that after moving the block of text, we can put the markers back
  7740. into place.
  7741. This function gets called just before an entry or tree gets cut from the
  7742. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7743. called immediately, to move the markers with the entries."
  7744. (setq org-markers-to-move nil)
  7745. (when (featurep 'org-clock)
  7746. (org-clock-save-markers-for-cut-and-paste beg end))
  7747. (when (featurep 'org-agenda)
  7748. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7749. (defun org-check-and-save-marker (marker beg end)
  7750. "Check if MARKER is between BEG and END.
  7751. If yes, remember the marker and the distance to BEG."
  7752. (when (and (marker-buffer marker)
  7753. (equal (marker-buffer marker) (current-buffer)))
  7754. (if (and (>= marker beg) (< marker end))
  7755. (push (cons marker (- marker beg)) org-markers-to-move))))
  7756. (defun org-reinstall-markers-in-region (beg)
  7757. "Move all remembered markers to their position relative to BEG."
  7758. (mapc (lambda (x)
  7759. (move-marker (car x) (+ beg (cdr x))))
  7760. org-markers-to-move)
  7761. (setq org-markers-to-move nil))
  7762. (defun org-narrow-to-subtree ()
  7763. "Narrow buffer to the current subtree."
  7764. (interactive)
  7765. (save-excursion
  7766. (save-match-data
  7767. (org-with-limited-levels
  7768. (narrow-to-region
  7769. (progn (org-back-to-heading t) (point))
  7770. (progn (org-end-of-subtree t t)
  7771. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7772. (point)))))))
  7773. (defun org-narrow-to-block ()
  7774. "Narrow buffer to the current block."
  7775. (interactive)
  7776. (let* ((case-fold-search t)
  7777. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7778. "^[ \t]*#\\+end_.*")))
  7779. (if blockp
  7780. (narrow-to-region (car blockp) (cdr blockp))
  7781. (user-error "Not in a block"))))
  7782. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7783. "Clone the task (subtree) at point N times.
  7784. The clones will be inserted as siblings.
  7785. In interactive use, the user will be prompted for the number of
  7786. clones to be produced. If the entry has a timestamp, the user
  7787. will also be prompted for a time shift, which may be a repeater
  7788. as used in time stamps, for example `+3d'. To disable this,
  7789. you can call the function with a universal prefix argument.
  7790. When a valid repeater is given and the entry contains any time
  7791. stamps, the clones will become a sequence in time, with time
  7792. stamps in the subtree shifted for each clone produced. If SHIFT
  7793. is nil or the empty string, time stamps will be left alone. The
  7794. ID property of the original subtree is removed.
  7795. If the original subtree did contain time stamps with a repeater,
  7796. the following will happen:
  7797. - the repeater will be removed in each clone
  7798. - an additional clone will be produced, with the current, unshifted
  7799. date(s) in the entry.
  7800. - the original entry will be placed *after* all the clones, with
  7801. repeater intact.
  7802. - the start days in the repeater in the original entry will be shifted
  7803. to past the last clone.
  7804. In this way you can spell out a number of instances of a repeating task,
  7805. and still retain the repeater to cover future instances of the task.
  7806. As described above, N+1 clones are produced when the original
  7807. subtree has a repeater. Setting N to 0, then, can be used to
  7808. remove the repeater from a subtree and create a shifted clone
  7809. with the original repeater."
  7810. (interactive "nNumber of clones to produce: ")
  7811. (let ((shift
  7812. (or shift
  7813. (if (and (not (equal current-prefix-arg '(4)))
  7814. (save-excursion
  7815. (re-search-forward org-ts-regexp-both
  7816. (save-excursion
  7817. (org-end-of-subtree t)
  7818. (point)) t)))
  7819. (read-from-minibuffer
  7820. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7821. ""))) ;; No time shift
  7822. (n-no-remove -1)
  7823. (drawer-re org-drawer-regexp)
  7824. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7825. beg end template task idprop
  7826. shift-n shift-what doshift nmin nmax)
  7827. (unless (wholenump n)
  7828. (user-error "Invalid number of replications %s" n))
  7829. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7830. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7831. shift)))
  7832. (user-error "Invalid shift specification %s" shift))
  7833. (when doshift
  7834. (setq shift-n (string-to-number (match-string 1 shift))
  7835. shift-what (cdr (assoc (match-string 2 shift)
  7836. '(("d" . day) ("w" . week)
  7837. ("m" . month) ("y" . year))))))
  7838. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7839. (setq nmin 1 nmax n)
  7840. (org-back-to-heading t)
  7841. (setq beg (point))
  7842. (setq idprop (org-entry-get nil "ID"))
  7843. (org-end-of-subtree t t)
  7844. (or (bolp) (insert "\n"))
  7845. (setq end (point))
  7846. (setq template (buffer-substring beg end))
  7847. (when (and doshift
  7848. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7849. (delete-region beg end)
  7850. (setq end beg)
  7851. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7852. (goto-char end)
  7853. (loop for n from nmin to nmax do
  7854. ;; prepare clone
  7855. (with-temp-buffer
  7856. (insert template)
  7857. (org-mode)
  7858. (goto-char (point-min))
  7859. (org-show-subtree)
  7860. (and idprop (if org-clone-delete-id
  7861. (org-entry-delete nil "ID")
  7862. (org-id-get-create t)))
  7863. (unless (= n 0)
  7864. (while (re-search-forward org-clock-re nil t)
  7865. (kill-whole-line))
  7866. (goto-char (point-min))
  7867. (while (re-search-forward drawer-re nil t)
  7868. (org-remove-empty-drawer-at (point))))
  7869. (goto-char (point-min))
  7870. (when doshift
  7871. (while (re-search-forward org-ts-regexp-both nil t)
  7872. (org-timestamp-change (* n shift-n) shift-what))
  7873. (unless (= n n-no-remove)
  7874. (goto-char (point-min))
  7875. (while (re-search-forward org-ts-regexp nil t)
  7876. (save-excursion
  7877. (goto-char (match-beginning 0))
  7878. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7879. (delete-region (match-beginning 1) (match-end 1)))))))
  7880. (setq task (buffer-string)))
  7881. (insert task))
  7882. (goto-char beg)))
  7883. ;;; Outline Sorting
  7884. (defun org-sort (with-case)
  7885. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7886. Optional argument WITH-CASE means sort case-sensitively."
  7887. (interactive "P")
  7888. (cond
  7889. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7890. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7891. (t
  7892. (org-call-with-arg 'org-sort-entries with-case))))
  7893. (defun org-sort-remove-invisible (s)
  7894. "Remove invisible links from string S."
  7895. (remove-text-properties 0 (length s) org-rm-props s)
  7896. (while (string-match org-bracket-link-regexp s)
  7897. (setq s (replace-match (if (match-end 2)
  7898. (match-string 3 s)
  7899. (match-string 1 s))
  7900. t t s)))
  7901. (let ((st (format " %s " s)))
  7902. (while (string-match org-emph-re st)
  7903. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7904. (setq s (substring st 1 -1)))
  7905. s)
  7906. (defvar org-priority-regexp) ; defined later in the file
  7907. (defvar org-after-sorting-entries-or-items-hook nil
  7908. "Hook that is run after a bunch of entries or items have been sorted.
  7909. When children are sorted, the cursor is in the parent line when this
  7910. hook gets called. When a region or a plain list is sorted, the cursor
  7911. will be in the first entry of the sorted region/list.")
  7912. (defun org-sort-entries
  7913. (&optional with-case sorting-type getkey-func compare-func property)
  7914. "Sort entries on a certain level of an outline tree.
  7915. If there is an active region, the entries in the region are sorted.
  7916. Else, if the cursor is before the first entry, sort the top-level items.
  7917. Else, the children of the entry at point are sorted.
  7918. Sorting can be alphabetically, numerically, by date/time as given by
  7919. a time stamp, by a property, by priority order, or by a custom function.
  7920. The command prompts for the sorting type unless it has been given to the
  7921. function through the SORTING-TYPE argument, which needs to be a character,
  7922. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7923. the precise meaning of each character:
  7924. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7925. c By creation time, which is assumed to be the first inactive time stamp
  7926. at the beginning of a line.
  7927. d By deadline date/time.
  7928. k By clocking time.
  7929. n Numerically, by converting the beginning of the entry/item to a number.
  7930. o By order of TODO keywords.
  7931. p By priority according to the cookie.
  7932. r By the value of a property.
  7933. s By scheduled date/time.
  7934. t By date/time, either the first active time stamp in the entry, or, if
  7935. none exist, by the first inactive one.
  7936. Capital letters will reverse the sort order.
  7937. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7938. called with point at the beginning of the record. It must return either
  7939. a string or a number that should serve as the sorting key for that record.
  7940. Comparing entries ignores case by default. However, with an optional argument
  7941. WITH-CASE, the sorting considers case as well.
  7942. Sorting is done against the visible part of the headlines, it ignores hidden
  7943. links.
  7944. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7945. (interactive "P")
  7946. (let ((case-func (if with-case 'identity 'downcase))
  7947. (cmstr
  7948. ;; The clock marker is lost when using `sort-subr', let's
  7949. ;; store the clocking string.
  7950. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7951. (save-excursion
  7952. (goto-char org-clock-marker)
  7953. (buffer-substring-no-properties (line-beginning-position)
  7954. (point)))))
  7955. start beg end stars re re2
  7956. txt what tmp)
  7957. ;; Find beginning and end of region to sort
  7958. (cond
  7959. ((org-region-active-p)
  7960. ;; we will sort the region
  7961. (setq end (region-end)
  7962. what "region")
  7963. (goto-char (region-beginning))
  7964. (if (not (org-at-heading-p)) (outline-next-heading))
  7965. (setq start (point)))
  7966. ((or (org-at-heading-p)
  7967. (ignore-errors (progn (org-back-to-heading) t)))
  7968. ;; we will sort the children of the current headline
  7969. (org-back-to-heading)
  7970. (setq start (point)
  7971. end (progn (org-end-of-subtree t t)
  7972. (or (bolp) (insert "\n"))
  7973. (when (>= (org-back-over-empty-lines) 1)
  7974. (forward-line 1))
  7975. (point))
  7976. what "children")
  7977. (goto-char start)
  7978. (show-subtree)
  7979. (outline-next-heading))
  7980. (t
  7981. ;; we will sort the top-level entries in this file
  7982. (goto-char (point-min))
  7983. (or (org-at-heading-p) (outline-next-heading))
  7984. (setq start (point))
  7985. (goto-char (point-max))
  7986. (beginning-of-line 1)
  7987. (when (looking-at ".*?\\S-")
  7988. ;; File ends in a non-white line
  7989. (end-of-line 1)
  7990. (insert "\n"))
  7991. (setq end (point-max))
  7992. (setq what "top-level")
  7993. (goto-char start)
  7994. (show-all)))
  7995. (setq beg (point))
  7996. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7997. (looking-at "\\(\\*+\\)")
  7998. (setq stars (match-string 1)
  7999. re (concat "^" (regexp-quote stars) " +")
  8000. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8001. txt (buffer-substring beg end))
  8002. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8003. (if (and (not (equal stars "*")) (string-match re2 txt))
  8004. (user-error "Region to sort contains a level above the first entry"))
  8005. (unless sorting-type
  8006. (message
  8007. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8008. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8009. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8010. what)
  8011. (setq sorting-type (read-char-exclusive))
  8012. (unless getkey-func
  8013. (and (= (downcase sorting-type) ?f)
  8014. (setq getkey-func
  8015. (org-icompleting-read "Sort using function: "
  8016. obarray 'fboundp t nil nil))
  8017. (setq getkey-func (intern getkey-func))))
  8018. (and (= (downcase sorting-type) ?r)
  8019. (not property)
  8020. (setq property
  8021. (org-icompleting-read "Property: "
  8022. (mapcar 'list (org-buffer-property-keys t))
  8023. nil t))))
  8024. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8025. (message "Sorting entries...")
  8026. (save-restriction
  8027. (narrow-to-region start end)
  8028. (let ((dcst (downcase sorting-type))
  8029. (case-fold-search nil)
  8030. (now (current-time)))
  8031. (sort-subr
  8032. (/= dcst sorting-type)
  8033. ;; This function moves to the beginning character of the "record" to
  8034. ;; be sorted.
  8035. (lambda nil
  8036. (if (re-search-forward re nil t)
  8037. (goto-char (match-beginning 0))
  8038. (goto-char (point-max))))
  8039. ;; This function moves to the last character of the "record" being
  8040. ;; sorted.
  8041. (lambda nil
  8042. (save-match-data
  8043. (condition-case nil
  8044. (outline-forward-same-level 1)
  8045. (error
  8046. (goto-char (point-max))))))
  8047. ;; This function returns the value that gets sorted against.
  8048. (lambda nil
  8049. (cond
  8050. ((= dcst ?n)
  8051. (if (looking-at org-complex-heading-regexp)
  8052. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8053. nil))
  8054. ((= dcst ?a)
  8055. (if (looking-at org-complex-heading-regexp)
  8056. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8057. nil))
  8058. ((= dcst ?k)
  8059. (or (get-text-property (point) :org-clock-minutes) 0))
  8060. ((= dcst ?t)
  8061. (let ((end (save-excursion (outline-next-heading) (point))))
  8062. (if (or (re-search-forward org-ts-regexp end t)
  8063. (re-search-forward org-ts-regexp-both end t))
  8064. (org-time-string-to-seconds (match-string 0))
  8065. (org-float-time now))))
  8066. ((= dcst ?c)
  8067. (let ((end (save-excursion (outline-next-heading) (point))))
  8068. (if (re-search-forward
  8069. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8070. end t)
  8071. (org-time-string-to-seconds (match-string 0))
  8072. (org-float-time now))))
  8073. ((= dcst ?s)
  8074. (let ((end (save-excursion (outline-next-heading) (point))))
  8075. (if (re-search-forward org-scheduled-time-regexp end t)
  8076. (org-time-string-to-seconds (match-string 1))
  8077. (org-float-time now))))
  8078. ((= dcst ?d)
  8079. (let ((end (save-excursion (outline-next-heading) (point))))
  8080. (if (re-search-forward org-deadline-time-regexp end t)
  8081. (org-time-string-to-seconds (match-string 1))
  8082. (org-float-time now))))
  8083. ((= dcst ?p)
  8084. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8085. (string-to-char (match-string 2))
  8086. org-default-priority))
  8087. ((= dcst ?r)
  8088. (or (org-entry-get nil property) ""))
  8089. ((= dcst ?o)
  8090. (if (looking-at org-complex-heading-regexp)
  8091. (let* ((m (match-string 2))
  8092. (s (if (member m org-done-keywords) '- '+)))
  8093. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8094. ((= dcst ?f)
  8095. (if getkey-func
  8096. (progn
  8097. (setq tmp (funcall getkey-func))
  8098. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8099. tmp)
  8100. (error "Invalid key function `%s'" getkey-func)))
  8101. (t (error "Invalid sorting type `%c'" sorting-type))))
  8102. nil
  8103. (cond
  8104. ((= dcst ?a) 'string<)
  8105. ((= dcst ?f) compare-func)
  8106. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8107. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8108. ;; Reset the clock marker if needed
  8109. (when cmstr
  8110. (save-excursion
  8111. (goto-char start)
  8112. (search-forward cmstr nil t)
  8113. (move-marker org-clock-marker (point))))
  8114. (message "Sorting entries...done")))
  8115. ;;; The orgstruct minor mode
  8116. ;; Define a minor mode which can be used in other modes in order to
  8117. ;; integrate the org-mode structure editing commands.
  8118. ;; This is really a hack, because the org-mode structure commands use
  8119. ;; keys which normally belong to the major mode. Here is how it
  8120. ;; works: The minor mode defines all the keys necessary to operate the
  8121. ;; structure commands, but wraps the commands into a function which
  8122. ;; tests if the cursor is currently at a headline or a plain list
  8123. ;; item. If that is the case, the structure command is used,
  8124. ;; temporarily setting many Org-mode variables like regular
  8125. ;; expressions for filling etc. However, when any of those keys is
  8126. ;; used at a different location, function uses `key-binding' to look
  8127. ;; up if the key has an associated command in another currently active
  8128. ;; keymap (minor modes, major mode, global), and executes that
  8129. ;; command. There might be problems if any of the keys is otherwise
  8130. ;; used as a prefix key.
  8131. (defcustom orgstruct-heading-prefix-regexp ""
  8132. "Regexp that matches the custom prefix of Org headlines in
  8133. orgstruct(++)-mode."
  8134. :group 'org
  8135. :version "24.4"
  8136. :package-version '(Org . "8.3")
  8137. :type 'regexp)
  8138. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8139. (defcustom orgstruct-setup-hook nil
  8140. "Hook run after orgstruct-mode-map is filled."
  8141. :group 'org
  8142. :version "24.4"
  8143. :package-version '(Org . "8.0")
  8144. :type 'hook)
  8145. (defvar orgstruct-initialized nil)
  8146. (defvar org-local-vars nil
  8147. "List of local variables, for use by `orgstruct-mode'.")
  8148. ;;;###autoload
  8149. (define-minor-mode orgstruct-mode
  8150. "Toggle the minor mode `orgstruct-mode'.
  8151. This mode is for using Org-mode structure commands in other
  8152. modes. The following keys behave as if Org-mode were active, if
  8153. the cursor is on a headline, or on a plain list item (both as
  8154. defined by Org-mode)."
  8155. nil " OrgStruct" (make-sparse-keymap)
  8156. (funcall (if orgstruct-mode
  8157. 'add-to-invisibility-spec
  8158. 'remove-from-invisibility-spec)
  8159. '(outline . t))
  8160. (when orgstruct-mode
  8161. (org-load-modules-maybe)
  8162. (unless orgstruct-initialized
  8163. (orgstruct-setup)
  8164. (setq orgstruct-initialized t))))
  8165. ;;;###autoload
  8166. (defun turn-on-orgstruct ()
  8167. "Unconditionally turn on `orgstruct-mode'."
  8168. (orgstruct-mode 1))
  8169. (defvar org-fb-vars nil)
  8170. (make-variable-buffer-local 'org-fb-vars)
  8171. (defun orgstruct++-mode (&optional arg)
  8172. "Toggle `orgstruct-mode', the enhanced version of it.
  8173. In addition to setting orgstruct-mode, this also exports all
  8174. indentation and autofilling variables from org-mode into the
  8175. buffer. It will also recognize item context in multiline items."
  8176. (interactive "P")
  8177. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8178. (if (< arg 1)
  8179. (progn (orgstruct-mode -1)
  8180. (mapc (lambda(v)
  8181. (org-set-local (car v)
  8182. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8183. org-fb-vars))
  8184. (orgstruct-mode 1)
  8185. (setq org-fb-vars nil)
  8186. (unless org-local-vars
  8187. (setq org-local-vars (org-get-local-variables)))
  8188. (let (var val)
  8189. (mapc
  8190. (lambda (x)
  8191. (when (string-match
  8192. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8193. (symbol-name (car x)))
  8194. (setq var (car x) val (nth 1 x))
  8195. (push (list var `(quote ,(eval var))) org-fb-vars)
  8196. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8197. org-local-vars)
  8198. (org-set-local 'orgstruct-is-++ t))))
  8199. (defvar orgstruct-is-++ nil
  8200. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8201. (make-variable-buffer-local 'orgstruct-is-++)
  8202. ;;;###autoload
  8203. (defun turn-on-orgstruct++ ()
  8204. "Unconditionally turn on `orgstruct++-mode'."
  8205. (orgstruct++-mode 1))
  8206. (defun orgstruct-error ()
  8207. "Error when there is no default binding for a structure key."
  8208. (interactive)
  8209. (funcall (if (fboundp 'user-error)
  8210. 'user-error
  8211. 'error)
  8212. "This key has no function outside structure elements"))
  8213. (defun orgstruct-setup ()
  8214. "Setup orgstruct keymap."
  8215. (dolist (cell '((org-demote . t)
  8216. (org-metaleft . t)
  8217. (org-metaright . t)
  8218. (org-promote . t)
  8219. (org-shiftmetaleft . t)
  8220. (org-shiftmetaright . t)
  8221. org-backward-element
  8222. org-backward-heading-same-level
  8223. org-ctrl-c-ret
  8224. org-ctrl-c-minus
  8225. org-ctrl-c-star
  8226. org-cycle
  8227. org-forward-heading-same-level
  8228. org-insert-heading
  8229. org-insert-heading-respect-content
  8230. org-kill-note-or-show-branches
  8231. org-mark-subtree
  8232. org-meta-return
  8233. org-metadown
  8234. org-metaup
  8235. org-narrow-to-subtree
  8236. org-promote-subtree
  8237. org-reveal
  8238. org-shiftdown
  8239. org-shiftleft
  8240. org-shiftmetadown
  8241. org-shiftmetaup
  8242. org-shiftright
  8243. org-shifttab
  8244. org-shifttab
  8245. org-shiftup
  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. show-children))
  8255. (let ((f (or (car-safe cell) cell))
  8256. (disable-when-heading-prefix (cdr-safe cell)))
  8257. (when (fboundp f)
  8258. (let ((new-bindings))
  8259. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8260. (where-is-internal f outline-mode-map)))
  8261. (push binding new-bindings)
  8262. ;; TODO use local-function-key-map
  8263. (dolist (rep '(("<tab>" . "TAB")
  8264. ("<return>" . "RET")
  8265. ("<escape>" . "ESC")
  8266. ("<delete>" . "DEL")))
  8267. (setq binding (read-kbd-macro
  8268. (let ((case-fold-search))
  8269. (replace-regexp-in-string
  8270. (regexp-quote (cdr rep))
  8271. (car rep)
  8272. (key-description binding)))))
  8273. (pushnew binding new-bindings :test 'equal)))
  8274. (dolist (binding new-bindings)
  8275. (let ((key (lookup-key orgstruct-mode-map binding)))
  8276. (when (or (not key) (numberp key))
  8277. (ignore-errors
  8278. (org-defkey orgstruct-mode-map
  8279. binding
  8280. (orgstruct-make-binding
  8281. f binding disable-when-heading-prefix))))))))))
  8282. (run-hooks 'orgstruct-setup-hook))
  8283. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8284. "Create a function for binding in the structure minor mode.
  8285. FUN is the command to call inside a table. KEY is the key that
  8286. should be checked in for a command to execute outside of tables.
  8287. Non-nil `disable-when-heading-prefix' means to disable the command
  8288. if `orgstruct-heading-prefix-regexp' is not empty."
  8289. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8290. (let ((nname name)
  8291. (i 0))
  8292. (while (fboundp (intern nname))
  8293. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8294. (setq name (intern nname)))
  8295. (eval
  8296. (let ((bindings '((org-heading-regexp
  8297. (concat "^"
  8298. orgstruct-heading-prefix-regexp
  8299. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8300. (org-outline-regexp
  8301. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8302. (org-outline-regexp-bol
  8303. (concat "^" org-outline-regexp))
  8304. (outline-regexp org-outline-regexp)
  8305. (outline-heading-end-regexp "\n")
  8306. (outline-level 'org-outline-level)
  8307. (outline-heading-alist))))
  8308. `(defun ,name (arg)
  8309. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8310. "Outside of structure, run the binding of `"
  8311. (key-description key) "'."
  8312. (when disable-when-heading-prefix
  8313. (concat
  8314. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8315. "back to the default binding due to limitations of Org's implementation of\n"
  8316. "`" (symbol-name fun) "'.")))
  8317. (interactive "p")
  8318. (let* ((disable
  8319. ,(and disable-when-heading-prefix
  8320. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8321. (fallback
  8322. (or disable
  8323. (not
  8324. (let* ,bindings
  8325. (org-context-p 'headline 'item
  8326. ,(when (memq fun
  8327. '(org-insert-heading
  8328. org-insert-heading-respect-content
  8329. org-meta-return))
  8330. '(when orgstruct-is-++
  8331. 'item-body))))))))
  8332. (if fallback
  8333. (let* ((orgstruct-mode)
  8334. (binding
  8335. (let ((key ,key))
  8336. (catch 'exit
  8337. (dolist
  8338. (rep
  8339. '(nil
  8340. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8341. ("<\\([^>]*\\)return>" . "\\1RET")
  8342. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8343. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8344. nil)
  8345. (when rep
  8346. (setq key (read-kbd-macro
  8347. (let ((case-fold-search))
  8348. (replace-regexp-in-string
  8349. (car rep)
  8350. (cdr rep)
  8351. (key-description key))))))
  8352. (when (key-binding key)
  8353. (throw 'exit (key-binding key))))))))
  8354. (if (keymapp binding)
  8355. (org-set-transient-map binding)
  8356. (let ((func (or binding
  8357. (unless disable
  8358. 'orgstruct-error))))
  8359. (when func
  8360. (call-interactively func)))))
  8361. (org-run-like-in-org-mode
  8362. (lambda ()
  8363. (interactive)
  8364. (let* ,bindings
  8365. (call-interactively ',fun)))))))))
  8366. name))
  8367. (defun org-contextualize-keys (alist contexts)
  8368. "Return valid elements in ALIST depending on CONTEXTS.
  8369. `org-agenda-custom-commands' or `org-capture-templates' are the
  8370. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8371. or `org-capture-templates-contexts' are the associated contexts
  8372. definitions."
  8373. (let ((contexts
  8374. ;; normalize contexts
  8375. (mapcar
  8376. (lambda(c) (cond ((listp (cadr c))
  8377. (list (car c) (car c) (cadr c)))
  8378. ((string= "" (cadr c))
  8379. (list (car c) (car c) (caddr c)))
  8380. (t c)))
  8381. contexts))
  8382. (a alist) r s)
  8383. ;; loop over all commands or templates
  8384. (dolist (c a)
  8385. (let (vrules repl)
  8386. (cond
  8387. ((not (assoc (car c) contexts))
  8388. (push c r))
  8389. ((and (assoc (car c) contexts)
  8390. (setq vrules (org-contextualize-validate-key
  8391. (car c) contexts)))
  8392. (mapc (lambda (vr)
  8393. (when (not (equal (car vr) (cadr vr)))
  8394. (setq repl vr)))
  8395. vrules)
  8396. (if (not repl) (push c r)
  8397. (push (cadr repl) s)
  8398. (push
  8399. (cons (car c)
  8400. (cdr (or (assoc (cadr repl) alist)
  8401. (error "Undefined key `%s' as contextual replacement for `%s'"
  8402. (cadr repl) (car c)))))
  8403. r))))))
  8404. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8405. (delq
  8406. nil
  8407. (delete-dups
  8408. (mapcar (lambda (x)
  8409. (let ((tpl (car x)))
  8410. (when (not (delq
  8411. nil
  8412. (mapcar (lambda (y)
  8413. (equal y tpl))
  8414. s)))
  8415. x)))
  8416. (reverse r))))))
  8417. (defun org-contextualize-validate-key (key contexts)
  8418. "Check CONTEXTS for agenda or capture KEY."
  8419. (let (rr res)
  8420. (dolist (r contexts)
  8421. (mapc
  8422. (lambda (rr)
  8423. (when
  8424. (and (equal key (car r))
  8425. (if (functionp rr) (funcall rr)
  8426. (or (and (eq (car rr) 'in-file)
  8427. (buffer-file-name)
  8428. (string-match (cdr rr) (buffer-file-name)))
  8429. (and (eq (car rr) 'in-mode)
  8430. (string-match (cdr rr) (symbol-name major-mode)))
  8431. (and (eq (car rr) 'in-buffer)
  8432. (string-match (cdr rr) (buffer-name)))
  8433. (when (and (eq (car rr) 'not-in-file)
  8434. (buffer-file-name))
  8435. (not (string-match (cdr rr) (buffer-file-name))))
  8436. (when (eq (car rr) 'not-in-mode)
  8437. (not (string-match (cdr rr) (symbol-name major-mode))))
  8438. (when (eq (car rr) 'not-in-buffer)
  8439. (not (string-match (cdr rr) (buffer-name)))))))
  8440. (push r res)))
  8441. (car (last r))))
  8442. (delete-dups (delq nil res))))
  8443. (defun org-context-p (&rest contexts)
  8444. "Check if local context is any of CONTEXTS.
  8445. Possible values in the list of contexts are `table', `headline', and `item'."
  8446. (let ((pos (point)))
  8447. (goto-char (point-at-bol))
  8448. (prog1 (or (and (memq 'table contexts)
  8449. (looking-at "[ \t]*|"))
  8450. (and (memq 'headline contexts)
  8451. (looking-at org-outline-regexp))
  8452. (and (memq 'item contexts)
  8453. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8454. (and (memq 'item-body contexts)
  8455. (org-in-item-p)))
  8456. (goto-char pos))))
  8457. (defun org-get-local-variables ()
  8458. "Return a list of all local variables in an Org mode buffer."
  8459. (let (varlist)
  8460. (with-current-buffer (get-buffer-create "*Org tmp*")
  8461. (erase-buffer)
  8462. (org-mode)
  8463. (setq varlist (buffer-local-variables)))
  8464. (kill-buffer "*Org tmp*")
  8465. (delq nil
  8466. (mapcar
  8467. (lambda (x)
  8468. (setq x
  8469. (if (symbolp x)
  8470. (list x)
  8471. (list (car x) (cdr x))))
  8472. (if (and (not (get (car x) 'org-state))
  8473. (string-match
  8474. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8475. (symbol-name (car x))))
  8476. x nil))
  8477. varlist))))
  8478. (defun org-clone-local-variables (from-buffer &optional regexp)
  8479. "Clone local variables from FROM-BUFFER.
  8480. Optional argument REGEXP selects variables to clone."
  8481. (mapc
  8482. (lambda (pair)
  8483. (and (symbolp (car pair))
  8484. (or (null regexp)
  8485. (string-match regexp (symbol-name (car pair))))
  8486. (set (make-local-variable (car pair))
  8487. (cdr pair))))
  8488. (buffer-local-variables from-buffer)))
  8489. ;;;###autoload
  8490. (defun org-run-like-in-org-mode (cmd)
  8491. "Run a command, pretending that the current buffer is in Org-mode.
  8492. This will temporarily bind local variables that are typically bound in
  8493. Org-mode to the values they have in Org-mode, and then interactively
  8494. call CMD."
  8495. (org-load-modules-maybe)
  8496. (unless org-local-vars
  8497. (setq org-local-vars (org-get-local-variables)))
  8498. (let (binds)
  8499. (dolist (var org-local-vars)
  8500. (when (or (not (boundp (car var)))
  8501. (eq (symbol-value (car var))
  8502. (default-value (car var))))
  8503. (push (list (car var) `(quote ,(cadr var))) binds)))
  8504. (eval `(let ,binds
  8505. (call-interactively (quote ,cmd))))))
  8506. (defun org-get-category (&optional pos force-refresh)
  8507. "Get the category applying to position POS."
  8508. (save-match-data
  8509. (if force-refresh (org-refresh-category-properties))
  8510. (let ((pos (or pos (point))))
  8511. (or (get-text-property pos 'org-category)
  8512. (progn (org-refresh-category-properties)
  8513. (get-text-property pos 'org-category))))))
  8514. ;;; Refresh properties
  8515. (defun org-refresh-properties (dprop tprop)
  8516. "Refresh buffer text properties.
  8517. DPROP is the drawer property and TPROP is either the
  8518. corresponding text property to set, or an alist with each element
  8519. being a text property (as a symbol) and a function to apply to
  8520. the value of the drawer property."
  8521. (let ((case-fold-search t)
  8522. (inhibit-read-only t))
  8523. (org-with-silent-modifications
  8524. (save-excursion
  8525. (save-restriction
  8526. (widen)
  8527. (goto-char (point-min))
  8528. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8529. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8530. (defun org-refresh-property (tprop p)
  8531. "Refresh the buffer text property TPROP from the drawer property P.
  8532. The refresh happens only for the current tree (not subtree)."
  8533. (unless (org-before-first-heading-p)
  8534. (save-excursion
  8535. (org-back-to-heading t)
  8536. (if (symbolp tprop)
  8537. ;; TPROP is a text property symbol
  8538. (put-text-property
  8539. (point) (or (outline-next-heading) (point-max)) tprop p)
  8540. ;; TPROP is an alist with (properties . function) elements
  8541. (dolist (al tprop)
  8542. (save-excursion
  8543. (put-text-property
  8544. (line-beginning-position) (or (outline-next-heading) (point-max))
  8545. (car al)
  8546. (funcall (cdr al) p))))))))
  8547. (defun org-refresh-category-properties ()
  8548. "Refresh category text properties in the buffer."
  8549. (let ((case-fold-search t)
  8550. (inhibit-read-only t)
  8551. (default-category
  8552. (cond ((null org-category)
  8553. (if buffer-file-name
  8554. (file-name-sans-extension
  8555. (file-name-nondirectory buffer-file-name))
  8556. "???"))
  8557. ((symbolp org-category) (symbol-name org-category))
  8558. (t org-category))))
  8559. (org-with-silent-modifications
  8560. (org-with-wide-buffer
  8561. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8562. ;; This is the default category for the buffer. If none is
  8563. ;; found, fall-back to `org-category' or buffer file name.
  8564. (put-text-property
  8565. (point-min) (point-max)
  8566. 'org-category
  8567. (catch 'buffer-category
  8568. (goto-char (point-max))
  8569. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8570. (let ((element (org-element-at-point)))
  8571. (when (eq (org-element-type element) 'keyword)
  8572. (throw 'buffer-category
  8573. (org-element-property :value element)))))
  8574. default-category))
  8575. ;; Set sub-tree specific categories.
  8576. (goto-char (point-min))
  8577. (let ((regexp (org-re-property "CATEGORY")))
  8578. (while (re-search-forward regexp nil t)
  8579. (let ((value (org-match-string-no-properties 3)))
  8580. (when (org-at-property-p)
  8581. (put-text-property
  8582. (save-excursion (org-back-to-heading t) (point))
  8583. (save-excursion (org-end-of-subtree t t) (point))
  8584. 'org-category
  8585. value)))))))))
  8586. (defun org-refresh-stats-properties ()
  8587. "Refresh stats text properties in the buffer."
  8588. (let (stats)
  8589. (org-with-silent-modifications
  8590. (save-excursion
  8591. (save-restriction
  8592. (widen)
  8593. (goto-char (point-min))
  8594. (while (re-search-forward
  8595. (concat org-outline-regexp-bol ".*"
  8596. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8597. nil t)
  8598. (setq stats (cond ((equal (match-string 3) "0") 0)
  8599. ((match-string 2)
  8600. (/ (* (string-to-number (match-string 2)) 100)
  8601. (string-to-number (match-string 3))))
  8602. (t (string-to-number (match-string 1)))))
  8603. (org-back-to-heading t)
  8604. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8605. 'org-stats stats)))))))
  8606. (defun org-refresh-effort-properties ()
  8607. "Refresh effort properties"
  8608. (org-refresh-properties
  8609. org-effort-property
  8610. '((effort . identity)
  8611. (effort-minutes . org-duration-string-to-minutes))))
  8612. ;;;; Link Stuff
  8613. ;;; Link abbreviations
  8614. (defun org-link-expand-abbrev (link)
  8615. "Apply replacements as defined in `org-link-abbrev-alist'."
  8616. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8617. (let* ((key (match-string 1 link))
  8618. (as (or (assoc key org-link-abbrev-alist-local)
  8619. (assoc key org-link-abbrev-alist)))
  8620. (tag (and (match-end 2) (match-string 3 link)))
  8621. rpl)
  8622. (if (not as)
  8623. link
  8624. (setq rpl (cdr as))
  8625. (cond
  8626. ((symbolp rpl) (funcall rpl tag))
  8627. ((string-match "%(\\([^)]+\\))" rpl)
  8628. (replace-match
  8629. (save-match-data
  8630. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8631. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8632. ((string-match "%h" rpl)
  8633. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8634. (t (concat rpl tag)))))
  8635. link))
  8636. ;;; Storing and inserting links
  8637. (defvar org-insert-link-history nil
  8638. "Minibuffer history for links inserted with `org-insert-link'.")
  8639. (defvar org-stored-links nil
  8640. "Contains the links stored with `org-store-link'.")
  8641. (defvar org-store-link-plist nil
  8642. "Plist with info about the most recently link created with `org-store-link'.")
  8643. (defvar org-link-protocols nil
  8644. "Link protocols added to Org-mode using `org-add-link-type'.")
  8645. (defvar org-store-link-functions nil
  8646. "List of functions that are called to create and store a link.
  8647. Each function will be called in turn until one returns a non-nil
  8648. value. Each function should check if it is responsible for creating
  8649. this link (for example by looking at the major mode).
  8650. If not, it must exit and return nil.
  8651. If yes, it should return a non-nil value after a calling
  8652. `org-store-link-props' with a list of properties and values.
  8653. Special properties are:
  8654. :type The link prefix, like \"http\". This must be given.
  8655. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8656. This is obligatory as well.
  8657. :description Optional default description for the second pair
  8658. of brackets in an Org-mode link. The user can still change
  8659. this when inserting this link into an Org-mode buffer.
  8660. In addition to these, any additional properties can be specified
  8661. and then used in capture templates.")
  8662. (defun org-add-link-type (type &optional follow export)
  8663. "Add TYPE to the list of `org-link-types'.
  8664. Re-compute all regular expressions depending on `org-link-types'
  8665. FOLLOW and EXPORT are two functions.
  8666. FOLLOW should take the link path as the single argument and do whatever
  8667. is necessary to follow the link, for example find a file or display
  8668. a mail message.
  8669. EXPORT should format the link path for export to one of the export formats.
  8670. It should be a function accepting three arguments:
  8671. path the path of the link, the text after the prefix (like \"http:\")
  8672. desc the description of the link, if any
  8673. format the export format, a symbol like `html' or `latex' or `ascii'.
  8674. The function may use the FORMAT information to return different values
  8675. depending on the format. The return value will be put literally into
  8676. the exported file. If the return value is nil, this means Org should
  8677. do what it normally does with links which do not have EXPORT defined.
  8678. Org mode has a built-in default for exporting links. If you are happy with
  8679. this default, there is no need to define an export function for the link
  8680. type. For a simple example of an export function, see `org-bbdb.el'."
  8681. (add-to-list 'org-link-types type t)
  8682. (org-make-link-regexps)
  8683. (org-element-update-syntax)
  8684. (if (assoc type org-link-protocols)
  8685. (setcdr (assoc type org-link-protocols) (list follow export))
  8686. (push (list type follow export) org-link-protocols)))
  8687. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8688. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8689. ;;;###autoload
  8690. (defun org-store-link (arg)
  8691. "\\<org-mode-map>Store an org-link to the current location.
  8692. This link is added to `org-stored-links' and can later be inserted
  8693. into an Org buffer with \\[org-insert-link].
  8694. For some link types, a prefix ARG is interpreted.
  8695. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8696. For file links, ARG negates `org-context-in-file-links'.
  8697. A double prefix ARG force skipping storing functions that are not
  8698. part of Org's core.
  8699. A triple prefix ARG force storing a link for each line in the
  8700. active region."
  8701. (interactive "P")
  8702. (org-load-modules-maybe)
  8703. (if (and (equal arg '(64)) (org-region-active-p))
  8704. (save-excursion
  8705. (let ((end (region-end)))
  8706. (goto-char (region-beginning))
  8707. (set-mark (point))
  8708. (while (< (point-at-eol) end)
  8709. (move-end-of-line 1) (activate-mark)
  8710. (let (current-prefix-arg)
  8711. (call-interactively 'org-store-link))
  8712. (move-beginning-of-line 2)
  8713. (set-mark (point)))))
  8714. (org-with-limited-levels
  8715. (setq org-store-link-plist nil)
  8716. (let (link cpltxt desc description search
  8717. txt custom-id agenda-link sfuns sfunsn)
  8718. (cond
  8719. ;; Store a link using an external link type
  8720. ((and (not (equal arg '(16)))
  8721. (setq sfuns
  8722. (delq
  8723. nil (mapcar (lambda (f)
  8724. (let (fs) (if (funcall f) (push f fs))))
  8725. org-store-link-functions))
  8726. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8727. (or (and (cdr sfuns)
  8728. (funcall (intern
  8729. (completing-read
  8730. "Which function for creating the link? "
  8731. sfunsn nil t (car sfunsn)))))
  8732. (funcall (caar sfuns)))
  8733. (setq link (plist-get org-store-link-plist :link)
  8734. desc (or (plist-get org-store-link-plist
  8735. :description)
  8736. link))))
  8737. ;; Store a link from a source code buffer.
  8738. ((org-src-edit-buffer-p)
  8739. (cond
  8740. ((save-excursion
  8741. (beginning-of-line)
  8742. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8743. "[ \t]*$")))
  8744. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8745. ((org-called-interactively-p 'any)
  8746. (let (label)
  8747. (while (or (not label)
  8748. (org-with-wide-buffer
  8749. (goto-char (point-min))
  8750. (re-search-forward
  8751. (regexp-quote (format org-coderef-label-format label))
  8752. nil t)))
  8753. (when label (message "Label exists already") (sit-for 2))
  8754. (setq label (read-string "Code line label: " label)))
  8755. (end-of-line)
  8756. (setq link (format org-coderef-label-format label))
  8757. (let ((gc (- 79 (length link))))
  8758. (if (< (current-column) gc) (org-move-to-column gc t)
  8759. (insert " ")))
  8760. (insert link)
  8761. (setq link (concat "(" label ")") desc nil)))
  8762. (t (setq link nil))))
  8763. ;; We are in the agenda, link to referenced location
  8764. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8765. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8766. (get-text-property (point) 'org-marker))))
  8767. (when m
  8768. (org-with-point-at m
  8769. (setq agenda-link
  8770. (if (org-called-interactively-p 'any)
  8771. (call-interactively 'org-store-link)
  8772. (org-store-link nil)))))))
  8773. ((eq major-mode 'calendar-mode)
  8774. (let ((cd (calendar-cursor-to-date)))
  8775. (setq link
  8776. (format-time-string
  8777. (car org-time-stamp-formats)
  8778. (apply 'encode-time
  8779. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8780. nil nil nil))))
  8781. (org-store-link-props :type "calendar" :date cd)))
  8782. ((eq major-mode 'help-mode)
  8783. (setq link (concat "help:" (save-excursion
  8784. (goto-char (point-min))
  8785. (looking-at "^[^ ]+")
  8786. (match-string 0))))
  8787. (org-store-link-props :type "help"))
  8788. ((eq major-mode 'w3-mode)
  8789. (setq cpltxt (if (and (buffer-name)
  8790. (not (string-match "Untitled" (buffer-name))))
  8791. (buffer-name)
  8792. (url-view-url t))
  8793. link (url-view-url t))
  8794. (org-store-link-props :type "w3" :url (url-view-url t)))
  8795. ((eq major-mode 'image-mode)
  8796. (setq cpltxt (concat "file:"
  8797. (abbreviate-file-name buffer-file-name))
  8798. link cpltxt)
  8799. (org-store-link-props :type "image" :file buffer-file-name))
  8800. ;; In dired, store a link to the file of the current line
  8801. ((derived-mode-p 'dired-mode)
  8802. (let ((file (dired-get-filename nil t)))
  8803. (setq file (if file
  8804. (abbreviate-file-name
  8805. (expand-file-name (dired-get-filename nil t)))
  8806. ;; otherwise, no file so use current directory.
  8807. default-directory))
  8808. (setq cpltxt (concat "file:" file)
  8809. link cpltxt)))
  8810. ((setq search (run-hook-with-args-until-success
  8811. 'org-create-file-search-functions))
  8812. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8813. "::" search))
  8814. (setq cpltxt (or description link)))
  8815. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8816. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8817. (cond
  8818. ;; Store a link using the target at point
  8819. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8820. (setq cpltxt
  8821. (concat "file:"
  8822. (abbreviate-file-name
  8823. (buffer-file-name (buffer-base-buffer)))
  8824. "::" (match-string 1))
  8825. link cpltxt))
  8826. ((and (featurep 'org-id)
  8827. (or (eq org-id-link-to-org-use-id t)
  8828. (and (org-called-interactively-p 'any)
  8829. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8830. (and (eq org-id-link-to-org-use-id
  8831. 'create-if-interactive-and-no-custom-id)
  8832. (not custom-id))))
  8833. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8834. ;; Store a link using the ID at point
  8835. (setq link (condition-case nil
  8836. (prog1 (org-id-store-link)
  8837. (setq desc (or (plist-get org-store-link-plist
  8838. :description)
  8839. "")))
  8840. (error
  8841. ;; Probably before first headline, link only to file
  8842. (concat "file:"
  8843. (abbreviate-file-name
  8844. (buffer-file-name (buffer-base-buffer))))))))
  8845. (t
  8846. ;; Just link to current headline
  8847. (setq cpltxt (concat "file:"
  8848. (abbreviate-file-name
  8849. (buffer-file-name (buffer-base-buffer)))))
  8850. ;; Add a context search string
  8851. (when (org-xor org-context-in-file-links arg)
  8852. (let* ((ee (org-element-at-point))
  8853. (et (org-element-type ee))
  8854. (ev (plist-get (cadr ee) :value))
  8855. (ek (plist-get (cadr ee) :key))
  8856. (eok (and (stringp ek) (string-match "name" ek))))
  8857. (setq txt (cond
  8858. ((org-at-heading-p) nil)
  8859. ((and (eq et 'keyword) eok) ev)
  8860. ((org-region-active-p)
  8861. (buffer-substring (region-beginning) (region-end)))))
  8862. (when (or (null txt) (string-match "\\S-" txt))
  8863. (setq cpltxt
  8864. (concat cpltxt "::"
  8865. (condition-case nil
  8866. (org-make-org-heading-search-string txt)
  8867. (error "")))
  8868. desc (or (and (eq et 'keyword) eok ev)
  8869. (nth 4 (ignore-errors (org-heading-components)))
  8870. "NONE")))))
  8871. (if (string-match "::\\'" cpltxt)
  8872. (setq cpltxt (substring cpltxt 0 -2)))
  8873. (setq link cpltxt))))
  8874. ((buffer-file-name (buffer-base-buffer))
  8875. ;; Just link to this file here.
  8876. (setq cpltxt (concat "file:"
  8877. (abbreviate-file-name
  8878. (buffer-file-name (buffer-base-buffer)))))
  8879. ;; Add a context string.
  8880. (when (org-xor org-context-in-file-links arg)
  8881. (setq txt (if (org-region-active-p)
  8882. (buffer-substring (region-beginning) (region-end))
  8883. (buffer-substring (point-at-bol) (point-at-eol))))
  8884. ;; Only use search option if there is some text.
  8885. (when (string-match "\\S-" txt)
  8886. (setq cpltxt
  8887. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8888. desc "NONE")))
  8889. (setq link cpltxt))
  8890. ((org-called-interactively-p 'interactive)
  8891. (user-error "No method for storing a link from this buffer"))
  8892. (t (setq link nil)))
  8893. ;; We're done setting link and desc, clean up
  8894. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8895. (setq link (or link cpltxt)
  8896. desc (or desc cpltxt))
  8897. (cond ((equal desc "NONE") (setq desc nil))
  8898. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8899. (let ((d0 (match-string 3 desc))
  8900. (p0 (match-string 5 desc)))
  8901. (setq desc
  8902. (replace-regexp-in-string
  8903. org-bracket-link-regexp
  8904. (concat (or p0 d0)
  8905. (if (equal (length (match-string 0 desc))
  8906. (length desc)) "*" "")) desc)))))
  8907. ;; Return the link
  8908. (if (not (and (or (org-called-interactively-p 'any)
  8909. executing-kbd-macro)
  8910. link))
  8911. (or agenda-link (and link (org-make-link-string link desc)))
  8912. (push (list link desc) org-stored-links)
  8913. (message "Stored: %s" (or desc link))
  8914. (when custom-id
  8915. (setq link (concat "file:" (abbreviate-file-name
  8916. (buffer-file-name)) "::#" custom-id))
  8917. (push (list link desc) org-stored-links))
  8918. (car org-stored-links))))))
  8919. (defun org-store-link-props (&rest plist)
  8920. "Store link properties, extract names and addresses."
  8921. (let (x adr)
  8922. (when (setq x (plist-get plist :from))
  8923. (setq adr (mail-extract-address-components x))
  8924. (setq plist (plist-put plist :fromname (car adr)))
  8925. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8926. (when (setq x (plist-get plist :to))
  8927. (setq adr (mail-extract-address-components x))
  8928. (setq plist (plist-put plist :toname (car adr)))
  8929. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8930. (let ((from (plist-get plist :from))
  8931. (to (plist-get plist :to)))
  8932. (when (and from to org-from-is-user-regexp)
  8933. (setq plist
  8934. (plist-put plist :fromto
  8935. (if (string-match org-from-is-user-regexp from)
  8936. (concat "to %t")
  8937. (concat "from %f"))))))
  8938. (setq org-store-link-plist plist))
  8939. (defun org-add-link-props (&rest plist)
  8940. "Add these properties to the link property list."
  8941. (let (key value)
  8942. (while plist
  8943. (setq key (pop plist) value (pop plist))
  8944. (setq org-store-link-plist
  8945. (plist-put org-store-link-plist key value)))))
  8946. (defun org-email-link-description (&optional fmt)
  8947. "Return the description part of an email link.
  8948. This takes information from `org-store-link-plist' and formats it
  8949. according to FMT (default from `org-email-link-description-format')."
  8950. (setq fmt (or fmt org-email-link-description-format))
  8951. (let* ((p org-store-link-plist)
  8952. (to (plist-get p :toaddress))
  8953. (from (plist-get p :fromaddress))
  8954. (table
  8955. (list
  8956. (cons "%c" (plist-get p :fromto))
  8957. (cons "%F" (plist-get p :from))
  8958. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8959. (cons "%T" (plist-get p :to))
  8960. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8961. (cons "%s" (plist-get p :subject))
  8962. (cons "%d" (plist-get p :date))
  8963. (cons "%m" (plist-get p :message-id)))))
  8964. (when (string-match "%c" fmt)
  8965. ;; Check if the user wrote this message
  8966. (if (and org-from-is-user-regexp from to
  8967. (save-match-data (string-match org-from-is-user-regexp from)))
  8968. (setq fmt (replace-match "to %t" t t fmt))
  8969. (setq fmt (replace-match "from %f" t t fmt))))
  8970. (org-replace-escapes fmt table)))
  8971. (defun org-make-org-heading-search-string (&optional string)
  8972. "Make search string for the current headline or STRING."
  8973. (let ((s (or string
  8974. (and (derived-mode-p 'org-mode)
  8975. (save-excursion
  8976. (org-back-to-heading t)
  8977. (org-element-property :raw-value (org-element-at-point))))))
  8978. (lines org-context-in-file-links))
  8979. (or string (setq s (concat "*" s))) ; Add * for headlines
  8980. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8981. (when (and string (integerp lines) (> lines 0))
  8982. (let ((slines (org-split-string s "\n")))
  8983. (when (< lines (length slines))
  8984. (setq s (mapconcat
  8985. 'identity
  8986. (reverse (nthcdr (- (length slines) lines)
  8987. (reverse slines))) "\n")))))
  8988. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8989. (defun org-make-link-string (link &optional description)
  8990. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8991. (unless (org-string-nw-p link) (error "Empty link"))
  8992. (let ((uri (cond ((string-match org-link-types-re link)
  8993. (concat (match-string 1 link)
  8994. (org-link-escape (substring link (match-end 1)))))
  8995. ;; For readability, url-encode internal links only
  8996. ;; when absolutely needed (i.e, when they contain
  8997. ;; square brackets). File links however, are
  8998. ;; encoded since, e.g., spaces are significant.
  8999. ((or (file-name-absolute-p link)
  9000. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9001. (org-link-escape link))
  9002. (t link)))
  9003. (description
  9004. (and (org-string-nw-p description)
  9005. ;; Remove brackets from description, as they are fatal.
  9006. (replace-regexp-in-string
  9007. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9008. (org-trim description)))))
  9009. (format "[[%s]%s]"
  9010. uri
  9011. (if description (format "[%s]" description) ""))))
  9012. (defconst org-link-escape-chars
  9013. ;;%20 %5B %5D %25
  9014. '(?\s ?\[ ?\] ?%)
  9015. "List of characters that should be escaped in a link when stored to Org.
  9016. This is the list that is used for internal purposes.")
  9017. (defconst org-link-escape-chars-browser
  9018. ;;%20 %22
  9019. '(?\s ?\")
  9020. "List of characters to be escaped before handing over to the browser.
  9021. If you consider using this constant then you probably want to use
  9022. the function `org-link-escape-browser' instead. See there why
  9023. this constant is a candidate to be removed once Org drops support
  9024. for Emacs 24.1 and 24.2.")
  9025. (defun org-link-escape (text &optional table merge)
  9026. "Return percent escaped representation of TEXT.
  9027. TEXT is a string with the text to escape.
  9028. Optional argument TABLE is a list with characters that should be
  9029. escaped. When nil, `org-link-escape-chars' is used.
  9030. If optional argument MERGE is set, merge TABLE into
  9031. `org-link-escape-chars'."
  9032. (let ((characters-to-encode
  9033. (cond ((null table) org-link-escape-chars)
  9034. (merge (append org-link-escape-chars table))
  9035. (t table))))
  9036. (mapconcat
  9037. (lambda (c)
  9038. (if (or (memq c characters-to-encode)
  9039. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9040. (mapconcat (lambda (e) (format "%%%.2X" e))
  9041. (or (encode-coding-char c 'utf-8)
  9042. (error "Unable to percent escape character: %c" c))
  9043. "")
  9044. (char-to-string c)))
  9045. text "")))
  9046. (defun org-link-escape-browser (text)
  9047. "Escape some characters before handing over to the browser.
  9048. This function is a candidate to be removed together with the
  9049. constant `org-link-escape-chars-browser' once Org drops support
  9050. for Emacs 24.1 and 24.2. All calls to this function will have to
  9051. be replaced with `url-encode-url' which is available since Emacs
  9052. 24.3.1."
  9053. ;; Example with the Org link
  9054. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9055. ;; to open the browser with +subject:"Release 8.2" filled into the
  9056. ;; query field: In this case the variable TEXT contains the
  9057. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9058. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9059. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9060. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9061. (if (fboundp 'url-encode-url)
  9062. (url-encode-url text)
  9063. (if (org-string-match-p
  9064. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9065. text)
  9066. (org-link-escape text org-link-escape-chars-browser)
  9067. text)))
  9068. (defun org-link-unescape (str)
  9069. "Unhex hexified Unicode parts in string STR.
  9070. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9071. reciprocal of `org-link-escape', which see."
  9072. (if (org-string-nw-p str)
  9073. (replace-regexp-in-string
  9074. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9075. str))
  9076. (defun org-link-unescape-compound (hex)
  9077. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9078. Note: this function also decodes single byte encodings like
  9079. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9080. (save-match-data
  9081. (let* ((bytes (cdr (split-string hex "%")))
  9082. (ret "")
  9083. (eat 0)
  9084. (sum 0))
  9085. (while bytes
  9086. (let* ((val (string-to-number (pop bytes) 16))
  9087. (shift-xor
  9088. (if (= 0 eat)
  9089. (cond
  9090. ((>= val 252) (cons 6 252))
  9091. ((>= val 248) (cons 5 248))
  9092. ((>= val 240) (cons 4 240))
  9093. ((>= val 224) (cons 3 224))
  9094. ((>= val 192) (cons 2 192))
  9095. (t (cons 0 0)))
  9096. (cons 6 128))))
  9097. (if (>= val 192) (setq eat (car shift-xor)))
  9098. (setq val (logxor val (cdr shift-xor)))
  9099. (setq sum (+ (lsh sum (car shift-xor)) val))
  9100. (if (> eat 0) (setq eat (- eat 1)))
  9101. (cond
  9102. ((= 0 eat) ;multi byte
  9103. (setq ret (concat ret (org-char-to-string sum)))
  9104. (setq sum 0))
  9105. ((not bytes) ; single byte(s)
  9106. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9107. ret)))
  9108. (defun org-link-unescape-single-byte-sequence (hex)
  9109. "Unhexify hex-encoded single byte character sequences."
  9110. (mapconcat (lambda (byte)
  9111. (char-to-string (string-to-number byte 16)))
  9112. (cdr (split-string hex "%")) ""))
  9113. (defun org-xor (a b)
  9114. "Exclusive or."
  9115. (if a (not b) b))
  9116. (defun org-fixup-message-id-for-http (s)
  9117. "Replace special characters in a message id, so it can be used in an http query."
  9118. (when (string-match "%" s)
  9119. (setq s (mapconcat (lambda (c)
  9120. (if (eq c ?%)
  9121. "%25"
  9122. (char-to-string c)))
  9123. s "")))
  9124. (while (string-match "<" s)
  9125. (setq s (replace-match "%3C" t t s)))
  9126. (while (string-match ">" s)
  9127. (setq s (replace-match "%3E" t t s)))
  9128. (while (string-match "@" s)
  9129. (setq s (replace-match "%40" t t s)))
  9130. s)
  9131. (defun org-link-prettify (link)
  9132. "Return a human-readable representation of LINK.
  9133. The car of LINK must be a raw link.
  9134. The cdr of LINK must be either a link description or nil."
  9135. (let ((desc (or (cadr link) "<no description>")))
  9136. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9137. "<" (car link) ">")))
  9138. ;;;###autoload
  9139. (defun org-insert-link-global ()
  9140. "Insert a link like Org-mode does.
  9141. This command can be called in any mode to insert a link in Org-mode syntax."
  9142. (interactive)
  9143. (org-load-modules-maybe)
  9144. (org-run-like-in-org-mode 'org-insert-link))
  9145. (defun org-insert-all-links (arg &optional pre post)
  9146. "Insert all links in `org-stored-links'.
  9147. When a universal prefix, do not delete the links from `org-stored-links'.
  9148. When `ARG' is a number, insert the last N link(s).
  9149. `PRE' and `POST' are optional arguments to define a string to
  9150. prepend or to append."
  9151. (interactive "P")
  9152. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9153. (links (copy-seq org-stored-links))
  9154. (pr (or pre "- "))
  9155. (po (or post "\n"))
  9156. (cnt 1) l)
  9157. (if (null org-stored-links)
  9158. (message "No link to insert")
  9159. (while (and (or (listp arg) (>= arg cnt))
  9160. (setq l (if (listp arg)
  9161. (pop links)
  9162. (pop org-stored-links))))
  9163. (setq cnt (1+ cnt))
  9164. (insert pr)
  9165. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9166. (insert po)))))
  9167. (defun org-insert-last-stored-link (arg)
  9168. "Insert the last link stored in `org-stored-links'."
  9169. (interactive "p")
  9170. (org-insert-all-links arg "" "\n"))
  9171. (defun org-link-fontify-links-to-this-file ()
  9172. "Fontify links to the current file in `org-stored-links'."
  9173. (let ((f (buffer-file-name)) a b)
  9174. (setq a (mapcar (lambda(l)
  9175. (let ((ll (car l)))
  9176. (when (and (string-match "^file:\\(.+\\)::" ll)
  9177. (equal f (expand-file-name (match-string 1 ll))))
  9178. ll)))
  9179. org-stored-links))
  9180. (when (featurep 'org-id)
  9181. (setq b (mapcar (lambda(l)
  9182. (let ((ll (car l)))
  9183. (when (and (string-match "^id:\\(.+\\)$" ll)
  9184. (equal f (expand-file-name
  9185. (or (org-id-find-id-file
  9186. (match-string 1 ll)) ""))))
  9187. ll)))
  9188. org-stored-links)))
  9189. (mapcar (lambda(l)
  9190. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9191. (delq nil (append a b)))))
  9192. (defvar org-link-links-in-this-file nil)
  9193. (defun org-insert-link (&optional complete-file link-location default-description)
  9194. "Insert a link. At the prompt, enter the link.
  9195. Completion can be used to insert any of the link protocol prefixes like
  9196. http or ftp in use.
  9197. The history can be used to select a link previously stored with
  9198. `org-store-link'. When the empty string is entered (i.e. if you just
  9199. press RET at the prompt), the link defaults to the most recently
  9200. stored link. As SPC triggers completion in the minibuffer, you need to
  9201. use M-SPC or C-q SPC to force the insertion of a space character.
  9202. You will also be prompted for a description, and if one is given, it will
  9203. be displayed in the buffer instead of the link.
  9204. If there is already a link at point, this command will allow you to edit link
  9205. and description parts.
  9206. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9207. be selected using completion. The path to the file will be relative to the
  9208. current directory if the file is in the current directory or a subdirectory.
  9209. Otherwise, the link will be the absolute path as completed in the minibuffer
  9210. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9211. option `org-link-file-path-type'.
  9212. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9213. the current directory or below.
  9214. With three \\[universal-argument] prefixes, negate the meaning of
  9215. `org-keep-stored-link-after-insertion'.
  9216. If `org-make-link-description-function' is non-nil, this function will be
  9217. called with the link target, and the result will be the default
  9218. link description.
  9219. If the LINK-LOCATION parameter is non-nil, this value will be
  9220. used as the link location instead of reading one interactively.
  9221. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9222. be used as the default description."
  9223. (interactive "P")
  9224. (let* ((wcf (current-window-configuration))
  9225. (origbuf (current-buffer))
  9226. (region (if (org-region-active-p)
  9227. (buffer-substring (region-beginning) (region-end))))
  9228. (remove (and region (list (region-beginning) (region-end))))
  9229. (desc region)
  9230. tmphist ; byte-compile incorrectly complains about this
  9231. (link link-location)
  9232. (abbrevs org-link-abbrev-alist-local)
  9233. entry file all-prefixes auto-desc)
  9234. (cond
  9235. (link-location) ; specified by arg, just use it.
  9236. ((org-in-regexp org-bracket-link-regexp 1)
  9237. ;; We do have a link at point, and we are going to edit it.
  9238. (setq remove (list (match-beginning 0) (match-end 0)))
  9239. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9240. (setq link (read-string "Link: "
  9241. (org-link-unescape
  9242. (org-match-string-no-properties 1)))))
  9243. ((or (org-in-regexp org-angle-link-re)
  9244. (org-in-regexp org-plain-link-re))
  9245. ;; Convert to bracket link
  9246. (setq remove (list (match-beginning 0) (match-end 0))
  9247. link (read-string "Link: "
  9248. (org-remove-angle-brackets (match-string 0)))))
  9249. ((member complete-file '((4) (16)))
  9250. ;; Completing read for file names.
  9251. (setq link (org-file-complete-link complete-file)))
  9252. (t
  9253. ;; Read link, with completion for stored links.
  9254. (org-link-fontify-links-to-this-file)
  9255. (org-switch-to-buffer-other-window "*Org Links*")
  9256. (with-current-buffer "*Org Links*"
  9257. (erase-buffer)
  9258. (insert "Insert a link.
  9259. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9260. (when org-stored-links
  9261. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9262. (insert (mapconcat 'org-link-prettify
  9263. (reverse org-stored-links) "\n")))
  9264. (goto-char (point-min)))
  9265. (let ((cw (selected-window)))
  9266. (select-window (get-buffer-window "*Org Links*" 'visible))
  9267. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9268. (unless (pos-visible-in-window-p (point-max))
  9269. (org-fit-window-to-buffer))
  9270. (and (window-live-p cw) (select-window cw)))
  9271. ;; Fake a link history, containing the stored links.
  9272. (setq tmphist (append (mapcar 'car org-stored-links)
  9273. org-insert-link-history))
  9274. (setq all-prefixes (append (mapcar 'car abbrevs)
  9275. (mapcar 'car org-link-abbrev-alist)
  9276. org-link-types))
  9277. (unwind-protect
  9278. (progn
  9279. (setq link
  9280. (org-completing-read
  9281. "Link: "
  9282. (append
  9283. (mapcar (lambda (x) (concat x ":"))
  9284. all-prefixes)
  9285. (mapcar 'car org-stored-links))
  9286. nil nil nil
  9287. 'tmphist
  9288. (caar org-stored-links)))
  9289. (if (not (string-match "\\S-" link))
  9290. (user-error "No link selected"))
  9291. (mapc (lambda(l)
  9292. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9293. org-stored-links)
  9294. (if (or (member link all-prefixes)
  9295. (and (equal ":" (substring link -1))
  9296. (member (substring link 0 -1) all-prefixes)
  9297. (setq link (substring link 0 -1))))
  9298. (setq link (with-current-buffer origbuf
  9299. (org-link-try-special-completion link)))))
  9300. (set-window-configuration wcf)
  9301. (kill-buffer "*Org Links*"))
  9302. (setq entry (assoc link org-stored-links))
  9303. (or entry (push link org-insert-link-history))
  9304. (setq desc (or desc (nth 1 entry)))))
  9305. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9306. (not org-keep-stored-link-after-insertion))
  9307. (setq org-stored-links (delq (assoc link org-stored-links)
  9308. org-stored-links)))
  9309. (if (and (string-match org-plain-link-re link)
  9310. (not (string-match org-ts-regexp link)))
  9311. ;; URL-like link, normalize the use of angular brackets.
  9312. (setq link (org-remove-angle-brackets link)))
  9313. ;; Check if we are linking to the current file with a search
  9314. ;; option If yes, simplify the link by using only the search
  9315. ;; option.
  9316. (when (and buffer-file-name
  9317. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9318. (let* ((path (match-string 1 link))
  9319. (case-fold-search nil)
  9320. (search (match-string 2 link)))
  9321. (save-match-data
  9322. (if (equal (file-truename buffer-file-name) (file-truename path))
  9323. ;; We are linking to this same file, with a search option
  9324. (setq link search)))))
  9325. ;; Check if we can/should use a relative path. If yes, simplify the link
  9326. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9327. (let* ((type (match-string 1 link))
  9328. (path (match-string 2 link))
  9329. (origpath path)
  9330. (case-fold-search nil))
  9331. (cond
  9332. ((or (eq org-link-file-path-type 'absolute)
  9333. (equal complete-file '(16)))
  9334. (setq path (abbreviate-file-name (expand-file-name path))))
  9335. ((eq org-link-file-path-type 'noabbrev)
  9336. (setq path (expand-file-name path)))
  9337. ((eq org-link-file-path-type 'relative)
  9338. (setq path (file-relative-name path)))
  9339. (t
  9340. (save-match-data
  9341. (if (string-match (concat "^" (regexp-quote
  9342. (expand-file-name
  9343. (file-name-as-directory
  9344. default-directory))))
  9345. (expand-file-name path))
  9346. ;; We are linking a file with relative path name.
  9347. (setq path (substring (expand-file-name path)
  9348. (match-end 0)))
  9349. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9350. (setq link (concat type path))
  9351. (if (equal desc origpath)
  9352. (setq desc path))))
  9353. (if org-make-link-description-function
  9354. (setq desc
  9355. (or (condition-case nil
  9356. (funcall org-make-link-description-function link desc)
  9357. (error (progn (message "Can't get link description from `%s'"
  9358. (symbol-name org-make-link-description-function))
  9359. (sit-for 2) nil)))
  9360. (read-string "Description: " default-description)))
  9361. (if default-description (setq desc default-description)
  9362. (setq desc (or (and auto-desc desc)
  9363. (read-string "Description: " desc)))))
  9364. (unless (string-match "\\S-" desc) (setq desc nil))
  9365. (if remove (apply 'delete-region remove))
  9366. (insert (org-make-link-string link desc))))
  9367. (defun org-link-try-special-completion (type)
  9368. "If there is completion support for link type TYPE, offer it."
  9369. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9370. (if (functionp fun)
  9371. (funcall fun)
  9372. (read-string "Link (no completion support): " (concat type ":")))))
  9373. (defun org-file-complete-link (&optional arg)
  9374. "Create a file link using completion."
  9375. (let ((file (org-iread-file-name "File: "))
  9376. (pwd (file-name-as-directory (expand-file-name ".")))
  9377. (pwd1 (file-name-as-directory (abbreviate-file-name
  9378. (expand-file-name ".")))))
  9379. (cond ((equal arg '(16))
  9380. (concat "file:"
  9381. (abbreviate-file-name (expand-file-name file))))
  9382. ((string-match
  9383. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9384. (concat "file:" (match-string 1 file)))
  9385. ((string-match
  9386. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9387. (expand-file-name file))
  9388. (concat "file:"
  9389. (match-string 1 (expand-file-name file))))
  9390. (t (concat "file:" file)))))
  9391. (defun org-iread-file-name (&rest args)
  9392. "Read-file-name using `ido-mode' speedup if available.
  9393. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9394. See `read-file-name' for a description of parameters."
  9395. (org-without-partial-completion
  9396. (if (and org-completion-use-ido
  9397. (fboundp 'ido-read-file-name)
  9398. (org-bound-and-true-p ido-mode)
  9399. (listp (nth 1 args)))
  9400. (let ((ido-enter-matching-directory nil))
  9401. (apply #'ido-read-file-name args))
  9402. (apply #'read-file-name args))))
  9403. (defun org-completing-read (&rest args)
  9404. "Completing-read with SPACE being a normal character."
  9405. (let ((enable-recursive-minibuffers t)
  9406. (minibuffer-local-completion-map
  9407. (copy-keymap minibuffer-local-completion-map)))
  9408. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9409. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9410. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9411. (apply 'org-icompleting-read args)))
  9412. (defun org-completing-read-no-i (&rest args)
  9413. (let (org-completion-use-ido org-completion-use-iswitchb)
  9414. (apply 'org-completing-read args)))
  9415. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9416. "Use iswitch as a completing-read replacement to choose from choices.
  9417. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9418. from."
  9419. (let* ((iswitchb-use-virtual-buffers nil)
  9420. (iswitchb-make-buflist-hook
  9421. (lambda ()
  9422. (setq iswitchb-temp-buflist choices))))
  9423. (iswitchb-read-buffer prompt)))
  9424. (defun org-icompleting-read (&rest args)
  9425. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9426. Should be called like `completing-read'."
  9427. (org-without-partial-completion
  9428. (if (not (listp (nth 1 args)))
  9429. ;; Ido only supports lists as the COLLECTION argument. Use
  9430. ;; default completion function when second argument is not
  9431. ;; a list.
  9432. (apply #'completing-read args)
  9433. (let ((ido-enter-matching-directory nil))
  9434. (apply (cond ((and org-completion-use-ido
  9435. (fboundp 'ido-completing-read)
  9436. (org-bound-and-true-p ido-mode))
  9437. #'ido-completing-read)
  9438. ((and org-completion-use-iswitchb
  9439. (org-bound-and-true-p iswitchb-mode))
  9440. #'org-iswitchb-completing-read)
  9441. (t #'completing-read))
  9442. args)))))
  9443. ;;; Opening/following a link
  9444. (defvar org-link-search-failed nil)
  9445. (defvar org-open-link-functions nil
  9446. "Hook for functions finding a plain text link.
  9447. These functions must take a single argument, the link content.
  9448. They will be called for links that look like [[link text][description]]
  9449. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9450. like a filename (e.g. \"./blue.png\").
  9451. These functions will be called *before* Org attempts to resolve the
  9452. link by doing text searches in the current buffer - so if you want a
  9453. link \"[[target]]\" to still find \"<<target>>\", your function should
  9454. handle this as a special case.
  9455. When the function does handle the link, it must return a non-nil value.
  9456. If it decides that it is not responsible for this link, it must return
  9457. nil to indicate that that Org-mode can continue with other options
  9458. like exact and fuzzy text search.")
  9459. (defun org-next-link (&optional search-backward)
  9460. "Move forward to the next link.
  9461. If the link is in hidden text, expose it."
  9462. (interactive "P")
  9463. (when (and org-link-search-failed (eq this-command last-command))
  9464. (goto-char (point-min))
  9465. (message "Link search wrapped back to beginning of buffer"))
  9466. (setq org-link-search-failed nil)
  9467. (let* ((pos (point))
  9468. (ct (org-context))
  9469. (a (assoc :link ct))
  9470. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9471. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9472. ((looking-at org-any-link-re)
  9473. ;; Don't stay stuck at link without an org-link face
  9474. (forward-char (if search-backward -1 1))))
  9475. (if (funcall srch-fun org-any-link-re nil t)
  9476. (progn
  9477. (goto-char (match-beginning 0))
  9478. (if (outline-invisible-p) (org-show-context)))
  9479. (goto-char pos)
  9480. (setq org-link-search-failed t)
  9481. (message "No further link found"))))
  9482. (defun org-previous-link ()
  9483. "Move backward to the previous link.
  9484. If the link is in hidden text, expose it."
  9485. (interactive)
  9486. (funcall 'org-next-link t))
  9487. (defun org-translate-link (s)
  9488. "Translate a link string if a translation function has been defined."
  9489. (if (and org-link-translation-function
  9490. (fboundp org-link-translation-function)
  9491. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9492. (progn
  9493. (setq s (funcall org-link-translation-function
  9494. (match-string 1 s) (match-string 2 s)))
  9495. (concat (car s) ":" (cdr s)))
  9496. s))
  9497. (defun org-translate-link-from-planner (type path)
  9498. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9499. This is still an experimental function, your mileage may vary."
  9500. (cond
  9501. ((member type '("http" "https" "news" "ftp"))
  9502. ;; standard Internet links are the same.
  9503. nil)
  9504. ((and (equal type "irc") (string-match "^//" path))
  9505. ;; Planner has two / at the beginning of an irc link, we have 1.
  9506. ;; We should have zero, actually....
  9507. (setq path (substring path 1)))
  9508. ((and (equal type "lisp") (string-match "^/" path))
  9509. ;; Planner has a slash, we do not.
  9510. (setq type "elisp" path (substring path 1)))
  9511. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9512. ;; A typical message link. Planner has the id after the final slash,
  9513. ;; we separate it with a hash mark
  9514. (setq path (concat (match-string 1 path) "#"
  9515. (org-remove-angle-brackets (match-string 2 path))))))
  9516. (cons type path))
  9517. (defun org-find-file-at-mouse (ev)
  9518. "Open file link or URL at mouse."
  9519. (interactive "e")
  9520. (mouse-set-point ev)
  9521. (org-open-at-point 'in-emacs))
  9522. (defun org-open-at-mouse (ev)
  9523. "Open file link or URL at mouse.
  9524. See the docstring of `org-open-file' for details."
  9525. (interactive "e")
  9526. (mouse-set-point ev)
  9527. (if (eq major-mode 'org-agenda-mode)
  9528. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9529. (org-open-at-point))
  9530. (defvar org-window-config-before-follow-link nil
  9531. "The window configuration before following a link.
  9532. This is saved in case the need arises to restore it.")
  9533. ;;;###autoload
  9534. (defun org-open-at-point-global ()
  9535. "Follow a link like Org-mode does.
  9536. This command can be called in any mode to follow a link that has
  9537. Org-mode syntax."
  9538. (interactive)
  9539. (org-run-like-in-org-mode 'org-open-at-point))
  9540. ;;;###autoload
  9541. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9542. "Open a link in the string S, as if it was in Org-mode."
  9543. (interactive "sLink: \nP")
  9544. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9545. (with-temp-buffer
  9546. (let ((org-inhibit-startup (not reference-buffer)))
  9547. (org-mode)
  9548. (insert s)
  9549. (goto-char (point-min))
  9550. (when reference-buffer
  9551. (setq org-link-abbrev-alist-local
  9552. (with-current-buffer reference-buffer
  9553. org-link-abbrev-alist-local)))
  9554. (org-open-at-point arg reference-buffer)))))
  9555. (defvar org-open-at-point-functions nil
  9556. "Hook that is run when following a link at point.
  9557. Functions in this hook must return t if they identify and follow
  9558. a link at point. If they don't find anything interesting at point,
  9559. they must return nil.")
  9560. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9561. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9562. (defun org-open-at-point (&optional arg reference-buffer)
  9563. "Open link, timestamp, footnote or tags at point.
  9564. When point is on a link, follow it. Normally, files will be
  9565. opened by an appropriate application. If the optional prefix
  9566. argument ARG is non-nil, Emacs will visit the file. With
  9567. a double prefix argument, try to open outside of Emacs, in the
  9568. application the system uses for this file type.
  9569. When point is on a timestamp, open the agenda at the day
  9570. specified.
  9571. When point is a footnote definition, move to the first reference
  9572. found. If it is on a reference, move to the associated
  9573. definition.
  9574. When point is on a headline, display a list of every link in the
  9575. entry, so it is possible to pick one, or all, of them. If point
  9576. is on a tag, call `org-tags-view' instead.
  9577. When optional argument REFERENCE-BUFFER is non-nil, it should
  9578. specify a buffer from where the link search should happen. This
  9579. is used internally by `org-open-link-from-string'.
  9580. On top of syntactically correct links, this function will open
  9581. the link at point in comments or comment blocks and the first
  9582. link in a property drawer line."
  9583. (interactive "P")
  9584. ;; On a code block, open block's results.
  9585. (unless (call-interactively 'org-babel-open-src-block-result)
  9586. (org-load-modules-maybe)
  9587. (setq org-window-config-before-follow-link (current-window-configuration))
  9588. (org-remove-occur-highlights nil nil t)
  9589. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9590. (let* ((context
  9591. ;; Only consider supported types, even if they are not
  9592. ;; the closest one.
  9593. (org-element-lineage
  9594. (org-element-context)
  9595. '(clock comment comment-block footnote-definition
  9596. footnote-reference headline inlinetask keyword link
  9597. node-property timestamp)
  9598. t))
  9599. (type (org-element-type context))
  9600. (value (org-element-property :value context)))
  9601. (cond
  9602. ((not context) (user-error "No link found"))
  9603. ;; Exception: open timestamps and links in properties
  9604. ;; drawers, keywords and comments.
  9605. ((memq type '(comment comment-block keyword node-property))
  9606. (cond ((org-in-regexp org-any-link-re)
  9607. (org-open-link-from-string (match-string-no-properties 0)))
  9608. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9609. (org-follow-timestamp-link))
  9610. (t (user-error "No link found"))))
  9611. ;; On a headline or an inlinetask, but not on a timestamp,
  9612. ;; a link, a footnote reference or on tags.
  9613. ((and (memq type '(headline inlinetask))
  9614. ;; Not on tags.
  9615. (progn (save-excursion (beginning-of-line)
  9616. (looking-at org-complex-heading-regexp))
  9617. (or (not (match-beginning 5))
  9618. (< (point) (match-beginning 5)))))
  9619. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9620. (links (car data))
  9621. (links-end (cdr data)))
  9622. (if links
  9623. (dolist (link (if (stringp links) (list links) links))
  9624. (search-forward link nil links-end)
  9625. (goto-char (match-beginning 0))
  9626. (org-open-at-point))
  9627. (require 'org-attach)
  9628. (org-attach-reveal 'if-exists))))
  9629. ;; On a clock line, make sure point is on the timestamp
  9630. ;; before opening it.
  9631. ((and (eq type 'clock)
  9632. value
  9633. (>= (point) (org-element-property :begin value))
  9634. (<= (point) (org-element-property :end value)))
  9635. (org-follow-timestamp-link))
  9636. ;; Do nothing on white spaces after an object, unless point
  9637. ;; is right after it.
  9638. ((> (point)
  9639. (save-excursion
  9640. (goto-char (org-element-property :end context))
  9641. (skip-chars-backward " \t")
  9642. (point)))
  9643. (user-error "No link found"))
  9644. ((eq type 'timestamp) (org-follow-timestamp-link))
  9645. ;; On tags within a headline or an inlinetask.
  9646. ((and (memq type '(headline inlinetask))
  9647. (progn (save-excursion (beginning-of-line)
  9648. (looking-at org-complex-heading-regexp))
  9649. (and (match-beginning 5)
  9650. (>= (point) (match-beginning 5)))))
  9651. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9652. ((eq type 'link)
  9653. ;; When link is located within the description of another
  9654. ;; link (e.g., an inline image), always open the parent
  9655. ;; link.
  9656. (let*((link (let ((up (org-element-property :parent context)))
  9657. (if (eq (org-element-type up) 'link) up context)))
  9658. (type (org-element-property :type link))
  9659. (path (org-link-unescape (org-element-property :path link))))
  9660. ;; Switch back to REFERENCE-BUFFER needed when called in
  9661. ;; a temporary buffer through `org-open-link-from-string'.
  9662. (with-current-buffer (or reference-buffer (current-buffer))
  9663. (cond
  9664. ((equal type "file")
  9665. (if (string-match "[*?{]" (file-name-nondirectory path))
  9666. (dired path)
  9667. ;; Look into `org-link-protocols' in order to find
  9668. ;; a DEDICATED-FUNCTION to open file. The function
  9669. ;; will be applied on raw link instead of parsed
  9670. ;; link due to the limitation in `org-add-link-type'
  9671. ;; ("open" function called with a single argument).
  9672. ;; If no such function is found, fallback to
  9673. ;; `org-open-file'.
  9674. ;;
  9675. ;; Note : "file+emacs" and "file+sys" types are
  9676. ;; hard-coded in order to escape the previous
  9677. ;; limitation.
  9678. (let* ((option (org-element-property :search-option link))
  9679. (app (org-element-property :application link))
  9680. (dedicated-function
  9681. (nth 1 (assoc app org-link-protocols))))
  9682. (if dedicated-function
  9683. (funcall dedicated-function
  9684. (concat path
  9685. (and option (concat "::" option))))
  9686. (apply #'org-open-file
  9687. path
  9688. (cond (arg)
  9689. ((equal app "emacs") 'emacs)
  9690. ((equal app "sys") 'system))
  9691. (cond ((not option) nil)
  9692. ((org-string-match-p "\\`[0-9]+\\'" option)
  9693. (list (string-to-number option)))
  9694. (t (list nil
  9695. (org-link-unescape option)))))))))
  9696. ((assoc type org-link-protocols)
  9697. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9698. ((equal type "help")
  9699. (let ((f-or-v (intern path)))
  9700. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9701. ((boundp f-or-v) (describe-variable f-or-v))
  9702. (t (error "Not a known function or variable")))))
  9703. ((member type '("http" "https" "ftp" "mailto" "news"))
  9704. (browse-url (org-link-escape-browser (concat type ":" path))))
  9705. ((equal type "doi")
  9706. (browse-url
  9707. (org-link-escape-browser (concat org-doi-server-url path))))
  9708. ((equal type "message") (browse-url (concat type ":" path)))
  9709. ((equal type "shell")
  9710. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9711. (cmd path))
  9712. (if (or (and (org-string-nw-p
  9713. org-confirm-shell-link-not-regexp)
  9714. (string-match
  9715. org-confirm-shell-link-not-regexp cmd))
  9716. (not org-confirm-shell-link-function)
  9717. (funcall org-confirm-shell-link-function
  9718. (format "Execute \"%s\" in shell? "
  9719. (org-add-props cmd nil
  9720. 'face 'org-warning))))
  9721. (progn
  9722. (message "Executing %s" cmd)
  9723. (shell-command cmd buf)
  9724. (when (featurep 'midnight)
  9725. (setq clean-buffer-list-kill-buffer-names
  9726. (cons (buffer-name buf)
  9727. clean-buffer-list-kill-buffer-names))))
  9728. (user-error "Abort"))))
  9729. ((equal type "elisp")
  9730. (let ((cmd path))
  9731. (if (or (and (org-string-nw-p
  9732. org-confirm-elisp-link-not-regexp)
  9733. (org-string-match-p
  9734. org-confirm-elisp-link-not-regexp cmd))
  9735. (not org-confirm-elisp-link-function)
  9736. (funcall org-confirm-elisp-link-function
  9737. (format "Execute \"%s\" as elisp? "
  9738. (org-add-props cmd nil
  9739. 'face 'org-warning))))
  9740. (message "%s => %s" cmd
  9741. (if (eq (string-to-char cmd) ?\()
  9742. (eval (read cmd))
  9743. (call-interactively (read cmd))))
  9744. (user-error "Abort"))))
  9745. ((equal type "id")
  9746. (require 'ord-id)
  9747. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9748. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9749. (unless (run-hook-with-args-until-success
  9750. 'org-open-link-functions path)
  9751. (if (not arg) (org-mark-ring-push)
  9752. (switch-to-buffer-other-window
  9753. (org-get-buffer-for-internal-link (current-buffer))))
  9754. (let ((destination
  9755. (org-with-wide-buffer
  9756. (if (equal type "radio")
  9757. (org-search-radio-target
  9758. (org-element-property :path link))
  9759. (org-link-search
  9760. (if (member type '("custom-id" "coderef"))
  9761. (org-element-property :raw-link link)
  9762. path)
  9763. ;; Prevent fuzzy links from matching
  9764. ;; themselves.
  9765. (and (equal type "fuzzy")
  9766. (+ 2 (org-element-property :begin link)))))
  9767. (point))))
  9768. (unless (and (<= (point-min) destination)
  9769. (>= (point-max) destination))
  9770. (widen))
  9771. (goto-char destination))))
  9772. (t (browse-url-at-point))))))
  9773. ;; On a footnote reference or at a footnote definition's label.
  9774. ((or (eq type 'footnote-reference)
  9775. (and (eq type 'footnote-definition)
  9776. (save-excursion
  9777. ;; Do not validate action when point is on the
  9778. ;; spaces right after the footnote label, in
  9779. ;; order to be on par with behaviour on links.
  9780. (skip-chars-forward " \t")
  9781. (let ((begin
  9782. (org-element-property :contents-begin context)))
  9783. (if begin (< (point) begin)
  9784. (= (org-element-property :post-affiliated context)
  9785. (line-beginning-position)))))))
  9786. (org-footnote-action))
  9787. (t (user-error "No link found")))))
  9788. (run-hook-with-args 'org-follow-link-hook)))
  9789. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9790. "Offer links in the current entry and return the selected link.
  9791. If there is only one link, return it.
  9792. If NTH is an integer, return the NTH link found.
  9793. If ZERO is a string, check also this string for a link, and if
  9794. there is one, return it."
  9795. (with-current-buffer buffer
  9796. (save-excursion
  9797. (save-restriction
  9798. (widen)
  9799. (goto-char marker)
  9800. (let ((cnt ?0)
  9801. (in-emacs (if (integerp nth) nil nth))
  9802. have-zero end links link c)
  9803. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9804. (push (match-string 0 zero) links)
  9805. (setq cnt (1- cnt) have-zero t))
  9806. (save-excursion
  9807. (org-back-to-heading t)
  9808. (setq end (save-excursion (outline-next-heading) (point)))
  9809. (while (re-search-forward org-any-link-re end t)
  9810. (push (match-string 0) links))
  9811. (setq links (org-uniquify (reverse links))))
  9812. (cond
  9813. ((null links)
  9814. (message "No links"))
  9815. ((equal (length links) 1)
  9816. (setq link (car links)))
  9817. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9818. (setq link (nth (if have-zero nth (1- nth)) links)))
  9819. (t ; we have to select a link
  9820. (save-excursion
  9821. (save-window-excursion
  9822. (delete-other-windows)
  9823. (with-output-to-temp-buffer "*Select Link*"
  9824. (mapc (lambda (l)
  9825. (if (not (string-match org-bracket-link-regexp l))
  9826. (princ (format "[%c] %s\n" (incf cnt)
  9827. (org-remove-angle-brackets l)))
  9828. (if (match-end 3)
  9829. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9830. (match-string 3 l) (match-string 1 l)))
  9831. (princ (format "[%c] %s\n" (incf cnt)
  9832. (match-string 1 l))))))
  9833. links))
  9834. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9835. (message "Select link to open, RET to open all:")
  9836. (setq c (read-char-exclusive))
  9837. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9838. (when (equal c ?q) (user-error "Abort"))
  9839. (if (equal c ?\C-m)
  9840. (setq link links)
  9841. (setq nth (- c ?0))
  9842. (if have-zero (setq nth (1+ nth)))
  9843. (unless (and (integerp nth) (>= (length links) nth))
  9844. (user-error "Invalid link selection"))
  9845. (setq link (nth (1- nth) links)))))
  9846. (cons link end))))))
  9847. ;; TODO: These functions are deprecated since `org-open-at-point'
  9848. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9849. (defun org-open-file-with-system (path)
  9850. "Open file at PATH using the system way of opening it."
  9851. (org-open-file path 'system))
  9852. (defun org-open-file-with-emacs (path)
  9853. "Open file at PATH in Emacs."
  9854. (org-open-file path 'emacs))
  9855. ;;; File search
  9856. (defvar org-create-file-search-functions nil
  9857. "List of functions to construct the right search string for a file link.
  9858. These functions are called in turn with point at the location to
  9859. which the link should point.
  9860. A function in the hook should first test if it would like to
  9861. handle this file type, for example by checking the `major-mode'
  9862. or the file extension. If it decides not to handle this file, it
  9863. should just return nil to give other functions a chance. If it
  9864. does handle the file, it must return the search string to be used
  9865. when following the link. The search string will be part of the
  9866. file link, given after a double colon, and `org-open-at-point'
  9867. will automatically search for it. If special measures must be
  9868. taken to make the search successful, another function should be
  9869. added to the companion hook `org-execute-file-search-functions',
  9870. which see.
  9871. A function in this hook may also use `setq' to set the variable
  9872. `description' to provide a suggestion for the descriptive text to
  9873. be used for this link when it gets inserted into an Org-mode
  9874. buffer with \\[org-insert-link].")
  9875. (defvar org-execute-file-search-functions nil
  9876. "List of functions to execute a file search triggered by a link.
  9877. Functions added to this hook must accept a single argument, the
  9878. search string that was part of the file link, the part after the
  9879. double colon. The function must first check if it would like to
  9880. handle this search, for example by checking the `major-mode' or
  9881. the file extension. If it decides not to handle this search, it
  9882. should just return nil to give other functions a chance. If it
  9883. does handle the search, it must return a non-nil value to keep
  9884. other functions from trying.
  9885. Each function can access the current prefix argument through the
  9886. variable `current-prefix-arg'. Note that a single prefix is used
  9887. to force opening a link in Emacs, so it may be good to only use a
  9888. numeric or double prefix to guide the search function.
  9889. In case this is needed, a function in this hook can also restore
  9890. the window configuration before `org-open-at-point' was called using:
  9891. (set-window-configuration org-window-config-before-follow-link)")
  9892. (defun org-search-radio-target (target)
  9893. "Search a radio target matching TARGET in current buffer.
  9894. White spaces are not significant."
  9895. (let ((re (format "<<<%s>>>"
  9896. (mapconcat #'regexp-quote
  9897. (org-split-string target "[ \t\n]+")
  9898. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9899. (origin (point)))
  9900. (goto-char (point-min))
  9901. (catch :radio-match
  9902. (while (re-search-forward re nil t)
  9903. (backward-char)
  9904. (let ((object (org-element-context)))
  9905. (when (eq (org-element-type object) 'radio-target)
  9906. (goto-char (org-element-property :begin object))
  9907. (org-show-context 'link-search)
  9908. (throw :radio-match nil))))
  9909. (goto-char origin)
  9910. (user-error "No match for radio target: %s" target))))
  9911. (defun org-link-search (s &optional avoid-pos stealth)
  9912. "Search for a search string S.
  9913. If S starts with \"#\", it triggers a custom ID search.
  9914. If S is enclosed within parenthesis, it initiates a coderef
  9915. search.
  9916. If S is surrounded by forward slashes, it is interpreted as
  9917. a regular expression. In Org mode files, this will create an
  9918. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9919. to list matches. If the current buffer is in `dired-mode', grep
  9920. will be used to search in all files.
  9921. When AVOID-POS is given, ignore matches near that position.
  9922. When optional argument STEALTH is non-nil, do not modify
  9923. visibility around point, thus ignoring `org-show-context-detail'
  9924. variable.
  9925. Search is case-insensitive and ignores white spaces. Return type
  9926. of matched result, with is either `dedicated' or `fuzzy'."
  9927. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9928. (let* ((case-fold-search t)
  9929. (origin (point))
  9930. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9931. (words (org-split-string s "[ \t\n]+"))
  9932. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9933. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9934. type)
  9935. (cond
  9936. ;; Check if there are any special search functions.
  9937. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9938. ((eq (string-to-char s) ?#)
  9939. ;; Look for a custom ID S if S starts with "#".
  9940. (let* ((id (substring normalized 1))
  9941. (match (org-find-property "CUSTOM_ID" id)))
  9942. (if match (progn (goto-char match) (setf type 'dedicated))
  9943. (error "No match for custom ID: %s" id))))
  9944. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9945. ;; Look for coderef targets if S is enclosed within parenthesis.
  9946. (let ((coderef (match-string-no-properties 1 normalized))
  9947. (re (substring s-single-re 1 -1)))
  9948. (goto-char (point-min))
  9949. (catch :coderef-match
  9950. (while (re-search-forward re nil t)
  9951. (let ((element (org-element-at-point)))
  9952. (when (and (memq (org-element-type element)
  9953. '(example-block src-block))
  9954. ;; Build proper regexp according to current
  9955. ;; block's label format.
  9956. (let ((label-fmt
  9957. (regexp-quote
  9958. (or (org-element-property :label-fmt element)
  9959. org-coderef-label-format))))
  9960. (save-excursion
  9961. (beginning-of-line)
  9962. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9963. (format label-fmt coderef))))))
  9964. (setq type 'dedicated)
  9965. (goto-char (match-beginning 1))
  9966. (throw :coderef-match nil))))
  9967. (goto-char origin)
  9968. (error "No match for coderef: %s" coderef))))
  9969. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9970. ;; Look for a regular expression.
  9971. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9972. (match-string 1 s)))
  9973. ;; Fuzzy links.
  9974. (t
  9975. (let* ((starred (eq (string-to-char normalized) ?*))
  9976. (headline-search (and (derived-mode-p 'org-mode)
  9977. (or org-link-search-must-match-exact-headline
  9978. starred))))
  9979. (cond
  9980. ;; Look for targets, only if not in a headline search.
  9981. ((and (not starred)
  9982. (let ((target (format "<<%s>>" s-multi-re)))
  9983. (catch :target-match
  9984. (goto-char (point-min))
  9985. (while (re-search-forward target nil t)
  9986. (backward-char)
  9987. (let ((context (org-element-context)))
  9988. (when (eq (org-element-type context) 'target)
  9989. (setq type 'dedicated)
  9990. (goto-char (org-element-property :begin context))
  9991. (throw :target-match t))))
  9992. nil))))
  9993. ;; Look for elements named after S, only if not in a headline
  9994. ;; search.
  9995. ((and (not starred)
  9996. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9997. (catch :name-match
  9998. (goto-char (point-min))
  9999. (while (re-search-forward name nil t)
  10000. (let ((element (org-element-at-point)))
  10001. (when (equal (org-split-string
  10002. (org-element-property :name element)
  10003. "[ \t]+")
  10004. words)
  10005. (setq type 'dedicated)
  10006. (beginning-of-line)
  10007. (throw :name-match t))))
  10008. nil))))
  10009. ;; Regular text search. Prefer headlines in Org mode
  10010. ;; buffers.
  10011. ((and (derived-mode-p 'org-mode)
  10012. (let* ((wspace "[ \t]")
  10013. (wspaceopt (concat wspace "*"))
  10014. (cookie (concat "\\(?:"
  10015. wspaceopt
  10016. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10017. wspaceopt
  10018. "\\)"))
  10019. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)"))
  10020. (re (concat
  10021. org-outline-regexp-bol
  10022. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10023. "\\(?:\\[#.\\][ \t]+\\)?"
  10024. "\\(?:" org-comment-string "[ \t]+\\)?"
  10025. sep "*"
  10026. (let ((title (mapconcat #'regexp-quote
  10027. words
  10028. (concat sep "+"))))
  10029. (if starred (substring title 1) title))
  10030. sep "*"
  10031. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10032. "[ \t]*$")))
  10033. (goto-char (point-min))
  10034. (re-search-forward re nil t)))
  10035. (goto-char (match-beginning 0))
  10036. (setq type 'dedicated))
  10037. ;; Offer to create non-existent headline depending on
  10038. ;; `org-link-search-must-match-exact-headline'.
  10039. ((and (derived-mode-p 'org-mode)
  10040. (not org-link-search-inhibit-query)
  10041. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10042. (yes-or-no-p "No match - create this as a new heading? "))
  10043. (goto-char (point-max))
  10044. (unless (bolp) (newline))
  10045. (org-insert-heading nil t t)
  10046. (insert s "\n")
  10047. (beginning-of-line 0))
  10048. ;; Only headlines are looked after. No need to process
  10049. ;; further: throw an error.
  10050. ((and (derived-mode-p 'org-mode)
  10051. (or starred org-link-search-must-match-exact-headline))
  10052. (goto-char origin)
  10053. (error "No match for fuzzy expression: %s" normalized))
  10054. ;; Regular text search.
  10055. ((catch :fuzzy-match
  10056. (goto-char (point-min))
  10057. (while (re-search-forward s-multi-re nil t)
  10058. ;; Skip match if it contains AVOID-POS or it is included
  10059. ;; in a link with a description but outside the
  10060. ;; description.
  10061. (unless (or (and avoid-pos
  10062. (<= (match-beginning 0) avoid-pos)
  10063. (> (match-end 0) avoid-pos))
  10064. (and (save-match-data
  10065. (org-in-regexp org-bracket-link-regexp))
  10066. (match-beginning 3)
  10067. (or (> (match-beginning 3) (point))
  10068. (<= (match-end 3) (point)))
  10069. (org-element-lineage
  10070. (save-match-data (org-element-context))
  10071. '(link) t)))
  10072. (goto-char (match-beginning 0))
  10073. (setq type 'fuzzy)
  10074. (throw :fuzzy-match t)))
  10075. nil))
  10076. ;; All failed. Throw an error.
  10077. (t (goto-char origin)
  10078. (error "No match for fuzzy expression: %s" normalized))))))
  10079. ;; Disclose surroundings of match, if appropriate.
  10080. (when (and (derived-mode-p 'org-mode) (not stealth))
  10081. (org-show-context 'link-search))
  10082. type))
  10083. (defun org-get-buffer-for-internal-link (buffer)
  10084. "Return a buffer to be used for displaying the link target of internal links."
  10085. (cond
  10086. ((not org-display-internal-link-with-indirect-buffer)
  10087. buffer)
  10088. ((string-match "(Clone)$" (buffer-name buffer))
  10089. (message "Buffer is already a clone, not making another one")
  10090. ;; we also do not modify visibility in this case
  10091. buffer)
  10092. (t ; make a new indirect buffer for displaying the link
  10093. (let* ((bn (buffer-name buffer))
  10094. (ibn (concat bn "(Clone)"))
  10095. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10096. (with-current-buffer ib (org-overview))
  10097. ib))))
  10098. (defun org-do-occur (regexp &optional cleanup)
  10099. "Call the Emacs command `occur'.
  10100. If CLEANUP is non-nil, remove the printout of the regular expression
  10101. in the *Occur* buffer. This is useful if the regex is long and not useful
  10102. to read."
  10103. (occur regexp)
  10104. (when cleanup
  10105. (let ((cwin (selected-window)) win beg end)
  10106. (when (setq win (get-buffer-window "*Occur*"))
  10107. (select-window win))
  10108. (goto-char (point-min))
  10109. (when (re-search-forward "match[a-z]+" nil t)
  10110. (setq beg (match-end 0))
  10111. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10112. (setq end (1- (match-beginning 0)))))
  10113. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10114. (goto-char (point-min))
  10115. (select-window cwin))))
  10116. ;;; The mark ring for links jumps
  10117. (defvar org-mark-ring nil
  10118. "Mark ring for positions before jumps in Org-mode.")
  10119. (defvar org-mark-ring-last-goto nil
  10120. "Last position in the mark ring used to go back.")
  10121. ;; Fill and close the ring
  10122. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10123. (loop for i from 1 to org-mark-ring-length do
  10124. (push (make-marker) org-mark-ring))
  10125. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10126. org-mark-ring)
  10127. (defun org-mark-ring-push (&optional pos buffer)
  10128. "Put the current position or POS into the mark ring and rotate it."
  10129. (interactive)
  10130. (setq pos (or pos (point)))
  10131. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10132. (move-marker (car org-mark-ring)
  10133. (or pos (point))
  10134. (or buffer (current-buffer)))
  10135. (message "%s"
  10136. (substitute-command-keys
  10137. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10138. (defun org-mark-ring-goto (&optional n)
  10139. "Jump to the previous position in the mark ring.
  10140. With prefix arg N, jump back that many stored positions. When
  10141. called several times in succession, walk through the entire ring.
  10142. Org-mode commands jumping to a different position in the current file,
  10143. or to another Org-mode file, automatically push the old position
  10144. onto the ring."
  10145. (interactive "p")
  10146. (let (p m)
  10147. (if (eq last-command this-command)
  10148. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10149. (setq p org-mark-ring))
  10150. (setq org-mark-ring-last-goto p)
  10151. (setq m (car p))
  10152. (org-pop-to-buffer-same-window (marker-buffer m))
  10153. (goto-char m)
  10154. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10155. (defun org-remove-angle-brackets (s)
  10156. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10157. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10158. s)
  10159. (defun org-add-angle-brackets (s)
  10160. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10161. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10162. s)
  10163. (defun org-remove-double-quotes (s)
  10164. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10165. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10166. s)
  10167. ;;; Following specific links
  10168. (defun org-follow-timestamp-link ()
  10169. "Open an agenda view for the time-stamp date/range at point."
  10170. (cond
  10171. ((org-at-date-range-p t)
  10172. (let ((org-agenda-start-on-weekday)
  10173. (t1 (match-string 1))
  10174. (t2 (match-string 2)) tt1 tt2)
  10175. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10176. tt2 (time-to-days (org-time-string-to-time t2)))
  10177. (let ((org-agenda-buffer-tmp-name
  10178. (format "*Org Agenda(a:%s)"
  10179. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10180. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10181. ((org-at-timestamp-p t)
  10182. (let ((org-agenda-buffer-tmp-name
  10183. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10184. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10185. (substring (match-string 1) 0 10)))
  10186. 1)))
  10187. (t (error "This should not happen"))))
  10188. ;;; Following file links
  10189. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10190. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10191. (declare-function mailcap-mime-info
  10192. "mailcap" (string &optional request no-decode))
  10193. (defvar org-wait nil)
  10194. (defun org-open-file (path &optional in-emacs line search)
  10195. "Open the file at PATH.
  10196. First, this expands any special file name abbreviations. Then the
  10197. configuration variable `org-file-apps' is checked if it contains an
  10198. entry for this file type, and if yes, the corresponding command is launched.
  10199. If no application is found, Emacs simply visits the file.
  10200. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10201. With a double \\[universal-argument] \\[universal-argument] \
  10202. prefix arg, Org tries to avoid opening in Emacs
  10203. and to use an external application to visit the file.
  10204. Optional LINE specifies a line to go to, optional SEARCH a string
  10205. to search for. If LINE or SEARCH is given, the file will be
  10206. opened in Emacs, unless an entry from org-file-apps that makes
  10207. use of groups in a regexp matches.
  10208. If you want to change the way frames are used when following a
  10209. link, please customize `org-link-frame-setup'.
  10210. If the file does not exist, an error is thrown."
  10211. (let* ((file (if (equal path "")
  10212. buffer-file-name
  10213. (substitute-in-file-name (expand-file-name path))))
  10214. (file-apps (append org-file-apps (org-default-apps)))
  10215. (apps (org-remove-if
  10216. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10217. (apps-dlink (org-remove-if-not
  10218. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10219. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10220. (dirp (if remp nil (file-directory-p file)))
  10221. (file (if (and dirp org-open-directory-means-index-dot-org)
  10222. (concat (file-name-as-directory file) "index.org")
  10223. file))
  10224. (a-m-a-p (assq 'auto-mode apps))
  10225. (dfile (downcase file))
  10226. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10227. (link (cond ((and (eq line nil)
  10228. (eq search nil))
  10229. file)
  10230. (line
  10231. (concat file "::" (number-to-string line)))
  10232. (search
  10233. (concat file "::" search))))
  10234. (dlink (downcase link))
  10235. (old-buffer (current-buffer))
  10236. (old-pos (point))
  10237. (old-mode major-mode)
  10238. ext cmd link-match-data)
  10239. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10240. (setq ext (match-string 1 dfile))
  10241. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10242. (setq ext (match-string 1 dfile))))
  10243. (cond
  10244. ((member in-emacs '((16) system))
  10245. (setq cmd (cdr (assoc 'system apps))))
  10246. (in-emacs (setq cmd 'emacs))
  10247. (t
  10248. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10249. (and dirp (cdr (assoc 'directory apps)))
  10250. ; first, try matching against apps-dlink
  10251. ; if we get a match here, store the match data for later
  10252. (let ((match (assoc-default dlink apps-dlink
  10253. 'string-match)))
  10254. (if match
  10255. (progn (setq link-match-data (match-data))
  10256. match)
  10257. (progn (setq in-emacs (or in-emacs line search))
  10258. nil))) ; if we have no match in apps-dlink,
  10259. ; always open the file in emacs if line or search
  10260. ; is given (for backwards compatibility)
  10261. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10262. 'string-match)
  10263. (cdr (assoc ext apps))
  10264. (cdr (assoc t apps))))))
  10265. (when (eq cmd 'system)
  10266. (setq cmd (cdr (assoc 'system apps))))
  10267. (when (eq cmd 'default)
  10268. (setq cmd (cdr (assoc t apps))))
  10269. (when (eq cmd 'mailcap)
  10270. (require 'mailcap)
  10271. (mailcap-parse-mailcaps)
  10272. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10273. (command (mailcap-mime-info mime-type)))
  10274. (if (stringp command)
  10275. (setq cmd command)
  10276. (setq cmd 'emacs))))
  10277. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10278. (not (file-exists-p file))
  10279. (not org-open-non-existing-files))
  10280. (user-error "No such file: %s" file))
  10281. (cond
  10282. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10283. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10284. (while (string-match "['\"]%s['\"]" cmd)
  10285. (setq cmd (replace-match "%s" t t cmd)))
  10286. (while (string-match "%s" cmd)
  10287. (setq cmd (replace-match
  10288. (save-match-data
  10289. (shell-quote-argument
  10290. (convert-standard-filename file)))
  10291. t t cmd)))
  10292. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10293. (save-match-data
  10294. (let ((match-index 1)
  10295. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10296. (set-match-data link-match-data)
  10297. (while (<= match-index number-of-groups)
  10298. (let ((regex (concat "%" (number-to-string match-index)))
  10299. (replace-with (match-string match-index dlink)))
  10300. (while (string-match regex cmd)
  10301. (setq cmd (replace-match replace-with t t cmd))))
  10302. (setq match-index (+ match-index 1)))))
  10303. (save-window-excursion
  10304. (message "Running %s...done" cmd)
  10305. (start-process-shell-command cmd nil cmd)
  10306. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10307. ((or (stringp cmd)
  10308. (eq cmd 'emacs))
  10309. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10310. (widen)
  10311. (if line (progn (org-goto-line line)
  10312. (if (derived-mode-p 'org-mode)
  10313. (org-reveal)))
  10314. (if search (org-link-search search))))
  10315. ((consp cmd)
  10316. (let ((file (convert-standard-filename file)))
  10317. (save-match-data
  10318. (set-match-data link-match-data)
  10319. (eval cmd))))
  10320. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10321. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10322. (or (not (equal old-buffer (current-buffer)))
  10323. (not (equal old-pos (point))))
  10324. (org-mark-ring-push old-pos old-buffer))))
  10325. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10326. "This function returns non-nil if `entry' uses a regular
  10327. expression which should be matched against the whole link by
  10328. org-open-file.
  10329. It assumes that is the case when the entry uses a regular
  10330. expression which has at least one grouping construct and the
  10331. action is either a lisp form or a command string containing
  10332. '%1', i.e. using at least one subexpression match as a
  10333. parameter."
  10334. (let ((selector (car entry))
  10335. (action (cdr entry)))
  10336. (if (stringp selector)
  10337. (and (> (regexp-opt-depth selector) 0)
  10338. (or (and (stringp action)
  10339. (string-match "%[0-9]" action))
  10340. (consp action)))
  10341. nil)))
  10342. (defun org-default-apps ()
  10343. "Return the default applications for this operating system."
  10344. (cond
  10345. ((eq system-type 'darwin)
  10346. org-file-apps-defaults-macosx)
  10347. ((eq system-type 'windows-nt)
  10348. org-file-apps-defaults-windowsnt)
  10349. (t org-file-apps-defaults-gnu)))
  10350. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10351. "Convert extensions to regular expressions in the cars of LIST.
  10352. Also, weed out any non-string entries, because the return value is used
  10353. only for regexp matching.
  10354. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10355. point to the symbol `emacs', indicating that the file should
  10356. be opened in Emacs."
  10357. (append
  10358. (delq nil
  10359. (mapcar (lambda (x)
  10360. (if (not (stringp (car x)))
  10361. nil
  10362. (if (string-match "\\W" (car x))
  10363. x
  10364. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10365. list))
  10366. (if add-auto-mode
  10367. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10368. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10369. (defun org-file-remote-p (file)
  10370. "Test whether FILE specifies a location on a remote system.
  10371. Return non-nil if the location is indeed remote.
  10372. For example, the filename \"/user@host:/foo\" specifies a location
  10373. on the system \"/user@host:\"."
  10374. (cond ((fboundp 'file-remote-p)
  10375. (file-remote-p file))
  10376. ((fboundp 'tramp-handle-file-remote-p)
  10377. (tramp-handle-file-remote-p file))
  10378. ((and (boundp 'ange-ftp-name-format)
  10379. (string-match (car ange-ftp-name-format) file))
  10380. t)))
  10381. ;;;; Refiling
  10382. (defun org-get-org-file ()
  10383. "Read a filename, with default directory `org-directory'."
  10384. (let ((default (or org-default-notes-file remember-data-file)))
  10385. (read-file-name (format "File name [%s]: " default)
  10386. (file-name-as-directory org-directory)
  10387. default)))
  10388. (defun org-notes-order-reversed-p ()
  10389. "Check if the current file should receive notes in reversed order."
  10390. (cond
  10391. ((not org-reverse-note-order) nil)
  10392. ((eq t org-reverse-note-order) t)
  10393. ((not (listp org-reverse-note-order)) nil)
  10394. (t (catch 'exit
  10395. (dolist (entry org-reverse-note-order)
  10396. (if (string-match (car entry) buffer-file-name)
  10397. (throw 'exit (cdr entry))))))))
  10398. (defvar org-refile-target-table nil
  10399. "The list of refile targets, created by `org-refile'.")
  10400. (defvar org-agenda-new-buffers nil
  10401. "Buffers created to visit agenda files.")
  10402. (defvar org-refile-cache nil
  10403. "Cache for refile targets.")
  10404. (defvar org-refile-markers nil
  10405. "All the markers used for caching refile locations.")
  10406. (defun org-refile-marker (pos)
  10407. "Get a new refile marker, but only if caching is in use."
  10408. (if (not org-refile-use-cache)
  10409. pos
  10410. (let ((m (make-marker)))
  10411. (move-marker m pos)
  10412. (push m org-refile-markers)
  10413. m)))
  10414. (defun org-refile-cache-clear ()
  10415. "Clear the refile cache and disable all the markers."
  10416. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10417. (setq org-refile-markers nil)
  10418. (setq org-refile-cache nil)
  10419. (message "Refile cache has been cleared"))
  10420. (defun org-refile-cache-check-set (set)
  10421. "Check if all the markers in the cache still have live buffers."
  10422. (let (marker)
  10423. (catch 'exit
  10424. (while (and set (setq marker (nth 3 (pop set))))
  10425. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10426. (when (and marker (null (marker-buffer marker)))
  10427. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10428. (sit-for 3)
  10429. (throw 'exit nil)))
  10430. t)))
  10431. (defun org-refile-cache-put (set &rest identifiers)
  10432. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10433. (let* ((key (sha1 (prin1-to-string identifiers)))
  10434. (entry (assoc key org-refile-cache)))
  10435. (if entry
  10436. (setcdr entry set)
  10437. (push (cons key set) org-refile-cache))))
  10438. (defun org-refile-cache-get (&rest identifiers)
  10439. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10440. (cond
  10441. ((not org-refile-cache) nil)
  10442. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10443. (t
  10444. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10445. org-refile-cache))))
  10446. (and set (org-refile-cache-check-set set) set)))))
  10447. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10448. "Produce a table with refile targets."
  10449. (let ((case-fold-search nil)
  10450. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10451. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10452. targets tgs txt re files desc descre fast-path-p level pos0)
  10453. (message "Getting targets...")
  10454. (with-current-buffer (or default-buffer (current-buffer))
  10455. (dolist (entry entries)
  10456. (setq files (car entry) desc (cdr entry))
  10457. (setq fast-path-p nil)
  10458. (cond
  10459. ((null files) (setq files (list (current-buffer))))
  10460. ((eq files 'org-agenda-files)
  10461. (setq files (org-agenda-files 'unrestricted)))
  10462. ((and (symbolp files) (fboundp files))
  10463. (setq files (funcall files)))
  10464. ((and (symbolp files) (boundp files))
  10465. (setq files (symbol-value files))))
  10466. (if (stringp files) (setq files (list files)))
  10467. (cond
  10468. ((eq (car desc) :tag)
  10469. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10470. ((eq (car desc) :todo)
  10471. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10472. ((eq (car desc) :regexp)
  10473. (setq descre (cdr desc)))
  10474. ((eq (car desc) :level)
  10475. (setq descre (concat "^\\*\\{" (number-to-string
  10476. (if org-odd-levels-only
  10477. (1- (* 2 (cdr desc)))
  10478. (cdr desc)))
  10479. "\\}[ \t]")))
  10480. ((eq (car desc) :maxlevel)
  10481. (setq fast-path-p t)
  10482. (setq descre (concat "^\\*\\{1," (number-to-string
  10483. (if org-odd-levels-only
  10484. (1- (* 2 (cdr desc)))
  10485. (cdr desc)))
  10486. "\\}[ \t]")))
  10487. (t (error "Bad refiling target description %s" desc)))
  10488. (dolist (f files)
  10489. (with-current-buffer
  10490. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10491. (or
  10492. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10493. (progn
  10494. (if (bufferp f) (setq f (buffer-file-name
  10495. (buffer-base-buffer f))))
  10496. (setq f (and f (expand-file-name f)))
  10497. (if (eq org-refile-use-outline-path 'file)
  10498. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10499. (save-excursion
  10500. (save-restriction
  10501. (widen)
  10502. (goto-char (point-min))
  10503. (while (re-search-forward descre nil t)
  10504. (goto-char (setq pos0 (point-at-bol)))
  10505. (catch 'next
  10506. (when org-refile-target-verify-function
  10507. (save-match-data
  10508. (or (funcall org-refile-target-verify-function)
  10509. (throw 'next t))))
  10510. (when (and (looking-at org-complex-heading-regexp)
  10511. (not (member (match-string 4) excluded-entries))
  10512. (match-string 4))
  10513. (setq level (org-reduced-level
  10514. (- (match-end 1) (match-beginning 1)))
  10515. txt (org-link-display-format (match-string 4))
  10516. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10517. re (format org-complex-heading-regexp-format
  10518. (regexp-quote (match-string 4))))
  10519. (when org-refile-use-outline-path
  10520. (setq txt (mapconcat
  10521. 'org-protect-slash
  10522. (append
  10523. (if (eq org-refile-use-outline-path
  10524. 'file)
  10525. (list (file-name-nondirectory
  10526. (buffer-file-name
  10527. (buffer-base-buffer))))
  10528. (if (eq org-refile-use-outline-path
  10529. 'full-file-path)
  10530. (list (buffer-file-name
  10531. (buffer-base-buffer)))))
  10532. (org-get-outline-path fast-path-p
  10533. level txt)
  10534. (list txt))
  10535. "/")))
  10536. (push (list txt f re (org-refile-marker (point)))
  10537. tgs)))
  10538. (when (= (point) pos0)
  10539. ;; verification function has not moved point
  10540. (goto-char (point-at-eol))))))))
  10541. (when org-refile-use-cache
  10542. (org-refile-cache-put tgs (buffer-file-name) descre))
  10543. (setq targets (append tgs targets))))))
  10544. (message "Getting targets...done")
  10545. (nreverse targets)))
  10546. (defun org-protect-slash (s)
  10547. (while (string-match "/" s)
  10548. (setq s (replace-match "\\" t t s)))
  10549. s)
  10550. (defvar org-olpa (make-vector 20 nil))
  10551. (defun org-get-outline-path (&optional fastp level heading)
  10552. "Return the outline path to the current entry, as a list.
  10553. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10554. routine which makes outline path derivations for an entire file,
  10555. avoiding backtracing. Refile target collection makes use of that."
  10556. (if fastp
  10557. (progn
  10558. (if (> level 19)
  10559. (error "Outline path failure, more than 19 levels"))
  10560. (loop for i from level upto 19 do
  10561. (aset org-olpa i nil))
  10562. (prog1
  10563. (delq nil (append org-olpa nil))
  10564. (aset org-olpa level heading)))
  10565. (let (rtn case-fold-search)
  10566. (save-excursion
  10567. (save-restriction
  10568. (widen)
  10569. (while (org-up-heading-safe)
  10570. (when (looking-at org-complex-heading-regexp)
  10571. (push (org-trim
  10572. (replace-regexp-in-string
  10573. ;; Remove statistical/checkboxes cookies
  10574. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10575. (org-match-string-no-properties 4)))
  10576. rtn)))
  10577. rtn)))))
  10578. (defun org-format-outline-path (path &optional width prefix separator)
  10579. "Format the outline path PATH for display.
  10580. WIDTH is the maximum number of characters that is available.
  10581. PREFIX is a prefix to be included in the returned string,
  10582. such as the file name.
  10583. SEPARATOR is inserted between the different parts of the path,
  10584. the default is \"/\"."
  10585. (setq width (or width 79))
  10586. (if prefix (setq width (- width (length prefix))))
  10587. (if (not path)
  10588. (or prefix "")
  10589. (let* ((nsteps (length path))
  10590. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10591. (maxwidth (if (<= total-width width)
  10592. 10000 ;; everything fits
  10593. ;; we need to shorten the level headings
  10594. (/ (- width nsteps) nsteps)))
  10595. (org-odd-levels-only nil)
  10596. (n 0)
  10597. (total (1+ (length prefix))))
  10598. (setq maxwidth (max maxwidth 10))
  10599. (concat prefix
  10600. (if prefix (or separator "/"))
  10601. (mapconcat
  10602. (lambda (h)
  10603. (setq n (1+ n))
  10604. (if (and (= n nsteps) (< maxwidth 10000))
  10605. (setq maxwidth (- total-width total)))
  10606. (if (< (length h) maxwidth)
  10607. (progn (setq total (+ total (length h) 1)) h)
  10608. (setq h (substring h 0 (- maxwidth 2))
  10609. total (+ total maxwidth 1))
  10610. (if (string-match "[ \t]+\\'" h)
  10611. (setq h (substring h 0 (match-beginning 0))))
  10612. (setq h (concat h "..")))
  10613. (org-add-props h nil 'face
  10614. (nth (% (1- n) org-n-level-faces)
  10615. org-level-faces))
  10616. h)
  10617. path (or separator "/"))))))
  10618. (defun org-display-outline-path (&optional file current separator just-return-string)
  10619. "Display the current outline path in the echo area.
  10620. If FILE is non-nil, prepend the output with the file name.
  10621. If CURRENT is non-nil, append the current heading to the output.
  10622. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10623. the different parts of the path and defaults to \"/\".
  10624. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10625. (interactive "P")
  10626. (let* (case-fold-search
  10627. (bfn (buffer-file-name (buffer-base-buffer)))
  10628. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10629. res)
  10630. (if current (setq path (append path
  10631. (save-excursion
  10632. (org-back-to-heading t)
  10633. (if (looking-at org-complex-heading-regexp)
  10634. (list (match-string 4)))))))
  10635. (setq res
  10636. (org-format-outline-path
  10637. path
  10638. (1- (frame-width))
  10639. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10640. separator))
  10641. (if just-return-string
  10642. (org-no-properties res)
  10643. (org-unlogged-message "%s" res))))
  10644. (defvar org-refile-history nil
  10645. "History for refiling operations.")
  10646. (defvar org-after-refile-insert-hook nil
  10647. "Hook run after `org-refile' has inserted its stuff at the new location.
  10648. Note that this is still *before* the stuff will be removed from
  10649. the *old* location.")
  10650. (defvar org-capture-last-stored-marker)
  10651. (defvar org-refile-keep nil
  10652. "Non-nil means `org-refile' will copy instead of refile.")
  10653. (defun org-copy ()
  10654. "Like `org-refile', but copy."
  10655. (interactive)
  10656. (let ((org-refile-keep t))
  10657. (funcall 'org-refile nil nil nil "Copy")))
  10658. (defun org-refile (&optional arg default-buffer rfloc msg)
  10659. "Move the entry or entries at point to another heading.
  10660. The list of target headings is compiled using the information in
  10661. `org-refile-targets', which see.
  10662. At the target location, the entry is filed as a subitem of the
  10663. target heading. Depending on `org-reverse-note-order', the new
  10664. subitem will either be the first or the last subitem.
  10665. If there is an active region, all entries in that region will be
  10666. refiled. However, the region must fulfill the requirement that
  10667. the first heading sets the top-level of the moved text.
  10668. With prefix arg ARG, the command will only visit the target
  10669. location and not actually move anything.
  10670. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10671. refiling operation has put the subtree.
  10672. With a numeric prefix argument of `2', refile to the running clock.
  10673. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10674. being set to t and copy to the target location, don't move it.
  10675. Beware that keeping refiled entries may result in duplicated ID
  10676. properties.
  10677. RFLOC can be a refile location obtained in a different way.
  10678. MSG is a string to replace \"Refile\" in the default prompt with
  10679. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10680. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10681. If you are using target caching (see `org-refile-use-cache'), you
  10682. have to clear the target cache in order to find new targets.
  10683. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10684. prefix argument (`C-u C-u C-u C-c C-w')."
  10685. (interactive "P")
  10686. (if (member arg '(0 (64)))
  10687. (org-refile-cache-clear)
  10688. (let* ((actionmsg (cond (msg msg)
  10689. ((equal arg 3) "Refile (and keep)")
  10690. (t "Refile")))
  10691. (cbuf (current-buffer))
  10692. (regionp (org-region-active-p))
  10693. (region-start (and regionp (region-beginning)))
  10694. (region-end (and regionp (region-end)))
  10695. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10696. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10697. pos it nbuf file re level reversed)
  10698. (setq last-command nil)
  10699. (when regionp
  10700. (goto-char region-start)
  10701. (or (bolp) (goto-char (point-at-bol)))
  10702. (setq region-start (point))
  10703. (unless (or (org-kill-is-subtree-p
  10704. (buffer-substring region-start region-end))
  10705. (prog1 org-refile-active-region-within-subtree
  10706. (let ((s (point-at-eol)))
  10707. (org-toggle-heading)
  10708. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10709. (user-error "The region is not a (sequence of) subtree(s)")))
  10710. (if (equal arg '(16))
  10711. (org-refile-goto-last-stored)
  10712. (when (or
  10713. (and (equal arg 2)
  10714. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10715. (prog1
  10716. (setq it (list (or org-clock-heading "running clock")
  10717. (buffer-file-name
  10718. (marker-buffer org-clock-hd-marker))
  10719. ""
  10720. (marker-position org-clock-hd-marker)))
  10721. (setq arg nil)))
  10722. (setq it (or rfloc
  10723. (let (heading-text)
  10724. (save-excursion
  10725. (unless (and arg (listp arg))
  10726. (org-back-to-heading t)
  10727. (setq heading-text
  10728. (replace-regexp-in-string
  10729. org-bracket-link-regexp
  10730. "\\3"
  10731. (nth 4 (org-heading-components)))))
  10732. (org-refile-get-location
  10733. (cond ((and arg (listp arg)) "Goto")
  10734. (regionp (concat actionmsg " region to"))
  10735. (t (concat actionmsg " subtree \""
  10736. heading-text "\" to")))
  10737. default-buffer
  10738. (and (not (equal '(4) arg))
  10739. org-refile-allow-creating-parent-nodes)
  10740. arg))))))
  10741. (setq file (nth 1 it)
  10742. re (nth 2 it)
  10743. pos (nth 3 it))
  10744. (if (and (not arg)
  10745. pos
  10746. (equal (buffer-file-name) file)
  10747. (if regionp
  10748. (and (>= pos region-start)
  10749. (<= pos region-end))
  10750. (and (>= pos (point))
  10751. (< pos (save-excursion
  10752. (org-end-of-subtree t t))))))
  10753. (error "Cannot refile to position inside the tree or region"))
  10754. (setq nbuf (or (find-buffer-visiting file)
  10755. (find-file-noselect file)))
  10756. (if (and arg (not (equal arg 3)))
  10757. (progn
  10758. (org-pop-to-buffer-same-window nbuf)
  10759. (goto-char pos)
  10760. (org-show-context 'org-goto))
  10761. (if regionp
  10762. (progn
  10763. (org-kill-new (buffer-substring region-start region-end))
  10764. (org-save-markers-in-region region-start region-end))
  10765. (org-copy-subtree 1 nil t))
  10766. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10767. (find-file-noselect file)))
  10768. (setq reversed (org-notes-order-reversed-p))
  10769. (save-excursion
  10770. (save-restriction
  10771. (widen)
  10772. (if pos
  10773. (progn
  10774. (goto-char pos)
  10775. (looking-at org-outline-regexp)
  10776. (setq level (org-get-valid-level (funcall outline-level) 1))
  10777. (goto-char
  10778. (if reversed
  10779. (or (outline-next-heading) (point-max))
  10780. (or (save-excursion (org-get-next-sibling))
  10781. (org-end-of-subtree t t)
  10782. (point-max)))))
  10783. (setq level 1)
  10784. (if (not reversed)
  10785. (goto-char (point-max))
  10786. (goto-char (point-min))
  10787. (or (outline-next-heading) (goto-char (point-max)))))
  10788. (if (not (bolp)) (newline))
  10789. (org-paste-subtree level nil nil t)
  10790. (when org-log-refile
  10791. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10792. (unless (eq org-log-refile 'note)
  10793. (save-excursion (org-add-log-note))))
  10794. (and org-auto-align-tags
  10795. (let ((org-loop-over-headlines-in-active-region nil))
  10796. (org-set-tags nil t)))
  10797. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10798. :last-refile)))
  10799. (when bookmark-name
  10800. (with-demoted-errors
  10801. (bookmark-set bookmark-name))))
  10802. ;; If we are refiling for capture, make sure that the
  10803. ;; last-capture pointers point here
  10804. (when (org-bound-and-true-p org-refile-for-capture)
  10805. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10806. :last-capture-marker)))
  10807. (when bookmark-name
  10808. (with-demoted-errors
  10809. (bookmark-set bookmark-name))))
  10810. (move-marker org-capture-last-stored-marker (point)))
  10811. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10812. (run-hooks 'org-after-refile-insert-hook))))
  10813. (unless org-refile-keep
  10814. (if regionp
  10815. (delete-region (point) (+ (point) (- region-end region-start)))
  10816. (delete-region
  10817. (and (org-back-to-heading t) (point))
  10818. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10819. (when (featurep 'org-inlinetask)
  10820. (org-inlinetask-remove-END-maybe))
  10821. (setq org-markers-to-move nil)
  10822. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10823. (defun org-refile-goto-last-stored ()
  10824. "Go to the location where the last refile was stored."
  10825. (interactive)
  10826. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10827. (message "This is the location of the last refile"))
  10828. (defun org-refile--get-location (refloc tbl)
  10829. "When user refile to REFLOC, find the associated target in TBL.
  10830. Also check `org-refile-target-table'."
  10831. (car (delq
  10832. nil
  10833. (mapcar
  10834. (lambda (r) (or (assoc r tbl)
  10835. (assoc r org-refile-target-table)))
  10836. (list (replace-regexp-in-string "/$" "" refloc)
  10837. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10838. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10839. no-exclude)
  10840. "Prompt the user for a refile location, using PROMPT.
  10841. PROMPT should not be suffixed with a colon and a space, because
  10842. this function appends the default value from
  10843. `org-refile-history' automatically, if that is not empty.
  10844. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10845. this is used for the GOTO interface."
  10846. (let ((org-refile-targets org-refile-targets)
  10847. (org-refile-use-outline-path org-refile-use-outline-path)
  10848. excluded-entries)
  10849. (when (and (derived-mode-p 'org-mode)
  10850. (not org-refile-use-cache)
  10851. (not no-exclude))
  10852. (org-map-tree
  10853. (lambda()
  10854. (setq excluded-entries
  10855. (append excluded-entries (list (org-get-heading t t)))))))
  10856. (setq org-refile-target-table
  10857. (org-refile-get-targets default-buffer excluded-entries)))
  10858. (unless org-refile-target-table
  10859. (user-error "No refile targets"))
  10860. (let* ((cbuf (current-buffer))
  10861. (partial-completion-mode nil)
  10862. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10863. (cfunc (if (and org-refile-use-outline-path
  10864. org-outline-path-complete-in-steps)
  10865. 'org-olpath-completing-read
  10866. 'org-icompleting-read))
  10867. (extra (if org-refile-use-outline-path "/" ""))
  10868. (cbnex (concat (buffer-name) extra))
  10869. (filename (and cfn (expand-file-name cfn)))
  10870. (tbl (mapcar
  10871. (lambda (x)
  10872. (if (and (not (member org-refile-use-outline-path
  10873. '(file full-file-path)))
  10874. (not (equal filename (nth 1 x))))
  10875. (cons (concat (car x) extra " ("
  10876. (file-name-nondirectory (nth 1 x)) ")")
  10877. (cdr x))
  10878. (cons (concat (car x) extra) (cdr x))))
  10879. org-refile-target-table))
  10880. (completion-ignore-case t)
  10881. cdef
  10882. (prompt (concat prompt
  10883. (or (and (car org-refile-history)
  10884. (concat " (default " (car org-refile-history) ")"))
  10885. (and (assoc cbnex tbl) (setq cdef cbnex)
  10886. (concat " (default " cbnex ")"))) ": "))
  10887. pa answ parent-target child parent old-hist)
  10888. (setq old-hist org-refile-history)
  10889. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10890. nil 'org-refile-history (or cdef (car org-refile-history))))
  10891. (if (setq pa (org-refile--get-location answ tbl))
  10892. (progn
  10893. (org-refile-check-position pa)
  10894. (when (or (not org-refile-history)
  10895. (not (eq old-hist org-refile-history))
  10896. (not (equal (car pa) (car org-refile-history))))
  10897. (setq org-refile-history
  10898. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10899. org-refile-history
  10900. (cdr org-refile-history))))
  10901. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10902. (pop org-refile-history)))
  10903. pa)
  10904. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10905. (progn
  10906. (setq parent (match-string 1 answ)
  10907. child (match-string 2 answ))
  10908. (setq parent-target (org-refile--get-location parent tbl))
  10909. (when (and parent-target
  10910. (or (eq new-nodes t)
  10911. (and (eq new-nodes 'confirm)
  10912. (y-or-n-p (format "Create new node \"%s\"? "
  10913. child)))))
  10914. (org-refile-new-child parent-target child)))
  10915. (user-error "Invalid target location")))))
  10916. (declare-function org-string-nw-p "org-macs" (s))
  10917. (defun org-refile-check-position (refile-pointer)
  10918. "Check if the refile pointer matches the headline to which it points."
  10919. (let* ((file (nth 1 refile-pointer))
  10920. (re (nth 2 refile-pointer))
  10921. (pos (nth 3 refile-pointer))
  10922. buffer)
  10923. (if (and (not (markerp pos)) (not file))
  10924. (user-error "Please indicate a target file in the refile path")
  10925. (when (org-string-nw-p re)
  10926. (setq buffer (if (markerp pos)
  10927. (marker-buffer pos)
  10928. (or (find-buffer-visiting file)
  10929. (find-file-noselect file))))
  10930. (with-current-buffer buffer
  10931. (save-excursion
  10932. (save-restriction
  10933. (widen)
  10934. (goto-char pos)
  10935. (beginning-of-line 1)
  10936. (unless (org-looking-at-p re)
  10937. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10938. (defun org-refile-new-child (parent-target child)
  10939. "Use refile target PARENT-TARGET to add new CHILD below it."
  10940. (unless parent-target
  10941. (error "Cannot find parent for new node"))
  10942. (let ((file (nth 1 parent-target))
  10943. (pos (nth 3 parent-target))
  10944. level)
  10945. (with-current-buffer (or (find-buffer-visiting file)
  10946. (find-file-noselect file))
  10947. (save-excursion
  10948. (save-restriction
  10949. (widen)
  10950. (if pos
  10951. (goto-char pos)
  10952. (goto-char (point-max))
  10953. (if (not (bolp)) (newline)))
  10954. (when (looking-at org-outline-regexp)
  10955. (setq level (funcall outline-level))
  10956. (org-end-of-subtree t t))
  10957. (org-back-over-empty-lines)
  10958. (insert "\n" (make-string
  10959. (if pos (org-get-valid-level level 1) 1) ?*)
  10960. " " child "\n")
  10961. (beginning-of-line 0)
  10962. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10963. (defun org-olpath-completing-read (prompt collection &rest args)
  10964. "Read an outline path like a file name."
  10965. (let ((thetable collection)
  10966. (org-completion-use-ido nil) ; does not work with ido.
  10967. (org-completion-use-iswitchb nil)) ; or iswitchb
  10968. (apply #'org-icompleting-read
  10969. prompt
  10970. (lambda (string predicate &optional flag)
  10971. (cond
  10972. ((eq flag nil) (try-completion string thetable))
  10973. ((eq flag t)
  10974. (let ((l (length string)))
  10975. (mapcar (lambda (x)
  10976. (let ((r (substring x l))
  10977. (f (if (string-match " ([^)]*)$" x)
  10978. (match-string 0 x)
  10979. "")))
  10980. (if (string-match "/" r)
  10981. (concat string (substring r 0 (match-end 0)) f)
  10982. x)))
  10983. (all-completions string thetable predicate))))
  10984. ;; Exact match?
  10985. ((eq flag 'lambda) (assoc string thetable))))
  10986. args)))
  10987. ;;;; Dynamic blocks
  10988. (defun org-find-dblock (name)
  10989. "Find the first dynamic block with name NAME in the buffer.
  10990. If not found, stay at current position and return nil."
  10991. (let ((case-fold-search t) pos)
  10992. (save-excursion
  10993. (goto-char (point-min))
  10994. (setq pos (and (re-search-forward
  10995. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10996. (match-beginning 0))))
  10997. (if pos (goto-char pos))
  10998. pos))
  10999. (defun org-create-dblock (plist)
  11000. "Create a dynamic block section, with parameters taken from PLIST.
  11001. PLIST must contain a :name entry which is used as the name of the block."
  11002. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11003. (end-of-line 1)
  11004. (newline))
  11005. (let ((col (current-column))
  11006. (name (plist-get plist :name)))
  11007. (insert "#+BEGIN: " name)
  11008. (while plist
  11009. (if (eq (car plist) :name)
  11010. (setq plist (cddr plist))
  11011. (insert " " (prin1-to-string (pop plist)))))
  11012. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11013. (beginning-of-line -2)))
  11014. (defun org-prepare-dblock ()
  11015. "Prepare dynamic block for refresh.
  11016. This empties the block, puts the cursor at the insert position and returns
  11017. the property list including an extra property :name with the block name."
  11018. (unless (looking-at org-dblock-start-re)
  11019. (user-error "Not at a dynamic block"))
  11020. (let* ((begdel (1+ (match-end 0)))
  11021. (name (org-no-properties (match-string 1)))
  11022. (params (append (list :name name)
  11023. (read (concat "(" (match-string 3) ")")))))
  11024. (save-excursion
  11025. (beginning-of-line 1)
  11026. (skip-chars-forward " \t")
  11027. (setq params (plist-put params :indentation-column (current-column))))
  11028. (unless (re-search-forward org-dblock-end-re nil t)
  11029. (error "Dynamic block not terminated"))
  11030. (setq params
  11031. (append params
  11032. (list :content (buffer-substring
  11033. begdel (match-beginning 0)))))
  11034. (delete-region begdel (match-beginning 0))
  11035. (goto-char begdel)
  11036. (open-line 1)
  11037. params))
  11038. (defun org-map-dblocks (&optional command)
  11039. "Apply COMMAND to all dynamic blocks in the current buffer.
  11040. If COMMAND is not given, use `org-update-dblock'."
  11041. (let ((cmd (or command 'org-update-dblock)))
  11042. (save-excursion
  11043. (goto-char (point-min))
  11044. (while (re-search-forward org-dblock-start-re nil t)
  11045. (goto-char (match-beginning 0))
  11046. (save-excursion
  11047. (condition-case nil
  11048. (funcall cmd)
  11049. (error (message "Error during update of dynamic block"))))
  11050. (unless (re-search-forward org-dblock-end-re nil t)
  11051. (error "Dynamic block not terminated"))))))
  11052. (defun org-dblock-update (&optional arg)
  11053. "User command for updating dynamic blocks.
  11054. Update the dynamic block at point. With prefix ARG, update all dynamic
  11055. blocks in the buffer."
  11056. (interactive "P")
  11057. (if arg
  11058. (org-update-all-dblocks)
  11059. (or (looking-at org-dblock-start-re)
  11060. (org-beginning-of-dblock))
  11061. (org-update-dblock)))
  11062. (defun org-update-dblock ()
  11063. "Update the dynamic block at point.
  11064. This means to empty the block, parse for parameters and then call
  11065. the correct writing function."
  11066. (interactive)
  11067. (save-excursion
  11068. (let* ((win (selected-window))
  11069. (pos (point))
  11070. (line (org-current-line))
  11071. (params (org-prepare-dblock))
  11072. (name (plist-get params :name))
  11073. (indent (plist-get params :indentation-column))
  11074. (cmd (intern (concat "org-dblock-write:" name))))
  11075. (message "Updating dynamic block `%s' at line %d..." name line)
  11076. (funcall cmd params)
  11077. (message "Updating dynamic block `%s' at line %d...done" name line)
  11078. (goto-char pos)
  11079. (when (and indent (> indent 0))
  11080. (setq indent (make-string indent ?\ ))
  11081. (save-excursion
  11082. (select-window win)
  11083. (org-beginning-of-dblock)
  11084. (forward-line 1)
  11085. (while (not (looking-at org-dblock-end-re))
  11086. (insert indent)
  11087. (beginning-of-line 2))
  11088. (when (looking-at org-dblock-end-re)
  11089. (and (looking-at "[ \t]+")
  11090. (replace-match ""))
  11091. (insert indent)))))))
  11092. (defun org-beginning-of-dblock ()
  11093. "Find the beginning of the dynamic block at point.
  11094. Error if there is no such block at point."
  11095. (let ((pos (point))
  11096. beg)
  11097. (end-of-line 1)
  11098. (if (and (re-search-backward org-dblock-start-re nil t)
  11099. (setq beg (match-beginning 0))
  11100. (re-search-forward org-dblock-end-re nil t)
  11101. (> (match-end 0) pos))
  11102. (goto-char beg)
  11103. (goto-char pos)
  11104. (error "Not in a dynamic block"))))
  11105. (defun org-update-all-dblocks ()
  11106. "Update all dynamic blocks in the buffer.
  11107. This function can be used in a hook."
  11108. (interactive)
  11109. (when (derived-mode-p 'org-mode)
  11110. (org-map-dblocks 'org-update-dblock)))
  11111. ;;;; Completion
  11112. (declare-function org-export-backend-name "org-export" (cl-x))
  11113. (declare-function org-export-backend-options "org-export" (cl-x))
  11114. (defun org-get-export-keywords ()
  11115. "Return a list of all currently understood export keywords.
  11116. Export keywords include options, block names, attributes and
  11117. keywords relative to each registered export back-end."
  11118. (let (keywords)
  11119. (dolist (backend
  11120. (org-bound-and-true-p org-export-registered-backends)
  11121. (delq nil keywords))
  11122. ;; Back-end name (for keywords, like #+LATEX:)
  11123. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11124. (dolist (option-entry (org-export-backend-options backend))
  11125. ;; Back-end options.
  11126. (push (nth 1 option-entry) keywords)))))
  11127. (defconst org-options-keywords
  11128. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11129. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11130. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11131. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11132. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11133. (defcustom org-structure-template-alist
  11134. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11135. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11136. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11137. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11138. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11139. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11140. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11141. ("L" "#+LaTeX: ")
  11142. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11143. ("H" "#+HTML: ")
  11144. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11145. ("A" "#+ASCII: ")
  11146. ("i" "#+INDEX: ?")
  11147. ("I" "#+INCLUDE: %file ?"))
  11148. "Structure completion elements.
  11149. This is a list of abbreviation keys and values. The value gets inserted
  11150. if you type `<' followed by the key and then press the completion key,
  11151. usually `TAB'. %file will be replaced by a file name after prompting
  11152. for the file using completion. The cursor will be placed at the position
  11153. of the `?' in the template.
  11154. There are two templates for each key, the first uses the original Org syntax,
  11155. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11156. the default when the /org-mtags.el/ module has been loaded. See also the
  11157. variable `org-mtags-prefer-muse-templates'."
  11158. :group 'org-completion
  11159. :type '(repeat
  11160. (list
  11161. (string :tag "Key")
  11162. (string :tag "Template")))
  11163. :version "25.1"
  11164. :package-version '(Org . "8.3"))
  11165. (defun org-try-structure-completion ()
  11166. "Try to complete a structure template before point.
  11167. This looks for strings like \"<e\" on an otherwise empty line and
  11168. expands them."
  11169. (let ((l (buffer-substring (point-at-bol) (point)))
  11170. a)
  11171. (when (and (looking-at "[ \t]*$")
  11172. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11173. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11174. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11175. (match-beginning 1)) a)
  11176. t)))
  11177. (defun org-complete-expand-structure-template (start cell)
  11178. "Expand a structure template."
  11179. (let ((rpl (nth 1 cell))
  11180. (ind ""))
  11181. (delete-region start (point))
  11182. (when (string-match "\\`[ \t]*#\\+" rpl)
  11183. (cond
  11184. ((bolp))
  11185. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11186. (setq ind (buffer-substring (point-at-bol) (point))))
  11187. (t (newline))))
  11188. (setq start (point))
  11189. (if (string-match "%file" rpl)
  11190. (setq rpl (replace-match
  11191. (concat
  11192. "\""
  11193. (save-match-data
  11194. (abbreviate-file-name (read-file-name "Include file: ")))
  11195. "\"")
  11196. t t rpl)))
  11197. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11198. (concat "\n" ind)))
  11199. (insert rpl)
  11200. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11201. ;;;; TODO, DEADLINE, Comments
  11202. (defun org-toggle-comment ()
  11203. "Change the COMMENT state of an entry."
  11204. (interactive)
  11205. (save-excursion
  11206. (org-back-to-heading)
  11207. (looking-at org-complex-heading-regexp)
  11208. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11209. (skip-chars-forward " \t")
  11210. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11211. (if (org-in-commented-heading-p t)
  11212. (delete-region (point)
  11213. (progn (search-forward " " (line-end-position) 'move)
  11214. (skip-chars-forward " \t")
  11215. (point)))
  11216. (insert org-comment-string)
  11217. (unless (eolp) (insert " ")))))
  11218. (defvar org-last-todo-state-is-todo nil
  11219. "This is non-nil when the last TODO state change led to a TODO state.
  11220. If the last change removed the TODO tag or switched to DONE, then
  11221. this is nil.")
  11222. (defvar org-setting-tags nil) ; dynamically skipped
  11223. (defvar org-todo-setup-filter-hook nil
  11224. "Hook for functions that pre-filter todo specs.
  11225. Each function takes a todo spec and returns either nil or the spec
  11226. transformed into canonical form." )
  11227. (defvar org-todo-get-default-hook nil
  11228. "Hook for functions that get a default item for todo.
  11229. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11230. nil or a string to be used for the todo mark." )
  11231. (defvar org-agenda-headline-snapshot-before-repeat)
  11232. (defun org-current-effective-time ()
  11233. "Return current time adjusted for `org-extend-today-until' variable."
  11234. (let* ((ct (org-current-time))
  11235. (dct (decode-time ct))
  11236. (ct1
  11237. (cond
  11238. (org-use-last-clock-out-time-as-effective-time
  11239. (or (org-clock-get-last-clock-out-time) ct))
  11240. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11241. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11242. (t ct))))
  11243. ct1))
  11244. (defun org-todo-yesterday (&optional arg)
  11245. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11246. (interactive "P")
  11247. (if (eq major-mode 'org-agenda-mode)
  11248. (apply 'org-agenda-todo-yesterday arg)
  11249. (let* ((org-use-effective-time t)
  11250. (hour (third (decode-time
  11251. (org-current-time))))
  11252. (org-extend-today-until (1+ hour)))
  11253. (org-todo arg))))
  11254. (defvar org-block-entry-blocking ""
  11255. "First entry preventing the TODO state change.")
  11256. (defun org-cancel-repeater ()
  11257. "Cancel a repeater by setting its numeric value to zero."
  11258. (interactive)
  11259. (save-excursion
  11260. (org-back-to-heading t)
  11261. (let ((bound1 (point))
  11262. (bound0 (save-excursion (outline-next-heading) (point))))
  11263. (when (re-search-forward
  11264. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11265. org-deadline-time-regexp "\\)\\|\\("
  11266. org-ts-regexp "\\)")
  11267. bound0 t)
  11268. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11269. (replace-match "0" t nil nil 1))))))
  11270. (defun org-todo (&optional arg)
  11271. "Change the TODO state of an item.
  11272. The state of an item is given by a keyword at the start of the heading,
  11273. like
  11274. *** TODO Write paper
  11275. *** DONE Call mom
  11276. The different keywords are specified in the variable `org-todo-keywords'.
  11277. By default the available states are \"TODO\" and \"DONE\".
  11278. So for this example: when the item starts with TODO, it is changed to DONE.
  11279. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11280. DONE are present, add TODO at the beginning of the heading.
  11281. With \\[universal-argument] prefix arg, use completion to determine the new \
  11282. state.
  11283. With numeric prefix arg, switch to that state.
  11284. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11285. keywords (nextset).
  11286. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11287. With a numeric prefix arg of 0, inhibit note taking for the change.
  11288. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11289. When called through ELisp, arg is also interpreted in the following way:
  11290. 'none -> empty state
  11291. \"\"(empty string) -> switch to empty state
  11292. 'done -> switch to DONE
  11293. 'nextset -> switch to the next set of keywords
  11294. 'previousset -> switch to the previous set of keywords
  11295. \"WAITING\" -> switch to the specified keyword, but only if it
  11296. really is a member of `org-todo-keywords'."
  11297. (interactive "P")
  11298. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11299. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11300. 'region-start-level 'region))
  11301. org-loop-over-headlines-in-active-region)
  11302. (org-map-entries
  11303. `(org-todo ,arg)
  11304. org-loop-over-headlines-in-active-region
  11305. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11306. (if (equal arg '(16)) (setq arg 'nextset))
  11307. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11308. (let ((org-blocker-hook org-blocker-hook)
  11309. commentp
  11310. case-fold-search)
  11311. (when (equal arg '(64))
  11312. (setq arg nil org-blocker-hook nil))
  11313. (when (and org-blocker-hook
  11314. (or org-inhibit-blocking
  11315. (org-entry-get nil "NOBLOCKING")))
  11316. (setq org-blocker-hook nil))
  11317. (save-excursion
  11318. (catch 'exit
  11319. (org-back-to-heading t)
  11320. (when (org-in-commented-heading-p t)
  11321. (org-toggle-comment)
  11322. (setq commentp t))
  11323. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11324. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11325. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11326. (let* ((match-data (match-data))
  11327. (startpos (point-at-bol))
  11328. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11329. (org-log-done org-log-done)
  11330. (org-log-repeat org-log-repeat)
  11331. (org-todo-log-states org-todo-log-states)
  11332. (org-inhibit-logging
  11333. (if (equal arg 0)
  11334. (progn (setq arg nil) 'note) org-inhibit-logging))
  11335. (this (match-string 1))
  11336. (hl-pos (match-beginning 0))
  11337. (head (org-get-todo-sequence-head this))
  11338. (ass (assoc head org-todo-kwd-alist))
  11339. (interpret (nth 1 ass))
  11340. (done-word (nth 3 ass))
  11341. (final-done-word (nth 4 ass))
  11342. (org-last-state (or this ""))
  11343. (completion-ignore-case t)
  11344. (member (member this org-todo-keywords-1))
  11345. (tail (cdr member))
  11346. (org-state (cond
  11347. ((and org-todo-key-trigger
  11348. (or (and (equal arg '(4))
  11349. (eq org-use-fast-todo-selection 'prefix))
  11350. (and (not arg) org-use-fast-todo-selection
  11351. (not (eq org-use-fast-todo-selection
  11352. 'prefix)))))
  11353. ;; Use fast selection
  11354. (org-fast-todo-selection))
  11355. ((and (equal arg '(4))
  11356. (or (not org-use-fast-todo-selection)
  11357. (not org-todo-key-trigger)))
  11358. ;; Read a state with completion
  11359. (org-icompleting-read
  11360. "State: " (mapcar 'list org-todo-keywords-1)
  11361. nil t))
  11362. ((eq arg 'right)
  11363. (if this
  11364. (if tail (car tail) nil)
  11365. (car org-todo-keywords-1)))
  11366. ((eq arg 'left)
  11367. (if (equal member org-todo-keywords-1)
  11368. nil
  11369. (if this
  11370. (nth (- (length org-todo-keywords-1)
  11371. (length tail) 2)
  11372. org-todo-keywords-1)
  11373. (org-last org-todo-keywords-1))))
  11374. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11375. (setq arg nil))) ; hack to fall back to cycling
  11376. (arg
  11377. ;; user or caller requests a specific state
  11378. (cond
  11379. ((equal arg "") nil)
  11380. ((eq arg 'none) nil)
  11381. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11382. ((eq arg 'nextset)
  11383. (or (car (cdr (member head org-todo-heads)))
  11384. (car org-todo-heads)))
  11385. ((eq arg 'previousset)
  11386. (let ((org-todo-heads (reverse org-todo-heads)))
  11387. (or (car (cdr (member head org-todo-heads)))
  11388. (car org-todo-heads))))
  11389. ((car (member arg org-todo-keywords-1)))
  11390. ((stringp arg)
  11391. (user-error "State `%s' not valid in this file" arg))
  11392. ((nth (1- (prefix-numeric-value arg))
  11393. org-todo-keywords-1))))
  11394. ((null member) (or head (car org-todo-keywords-1)))
  11395. ((equal this final-done-word) nil) ;; -> make empty
  11396. ((null tail) nil) ;; -> first entry
  11397. ((memq interpret '(type priority))
  11398. (if (eq this-command last-command)
  11399. (car tail)
  11400. (if (> (length tail) 0)
  11401. (or done-word (car org-done-keywords))
  11402. nil)))
  11403. (t
  11404. (car tail))))
  11405. (org-state (or
  11406. (run-hook-with-args-until-success
  11407. 'org-todo-get-default-hook org-state org-last-state)
  11408. org-state))
  11409. (next (if org-state (concat " " org-state " ") " "))
  11410. (change-plist (list :type 'todo-state-change :from this :to org-state
  11411. :position startpos))
  11412. dolog now-done-p)
  11413. (when org-blocker-hook
  11414. (setq org-last-todo-state-is-todo
  11415. (not (member this org-done-keywords)))
  11416. (unless (save-excursion
  11417. (save-match-data
  11418. (org-with-wide-buffer
  11419. (run-hook-with-args-until-failure
  11420. 'org-blocker-hook change-plist))))
  11421. (if (org-called-interactively-p 'interactive)
  11422. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11423. this org-state org-block-entry-blocking)
  11424. ;; fail silently
  11425. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11426. this org-state org-block-entry-blocking)
  11427. (throw 'exit nil))))
  11428. (store-match-data match-data)
  11429. (replace-match next t t)
  11430. (cond ((equal this org-state)
  11431. (message "TODO state was already %s" (org-trim next)))
  11432. ((pos-visible-in-window-p hl-pos)
  11433. (message "TODO state changed to %s" (org-trim next))))
  11434. (unless head
  11435. (setq head (org-get-todo-sequence-head org-state)
  11436. ass (assoc head org-todo-kwd-alist)
  11437. interpret (nth 1 ass)
  11438. done-word (nth 3 ass)
  11439. final-done-word (nth 4 ass)))
  11440. (when (memq arg '(nextset previousset))
  11441. (message "Keyword-Set %d/%d: %s"
  11442. (- (length org-todo-sets) -1
  11443. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11444. (length org-todo-sets)
  11445. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11446. (setq org-last-todo-state-is-todo
  11447. (not (member org-state org-done-keywords)))
  11448. (setq now-done-p (and (member org-state org-done-keywords)
  11449. (not (member this org-done-keywords))))
  11450. (and logging (org-local-logging logging))
  11451. (when (and (or org-todo-log-states org-log-done)
  11452. (not (eq org-inhibit-logging t))
  11453. (not (memq arg '(nextset previousset))))
  11454. ;; we need to look at recording a time and note
  11455. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11456. (nth 2 (assoc this org-todo-log-states))))
  11457. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11458. (setq dolog 'time))
  11459. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11460. (and org-state
  11461. (member org-state org-not-done-keywords)
  11462. (not (member this org-not-done-keywords))))
  11463. ;; This is now a todo state and was not one before
  11464. ;; If there was a CLOSED time stamp, get rid of it.
  11465. (org-add-planning-info nil nil 'closed))
  11466. (when (and now-done-p org-log-done)
  11467. ;; It is now done, and it was not done before
  11468. (org-add-planning-info 'closed (org-current-effective-time))
  11469. (if (and (not dolog) (eq 'note org-log-done))
  11470. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11471. (when (and org-state dolog)
  11472. ;; This is a non-nil state, and we need to log it
  11473. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11474. ;; Fixup tag positioning
  11475. (org-todo-trigger-tag-changes org-state)
  11476. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11477. (when org-provide-todo-statistics
  11478. (org-update-parent-todo-statistics))
  11479. (run-hooks 'org-after-todo-state-change-hook)
  11480. (if (and arg (not (member org-state org-done-keywords)))
  11481. (setq head (org-get-todo-sequence-head org-state)))
  11482. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11483. ;; Do we need to trigger a repeat?
  11484. (when now-done-p
  11485. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11486. ;; This is for the agenda, take a snapshot of the headline.
  11487. (save-match-data
  11488. (setq org-agenda-headline-snapshot-before-repeat
  11489. (org-get-heading))))
  11490. (org-auto-repeat-maybe org-state))
  11491. ;; Fixup cursor location if close to the keyword
  11492. (if (and (outline-on-heading-p)
  11493. (not (bolp))
  11494. (save-excursion (beginning-of-line 1)
  11495. (looking-at org-todo-line-regexp))
  11496. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11497. (progn
  11498. (goto-char (or (match-end 2) (match-end 1)))
  11499. (and (looking-at " ") (just-one-space))))
  11500. (when org-trigger-hook
  11501. (save-excursion
  11502. (run-hook-with-args 'org-trigger-hook change-plist)))
  11503. (when commentp (org-toggle-comment))))))))
  11504. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11505. "Block turning an entry into a TODO, using the hierarchy.
  11506. This checks whether the current task should be blocked from state
  11507. changes. Such blocking occurs when:
  11508. 1. The task has children which are not all in a completed state.
  11509. 2. A task has a parent with the property :ORDERED:, and there
  11510. are siblings prior to the current task with incomplete
  11511. status.
  11512. 3. The parent of the task is blocked because it has siblings that should
  11513. be done first, or is child of a block grandparent TODO entry."
  11514. (if (not org-enforce-todo-dependencies)
  11515. t ; if locally turned off don't block
  11516. (catch 'dont-block
  11517. ;; If this is not a todo state change, or if this entry is already DONE,
  11518. ;; do not block
  11519. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11520. (member (plist-get change-plist :from)
  11521. (cons 'done org-done-keywords))
  11522. (member (plist-get change-plist :to)
  11523. (cons 'todo org-not-done-keywords))
  11524. (not (plist-get change-plist :to)))
  11525. (throw 'dont-block t))
  11526. ;; If this task has children, and any are undone, it's blocked
  11527. (save-excursion
  11528. (org-back-to-heading t)
  11529. (let ((this-level (funcall outline-level)))
  11530. (outline-next-heading)
  11531. (let ((child-level (funcall outline-level)))
  11532. (while (and (not (eobp))
  11533. (> child-level this-level))
  11534. ;; this todo has children, check whether they are all
  11535. ;; completed
  11536. (if (and (not (org-entry-is-done-p))
  11537. (org-entry-is-todo-p))
  11538. (progn (setq org-block-entry-blocking (org-get-heading))
  11539. (throw 'dont-block nil)))
  11540. (outline-next-heading)
  11541. (setq child-level (funcall outline-level))))))
  11542. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11543. ;; any previous siblings are undone, it's blocked
  11544. (save-excursion
  11545. (org-back-to-heading t)
  11546. (let* ((pos (point))
  11547. (parent-pos (and (org-up-heading-safe) (point))))
  11548. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11549. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11550. (forward-line 1)
  11551. (re-search-forward org-not-done-heading-regexp pos t))
  11552. (setq org-block-entry-blocking (match-string 0))
  11553. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11554. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11555. (while t
  11556. (goto-char parent-pos)
  11557. (if (not (looking-at org-not-done-heading-regexp))
  11558. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11559. (setq pos (point))
  11560. (setq parent-pos (and (org-up-heading-safe) (point)))
  11561. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11562. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11563. (forward-line 1)
  11564. (re-search-forward org-not-done-heading-regexp pos t)
  11565. (setq org-block-entry-blocking (org-get-heading)))
  11566. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11567. (defcustom org-track-ordered-property-with-tag nil
  11568. "Should the ORDERED property also be shown as a tag?
  11569. The ORDERED property decides if an entry should require subtasks to be
  11570. completed in sequence. Since a property is not very visible, setting
  11571. this option means that toggling the ORDERED property with the command
  11572. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11573. not relevant for the behavior, but it makes things more visible.
  11574. Note that toggling the tag with tags commands will not change the property
  11575. and therefore not influence behavior!
  11576. This can be t, meaning the tag ORDERED should be used, It can also be a
  11577. string to select a different tag for this task."
  11578. :group 'org-todo
  11579. :type '(choice
  11580. (const :tag "No tracking" nil)
  11581. (const :tag "Track with ORDERED tag" t)
  11582. (string :tag "Use other tag")))
  11583. (defun org-toggle-ordered-property ()
  11584. "Toggle the ORDERED property of the current entry.
  11585. For better visibility, you can track the value of this property with a tag.
  11586. See variable `org-track-ordered-property-with-tag'."
  11587. (interactive)
  11588. (let* ((t1 org-track-ordered-property-with-tag)
  11589. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11590. (save-excursion
  11591. (org-back-to-heading)
  11592. (if (org-entry-get nil "ORDERED")
  11593. (progn
  11594. (org-delete-property "ORDERED")
  11595. (and tag (org-toggle-tag tag 'off))
  11596. (message "Subtasks can be completed in arbitrary order"))
  11597. (org-entry-put nil "ORDERED" "t")
  11598. (and tag (org-toggle-tag tag 'on))
  11599. (message "Subtasks must be completed in sequence")))))
  11600. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11601. (defun org-block-todo-from-checkboxes (change-plist)
  11602. "Block turning an entry into a TODO, using checkboxes.
  11603. This checks whether the current task should be blocked from state
  11604. changes because there are unchecked boxes in this entry."
  11605. (if (not org-enforce-todo-checkbox-dependencies)
  11606. t ; if locally turned off don't block
  11607. (catch 'dont-block
  11608. ;; If this is not a todo state change, or if this entry is already DONE,
  11609. ;; do not block
  11610. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11611. (member (plist-get change-plist :from)
  11612. (cons 'done org-done-keywords))
  11613. (member (plist-get change-plist :to)
  11614. (cons 'todo org-not-done-keywords))
  11615. (not (plist-get change-plist :to)))
  11616. (throw 'dont-block t))
  11617. ;; If this task has checkboxes that are not checked, it's blocked
  11618. (save-excursion
  11619. (org-back-to-heading t)
  11620. (let ((beg (point)) end)
  11621. (outline-next-heading)
  11622. (setq end (point))
  11623. (goto-char beg)
  11624. (if (org-list-search-forward
  11625. (concat (org-item-beginning-re)
  11626. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11627. "\\[[- ]\\]")
  11628. end t)
  11629. (progn
  11630. (if (boundp 'org-blocked-by-checkboxes)
  11631. (setq org-blocked-by-checkboxes t))
  11632. (throw 'dont-block nil)))))
  11633. t))) ; do not block
  11634. (defun org-entry-blocked-p ()
  11635. "Non-nil if entry at point is blocked."
  11636. (and (not (org-entry-get nil "NOBLOCKING"))
  11637. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11638. (not (run-hook-with-args-until-failure
  11639. 'org-blocker-hook
  11640. (list :type 'todo-state-change
  11641. :position (point)
  11642. :from 'todo
  11643. :to 'done)))))
  11644. (defun org-update-statistics-cookies (all)
  11645. "Update the statistics cookie, either from TODO or from checkboxes.
  11646. This should be called with the cursor in a line with a statistics cookie."
  11647. (interactive "P")
  11648. (if all
  11649. (progn
  11650. (org-update-checkbox-count 'all)
  11651. (org-map-entries 'org-update-parent-todo-statistics))
  11652. (if (not (org-at-heading-p))
  11653. (org-update-checkbox-count)
  11654. (let ((pos (point-marker))
  11655. end l1 l2)
  11656. (ignore-errors (org-back-to-heading t))
  11657. (if (not (org-at-heading-p))
  11658. (org-update-checkbox-count)
  11659. (setq l1 (org-outline-level))
  11660. (setq end (save-excursion
  11661. (outline-next-heading)
  11662. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11663. (point)))
  11664. (if (and (save-excursion
  11665. (re-search-forward
  11666. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11667. (not (save-excursion (re-search-forward
  11668. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11669. (org-update-checkbox-count)
  11670. (if (and l2 (> l2 l1))
  11671. (progn
  11672. (goto-char end)
  11673. (org-update-parent-todo-statistics))
  11674. (goto-char pos)
  11675. (beginning-of-line 1)
  11676. (while (re-search-forward
  11677. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11678. (point-at-eol) t)
  11679. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11680. (goto-char pos)
  11681. (move-marker pos nil)))))
  11682. (defvar org-entry-property-inherited-from) ;; defined below
  11683. (defun org-update-parent-todo-statistics ()
  11684. "Update any statistics cookie in the parent of the current headline.
  11685. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11686. the entire subtree and this will travel up the hierarchy and update
  11687. statistics everywhere."
  11688. (let* ((prop (save-excursion (org-up-heading-safe)
  11689. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11690. (recursive (or (not org-hierarchical-todo-statistics)
  11691. (and prop (string-match "\\<recursive\\>" prop))))
  11692. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11693. 0))
  11694. (first t)
  11695. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11696. level ltoggle l1 new ndel
  11697. (cnt-all 0) (cnt-done 0) is-percent kwd
  11698. checkbox-beg ov ovs ove cookie-present)
  11699. (catch 'exit
  11700. (save-excursion
  11701. (beginning-of-line 1)
  11702. (setq ltoggle (funcall outline-level))
  11703. ;; Three situations are to consider:
  11704. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11705. ;; to the top-level ancestor on the headline;
  11706. ;; 2. If parent has "recursive" property, repeat up to the
  11707. ;; headline setting that property, taking inheritance into
  11708. ;; account;
  11709. ;; 3. Else, move up to direct parent and proceed only once.
  11710. (while (and (setq level (org-up-heading-safe))
  11711. (or recursive first)
  11712. (>= (point) lim))
  11713. (setq first nil cookie-present nil)
  11714. (unless (and level
  11715. (not (string-match
  11716. "\\<checkbox\\>"
  11717. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11718. "")))))
  11719. (throw 'exit nil))
  11720. (while (re-search-forward box-re (point-at-eol) t)
  11721. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11722. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11723. (save-match-data
  11724. (unless (outline-next-heading) (throw 'exit nil))
  11725. (while (and (looking-at org-complex-heading-regexp)
  11726. (> (setq l1 (length (match-string 1))) level))
  11727. (setq kwd (and (or recursive (= l1 ltoggle))
  11728. (match-string 2)))
  11729. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11730. (and (eq org-provide-todo-statistics t)
  11731. (or (member kwd org-done-keywords)))
  11732. (and (listp org-provide-todo-statistics)
  11733. (stringp (car org-provide-todo-statistics))
  11734. (or (member kwd org-provide-todo-statistics)
  11735. (member kwd org-done-keywords)))
  11736. (and (listp org-provide-todo-statistics)
  11737. (listp (car org-provide-todo-statistics))
  11738. (or (member kwd (car org-provide-todo-statistics))
  11739. (and (member kwd org-done-keywords)
  11740. (member kwd (cadr org-provide-todo-statistics))))))
  11741. (setq cnt-all (1+ cnt-all))
  11742. (if (eq org-provide-todo-statistics t)
  11743. (and kwd (setq cnt-all (1+ cnt-all)))))
  11744. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11745. (member kwd org-done-keywords))
  11746. (and (listp org-provide-todo-statistics)
  11747. (listp (car org-provide-todo-statistics))
  11748. (member kwd org-done-keywords)
  11749. (member kwd (cadr org-provide-todo-statistics)))
  11750. (and (listp org-provide-todo-statistics)
  11751. (stringp (car org-provide-todo-statistics))
  11752. (member kwd org-done-keywords)))
  11753. (setq cnt-done (1+ cnt-done)))
  11754. (outline-next-heading)))
  11755. (setq new
  11756. (if is-percent
  11757. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11758. (max 1 cnt-all)))
  11759. (format "[%d/%d]" cnt-done cnt-all))
  11760. ndel (- (match-end 0) checkbox-beg))
  11761. ;; handle overlays when updating cookie from column view
  11762. (when (setq ov (car (overlays-at checkbox-beg)))
  11763. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11764. (delete-overlay ov))
  11765. (goto-char checkbox-beg)
  11766. (insert new)
  11767. (delete-region (point) (+ (point) ndel))
  11768. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11769. (when ov (move-overlay ov ovs ove)))
  11770. (when cookie-present
  11771. (run-hook-with-args 'org-after-todo-statistics-hook
  11772. cnt-done (- cnt-all cnt-done))))))
  11773. (run-hooks 'org-todo-statistics-hook)))
  11774. (defvar org-after-todo-statistics-hook nil
  11775. "Hook that is called after a TODO statistics cookie has been updated.
  11776. Each function is called with two arguments: the number of not-done entries
  11777. and the number of done entries.
  11778. For example, the following function, when added to this hook, will switch
  11779. an entry to DONE when all children are done, and back to TODO when new
  11780. entries are set to a TODO status. Note that this hook is only called
  11781. when there is a statistics cookie in the headline!
  11782. (defun org-summary-todo (n-done n-not-done)
  11783. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11784. (let (org-log-done org-log-states) ; turn off logging
  11785. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11786. ")
  11787. (defvar org-todo-statistics-hook nil
  11788. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11789. This hook runs even if there is no statistics cookie present, in which case
  11790. `org-after-todo-statistics-hook' would not run.")
  11791. (defun org-todo-trigger-tag-changes (state)
  11792. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11793. (let ((l org-todo-state-tags-triggers)
  11794. changes)
  11795. (when (or (not state) (equal state ""))
  11796. (setq changes (append changes (cdr (assoc "" l)))))
  11797. (when (and (stringp state) (> (length state) 0))
  11798. (setq changes (append changes (cdr (assoc state l)))))
  11799. (when (member state org-not-done-keywords)
  11800. (setq changes (append changes (cdr (assoc 'todo l)))))
  11801. (when (member state org-done-keywords)
  11802. (setq changes (append changes (cdr (assoc 'done l)))))
  11803. (dolist (c changes)
  11804. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11805. (defun org-local-logging (value)
  11806. "Get logging settings from a property VALUE."
  11807. ;; Directly set the variables, they are already local.
  11808. (setq org-log-done nil
  11809. org-log-repeat nil
  11810. org-todo-log-states nil)
  11811. (dolist (w (org-split-string value))
  11812. (let (a)
  11813. (cond
  11814. ((setq a (assoc w org-startup-options))
  11815. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11816. (set (nth 1 a) (nth 2 a))))
  11817. ((setq a (org-extract-log-state-settings w))
  11818. (and (member (car a) org-todo-keywords-1)
  11819. (push a org-todo-log-states)))))))
  11820. (defun org-get-todo-sequence-head (kwd)
  11821. "Return the head of the TODO sequence to which KWD belongs.
  11822. If KWD is not set, check if there is a text property remembering the
  11823. right sequence."
  11824. (let (p)
  11825. (cond
  11826. ((not kwd)
  11827. (or (get-text-property (point-at-bol) 'org-todo-head)
  11828. (progn
  11829. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11830. nil (point-at-eol)))
  11831. (get-text-property p 'org-todo-head))))
  11832. ((not (member kwd org-todo-keywords-1))
  11833. (car org-todo-keywords-1))
  11834. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11835. (defun org-fast-todo-selection ()
  11836. "Fast TODO keyword selection with single keys.
  11837. Returns the new TODO keyword, or nil if no state change should occur."
  11838. (let* ((fulltable org-todo-key-alist)
  11839. (done-keywords org-done-keywords) ;; needed for the faces.
  11840. (maxlen (apply 'max (mapcar
  11841. (lambda (x)
  11842. (if (stringp (car x)) (string-width (car x)) 0))
  11843. fulltable)))
  11844. (expert nil)
  11845. (fwidth (+ maxlen 3 1 3))
  11846. (ncol (/ (- (window-width) 4) fwidth))
  11847. tg cnt e c tbl
  11848. groups ingroup)
  11849. (save-excursion
  11850. (save-window-excursion
  11851. (if expert
  11852. (set-buffer (get-buffer-create " *Org todo*"))
  11853. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11854. (erase-buffer)
  11855. (org-set-local 'org-done-keywords done-keywords)
  11856. (setq tbl fulltable cnt 0)
  11857. (while (setq e (pop tbl))
  11858. (cond
  11859. ((equal e '(:startgroup))
  11860. (push '() groups) (setq ingroup t)
  11861. (when (not (= cnt 0))
  11862. (setq cnt 0)
  11863. (insert "\n"))
  11864. (insert "{ "))
  11865. ((equal e '(:endgroup))
  11866. (setq ingroup nil cnt 0)
  11867. (insert "}\n"))
  11868. ((equal e '(:newline))
  11869. (when (not (= cnt 0))
  11870. (setq cnt 0)
  11871. (insert "\n")
  11872. (setq e (car tbl))
  11873. (while (equal (car tbl) '(:newline))
  11874. (insert "\n")
  11875. (setq tbl (cdr tbl)))))
  11876. (t
  11877. (setq tg (car e) c (cdr e))
  11878. (if ingroup (push tg (car groups)))
  11879. (setq tg (org-add-props tg nil 'face
  11880. (org-get-todo-face tg)))
  11881. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11882. (insert "[" c "] " tg (make-string
  11883. (- fwidth 4 (length tg)) ?\ ))
  11884. (when (= (setq cnt (1+ cnt)) ncol)
  11885. (insert "\n")
  11886. (if ingroup (insert " "))
  11887. (setq cnt 0)))))
  11888. (insert "\n")
  11889. (goto-char (point-min))
  11890. (if (not expert) (org-fit-window-to-buffer))
  11891. (message "[a-z..]:Set [SPC]:clear")
  11892. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11893. (cond
  11894. ((or (= c ?\C-g)
  11895. (and (= c ?q) (not (rassoc c fulltable))))
  11896. (setq quit-flag t))
  11897. ((= c ?\ ) nil)
  11898. ((setq e (rassoc c fulltable) tg (car e))
  11899. tg)
  11900. (t (setq quit-flag t)))))))
  11901. (defun org-entry-is-todo-p ()
  11902. (member (org-get-todo-state) org-not-done-keywords))
  11903. (defun org-entry-is-done-p ()
  11904. (member (org-get-todo-state) org-done-keywords))
  11905. (defun org-get-todo-state ()
  11906. "Return the TODO keyword of the current subtree."
  11907. (save-excursion
  11908. (org-back-to-heading t)
  11909. (and (looking-at org-todo-line-regexp)
  11910. (match-end 2)
  11911. (match-string 2))))
  11912. (defun org-at-date-range-p (&optional inactive-ok)
  11913. "Non-nil if point is inside a date range.
  11914. When optional argument INACTIVE-OK is non-nil, also consider
  11915. inactive time ranges.
  11916. When this function returns a non-nil value, match data is set
  11917. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11918. on INACTIVE-OK."
  11919. (interactive)
  11920. (save-excursion
  11921. (catch 'exit
  11922. (let ((pos (point)))
  11923. (skip-chars-backward "^[<\r\n")
  11924. (skip-chars-backward "<[")
  11925. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11926. (>= (match-end 0) pos)
  11927. (throw 'exit t))
  11928. (skip-chars-backward "^<[\r\n")
  11929. (skip-chars-backward "<[")
  11930. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11931. (>= (match-end 0) pos)
  11932. (throw 'exit t)))
  11933. nil)))
  11934. (defun org-get-repeat (&optional tagline)
  11935. "Check if there is a deadline/schedule with repeater in this entry."
  11936. (save-match-data
  11937. (save-excursion
  11938. (org-back-to-heading t)
  11939. (and (re-search-forward (if tagline
  11940. (concat tagline "\\s-*" org-repeat-re)
  11941. org-repeat-re)
  11942. (org-entry-end-position) t)
  11943. (match-string-no-properties 1)))))
  11944. (defvar org-last-changed-timestamp)
  11945. (defvar org-last-inserted-timestamp)
  11946. (defvar org-log-post-message)
  11947. (defvar org-log-note-purpose)
  11948. (defvar org-log-note-how nil)
  11949. (defvar org-log-note-extra)
  11950. (defun org-auto-repeat-maybe (done-word)
  11951. "Check if the current headline contains a repeated deadline/schedule.
  11952. If yes, set TODO state back to what it was and change the base date
  11953. of repeating deadline/scheduled time stamps to new date.
  11954. This function is run automatically after each state change to a DONE state."
  11955. ;; last-state is dynamically scoped into this function
  11956. (let* ((repeat (org-get-repeat))
  11957. (aa (assoc org-last-state org-todo-kwd-alist))
  11958. (interpret (nth 1 aa))
  11959. (head (nth 2 aa))
  11960. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11961. (msg "Entry repeats: ")
  11962. (org-log-done nil)
  11963. (org-todo-log-states nil)
  11964. re type n what ts time to-state)
  11965. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11966. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11967. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11968. org-todo-repeat-to-state))
  11969. (unless (and to-state (member to-state org-todo-keywords-1))
  11970. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11971. (org-todo to-state)
  11972. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11973. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11974. (org-time-stamp-format t t))))
  11975. (when org-log-repeat
  11976. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11977. (memq 'org-add-log-note post-command-hook))
  11978. ;; OK, we are already setup for some record
  11979. (if (eq org-log-repeat 'note)
  11980. ;; make sure we take a note, not only a time stamp
  11981. (setq org-log-note-how 'note))
  11982. ;; Set up for taking a record
  11983. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11984. org-last-state
  11985. 'findpos org-log-repeat)))
  11986. (org-back-to-heading t)
  11987. (org-add-planning-info nil nil 'closed)
  11988. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11989. org-deadline-time-regexp "\\)\\|\\("
  11990. org-ts-regexp "\\)"))
  11991. (while (re-search-forward
  11992. re (save-excursion (outline-next-heading) (point)) t)
  11993. (setq type (if (match-end 1) org-scheduled-string
  11994. (if (match-end 3) org-deadline-string "Plain:"))
  11995. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11996. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11997. (setq n (string-to-number (match-string 2 ts))
  11998. what (match-string 3 ts))
  11999. (if (equal what "w") (setq n (* n 7) what "d"))
  12000. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12001. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12002. ;; Preparation, see if we need to modify the start date for the change
  12003. (when (match-end 1)
  12004. (setq time (save-match-data (org-time-string-to-time ts)))
  12005. (cond
  12006. ((equal (match-string 1 ts) ".")
  12007. ;; Shift starting date to today
  12008. (org-timestamp-change
  12009. (- (org-today) (time-to-days time))
  12010. 'day))
  12011. ((equal (match-string 1 ts) "+")
  12012. (let ((nshiftmax 10) (nshift 0))
  12013. (while (or (= nshift 0)
  12014. (<= (time-to-days time)
  12015. (time-to-days (current-time))))
  12016. (when (= (incf nshift) nshiftmax)
  12017. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12018. (user-error "Abort")))
  12019. (org-timestamp-change n (cdr (assoc what whata)))
  12020. (org-at-timestamp-p t)
  12021. (setq ts (match-string 1))
  12022. (setq time (save-match-data (org-time-string-to-time ts)))))
  12023. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12024. ;; rematch, so that we have everything in place for the real shift
  12025. (org-at-timestamp-p t)
  12026. (setq ts (match-string 1))
  12027. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12028. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12029. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12030. (setq org-log-post-message msg)
  12031. (message "%s" msg))))
  12032. (defun org-show-todo-tree (arg)
  12033. "Make a compact tree which shows all headlines marked with TODO.
  12034. The tree will show the lines where the regexp matches, and all higher
  12035. headlines above the match.
  12036. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12037. With a numeric prefix N, construct a sparse tree for the Nth element
  12038. of `org-todo-keywords-1'."
  12039. (interactive "P")
  12040. (let ((case-fold-search nil)
  12041. (kwd-re
  12042. (cond ((null arg) org-not-done-regexp)
  12043. ((equal arg '(4))
  12044. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12045. (mapcar 'list org-todo-keywords-1))))
  12046. (concat "\\("
  12047. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12048. "\\)\\>")))
  12049. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12050. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12051. org-todo-keywords-1)))
  12052. (t (user-error "Invalid prefix argument: %s" arg)))))
  12053. (message "%d TODO entries found"
  12054. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12055. (defun org-deadline (arg &optional time)
  12056. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12057. With one universal prefix argument, remove any deadline from the item.
  12058. With two universal prefix arguments, prompt for a warning delay.
  12059. With argument TIME, set the deadline at the corresponding date. TIME
  12060. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12061. (interactive "P")
  12062. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12063. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12064. 'region-start-level 'region))
  12065. org-loop-over-headlines-in-active-region)
  12066. (org-map-entries
  12067. `(org-deadline ',arg ,time)
  12068. org-loop-over-headlines-in-active-region
  12069. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12070. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12071. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12072. (repeater (and old-date
  12073. (string-match
  12074. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12075. old-date)
  12076. (match-string 1 old-date))))
  12077. (cond
  12078. ((equal arg '(4))
  12079. (when (and old-date org-log-redeadline)
  12080. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12081. org-log-redeadline))
  12082. (org-remove-timestamp-with-keyword org-deadline-string)
  12083. (message "Item no longer has a deadline."))
  12084. ((equal arg '(16))
  12085. (save-excursion
  12086. (org-back-to-heading t)
  12087. (if (re-search-forward
  12088. org-deadline-time-regexp
  12089. (save-excursion (outline-next-heading) (point)) t)
  12090. (let* ((rpl0 (match-string 1))
  12091. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12092. (replace-match
  12093. (concat org-deadline-string
  12094. " <" rpl
  12095. (format " -%dd"
  12096. (abs
  12097. (- (time-to-days
  12098. (save-match-data
  12099. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12100. (time-to-days old-date-time))))
  12101. ">") t t))
  12102. (user-error "No deadline information to update"))))
  12103. (t
  12104. (org-add-planning-info 'deadline time 'closed)
  12105. (when (and old-date org-log-redeadline
  12106. (not (equal old-date
  12107. (substring org-last-inserted-timestamp 1 -1))))
  12108. (org-add-log-setup 'redeadline nil old-date 'findpos
  12109. org-log-redeadline))
  12110. (when repeater
  12111. (save-excursion
  12112. (org-back-to-heading t)
  12113. (when (re-search-forward (concat org-deadline-string " "
  12114. org-last-inserted-timestamp)
  12115. (save-excursion
  12116. (outline-next-heading) (point)) t)
  12117. (goto-char (1- (match-end 0)))
  12118. (insert " " repeater)
  12119. (setq org-last-inserted-timestamp
  12120. (concat (substring org-last-inserted-timestamp 0 -1)
  12121. " " repeater
  12122. (substring org-last-inserted-timestamp -1))))))
  12123. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12124. (defun org-schedule (arg &optional time)
  12125. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12126. With one universal prefix argument, remove any scheduling date from the item.
  12127. With two universal prefix arguments, prompt for a delay cookie.
  12128. With argument TIME, scheduled at the corresponding date. TIME can
  12129. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12130. (interactive "P")
  12131. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12132. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12133. 'region-start-level 'region))
  12134. org-loop-over-headlines-in-active-region)
  12135. (org-map-entries
  12136. `(org-schedule ',arg ,time)
  12137. org-loop-over-headlines-in-active-region
  12138. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12139. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12140. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12141. (repeater (and old-date
  12142. (string-match
  12143. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12144. old-date)
  12145. (match-string 1 old-date))))
  12146. (cond
  12147. ((equal arg '(4))
  12148. (progn
  12149. (when (and old-date org-log-reschedule)
  12150. (org-add-log-setup 'delschedule nil old-date 'findpos
  12151. org-log-reschedule))
  12152. (org-remove-timestamp-with-keyword org-scheduled-string)
  12153. (message "Item is no longer scheduled.")))
  12154. ((equal arg '(16))
  12155. (save-excursion
  12156. (org-back-to-heading t)
  12157. (if (re-search-forward
  12158. org-scheduled-time-regexp
  12159. (save-excursion (outline-next-heading) (point)) t)
  12160. (let* ((rpl0 (match-string 1))
  12161. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12162. (replace-match
  12163. (concat org-scheduled-string
  12164. " <" rpl
  12165. (format " -%dd"
  12166. (abs
  12167. (- (time-to-days
  12168. (save-match-data
  12169. (org-read-date nil t nil "Delay until" old-date-time)))
  12170. (time-to-days old-date-time))))
  12171. ">") t t))
  12172. (user-error "No scheduled information to update"))))
  12173. (t
  12174. (org-add-planning-info 'scheduled time 'closed)
  12175. (when (and old-date org-log-reschedule
  12176. (not (equal old-date
  12177. (substring org-last-inserted-timestamp 1 -1))))
  12178. (org-add-log-setup 'reschedule nil old-date 'findpos
  12179. org-log-reschedule))
  12180. (when repeater
  12181. (save-excursion
  12182. (org-back-to-heading t)
  12183. (when (re-search-forward (concat org-scheduled-string " "
  12184. org-last-inserted-timestamp)
  12185. (save-excursion
  12186. (outline-next-heading) (point)) t)
  12187. (goto-char (1- (match-end 0)))
  12188. (insert " " repeater)
  12189. (setq org-last-inserted-timestamp
  12190. (concat (substring org-last-inserted-timestamp 0 -1)
  12191. " " repeater
  12192. (substring org-last-inserted-timestamp -1))))))
  12193. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12194. (defun org-get-scheduled-time (pom &optional inherit)
  12195. "Get the scheduled time as a time tuple, of a format suitable
  12196. for calling org-schedule with, or if there is no scheduling,
  12197. returns nil."
  12198. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12199. (when time
  12200. (apply 'encode-time (org-parse-time-string time)))))
  12201. (defun org-get-deadline-time (pom &optional inherit)
  12202. "Get the deadline as a time tuple, of a format suitable for
  12203. calling org-deadline with, or if there is no scheduling, returns
  12204. nil."
  12205. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12206. (when time
  12207. (apply 'encode-time (org-parse-time-string time)))))
  12208. (defun org-remove-timestamp-with-keyword (keyword)
  12209. "Remove all time stamps with KEYWORD in the current entry."
  12210. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12211. beg)
  12212. (save-excursion
  12213. (org-back-to-heading t)
  12214. (setq beg (point))
  12215. (outline-next-heading)
  12216. (while (re-search-backward re beg t)
  12217. (replace-match "")
  12218. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12219. (equal (char-before) ?\ ))
  12220. (backward-delete-char 1)
  12221. (if (string-match "^[ \t]*$" (buffer-substring
  12222. (point-at-bol) (point-at-eol)))
  12223. (delete-region (point-at-bol)
  12224. (min (point-max) (1+ (point-at-eol))))))))))
  12225. (defvar org-time-was-given) ; dynamically scoped parameter
  12226. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12227. (defun org-at-planning-p ()
  12228. "Non-nil when point is on a planning info line."
  12229. ;; This is as accurate and faster than `org-element-at-point' since
  12230. ;; planning info location is fixed in the section.
  12231. (org-with-wide-buffer
  12232. (beginning-of-line)
  12233. (and (org-looking-at-p org-planning-line-re)
  12234. (eq (point)
  12235. (ignore-errors
  12236. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12237. (org-back-to-heading t)
  12238. (org-with-limited-levels (org-back-to-heading t)))
  12239. (line-beginning-position 2))))))
  12240. (defun org-add-planning-info (what &optional time &rest remove)
  12241. "Insert new timestamp with keyword in the planning line.
  12242. WHAT indicates what kind of time stamp to add. It is a symbol
  12243. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12244. the time to use. If none is given, the user is prompted for
  12245. a date. REMOVE indicates what kind of entries to remove. An old
  12246. WHAT entry will also be removed."
  12247. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12248. (catch 'exit
  12249. (when (and (memq what '(scheduled deadline))
  12250. (or (not time)
  12251. (and (stringp time)
  12252. (string-match "^[-+]+[0-9]" time))))
  12253. ;; Try to get a default date/time from existing timestamp
  12254. (save-excursion
  12255. (org-back-to-heading t)
  12256. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12257. (when (re-search-forward (if (eq what 'scheduled)
  12258. org-scheduled-time-regexp
  12259. org-deadline-time-regexp)
  12260. end t)
  12261. (setq ts (match-string 1)
  12262. default-time (apply 'encode-time (org-parse-time-string ts))
  12263. default-input (and ts (org-get-compact-tod ts)))))))
  12264. (when what
  12265. (setq time
  12266. (if (stringp time)
  12267. ;; This is a string (relative or absolute), set
  12268. ;; proper date.
  12269. (apply #'encode-time
  12270. (org-read-date-analyze
  12271. time default-time (decode-time default-time)))
  12272. ;; If necessary, get the time from the user
  12273. (or time (org-read-date nil 'to-time nil nil
  12274. default-time default-input)))))
  12275. (org-with-wide-buffer
  12276. (org-back-to-heading t)
  12277. (forward-line)
  12278. (unless (bolp) (insert "\n"))
  12279. (cond ((org-looking-at-p org-planning-line-re)
  12280. ;; Move to current indentation.
  12281. (skip-chars-forward " \t")
  12282. ;; Check if we have to remove something.
  12283. (dolist (type (if what (cons what remove) remove))
  12284. (save-excursion
  12285. (when (re-search-forward
  12286. (concat
  12287. " *"
  12288. (case type
  12289. (closed org-closed-time-regexp)
  12290. (deadline org-deadline-time-regexp)
  12291. (scheduled org-scheduled-time-regexp)
  12292. (otherwise
  12293. (error "Invalid planning type: %s" type))))
  12294. (line-end-position) t)
  12295. (replace-match "")
  12296. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12297. (when (and (not what) (eq type 'closed))
  12298. (save-excursion
  12299. (beginning-of-line)
  12300. (when (looking-at "[ \t]*$")
  12301. (delete-region (point)
  12302. (line-beginning-position 2)))))))
  12303. ;; Remove leading white spaces.
  12304. (when (looking-at "[ \t]+") (replace-match ""))))
  12305. ((not what) (throw 'exit nil)) ; Nothing to do.
  12306. (t (insert-before-markers "\n")
  12307. (backward-char 1)
  12308. (when org-adapt-indentation
  12309. (org-indent-to-column (1+ (org-outline-level))))))
  12310. (when what
  12311. ;; Insert planning keyword.
  12312. (insert (case what
  12313. (closed org-closed-string)
  12314. (deadline org-deadline-string)
  12315. (scheduled org-scheduled-string)
  12316. (otherwise (error "Invalid planning type: %s" what)))
  12317. " ")
  12318. ;; Insert associated timestamp.
  12319. (let ((ts (org-insert-time-stamp
  12320. time
  12321. (or org-time-was-given
  12322. (and (eq what 'closed) org-log-done-with-time))
  12323. (eq what 'closed)
  12324. nil nil (list org-end-time-was-given))))
  12325. (unless (eolp) (insert " "))
  12326. ts))))))
  12327. (defvar org-log-note-marker (make-marker))
  12328. (defvar org-log-note-purpose nil)
  12329. (defvar org-log-note-state nil)
  12330. (defvar org-log-note-previous-state nil)
  12331. (defvar org-log-note-extra nil)
  12332. (defvar org-log-note-window-configuration nil)
  12333. (defvar org-log-note-return-to (make-marker))
  12334. (defvar org-log-note-effective-time nil
  12335. "Remembered current time so that dynamically scoped
  12336. `org-extend-today-until' affects tha timestamps in state change
  12337. log")
  12338. (defvar org-log-post-message nil
  12339. "Message to be displayed after a log note has been stored.
  12340. The auto-repeater uses this.")
  12341. (defun org-add-note ()
  12342. "Add a note to the current entry.
  12343. This is done in the same way as adding a state change note."
  12344. (interactive)
  12345. (org-add-log-setup 'note nil nil 'findpos nil))
  12346. (defun org-log-beginning (&optional create)
  12347. "Return expected start of log notes in current entry.
  12348. When optional argument CREATE is non-nil, the function creates
  12349. a drawer to store notes, if necessary. Returned position ignores
  12350. narrowing."
  12351. (org-with-wide-buffer
  12352. (org-end-of-meta-data)
  12353. (let ((end (if (org-at-heading-p) (point)
  12354. (save-excursion (outline-next-heading) (point))))
  12355. (drawer (org-log-into-drawer)))
  12356. (cond
  12357. (drawer
  12358. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12359. (case-fold-search t))
  12360. (catch 'exit
  12361. ;; Try to find existing drawer.
  12362. (while (re-search-forward regexp end t)
  12363. (let ((element (org-element-at-point)))
  12364. (when (eq (org-element-type element) 'drawer)
  12365. (let ((cend (org-element-property :contents-end element)))
  12366. (when (and (not org-log-states-order-reversed) cend)
  12367. (goto-char cend)))
  12368. (throw 'exit nil))))
  12369. ;; No drawer found. Create one, if permitted.
  12370. (when create
  12371. (unless (bolp) (insert "\n"))
  12372. (let ((beg (point)))
  12373. (insert ":" drawer ":\n:END:\n")
  12374. (org-indent-region beg (point)))
  12375. (end-of-line -1)))))
  12376. (org-log-state-notes-insert-after-drawers
  12377. (while (and (looking-at org-drawer-regexp)
  12378. (progn (goto-char (match-end 0))
  12379. (re-search-forward org-property-end-re end t)))
  12380. (forward-line)))))
  12381. (if (bolp) (point) (line-beginning-position 2))))
  12382. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12383. "Set up the post command hook to take a note.
  12384. If this is about to TODO state change, the new state is expected in STATE.
  12385. When FINDPOS is non-nil, find the correct position for the note in
  12386. the current entry. If not, assume that it can be inserted at point.
  12387. HOW is an indicator what kind of note should be created.
  12388. EXTRA is additional text that will be inserted into the notes buffer."
  12389. (org-with-wide-buffer
  12390. (when findpos
  12391. (goto-char (org-log-beginning t))
  12392. (unless org-log-states-order-reversed
  12393. (org-skip-over-state-notes)
  12394. (skip-chars-backward " \t\n\r")
  12395. (forward-line)))
  12396. (move-marker org-log-note-marker (point))
  12397. ;; Preserve position even if a property drawer is inserted in the
  12398. ;; process.
  12399. (set-marker-insertion-type org-log-note-marker t)
  12400. (setq org-log-note-purpose purpose
  12401. org-log-note-state state
  12402. org-log-note-previous-state prev-state
  12403. org-log-note-how how
  12404. org-log-note-extra extra
  12405. org-log-note-effective-time (org-current-effective-time))
  12406. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12407. (defun org-skip-over-state-notes ()
  12408. "Skip past the list of State notes in an entry."
  12409. (when (ignore-errors (goto-char (org-in-item-p)))
  12410. (let* ((struct (org-list-struct))
  12411. (prevs (org-list-prevs-alist struct))
  12412. (regexp
  12413. (concat "[ \t]*- +"
  12414. (replace-regexp-in-string
  12415. " +" " +"
  12416. (org-replace-escapes
  12417. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12418. `(("%d" . ,org-ts-regexp-inactive)
  12419. ("%D" . ,org-ts-regexp)
  12420. ("%s" . "\"\\S-+\"")
  12421. ("%S" . "\"\\S-+\"")
  12422. ("%t" . ,org-ts-regexp-inactive)
  12423. ("%T" . ,org-ts-regexp)
  12424. ("%u" . ".*?")
  12425. ("%U" . ".*?")))))))
  12426. (while (org-looking-at-p regexp)
  12427. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12428. (org-list-get-item-end (point) struct)))))))
  12429. (defun org-add-log-note (&optional purpose)
  12430. "Pop up a window for taking a note, and add this note later at point."
  12431. (remove-hook 'post-command-hook 'org-add-log-note)
  12432. (setq org-log-note-window-configuration (current-window-configuration))
  12433. (delete-other-windows)
  12434. (move-marker org-log-note-return-to (point))
  12435. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12436. (goto-char org-log-note-marker)
  12437. (org-switch-to-buffer-other-window "*Org Note*")
  12438. (erase-buffer)
  12439. (if (memq org-log-note-how '(time state))
  12440. (let (current-prefix-arg) (org-store-log-note))
  12441. (let ((org-inhibit-startup t)) (org-mode))
  12442. (insert (format "# Insert note for %s.
  12443. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12444. (cond
  12445. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12446. ((eq org-log-note-purpose 'done) "closed todo item")
  12447. ((eq org-log-note-purpose 'state)
  12448. (format "state change from \"%s\" to \"%s\""
  12449. (or org-log-note-previous-state "")
  12450. (or org-log-note-state "")))
  12451. ((eq org-log-note-purpose 'reschedule)
  12452. "rescheduling")
  12453. ((eq org-log-note-purpose 'delschedule)
  12454. "no longer scheduled")
  12455. ((eq org-log-note-purpose 'redeadline)
  12456. "changing deadline")
  12457. ((eq org-log-note-purpose 'deldeadline)
  12458. "removing deadline")
  12459. ((eq org-log-note-purpose 'refile)
  12460. "refiling")
  12461. ((eq org-log-note-purpose 'note)
  12462. "this entry")
  12463. (t (error "This should not happen")))))
  12464. (if org-log-note-extra (insert org-log-note-extra))
  12465. (org-set-local 'org-finish-function 'org-store-log-note)
  12466. (run-hooks 'org-log-buffer-setup-hook)))
  12467. (defvar org-note-abort nil) ; dynamically scoped
  12468. (defun org-store-log-note ()
  12469. "Finish taking a log note, and insert it to where it belongs."
  12470. (let ((txt (buffer-string)))
  12471. (kill-buffer (current-buffer))
  12472. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12473. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12474. (setq txt (replace-match "" t t txt)))
  12475. (if (string-match "\\s-+\\'" txt)
  12476. (setq txt (replace-match "" t t txt)))
  12477. (setq lines (org-split-string txt "\n"))
  12478. (when (and note (string-match "\\S-" note))
  12479. (setq note
  12480. (org-replace-escapes
  12481. note
  12482. (list (cons "%u" (user-login-name))
  12483. (cons "%U" user-full-name)
  12484. (cons "%t" (format-time-string
  12485. (org-time-stamp-format 'long 'inactive)
  12486. org-log-note-effective-time))
  12487. (cons "%T" (format-time-string
  12488. (org-time-stamp-format 'long nil)
  12489. org-log-note-effective-time))
  12490. (cons "%d" (format-time-string
  12491. (org-time-stamp-format nil 'inactive)
  12492. org-log-note-effective-time))
  12493. (cons "%D" (format-time-string
  12494. (org-time-stamp-format nil nil)
  12495. org-log-note-effective-time))
  12496. (cons "%s" (cond
  12497. ((not org-log-note-state) "")
  12498. ((org-string-match-p org-ts-regexp
  12499. org-log-note-state)
  12500. (format "\"[%s]\""
  12501. (substring org-log-note-state 1 -1)))
  12502. (t (format "\"%s\"" org-log-note-state))))
  12503. (cons "%S"
  12504. (cond
  12505. ((not org-log-note-previous-state) "")
  12506. ((org-string-match-p org-ts-regexp
  12507. org-log-note-previous-state)
  12508. (format "\"[%s]\""
  12509. (substring
  12510. org-log-note-previous-state 1 -1)))
  12511. (t (format "\"%s\""
  12512. org-log-note-previous-state)))))))
  12513. (when lines (setq note (concat note " \\\\")))
  12514. (push note lines))
  12515. (when (or current-prefix-arg org-note-abort)
  12516. (when (org-log-into-drawer)
  12517. (org-remove-empty-drawer-at org-log-note-marker))
  12518. (setq lines nil))
  12519. (when lines
  12520. (with-current-buffer (marker-buffer org-log-note-marker)
  12521. (org-with-wide-buffer
  12522. (goto-char org-log-note-marker)
  12523. (move-marker org-log-note-marker nil)
  12524. ;; Make sure point is at the beginning of an empty line.
  12525. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12526. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12527. ;; In an existing list, add a new item at the top level.
  12528. ;; Otherwise, indent line like a regular one.
  12529. (let ((itemp (org-in-item-p)))
  12530. (if itemp
  12531. (org-indent-line-to
  12532. (let ((struct (save-excursion
  12533. (goto-char itemp) (org-list-struct))))
  12534. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12535. (org-indent-line)))
  12536. (insert (org-list-bullet-string "-") (pop lines))
  12537. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12538. (dolist (line lines)
  12539. (insert "\n")
  12540. (org-indent-line-to ind)
  12541. (insert line)))
  12542. (message "Note stored")
  12543. (org-back-to-heading t)
  12544. (org-cycle-hide-drawers 'children))
  12545. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12546. ;; from within `org-add-log-note' because `buffer-undo-list'
  12547. ;; is then modified outside of `org-with-remote-undo'.
  12548. (when (eq this-command 'org-agenda-todo)
  12549. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12550. ;; Don't add undo information when called from `org-agenda-todo'
  12551. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12552. (set-window-configuration org-log-note-window-configuration)
  12553. (with-current-buffer (marker-buffer org-log-note-return-to)
  12554. (goto-char org-log-note-return-to))
  12555. (move-marker org-log-note-return-to nil)
  12556. (and org-log-post-message (message "%s" org-log-post-message))))
  12557. (defun org-remove-empty-drawer-at (pos)
  12558. "Remove an empty drawer at position POS.
  12559. POS may also be a marker."
  12560. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12561. (org-with-wide-buffer
  12562. (goto-char pos)
  12563. (let ((drawer (org-element-at-point)))
  12564. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12565. (not (org-element-property :contents-begin drawer)))
  12566. (delete-region (org-element-property :begin drawer)
  12567. (progn (goto-char (org-element-property :end drawer))
  12568. (skip-chars-backward " \r\t\n")
  12569. (forward-line)
  12570. (point))))))))
  12571. (defvar org-ts-type nil)
  12572. (defun org-sparse-tree (&optional arg type)
  12573. "Create a sparse tree, prompt for the details.
  12574. This command can create sparse trees. You first need to select the type
  12575. of match used to create the tree:
  12576. t Show all TODO entries.
  12577. T Show entries with a specific TODO keyword.
  12578. m Show entries selected by a tags/property match.
  12579. p Enter a property name and its value (both with completion on existing
  12580. names/values) and show entries with that property.
  12581. r Show entries matching a regular expression (`/' can be used as well).
  12582. b Show deadlines and scheduled items before a date.
  12583. a Show deadlines and scheduled items after a date.
  12584. d Show deadlines due within `org-deadline-warning-days'.
  12585. D Show deadlines and scheduled items between a date range."
  12586. (interactive "P")
  12587. (setq type (or type org-sparse-tree-default-date-type))
  12588. (setq org-ts-type type)
  12589. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12590. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12591. [c]ycle through date types: %s"
  12592. (case type
  12593. (all "all timestamps")
  12594. (scheduled "only scheduled")
  12595. (deadline "only deadline")
  12596. (active "only active timestamps")
  12597. (inactive "only inactive timestamps")
  12598. (closed "with a closed time-stamp")
  12599. (otherwise "scheduled/deadline")))
  12600. (let ((answer (read-char-exclusive)))
  12601. (case answer
  12602. (?c
  12603. (org-sparse-tree
  12604. arg
  12605. (cadr
  12606. (memq type '(nil all scheduled deadline active inactive closed)))))
  12607. (?d (call-interactively 'org-check-deadlines))
  12608. (?b (call-interactively 'org-check-before-date))
  12609. (?a (call-interactively 'org-check-after-date))
  12610. (?D (call-interactively 'org-check-dates-range))
  12611. (?t (call-interactively 'org-show-todo-tree))
  12612. (?T (org-show-todo-tree '(4)))
  12613. (?m (call-interactively 'org-match-sparse-tree))
  12614. ((?p ?P)
  12615. (let* ((kwd (org-icompleting-read
  12616. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12617. (value (org-icompleting-read
  12618. "Value: " (mapcar 'list (org-property-values kwd)))))
  12619. (unless (string-match "\\`{.*}\\'" value)
  12620. (setq value (concat "\"" value "\"")))
  12621. (org-match-sparse-tree arg (concat kwd "=" value))))
  12622. ((?r ?R ?/) (call-interactively 'org-occur))
  12623. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12624. (defvar org-occur-highlights nil
  12625. "List of overlays used for occur matches.")
  12626. (make-variable-buffer-local 'org-occur-highlights)
  12627. (defvar org-occur-parameters nil
  12628. "Parameters of the active org-occur calls.
  12629. This is a list, each call to org-occur pushes as cons cell,
  12630. containing the regular expression and the callback, onto the list.
  12631. The list can contain several entries if `org-occur' has been called
  12632. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12633. will only contain one set of parameters. When the highlights are
  12634. removed (for example with `C-c C-c', or with the next edit (depending
  12635. on `org-remove-highlights-with-change'), this variable is emptied
  12636. as well.")
  12637. (make-variable-buffer-local 'org-occur-parameters)
  12638. (defun org-occur (regexp &optional keep-previous callback)
  12639. "Make a compact tree which shows all matches of REGEXP.
  12640. The tree will show the lines where the regexp matches, and all higher
  12641. headlines above the match. It will also show the heading after the match,
  12642. to make sure editing the matching entry is easy.
  12643. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12644. call to `org-occur' will be kept, to allow stacking of calls to this
  12645. command.
  12646. If CALLBACK is non-nil, it is a function which is called to confirm
  12647. that the match should indeed be shown."
  12648. (interactive "sRegexp: \nP")
  12649. (when (equal regexp "")
  12650. (user-error "Regexp cannot be empty"))
  12651. (unless keep-previous
  12652. (org-remove-occur-highlights nil nil t))
  12653. (push (cons regexp callback) org-occur-parameters)
  12654. (let ((cnt 0))
  12655. (save-excursion
  12656. (goto-char (point-min))
  12657. (if (or (not keep-previous) ; do not want to keep
  12658. (not org-occur-highlights)) ; no previous matches
  12659. ;; hide everything
  12660. (org-overview))
  12661. (while (re-search-forward regexp nil t)
  12662. (when (or (not callback)
  12663. (save-match-data (funcall callback)))
  12664. (setq cnt (1+ cnt))
  12665. (when org-highlight-sparse-tree-matches
  12666. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12667. (org-show-context 'occur-tree))))
  12668. (when org-remove-highlights-with-change
  12669. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12670. nil 'local))
  12671. (unless org-sparse-tree-open-archived-trees
  12672. (org-hide-archived-subtrees (point-min) (point-max)))
  12673. (run-hooks 'org-occur-hook)
  12674. (if (org-called-interactively-p 'interactive)
  12675. (message "%d match(es) for regexp %s" cnt regexp))
  12676. cnt))
  12677. (defun org-occur-next-match (&optional n reset)
  12678. "Function for `next-error-function' to find sparse tree matches.
  12679. N is the number of matches to move, when negative move backwards.
  12680. RESET is entirely ignored - this function always goes back to the
  12681. starting point when no match is found."
  12682. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12683. (search-func (if (< n 0)
  12684. 'previous-single-char-property-change
  12685. 'next-single-char-property-change))
  12686. (n (abs n))
  12687. (pos (point))
  12688. p1)
  12689. (catch 'exit
  12690. (while (setq p1 (funcall search-func (point) 'org-type))
  12691. (when (equal p1 limit)
  12692. (goto-char pos)
  12693. (user-error "No more matches"))
  12694. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12695. (setq n (1- n))
  12696. (when (= n 0)
  12697. (goto-char p1)
  12698. (throw 'exit (point))))
  12699. (goto-char p1))
  12700. (goto-char p1)
  12701. (user-error "No more matches"))))
  12702. (defun org-show-context (&optional key)
  12703. "Make sure point and context are visible.
  12704. Optional argument KEY, when non-nil, is a symbol. See
  12705. `org-show-context-detail' for allowed values and how much is to
  12706. be shown."
  12707. (org-show-set-visibility
  12708. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12709. ((cdr (assq key org-show-context-detail)))
  12710. (t (cdr (assq 'default org-show-context-detail))))))
  12711. (defun org-show-set-visibility (detail)
  12712. "Set visibility around point according to DETAIL.
  12713. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12714. `tree', `canonical' or t. See `org-show-context-detail' for more
  12715. information."
  12716. (unless (org-before-first-heading-p)
  12717. ;; Show current heading and possibly its entry, following headline
  12718. ;; or all children.
  12719. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12720. (org-flag-heading nil)
  12721. (org-show-entry)
  12722. (org-with-limited-levels
  12723. (case detail
  12724. ((tree canonical t) (show-children))
  12725. ((nil minimal ancestors))
  12726. (t (save-excursion
  12727. (outline-next-heading)
  12728. (org-flag-heading nil))))))
  12729. ;; Show all siblings.
  12730. (when (eq detail 'lineage) (org-show-siblings))
  12731. ;; Show ancestors, possibly with their children.
  12732. (when (memq detail '(ancestors lineage tree canonical t))
  12733. (save-excursion
  12734. (while (org-up-heading-safe)
  12735. (org-flag-heading nil)
  12736. (when (memq detail '(canonical t)) (org-show-entry))
  12737. (when (memq detail '(tree canonical t)) (show-children)))))))
  12738. (defvar org-reveal-start-hook nil
  12739. "Hook run before revealing a location.")
  12740. (defun org-reveal (&optional siblings)
  12741. "Show current entry, hierarchy above it, and the following headline.
  12742. This can be used to show a consistent set of context around
  12743. locations exposed with `org-show-context'.
  12744. With optional argument SIBLINGS, on each level of the hierarchy all
  12745. siblings are shown. This repairs the tree structure to what it would
  12746. look like when opened with hierarchical calls to `org-cycle'.
  12747. With double optional argument \\[universal-argument] \\[universal-argument], \
  12748. go to the parent and show the
  12749. entire tree."
  12750. (interactive "P")
  12751. (run-hooks 'org-reveal-start-hook)
  12752. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12753. ((equal siblings '(16))
  12754. (save-excursion
  12755. (when (org-up-heading-safe)
  12756. (org-show-subtree)
  12757. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12758. (t (org-show-set-visibility 'lineage))))
  12759. (defun org-highlight-new-match (beg end)
  12760. "Highlight from BEG to END and mark the highlight is an occur headline."
  12761. (let ((ov (make-overlay beg end)))
  12762. (overlay-put ov 'face 'secondary-selection)
  12763. (overlay-put ov 'org-type 'org-occur)
  12764. (push ov org-occur-highlights)))
  12765. (defun org-remove-occur-highlights (&optional beg end noremove)
  12766. "Remove the occur highlights from the buffer.
  12767. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12768. from the `before-change-functions' in the current buffer."
  12769. (interactive)
  12770. (unless org-inhibit-highlight-removal
  12771. (mapc 'delete-overlay org-occur-highlights)
  12772. (setq org-occur-highlights nil)
  12773. (setq org-occur-parameters nil)
  12774. (unless noremove
  12775. (remove-hook 'before-change-functions
  12776. 'org-remove-occur-highlights 'local))))
  12777. ;;;; Priorities
  12778. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12779. "Regular expression matching the priority indicator.")
  12780. (defvar org-remove-priority-next-time nil)
  12781. (defun org-priority-up ()
  12782. "Increase the priority of the current item."
  12783. (interactive)
  12784. (org-priority 'up))
  12785. (defun org-priority-down ()
  12786. "Decrease the priority of the current item."
  12787. (interactive)
  12788. (org-priority 'down))
  12789. (defun org-priority (&optional action show)
  12790. "Change the priority of an item.
  12791. ACTION can be `set', `up', `down', or a character."
  12792. (interactive "P")
  12793. (if (equal action '(4))
  12794. (org-show-priority)
  12795. (unless org-enable-priority-commands
  12796. (user-error "Priority commands are disabled"))
  12797. (setq action (or action 'set))
  12798. (let (current new news have remove)
  12799. (save-excursion
  12800. (org-back-to-heading t)
  12801. (if (looking-at org-priority-regexp)
  12802. (setq current (string-to-char (match-string 2))
  12803. have t))
  12804. (cond
  12805. ((eq action 'remove)
  12806. (setq remove t new ?\ ))
  12807. ((or (eq action 'set)
  12808. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12809. (if (not (eq action 'set))
  12810. (setq new action)
  12811. (message "Priority %c-%c, SPC to remove: "
  12812. org-highest-priority org-lowest-priority)
  12813. (save-match-data
  12814. (setq new (read-char-exclusive))))
  12815. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12816. (= (upcase org-lowest-priority) org-lowest-priority))
  12817. (setq new (upcase new)))
  12818. (cond ((equal new ?\ ) (setq remove t))
  12819. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12820. (user-error "Priority must be between `%c' and `%c'"
  12821. org-highest-priority org-lowest-priority))))
  12822. ((eq action 'up)
  12823. (setq new (if have
  12824. (1- current) ; normal cycling
  12825. ;; last priority was empty
  12826. (if (eq last-command this-command)
  12827. org-lowest-priority ; wrap around empty to lowest
  12828. ;; default
  12829. (if org-priority-start-cycle-with-default
  12830. org-default-priority
  12831. (1- org-default-priority))))))
  12832. ((eq action 'down)
  12833. (setq new (if have
  12834. (1+ current) ; normal cycling
  12835. ;; last priority was empty
  12836. (if (eq last-command this-command)
  12837. org-highest-priority ; wrap around empty to highest
  12838. ;; default
  12839. (if org-priority-start-cycle-with-default
  12840. org-default-priority
  12841. (1+ org-default-priority))))))
  12842. (t (user-error "Invalid action")))
  12843. (if (or (< (upcase new) org-highest-priority)
  12844. (> (upcase new) org-lowest-priority))
  12845. (if (and (memq action '(up down))
  12846. (not have) (not (eq last-command this-command)))
  12847. ;; `new' is from default priority
  12848. (error
  12849. "The default can not be set, see `org-default-priority' why")
  12850. ;; normal cycling: `new' is beyond highest/lowest priority
  12851. ;; and is wrapped around to the empty priority
  12852. (setq remove t)))
  12853. (setq news (format "%c" new))
  12854. (if have
  12855. (if remove
  12856. (replace-match "" t t nil 1)
  12857. (replace-match news t t nil 2))
  12858. (if remove
  12859. (user-error "No priority cookie found in line")
  12860. (let ((case-fold-search nil))
  12861. (looking-at org-todo-line-regexp))
  12862. (if (match-end 2)
  12863. (progn
  12864. (goto-char (match-end 2))
  12865. (insert " [#" news "]"))
  12866. (goto-char (match-beginning 3))
  12867. (insert "[#" news "] "))))
  12868. (org-set-tags nil 'align))
  12869. (if remove
  12870. (message "Priority removed")
  12871. (message "Priority of current item set to %s" news)))))
  12872. (defun org-show-priority ()
  12873. "Show the priority of the current item.
  12874. This priority is composed of the main priority given with the [#A] cookies,
  12875. and by additional input from the age of a schedules or deadline entry."
  12876. (interactive)
  12877. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12878. (org-get-at-bol 'priority)
  12879. (save-excursion
  12880. (save-match-data
  12881. (beginning-of-line)
  12882. (and (looking-at org-heading-regexp)
  12883. (org-get-priority (match-string 0))))))))
  12884. (message "Priority is %d" (if pri pri -1000))))
  12885. (defun org-get-priority (s)
  12886. "Find priority cookie and return priority."
  12887. (save-match-data
  12888. (if (functionp org-get-priority-function)
  12889. (funcall org-get-priority-function)
  12890. (if (not (string-match org-priority-regexp s))
  12891. (* 1000 (- org-lowest-priority org-default-priority))
  12892. (* 1000 (- org-lowest-priority
  12893. (string-to-char (match-string 2 s))))))))
  12894. ;;;; Tags
  12895. (defvar org-agenda-archives-mode)
  12896. (defvar org-map-continue-from nil
  12897. "Position from where mapping should continue.
  12898. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12899. (defvar org-scanner-tags nil
  12900. "The current tag list while the tags scanner is running.")
  12901. (defvar org-trust-scanner-tags nil
  12902. "Should `org-get-tags-at' use the tags for the scanner.
  12903. This is for internal dynamical scoping only.
  12904. When this is non-nil, the function `org-get-tags-at' will return the value
  12905. of `org-scanner-tags' instead of building the list by itself. This
  12906. can lead to large speed-ups when the tags scanner is used in a file with
  12907. many entries, and when the list of tags is retrieved, for example to
  12908. obtain a list of properties. Building the tags list for each entry in such
  12909. a file becomes an N^2 operation - but with this variable set, it scales
  12910. as N.")
  12911. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12912. "Scan headline tags with inheritance and produce output ACTION.
  12913. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12914. or `agenda' to produce an entry list for an agenda view. It can also be
  12915. a Lisp form or a function that should be called at each matched headline, in
  12916. this case the return value is a list of all return values from these calls.
  12917. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12918. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12919. only lines with a not-done TODO keyword are included in the output.
  12920. This should be the same variable that was scoped into
  12921. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12922. START-LEVEL can be a string with asterisks, reducing the scope to
  12923. headlines matching this string."
  12924. (require 'org-agenda)
  12925. (let* ((re (concat "^"
  12926. (if start-level
  12927. ;; Get the correct level to match
  12928. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12929. org-outline-regexp)
  12930. " *\\(\\<\\("
  12931. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12932. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12933. (props (list 'face 'default
  12934. 'done-face 'org-agenda-done
  12935. 'undone-face 'default
  12936. 'mouse-face 'highlight
  12937. 'org-not-done-regexp org-not-done-regexp
  12938. 'org-todo-regexp org-todo-regexp
  12939. 'org-complex-heading-regexp org-complex-heading-regexp
  12940. 'help-echo
  12941. (format "mouse-2 or RET jump to org file %s"
  12942. (abbreviate-file-name
  12943. (or (buffer-file-name (buffer-base-buffer))
  12944. (buffer-name (buffer-base-buffer)))))))
  12945. (org-map-continue-from nil)
  12946. lspos tags tags-list
  12947. (tags-alist (list (cons 0 org-file-tags)))
  12948. (llast 0) rtn rtn1 level category i txt
  12949. todo marker entry priority
  12950. ts-date ts-date-type ts-date-pair)
  12951. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12952. (setq action (list 'lambda nil action)))
  12953. (save-excursion
  12954. (goto-char (point-min))
  12955. (when (eq action 'sparse-tree)
  12956. (org-overview)
  12957. (org-remove-occur-highlights))
  12958. (while (let (case-fold-search)
  12959. (re-search-forward re nil t))
  12960. (setq org-map-continue-from nil)
  12961. (catch :skip
  12962. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12963. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12964. (goto-char (setq lspos (match-beginning 0)))
  12965. (setq level (org-reduced-level (org-outline-level))
  12966. category (org-get-category))
  12967. (when (eq action 'agenda)
  12968. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12969. ts-date (car ts-date-pair)
  12970. ts-date-type (cdr ts-date-pair)))
  12971. (setq i llast llast level)
  12972. ;; remove tag lists from same and sublevels
  12973. (while (>= i level)
  12974. (when (setq entry (assoc i tags-alist))
  12975. (setq tags-alist (delete entry tags-alist)))
  12976. (setq i (1- i)))
  12977. ;; add the next tags
  12978. (when tags
  12979. (setq tags (org-split-string tags ":")
  12980. tags-alist
  12981. (cons (cons level tags) tags-alist)))
  12982. ;; compile tags for current headline
  12983. (setq tags-list
  12984. (if org-use-tag-inheritance
  12985. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12986. tags)
  12987. org-scanner-tags tags-list)
  12988. (when org-use-tag-inheritance
  12989. (setcdr (car tags-alist)
  12990. (mapcar (lambda (x)
  12991. (setq x (copy-sequence x))
  12992. (org-add-prop-inherited x))
  12993. (cdar tags-alist))))
  12994. (when (and tags org-use-tag-inheritance
  12995. (or (not (eq t org-use-tag-inheritance))
  12996. org-tags-exclude-from-inheritance))
  12997. ;; selective inheritance, remove uninherited ones
  12998. (setcdr (car tags-alist)
  12999. (org-remove-uninherited-tags (cdar tags-alist))))
  13000. (when (and
  13001. ;; eval matcher only when the todo condition is OK
  13002. (and (or (not todo-only) (member todo org-not-done-keywords))
  13003. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13004. (eval matcher)))
  13005. ;; Call the skipper, but return t if it does not skip,
  13006. ;; so that the `and' form continues evaluating
  13007. (progn
  13008. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13009. t)
  13010. ;; Check if timestamps are deselecting this entry
  13011. (or (not todo-only)
  13012. (and (member todo org-not-done-keywords)
  13013. (or (not org-agenda-tags-todo-honor-ignore-options)
  13014. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13015. ;; select this headline
  13016. (cond
  13017. ((eq action 'sparse-tree)
  13018. (and org-highlight-sparse-tree-matches
  13019. (org-get-heading) (match-end 0)
  13020. (org-highlight-new-match
  13021. (match-beginning 1) (match-end 1)))
  13022. (org-show-context 'tags-tree))
  13023. ((eq action 'agenda)
  13024. (setq txt (org-agenda-format-item
  13025. ""
  13026. (concat
  13027. (if (eq org-tags-match-list-sublevels 'indented)
  13028. (make-string (1- level) ?.) "")
  13029. (org-get-heading))
  13030. (make-string level ?\s)
  13031. category
  13032. tags-list)
  13033. priority (org-get-priority txt))
  13034. (goto-char lspos)
  13035. (setq marker (org-agenda-new-marker))
  13036. (org-add-props txt props
  13037. 'org-marker marker 'org-hd-marker marker 'org-category category
  13038. 'todo-state todo
  13039. 'ts-date ts-date
  13040. 'priority priority
  13041. 'type (concat "tagsmatch" ts-date-type))
  13042. (push txt rtn))
  13043. ((functionp action)
  13044. (setq org-map-continue-from nil)
  13045. (save-excursion
  13046. (setq rtn1 (funcall action))
  13047. (push rtn1 rtn)))
  13048. (t (user-error "Invalid action")))
  13049. ;; if we are to skip sublevels, jump to end of subtree
  13050. (unless org-tags-match-list-sublevels
  13051. (org-end-of-subtree t)
  13052. (backward-char 1))))
  13053. ;; Get the correct position from where to continue
  13054. (if org-map-continue-from
  13055. (goto-char org-map-continue-from)
  13056. (and (= (point) lspos) (end-of-line 1)))))
  13057. (when (and (eq action 'sparse-tree)
  13058. (not org-sparse-tree-open-archived-trees))
  13059. (org-hide-archived-subtrees (point-min) (point-max)))
  13060. (nreverse rtn)))
  13061. (defun org-remove-uninherited-tags (tags)
  13062. "Remove all tags that are not inherited from the list TAGS."
  13063. (cond
  13064. ((eq org-use-tag-inheritance t)
  13065. (if org-tags-exclude-from-inheritance
  13066. (org-delete-all org-tags-exclude-from-inheritance tags)
  13067. tags))
  13068. ((not org-use-tag-inheritance) nil)
  13069. ((stringp org-use-tag-inheritance)
  13070. (delq nil (mapcar
  13071. (lambda (x)
  13072. (if (and (string-match org-use-tag-inheritance x)
  13073. (not (member x org-tags-exclude-from-inheritance)))
  13074. x nil))
  13075. tags)))
  13076. ((listp org-use-tag-inheritance)
  13077. (delq nil (mapcar
  13078. (lambda (x)
  13079. (if (member x org-use-tag-inheritance) x nil))
  13080. tags)))))
  13081. (defun org-match-sparse-tree (&optional todo-only match)
  13082. "Create a sparse tree according to tags string MATCH.
  13083. MATCH can contain positive and negative selection of tags, like
  13084. \"+WORK+URGENT-WITHBOSS\".
  13085. If optional argument TODO-ONLY is non-nil, only select lines that are
  13086. also TODO lines."
  13087. (interactive "P")
  13088. (org-agenda-prepare-buffers (list (current-buffer)))
  13089. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13090. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13091. (defvar org-cached-props nil)
  13092. (defun org-cached-entry-get (pom property)
  13093. (if (or (eq t org-use-property-inheritance)
  13094. (and (stringp org-use-property-inheritance)
  13095. (let ((case-fold-search t))
  13096. (org-string-match-p org-use-property-inheritance property)))
  13097. (and (listp org-use-property-inheritance)
  13098. (member-ignore-case property org-use-property-inheritance)))
  13099. ;; Caching is not possible, check it directly.
  13100. (org-entry-get pom property 'inherit)
  13101. ;; Get all properties, so we can do complicated checks easily.
  13102. (cdr (assoc-string property
  13103. (or org-cached-props
  13104. (setq org-cached-props (org-entry-properties pom)))
  13105. t))))
  13106. (defun org-global-tags-completion-table (&optional files)
  13107. "Return the list of all tags in all agenda buffer/files.
  13108. Optional FILES argument is a list of files which can be used
  13109. instead of the agenda files."
  13110. (save-excursion
  13111. (org-uniquify
  13112. (delq nil
  13113. (apply 'append
  13114. (mapcar
  13115. (lambda (file)
  13116. (set-buffer (find-file-noselect file))
  13117. (append (org-get-buffer-tags)
  13118. (mapcar (lambda (x) (if (stringp (car-safe x))
  13119. (list (car-safe x)) nil))
  13120. org-tag-alist)))
  13121. (if (and files (car files))
  13122. files
  13123. (org-agenda-files))))))))
  13124. (defun org-make-tags-matcher (match)
  13125. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13126. The variable `todo-only' is scoped dynamically into this function.
  13127. It will be set to t if the matcher restricts matching to TODO entries,
  13128. otherwise will not be touched.
  13129. Returns a cons of the selection string MATCH and the constructed
  13130. lisp form implementing the matcher. The matcher is to be evaluated
  13131. at an Org entry, with point on the headline, and returns t if the
  13132. entry matches the selection string MATCH. The returned lisp form
  13133. references two variables with information about the entry, which
  13134. must be bound around the form's evaluation: todo, the TODO keyword
  13135. at the entry (or nil of none); and tags-list, the list of all tags
  13136. at the entry including inherited ones. Additionally, the category
  13137. of the entry (if any) must be specified as the text property
  13138. 'org-category on the headline.
  13139. See also `org-scan-tags'.
  13140. "
  13141. (declare (special todo-only))
  13142. (unless (boundp 'todo-only)
  13143. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13144. (unless match
  13145. ;; Get a new match request, with completion against the global
  13146. ;; tags table and the local tags in current buffer
  13147. (let ((org-last-tags-completion-table
  13148. (org-uniquify
  13149. (delq nil (append (org-get-buffer-tags)
  13150. (org-global-tags-completion-table))))))
  13151. (setq match (org-completing-read-no-i
  13152. "Match: " 'org-tags-completion-function nil nil nil
  13153. 'org-tags-history))))
  13154. ;; Parse the string and create a lisp form
  13155. (let ((match0 match)
  13156. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13157. minus tag mm
  13158. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13159. orterms term orlist re-p str-p level-p level-op time-p
  13160. prop-p pn pv po gv rest (start 0) (ss 0))
  13161. ;; Expand group tags
  13162. (setq match (org-tags-expand match))
  13163. ;; Check if there is a TODO part of this match, which would be the
  13164. ;; part after a "/". TO make sure that this slash is not part of
  13165. ;; a property value to be matched against, we also check that there
  13166. ;; is no " after that slash.
  13167. ;; First, find the last slash
  13168. (while (string-match "/+" match ss)
  13169. (setq start (match-beginning 0) ss (match-end 0)))
  13170. (if (and (string-match "/+" match start)
  13171. (not (save-match-data (string-match "\"" match start))))
  13172. ;; match contains also a todo-matching request
  13173. (progn
  13174. (setq tagsmatch (substring match 0 (match-beginning 0))
  13175. todomatch (substring match (match-end 0)))
  13176. (if (string-match "^!" todomatch)
  13177. (setq todo-only t todomatch (substring todomatch 1)))
  13178. (if (string-match "^\\s-*$" todomatch)
  13179. (setq todomatch nil)))
  13180. ;; only matching tags
  13181. (setq tagsmatch match todomatch nil))
  13182. ;; Make the tags matcher
  13183. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13184. (setq tagsmatcher t)
  13185. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13186. (while (setq term (pop orterms))
  13187. (while (and (equal (substring term -1) "\\") orterms)
  13188. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13189. (while (string-match re term)
  13190. (setq rest (substring term (match-end 0))
  13191. minus (and (match-end 1)
  13192. (equal (match-string 1 term) "-"))
  13193. tag (save-match-data (replace-regexp-in-string
  13194. "\\\\-" "-"
  13195. (match-string 2 term)))
  13196. re-p (equal (string-to-char tag) ?{)
  13197. level-p (match-end 4)
  13198. prop-p (match-end 5)
  13199. mm (cond
  13200. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13201. (level-p
  13202. (setq level-op (org-op-to-function (match-string 3 term)))
  13203. `(,level-op level ,(string-to-number
  13204. (match-string 4 term))))
  13205. (prop-p
  13206. (setq pn (match-string 5 term)
  13207. po (match-string 6 term)
  13208. pv (match-string 7 term)
  13209. re-p (equal (string-to-char pv) ?{)
  13210. str-p (equal (string-to-char pv) ?\")
  13211. time-p (save-match-data
  13212. (string-match "^\"[[<].*[]>]\"$" pv))
  13213. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13214. (if time-p (setq pv (org-matcher-time pv)))
  13215. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13216. (cond
  13217. ((equal pn "CATEGORY")
  13218. (setq gv '(get-text-property (point) 'org-category)))
  13219. ((equal pn "TODO")
  13220. (setq gv 'todo))
  13221. (t
  13222. (setq gv `(org-cached-entry-get nil ,pn))))
  13223. (if re-p
  13224. (if (eq po 'org<>)
  13225. `(not (string-match ,pv (or ,gv "")))
  13226. `(string-match ,pv (or ,gv "")))
  13227. (if str-p
  13228. `(,po (or ,gv "") ,pv)
  13229. `(,po (string-to-number (or ,gv ""))
  13230. ,(string-to-number pv) ))))
  13231. (t `(member ,tag tags-list)))
  13232. mm (if minus (list 'not mm) mm)
  13233. term rest)
  13234. (push mm tagsmatcher))
  13235. (push (if (> (length tagsmatcher) 1)
  13236. (cons 'and tagsmatcher)
  13237. (car tagsmatcher))
  13238. orlist)
  13239. (setq tagsmatcher nil))
  13240. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13241. (setq tagsmatcher
  13242. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13243. ;; Make the todo matcher
  13244. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13245. (setq todomatcher t)
  13246. (setq orterms (org-split-string todomatch "|") orlist nil)
  13247. (dolist (term orterms)
  13248. (while (string-match re term)
  13249. (setq minus (and (match-end 1)
  13250. (equal (match-string 1 term) "-"))
  13251. kwd (match-string 2 term)
  13252. re-p (equal (string-to-char kwd) ?{)
  13253. term (substring term (match-end 0))
  13254. mm (if re-p
  13255. `(string-match ,(substring kwd 1 -1) todo)
  13256. (list 'equal 'todo kwd))
  13257. mm (if minus (list 'not mm) mm))
  13258. (push mm todomatcher))
  13259. (push (if (> (length todomatcher) 1)
  13260. (cons 'and todomatcher)
  13261. (car todomatcher))
  13262. orlist)
  13263. (setq todomatcher nil))
  13264. (setq todomatcher (if (> (length orlist) 1)
  13265. (cons 'or orlist) (car orlist))))
  13266. ;; Return the string and lisp forms of the matcher
  13267. (setq matcher (if todomatcher
  13268. (list 'and tagsmatcher todomatcher)
  13269. tagsmatcher))
  13270. (when todo-only
  13271. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13272. matcher)))
  13273. (cons match0 matcher)))
  13274. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13275. "Expand group tags in MATCH.
  13276. This replaces every group tag in MATCH with a regexp tag search.
  13277. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13278. will be replaced like this:
  13279. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13280. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13281. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13282. Replacing by a regexp preserves the structure of the match.
  13283. E.g., this expansion
  13284. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13285. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13286. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13287. A group tag in MATCH can contain regular expressions of its own.
  13288. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13289. will be replaced like this:
  13290. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13291. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13292. assumed to be a single group tag, and the function will return
  13293. the list of tags in this group.
  13294. When DOWNCASE is non-nil, expand downcased TAGS."
  13295. (if org-group-tags
  13296. (let* ((case-fold-search t)
  13297. (stable org-mode-syntax-table)
  13298. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13299. (taggroups (if downcased
  13300. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13301. taggroups)
  13302. taggroups))
  13303. (taggroups-keys (mapcar #'car taggroups))
  13304. (return-match (if downcased (downcase match) match))
  13305. (count 0)
  13306. (work-already-expanded tags-already-expanded)
  13307. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13308. ;; @ and _ are allowed as word-components in tags.
  13309. (modify-syntax-entry ?@ "w" stable)
  13310. (modify-syntax-entry ?_ "w" stable)
  13311. ;; Temporarily replace regexp-expressions in the match-expression.
  13312. (while (string-match "{.+?}" return-match)
  13313. (incf count)
  13314. (push (match-string 0 return-match) regexps-in-match)
  13315. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13316. (while (and taggroups-keys
  13317. (with-syntax-table stable
  13318. (string-match
  13319. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13320. (regexp-opt taggroups-keys) "\\>\\)")
  13321. return-match)))
  13322. (let* ((dir (match-string 1 return-match))
  13323. (tag (match-string 2 return-match))
  13324. (tag (if downcased (downcase tag) tag)))
  13325. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13326. (member tag work-already-expanded))
  13327. (setq tags-in-group (assoc tag taggroups))
  13328. (push tag work-already-expanded)
  13329. ;; Recursively expand each tag in the group, if the tag hasn't
  13330. ;; already been expanded. Restore the match-data after all recursive calls.
  13331. (save-match-data
  13332. (let (tags-expanded)
  13333. (dolist (x (cdr tags-in-group))
  13334. (if (and (member x taggroups-keys)
  13335. (not (member x work-already-expanded)))
  13336. (setq tags-expanded
  13337. (delete-dups
  13338. (append
  13339. (org-tags-expand x t downcased
  13340. work-already-expanded)
  13341. tags-expanded)))
  13342. (setq tags-expanded
  13343. (append (list x) tags-expanded)))
  13344. (setq work-already-expanded
  13345. (delete-dups
  13346. (append tags-expanded
  13347. work-already-expanded))))
  13348. (setq tags-in-group
  13349. (delete-dups (cons (car tags-in-group)
  13350. tags-expanded)))))
  13351. ;; Filter tag-regexps from tags.
  13352. (setq regexp-in-group-escaped
  13353. (delq nil (mapcar (lambda (x)
  13354. (if (stringp x)
  13355. (and (equal "{" (substring x 0 1))
  13356. (equal "}" (substring x -1))
  13357. x)
  13358. x))
  13359. tags-in-group))
  13360. regexp-in-group
  13361. (mapcar (lambda (x)
  13362. (substring x 1 -1))
  13363. regexp-in-group-escaped)
  13364. tags-in-group
  13365. (delq nil (mapcar (lambda (x)
  13366. (if (stringp x)
  13367. (and (not (equal "{" (substring x 0 1)))
  13368. (not (equal "}" (substring x -1)))
  13369. x)
  13370. x))
  13371. tags-in-group)))
  13372. ;; If single-as-list, do no more in the while-loop.
  13373. (if (not single-as-list)
  13374. (progn
  13375. (when regexp-in-group
  13376. (setq regexp-in-group
  13377. (concat "\\|"
  13378. (mapconcat 'identity regexp-in-group
  13379. "\\|"))))
  13380. (setq tags-in-group
  13381. (concat dir
  13382. "{\\<"
  13383. (regexp-opt tags-in-group)
  13384. "\\>"
  13385. regexp-in-group
  13386. "}"))
  13387. (when (stringp tags-in-group)
  13388. (org-add-props tags-in-group '(grouptag t)))
  13389. (setq return-match
  13390. (replace-match tags-in-group t t return-match)))
  13391. (setq tags-in-group
  13392. (append regexp-in-group-escaped tags-in-group))))
  13393. (setq taggroups-keys (delete tag taggroups-keys))))
  13394. ;; Add the regular expressions back into the match-expression again.
  13395. (while regexps-in-match
  13396. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13397. (pop regexps-in-match)
  13398. return-match t t))
  13399. (decf count))
  13400. (if single-as-list
  13401. (if tags-in-group tags-in-group (list return-match))
  13402. return-match))
  13403. (if single-as-list
  13404. (list (if downcased (downcase match) match))
  13405. match)))
  13406. (defun org-op-to-function (op &optional stringp)
  13407. "Turn an operator into the appropriate function."
  13408. (setq op
  13409. (cond
  13410. ((equal op "<" ) '(< string< org-time<))
  13411. ((equal op ">" ) '(> org-string> org-time>))
  13412. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13413. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13414. ((member op '("=" "==")) '(= string= org-time=))
  13415. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13416. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13417. (defun org<> (a b) (not (= a b)))
  13418. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13419. (defun org-string>= (a b) (not (string< a b)))
  13420. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13421. (defun org-string<> (a b) (not (string= a b)))
  13422. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13423. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13424. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13425. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13426. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13427. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13428. (defun org-2ft (s)
  13429. "Convert S to a floating point time.
  13430. If S is already a number, just return it. If it is a string, parse
  13431. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13432. (cond
  13433. ((numberp s) s)
  13434. ((stringp s)
  13435. (condition-case nil
  13436. (float-time (apply 'encode-time (org-parse-time-string s)))
  13437. (error 0.)))
  13438. (t 0.)))
  13439. (defun org-time-today ()
  13440. "Time in seconds today at 0:00.
  13441. Returns the float number of seconds since the beginning of the
  13442. epoch to the beginning of today (00:00)."
  13443. (float-time (apply 'encode-time
  13444. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13445. (defun org-matcher-time (s)
  13446. "Interpret a time comparison value."
  13447. (save-match-data
  13448. (cond
  13449. ((string= s "<now>") (float-time))
  13450. ((string= s "<today>") (org-time-today))
  13451. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13452. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13453. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13454. (+ (org-time-today)
  13455. (* (string-to-number (match-string 1 s))
  13456. (cdr (assoc (match-string 2 s)
  13457. '(("d" . 86400.0) ("w" . 604800.0)
  13458. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13459. (t (org-2ft s)))))
  13460. (defun org-match-any-p (re list)
  13461. "Does re match any element of list?"
  13462. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13463. (delq nil list))
  13464. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13465. (defvar org-tags-overlay (make-overlay 1 1))
  13466. (org-detach-overlay org-tags-overlay)
  13467. (defun org-get-local-tags-at (&optional pos)
  13468. "Get a list of tags defined in the current headline."
  13469. (org-get-tags-at pos 'local))
  13470. (defun org-get-local-tags ()
  13471. "Get a list of tags defined in the current headline."
  13472. (org-get-tags-at nil 'local))
  13473. (defun org-get-tags-at (&optional pos local)
  13474. "Get a list of all headline tags applicable at POS.
  13475. POS defaults to point. If tags are inherited, the list contains
  13476. the targets in the same sequence as the headlines appear, i.e.
  13477. the tags of the current headline come last.
  13478. When LOCAL is non-nil, only return tags from the current headline,
  13479. ignore inherited ones."
  13480. (interactive)
  13481. (if (and org-trust-scanner-tags
  13482. (or (not pos) (equal pos (point)))
  13483. (not local))
  13484. org-scanner-tags
  13485. (let (tags ltags lastpos parent)
  13486. (save-excursion
  13487. (save-restriction
  13488. (widen)
  13489. (goto-char (or pos (point)))
  13490. (save-match-data
  13491. (catch 'done
  13492. (condition-case nil
  13493. (progn
  13494. (org-back-to-heading t)
  13495. (while (not (equal lastpos (point)))
  13496. (setq lastpos (point))
  13497. (when (looking-at
  13498. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13499. (setq ltags (org-split-string
  13500. (org-match-string-no-properties 1) ":"))
  13501. (when parent
  13502. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13503. (setq tags (append
  13504. (if parent
  13505. (org-remove-uninherited-tags ltags)
  13506. ltags)
  13507. tags)))
  13508. (or org-use-tag-inheritance (throw 'done t))
  13509. (if local (throw 'done t))
  13510. (or (org-up-heading-safe) (error nil))
  13511. (setq parent t)))
  13512. (error nil)))))
  13513. (if local
  13514. tags
  13515. (reverse (delete-dups
  13516. (reverse (append
  13517. (org-remove-uninherited-tags
  13518. org-file-tags)
  13519. tags)))))))))
  13520. (defun org-add-prop-inherited (s)
  13521. (add-text-properties 0 (length s) '(inherited t) s)
  13522. s)
  13523. (defun org-toggle-tag (tag &optional onoff)
  13524. "Toggle the tag TAG for the current line.
  13525. If ONOFF is `on' or `off', don't toggle but set to this state."
  13526. (let (res current)
  13527. (save-excursion
  13528. (org-back-to-heading t)
  13529. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13530. (point-at-eol) t)
  13531. (progn
  13532. (setq current (match-string 1))
  13533. (replace-match ""))
  13534. (setq current ""))
  13535. (setq current (nreverse (org-split-string current ":")))
  13536. (cond
  13537. ((eq onoff 'on)
  13538. (setq res t)
  13539. (or (member tag current) (push tag current)))
  13540. ((eq onoff 'off)
  13541. (or (not (member tag current)) (setq current (delete tag current))))
  13542. (t (if (member tag current)
  13543. (setq current (delete tag current))
  13544. (setq res t)
  13545. (push tag current))))
  13546. (end-of-line 1)
  13547. (if current
  13548. (progn
  13549. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13550. (org-set-tags nil t))
  13551. (delete-horizontal-space))
  13552. (run-hooks 'org-after-tags-change-hook))
  13553. res))
  13554. (defun org-align-tags-here (to-col)
  13555. ;; Assumes that this is a headline
  13556. "Align tags on the current headline to TO-COL."
  13557. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13558. (beginning-of-line 1)
  13559. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13560. (< pos (match-beginning 2)))
  13561. (progn
  13562. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13563. (goto-char (match-beginning 1))
  13564. (insert " ")
  13565. (delete-region (point) (1+ (match-beginning 2)))
  13566. (setq ncol (max (current-column)
  13567. (1+ col)
  13568. (if (> to-col 0)
  13569. to-col
  13570. (- (abs to-col) tags-l))))
  13571. (setq p (point))
  13572. (insert (make-string (- ncol (current-column)) ?\ ))
  13573. (setq ncol (current-column))
  13574. (when indent-tabs-mode (tabify p (point-at-eol)))
  13575. (org-move-to-column (min ncol col)))
  13576. (goto-char pos))))
  13577. (defun org-set-tags-command (&optional arg just-align)
  13578. "Call the set-tags command for the current entry."
  13579. (interactive "P")
  13580. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13581. (org-set-tags arg just-align)
  13582. (save-excursion
  13583. (unless (and (org-region-active-p)
  13584. org-loop-over-headlines-in-active-region)
  13585. (org-back-to-heading t))
  13586. (org-set-tags arg just-align))))
  13587. (defun org-set-tags-to (data)
  13588. "Set the tags of the current entry to DATA, replacing the current tags.
  13589. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13590. If DATA is nil or the empty string, any tags will be removed."
  13591. (interactive "sTags: ")
  13592. (setq data
  13593. (cond
  13594. ((eq data nil) "")
  13595. ((equal data "") "")
  13596. ((stringp data)
  13597. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13598. ":"))
  13599. ((listp data)
  13600. (concat ":" (mapconcat 'identity data ":") ":"))))
  13601. (when data
  13602. (save-excursion
  13603. (org-back-to-heading t)
  13604. (when (looking-at org-complex-heading-regexp)
  13605. (if (match-end 5)
  13606. (progn
  13607. (goto-char (match-beginning 5))
  13608. (insert data)
  13609. (delete-region (point) (point-at-eol))
  13610. (org-set-tags nil 'align))
  13611. (goto-char (point-at-eol))
  13612. (insert " " data)
  13613. (org-set-tags nil 'align)))
  13614. (beginning-of-line 1)
  13615. (if (looking-at ".*?\\([ \t]+\\)$")
  13616. (delete-region (match-beginning 1) (match-end 1))))))
  13617. (defun org-align-all-tags ()
  13618. "Align the tags i all headings."
  13619. (interactive)
  13620. (save-excursion
  13621. (or (ignore-errors (org-back-to-heading t))
  13622. (outline-next-heading))
  13623. (if (org-at-heading-p)
  13624. (org-set-tags t)
  13625. (message "No headings"))))
  13626. (defvar org-indent-indentation-per-level)
  13627. (defun org-set-tags (&optional arg just-align)
  13628. "Set the tags for the current headline.
  13629. With prefix ARG, realign all tags in headings in the current buffer.
  13630. When JUST-ALIGN is non-nil, only align tags."
  13631. (interactive "P")
  13632. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13633. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13634. 'region-start-level
  13635. 'region))
  13636. org-loop-over-headlines-in-active-region)
  13637. (org-map-entries
  13638. ;; We don't use ARG and JUST-ALIGN here because these args
  13639. ;; are not useful when looping over headlines.
  13640. #'org-set-tags
  13641. org-loop-over-headlines-in-active-region
  13642. cl
  13643. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13644. (let ((org-setting-tags t))
  13645. (if arg
  13646. (save-excursion
  13647. (goto-char (point-min))
  13648. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13649. (while (re-search-forward org-outline-regexp-bol nil t)
  13650. (org-set-tags nil t)
  13651. (end-of-line)))
  13652. (message "All tags realigned to column %d" org-tags-column))
  13653. (let* ((current (org-get-tags-string))
  13654. (col (current-column))
  13655. (tags
  13656. (if just-align current
  13657. ;; Get a new set of tags from the user.
  13658. (save-excursion
  13659. (let* ((table
  13660. (setq
  13661. org-last-tags-completion-table
  13662. (append
  13663. org-tag-persistent-alist
  13664. (or org-tag-alist (org-get-buffer-tags))
  13665. (and
  13666. org-complete-tags-always-offer-all-agenda-tags
  13667. (org-global-tags-completion-table
  13668. (org-agenda-files))))))
  13669. (current-tags (org-split-string current ":"))
  13670. (inherited-tags
  13671. (nreverse (nthcdr (length current-tags)
  13672. (nreverse (org-get-tags-at))))))
  13673. (replace-regexp-in-string
  13674. "\\([-+&]+\\|,\\)"
  13675. ":"
  13676. (if (or (eq t org-use-fast-tag-selection)
  13677. (and org-use-fast-tag-selection
  13678. (delq nil (mapcar #'cdr table))))
  13679. (org-fast-tag-selection
  13680. current-tags inherited-tags table
  13681. (and org-fast-tag-selection-include-todo
  13682. org-todo-key-alist))
  13683. (let ((org-add-colon-after-tag-completion
  13684. (< 1 (length table))))
  13685. (org-trim
  13686. (completing-read
  13687. "Tags: "
  13688. #'org-tags-completion-function
  13689. nil nil current 'org-tags-history))))))))))
  13690. (when org-tags-sort-function
  13691. (setq tags
  13692. (mapconcat
  13693. #'identity
  13694. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13695. org-tags-sort-function)
  13696. ":")))
  13697. (if (not (org-string-nw-p tags)) (setq tags "")
  13698. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13699. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13700. ;; Insert new tags at the correct column
  13701. (beginning-of-line)
  13702. (let ((level (if (looking-at org-outline-regexp)
  13703. (- (match-end 0) (point) 1)
  13704. 1)))
  13705. (cond
  13706. ((and (equal current "") (equal tags "")))
  13707. ((re-search-forward
  13708. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13709. (line-end-position)
  13710. t)
  13711. (if (equal tags "") (replace-match "" t t)
  13712. (goto-char (match-beginning 0))
  13713. (let* ((c0 (current-column))
  13714. ;; Compute offset for the case of org-indent-mode
  13715. ;; active.
  13716. (di (if (org-bound-and-true-p org-indent-mode)
  13717. (* (1- org-indent-indentation-per-level)
  13718. (1- level))
  13719. 0))
  13720. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13721. (tc (+ org-tags-column
  13722. (if (> org-tags-column 0) (- di) di)))
  13723. (c1 (max (1+ c0)
  13724. (if (> tc 0) tc
  13725. (- (- tc) (string-width tags)))))
  13726. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13727. (replace-match rpl t t)
  13728. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13729. (tabify p0 (point))))))
  13730. (t (error "Tags alignment failed"))))
  13731. (org-move-to-column col))
  13732. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13733. (defun org-change-tag-in-region (beg end tag off)
  13734. "Add or remove TAG for each entry in the region.
  13735. This works in the agenda, and also in an org-mode buffer."
  13736. (interactive
  13737. (list (region-beginning) (region-end)
  13738. (let ((org-last-tags-completion-table
  13739. (if (derived-mode-p 'org-mode)
  13740. (org-uniquify
  13741. (delq nil (append (org-get-buffer-tags)
  13742. (org-global-tags-completion-table))))
  13743. (org-global-tags-completion-table))))
  13744. (org-icompleting-read
  13745. "Tag: " 'org-tags-completion-function nil nil nil
  13746. 'org-tags-history))
  13747. (progn
  13748. (message "[s]et or [r]emove? ")
  13749. (equal (read-char-exclusive) ?r))))
  13750. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13751. (let ((agendap (equal major-mode 'org-agenda-mode))
  13752. l1 l2 m buf pos newhead (cnt 0))
  13753. (goto-char end)
  13754. (setq l2 (1- (org-current-line)))
  13755. (goto-char beg)
  13756. (setq l1 (org-current-line))
  13757. (loop for l from l1 to l2 do
  13758. (org-goto-line l)
  13759. (setq m (get-text-property (point) 'org-hd-marker))
  13760. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13761. (and agendap m))
  13762. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13763. pos (if agendap m (point)))
  13764. (with-current-buffer buf
  13765. (save-excursion
  13766. (save-restriction
  13767. (goto-char pos)
  13768. (setq cnt (1+ cnt))
  13769. (org-toggle-tag tag (if off 'off 'on))
  13770. (setq newhead (org-get-heading)))))
  13771. (and agendap (org-agenda-change-all-lines newhead m))))
  13772. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13773. (defun org-tags-completion-function (string predicate &optional flag)
  13774. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13775. (confirm (lambda (x) (stringp (car x)))))
  13776. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13777. (setq s1 (match-string 1 string)
  13778. s2 (match-string 2 string))
  13779. (setq s1 "" s2 string))
  13780. (cond
  13781. ((eq flag nil)
  13782. ;; try completion
  13783. (setq rtn (try-completion s2 ctable confirm))
  13784. (if (stringp rtn)
  13785. (setq rtn
  13786. (concat s1 s2 (substring rtn (length s2))
  13787. (if (and org-add-colon-after-tag-completion
  13788. (assoc rtn ctable))
  13789. ":" ""))))
  13790. rtn)
  13791. ((eq flag t)
  13792. ;; all-completions
  13793. (all-completions s2 ctable confirm))
  13794. ((eq flag 'lambda)
  13795. ;; exact match?
  13796. (assoc s2 ctable)))))
  13797. (defun org-fast-tag-insert (kwd tags face &optional end)
  13798. "Insert KDW, and the TAGS, the latter with face FACE.
  13799. Also insert END."
  13800. (insert (format "%-12s" (concat kwd ":"))
  13801. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13802. (or end "")))
  13803. (defun org-fast-tag-show-exit (flag)
  13804. (save-excursion
  13805. (org-goto-line 3)
  13806. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13807. (replace-match ""))
  13808. (when flag
  13809. (end-of-line 1)
  13810. (org-move-to-column (- (window-width) 19) t)
  13811. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13812. (defun org-set-current-tags-overlay (current prefix)
  13813. "Add an overlay to CURRENT tag with PREFIX."
  13814. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13815. (if (featurep 'xemacs)
  13816. (org-overlay-display org-tags-overlay (concat prefix s)
  13817. 'secondary-selection)
  13818. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13819. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13820. (defvar org-last-tag-selection-key nil)
  13821. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13822. "Fast tag selection with single keys.
  13823. CURRENT is the current list of tags in the headline, INHERITED is the
  13824. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13825. possibly with grouping information. TODO-TABLE is a similar table with
  13826. TODO keywords, should these have keys assigned to them.
  13827. If the keys are nil, a-z are automatically assigned.
  13828. Returns the new tags string, or nil to not change the current settings."
  13829. (let* ((fulltable (append table todo-table))
  13830. (maxlen (apply 'max (mapcar
  13831. (lambda (x)
  13832. (if (stringp (car x)) (string-width (car x)) 0))
  13833. fulltable)))
  13834. (buf (current-buffer))
  13835. (expert (eq org-fast-tag-selection-single-key 'expert))
  13836. (buffer-tags nil)
  13837. (fwidth (+ maxlen 3 1 3))
  13838. (ncol (/ (- (window-width) 4) fwidth))
  13839. (i-face 'org-done)
  13840. (c-face 'org-todo)
  13841. tg cnt e c char c1 c2 ntable tbl rtn
  13842. ov-start ov-end ov-prefix
  13843. (exit-after-next org-fast-tag-selection-single-key)
  13844. (done-keywords org-done-keywords)
  13845. groups ingroup intaggroup)
  13846. (save-excursion
  13847. (beginning-of-line 1)
  13848. (if (looking-at
  13849. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13850. (setq ov-start (match-beginning 1)
  13851. ov-end (match-end 1)
  13852. ov-prefix "")
  13853. (setq ov-start (1- (point-at-eol))
  13854. ov-end (1+ ov-start))
  13855. (skip-chars-forward "^\n\r")
  13856. (setq ov-prefix
  13857. (concat
  13858. (buffer-substring (1- (point)) (point))
  13859. (if (> (current-column) org-tags-column)
  13860. " "
  13861. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13862. (move-overlay org-tags-overlay ov-start ov-end)
  13863. (save-window-excursion
  13864. (if expert
  13865. (set-buffer (get-buffer-create " *Org tags*"))
  13866. (delete-other-windows)
  13867. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13868. (org-switch-to-buffer-other-window " *Org tags*"))
  13869. (erase-buffer)
  13870. (org-set-local 'org-done-keywords done-keywords)
  13871. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13872. (org-fast-tag-insert "Current" current c-face "\n\n")
  13873. (org-fast-tag-show-exit exit-after-next)
  13874. (org-set-current-tags-overlay current ov-prefix)
  13875. (setq tbl fulltable char ?a cnt 0)
  13876. (while (setq e (pop tbl))
  13877. (cond
  13878. ((eq (car e) :startgroup)
  13879. (push '() groups) (setq ingroup t)
  13880. (unless (zerop cnt)
  13881. (setq cnt 0)
  13882. (insert "\n"))
  13883. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13884. ((eq (car e) :endgroup)
  13885. (setq ingroup nil cnt 0)
  13886. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13887. ((eq (car e) :startgrouptag)
  13888. (setq intaggroup t)
  13889. (unless (zerop cnt)
  13890. (setq cnt 0)
  13891. (insert "\n"))
  13892. (insert "[ "))
  13893. ((eq (car e) :endgrouptag)
  13894. (setq intaggroup nil cnt 0)
  13895. (insert "]\n"))
  13896. ((equal e '(:newline))
  13897. (unless (zerop cnt)
  13898. (setq cnt 0)
  13899. (insert "\n")
  13900. (setq e (car tbl))
  13901. (while (equal (car tbl) '(:newline))
  13902. (insert "\n")
  13903. (setq tbl (cdr tbl)))))
  13904. ((equal e '(:grouptags)) (insert " : "))
  13905. (t
  13906. (setq tg (copy-sequence (car e)) c2 nil)
  13907. (if (cdr e)
  13908. (setq c (cdr e))
  13909. ;; automatically assign a character.
  13910. (setq c1 (string-to-char
  13911. (downcase (substring
  13912. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13913. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13914. (while (or (rassoc char ntable) (rassoc char table))
  13915. (setq char (1+ char)))
  13916. (setq c2 c1))
  13917. (setq c (or c2 char)))
  13918. (when ingroup (push tg (car groups)))
  13919. (setq tg (org-add-props tg nil 'face
  13920. (cond
  13921. ((not (assoc tg table))
  13922. (org-get-todo-face tg))
  13923. ((member tg current) c-face)
  13924. ((member tg inherited) i-face))))
  13925. (when (equal (caar tbl) :grouptags)
  13926. (org-add-props tg nil 'face 'org-tag-group))
  13927. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13928. (insert "[" c "] " tg (make-string
  13929. (- fwidth 4 (length tg)) ?\ ))
  13930. (push (cons tg c) ntable)
  13931. (when (= (incf cnt) ncol)
  13932. (insert "\n")
  13933. (when (or ingroup intaggroup) (insert " "))
  13934. (setq cnt 0)))))
  13935. (setq ntable (nreverse ntable))
  13936. (insert "\n")
  13937. (goto-char (point-min))
  13938. (unless expert (org-fit-window-to-buffer))
  13939. (setq rtn
  13940. (catch 'exit
  13941. (while t
  13942. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13943. (if (not groups) "no " "")
  13944. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13945. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13946. (setq org-last-tag-selection-key c)
  13947. (cond
  13948. ((= c ?\r) (throw 'exit t))
  13949. ((= c ?!)
  13950. (setq groups (not groups))
  13951. (goto-char (point-min))
  13952. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13953. ((= c ?\C-c)
  13954. (if (not expert)
  13955. (org-fast-tag-show-exit
  13956. (setq exit-after-next (not exit-after-next)))
  13957. (setq expert nil)
  13958. (delete-other-windows)
  13959. (set-window-buffer (split-window-vertically) " *Org tags*")
  13960. (org-switch-to-buffer-other-window " *Org tags*")
  13961. (org-fit-window-to-buffer)))
  13962. ((or (= c ?\C-g)
  13963. (and (= c ?q) (not (rassoc c ntable))))
  13964. (org-detach-overlay org-tags-overlay)
  13965. (setq quit-flag t))
  13966. ((= c ?\ )
  13967. (setq current nil)
  13968. (when exit-after-next (setq exit-after-next 'now)))
  13969. ((= c ?\t)
  13970. (condition-case nil
  13971. (setq tg (org-icompleting-read
  13972. "Tag: "
  13973. (or buffer-tags
  13974. (with-current-buffer buf
  13975. (org-get-buffer-tags)))))
  13976. (quit (setq tg "")))
  13977. (when (string-match "\\S-" tg)
  13978. (add-to-list 'buffer-tags (list tg))
  13979. (if (member tg current)
  13980. (setq current (delete tg current))
  13981. (push tg current)))
  13982. (when exit-after-next (setq exit-after-next 'now)))
  13983. ((setq e (rassoc c todo-table) tg (car e))
  13984. (with-current-buffer buf
  13985. (save-excursion (org-todo tg)))
  13986. (when exit-after-next (setq exit-after-next 'now)))
  13987. ((setq e (rassoc c ntable) tg (car e))
  13988. (if (member tg current)
  13989. (setq current (delete tg current))
  13990. (loop for g in groups do
  13991. (when (member tg g)
  13992. (dolist (x g) (setq current (delete x current)))))
  13993. (push tg current))
  13994. (when exit-after-next (setq exit-after-next 'now))))
  13995. ;; Create a sorted list
  13996. (setq current
  13997. (sort current
  13998. (lambda (a b)
  13999. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14000. (when (eq exit-after-next 'now) (throw 'exit t))
  14001. (goto-char (point-min))
  14002. (beginning-of-line 2)
  14003. (delete-region (point) (point-at-eol))
  14004. (org-fast-tag-insert "Current" current c-face)
  14005. (org-set-current-tags-overlay current ov-prefix)
  14006. (while (re-search-forward
  14007. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14008. (setq tg (match-string 1))
  14009. (add-text-properties
  14010. (match-beginning 1) (match-end 1)
  14011. (list 'face
  14012. (cond
  14013. ((member tg current) c-face)
  14014. ((member tg inherited) i-face)
  14015. (t (get-text-property (match-beginning 1) 'face))))))
  14016. (goto-char (point-min)))))
  14017. (org-detach-overlay org-tags-overlay)
  14018. (if rtn
  14019. (mapconcat 'identity current ":")
  14020. nil))))
  14021. (defun org-get-tags-string ()
  14022. "Get the TAGS string in the current headline."
  14023. (unless (org-at-heading-p t)
  14024. (user-error "Not on a heading"))
  14025. (save-excursion
  14026. (beginning-of-line 1)
  14027. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14028. (org-match-string-no-properties 1)
  14029. "")))
  14030. (defun org-get-tags ()
  14031. "Get the list of tags specified in the current headline."
  14032. (org-split-string (org-get-tags-string) ":"))
  14033. (defun org-get-buffer-tags ()
  14034. "Get a table of all tags used in the buffer, for completion."
  14035. (org-with-wide-buffer
  14036. (goto-char (point-min))
  14037. (let ((tag-re (concat org-outline-regexp-bol
  14038. "\\(?:.*?[ \t]\\)?"
  14039. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14040. tags)
  14041. (while (re-search-forward tag-re nil t)
  14042. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14043. (push tag tags)))
  14044. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14045. ;;;; The mapping API
  14046. (defun org-map-entries (func &optional match scope &rest skip)
  14047. "Call FUNC at each headline selected by MATCH in SCOPE.
  14048. FUNC is a function or a lisp form. The function will be called without
  14049. arguments, with the cursor positioned at the beginning of the headline.
  14050. The return values of all calls to the function will be collected and
  14051. returned as a list.
  14052. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14053. does not need to preserve point. After evaluation, the cursor will be
  14054. moved to the end of the line (presumably of the headline of the
  14055. processed entry) and search continues from there. Under some
  14056. circumstances, this may not produce the wanted results. For example,
  14057. if you have removed (e.g. archived) the current (sub)tree it could
  14058. mean that the next entry will be skipped entirely. In such cases, you
  14059. can specify the position from where search should continue by making
  14060. FUNC set the variable `org-map-continue-from' to the desired buffer
  14061. position.
  14062. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14063. Only headlines that are matched by this query will be considered during
  14064. the iteration. When MATCH is nil or t, all headlines will be
  14065. visited by the iteration.
  14066. SCOPE determines the scope of this command. It can be any of:
  14067. nil The current buffer, respecting the restriction if any
  14068. tree The subtree started with the entry at point
  14069. region The entries within the active region, if any
  14070. region-start-level
  14071. The entries within the active region, but only those at
  14072. the same level than the first one.
  14073. file The current buffer, without restriction
  14074. file-with-archives
  14075. The current buffer, and any archives associated with it
  14076. agenda All agenda files
  14077. agenda-with-archives
  14078. All agenda files with any archive files associated with them
  14079. \(file1 file2 ...)
  14080. If this is a list, all files in the list will be scanned
  14081. The remaining args are treated as settings for the skipping facilities of
  14082. the scanner. The following items can be given here:
  14083. archive skip trees with the archive tag
  14084. comment skip trees with the COMMENT keyword
  14085. function or Emacs Lisp form:
  14086. will be used as value for `org-agenda-skip-function', so
  14087. whenever the function returns a position, FUNC will not be
  14088. called for that entry and search will continue from the
  14089. position returned
  14090. If your function needs to retrieve the tags including inherited tags
  14091. at the *current* entry, you can use the value of the variable
  14092. `org-scanner-tags' which will be much faster than getting the value
  14093. with `org-get-tags-at'. If your function gets properties with
  14094. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14095. to t around the call to `org-entry-properties' to get the same speedup.
  14096. Note that if your function moves around to retrieve tags and properties at
  14097. a *different* entry, you cannot use these techniques."
  14098. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14099. (not (org-region-active-p)))
  14100. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14101. (org-agenda-skip-archived-trees (memq 'archive skip))
  14102. (org-agenda-skip-comment-trees (memq 'comment skip))
  14103. (org-agenda-skip-function
  14104. (car (org-delete-all '(comment archive) skip)))
  14105. (org-tags-match-list-sublevels t)
  14106. (start-level (eq scope 'region-start-level))
  14107. matcher file res
  14108. org-todo-keywords-for-agenda
  14109. org-done-keywords-for-agenda
  14110. org-todo-keyword-alist-for-agenda
  14111. org-tag-alist-for-agenda
  14112. todo-only)
  14113. (cond
  14114. ((eq match t) (setq matcher t))
  14115. ((eq match nil) (setq matcher t))
  14116. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14117. (save-excursion
  14118. (save-restriction
  14119. (cond ((eq scope 'tree)
  14120. (org-back-to-heading t)
  14121. (org-narrow-to-subtree)
  14122. (setq scope nil))
  14123. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14124. (org-region-active-p))
  14125. ;; If needed, set start-level to a string like "2"
  14126. (when start-level
  14127. (save-excursion
  14128. (goto-char (region-beginning))
  14129. (unless (org-at-heading-p) (outline-next-heading))
  14130. (setq start-level (org-current-level))))
  14131. (narrow-to-region (region-beginning)
  14132. (save-excursion
  14133. (goto-char (region-end))
  14134. (unless (and (bolp) (org-at-heading-p))
  14135. (outline-next-heading))
  14136. (point)))
  14137. (setq scope nil)))
  14138. (if (not scope)
  14139. (progn
  14140. (org-agenda-prepare-buffers
  14141. (and buffer-file-name (list buffer-file-name)))
  14142. (setq res (org-scan-tags func matcher todo-only start-level)))
  14143. ;; Get the right scope
  14144. (cond
  14145. ((and scope (listp scope) (symbolp (car scope)))
  14146. (setq scope (eval scope)))
  14147. ((eq scope 'agenda)
  14148. (setq scope (org-agenda-files t)))
  14149. ((eq scope 'agenda-with-archives)
  14150. (setq scope (org-agenda-files t))
  14151. (setq scope (org-add-archive-files scope)))
  14152. ((eq scope 'file)
  14153. (setq scope (and buffer-file-name (list buffer-file-name))))
  14154. ((eq scope 'file-with-archives)
  14155. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14156. (org-agenda-prepare-buffers scope)
  14157. (dolist (file scope)
  14158. (with-current-buffer (org-find-base-buffer-visiting file)
  14159. (save-excursion
  14160. (save-restriction
  14161. (widen)
  14162. (goto-char (point-min))
  14163. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14164. res)))
  14165. ;;; Properties API
  14166. (defconst org-special-properties
  14167. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14168. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14169. "The special properties valid in Org mode.
  14170. These are properties that are not defined in the property drawer,
  14171. but in some other way.")
  14172. (defconst org-default-properties
  14173. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14174. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14175. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14176. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14177. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14178. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14179. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14180. "Some properties that are used by Org mode for various purposes.
  14181. Being in this list makes sure that they are offered for completion.")
  14182. (defun org--update-property-plist (key val props)
  14183. "Associate KEY to VAL in alist PROPS.
  14184. Modifications are made by side-effect. Return new alist."
  14185. (let* ((appending (string= (substring key -1) "+"))
  14186. (key (if appending (substring key 0 -1) key))
  14187. (old (assoc-string key props t)))
  14188. (if (not old) (cons (cons key val) props)
  14189. (setcdr old (if appending (concat (cdr old) " " val) val))
  14190. props)))
  14191. (defun org-get-property-block (&optional beg force)
  14192. "Return the (beg . end) range of the body of the property drawer.
  14193. BEG is the beginning of the current subtree, or of the part
  14194. before the first headline. If it is not given, it will be found.
  14195. If the drawer does not exist, create it if FORCE is non-nil, or
  14196. return nil."
  14197. (org-with-wide-buffer
  14198. (when beg (goto-char beg))
  14199. (unless (org-before-first-heading-p)
  14200. (let ((beg (cond (beg)
  14201. ((or (not (featurep 'org-inlinetask))
  14202. (org-inlinetask-in-task-p))
  14203. (org-back-to-heading t))
  14204. (t (org-with-limited-levels (org-back-to-heading t))))))
  14205. (forward-line)
  14206. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14207. (cond ((looking-at org-property-drawer-re)
  14208. (forward-line)
  14209. (cons (point) (progn (goto-char (match-end 0))
  14210. (line-beginning-position))))
  14211. (force
  14212. (goto-char beg)
  14213. (org-insert-property-drawer)
  14214. (let ((pos (save-excursion (search-forward ":END:")
  14215. (line-beginning-position))))
  14216. (cons pos pos))))))))
  14217. (defun org-at-property-p ()
  14218. "Non-nil when point is inside a property drawer.
  14219. See `org-property-re' for match data, if applicable."
  14220. (save-excursion
  14221. (beginning-of-line)
  14222. (and (looking-at org-property-re)
  14223. (let ((property-drawer (save-match-data (org-get-property-block))))
  14224. (and property-drawer
  14225. (>= (point) (car property-drawer))
  14226. (< (point) (cdr property-drawer)))))))
  14227. (defun org-property-action ()
  14228. "Do an action on properties."
  14229. (interactive)
  14230. (unless (org-at-property-p) (user-error "Not at a property"))
  14231. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14232. (let ((c (read-char-exclusive)))
  14233. (case c
  14234. (?s (call-interactively #'org-set-property))
  14235. (?d (call-interactively #'org-delete-property))
  14236. (?D (call-interactively #'org-delete-property-globally))
  14237. (?c (call-interactively #'org-compute-property-at-point))
  14238. (otherwise (user-error "No such property action %c" c)))))
  14239. (defun org-inc-effort ()
  14240. "Increment the value of the effort property in the current entry."
  14241. (interactive)
  14242. (org-set-effort nil t))
  14243. (defvar org-clock-effort) ; Defined in org-clock.el.
  14244. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14245. (defun org-set-effort (&optional value increment)
  14246. "Set the effort property of the current entry.
  14247. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14248. 10th allowed value.
  14249. When INCREMENT is non-nil, set the property to the next allowed value."
  14250. (interactive "P")
  14251. (if (equal value 0) (setq value 10))
  14252. (let* ((completion-ignore-case t)
  14253. (prop org-effort-property)
  14254. (cur (org-entry-get nil prop))
  14255. (allowed (org-property-get-allowed-values nil prop 'table))
  14256. (existing (mapcar 'list (org-property-values prop)))
  14257. (heading (nth 4 (org-heading-components)))
  14258. rpl
  14259. (val (cond
  14260. ((stringp value) value)
  14261. ((and allowed (integerp value))
  14262. (or (car (nth (1- value) allowed))
  14263. (car (org-last allowed))))
  14264. ((and allowed increment)
  14265. (or (caadr (member (list cur) allowed))
  14266. (user-error "Allowed effort values are not set")))
  14267. (allowed
  14268. (message "Select 1-9,0, [RET%s]: %s"
  14269. (if cur (concat "=" cur) "")
  14270. (mapconcat 'car allowed " "))
  14271. (setq rpl (read-char-exclusive))
  14272. (if (equal rpl ?\r)
  14273. cur
  14274. (setq rpl (- rpl ?0))
  14275. (if (equal rpl 0) (setq rpl 10))
  14276. (if (and (> rpl 0) (<= rpl (length allowed)))
  14277. (car (nth (1- rpl) allowed))
  14278. (org-completing-read "Effort: " allowed nil))))
  14279. (t
  14280. (let (org-completion-use-ido org-completion-use-iswitchb)
  14281. (org-completing-read
  14282. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14283. (concat "[" cur "]") "")
  14284. ": ")
  14285. existing nil nil "" nil cur))))))
  14286. (unless (equal (org-entry-get nil prop) val)
  14287. (org-entry-put nil prop val))
  14288. (org-refresh-property
  14289. '((effort . identity)
  14290. (effort-minutes . org-duration-string-to-minutes))
  14291. val)
  14292. (when (string= heading org-clock-current-task)
  14293. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14294. (org-clock-update-mode-line))
  14295. (message "%s is now %s" prop val)))
  14296. (defun org-entry-properties (&optional pom which)
  14297. "Get all properties of the current entry.
  14298. When POM is a buffer position, get all properties from the entry
  14299. there instead.
  14300. This includes the TODO keyword, the tags, time strings for
  14301. deadline, scheduled, and clocking, and any additional properties
  14302. defined in the entry.
  14303. If WHICH is nil or `all', get all properties. If WHICH is
  14304. `special' or `standard', only get that subclass. If WHICH is
  14305. a string, only get that property.
  14306. Return value is an alist. Keys are properties, as upcased
  14307. strings."
  14308. (org-with-point-at pom
  14309. (when (and (derived-mode-p 'org-mode)
  14310. (ignore-errors (org-back-to-heading t)))
  14311. (catch 'exit
  14312. (let* ((beg (point))
  14313. (specific (and (stringp which) (upcase which)))
  14314. (which (cond ((not specific) which)
  14315. ((member specific org-special-properties) 'special)
  14316. (t 'standard)))
  14317. props)
  14318. ;; Get the special properties, like TODO and TAGS.
  14319. (when (memq which '(nil all special))
  14320. (when (or (not specific) (string= specific "CLOCKSUM"))
  14321. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14322. (when clocksum
  14323. (push (cons "CLOCKSUM"
  14324. (org-columns-number-to-string
  14325. (/ (float clocksum) 60.) 'add_times))
  14326. props)))
  14327. (when specific (throw 'exit props)))
  14328. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14329. (let ((clocksumt (get-text-property (point)
  14330. :org-clock-minutes-today)))
  14331. (when clocksumt
  14332. (push (cons "CLOCKSUM_T"
  14333. (org-columns-number-to-string
  14334. (/ (float clocksumt) 60.) 'add_times))
  14335. props)))
  14336. (when specific (throw 'exit props)))
  14337. (when (or (not specific) (string= specific "ITEM"))
  14338. (when (looking-at org-complex-heading-regexp)
  14339. (push (cons "ITEM"
  14340. (concat
  14341. (org-match-string-no-properties 1)
  14342. (let ((title (org-match-string-no-properties 4)))
  14343. (when (org-string-nw-p title)
  14344. (concat " " (org-remove-tabs title))))))
  14345. props))
  14346. (when specific (throw 'exit props)))
  14347. (when (or (not specific) (string= specific "TODO"))
  14348. (let ((case-fold-search nil))
  14349. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14350. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14351. (when specific (throw 'exit props)))
  14352. (when (or (not specific) (string= specific "PRIORITY"))
  14353. (when (looking-at org-priority-regexp)
  14354. (push (cons "PRIORITY" (org-match-string-no-properties 2))
  14355. props))
  14356. (when specific (throw 'exit props)))
  14357. (when (or (not specific) (string= specific "FILE"))
  14358. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14359. props)
  14360. (when specific (throw 'exit props)))
  14361. (when (or (not specific) (string= specific "TAGS"))
  14362. (let ((value (org-string-nw-p (org-get-tags-string))))
  14363. (when value (push (cons "TAGS" value) props)))
  14364. (when specific (throw 'exit props)))
  14365. (when (or (not specific) (string= specific "ALLTAGS"))
  14366. (let ((value (org-get-tags-at)))
  14367. (when value
  14368. (push (cons "ALLTAGS"
  14369. (format ":%s:" (mapconcat #'identity value ":")))
  14370. props)))
  14371. (when specific (throw 'exit props)))
  14372. (when (or (not specific) (string= specific "BLOCKED"))
  14373. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14374. (when specific (throw 'exit props)))
  14375. (when (or (not specific)
  14376. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14377. (forward-line)
  14378. (when (org-looking-at-p org-planning-line-re)
  14379. (end-of-line)
  14380. (let ((bol (line-beginning-position))
  14381. ;; Backward compatibility: time keywords used to
  14382. ;; be configurable (before 8.3). Make sure we
  14383. ;; get the correct keyword.
  14384. (key-assoc `(("CLOSED" . ,org-closed-string)
  14385. ("DEADLINE" . ,org-deadline-string)
  14386. ("SCHEDULED" . ,org-scheduled-string))))
  14387. (dolist (pair (if specific (list (assoc specific key-assoc))
  14388. key-assoc))
  14389. (save-excursion
  14390. (when (search-backward (cdr pair) bol t)
  14391. (goto-char (match-end 0))
  14392. (skip-chars-forward " \t")
  14393. (and (looking-at org-ts-regexp-both)
  14394. (push (cons (car pair)
  14395. (org-match-string-no-properties 0))
  14396. props)))))))
  14397. (when specific (throw 'exit props)))
  14398. (when (or (not specific)
  14399. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14400. (let ((find-ts
  14401. (lambda (end ts)
  14402. (let ((regexp (cond
  14403. ((string= specific "TIMESTAMP")
  14404. org-ts-regexp)
  14405. ((string= specific "TIMESTAMP_IA")
  14406. org-ts-regexp-inactive)
  14407. ((assoc "TIMESTAMP_IA" ts)
  14408. org-ts-regexp)
  14409. ((assoc "TIMESTAMP" ts)
  14410. org-ts-regexp-inactive)
  14411. (t org-ts-regexp-both))))
  14412. (catch 'next
  14413. (while (re-search-forward regexp end t)
  14414. (backward-char)
  14415. (let ((object (org-element-context)))
  14416. ;; Accept to match timestamps in node
  14417. ;; properties, too.
  14418. (when (memq (org-element-type object)
  14419. '(node-property timestamp))
  14420. (let ((type
  14421. (org-element-property :type object)))
  14422. (cond
  14423. ((and (memq type '(active active-range))
  14424. (not (equal specific "TIMESTAMP_IA")))
  14425. (unless (assoc "TIMESTAMP" ts)
  14426. (push (cons "TIMESTAMP"
  14427. (org-element-property
  14428. :raw-value object))
  14429. ts)
  14430. (when specific (throw 'exit ts))))
  14431. ((and (memq type '(inactive inactive-range))
  14432. (not (string= specific "TIMESTAMP")))
  14433. (unless (assoc "TIMESTAMP_IA" ts)
  14434. (push (cons "TIMESTAMP_IA"
  14435. (org-element-property
  14436. :raw-value object))
  14437. ts)
  14438. (when specific (throw 'exit ts))))))
  14439. ;; Both timestamp types are found,
  14440. ;; move to next part.
  14441. (when (= (length ts) 2) (throw 'next ts)))))
  14442. ts)))))
  14443. (goto-char beg)
  14444. ;; First look for timestamps within headline.
  14445. (let ((ts (funcall find-ts (line-end-position) nil)))
  14446. (if (= (length ts) 2) (setq props (nconc ts props))
  14447. (forward-line)
  14448. ;; Then find timestamps in the section, skipping
  14449. ;; planning line.
  14450. (when (org-looking-at-p org-planning-line-re)
  14451. (forward-line))
  14452. (let ((end (save-excursion (outline-next-heading))))
  14453. (setq props (nconc (funcall find-ts end ts) props))))))))
  14454. ;; Get the standard properties, like :PROP:.
  14455. (when (memq which '(nil all standard))
  14456. ;; If we are looking after a specific property, delegate
  14457. ;; to `org-entry-get', which is faster. However, make an
  14458. ;; exception for "CATEGORY", since it can be also set
  14459. ;; through keywords (i.e. #+CATEGORY).
  14460. (if (and specific (not (equal specific "CATEGORY")))
  14461. (let ((value (org-entry-get beg specific nil t)))
  14462. (throw 'exit (and value (list (cons specific value)))))
  14463. (let ((range (org-get-property-block beg)))
  14464. (when range
  14465. (let ((end (cdr range)) seen-base)
  14466. (goto-char (car range))
  14467. ;; Unlike to `org--update-property-plist', we
  14468. ;; handle the case where base values is found
  14469. ;; after its extension. We also forbid standard
  14470. ;; properties to be named as special properties.
  14471. (while (re-search-forward org-property-re end t)
  14472. (let* ((key (upcase (org-match-string-no-properties 2)))
  14473. (extendp (org-string-match-p "\\+\\'" key))
  14474. (key-base (if extendp (substring key 0 -1) key))
  14475. (value (org-match-string-no-properties 3)))
  14476. (cond
  14477. ((member-ignore-case key-base org-special-properties))
  14478. (extendp
  14479. (setq props
  14480. (org--update-property-plist key value props)))
  14481. ((member key seen-base))
  14482. (t (push key seen-base)
  14483. (let ((p (assoc-string key props t)))
  14484. (if p (setcdr p (concat value " " (cdr p)))
  14485. (push (cons key value) props))))))))))))
  14486. (unless (assoc "CATEGORY" props)
  14487. (push (cons "CATEGORY" (org-get-category beg)) props)
  14488. (when (string= specific "CATEGORY") (throw 'exit props)))
  14489. ;; Return value.
  14490. props)))))
  14491. (defun org-property--local-values (property literal-nil)
  14492. "Return value for PROPERTY in current entry.
  14493. Value is a list whose care is the base value for PROPERTY and cdr
  14494. a list of accumulated values. Return nil if neither is found in
  14495. the entry. Also return nil when PROPERTY is set to \"nil\",
  14496. unless LITERAL-NIL is non-nil."
  14497. (let ((range (org-get-property-block)))
  14498. (when range
  14499. (goto-char (car range))
  14500. (let* ((case-fold-search t)
  14501. (end (cdr range))
  14502. (value
  14503. ;; Base value.
  14504. (save-excursion
  14505. (let ((v (and (re-search-forward
  14506. (org-re-property property nil t) end t)
  14507. (org-match-string-no-properties 3))))
  14508. (list (if literal-nil v (org-not-nil v)))))))
  14509. ;; Find additional values.
  14510. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14511. (while (re-search-forward property+ end t)
  14512. (push (org-match-string-no-properties 3) value)))
  14513. ;; Return final values.
  14514. (and (not (equal value '(nil))) (nreverse value))))))
  14515. (defun org-entry-get (pom property &optional inherit literal-nil)
  14516. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14517. If INHERIT is non-nil and the entry does not have the property,
  14518. then also check higher levels of the hierarchy. If INHERIT is
  14519. the symbol `selective', use inheritance only if the setting in
  14520. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14521. If the property is present but empty, the return value is the
  14522. empty string. If the property is not present at all, nil is
  14523. returned. In any other case, return the value as a string.
  14524. Search is case-insensitive.
  14525. If LITERAL-NIL is set, return the string value \"nil\" as
  14526. a string, do not interpret it as the list atom nil. This is used
  14527. for inheritance when a \"nil\" value can supersede a non-nil
  14528. value higher up the hierarchy."
  14529. (org-with-point-at pom
  14530. (cond
  14531. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14532. ;; We need a special property. Use `org-entry-properties' to
  14533. ;; retrieve it, but specify the wanted property.
  14534. (cdr (assoc-string property (org-entry-properties nil property))))
  14535. ((and inherit
  14536. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14537. (org-entry-get-with-inheritance property literal-nil))
  14538. (t
  14539. (let* ((local (org-property--local-values property literal-nil))
  14540. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14541. (if literal-nil value (org-not-nil value)))))))
  14542. (defun org-property-or-variable-value (var &optional inherit)
  14543. "Check if there is a property fixing the value of VAR.
  14544. If yes, return this value. If not, return the current value of the variable."
  14545. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14546. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14547. (read prop)
  14548. (symbol-value var))))
  14549. (defun org-entry-delete (pom property)
  14550. "Delete PROPERTY from entry at point-or-marker POM.
  14551. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14552. non-nil when a property was removed."
  14553. (unless (member property org-special-properties)
  14554. (org-with-point-at pom
  14555. (let ((range (org-get-property-block)))
  14556. (when range
  14557. (let* ((begin (car range))
  14558. (origin (cdr range))
  14559. (end (copy-marker origin))
  14560. (re (org-re-property
  14561. (concat (regexp-quote property) "\\+?") t t)))
  14562. (goto-char begin)
  14563. (while (re-search-forward re end t)
  14564. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14565. ;; If drawer is empty, remove it altogether.
  14566. (when (= begin end)
  14567. (delete-region (line-beginning-position 0)
  14568. (line-beginning-position 2)))
  14569. ;; Return non-nil if some property was removed.
  14570. (prog1 (/= end origin) (set-marker end nil))))))))
  14571. ;; Multi-values properties are properties that contain multiple values
  14572. ;; These values are assumed to be single words, separated by whitespace.
  14573. (defun org-entry-add-to-multivalued-property (pom property value)
  14574. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14575. (let* ((old (org-entry-get pom property))
  14576. (values (and old (org-split-string old "[ \t]"))))
  14577. (setq value (org-entry-protect-space value))
  14578. (unless (member value values)
  14579. (setq values (append values (list value)))
  14580. (org-entry-put pom property
  14581. (mapconcat 'identity values " ")))))
  14582. (defun org-entry-remove-from-multivalued-property (pom property value)
  14583. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14584. (let* ((old (org-entry-get pom property))
  14585. (values (and old (org-split-string old "[ \t]"))))
  14586. (setq value (org-entry-protect-space value))
  14587. (when (member value values)
  14588. (setq values (delete value values))
  14589. (org-entry-put pom property
  14590. (mapconcat 'identity values " ")))))
  14591. (defun org-entry-member-in-multivalued-property (pom property value)
  14592. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14593. (let* ((old (org-entry-get pom property))
  14594. (values (and old (org-split-string old "[ \t]"))))
  14595. (setq value (org-entry-protect-space value))
  14596. (member value values)))
  14597. (defun org-entry-get-multivalued-property (pom property)
  14598. "Return a list of values in a multivalued property."
  14599. (let* ((value (org-entry-get pom property))
  14600. (values (and value (org-split-string value "[ \t]"))))
  14601. (mapcar 'org-entry-restore-space values)))
  14602. (defun org-entry-put-multivalued-property (pom property &rest values)
  14603. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14604. VALUES should be a list of strings. Spaces will be protected."
  14605. (org-entry-put pom property
  14606. (mapconcat 'org-entry-protect-space values " "))
  14607. (let* ((value (org-entry-get pom property))
  14608. (values (and value (org-split-string value "[ \t]"))))
  14609. (mapcar 'org-entry-restore-space values)))
  14610. (defun org-entry-protect-space (s)
  14611. "Protect spaces and newline in string S."
  14612. (while (string-match " " s)
  14613. (setq s (replace-match "%20" t t s)))
  14614. (while (string-match "\n" s)
  14615. (setq s (replace-match "%0A" t t s)))
  14616. s)
  14617. (defun org-entry-restore-space (s)
  14618. "Restore spaces and newline in string S."
  14619. (while (string-match "%20" s)
  14620. (setq s (replace-match " " t t s)))
  14621. (while (string-match "%0A" s)
  14622. (setq s (replace-match "\n" t t s)))
  14623. s)
  14624. (defvar org-entry-property-inherited-from (make-marker)
  14625. "Marker pointing to the entry from where a property was inherited.
  14626. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14627. location of the entry where the inheritance search matched. If there was
  14628. no match, the marker will point nowhere.
  14629. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14630. is set.")
  14631. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14632. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14633. The search will stop at the first ancestor which has the property defined.
  14634. If the value found is \"nil\", return nil to show that the property
  14635. should be considered as undefined (this is the meaning of nil here).
  14636. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14637. (move-marker org-entry-property-inherited-from nil)
  14638. (org-with-wide-buffer
  14639. (let (value)
  14640. (catch 'exit
  14641. (while t
  14642. (let ((v (org-property--local-values property literal-nil)))
  14643. (when v
  14644. (setq value
  14645. (concat (mapconcat #'identity (delq nil v) " ")
  14646. (and value " ")
  14647. value)))
  14648. (cond
  14649. ((car v)
  14650. (org-back-to-heading t)
  14651. (move-marker org-entry-property-inherited-from (point))
  14652. (throw 'exit nil))
  14653. ((org-up-heading-safe))
  14654. (t
  14655. (let ((global
  14656. (cdr (or (assoc-string property org-file-properties t)
  14657. (assoc-string property org-global-properties t)
  14658. (assoc-string property org-global-properties-fixed t)))))
  14659. (cond ((not global))
  14660. (value (setq value (concat global " " value)))
  14661. (t (setq value global))))
  14662. (throw 'exit nil))))))
  14663. (if literal-nil value (org-not-nil value)))))
  14664. (defvar org-property-changed-functions nil
  14665. "Hook called when the value of a property has changed.
  14666. Each hook function should accept two arguments, the name of the property
  14667. and the new value.")
  14668. (defun org-entry-put (pom property value)
  14669. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14670. If the value is nil, it is converted to the empty string. If
  14671. it is not a string, an error is raised.
  14672. PROPERTY can be any regular property (see
  14673. `org-special-properties'). It can also be \"TODO\",
  14674. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14675. For the last two properties, VALUE may have any of the special
  14676. values \"earlier\" and \"later\". The function then increases or
  14677. decreases scheduled or deadline date by one day."
  14678. (cond ((null value) (setq value ""))
  14679. ((not (stringp value)) (error "Properties values should be strings")))
  14680. (org-with-point-at pom
  14681. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14682. (org-back-to-heading t)
  14683. (org-with-limited-levels (org-back-to-heading t)))
  14684. (let ((beg (point)))
  14685. (cond
  14686. ((equal property "TODO")
  14687. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14688. ((not (member value org-todo-keywords-1))
  14689. (user-error "\"%s\" is not a valid TODO state" value)))
  14690. (org-todo value)
  14691. (org-set-tags nil 'align))
  14692. ((equal property "PRIORITY")
  14693. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14694. (org-set-tags nil 'align))
  14695. ((equal property "SCHEDULED")
  14696. (forward-line)
  14697. (if (and (org-looking-at-p org-planning-line-re)
  14698. (re-search-forward
  14699. org-scheduled-time-regexp (line-end-position) t))
  14700. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14701. ((string= value "later") (org-timestamp-change 1 'day))
  14702. ((string= value "") (org-schedule '(4)))
  14703. (t (org-schedule nil value)))
  14704. (if (member value '("earlier" "later" ""))
  14705. (call-interactively #'org-schedule)
  14706. (org-schedule nil value))))
  14707. ((equal property "DEADLINE")
  14708. (forward-line)
  14709. (if (and (org-looking-at-p org-planning-line-re)
  14710. (re-search-forward
  14711. org-deadline-time-regexp (line-end-position) t))
  14712. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14713. ((string= value "later") (org-timestamp-change 1 'day))
  14714. ((string= value "") (org-deadline '(4)))
  14715. (t (org-deadline nil value)))
  14716. (if (member value '("earlier" "later" ""))
  14717. (call-interactively #'org-deadline)
  14718. (org-deadline nil value))))
  14719. ((member property org-special-properties)
  14720. (error "The %s property cannot be set with `org-entry-put'" property))
  14721. (t
  14722. (let* ((range (org-get-property-block beg 'force))
  14723. (end (cdr range))
  14724. (case-fold-search t))
  14725. (goto-char (car range))
  14726. (if (re-search-forward (org-re-property property nil t) end t)
  14727. (progn (delete-region (match-beginning 0) (match-end 0))
  14728. (goto-char (match-beginning 0)))
  14729. (goto-char end)
  14730. (insert "\n")
  14731. (backward-char))
  14732. (insert ":" property ":")
  14733. (when value (insert " " value))
  14734. (org-indent-line)))))
  14735. (run-hook-with-args 'org-property-changed-functions property value)))
  14736. (defun org-buffer-property-keys (&optional specials defaults columns)
  14737. "Get all property keys in the current buffer.
  14738. When SPECIALS is non-nil, also list the special properties that
  14739. reflect things like tags and TODO state.
  14740. When DEFAULTS is non-nil, also include properties that has
  14741. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14742. DESCRIPTION, LOCATION, and LOGGING and others.
  14743. When COLUMNS in non-nil, also include property names given in
  14744. COLUMN formats in the current buffer."
  14745. (let ((case-fold-search t)
  14746. (props (append
  14747. (and specials org-special-properties)
  14748. (and defaults (cons org-effort-property org-default-properties))
  14749. nil)))
  14750. (org-with-wide-buffer
  14751. (goto-char (point-min))
  14752. (while (re-search-forward org-property-start-re nil t)
  14753. (let ((range (org-get-property-block)))
  14754. (catch 'skip
  14755. (unless range
  14756. (when (and (not (org-before-first-heading-p))
  14757. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14758. (line-beginning-position))))
  14759. (org-get-property-block nil t))
  14760. (throw 'skip nil))
  14761. (goto-char (car range))
  14762. (let ((begin (car range))
  14763. (end (cdr range)))
  14764. ;; Make sure that found property block is not located
  14765. ;; before current point, as it would generate an infloop.
  14766. ;; It can happen, for example, in the following
  14767. ;; situation:
  14768. ;;
  14769. ;; * Headline
  14770. ;; :PROPERTIES:
  14771. ;; ...
  14772. ;; :END:
  14773. ;; *************** Inlinetask
  14774. ;; #+BEGIN_EXAMPLE
  14775. ;; :PROPERTIES:
  14776. ;; #+END_EXAMPLE
  14777. ;;
  14778. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14779. (while (< (point) end)
  14780. (let ((p (progn (looking-at org-property-re)
  14781. (org-match-string-no-properties 2))))
  14782. ;; Only add true property name, not extension symbol.
  14783. (add-to-list 'props
  14784. (if (not (org-string-match-p "\\+\\'" p)) p
  14785. (substring p 0 -1))))
  14786. (forward-line))))
  14787. (outline-next-heading)))
  14788. (when columns
  14789. (goto-char (point-min))
  14790. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14791. (let ((element (org-element-at-point)))
  14792. (when (memq (org-element-type element) '(keyword node-property))
  14793. (let ((value (org-element-property :value element))
  14794. (start 0))
  14795. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14796. (setq start (match-end 0))
  14797. (let ((p (org-match-string-no-properties 1 value)))
  14798. (unless (member-ignore-case p org-special-properties)
  14799. (add-to-list 'props p))))))))))
  14800. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14801. (defun org-property-values (key)
  14802. "List all non-nil values of property KEY in current buffer."
  14803. (org-with-wide-buffer
  14804. (goto-char (point-min))
  14805. (let ((case-fold-search t)
  14806. (re (org-re-property key))
  14807. values)
  14808. (while (re-search-forward re nil t)
  14809. (add-to-list 'values (org-entry-get (point) key)))
  14810. values)))
  14811. (defun org-insert-property-drawer ()
  14812. "Insert a property drawer into the current entry."
  14813. (org-with-wide-buffer
  14814. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14815. (org-back-to-heading t)
  14816. (org-with-limited-levels (org-back-to-heading t)))
  14817. (forward-line)
  14818. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14819. (unless (org-looking-at-p org-property-drawer-re)
  14820. (let ((inhibit-read-only t))
  14821. (unless (bolp) (insert "\n"))
  14822. (let ((begin (point)))
  14823. (insert ":PROPERTIES:\n:END:\n")
  14824. (org-indent-region begin (point)))))))
  14825. (defun org-insert-drawer (&optional arg drawer)
  14826. "Insert a drawer at point.
  14827. When optional argument ARG is non-nil, insert a property drawer.
  14828. Optional argument DRAWER, when non-nil, is a string representing
  14829. drawer's name. Otherwise, the user is prompted for a name.
  14830. If a region is active, insert the drawer around that region
  14831. instead.
  14832. Point is left between drawer's boundaries."
  14833. (interactive "P")
  14834. (let* ((drawer (if arg "PROPERTIES"
  14835. (or drawer (read-from-minibuffer "Drawer: ")))))
  14836. (cond
  14837. ;; With C-u, fall back on `org-insert-property-drawer'
  14838. (arg (org-insert-property-drawer))
  14839. ;; Check validity of suggested drawer's name.
  14840. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14841. (user-error "Invalid drawer name"))
  14842. ;; With an active region, insert a drawer at point.
  14843. ((not (org-region-active-p))
  14844. (progn
  14845. (unless (bolp) (insert "\n"))
  14846. (insert (format ":%s:\n\n:END:\n" drawer))
  14847. (forward-line -2)))
  14848. ;; Otherwise, insert the drawer at point
  14849. (t
  14850. (let ((rbeg (region-beginning))
  14851. (rend (copy-marker (region-end))))
  14852. (unwind-protect
  14853. (progn
  14854. (goto-char rbeg)
  14855. (beginning-of-line)
  14856. (when (save-excursion
  14857. (re-search-forward org-outline-regexp-bol rend t))
  14858. (user-error "Drawers cannot contain headlines"))
  14859. ;; Position point at the beginning of the first
  14860. ;; non-blank line in region. Insert drawer's opening
  14861. ;; there, then indent it.
  14862. (org-skip-whitespace)
  14863. (beginning-of-line)
  14864. (insert ":" drawer ":\n")
  14865. (forward-line -1)
  14866. (indent-for-tab-command)
  14867. ;; Move point to the beginning of the first blank line
  14868. ;; after the last non-blank line in region. Insert
  14869. ;; drawer's closing, then indent it.
  14870. (goto-char rend)
  14871. (skip-chars-backward " \r\t\n")
  14872. (insert "\n:END:")
  14873. (deactivate-mark t)
  14874. (indent-for-tab-command)
  14875. (unless (eolp) (insert "\n")))
  14876. ;; Clear marker, whatever the outcome of insertion is.
  14877. (set-marker rend nil)))))))
  14878. (defvar org-property-set-functions-alist nil
  14879. "Property set function alist.
  14880. Each entry should have the following format:
  14881. (PROPERTY . READ-FUNCTION)
  14882. The read function will be called with the same argument as
  14883. `org-completing-read'.")
  14884. (defun org-set-property-function (property)
  14885. "Get the function that should be used to set PROPERTY.
  14886. This is computed according to `org-property-set-functions-alist'."
  14887. (or (cdr (assoc property org-property-set-functions-alist))
  14888. 'org-completing-read))
  14889. (defun org-read-property-value (property)
  14890. "Read PROPERTY value from user."
  14891. (let* ((completion-ignore-case t)
  14892. (allowed (org-property-get-allowed-values nil property 'table))
  14893. (cur (org-entry-get nil property))
  14894. (prompt (concat property " value"
  14895. (if (and cur (string-match "\\S-" cur))
  14896. (concat " [" cur "]") "") ": "))
  14897. (set-function (org-set-property-function property))
  14898. (val (if allowed
  14899. (funcall set-function prompt allowed nil
  14900. (not (get-text-property 0 'org-unrestricted
  14901. (caar allowed))))
  14902. (let (org-completion-use-ido org-completion-use-iswitchb)
  14903. (funcall set-function prompt
  14904. (mapcar 'list (org-property-values property))
  14905. nil nil "" nil cur)))))
  14906. (org-trim val)))
  14907. (defvar org-last-set-property nil)
  14908. (defvar org-last-set-property-value nil)
  14909. (defun org-read-property-name ()
  14910. "Read a property name."
  14911. (let ((completion-ignore-case t)
  14912. (default-prop (or (and (org-at-property-p)
  14913. (org-match-string-no-properties 2))
  14914. org-last-set-property)))
  14915. (org-completing-read
  14916. (concat "Property"
  14917. (if default-prop (concat " [" default-prop "]") "")
  14918. ": ")
  14919. (mapcar #'list (org-buffer-property-keys nil t t))
  14920. nil nil nil nil default-prop)))
  14921. (defun org-set-property-and-value (use-last)
  14922. "Allow to set [PROPERTY]: [value] direction from prompt.
  14923. When use-default, don't even ask, just use the last
  14924. \"[PROPERTY]: [value]\" string from the history."
  14925. (interactive "P")
  14926. (let* ((completion-ignore-case t)
  14927. (pv (or (and use-last org-last-set-property-value)
  14928. (org-completing-read
  14929. "Enter a \"[Property]: [value]\" pair: "
  14930. nil nil nil nil nil
  14931. org-last-set-property-value)))
  14932. prop val)
  14933. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14934. (setq prop (match-string 1 pv)
  14935. val (match-string 2 pv))
  14936. (org-set-property prop val))))
  14937. (defun org-set-property (property value)
  14938. "In the current entry, set PROPERTY to VALUE.
  14939. When called interactively, this will prompt for a property name, offering
  14940. completion on existing and default properties. And then it will prompt
  14941. for a value, offering completion either on allowed values (via an inherited
  14942. xxx_ALL property) or on existing values in other instances of this property
  14943. in the current file."
  14944. (interactive (list nil nil))
  14945. (let* ((property (or property (org-read-property-name)))
  14946. (value (or value (org-read-property-value property)))
  14947. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14948. (setq org-last-set-property property)
  14949. (setq org-last-set-property-value (concat property ": " value))
  14950. ;; Possibly postprocess the inserted value:
  14951. (when fn (setq value (funcall fn value)))
  14952. (unless (equal (org-entry-get nil property) value)
  14953. (org-entry-put nil property value))))
  14954. (defun org-find-property (property &optional value)
  14955. "Find first entry in buffer that sets PROPERTY.
  14956. When optional argument VALUE is non-nil, only consider an entry
  14957. if it contains PROPERTY set to this value. If PROPERTY should be
  14958. explicitly set to nil, use string \"nil\" for VALUE.
  14959. Return position where the entry begins, or nil if there is no
  14960. such entry. If narrowing is in effect, only search the visible
  14961. part of the buffer."
  14962. (save-excursion
  14963. (goto-char (point-min))
  14964. (let ((case-fold-search t)
  14965. (re (org-re-property property nil (not value) value)))
  14966. (catch 'exit
  14967. (while (re-search-forward re nil t)
  14968. (when (if value (org-at-property-p)
  14969. (org-entry-get (point) property nil t))
  14970. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14971. (defun org-delete-property (property)
  14972. "In the current entry, delete PROPERTY."
  14973. (interactive
  14974. (let* ((completion-ignore-case t)
  14975. (cat (org-entry-get (point) "CATEGORY"))
  14976. (props0 (org-entry-properties nil 'standard))
  14977. (props (if cat props0
  14978. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14979. (prop (if (< 1 (length props))
  14980. (org-icompleting-read "Property: " props nil t)
  14981. (caar props))))
  14982. (list prop)))
  14983. (if (not property)
  14984. (message "No property to delete in this entry")
  14985. (org-entry-delete nil property)
  14986. (message "Property \"%s\" deleted" property)))
  14987. (defun org-delete-property-globally (property)
  14988. "Remove PROPERTY globally, from all entries.
  14989. This function ignores narrowing, if any."
  14990. (interactive
  14991. (let* ((completion-ignore-case t)
  14992. (prop (org-icompleting-read
  14993. "Globally remove property: "
  14994. (mapcar #'list (org-buffer-property-keys)))))
  14995. (list prop)))
  14996. (org-with-wide-buffer
  14997. (goto-char (point-min))
  14998. (let ((count 0)
  14999. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15000. (while (re-search-forward re nil t)
  15001. (when (org-entry-delete (point) property) (incf count)))
  15002. (message "Property \"%s\" removed from %d entries" property count))))
  15003. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15004. (defun org-compute-property-at-point ()
  15005. "Compute the property at point.
  15006. This looks for an enclosing column format, extracts the operator and
  15007. then applies it to the property in the column format's scope."
  15008. (interactive)
  15009. (unless (org-at-property-p)
  15010. (user-error "Not at a property"))
  15011. (let ((prop (org-match-string-no-properties 2)))
  15012. (org-columns-get-format-and-top-level)
  15013. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15014. (user-error "No operator defined for property %s" prop))
  15015. (org-columns-compute prop)))
  15016. (defvar org-property-allowed-value-functions nil
  15017. "Hook for functions supplying allowed values for a specific property.
  15018. The functions must take a single argument, the name of the property, and
  15019. return a flat list of allowed values. If \":ETC\" is one of
  15020. the values, this means that these values are intended as defaults for
  15021. completion, but that other values should be allowed too.
  15022. The functions must return nil if they are not responsible for this
  15023. property.")
  15024. (defun org-property-get-allowed-values (pom property &optional table)
  15025. "Get allowed values for the property PROPERTY.
  15026. When TABLE is non-nil, return an alist that can directly be used for
  15027. completion."
  15028. (let (vals)
  15029. (cond
  15030. ((equal property "TODO")
  15031. (setq vals (org-with-point-at pom
  15032. (append org-todo-keywords-1 '("")))))
  15033. ((equal property "PRIORITY")
  15034. (let ((n org-lowest-priority))
  15035. (while (>= n org-highest-priority)
  15036. (push (char-to-string n) vals)
  15037. (setq n (1- n)))))
  15038. ((equal property "CATEGORY"))
  15039. ((member property org-special-properties))
  15040. ((setq vals (run-hook-with-args-until-success
  15041. 'org-property-allowed-value-functions property)))
  15042. (t
  15043. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15044. (when (and vals (string-match "\\S-" vals))
  15045. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15046. (setq vals (mapcar (lambda (x)
  15047. (cond ((stringp x) x)
  15048. ((numberp x) (number-to-string x))
  15049. ((symbolp x) (symbol-name x))
  15050. (t "???")))
  15051. vals)))))
  15052. (when (member ":ETC" vals)
  15053. (setq vals (remove ":ETC" vals))
  15054. (org-add-props (car vals) '(org-unrestricted t)))
  15055. (if table (mapcar 'list vals) vals)))
  15056. (defun org-property-previous-allowed-value (&optional previous)
  15057. "Switch to the next allowed value for this property."
  15058. (interactive)
  15059. (org-property-next-allowed-value t))
  15060. (defun org-property-next-allowed-value (&optional previous)
  15061. "Switch to the next allowed value for this property."
  15062. (interactive)
  15063. (unless (org-at-property-p)
  15064. (user-error "Not at a property"))
  15065. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15066. (key (match-string 2))
  15067. (value (match-string 3))
  15068. (allowed (or (org-property-get-allowed-values (point) key)
  15069. (and (member value '("[ ]" "[-]" "[X]"))
  15070. '("[ ]" "[X]"))))
  15071. (heading (save-match-data (nth 4 (org-heading-components))))
  15072. nval)
  15073. (unless allowed
  15074. (user-error "Allowed values for this property have not been defined"))
  15075. (if previous (setq allowed (reverse allowed)))
  15076. (if (member value allowed)
  15077. (setq nval (car (cdr (member value allowed)))))
  15078. (setq nval (or nval (car allowed)))
  15079. (if (equal nval value)
  15080. (user-error "Only one allowed value for this property"))
  15081. (org-at-property-p)
  15082. (replace-match (concat " :" key ": " nval) t t)
  15083. (org-indent-line)
  15084. (beginning-of-line 1)
  15085. (skip-chars-forward " \t")
  15086. (when (equal prop org-effort-property)
  15087. (org-refresh-property
  15088. '((effort . identity)
  15089. (effort-minutes . org-duration-string-to-minutes))
  15090. nval)
  15091. (when (string= org-clock-current-task heading)
  15092. (setq org-clock-effort nval)
  15093. (org-clock-update-mode-line)))
  15094. (run-hook-with-args 'org-property-changed-functions key nval)))
  15095. (defun org-find-olp (path &optional this-buffer)
  15096. "Return a marker pointing to the entry at outline path OLP.
  15097. If anything goes wrong, throw an error.
  15098. You can wrap this call to catch the error like this:
  15099. (condition-case msg
  15100. (org-mobile-locate-entry (match-string 4))
  15101. (error (nth 1 msg)))
  15102. The return value will then be either a string with the error message,
  15103. or a marker if everything is OK.
  15104. If THIS-BUFFER is set, the outline path does not contain a file,
  15105. only headings."
  15106. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15107. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15108. (level 1)
  15109. (lmin 1)
  15110. (lmax 1)
  15111. limit re end found pos heading cnt flevel)
  15112. (unless buffer (error "File not found :%s" file))
  15113. (with-current-buffer buffer
  15114. (save-excursion
  15115. (save-restriction
  15116. (widen)
  15117. (setq limit (point-max))
  15118. (goto-char (point-min))
  15119. (dolist (heading path)
  15120. (setq re (format org-complex-heading-regexp-format
  15121. (regexp-quote heading)))
  15122. (setq cnt 0 pos (point))
  15123. (while (re-search-forward re end t)
  15124. (setq level (- (match-end 1) (match-beginning 1)))
  15125. (if (and (>= level lmin) (<= level lmax))
  15126. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15127. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15128. lmax heading))
  15129. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15130. lmax heading))
  15131. (goto-char found)
  15132. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15133. (setq end (save-excursion (org-end-of-subtree t t))))
  15134. (when (org-at-heading-p)
  15135. (point-marker)))))))
  15136. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15137. "Find node HEADING in BUFFER.
  15138. Return a marker to the heading if it was found, or nil if not.
  15139. If POS-ONLY is set, return just the position instead of a marker.
  15140. The heading text must match exact, but it may have a TODO keyword,
  15141. a priority cookie and tags in the standard locations."
  15142. (with-current-buffer (or buffer (current-buffer))
  15143. (save-excursion
  15144. (save-restriction
  15145. (widen)
  15146. (goto-char (point-min))
  15147. (let (case-fold-search)
  15148. (if (re-search-forward
  15149. (format org-complex-heading-regexp-format
  15150. (regexp-quote heading)) nil t)
  15151. (if pos-only
  15152. (match-beginning 0)
  15153. (move-marker (make-marker) (match-beginning 0)))))))))
  15154. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15155. "Find Org node headline HEADING in all .org files in directory DIR.
  15156. When the target headline is found, return a marker to this location."
  15157. (let ((files (directory-files (or dir default-directory)
  15158. t "\\`[^.#].*\\.org\\'"))
  15159. visiting m buffer)
  15160. (catch 'found
  15161. (dolist (file files)
  15162. (message "trying %s" file)
  15163. (setq visiting (org-find-base-buffer-visiting file))
  15164. (setq buffer (or visiting (find-file-noselect file)))
  15165. (setq m (org-find-exact-headline-in-buffer
  15166. heading buffer))
  15167. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15168. (and m (throw 'found m))))))
  15169. (defun org-find-entry-with-id (ident)
  15170. "Locate the entry that contains the ID property with exact value IDENT.
  15171. IDENT can be a string, a symbol or a number, this function will search for
  15172. the string representation of it.
  15173. Return the position where this entry starts, or nil if there is no such entry."
  15174. (interactive "sID: ")
  15175. (let ((id (cond
  15176. ((stringp ident) ident)
  15177. ((symbolp ident) (symbol-name ident))
  15178. ((numberp ident) (number-to-string ident))
  15179. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15180. (org-with-wide-buffer (org-find-property "ID" id))))
  15181. ;;;; Timestamps
  15182. (defvar org-last-changed-timestamp nil)
  15183. (defvar org-last-inserted-timestamp nil
  15184. "The last time stamp inserted with `org-insert-time-stamp'.")
  15185. (defvar org-ts-what) ; dynamically scoped parameter
  15186. (defun org-time-stamp (arg &optional inactive)
  15187. "Prompt for a date/time and insert a time stamp.
  15188. If the user specifies a time like HH:MM or if this command is
  15189. called with at least one prefix argument, the time stamp contains
  15190. the date and the time. Otherwise, only the date is included.
  15191. All parts of a date not specified by the user are filled in from
  15192. the timestamp at point, if any, or the current date/time
  15193. otherwise.
  15194. If there is already a timestamp at the cursor, it is replaced.
  15195. With two universal prefix arguments, insert an active timestamp
  15196. with the current time without prompting the user.
  15197. When called from lisp, the timestamp is inactive if INACTIVE is
  15198. non-nil."
  15199. (interactive "P")
  15200. (let* ((ts (cond
  15201. ((org-at-date-range-p t)
  15202. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15203. ((org-at-timestamp-p t) (match-string 0))))
  15204. ;; Default time is either the timestamp at point or today.
  15205. ;; When entering a range, only the range start is considered.
  15206. (default-time (if (not ts) (current-time)
  15207. (apply #'encode-time (org-parse-time-string ts))))
  15208. (default-input (and ts (org-get-compact-tod ts)))
  15209. (repeater (and ts
  15210. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15211. (match-string 0 ts)))
  15212. org-time-was-given
  15213. org-end-time-was-given
  15214. (time
  15215. (and (if (equal arg '(16)) (current-time)
  15216. ;; Preserve `this-command' and `last-command'.
  15217. (let ((this-command this-command)
  15218. (last-command last-command))
  15219. (org-read-date
  15220. arg 'totime nil nil default-time default-input
  15221. inactive))))))
  15222. (cond
  15223. ((and ts
  15224. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15225. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15226. (insert "--")
  15227. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15228. (ts
  15229. ;; Make sure we're on a timestamp. When in the middle of a date
  15230. ;; range, move arbitrarily to range end.
  15231. (unless (org-at-timestamp-p t)
  15232. (skip-chars-forward "-")
  15233. (org-at-timestamp-p t))
  15234. (replace-match "")
  15235. (setq org-last-changed-timestamp
  15236. (org-insert-time-stamp
  15237. time (or org-time-was-given arg)
  15238. inactive nil nil (list org-end-time-was-given)))
  15239. (when repeater
  15240. (backward-char)
  15241. (insert " " repeater)
  15242. (setq org-last-changed-timestamp
  15243. (concat (substring org-last-inserted-timestamp 0 -1)
  15244. " " repeater ">")))
  15245. (message "Timestamp updated"))
  15246. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15247. (t (org-insert-time-stamp
  15248. time (or org-time-was-given arg) inactive nil nil
  15249. (list org-end-time-was-given))))))
  15250. ;; FIXME: can we use this for something else, like computing time differences?
  15251. (defun org-get-compact-tod (s)
  15252. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15253. (let* ((t1 (match-string 1 s))
  15254. (h1 (string-to-number (match-string 2 s)))
  15255. (m1 (string-to-number (match-string 3 s)))
  15256. (t2 (and (match-end 4) (match-string 5 s)))
  15257. (h2 (and t2 (string-to-number (match-string 6 s))))
  15258. (m2 (and t2 (string-to-number (match-string 7 s))))
  15259. dh dm)
  15260. (if (not t2)
  15261. t1
  15262. (setq dh (- h2 h1) dm (- m2 m1))
  15263. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15264. (concat t1 "+" (number-to-string dh)
  15265. (and (/= 0 dm) (format ":%02d" dm)))))))
  15266. (defun org-time-stamp-inactive (&optional arg)
  15267. "Insert an inactive time stamp.
  15268. An inactive time stamp is enclosed in square brackets instead of angle
  15269. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15270. does not link to the calendar and cannot be changed with the S-cursor keys.
  15271. So these are more for recording a certain time/date."
  15272. (interactive "P")
  15273. (org-time-stamp arg 'inactive))
  15274. (defvar org-date-ovl (make-overlay 1 1))
  15275. (overlay-put org-date-ovl 'face 'org-date-selected)
  15276. (org-detach-overlay org-date-ovl)
  15277. (defvar org-ans1) ; dynamically scoped parameter
  15278. (defvar org-ans2) ; dynamically scoped parameter
  15279. (defvar org-plain-time-of-day-regexp) ; defined below
  15280. (defvar org-overriding-default-time nil) ; dynamically scoped
  15281. (defvar org-read-date-overlay nil)
  15282. (defvar org-dcst nil) ; dynamically scoped
  15283. (defvar org-read-date-history nil)
  15284. (defvar org-read-date-final-answer nil)
  15285. (defvar org-read-date-analyze-futurep nil)
  15286. (defvar org-read-date-analyze-forced-year nil)
  15287. (defvar org-read-date-inactive)
  15288. (defvar org-read-date-minibuffer-local-map
  15289. (let* ((map (make-sparse-keymap)))
  15290. (set-keymap-parent map minibuffer-local-map)
  15291. (org-defkey map (kbd ".")
  15292. (lambda () (interactive)
  15293. ;; Are we at the beginning of the prompt?
  15294. (if (looking-back "^[^:]+: ")
  15295. (org-eval-in-calendar '(calendar-goto-today))
  15296. (insert "."))))
  15297. (org-defkey map (kbd "C-.")
  15298. (lambda () (interactive)
  15299. (org-eval-in-calendar '(calendar-goto-today))))
  15300. (org-defkey map [(meta shift left)]
  15301. (lambda () (interactive)
  15302. (org-eval-in-calendar '(calendar-backward-month 1))))
  15303. (org-defkey map [(meta shift right)]
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar '(calendar-forward-month 1))))
  15306. (org-defkey map [(meta shift up)]
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(calendar-backward-year 1))))
  15309. (org-defkey map [(meta shift down)]
  15310. (lambda () (interactive)
  15311. (org-eval-in-calendar '(calendar-forward-year 1))))
  15312. (org-defkey map [?\e (shift left)]
  15313. (lambda () (interactive)
  15314. (org-eval-in-calendar '(calendar-backward-month 1))))
  15315. (org-defkey map [?\e (shift right)]
  15316. (lambda () (interactive)
  15317. (org-eval-in-calendar '(calendar-forward-month 1))))
  15318. (org-defkey map [?\e (shift up)]
  15319. (lambda () (interactive)
  15320. (org-eval-in-calendar '(calendar-backward-year 1))))
  15321. (org-defkey map [?\e (shift down)]
  15322. (lambda () (interactive)
  15323. (org-eval-in-calendar '(calendar-forward-year 1))))
  15324. (org-defkey map [(shift up)]
  15325. (lambda () (interactive)
  15326. (org-eval-in-calendar '(calendar-backward-week 1))))
  15327. (org-defkey map [(shift down)]
  15328. (lambda () (interactive)
  15329. (org-eval-in-calendar '(calendar-forward-week 1))))
  15330. (org-defkey map [(shift left)]
  15331. (lambda () (interactive)
  15332. (org-eval-in-calendar '(calendar-backward-day 1))))
  15333. (org-defkey map [(shift right)]
  15334. (lambda () (interactive)
  15335. (org-eval-in-calendar '(calendar-forward-day 1))))
  15336. (org-defkey map "!"
  15337. (lambda () (interactive)
  15338. (org-eval-in-calendar '(diary-view-entries))
  15339. (message "")))
  15340. (org-defkey map ">"
  15341. (lambda () (interactive)
  15342. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15343. (org-defkey map "<"
  15344. (lambda () (interactive)
  15345. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15346. (org-defkey map "\C-v"
  15347. (lambda () (interactive)
  15348. (org-eval-in-calendar
  15349. '(calendar-scroll-left-three-months 1))))
  15350. (org-defkey map "\M-v"
  15351. (lambda () (interactive)
  15352. (org-eval-in-calendar
  15353. '(calendar-scroll-right-three-months 1))))
  15354. map)
  15355. "Keymap for minibuffer commands when using `org-read-date'.")
  15356. (defvar org-def)
  15357. (defvar org-defdecode)
  15358. (defvar org-with-time)
  15359. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15360. default-time default-input inactive)
  15361. "Read a date, possibly a time, and make things smooth for the user.
  15362. The prompt will suggest to enter an ISO date, but you can also enter anything
  15363. which will at least partially be understood by `parse-time-string'.
  15364. Unrecognized parts of the date will default to the current day, month, year,
  15365. hour and minute. If this command is called to replace a timestamp at point,
  15366. or to enter the second timestamp of a range, the default time is taken
  15367. from the existing stamp. Furthermore, the command prefers the future,
  15368. so if you are giving a date where the year is not given, and the day-month
  15369. combination is already past in the current year, it will assume you
  15370. mean next year. For details, see the manual. A few examples:
  15371. 3-2-5 --> 2003-02-05
  15372. feb 15 --> currentyear-02-15
  15373. 2/15 --> currentyear-02-15
  15374. sep 12 9 --> 2009-09-12
  15375. 12:45 --> today 12:45
  15376. 22 sept 0:34 --> currentyear-09-22 0:34
  15377. 12 --> currentyear-currentmonth-12
  15378. Fri --> nearest Friday after today
  15379. -Tue --> last Tuesday
  15380. etc.
  15381. Furthermore you can specify a relative date by giving, as the *first* thing
  15382. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15383. change in days weeks, months, years.
  15384. With a single plus or minus, the date is relative to today. With a double
  15385. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15386. +4d --> four days from today
  15387. +4 --> same as above
  15388. +2w --> two weeks from today
  15389. ++5 --> five days from default date
  15390. The function understands only English month and weekday abbreviations.
  15391. While prompting, a calendar is popped up - you can also select the
  15392. date with the mouse (button 1). The calendar shows a period of three
  15393. months. To scroll it to other months, use the keys `>' and `<'.
  15394. If you don't like the calendar, turn it off with
  15395. \(setq org-read-date-popup-calendar nil)
  15396. With optional argument TO-TIME, the date will immediately be converted
  15397. to an internal time.
  15398. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15399. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15400. still enter a time, and this function will inform the calling routine
  15401. about this change. The calling routine may then choose to change the
  15402. format used to insert the time stamp into the buffer to include the time.
  15403. With optional argument FROM-STRING, read from this string instead from
  15404. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15405. the time/date that is used for everything that is not specified by the
  15406. user."
  15407. (require 'parse-time)
  15408. (let* ((org-time-stamp-rounding-minutes
  15409. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15410. (org-dcst org-display-custom-times)
  15411. (ct (org-current-time))
  15412. (org-def (or org-overriding-default-time default-time ct))
  15413. (org-defdecode (decode-time org-def))
  15414. (dummy (progn
  15415. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15416. (setcar (nthcdr 2 org-defdecode) -1)
  15417. (setcar (nthcdr 1 org-defdecode) 59)
  15418. (setq org-def (apply 'encode-time org-defdecode)
  15419. org-defdecode (decode-time org-def)))))
  15420. (cur-frame (selected-frame))
  15421. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15422. (calendar-frame-setup nil)
  15423. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15424. (calendar-move-hook nil)
  15425. (calendar-view-diary-initially-flag nil)
  15426. (calendar-view-holidays-initially-flag nil)
  15427. (timestr (format-time-string
  15428. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15429. (prompt (concat (if prompt (concat prompt " ") "")
  15430. (format "Date+time [%s]: " timestr)))
  15431. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15432. (cond
  15433. (from-string (setq ans from-string))
  15434. (org-read-date-popup-calendar
  15435. (save-excursion
  15436. (save-window-excursion
  15437. (calendar)
  15438. (when (eq calendar-setup 'calendar-only)
  15439. (setq cal-frame
  15440. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15441. (select-frame cal-frame))
  15442. (org-eval-in-calendar '(setq cursor-type nil) t)
  15443. (unwind-protect
  15444. (progn
  15445. (calendar-forward-day (- (time-to-days org-def)
  15446. (calendar-absolute-from-gregorian
  15447. (calendar-current-date))))
  15448. (org-eval-in-calendar nil t)
  15449. (let* ((old-map (current-local-map))
  15450. (map (copy-keymap calendar-mode-map))
  15451. (minibuffer-local-map
  15452. (copy-keymap org-read-date-minibuffer-local-map)))
  15453. (org-defkey map (kbd "RET") 'org-calendar-select)
  15454. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15455. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15456. (unwind-protect
  15457. (progn
  15458. (use-local-map map)
  15459. (setq org-read-date-inactive inactive)
  15460. (add-hook 'post-command-hook 'org-read-date-display)
  15461. (setq org-ans0 (read-string prompt default-input
  15462. 'org-read-date-history nil))
  15463. ;; org-ans0: from prompt
  15464. ;; org-ans1: from mouse click
  15465. ;; org-ans2: from calendar motion
  15466. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15467. (remove-hook 'post-command-hook 'org-read-date-display)
  15468. (use-local-map old-map)
  15469. (when org-read-date-overlay
  15470. (delete-overlay org-read-date-overlay)
  15471. (setq org-read-date-overlay nil)))))
  15472. (bury-buffer "*Calendar*")
  15473. (when cal-frame
  15474. (delete-frame cal-frame)
  15475. (select-frame-set-input-focus cur-frame))))))
  15476. (t ; Naked prompt only
  15477. (unwind-protect
  15478. (setq ans (read-string prompt default-input
  15479. 'org-read-date-history timestr))
  15480. (when org-read-date-overlay
  15481. (delete-overlay org-read-date-overlay)
  15482. (setq org-read-date-overlay nil)))))
  15483. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15484. (when org-read-date-analyze-forced-year
  15485. (message "Year was forced into %s"
  15486. (if org-read-date-force-compatible-dates
  15487. "compatible range (1970-2037)"
  15488. "range representable on this machine"))
  15489. (ding))
  15490. ;; One round trip to get rid of 34th of August and stuff like that....
  15491. (setq final (decode-time (apply 'encode-time final)))
  15492. (setq org-read-date-final-answer ans)
  15493. (if to-time
  15494. (apply 'encode-time final)
  15495. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15496. (format "%04d-%02d-%02d %02d:%02d"
  15497. (nth 5 final) (nth 4 final) (nth 3 final)
  15498. (nth 2 final) (nth 1 final))
  15499. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15500. (defun org-read-date-display ()
  15501. "Display the current date prompt interpretation in the minibuffer."
  15502. (when org-read-date-display-live
  15503. (when org-read-date-overlay
  15504. (delete-overlay org-read-date-overlay))
  15505. (when (minibufferp (current-buffer))
  15506. (save-excursion
  15507. (end-of-line 1)
  15508. (while (not (equal (buffer-substring
  15509. (max (point-min) (- (point) 4)) (point))
  15510. " "))
  15511. (insert " ")))
  15512. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15513. " " (or org-ans1 org-ans2)))
  15514. (org-end-time-was-given nil)
  15515. (f (org-read-date-analyze ans org-def org-defdecode))
  15516. (fmts (if org-dcst
  15517. org-time-stamp-custom-formats
  15518. org-time-stamp-formats))
  15519. (fmt (if (or org-with-time
  15520. (and (boundp 'org-time-was-given) org-time-was-given))
  15521. (cdr fmts)
  15522. (car fmts)))
  15523. (txt (format-time-string fmt (apply 'encode-time f)))
  15524. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15525. (txt (concat "=> " txt)))
  15526. (when (and org-end-time-was-given
  15527. (string-match org-plain-time-of-day-regexp txt))
  15528. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15529. org-end-time-was-given
  15530. (substring txt (match-end 0)))))
  15531. (when org-read-date-analyze-futurep
  15532. (setq txt (concat txt " (=>F)")))
  15533. (setq org-read-date-overlay
  15534. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15535. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15536. (defun org-read-date-analyze (ans org-def org-defdecode)
  15537. "Analyze the combined answer of the date prompt."
  15538. ;; FIXME: cleanup and comment
  15539. ;; Pass `current-time' result to `decode-time' (instead of calling
  15540. ;; without arguments) so that only `current-time' has to be
  15541. ;; overriden in tests.
  15542. (let ((nowdecode (decode-time (current-time)))
  15543. delta deltan deltaw deltadef year month day
  15544. hour minute second wday pm h2 m2 tl wday1
  15545. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15546. (setq org-read-date-analyze-futurep nil
  15547. org-read-date-analyze-forced-year nil)
  15548. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15549. (setq ans "+0"))
  15550. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15551. (setq ans (replace-match "" t t ans)
  15552. deltan (car delta)
  15553. deltaw (nth 1 delta)
  15554. deltadef (nth 2 delta)))
  15555. ;; Check if there is an iso week date in there. If yes, store the
  15556. ;; info and postpone interpreting it until the rest of the parsing
  15557. ;; is done.
  15558. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15559. (setq iso-year (if (match-end 1)
  15560. (org-small-year-to-year
  15561. (string-to-number (match-string 1 ans))))
  15562. iso-weekday (if (match-end 3)
  15563. (string-to-number (match-string 3 ans)))
  15564. iso-week (string-to-number (match-string 2 ans)))
  15565. (setq ans (replace-match "" t t ans)))
  15566. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15567. (when (string-match
  15568. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15569. (setq year (if (match-end 2)
  15570. (string-to-number (match-string 2 ans))
  15571. (progn (setq kill-year t)
  15572. (string-to-number (format-time-string "%Y"))))
  15573. month (string-to-number (match-string 3 ans))
  15574. day (string-to-number (match-string 4 ans)))
  15575. (if (< year 100) (setq year (+ 2000 year)))
  15576. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15577. t nil ans)))
  15578. ;; Help matching dotted european dates
  15579. (when (string-match
  15580. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15581. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15582. (setq kill-year t)
  15583. (string-to-number (format-time-string "%Y")))
  15584. day (string-to-number (match-string 1 ans))
  15585. month (string-to-number (match-string 2 ans))
  15586. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15587. t nil ans)))
  15588. ;; Help matching american dates, like 5/30 or 5/30/7
  15589. (when (string-match
  15590. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15591. (setq year (if (match-end 4)
  15592. (string-to-number (match-string 4 ans))
  15593. (progn (setq kill-year t)
  15594. (string-to-number (format-time-string "%Y"))))
  15595. month (string-to-number (match-string 1 ans))
  15596. day (string-to-number (match-string 2 ans)))
  15597. (if (< year 100) (setq year (+ 2000 year)))
  15598. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15599. t nil ans)))
  15600. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15601. ;; If there is a time with am/pm, and *no* time without it, we convert
  15602. ;; so that matching will be successful.
  15603. (loop for i from 1 to 2 do ; twice, for end time as well
  15604. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15605. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15606. (setq hour (string-to-number (match-string 1 ans))
  15607. minute (if (match-end 3)
  15608. (string-to-number (match-string 3 ans))
  15609. 0)
  15610. pm (equal ?p
  15611. (string-to-char (downcase (match-string 4 ans)))))
  15612. (if (and (= hour 12) (not pm))
  15613. (setq hour 0)
  15614. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15615. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15616. t t ans))))
  15617. ;; Check if a time range is given as a duration
  15618. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15619. (setq hour (string-to-number (match-string 1 ans))
  15620. h2 (+ hour (string-to-number (match-string 3 ans)))
  15621. minute (string-to-number (match-string 2 ans))
  15622. m2 (+ minute (if (match-end 5) (string-to-number
  15623. (match-string 5 ans))0)))
  15624. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15625. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15626. t t ans)))
  15627. ;; Check if there is a time range
  15628. (when (boundp 'org-end-time-was-given)
  15629. (setq org-time-was-given nil)
  15630. (when (and (string-match org-plain-time-of-day-regexp ans)
  15631. (match-end 8))
  15632. (setq org-end-time-was-given (match-string 8 ans))
  15633. (setq ans (concat (substring ans 0 (match-beginning 7))
  15634. (substring ans (match-end 7))))))
  15635. (setq tl (parse-time-string ans)
  15636. day (or (nth 3 tl) (nth 3 org-defdecode))
  15637. month
  15638. (cond ((nth 4 tl))
  15639. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15640. ;; Day was specified. Make sure DAY+MONTH
  15641. ;; combination happens in the future.
  15642. ((nth 3 tl)
  15643. (setq futurep t)
  15644. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15645. (nth 4 nowdecode)))
  15646. (t (nth 4 org-defdecode)))
  15647. year
  15648. (cond ((and (not kill-year) (nth 5 tl)))
  15649. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15650. ;; Month was guessed in the future and is at least
  15651. ;; equal to NOWDECODE's. Fix year accordingly.
  15652. (futurep
  15653. (if (or (> month (nth 4 nowdecode))
  15654. (>= day (nth 3 nowdecode)))
  15655. (nth 5 nowdecode)
  15656. (1+ (nth 5 nowdecode))))
  15657. ;; Month was specified. Make sure MONTH+YEAR
  15658. ;; combination happens in the future.
  15659. ((nth 4 tl)
  15660. (setq futurep t)
  15661. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15662. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15663. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15664. (t (nth 5 nowdecode))))
  15665. (t (nth 5 org-defdecode)))
  15666. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15667. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15668. second (or (nth 0 tl) 0)
  15669. wday (nth 6 tl))
  15670. (when (and (eq org-read-date-prefer-future 'time)
  15671. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15672. (equal day (nth 3 nowdecode))
  15673. (equal month (nth 4 nowdecode))
  15674. (equal year (nth 5 nowdecode))
  15675. (nth 2 tl)
  15676. (or (< (nth 2 tl) (nth 2 nowdecode))
  15677. (and (= (nth 2 tl) (nth 2 nowdecode))
  15678. (nth 1 tl)
  15679. (< (nth 1 tl) (nth 1 nowdecode)))))
  15680. (setq day (1+ day)
  15681. futurep t))
  15682. ;; Special date definitions below
  15683. (cond
  15684. (iso-week
  15685. ;; There was an iso week
  15686. (require 'cal-iso)
  15687. (setq futurep nil)
  15688. (setq year (or iso-year year)
  15689. day (or iso-weekday wday 1)
  15690. wday nil ; to make sure that the trigger below does not match
  15691. iso-date (calendar-gregorian-from-absolute
  15692. (calendar-iso-to-absolute
  15693. (list iso-week day year))))
  15694. ; FIXME: Should we also push ISO weeks into the future?
  15695. ; (when (and org-read-date-prefer-future
  15696. ; (not iso-year)
  15697. ; (< (calendar-absolute-from-gregorian iso-date)
  15698. ; (time-to-days (current-time))))
  15699. ; (setq year (1+ year)
  15700. ; iso-date (calendar-gregorian-from-absolute
  15701. ; (calendar-iso-to-absolute
  15702. ; (list iso-week day year)))))
  15703. (setq month (car iso-date)
  15704. year (nth 2 iso-date)
  15705. day (nth 1 iso-date)))
  15706. (deltan
  15707. (setq futurep nil)
  15708. (unless deltadef
  15709. ;; Pass `current-time' result to `decode-time' (instead of
  15710. ;; calling without arguments) so that only `current-time' has
  15711. ;; to be overriden in tests.
  15712. (let ((now (decode-time (current-time))))
  15713. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15714. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15715. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15716. ((equal deltaw "m") (setq month (+ month deltan)))
  15717. ((equal deltaw "y") (setq year (+ year deltan)))))
  15718. ((and wday (not (nth 3 tl)))
  15719. ;; Weekday was given, but no day, so pick that day in the week
  15720. ;; on or after the derived date.
  15721. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15722. (unless (equal wday wday1)
  15723. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15724. (if (and (boundp 'org-time-was-given)
  15725. (nth 2 tl))
  15726. (setq org-time-was-given t))
  15727. (if (< year 100) (setq year (+ 2000 year)))
  15728. ;; Check of the date is representable
  15729. (if org-read-date-force-compatible-dates
  15730. (progn
  15731. (if (< year 1970)
  15732. (setq year 1970 org-read-date-analyze-forced-year t))
  15733. (if (> year 2037)
  15734. (setq year 2037 org-read-date-analyze-forced-year t)))
  15735. (condition-case nil
  15736. (ignore (encode-time second minute hour day month year))
  15737. (error
  15738. (setq year (nth 5 org-defdecode))
  15739. (setq org-read-date-analyze-forced-year t))))
  15740. (setq org-read-date-analyze-futurep futurep)
  15741. (list second minute hour day month year)))
  15742. (defvar parse-time-weekdays)
  15743. (defun org-read-date-get-relative (s today default)
  15744. "Check string S for special relative date string.
  15745. TODAY and DEFAULT are internal times, for today and for a default.
  15746. Return shift list (N what def-flag)
  15747. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15748. N is the number of WHATs to shift.
  15749. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15750. the DEFAULT date rather than TODAY."
  15751. (require 'parse-time)
  15752. (when (and
  15753. (string-match
  15754. (concat
  15755. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15756. "\\([0-9]+\\)?"
  15757. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15758. "\\([ \t]\\|$\\)") s)
  15759. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15760. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15761. (string-to-char (substring (match-string 1 s) -1))
  15762. ?+))
  15763. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15764. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15765. (what (if (match-end 3) (match-string 3 s) "d"))
  15766. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15767. (date (if rel default today))
  15768. (wday (nth 6 (decode-time date)))
  15769. delta)
  15770. (if wday1
  15771. (progn
  15772. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15773. (if (= delta 0) (setq delta 7))
  15774. (if (= dir ?-)
  15775. (progn
  15776. (setq delta (- delta 7))
  15777. (if (= delta 0) (setq delta -7))))
  15778. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15779. (list delta "d" rel))
  15780. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15781. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15782. "Turn a user-specified date into the internal representation.
  15783. The internal representation needed by the calendar is (month day year).
  15784. This is a wrapper to handle the brain-dead convention in calendar that
  15785. user function argument order change dependent on argument order."
  15786. (if (boundp 'calendar-date-style)
  15787. (cond
  15788. ((eq calendar-date-style 'american)
  15789. (list arg1 arg2 arg3))
  15790. ((eq calendar-date-style 'european)
  15791. (list arg2 arg1 arg3))
  15792. ((eq calendar-date-style 'iso)
  15793. (list arg2 arg3 arg1)))
  15794. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15795. (if (org-bound-and-true-p european-calendar-style)
  15796. (list arg2 arg1 arg3)
  15797. (list arg1 arg2 arg3)))))
  15798. (defun org-eval-in-calendar (form &optional keepdate)
  15799. "Eval FORM in the calendar window and return to current window.
  15800. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15801. otherwise stick to the current value of `org-ans2'."
  15802. (let ((sf (selected-frame))
  15803. (sw (selected-window)))
  15804. (select-window (get-buffer-window "*Calendar*" t))
  15805. (eval form)
  15806. (when (and (not keepdate) (calendar-cursor-to-date))
  15807. (let* ((date (calendar-cursor-to-date))
  15808. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15809. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15810. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15811. (select-window sw)
  15812. (org-select-frame-set-input-focus sf)))
  15813. (defun org-calendar-select ()
  15814. "Return to `org-read-date' with the date currently selected.
  15815. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15816. (interactive)
  15817. (when (calendar-cursor-to-date)
  15818. (let* ((date (calendar-cursor-to-date))
  15819. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15820. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15821. (if (active-minibuffer-window) (exit-minibuffer))))
  15822. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15823. "Insert a date stamp for the date given by the internal TIME.
  15824. See `format-time-string' for the format of TIME.
  15825. WITH-HM means use the stamp format that includes the time of the day.
  15826. INACTIVE means use square brackets instead of angular ones, so that the
  15827. stamp will not contribute to the agenda.
  15828. PRE and POST are optional strings to be inserted before and after the
  15829. stamp.
  15830. The command returns the inserted time stamp."
  15831. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15832. stamp)
  15833. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15834. (insert-before-markers (or pre ""))
  15835. (when (listp extra)
  15836. (setq extra (car extra))
  15837. (if (and (stringp extra)
  15838. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15839. (setq extra (format "-%02d:%02d"
  15840. (string-to-number (match-string 1 extra))
  15841. (string-to-number (match-string 2 extra))))
  15842. (setq extra nil)))
  15843. (when extra
  15844. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15845. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15846. (insert-before-markers (or post ""))
  15847. (setq org-last-inserted-timestamp stamp)))
  15848. (defun org-toggle-time-stamp-overlays ()
  15849. "Toggle the use of custom time stamp formats."
  15850. (interactive)
  15851. (setq org-display-custom-times (not org-display-custom-times))
  15852. (unless org-display-custom-times
  15853. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15854. (while (setq p (next-single-property-change p 'display))
  15855. (if (and (get-text-property p 'display)
  15856. (eq (get-text-property p 'face) 'org-date))
  15857. (remove-text-properties
  15858. p (setq p (next-single-property-change p 'display))
  15859. '(display t))))
  15860. (set-buffer-modified-p bmp)))
  15861. (if (featurep 'xemacs)
  15862. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15863. (org-restart-font-lock)
  15864. (setq org-table-may-need-update t)
  15865. (if org-display-custom-times
  15866. (message "Time stamps are overlaid with custom format")
  15867. (message "Time stamp overlays removed")))
  15868. (defun org-display-custom-time (beg end)
  15869. "Overlay modified time stamp format over timestamp between BEG and END."
  15870. (let* ((ts (buffer-substring beg end))
  15871. t1 w1 with-hm tf time str w2 (off 0))
  15872. (save-match-data
  15873. (setq t1 (org-parse-time-string ts t))
  15874. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15875. (setq off (- (match-end 0) (match-beginning 0)))))
  15876. (setq end (- end off))
  15877. (setq w1 (- end beg)
  15878. with-hm (and (nth 1 t1) (nth 2 t1))
  15879. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15880. time (org-fix-decoded-time t1)
  15881. str (org-add-props
  15882. (format-time-string
  15883. (substring tf 1 -1) (apply 'encode-time time))
  15884. nil 'mouse-face 'highlight)
  15885. w2 (length str))
  15886. (if (not (= w2 w1))
  15887. (add-text-properties (1+ beg) (+ 2 beg)
  15888. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15889. (if (featurep 'xemacs)
  15890. (progn
  15891. (put-text-property beg end 'invisible t)
  15892. (put-text-property beg end 'end-glyph (make-glyph str)))
  15893. (put-text-property beg end 'display str))))
  15894. (defun org-fix-decoded-time (time)
  15895. "Set 0 instead of nil for the first 6 elements of time.
  15896. Don't touch the rest."
  15897. (let ((n 0))
  15898. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15899. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15900. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15901. "Difference between TIMESTAMP-STRING and now in days.
  15902. If SECONDS is non-nil, return the difference in seconds."
  15903. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15904. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15905. (funcall fdiff (current-time)))))
  15906. (defun org-deadline-close (timestamp-string &optional ndays)
  15907. "Is the time in TIMESTAMP-STRING close to the current date?"
  15908. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15909. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15910. (not (org-entry-is-done-p))))
  15911. (defun org-get-wdays (ts &optional delay zero-delay)
  15912. "Get the deadline lead time appropriate for timestring TS.
  15913. When DELAY is non-nil, get the delay time for scheduled items
  15914. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15915. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15916. don't try to find the delay cookie in the scheduled timestamp."
  15917. (let ((tv (if delay org-scheduled-delay-days
  15918. org-deadline-warning-days)))
  15919. (cond
  15920. ((or (and delay (< tv 0))
  15921. (and delay zero-delay (<= tv 0))
  15922. (and (not delay) (<= tv 0)))
  15923. ;; Enforce this value no matter what
  15924. (- tv))
  15925. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15926. ;; lead time is specified.
  15927. (floor (* (string-to-number (match-string 1 ts))
  15928. (cdr (assoc (match-string 2 ts)
  15929. '(("d" . 1) ("w" . 7)
  15930. ("m" . 30.4) ("y" . 365.25)
  15931. ("h" . 0.041667)))))))
  15932. ;; go for the default.
  15933. (t tv))))
  15934. (defun org-calendar-select-mouse (ev)
  15935. "Return to `org-read-date' with the date currently selected.
  15936. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15937. (interactive "e")
  15938. (mouse-set-point ev)
  15939. (when (calendar-cursor-to-date)
  15940. (let* ((date (calendar-cursor-to-date))
  15941. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15942. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15943. (if (active-minibuffer-window) (exit-minibuffer))))
  15944. (defun org-check-deadlines (ndays)
  15945. "Check if there are any deadlines due or past due.
  15946. A deadline is considered due if it happens within `org-deadline-warning-days'
  15947. days from today's date. If the deadline appears in an entry marked DONE,
  15948. it is not shown. The prefix arg NDAYS can be used to test that many
  15949. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15950. (interactive "P")
  15951. (let* ((org-warn-days
  15952. (cond
  15953. ((equal ndays '(4)) 100000)
  15954. (ndays (prefix-numeric-value ndays))
  15955. (t (abs org-deadline-warning-days))))
  15956. (case-fold-search nil)
  15957. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15958. (callback
  15959. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15960. (message "%d deadlines past-due or due within %d days"
  15961. (org-occur regexp nil callback)
  15962. org-warn-days)))
  15963. (defsubst org-re-timestamp (type)
  15964. "Return a regexp for timestamp TYPE.
  15965. Allowed values for TYPE are:
  15966. all: all timestamps
  15967. active: only active timestamps (<...>)
  15968. inactive: only inactive timestamps ([...])
  15969. scheduled: only scheduled timestamps
  15970. deadline: only deadline timestamps
  15971. closed: only closed time-stamps
  15972. When TYPE is nil, fall back on returning a regexp that matches
  15973. both scheduled and deadline timestamps."
  15974. (case type
  15975. (all org-ts-regexp-both)
  15976. (active org-ts-regexp)
  15977. (inactive org-ts-regexp-inactive)
  15978. (scheduled org-scheduled-time-regexp)
  15979. (deadline org-deadline-time-regexp)
  15980. (closed org-closed-time-regexp)
  15981. (otherwise
  15982. (concat "\\<"
  15983. (regexp-opt (list org-deadline-string org-scheduled-string))
  15984. " *<\\([^>]+\\)>"))))
  15985. (defun org-check-before-date (date)
  15986. "Check if there are deadlines or scheduled entries before DATE."
  15987. (interactive (list (org-read-date)))
  15988. (let ((case-fold-search nil)
  15989. (regexp (org-re-timestamp org-ts-type))
  15990. (callback
  15991. `(lambda ()
  15992. (and ,(if (memq org-ts-type '(active inactive all))
  15993. '(eq (org-element-type (org-element-context)) 'timestamp)
  15994. '(org-at-planning-p))
  15995. (time-less-p
  15996. (org-time-string-to-time (match-string 1))
  15997. (org-time-string-to-time date))))))
  15998. (message "%d entries before %s"
  15999. (org-occur regexp nil callback) date)))
  16000. (defun org-check-after-date (date)
  16001. "Check if there are deadlines or scheduled entries after DATE."
  16002. (interactive (list (org-read-date)))
  16003. (let ((case-fold-search nil)
  16004. (regexp (org-re-timestamp org-ts-type))
  16005. (callback
  16006. `(lambda ()
  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-string 1))
  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. ;; Outline functions from `outline-mode-prefix-map' that can not
  18072. ;; be remapped in Org:
  18073. ;; - the column "key binding" shows whether the Outline function is still
  18074. ;; available in Org mode on the same key that it has been bound to in
  18075. ;; Outline mode:
  18076. ;; - "overridden": key used for a different functionality in Org mode
  18077. ;; - else: key still bound to the same Outline function in Org mode
  18078. ;; | Outline function | key binding | Org replacement |
  18079. ;; |------------------------------------+-------------+--------------------------|
  18080. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18081. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18082. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18083. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18084. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18085. ;; | `show-entry' | overridden | no replacement |
  18086. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18087. ;; | `show-branches' | `C-c C-k' | still same function |
  18088. ;; | `show-subtree' | overridden | visibility cycling |
  18089. ;; | `show-all' | overridden | no replacement |
  18090. ;; | `hide-subtree' | overridden | visibility cycling |
  18091. ;; | `hide-body' | overridden | no replacement |
  18092. ;; | `hide-entry' | overridden | visibility cycling |
  18093. ;; | `hide-leaves' | overridden | no replacement |
  18094. ;; | `hide-sublevels' | overridden | no replacement |
  18095. ;; | `hide-other' | overridden | no replacement |
  18096. ;; Make `C-c C-x' a prefix key
  18097. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18098. ;; TAB key with modifiers
  18099. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18100. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18101. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18102. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18103. ;; The following line is necessary under Suse GNU/Linux
  18104. (unless (featurep 'xemacs)
  18105. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18106. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18107. (define-key org-mode-map [backtab] 'org-shifttab)
  18108. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18109. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18110. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18111. ;; Cursor keys with modifiers
  18112. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18113. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18114. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18115. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18116. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18117. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18118. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18119. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18120. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18121. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18122. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18123. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18124. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18125. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18126. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18127. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18128. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18129. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18130. ;; Babel keys
  18131. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18132. (mapc (lambda (pair)
  18133. (define-key org-babel-map (car pair) (cdr pair)))
  18134. org-babel-key-bindings)
  18135. ;;; Extra keys for tty access.
  18136. ;; We only set them when really needed because otherwise the
  18137. ;; menus don't show the simple keys
  18138. (when (or org-use-extra-keys
  18139. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18140. (not window-system))
  18141. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18142. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18143. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18144. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18145. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18146. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18147. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18148. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18149. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18150. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18151. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18152. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18153. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18154. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18155. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18156. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18157. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18158. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18159. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18160. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18161. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18162. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18163. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18164. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18165. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18166. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18167. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18168. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18169. ;; All the other keys
  18170. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18171. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18172. (if (boundp 'narrow-map)
  18173. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18174. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18175. (if (boundp 'narrow-map)
  18176. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18177. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18178. (if (boundp 'narrow-map)
  18179. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18180. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18181. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18182. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18183. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18184. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18185. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18186. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18187. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18188. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18189. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18190. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18191. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18192. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18193. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18194. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18195. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18196. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18197. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18198. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18199. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18200. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18201. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18202. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18203. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18204. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18205. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18206. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18207. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18208. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18209. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18210. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18211. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18212. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18213. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18214. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18215. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18216. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18217. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18218. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18219. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18220. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18221. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18222. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18223. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18224. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18225. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18226. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18227. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18228. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18229. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18230. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18231. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18232. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18233. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18234. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18235. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18236. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18237. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18238. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18239. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18240. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18241. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18242. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18243. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18244. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18245. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18246. (org-defkey org-mode-map "\C-m" 'org-return)
  18247. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18248. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18249. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18250. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18251. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18252. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18253. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18254. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18255. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18256. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18257. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18258. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18259. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18260. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18261. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18262. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18263. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18264. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18265. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18266. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18267. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18268. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18269. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18270. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18271. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18272. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18273. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18274. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18288. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18290. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18291. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18292. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18293. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18294. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18295. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18296. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18297. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18298. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18299. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18300. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18301. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18302. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18303. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18304. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18305. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18306. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18307. (when (featurep 'xemacs)
  18308. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18309. (defconst org-speed-commands-default
  18310. '(
  18311. ("Outline Navigation")
  18312. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18313. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18314. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18315. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18316. ("F" . org-next-block)
  18317. ("B" . org-previous-block)
  18318. ("u" . (org-speed-move-safe 'outline-up-heading))
  18319. ("j" . org-goto)
  18320. ("g" . (org-refile t))
  18321. ("Outline Visibility")
  18322. ("c" . org-cycle)
  18323. ("C" . org-shifttab)
  18324. (" " . org-display-outline-path)
  18325. ("s" . org-narrow-to-subtree)
  18326. ("=" . org-columns)
  18327. ("Outline Structure Editing")
  18328. ("U" . org-metaup)
  18329. ("D" . org-metadown)
  18330. ("r" . org-metaright)
  18331. ("l" . org-metaleft)
  18332. ("R" . org-shiftmetaright)
  18333. ("L" . org-shiftmetaleft)
  18334. ("i" . (progn (forward-char 1) (call-interactively
  18335. 'org-insert-heading-respect-content)))
  18336. ("^" . org-sort)
  18337. ("w" . org-refile)
  18338. ("a" . org-archive-subtree-default-with-confirmation)
  18339. ("@" . org-mark-subtree)
  18340. ("#" . org-toggle-comment)
  18341. ("Clock Commands")
  18342. ("I" . org-clock-in)
  18343. ("O" . org-clock-out)
  18344. ("Meta Data Editing")
  18345. ("t" . org-todo)
  18346. ("," . (org-priority))
  18347. ("0" . (org-priority ?\ ))
  18348. ("1" . (org-priority ?A))
  18349. ("2" . (org-priority ?B))
  18350. ("3" . (org-priority ?C))
  18351. (":" . org-set-tags-command)
  18352. ("e" . org-set-effort)
  18353. ("E" . org-inc-effort)
  18354. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18355. (org-entry-put (point) "APPT_WARNTIME" m)))
  18356. ("Agenda Views etc")
  18357. ("v" . org-agenda)
  18358. ("/" . org-sparse-tree)
  18359. ("Misc")
  18360. ("o" . org-open-at-point)
  18361. ("?" . org-speed-command-help)
  18362. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18363. (">" . (org-agenda-remove-restriction-lock))
  18364. )
  18365. "The default speed commands.")
  18366. (defun org-print-speed-command (e)
  18367. (if (> (length (car e)) 1)
  18368. (progn
  18369. (princ "\n")
  18370. (princ (car e))
  18371. (princ "\n")
  18372. (princ (make-string (length (car e)) ?-))
  18373. (princ "\n"))
  18374. (princ (car e))
  18375. (princ " ")
  18376. (if (symbolp (cdr e))
  18377. (princ (symbol-name (cdr e)))
  18378. (prin1 (cdr e)))
  18379. (princ "\n")))
  18380. (defun org-speed-command-help ()
  18381. "Show the available speed commands."
  18382. (interactive)
  18383. (if (not org-use-speed-commands)
  18384. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18385. (with-output-to-temp-buffer "*Help*"
  18386. (princ "User-defined Speed commands\n===========================\n")
  18387. (mapc 'org-print-speed-command org-speed-commands-user)
  18388. (princ "\n")
  18389. (princ "Built-in Speed commands\n=======================\n")
  18390. (mapc 'org-print-speed-command org-speed-commands-default))
  18391. (with-current-buffer "*Help*"
  18392. (setq truncate-lines t))))
  18393. (defun org-speed-move-safe (cmd)
  18394. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18395. If not, return to the original position and throw an error."
  18396. (interactive)
  18397. (let ((pos (point)))
  18398. (call-interactively cmd)
  18399. (unless (and (bolp) (org-at-heading-p))
  18400. (goto-char pos)
  18401. (error "Boundary reached while executing %s" cmd))))
  18402. (defvar org-self-insert-command-undo-counter 0)
  18403. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18404. (defvar org-speed-command nil)
  18405. (define-obsolete-function-alias
  18406. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18407. (defun org-speed-command-activate (keys)
  18408. "Hook for activating single-letter speed commands.
  18409. `org-speed-commands-default' specifies a minimal command set.
  18410. Use `org-speed-commands-user' for further customization."
  18411. (when (or (and (bolp) (looking-at org-outline-regexp))
  18412. (and (functionp org-use-speed-commands)
  18413. (funcall org-use-speed-commands)))
  18414. (cdr (assoc keys (append org-speed-commands-user
  18415. org-speed-commands-default)))))
  18416. (define-obsolete-function-alias
  18417. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18418. (defun org-babel-speed-command-activate (keys)
  18419. "Hook for activating single-letter code block commands."
  18420. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18421. (cdr (assoc keys org-babel-key-bindings))))
  18422. (defcustom org-speed-command-hook
  18423. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18424. "Hook for activating speed commands at strategic locations.
  18425. Hook functions are called in sequence until a valid handler is
  18426. found.
  18427. Each hook takes a single argument, a user-pressed command key
  18428. which is also a `self-insert-command' from the global map.
  18429. Within the hook, examine the cursor position and the command key
  18430. and return nil or a valid handler as appropriate. Handler could
  18431. be one of an interactive command, a function, or a form.
  18432. Set `org-use-speed-commands' to non-nil value to enable this
  18433. hook. The default setting is `org-speed-command-activate'."
  18434. :group 'org-structure
  18435. :version "24.1"
  18436. :type 'hook)
  18437. (defun org-self-insert-command (N)
  18438. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18439. If the cursor is in a table looking at whitespace, the whitespace is
  18440. overwritten, and the table is not marked as requiring realignment."
  18441. (interactive "p")
  18442. (org-check-before-invisible-edit 'insert)
  18443. (cond
  18444. ((and org-use-speed-commands
  18445. (let ((kv (this-command-keys-vector)))
  18446. (setq org-speed-command
  18447. (run-hook-with-args-until-success
  18448. 'org-speed-command-hook
  18449. (make-string 1 (aref kv (1- (length kv))))))))
  18450. (cond
  18451. ((commandp org-speed-command)
  18452. (setq this-command org-speed-command)
  18453. (call-interactively org-speed-command))
  18454. ((functionp org-speed-command)
  18455. (funcall org-speed-command))
  18456. ((and org-speed-command (listp org-speed-command))
  18457. (eval org-speed-command))
  18458. (t (let (org-use-speed-commands)
  18459. (call-interactively 'org-self-insert-command)))))
  18460. ((and
  18461. (org-table-p)
  18462. (progn
  18463. ;; check if we blank the field, and if that triggers align
  18464. (and (featurep 'org-table) org-table-auto-blank-field
  18465. (memq last-command
  18466. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18467. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18468. ;; got extra space, this field does not determine column width
  18469. (let (org-table-may-need-update) (org-table-blank-field))
  18470. ;; no extra space, this field may determine column width
  18471. (org-table-blank-field)))
  18472. t)
  18473. (eq N 1)
  18474. (looking-at "[^|\n]* |"))
  18475. (let (org-table-may-need-update)
  18476. (goto-char (1- (match-end 0)))
  18477. (backward-delete-char 1)
  18478. (goto-char (match-beginning 0))
  18479. (self-insert-command N)))
  18480. (t
  18481. (setq org-table-may-need-update t)
  18482. (self-insert-command N)
  18483. (org-fix-tags-on-the-fly)
  18484. (if org-self-insert-cluster-for-undo
  18485. (if (not (eq last-command 'org-self-insert-command))
  18486. (setq org-self-insert-command-undo-counter 1)
  18487. (if (>= org-self-insert-command-undo-counter 20)
  18488. (setq org-self-insert-command-undo-counter 1)
  18489. (and (> org-self-insert-command-undo-counter 0)
  18490. buffer-undo-list (listp buffer-undo-list)
  18491. (not (cadr buffer-undo-list)) ; remove nil entry
  18492. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18493. (setq org-self-insert-command-undo-counter
  18494. (1+ org-self-insert-command-undo-counter))))))))
  18495. (defun org-check-before-invisible-edit (kind)
  18496. "Check is editing if kind KIND would be dangerous with invisible text around.
  18497. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18498. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18499. (if (and org-catch-invisible-edits
  18500. (or (not (boundp 'visible-mode)) (not visible-mode))
  18501. (or (get-char-property (point) 'invisible)
  18502. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18503. ;; OK, we need to take a closer look
  18504. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18505. (invisible-before-point (if (bobp) nil (get-char-property
  18506. (1- (point)) 'invisible)))
  18507. (border-and-ok-direction
  18508. (or
  18509. ;; Check if we are acting predictably before invisible text
  18510. (and invisible-at-point (not invisible-before-point)
  18511. (memq kind '(insert delete-backward)))
  18512. ;; Check if we are acting predictably after invisible text
  18513. ;; This works not well, and I have turned it off. It seems
  18514. ;; better to always show and stop after invisible text.
  18515. ;; (and (not invisible-at-point) invisible-before-point
  18516. ;; (memq kind '(insert delete)))
  18517. )))
  18518. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18519. (memq invisible-before-point '(outline org-hide-block t)))
  18520. (if (eq org-catch-invisible-edits 'error)
  18521. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18522. (if (and org-custom-properties-overlays
  18523. (y-or-n-p "Display invisible properties in this buffer? "))
  18524. (org-toggle-custom-properties-visibility)
  18525. ;; Make the area visible
  18526. (save-excursion
  18527. (if invisible-before-point
  18528. (goto-char (previous-single-char-property-change
  18529. (point) 'invisible)))
  18530. (show-subtree))
  18531. (cond
  18532. ((eq org-catch-invisible-edits 'show)
  18533. ;; That's it, we do the edit after showing
  18534. (message
  18535. "Unfolding invisible region around point before editing")
  18536. (sit-for 1))
  18537. ((and (eq org-catch-invisible-edits 'smart)
  18538. border-and-ok-direction)
  18539. (message "Unfolding invisible region around point before editing"))
  18540. (t
  18541. ;; Don't do the edit, make the user repeat it in full visibility
  18542. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18543. (defun org-fix-tags-on-the-fly ()
  18544. (when (and (equal (char-after (point-at-bol)) ?*)
  18545. (org-at-heading-p))
  18546. (org-align-tags-here org-tags-column)))
  18547. (defun org-delete-backward-char (N)
  18548. "Like `delete-backward-char', insert whitespace at field end in tables.
  18549. When deleting backwards, in tables this function will insert whitespace in
  18550. front of the next \"|\" separator, to keep the table aligned. The table will
  18551. still be marked for re-alignment if the field did fill the entire column,
  18552. because, in this case the deletion might narrow the column."
  18553. (interactive "p")
  18554. (save-match-data
  18555. (org-check-before-invisible-edit 'delete-backward)
  18556. (if (and (org-table-p)
  18557. (eq N 1)
  18558. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18559. (looking-at ".*?|"))
  18560. (let ((pos (point))
  18561. (noalign (looking-at "[^|\n\r]* |"))
  18562. (c org-table-may-need-update))
  18563. (backward-delete-char N)
  18564. (if (not overwrite-mode)
  18565. (progn
  18566. (skip-chars-forward "^|")
  18567. (insert " ")
  18568. (goto-char (1- pos))))
  18569. ;; noalign: if there were two spaces at the end, this field
  18570. ;; does not determine the width of the column.
  18571. (if noalign (setq org-table-may-need-update c)))
  18572. (backward-delete-char N)
  18573. (org-fix-tags-on-the-fly))))
  18574. (defun org-delete-char (N)
  18575. "Like `delete-char', but insert whitespace at field end in tables.
  18576. When deleting characters, in tables this function will insert whitespace in
  18577. front of the next \"|\" separator, to keep the table aligned. The table will
  18578. still be marked for re-alignment if the field did fill the entire column,
  18579. because, in this case the deletion might narrow the column."
  18580. (interactive "p")
  18581. (save-match-data
  18582. (org-check-before-invisible-edit 'delete)
  18583. (if (and (org-table-p)
  18584. (not (bolp))
  18585. (not (= (char-after) ?|))
  18586. (eq N 1))
  18587. (if (looking-at ".*?|")
  18588. (let ((pos (point))
  18589. (noalign (looking-at "[^|\n\r]* |"))
  18590. (c org-table-may-need-update))
  18591. (replace-match
  18592. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18593. (goto-char pos)
  18594. ;; noalign: if there were two spaces at the end, this field
  18595. ;; does not determine the width of the column.
  18596. (if noalign (setq org-table-may-need-update c)))
  18597. (delete-char N))
  18598. (delete-char N)
  18599. (org-fix-tags-on-the-fly))))
  18600. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18601. (put 'org-self-insert-command 'delete-selection
  18602. (lambda ()
  18603. (not (run-hook-with-args-until-success
  18604. 'self-insert-uses-region-functions))))
  18605. (put 'orgtbl-self-insert-command 'delete-selection
  18606. (lambda ()
  18607. (not (run-hook-with-args-until-success
  18608. 'self-insert-uses-region-functions))))
  18609. (put 'org-delete-char 'delete-selection 'supersede)
  18610. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18611. (put 'org-yank 'delete-selection 'yank)
  18612. ;; Make `flyspell-mode' delay after some commands
  18613. (put 'org-self-insert-command 'flyspell-delayed t)
  18614. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18615. (put 'org-delete-char 'flyspell-delayed t)
  18616. (put 'org-delete-backward-char 'flyspell-delayed t)
  18617. ;; Make pabbrev-mode expand after org-mode commands
  18618. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18619. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18620. (defun org-remap (map &rest commands)
  18621. "In MAP, remap the functions given in COMMANDS.
  18622. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18623. (let (new old)
  18624. (while commands
  18625. (setq old (pop commands) new (pop commands))
  18626. (if (fboundp 'command-remapping)
  18627. (org-defkey map (vector 'remap old) new)
  18628. (substitute-key-definition old new map global-map)))))
  18629. (defun org-transpose-words ()
  18630. "Transpose words for Org.
  18631. This uses the `org-mode-transpose-word-syntax-table' syntax
  18632. table, which interprets characters in `org-emphasis-alist' as
  18633. word constituents."
  18634. (interactive)
  18635. (with-syntax-table org-mode-transpose-word-syntax-table
  18636. (call-interactively 'transpose-words)))
  18637. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18638. (when (eq org-enable-table-editor 'optimized)
  18639. ;; If the user wants maximum table support, we need to hijack
  18640. ;; some standard editing functions
  18641. (org-remap org-mode-map
  18642. 'self-insert-command 'org-self-insert-command
  18643. 'delete-char 'org-delete-char
  18644. 'delete-backward-char 'org-delete-backward-char)
  18645. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18646. (defvar org-ctrl-c-ctrl-c-hook nil
  18647. "Hook for functions attaching themselves to `C-c C-c'.
  18648. This can be used to add additional functionality to the C-c C-c
  18649. key which executes context-dependent commands. This hook is run
  18650. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18651. run after the last test.
  18652. Each function will be called with no arguments. The function
  18653. must check if the context is appropriate for it to act. If yes,
  18654. it should do its thing and then return a non-nil value. If the
  18655. context is wrong, just do nothing and return nil.")
  18656. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18657. "Hook for functions attaching themselves to `C-c C-c'.
  18658. This can be used to add additional functionality to the C-c C-c
  18659. key which executes context-dependent commands. This hook is run
  18660. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18661. before the first test.
  18662. Each function will be called with no arguments. The function
  18663. must check if the context is appropriate for it to act. If yes,
  18664. it should do its thing and then return a non-nil value. If the
  18665. context is wrong, just do nothing and return nil.")
  18666. (defvar org-tab-first-hook nil
  18667. "Hook for functions to attach themselves to TAB.
  18668. See `org-ctrl-c-ctrl-c-hook' for more information.
  18669. This hook runs as the first action when TAB is pressed, even before
  18670. `org-cycle' messes around with the `outline-regexp' to cater for
  18671. inline tasks and plain list item folding.
  18672. If any function in this hook returns t, any other actions that
  18673. would have been caused by TAB (such as table field motion or visibility
  18674. cycling) will not occur.")
  18675. (defvar org-tab-after-check-for-table-hook nil
  18676. "Hook for functions to attach themselves to TAB.
  18677. See `org-ctrl-c-ctrl-c-hook' for more information.
  18678. This hook runs after it has been established that the cursor is not in a
  18679. table, but before checking if the cursor is in a headline or if global cycling
  18680. should be done.
  18681. If any function in this hook returns t, not other actions like visibility
  18682. cycling will be done.")
  18683. (defvar org-tab-after-check-for-cycling-hook nil
  18684. "Hook for functions to attach themselves to TAB.
  18685. See `org-ctrl-c-ctrl-c-hook' for more information.
  18686. This hook runs after it has been established that not table field motion and
  18687. not visibility should be done because of current context. This is probably
  18688. the place where a package like yasnippets can hook in.")
  18689. (defvar org-tab-before-tab-emulation-hook nil
  18690. "Hook for functions to attach themselves to TAB.
  18691. See `org-ctrl-c-ctrl-c-hook' for more information.
  18692. This hook runs after every other options for TAB have been exhausted, but
  18693. before indentation and \t insertion takes place.")
  18694. (defvar org-metaleft-hook nil
  18695. "Hook for functions attaching themselves to `M-left'.
  18696. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18697. (defvar org-metaright-hook nil
  18698. "Hook for functions attaching themselves to `M-right'.
  18699. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18700. (defvar org-metaup-hook nil
  18701. "Hook for functions attaching themselves to `M-up'.
  18702. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18703. (defvar org-metadown-hook nil
  18704. "Hook for functions attaching themselves to `M-down'.
  18705. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18706. (defvar org-shiftmetaleft-hook nil
  18707. "Hook for functions attaching themselves to `M-S-left'.
  18708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18709. (defvar org-shiftmetaright-hook nil
  18710. "Hook for functions attaching themselves to `M-S-right'.
  18711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18712. (defvar org-shiftmetaup-hook nil
  18713. "Hook for functions attaching themselves to `M-S-up'.
  18714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18715. (defvar org-shiftmetadown-hook nil
  18716. "Hook for functions attaching themselves to `M-S-down'.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-metareturn-hook nil
  18719. "Hook for functions attaching themselves to `M-RET'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-shiftup-hook nil
  18722. "Hook for functions attaching themselves to `S-up'.
  18723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18724. (defvar org-shiftup-final-hook nil
  18725. "Hook for functions attaching themselves to `S-up'.
  18726. This one runs after all other options except shift-select have been excluded.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-shiftdown-hook nil
  18729. "Hook for functions attaching themselves to `S-down'.
  18730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18731. (defvar org-shiftdown-final-hook nil
  18732. "Hook for functions attaching themselves to `S-down'.
  18733. This one runs after all other options except shift-select have been excluded.
  18734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18735. (defvar org-shiftleft-hook nil
  18736. "Hook for functions attaching themselves to `S-left'.
  18737. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18738. (defvar org-shiftleft-final-hook nil
  18739. "Hook for functions attaching themselves to `S-left'.
  18740. This one runs after all other options except shift-select have been excluded.
  18741. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18742. (defvar org-shiftright-hook nil
  18743. "Hook for functions attaching themselves to `S-right'.
  18744. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18745. (defvar org-shiftright-final-hook nil
  18746. "Hook for functions attaching themselves to `S-right'.
  18747. This one runs after all other options except shift-select have been excluded.
  18748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18749. (defun org-modifier-cursor-error ()
  18750. "Throw an error, a modified cursor command was applied in wrong context."
  18751. (user-error "This command is active in special context like tables, headlines or items"))
  18752. (defun org-shiftselect-error ()
  18753. "Throw an error because Shift-Cursor command was applied in wrong context."
  18754. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18755. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18756. (user-error "This command works only in special context like headlines or timestamps")))
  18757. (defun org-call-for-shift-select (cmd)
  18758. (let ((this-command-keys-shift-translated t))
  18759. (call-interactively cmd)))
  18760. (defun org-shifttab (&optional arg)
  18761. "Global visibility cycling or move to previous table field.
  18762. Call `org-table-previous-field' within a table.
  18763. When ARG is nil, cycle globally through visibility states.
  18764. When ARG is a numeric prefix, show contents of this level."
  18765. (interactive "P")
  18766. (cond
  18767. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18768. ((integerp arg)
  18769. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18770. (message "Content view to level: %d" arg)
  18771. (org-content (prefix-numeric-value arg2))
  18772. (org-cycle-show-empty-lines t)
  18773. (setq org-cycle-global-status 'overview)))
  18774. (t (call-interactively 'org-global-cycle))))
  18775. (defun org-shiftmetaleft ()
  18776. "Promote subtree or delete table column.
  18777. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18778. `org-table-delete-column', depending on context. See the
  18779. individual commands for more information."
  18780. (interactive)
  18781. (cond
  18782. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18783. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18784. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18785. ((if (not (org-region-active-p)) (org-at-item-p)
  18786. (save-excursion (goto-char (region-beginning))
  18787. (org-at-item-p)))
  18788. (call-interactively 'org-outdent-item-tree))
  18789. (t (org-modifier-cursor-error))))
  18790. (defun org-shiftmetaright ()
  18791. "Demote subtree or insert table column.
  18792. Calls `org-demote-subtree', `org-indent-item-tree', or
  18793. `org-table-insert-column', depending on context. See the
  18794. individual commands for more information."
  18795. (interactive)
  18796. (cond
  18797. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18798. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18799. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18800. ((if (not (org-region-active-p)) (org-at-item-p)
  18801. (save-excursion (goto-char (region-beginning))
  18802. (org-at-item-p)))
  18803. (call-interactively 'org-indent-item-tree))
  18804. (t (org-modifier-cursor-error))))
  18805. (defun org-shiftmetaup (&optional arg)
  18806. "Drag the line at point up.
  18807. In a table, kill the current row.
  18808. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18809. but also adjust the previous clocked item in the clock history.
  18810. Everywhere else, drag the line at point up."
  18811. (interactive "P")
  18812. (cond
  18813. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18814. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18815. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18816. (call-interactively 'org-timestamp-up)))
  18817. (t (call-interactively 'org-drag-line-backward))))
  18818. (defun org-shiftmetadown (&optional arg)
  18819. "Drag the line at point down.
  18820. In a table, insert an empty row at the current line.
  18821. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18822. but also adjust the previous clocked item in the clock history.
  18823. Everywhere else, drag the line at point down."
  18824. (interactive "P")
  18825. (cond
  18826. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18827. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18828. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18829. (call-interactively 'org-timestamp-down)))
  18830. (t (call-interactively 'org-drag-line-forward))))
  18831. (defsubst org-hidden-tree-error ()
  18832. (user-error
  18833. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18834. (defun org-metaleft (&optional arg)
  18835. "Promote heading, list item at point or move table column left.
  18836. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18837. depending on context. With no specific context, calls the Emacs
  18838. default `backward-word'. See the individual commands for more
  18839. information.
  18840. This function runs the hook `org-metaleft-hook' as a first step,
  18841. and returns at first non-nil value."
  18842. (interactive "P")
  18843. (cond
  18844. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18845. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18846. ((org-with-limited-levels
  18847. (or (org-at-heading-p)
  18848. (and (org-region-active-p)
  18849. (save-excursion
  18850. (goto-char (region-beginning))
  18851. (org-at-heading-p)))))
  18852. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18853. (call-interactively 'org-do-promote))
  18854. ;; At an inline task.
  18855. ((org-at-heading-p)
  18856. (call-interactively 'org-inlinetask-promote))
  18857. ((or (org-at-item-p)
  18858. (and (org-region-active-p)
  18859. (save-excursion
  18860. (goto-char (region-beginning))
  18861. (org-at-item-p))))
  18862. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18863. (call-interactively 'org-outdent-item))
  18864. (t (call-interactively 'backward-word))))
  18865. (defun org-metaright (&optional arg)
  18866. "Demote heading, list item at point or move table column right.
  18867. In front of a drawer or a block keyword, indent it correctly.
  18868. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18869. `org-indnet-drawer' or `org-indent-block' depending on context.
  18870. With no specific context, calls the Emacs default `forward-word'.
  18871. See the individual commands for more information.
  18872. This function runs the hook `org-metaright-hook' as a first step,
  18873. and returns at first non-nil value."
  18874. (interactive "P")
  18875. (cond
  18876. ((run-hook-with-args-until-success 'org-metaright-hook))
  18877. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18878. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18879. ((org-at-block-p) (call-interactively 'org-indent-block))
  18880. ((org-with-limited-levels
  18881. (or (org-at-heading-p)
  18882. (and (org-region-active-p)
  18883. (save-excursion
  18884. (goto-char (region-beginning))
  18885. (org-at-heading-p)))))
  18886. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18887. (call-interactively 'org-do-demote))
  18888. ;; At an inline task.
  18889. ((org-at-heading-p)
  18890. (call-interactively 'org-inlinetask-demote))
  18891. ((or (org-at-item-p)
  18892. (and (org-region-active-p)
  18893. (save-excursion
  18894. (goto-char (region-beginning))
  18895. (org-at-item-p))))
  18896. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18897. (call-interactively 'org-indent-item))
  18898. (t (call-interactively 'forward-word))))
  18899. (defun org-check-for-hidden (what)
  18900. "Check if there are hidden headlines/items in the current visual line.
  18901. WHAT can be either `headlines' or `items'. If the current line is
  18902. an outline or item heading and it has a folded subtree below it,
  18903. this function returns t, nil otherwise."
  18904. (let ((re (cond
  18905. ((eq what 'headlines) org-outline-regexp-bol)
  18906. ((eq what 'items) (org-item-beginning-re))
  18907. (t (error "This should not happen"))))
  18908. beg end)
  18909. (save-excursion
  18910. (catch 'exit
  18911. (unless (org-region-active-p)
  18912. (setq beg (point-at-bol))
  18913. (beginning-of-line 2)
  18914. (while (and (not (eobp)) ;; this is like `next-line'
  18915. (get-char-property (1- (point)) 'invisible))
  18916. (beginning-of-line 2))
  18917. (setq end (point))
  18918. (goto-char beg)
  18919. (goto-char (point-at-eol))
  18920. (setq end (max end (point)))
  18921. (while (re-search-forward re end t)
  18922. (if (get-char-property (match-beginning 0) 'invisible)
  18923. (throw 'exit t))))
  18924. nil))))
  18925. (defun org-metaup (&optional arg)
  18926. "Move subtree up or move table row up.
  18927. Calls `org-move-subtree-up' or `org-table-move-row' or
  18928. `org-move-item-up', depending on context. See the individual commands
  18929. for more information."
  18930. (interactive "P")
  18931. (cond
  18932. ((run-hook-with-args-until-success 'org-metaup-hook))
  18933. ((org-region-active-p)
  18934. (let* ((a (min (region-beginning) (region-end)))
  18935. (b (1- (max (region-beginning) (region-end))))
  18936. (c (save-excursion (goto-char a)
  18937. (move-beginning-of-line 0)))
  18938. (d (save-excursion (goto-char a)
  18939. (move-end-of-line 0) (point))))
  18940. (transpose-regions a b c d)
  18941. (goto-char c)))
  18942. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18943. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18944. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18945. (t (org-drag-element-backward))))
  18946. (defun org-metadown (&optional arg)
  18947. "Move subtree down or move table row down.
  18948. Calls `org-move-subtree-down' or `org-table-move-row' or
  18949. `org-move-item-down', depending on context. See the individual
  18950. commands for more information."
  18951. (interactive "P")
  18952. (cond
  18953. ((run-hook-with-args-until-success 'org-metadown-hook))
  18954. ((org-region-active-p)
  18955. (let* ((a (min (region-beginning) (region-end)))
  18956. (b (max (region-beginning) (region-end)))
  18957. (c (save-excursion (goto-char b)
  18958. (move-beginning-of-line 1)))
  18959. (d (save-excursion (goto-char b)
  18960. (move-end-of-line 1) (1+ (point)))))
  18961. (transpose-regions a b c d)
  18962. (goto-char d)))
  18963. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18964. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18965. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18966. (t (org-drag-element-forward))))
  18967. (defun org-shiftup (&optional arg)
  18968. "Increase item in timestamp or increase priority of current headline.
  18969. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18970. depending on context. See the individual commands for more information."
  18971. (interactive "P")
  18972. (cond
  18973. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18974. ((and org-support-shift-select (org-region-active-p))
  18975. (org-call-for-shift-select 'previous-line))
  18976. ((org-at-timestamp-p t)
  18977. (call-interactively (if org-edit-timestamp-down-means-later
  18978. 'org-timestamp-down 'org-timestamp-up)))
  18979. ((and (not (eq org-support-shift-select 'always))
  18980. org-enable-priority-commands
  18981. (org-at-heading-p))
  18982. (call-interactively 'org-priority-up))
  18983. ((and (not org-support-shift-select) (org-at-item-p))
  18984. (call-interactively 'org-previous-item))
  18985. ((org-clocktable-try-shift 'up arg))
  18986. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18987. (org-support-shift-select
  18988. (org-call-for-shift-select 'previous-line))
  18989. (t (org-shiftselect-error))))
  18990. (defun org-shiftdown (&optional arg)
  18991. "Decrease item in timestamp or decrease priority of current headline.
  18992. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18993. depending on context. See the individual commands for more information."
  18994. (interactive "P")
  18995. (cond
  18996. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18997. ((and org-support-shift-select (org-region-active-p))
  18998. (org-call-for-shift-select 'next-line))
  18999. ((org-at-timestamp-p t)
  19000. (call-interactively (if org-edit-timestamp-down-means-later
  19001. 'org-timestamp-up 'org-timestamp-down)))
  19002. ((and (not (eq org-support-shift-select 'always))
  19003. org-enable-priority-commands
  19004. (org-at-heading-p))
  19005. (call-interactively 'org-priority-down))
  19006. ((and (not org-support-shift-select) (org-at-item-p))
  19007. (call-interactively 'org-next-item))
  19008. ((org-clocktable-try-shift 'down arg))
  19009. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19010. (org-support-shift-select
  19011. (org-call-for-shift-select 'next-line))
  19012. (t (org-shiftselect-error))))
  19013. (defun org-shiftright (&optional arg)
  19014. "Cycle the thing at point or in the current line, depending on context.
  19015. Depending on context, this does one of the following:
  19016. - switch a timestamp at point one day into the future
  19017. - on a headline, switch to the next TODO keyword.
  19018. - on an item, switch entire list to the next bullet type
  19019. - on a property line, switch to the next allowed value
  19020. - on a clocktable definition line, move time block into the future"
  19021. (interactive "P")
  19022. (cond
  19023. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19024. ((and org-support-shift-select (org-region-active-p))
  19025. (org-call-for-shift-select 'forward-char))
  19026. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19027. ((and (not (eq org-support-shift-select 'always))
  19028. (org-at-heading-p))
  19029. (let ((org-inhibit-logging
  19030. (not org-treat-S-cursor-todo-selection-as-state-change))
  19031. (org-inhibit-blocking
  19032. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19033. (org-call-with-arg 'org-todo 'right)))
  19034. ((or (and org-support-shift-select
  19035. (not (eq org-support-shift-select 'always))
  19036. (org-at-item-bullet-p))
  19037. (and (not org-support-shift-select) (org-at-item-p)))
  19038. (org-call-with-arg 'org-cycle-list-bullet nil))
  19039. ((and (not (eq org-support-shift-select 'always))
  19040. (org-at-property-p))
  19041. (call-interactively 'org-property-next-allowed-value))
  19042. ((org-clocktable-try-shift 'right arg))
  19043. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19044. (org-support-shift-select
  19045. (org-call-for-shift-select 'forward-char))
  19046. (t (org-shiftselect-error))))
  19047. (defun org-shiftleft (&optional arg)
  19048. "Cycle the thing at point or in the current line, depending on context.
  19049. Depending on context, this does one of the following:
  19050. - switch a timestamp at point one day into the past
  19051. - on a headline, switch to the previous TODO keyword.
  19052. - on an item, switch entire list to the previous bullet type
  19053. - on a property line, switch to the previous allowed value
  19054. - on a clocktable definition line, move time block into the past"
  19055. (interactive "P")
  19056. (cond
  19057. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19058. ((and org-support-shift-select (org-region-active-p))
  19059. (org-call-for-shift-select 'backward-char))
  19060. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19061. ((and (not (eq org-support-shift-select 'always))
  19062. (org-at-heading-p))
  19063. (let ((org-inhibit-logging
  19064. (not org-treat-S-cursor-todo-selection-as-state-change))
  19065. (org-inhibit-blocking
  19066. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19067. (org-call-with-arg 'org-todo 'left)))
  19068. ((or (and org-support-shift-select
  19069. (not (eq org-support-shift-select 'always))
  19070. (org-at-item-bullet-p))
  19071. (and (not org-support-shift-select) (org-at-item-p)))
  19072. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19073. ((and (not (eq org-support-shift-select 'always))
  19074. (org-at-property-p))
  19075. (call-interactively 'org-property-previous-allowed-value))
  19076. ((org-clocktable-try-shift 'left arg))
  19077. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19078. (org-support-shift-select
  19079. (org-call-for-shift-select 'backward-char))
  19080. (t (org-shiftselect-error))))
  19081. (defun org-shiftcontrolright ()
  19082. "Switch to next TODO set."
  19083. (interactive)
  19084. (cond
  19085. ((and org-support-shift-select (org-region-active-p))
  19086. (org-call-for-shift-select 'forward-word))
  19087. ((and (not (eq org-support-shift-select 'always))
  19088. (org-at-heading-p))
  19089. (org-call-with-arg 'org-todo 'nextset))
  19090. (org-support-shift-select
  19091. (org-call-for-shift-select 'forward-word))
  19092. (t (org-shiftselect-error))))
  19093. (defun org-shiftcontrolleft ()
  19094. "Switch to previous TODO set."
  19095. (interactive)
  19096. (cond
  19097. ((and org-support-shift-select (org-region-active-p))
  19098. (org-call-for-shift-select 'backward-word))
  19099. ((and (not (eq org-support-shift-select 'always))
  19100. (org-at-heading-p))
  19101. (org-call-with-arg 'org-todo 'previousset))
  19102. (org-support-shift-select
  19103. (org-call-for-shift-select 'backward-word))
  19104. (t (org-shiftselect-error))))
  19105. (defun org-shiftcontrolup (&optional n)
  19106. "Change timestamps synchronously up in CLOCK log lines.
  19107. Optional argument N tells to change by that many units."
  19108. (interactive "P")
  19109. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19110. (let (org-support-shift-select)
  19111. (org-clock-timestamps-up n))
  19112. (user-error "Not at a clock log")))
  19113. (defun org-shiftcontroldown (&optional n)
  19114. "Change timestamps synchronously down in CLOCK log lines.
  19115. Optional argument N tells to change by that many units."
  19116. (interactive "P")
  19117. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19118. (let (org-support-shift-select)
  19119. (org-clock-timestamps-down n))
  19120. (user-error "Not at a clock log")))
  19121. (defun org-increase-number-at-point (&optional inc)
  19122. "Increment the number at point.
  19123. With an optional prefix numeric argument INC, increment using
  19124. this numeric value."
  19125. (interactive "p")
  19126. (if (not (number-at-point))
  19127. (user-error "Not on a number")
  19128. (unless inc (setq inc 1))
  19129. (let ((pos (point))
  19130. (beg (skip-chars-backward "-+^/*0-9eE."))
  19131. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19132. (setq nap (buffer-substring-no-properties
  19133. (+ pos beg) (+ pos beg end)))
  19134. (delete-region (+ pos beg) (+ pos beg end))
  19135. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19136. (when (org-at-table-p)
  19137. (org-table-align)
  19138. (org-table-end-of-field 1))))
  19139. (defun org-decrease-number-at-point (&optional inc)
  19140. "Decrement the number at point.
  19141. With an optional prefix numeric argument INC, decrement using
  19142. this numeric value."
  19143. (interactive "p")
  19144. (org-increase-number-at-point (- (or inc 1))))
  19145. (defun org-ctrl-c-ret ()
  19146. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19147. (interactive)
  19148. (cond
  19149. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19150. (t (call-interactively 'org-insert-heading))))
  19151. (defun org-find-visible ()
  19152. (let ((s (point)))
  19153. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19154. (get-char-property s 'invisible)))
  19155. s))
  19156. (defun org-find-invisible ()
  19157. (let ((s (point)))
  19158. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19159. (not (get-char-property s 'invisible))))
  19160. s))
  19161. (defun org-copy-visible (beg end)
  19162. "Copy the visible parts of the region."
  19163. (interactive "r")
  19164. (let (snippets s)
  19165. (save-excursion
  19166. (save-restriction
  19167. (narrow-to-region beg end)
  19168. (setq s (goto-char (point-min)))
  19169. (while (not (= (point) (point-max)))
  19170. (goto-char (org-find-invisible))
  19171. (push (buffer-substring s (point)) snippets)
  19172. (setq s (goto-char (org-find-visible))))))
  19173. (kill-new (apply 'concat (nreverse snippets)))))
  19174. (defun org-copy-special ()
  19175. "Copy region in table or copy current subtree.
  19176. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19177. See the individual commands for more information."
  19178. (interactive)
  19179. (call-interactively
  19180. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19181. (defun org-cut-special ()
  19182. "Cut region in table or cut current subtree.
  19183. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19184. See the individual commands for more information."
  19185. (interactive)
  19186. (call-interactively
  19187. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19188. (defun org-paste-special (arg)
  19189. "Paste rectangular region into table, or past subtree relative to level.
  19190. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19191. See the individual commands for more information."
  19192. (interactive "P")
  19193. (if (org-at-table-p)
  19194. (org-table-paste-rectangle)
  19195. (org-paste-subtree arg)))
  19196. (defsubst org-in-fixed-width-region-p ()
  19197. "Is point in a fixed-width region?"
  19198. (save-match-data
  19199. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19200. (defun org-edit-special (&optional arg)
  19201. "Call a special editor for the element at point.
  19202. When at a table, call the formula editor with `org-table-edit-formulas'.
  19203. When in a source code block, call `org-edit-src-code'.
  19204. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19205. When in an export block, call `org-edit-export-block'.
  19206. When at an #+INCLUDE keyword, visit the included file.
  19207. When at a footnote reference, call `org-edit-footnote-reference'
  19208. On a link, call `ffap' to visit the link at point.
  19209. Otherwise, return a user error."
  19210. (interactive "P")
  19211. (let ((element (org-element-at-point)))
  19212. (assert (not buffer-read-only) nil
  19213. "Buffer is read-only: %s" (buffer-name))
  19214. (case (org-element-type element)
  19215. (src-block
  19216. (if (not arg) (org-edit-src-code)
  19217. (let* ((info (org-babel-get-src-block-info))
  19218. (lang (nth 0 info))
  19219. (params (nth 2 info))
  19220. (session (cdr (assq :session params))))
  19221. (if (not session) (org-edit-src-code)
  19222. ;; At a src-block with a session and function called with
  19223. ;; an ARG: switch to the buffer related to the inferior
  19224. ;; process.
  19225. (switch-to-buffer
  19226. (funcall (intern (concat "org-babel-prep-session:" lang))
  19227. session params))))))
  19228. (keyword
  19229. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19230. (org-open-link-from-string
  19231. (format "[[%s]]"
  19232. (expand-file-name
  19233. (let ((value (org-element-property :value element)))
  19234. (cond ((not (org-string-nw-p value))
  19235. (user-error "No file to edit"))
  19236. ((string-match "\\`\"\\(.*?\\)\"" value)
  19237. (match-string 1 value))
  19238. ((string-match "\\`[^ \t\"]\\S-*" value)
  19239. (match-string 0 value))
  19240. (t (user-error "No valid file specified")))))))
  19241. (user-error "No special environment to edit here")))
  19242. (table
  19243. (if (eq (org-element-property :type element) 'table.el)
  19244. (org-edit-table.el)
  19245. (call-interactively 'org-table-edit-formulas)))
  19246. ;; Only Org tables contain `table-row' type elements.
  19247. (table-row (call-interactively 'org-table-edit-formulas))
  19248. (example-block (org-edit-src-code))
  19249. (export-block (org-edit-export-block))
  19250. (fixed-width (org-edit-fixed-width-region))
  19251. (otherwise
  19252. ;; No notable element at point. Though, we may be at a link or
  19253. ;; a footnote reference, which are objects. Thus, scan deeper.
  19254. (let ((context (org-element-context element)))
  19255. (case (org-element-type context)
  19256. (link (call-interactively #'ffap))
  19257. (footnote-reference (org-edit-footnote-reference))
  19258. (t (user-error "No special environment to edit here"))))))))
  19259. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19260. (defun org-ctrl-c-ctrl-c (&optional arg)
  19261. "Set tags in headline, or update according to changed information at point.
  19262. This command does many different things, depending on context:
  19263. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19264. this is what we do.
  19265. - If the cursor is on a statistics cookie, update it.
  19266. - If the cursor is in a headline, prompt for tags and insert them
  19267. into the current line, aligned to `org-tags-column'. When called
  19268. with prefix arg, realign all tags in the current buffer.
  19269. - If the cursor is in one of the special #+KEYWORD lines, this
  19270. triggers scanning the buffer for these lines and updating the
  19271. information.
  19272. - If the cursor is inside a table, realign the table. This command
  19273. works even if the automatic table editor has been turned off.
  19274. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19275. the entire table.
  19276. - If the cursor is at a footnote reference or definition, jump to
  19277. the corresponding definition or references, respectively.
  19278. - If the cursor is a the beginning of a dynamic block, update it.
  19279. - If the current buffer is a capture buffer, close note and file it.
  19280. - If the cursor is on a <<<target>>>, update radio targets and
  19281. corresponding links in this buffer.
  19282. - If the cursor is on a numbered item in a plain list, renumber the
  19283. ordered list.
  19284. - If the cursor is on a checkbox, toggle it.
  19285. - If the cursor is on a code block, evaluate it. The variable
  19286. `org-confirm-babel-evaluate' can be used to control prompting
  19287. before code block evaluation, by default every code block
  19288. evaluation requires confirmation. Code block evaluation can be
  19289. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19290. (interactive "P")
  19291. (cond
  19292. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19293. org-occur-highlights)
  19294. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19295. (org-remove-occur-highlights)
  19296. (message "Temporary highlights/overlays removed from current buffer"))
  19297. ((and (local-variable-p 'org-finish-function (current-buffer))
  19298. (fboundp org-finish-function))
  19299. (funcall org-finish-function))
  19300. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19301. (t
  19302. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19303. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19304. (user-error "C-c C-c can do nothing useful at this location"))
  19305. (let* ((context (org-element-context))
  19306. (type (org-element-type context)))
  19307. (case type
  19308. ;; When at a link, act according to the parent instead.
  19309. (link (setq context (org-element-property :parent context))
  19310. (setq type (org-element-type context)))
  19311. ;; Unsupported object types: refer to the first supported
  19312. ;; element or object containing it.
  19313. ((bold code entity export-snippet inline-babel-call inline-src-block
  19314. italic latex-fragment line-break macro strike-through subscript
  19315. superscript underline verbatim)
  19316. (setq context
  19317. (org-element-lineage
  19318. context '(radio-target paragraph verse-block table-cell)))))
  19319. ;; For convenience: at the first line of a paragraph on the
  19320. ;; same line as an item, apply function on that item instead.
  19321. (when (eq type 'paragraph)
  19322. (let ((parent (org-element-property :parent context)))
  19323. (when (and (eq (org-element-type parent) 'item)
  19324. (= (line-beginning-position)
  19325. (org-element-property :begin parent)))
  19326. (setq context parent type 'item))))
  19327. ;; Act according to type of element or object at point.
  19328. (case type
  19329. (clock (org-clock-update-time-maybe))
  19330. (dynamic-block
  19331. (save-excursion
  19332. (goto-char (org-element-property :post-affiliated context))
  19333. (org-update-dblock)))
  19334. (footnote-definition
  19335. (goto-char (org-element-property :post-affiliated context))
  19336. (call-interactively 'org-footnote-action))
  19337. (footnote-reference (call-interactively 'org-footnote-action))
  19338. ((headline inlinetask)
  19339. (save-excursion (goto-char (org-element-property :begin context))
  19340. (call-interactively 'org-set-tags)))
  19341. (item
  19342. ;; At an item: a double C-u set checkbox to "[-]"
  19343. ;; unconditionally, whereas a single one will toggle its
  19344. ;; presence. Without a universal argument, if the item
  19345. ;; has a checkbox, toggle it. Otherwise repair the list.
  19346. (let* ((box (org-element-property :checkbox context))
  19347. (struct (org-element-property :structure context))
  19348. (old-struct (copy-tree struct))
  19349. (parents (org-list-parents-alist struct))
  19350. (prevs (org-list-prevs-alist struct))
  19351. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19352. (org-list-set-checkbox
  19353. (org-element-property :begin context) struct
  19354. (cond ((equal arg '(16)) "[-]")
  19355. ((and (not box) (equal arg '(4))) "[ ]")
  19356. ((or (not box) (equal arg '(4))) nil)
  19357. ((eq box 'on) "[ ]")
  19358. (t "[X]")))
  19359. ;; Mimic `org-list-write-struct' but with grabbing
  19360. ;; a return value from `org-list-struct-fix-box'.
  19361. (org-list-struct-fix-ind struct parents 2)
  19362. (org-list-struct-fix-item-end struct)
  19363. (org-list-struct-fix-bul struct prevs)
  19364. (org-list-struct-fix-ind struct parents)
  19365. (let ((block-item
  19366. (org-list-struct-fix-box struct parents prevs orderedp)))
  19367. (if (and box (equal struct old-struct))
  19368. (if (equal arg '(16))
  19369. (message "Checkboxes already reset")
  19370. (user-error "Cannot toggle this checkbox: %s"
  19371. (if (eq box 'on)
  19372. "all subitems checked"
  19373. "unchecked subitems")))
  19374. (org-list-struct-apply-struct struct old-struct)
  19375. (org-update-checkbox-count-maybe))
  19376. (when block-item
  19377. (message "Checkboxes were removed due to empty box at line %d"
  19378. (org-current-line block-item))))))
  19379. (keyword
  19380. (let ((org-inhibit-startup-visibility-stuff t)
  19381. (org-startup-align-all-tables nil))
  19382. (when (boundp 'org-table-coordinate-overlays)
  19383. (mapc 'delete-overlay org-table-coordinate-overlays)
  19384. (setq org-table-coordinate-overlays nil))
  19385. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19386. (message "Local setup has been refreshed"))
  19387. (plain-list
  19388. ;; At a plain list, with a double C-u argument, set
  19389. ;; checkboxes of each item to "[-]", whereas a single one
  19390. ;; will toggle their presence according to the state of the
  19391. ;; first item in the list. Without an argument, repair the
  19392. ;; list.
  19393. (let* ((begin (org-element-property :contents-begin context))
  19394. (beginm (move-marker (make-marker) begin))
  19395. (struct (org-element-property :structure context))
  19396. (old-struct (copy-tree struct))
  19397. (first-box (save-excursion
  19398. (goto-char begin)
  19399. (looking-at org-list-full-item-re)
  19400. (match-string-no-properties 3)))
  19401. (new-box (cond ((equal arg '(16)) "[-]")
  19402. ((equal arg '(4)) (unless first-box "[ ]"))
  19403. ((equal first-box "[X]") "[ ]")
  19404. (t "[X]"))))
  19405. (cond
  19406. (arg
  19407. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19408. (org-list-get-all-items
  19409. begin struct (org-list-prevs-alist struct))))
  19410. ((and first-box (eq (point) begin))
  19411. ;; For convenience, when point is at bol on the first
  19412. ;; item of the list and no argument is provided, simply
  19413. ;; toggle checkbox of that item, if any.
  19414. (org-list-set-checkbox begin struct new-box)))
  19415. (org-list-write-struct
  19416. struct (org-list-parents-alist struct) old-struct)
  19417. (org-update-checkbox-count-maybe)
  19418. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19419. ((property-drawer node-property)
  19420. (call-interactively 'org-property-action))
  19421. ((radio-target target)
  19422. (call-interactively 'org-update-radio-target-regexp))
  19423. (statistics-cookie
  19424. (call-interactively 'org-update-statistics-cookies))
  19425. ((table table-cell table-row)
  19426. ;; At a table, recalculate every field and align it. Also
  19427. ;; send the table if necessary. If the table has
  19428. ;; a `table.el' type, just give up. At a table row or
  19429. ;; cell, maybe recalculate line but always align table.
  19430. (if (eq (org-element-property :type context) 'table.el)
  19431. (message "Use C-c ' to edit table.el tables")
  19432. (let ((org-enable-table-editor t))
  19433. (if (or (eq type 'table)
  19434. ;; Check if point is at a TBLFM line.
  19435. (and (eq type 'table-row)
  19436. (= (point) (org-element-property :end context))))
  19437. (save-excursion
  19438. (if (org-at-TBLFM-p)
  19439. (progn (require 'org-table)
  19440. (org-table-calc-current-TBLFM))
  19441. (goto-char (org-element-property :contents-begin context))
  19442. (org-call-with-arg 'org-table-recalculate (or arg t))
  19443. (orgtbl-send-table 'maybe)))
  19444. (org-table-maybe-eval-formula)
  19445. (cond (arg (call-interactively 'org-table-recalculate))
  19446. ((org-table-maybe-recalculate-line))
  19447. (t (org-table-align)))))))
  19448. (timestamp (org-timestamp-change 0 'day))
  19449. (otherwise
  19450. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19451. (user-error
  19452. "C-c C-c can do nothing useful at this location")))))))))
  19453. (defun org-mode-restart ()
  19454. (interactive)
  19455. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19456. (funcall major-mode)
  19457. (hack-local-variables)
  19458. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19459. (org-indent-mode -1)))
  19460. (message "%s restarted" major-mode))
  19461. (defun org-kill-note-or-show-branches ()
  19462. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19463. (interactive)
  19464. (if (not org-finish-function)
  19465. (progn
  19466. (hide-subtree)
  19467. (call-interactively 'show-branches))
  19468. (let ((org-note-abort t))
  19469. (funcall org-finish-function))))
  19470. (defun org-delete-indentation (&optional ARG)
  19471. "Join current line to previous and fix whitespace at join.
  19472. If previous line is a headline add to headline title. Otherwise
  19473. the function calls `delete-indentation'.
  19474. With argument, join this line to following line."
  19475. (interactive "*P")
  19476. (if (save-excursion
  19477. (if ARG (beginning-of-line)
  19478. (forward-line -1))
  19479. (looking-at org-complex-heading-regexp))
  19480. ;; At headline.
  19481. (let ((tags-column (when (match-beginning 5)
  19482. (save-excursion (goto-char (match-beginning 5))
  19483. (current-column))))
  19484. (string (concat " " (progn (when ARG (forward-line 1))
  19485. (org-trim (delete-and-extract-region
  19486. (line-beginning-position)
  19487. (line-end-position)))))))
  19488. (unless (bobp) (delete-region (point) (1- (point))))
  19489. (goto-char (or (match-end 4)
  19490. (match-beginning 5)
  19491. (match-end 0)))
  19492. (skip-chars-backward " \t")
  19493. (save-excursion (insert string))
  19494. ;; Adjust alignment of tags.
  19495. (when tags-column
  19496. (org-align-tags-here (if org-auto-align-tags
  19497. org-tags-column
  19498. tags-column))))
  19499. (delete-indentation ARG)))
  19500. (defun org-open-line (n)
  19501. "Insert a new row in tables, call `open-line' elsewhere.
  19502. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19503. (interactive "*p")
  19504. (cond
  19505. ((not org-special-ctrl-o)
  19506. (open-line n))
  19507. ((org-at-table-p)
  19508. (org-table-insert-row))
  19509. (t
  19510. (open-line n))))
  19511. (defun org-return (&optional indent)
  19512. "Goto next table row or insert a newline.
  19513. Calls `org-table-next-row' or `newline', depending on context.
  19514. When optional INDENT argument is non-nil, call
  19515. `newline-and-indent' instead of `newline'.
  19516. When `org-return-follows-link' is non-nil and point is on
  19517. a timestamp or a link, call `org-open-at-point'. However, it
  19518. will not happen if point is in a table or on a \"dead\"
  19519. object (e.g., within a comment). In these case, you need to use
  19520. `org-open-at-point' directly."
  19521. (interactive)
  19522. (let* ((context (if org-return-follows-link (org-element-context)
  19523. (org-element-at-point)))
  19524. (type (org-element-type context)))
  19525. (cond
  19526. ;; In a table, call `org-table-next-row'.
  19527. ((or (and (eq type 'table)
  19528. (>= (point) (org-element-property :contents-begin context))
  19529. (< (point) (org-element-property :contents-end context)))
  19530. (org-element-lineage context '(table-row table-cell) t))
  19531. (org-table-justify-field-maybe)
  19532. (call-interactively #'org-table-next-row))
  19533. ;; On a link or a timestamp but not on white spaces after it,
  19534. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19535. ((and org-return-follows-link
  19536. (memq type '(link timestamp))
  19537. (< (point)
  19538. (save-excursion (goto-char (org-element-property :end context))
  19539. (skip-chars-backward " \t")
  19540. (point))))
  19541. (call-interactively #'org-open-at-point))
  19542. ;; Insert newline in heading, but preserve tags.
  19543. ((and (not (bolp))
  19544. (save-excursion (beginning-of-line)
  19545. (looking-at org-complex-heading-regexp)))
  19546. ;; At headline. Split line. However, if point is on keyword,
  19547. ;; priority cookie or tags, do not break any of them: add
  19548. ;; a newline after the headline instead.
  19549. (let ((tags-column (and (match-beginning 5)
  19550. (save-excursion (goto-char (match-beginning 5))
  19551. (current-column))))
  19552. (string
  19553. (when (and (match-end 4)
  19554. (>= (point)
  19555. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19556. (<= (point) (match-end 4)))
  19557. (delete-and-extract-region (point) (match-end 4)))))
  19558. (when (and tags-column string) ; Adjust tag alignment.
  19559. (org-align-tags-here
  19560. (if org-auto-align-tags org-tags-column tags-column)))
  19561. (end-of-line)
  19562. (org-show-entry)
  19563. (if indent (newline-and-indent) (newline))
  19564. (when string (save-excursion (insert (org-trim string))))))
  19565. ;; In a list, make sure indenting keeps trailing text within.
  19566. ((and indent
  19567. (not (eolp))
  19568. (org-element-lineage context '(item)))
  19569. (let ((trailing-data
  19570. (delete-and-extract-region (point) (line-end-position))))
  19571. (newline-and-indent)
  19572. (save-excursion (insert trailing-data))))
  19573. (t (if indent (newline-and-indent) (newline))))))
  19574. (defun org-return-indent ()
  19575. "Goto next table row or insert a newline and indent.
  19576. Calls `org-table-next-row' or `newline-and-indent', depending on
  19577. context. See the individual commands for more information."
  19578. (interactive)
  19579. (org-return t))
  19580. (defun org-ctrl-c-star ()
  19581. "Compute table, or change heading status of lines.
  19582. Calls `org-table-recalculate' or `org-toggle-heading',
  19583. depending on context."
  19584. (interactive)
  19585. (cond
  19586. ((org-at-table-p)
  19587. (call-interactively 'org-table-recalculate))
  19588. (t
  19589. ;; Convert all lines in region to list items
  19590. (call-interactively 'org-toggle-heading))))
  19591. (defun org-ctrl-c-minus ()
  19592. "Insert separator line in table or modify bullet status of line.
  19593. Also turns a plain line or a region of lines into list items.
  19594. Calls `org-table-insert-hline', `org-toggle-item', or
  19595. `org-cycle-list-bullet', depending on context."
  19596. (interactive)
  19597. (cond
  19598. ((org-at-table-p)
  19599. (call-interactively 'org-table-insert-hline))
  19600. ((org-region-active-p)
  19601. (call-interactively 'org-toggle-item))
  19602. ((org-in-item-p)
  19603. (call-interactively 'org-cycle-list-bullet))
  19604. (t
  19605. (call-interactively 'org-toggle-item))))
  19606. (defun org-toggle-item (arg)
  19607. "Convert headings or normal lines to items, items to normal lines.
  19608. If there is no active region, only the current line is considered.
  19609. If the first non blank line in the region is a headline, convert
  19610. all headlines to items, shifting text accordingly.
  19611. If it is an item, convert all items to normal lines.
  19612. If it is normal text, change region into a list of items.
  19613. With a prefix argument ARG, change the region in a single item."
  19614. (interactive "P")
  19615. (let ((shift-text
  19616. (function
  19617. ;; Shift text in current section to IND, from point to END.
  19618. ;; The function leaves point to END line.
  19619. (lambda (ind end)
  19620. (let ((min-i 1000) (end (copy-marker end)))
  19621. ;; First determine the minimum indentation (MIN-I) of
  19622. ;; the text.
  19623. (save-excursion
  19624. (catch 'exit
  19625. (while (< (point) end)
  19626. (let ((i (org-get-indentation)))
  19627. (cond
  19628. ;; Skip blank lines and inline tasks.
  19629. ((looking-at "^[ \t]*$"))
  19630. ((looking-at org-outline-regexp-bol))
  19631. ;; We can't find less than 0 indentation.
  19632. ((zerop i) (throw 'exit (setq min-i 0)))
  19633. ((< i min-i) (setq min-i i))))
  19634. (forward-line))))
  19635. ;; Then indent each line so that a line indented to
  19636. ;; MIN-I becomes indented to IND. Ignore blank lines
  19637. ;; and inline tasks in the process.
  19638. (let ((delta (- ind min-i)))
  19639. (while (< (point) end)
  19640. (unless (or (looking-at "^[ \t]*$")
  19641. (looking-at org-outline-regexp-bol))
  19642. (org-indent-line-to (+ (org-get-indentation) delta)))
  19643. (forward-line)))))))
  19644. (skip-blanks
  19645. (function
  19646. ;; Return beginning of first non-blank line, starting from
  19647. ;; line at POS.
  19648. (lambda (pos)
  19649. (save-excursion
  19650. (goto-char pos)
  19651. (skip-chars-forward " \r\t\n")
  19652. (point-at-bol)))))
  19653. beg end)
  19654. ;; Determine boundaries of changes.
  19655. (if (org-region-active-p)
  19656. (setq beg (funcall skip-blanks (region-beginning))
  19657. end (copy-marker (region-end)))
  19658. (setq beg (funcall skip-blanks (point-at-bol))
  19659. end (copy-marker (point-at-eol))))
  19660. ;; Depending on the starting line, choose an action on the text
  19661. ;; between BEG and END.
  19662. (org-with-limited-levels
  19663. (save-excursion
  19664. (goto-char beg)
  19665. (cond
  19666. ;; Case 1. Start at an item: de-itemize. Note that it only
  19667. ;; happens when a region is active: `org-ctrl-c-minus'
  19668. ;; would call `org-cycle-list-bullet' otherwise.
  19669. ((org-at-item-p)
  19670. (while (< (point) end)
  19671. (when (org-at-item-p)
  19672. (skip-chars-forward " \t")
  19673. (delete-region (point) (match-end 0)))
  19674. (forward-line)))
  19675. ;; Case 2. Start at an heading: convert to items.
  19676. ((org-at-heading-p)
  19677. (let* ((bul (org-list-bullet-string "-"))
  19678. (bul-len (length bul))
  19679. (done (org-entry-is-done-p))
  19680. (todo (org-entry-is-todo-p))
  19681. ;; Indentation of the first heading. It should be
  19682. ;; relative to the indentation of its parent, if any.
  19683. (start-ind (save-excursion
  19684. (cond
  19685. ((not org-adapt-indentation) 0)
  19686. ((not (outline-previous-heading)) 0)
  19687. (t (length (match-string 0))))))
  19688. ;; Level of first heading. Further headings will be
  19689. ;; compared to it to determine hierarchy in the list.
  19690. (ref-level (org-reduced-level (org-outline-level))))
  19691. (when (or done todo) (org-todo ""))
  19692. (while (< (point) end)
  19693. (let* ((level (org-reduced-level (org-outline-level)))
  19694. (delta (max 0 (- level ref-level))))
  19695. ;; If current headline is less indented than the first
  19696. ;; one, set it as reference, in order to preserve
  19697. ;; subtrees.
  19698. (when (< level ref-level) (setq ref-level level))
  19699. (replace-match bul t t)
  19700. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19701. (when (or done todo)
  19702. (let* ((struct (org-list-struct))
  19703. (old (copy-tree struct)))
  19704. (org-list-set-checkbox (line-beginning-position)
  19705. struct
  19706. (if done "[X]" "[ ]"))
  19707. (org-list-write-struct struct
  19708. (org-list-parents-alist struct)
  19709. old)))
  19710. ;; Ensure all text down to END (or SECTION-END) belongs
  19711. ;; to the newly created item.
  19712. (let ((section-end (save-excursion
  19713. (or (outline-next-heading) (point)))))
  19714. (forward-line)
  19715. (funcall shift-text
  19716. (+ start-ind (* (1+ delta) bul-len))
  19717. (min end section-end)))))))
  19718. ;; Case 3. Normal line with ARG: make the first line of region
  19719. ;; an item, and shift indentation of others lines to
  19720. ;; set them as item's body.
  19721. (arg (let* ((bul (org-list-bullet-string "-"))
  19722. (bul-len (length bul))
  19723. (ref-ind (org-get-indentation)))
  19724. (skip-chars-forward " \t")
  19725. (insert bul)
  19726. (forward-line)
  19727. (while (< (point) end)
  19728. ;; Ensure that lines less indented than first one
  19729. ;; still get included in item body.
  19730. (funcall shift-text
  19731. (+ ref-ind bul-len)
  19732. (min end (save-excursion (or (outline-next-heading)
  19733. (point)))))
  19734. (forward-line))))
  19735. ;; Case 4. Normal line without ARG: turn each non-item line
  19736. ;; into an item.
  19737. (t
  19738. (while (< (point) end)
  19739. (unless (or (org-at-heading-p) (org-at-item-p))
  19740. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19741. (replace-match
  19742. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19743. (forward-line))))))))
  19744. (defun org-toggle-heading (&optional nstars)
  19745. "Convert headings to normal text, or items or text to headings.
  19746. If there is no active region, only convert the current line.
  19747. With a \\[universal-argument] prefix, convert the whole list at
  19748. point into heading.
  19749. In a region:
  19750. - If the first non blank line is a headline, remove the stars
  19751. from all headlines in the region.
  19752. - If it is a normal line, turn each and every normal line (i.e.,
  19753. not an heading or an item) in the region into headings. If you
  19754. want to convert only the first line of this region, use one
  19755. universal prefix argument.
  19756. - If it is a plain list item, turn all plain list items into headings.
  19757. When converting a line into a heading, the number of stars is chosen
  19758. such that the lines become children of the current entry. However,
  19759. when a numeric prefix argument is given, its value determines the
  19760. number of stars to add."
  19761. (interactive "P")
  19762. (let ((skip-blanks
  19763. (function
  19764. ;; Return beginning of first non-blank line, starting from
  19765. ;; line at POS.
  19766. (lambda (pos)
  19767. (save-excursion
  19768. (goto-char pos)
  19769. (while (org-at-comment-p) (forward-line))
  19770. (skip-chars-forward " \r\t\n")
  19771. (point-at-bol)))))
  19772. beg end toggled)
  19773. ;; Determine boundaries of changes. If a universal prefix has
  19774. ;; been given, put the list in a region. If region ends at a bol,
  19775. ;; do not consider the last line to be in the region.
  19776. (when (and current-prefix-arg (org-at-item-p))
  19777. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19778. (org-mark-element))
  19779. (if (org-region-active-p)
  19780. (setq beg (funcall skip-blanks (region-beginning))
  19781. end (copy-marker (save-excursion
  19782. (goto-char (region-end))
  19783. (if (bolp) (point) (point-at-eol)))))
  19784. (setq beg (funcall skip-blanks (point-at-bol))
  19785. end (copy-marker (point-at-eol))))
  19786. ;; Ensure inline tasks don't count as headings.
  19787. (org-with-limited-levels
  19788. (save-excursion
  19789. (goto-char beg)
  19790. (cond
  19791. ;; Case 1. Started at an heading: de-star headings.
  19792. ((org-at-heading-p)
  19793. (while (< (point) end)
  19794. (when (org-at-heading-p t)
  19795. (looking-at org-outline-regexp) (replace-match "")
  19796. (setq toggled t))
  19797. (forward-line)))
  19798. ;; Case 2. Started at an item: change items into headlines.
  19799. ;; One star will be added by `org-list-to-subtree'.
  19800. ((org-at-item-p)
  19801. (while (< (point) end)
  19802. (when (org-at-item-p)
  19803. ;; Pay attention to cases when region ends before list.
  19804. (let* ((struct (org-list-struct))
  19805. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19806. (save-restriction
  19807. (narrow-to-region (point) list-end)
  19808. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19809. (setq toggled t))
  19810. (forward-line)))
  19811. ;; Case 3. Started at normal text: make every line an heading,
  19812. ;; skipping headlines and items.
  19813. (t (let* ((stars
  19814. (make-string
  19815. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19816. (add-stars
  19817. (cond (nstars "") ; stars from prefix only
  19818. ((equal stars "") "*") ; before first heading
  19819. (org-odd-levels-only "**") ; inside heading, odd
  19820. (t "*"))) ; inside heading, oddeven
  19821. (rpl (concat stars add-stars " "))
  19822. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19823. (while (< (point) (if (equal nstars '(4)) lend end))
  19824. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19825. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19826. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19827. (forward-line)))))))
  19828. (unless toggled (message "Cannot toggle heading from here"))))
  19829. (defun org-meta-return (&optional _arg)
  19830. "Insert a new heading or wrap a region in a table.
  19831. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19832. on context. See the individual commands for more information."
  19833. (interactive)
  19834. (org-check-before-invisible-edit 'insert)
  19835. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19836. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19837. #'org-insert-heading))))
  19838. ;;; Menu entries
  19839. (defsubst org-in-subtree-not-table-p ()
  19840. "Are we in a subtree and not in a table?"
  19841. (and (not (org-before-first-heading-p))
  19842. (not (org-at-table-p))))
  19843. ;; Define the Org-mode menus
  19844. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19845. '("Tbl"
  19846. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19847. ["Next Field" org-cycle (org-at-table-p)]
  19848. ["Previous Field" org-shifttab (org-at-table-p)]
  19849. ["Next Row" org-return (org-at-table-p)]
  19850. "--"
  19851. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19852. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19853. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19854. "--"
  19855. ("Column"
  19856. ["Move Column Left" org-metaleft (org-at-table-p)]
  19857. ["Move Column Right" org-metaright (org-at-table-p)]
  19858. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19859. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19860. ("Row"
  19861. ["Move Row Up" org-metaup (org-at-table-p)]
  19862. ["Move Row Down" org-metadown (org-at-table-p)]
  19863. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19864. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19865. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19866. "--"
  19867. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19868. ("Rectangle"
  19869. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19870. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19871. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19872. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19873. "--"
  19874. ("Calculate"
  19875. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19876. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19877. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19878. "--"
  19879. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19880. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19881. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19882. "--"
  19883. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19884. "--"
  19885. ["Sum Column/Rectangle" org-table-sum
  19886. (or (org-at-table-p) (org-region-active-p))]
  19887. ["Which Column?" org-table-current-column (org-at-table-p)])
  19888. ["Debug Formulas"
  19889. org-table-toggle-formula-debugger
  19890. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19891. ["Show Col/Row Numbers"
  19892. org-table-toggle-coordinate-overlays
  19893. :style toggle
  19894. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19895. "--"
  19896. ["Create" org-table-create (and (not (org-at-table-p))
  19897. org-enable-table-editor)]
  19898. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19899. ["Import from File" org-table-import (not (org-at-table-p))]
  19900. ["Export to File" org-table-export (org-at-table-p)]
  19901. "--"
  19902. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19903. "--"
  19904. ("Plot"
  19905. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19906. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19907. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19908. '("Org"
  19909. ("Show/Hide"
  19910. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19911. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19912. ["Sparse Tree..." org-sparse-tree t]
  19913. ["Reveal Context" org-reveal t]
  19914. ["Show All" show-all t]
  19915. "--"
  19916. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19917. "--"
  19918. ["New Heading" org-insert-heading t]
  19919. ("Navigate Headings"
  19920. ["Up" outline-up-heading t]
  19921. ["Next" outline-next-visible-heading t]
  19922. ["Previous" outline-previous-visible-heading t]
  19923. ["Next Same Level" outline-forward-same-level t]
  19924. ["Previous Same Level" outline-backward-same-level t]
  19925. "--"
  19926. ["Jump" org-goto t])
  19927. ("Edit Structure"
  19928. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19929. "--"
  19930. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19931. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19932. "--"
  19933. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19934. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19935. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19936. "--"
  19937. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19938. "--"
  19939. ["Copy visible text" org-copy-visible t]
  19940. "--"
  19941. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19942. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19943. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19944. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19945. "--"
  19946. ["Sort Region/Children" org-sort t]
  19947. "--"
  19948. ["Convert to odd levels" org-convert-to-odd-levels t]
  19949. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19950. ("Editing"
  19951. ["Emphasis..." org-emphasize t]
  19952. ["Edit Source Example" org-edit-special t]
  19953. "--"
  19954. ["Footnote new/jump" org-footnote-action t]
  19955. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19956. ("Archive"
  19957. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19958. "--"
  19959. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19960. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19961. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19962. )
  19963. "--"
  19964. ("Hyperlinks"
  19965. ["Store Link (Global)" org-store-link t]
  19966. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19967. ["Insert Link" org-insert-link t]
  19968. ["Follow Link" org-open-at-point t]
  19969. "--"
  19970. ["Next link" org-next-link t]
  19971. ["Previous link" org-previous-link t]
  19972. "--"
  19973. ["Descriptive Links"
  19974. org-toggle-link-display
  19975. :style radio
  19976. :selected org-descriptive-links
  19977. ]
  19978. ["Literal Links"
  19979. org-toggle-link-display
  19980. :style radio
  19981. :selected (not org-descriptive-links)])
  19982. "--"
  19983. ("TODO Lists"
  19984. ["TODO/DONE/-" org-todo t]
  19985. ("Select keyword"
  19986. ["Next keyword" org-shiftright (org-at-heading-p)]
  19987. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19988. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19989. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19990. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19991. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19992. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19993. "--"
  19994. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19995. :selected org-enforce-todo-dependencies :style toggle :active t]
  19996. "Settings for tree at point"
  19997. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19998. :selected (org-entry-get nil "ORDERED")
  19999. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20000. ["Do Children parallel" org-toggle-ordered-property :style radio
  20001. :selected (not (org-entry-get nil "ORDERED"))
  20002. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20003. "--"
  20004. ["Set Priority" org-priority t]
  20005. ["Priority Up" org-shiftup t]
  20006. ["Priority Down" org-shiftdown t]
  20007. "--"
  20008. ["Get news from all feeds" org-feed-update-all t]
  20009. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20010. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20011. ("TAGS and Properties"
  20012. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20013. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20014. "--"
  20015. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20016. ["Column view of properties" org-columns t]
  20017. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20018. ("Dates and Scheduling"
  20019. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20020. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20021. ("Change Date"
  20022. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20023. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20024. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20025. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20026. ["Compute Time Range" org-evaluate-time-range t]
  20027. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20028. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20029. "--"
  20030. ["Custom time format" org-toggle-time-stamp-overlays
  20031. :style radio :selected org-display-custom-times]
  20032. "--"
  20033. ["Goto Calendar" org-goto-calendar t]
  20034. ["Date from Calendar" org-date-from-calendar t]
  20035. "--"
  20036. ["Start/Restart Timer" org-timer-start t]
  20037. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20038. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20039. ["Insert Timer String" org-timer t]
  20040. ["Insert Timer Item" org-timer-item t])
  20041. ("Logging work"
  20042. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20043. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20044. ["Clock out" org-clock-out t]
  20045. ["Clock cancel" org-clock-cancel t]
  20046. "--"
  20047. ["Mark as default task" org-clock-mark-default-task t]
  20048. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20049. ["Goto running clock" org-clock-goto t]
  20050. "--"
  20051. ["Display times" org-clock-display t]
  20052. ["Create clock table" org-clock-report t]
  20053. "--"
  20054. ["Record DONE time"
  20055. (progn (setq org-log-done (not org-log-done))
  20056. (message "Switching to %s will %s record a timestamp"
  20057. (car org-done-keywords)
  20058. (if org-log-done "automatically" "not")))
  20059. :style toggle :selected org-log-done])
  20060. "--"
  20061. ["Agenda Command..." org-agenda t]
  20062. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20063. ("File List for Agenda")
  20064. ("Special views current file"
  20065. ["TODO Tree" org-show-todo-tree t]
  20066. ["Check Deadlines" org-check-deadlines t]
  20067. ["Timeline" org-timeline t]
  20068. ["Tags/Property tree" org-match-sparse-tree t])
  20069. "--"
  20070. ["Export/Publish..." org-export-dispatch t]
  20071. ("LaTeX"
  20072. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20073. :selected org-cdlatex-mode]
  20074. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20075. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20076. ["Modify math symbol" org-cdlatex-math-modify
  20077. (org-inside-LaTeX-fragment-p)]
  20078. ["Insert citation" org-reftex-citation t]
  20079. "--"
  20080. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20081. "--"
  20082. ("MobileOrg"
  20083. ["Push Files and Views" org-mobile-push t]
  20084. ["Get Captured and Flagged" org-mobile-pull t]
  20085. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20086. "--"
  20087. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20088. "--"
  20089. ("Documentation"
  20090. ["Show Version" org-version t]
  20091. ["Info Documentation" org-info t])
  20092. ("Customize"
  20093. ["Browse Org Group" org-customize t]
  20094. "--"
  20095. ["Expand This Menu" org-create-customize-menu
  20096. (fboundp 'customize-menu-create)])
  20097. ["Send bug report" org-submit-bug-report t]
  20098. "--"
  20099. ("Refresh/Reload"
  20100. ["Refresh setup current buffer" org-mode-restart t]
  20101. ["Reload Org (after update)" org-reload t]
  20102. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20103. ))
  20104. (defun org-info (&optional node)
  20105. "Read documentation for Org-mode in the info system.
  20106. With optional NODE, go directly to that node."
  20107. (interactive)
  20108. (info (format "(org)%s" (or node ""))))
  20109. ;;;###autoload
  20110. (defun org-submit-bug-report ()
  20111. "Submit a bug report on Org-mode via mail.
  20112. Don't hesitate to report any problems or inaccurate documentation.
  20113. If you don't have setup sending mail from (X)Emacs, please copy the
  20114. output buffer into your mail program, as it gives us important
  20115. information about your Org-mode version and configuration."
  20116. (interactive)
  20117. (require 'reporter)
  20118. (defvar reporter-prompt-for-summary-p)
  20119. (org-load-modules-maybe)
  20120. (org-require-autoloaded-modules)
  20121. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20122. (reporter-submit-bug-report
  20123. "emacs-orgmode@gnu.org"
  20124. (org-version nil 'full)
  20125. (let (list)
  20126. (save-window-excursion
  20127. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20128. (delete-other-windows)
  20129. (erase-buffer)
  20130. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20131. We would like to add your full Org-mode and Outline configuration to the
  20132. bug report. This greatly simplifies the work of the maintainer and
  20133. other experts on the mailing list.
  20134. HOWEVER, some variables you have customized may contain private
  20135. information. The names of customers, colleagues, or friends, might
  20136. appear in the form of file names, tags, todo states, or search strings.
  20137. If you answer yes to the prompt, you might want to check and remove
  20138. such private information before sending the email.")
  20139. (add-text-properties (point-min) (point-max) '(face org-warning))
  20140. (when (yes-or-no-p "Include your Org-mode configuration ")
  20141. (mapatoms
  20142. (lambda (v)
  20143. (and (boundp v)
  20144. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20145. (or (and (symbol-value v)
  20146. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20147. (and
  20148. (get v 'custom-type) (get v 'standard-value)
  20149. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20150. (push v list)))))
  20151. (kill-buffer (get-buffer "*Warn about privacy*"))
  20152. list))
  20153. nil nil
  20154. "Remember to cover the basics, that is, what you expected to happen and
  20155. what in fact did happen. You don't know how to make a good report? See
  20156. http://orgmode.org/manual/Feedback.html#Feedback
  20157. Your bug report will be posted to the Org-mode mailing list.
  20158. ------------------------------------------------------------------------")
  20159. (save-excursion
  20160. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20161. (replace-match "\\1Bug: \\3 [\\2]")))))
  20162. (defun org-install-agenda-files-menu ()
  20163. (let ((bl (buffer-list)))
  20164. (save-excursion
  20165. (while bl
  20166. (set-buffer (pop bl))
  20167. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20168. (when (derived-mode-p 'org-mode)
  20169. (easy-menu-change
  20170. '("Org") "File List for Agenda"
  20171. (append
  20172. (list
  20173. ["Edit File List" (org-edit-agenda-file-list) t]
  20174. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20175. ["Remove Current File from List" org-remove-file t]
  20176. ["Cycle through agenda files" org-cycle-agenda-files t]
  20177. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20178. "--")
  20179. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20180. ;;;; Documentation
  20181. (defun org-require-autoloaded-modules ()
  20182. (interactive)
  20183. (mapc 'require
  20184. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20185. org-table org-timer)))
  20186. ;;;###autoload
  20187. (defun org-reload (&optional uncompiled)
  20188. "Reload all org lisp files.
  20189. With prefix arg UNCOMPILED, load the uncompiled versions."
  20190. (interactive "P")
  20191. (require 'loadhist)
  20192. (let* ((org-dir (org-find-library-dir "org"))
  20193. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20194. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20195. (remove-re (mapconcat 'identity
  20196. (mapcar (lambda (f) (concat "^" f "$"))
  20197. (list (if (featurep 'xemacs)
  20198. "org-colview"
  20199. "org-colview-xemacs")
  20200. "org" "org-loaddefs" "org-version"))
  20201. "\\|"))
  20202. (feats (delete-dups
  20203. (mapcar 'file-name-sans-extension
  20204. (mapcar 'file-name-nondirectory
  20205. (delq nil
  20206. (mapcar 'feature-file
  20207. features))))))
  20208. (lfeat (append
  20209. (sort
  20210. (setq feats
  20211. (delq nil (mapcar
  20212. (lambda (f)
  20213. (if (and (string-match feature-re f)
  20214. (not (string-match remove-re f)))
  20215. f nil))
  20216. feats)))
  20217. 'string-lessp)
  20218. (list "org-version" "org")))
  20219. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20220. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20221. load-uncore load-misses)
  20222. (setq load-misses
  20223. (delq 't
  20224. (mapcar (lambda (f)
  20225. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20226. (and (string= org-dir contrib-dir)
  20227. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20228. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20229. (add-to-list 'load-uncore f 'append)
  20230. 't)
  20231. f))
  20232. lfeat)))
  20233. (if load-uncore
  20234. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20235. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20236. (if load-misses
  20237. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20238. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20239. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20240. ;;;###autoload
  20241. (defun org-customize ()
  20242. "Call the customize function with org as argument."
  20243. (interactive)
  20244. (org-load-modules-maybe)
  20245. (org-require-autoloaded-modules)
  20246. (customize-browse 'org))
  20247. (defun org-create-customize-menu ()
  20248. "Create a full customization menu for Org-mode, insert it into the menu."
  20249. (interactive)
  20250. (org-load-modules-maybe)
  20251. (org-require-autoloaded-modules)
  20252. (if (fboundp 'customize-menu-create)
  20253. (progn
  20254. (easy-menu-change
  20255. '("Org") "Customize"
  20256. `(["Browse Org group" org-customize t]
  20257. "--"
  20258. ,(customize-menu-create 'org)
  20259. ["Set" Custom-set t]
  20260. ["Save" Custom-save t]
  20261. ["Reset to Current" Custom-reset-current t]
  20262. ["Reset to Saved" Custom-reset-saved t]
  20263. ["Reset to Standard Settings" Custom-reset-standard t]))
  20264. (message "\"Org\"-menu now contains full customization menu"))
  20265. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20266. ;;;; Miscellaneous stuff
  20267. ;;; Generally useful functions
  20268. (defsubst org-get-at-eol (property n)
  20269. "Get text property PROPERTY at the end of line less N characters."
  20270. (get-text-property (- (point-at-eol) n) property))
  20271. (defun org-find-text-property-in-string (prop s)
  20272. "Return the first non-nil value of property PROP in string S."
  20273. (or (get-text-property 0 prop s)
  20274. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20275. prop s)))
  20276. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20277. "Display the given MESSAGE as a warning."
  20278. (if (fboundp 'display-warning)
  20279. (display-warning 'org message
  20280. (if (featurep 'xemacs) 'warning :warning))
  20281. (let ((buf (get-buffer-create "*Org warnings*")))
  20282. (with-current-buffer buf
  20283. (goto-char (point-max))
  20284. (insert "Warning (Org): " message)
  20285. (unless (bolp)
  20286. (newline)))
  20287. (display-buffer buf)
  20288. (sit-for 0))))
  20289. (defun org-eval (form)
  20290. "Eval FORM and return result."
  20291. (condition-case error
  20292. (eval form)
  20293. (error (format "%%![Error: %s]" error))))
  20294. (defun org-in-clocktable-p ()
  20295. "Check if the cursor is in a clocktable."
  20296. (let ((pos (point)) start)
  20297. (save-excursion
  20298. (end-of-line 1)
  20299. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20300. (setq start (match-beginning 0))
  20301. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20302. (>= (match-end 0) pos)
  20303. start))))
  20304. (defun org-in-verbatim-emphasis ()
  20305. (save-match-data
  20306. (and (org-in-regexp org-emph-re 2)
  20307. (>= (point) (match-beginning 3))
  20308. (<= (point) (match-end 4))
  20309. (member (match-string 3) '("=" "~")))))
  20310. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20311. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20312. (if (and marker (marker-buffer marker)
  20313. (buffer-live-p (marker-buffer marker)))
  20314. (progn
  20315. (org-pop-to-buffer-same-window (marker-buffer marker))
  20316. (if (or (> marker (point-max)) (< marker (point-min)))
  20317. (widen))
  20318. (goto-char marker)
  20319. (org-show-context 'org-goto))
  20320. (if bookmark
  20321. (bookmark-jump bookmark)
  20322. (error "Cannot find location"))))
  20323. (defun org-quote-csv-field (s)
  20324. "Quote field for inclusion in CSV material."
  20325. (if (string-match "[\",]" s)
  20326. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20327. s))
  20328. (defun org-force-self-insert (N)
  20329. "Needed to enforce self-insert under remapping."
  20330. (interactive "p")
  20331. (self-insert-command N))
  20332. (defun org-string-width (s)
  20333. "Compute width of string, ignoring invisible characters.
  20334. This ignores character with invisibility property `org-link', and also
  20335. characters with property `org-cwidth', because these will become invisible
  20336. upon the next fontification round."
  20337. (let (b l)
  20338. (when (or (eq t buffer-invisibility-spec)
  20339. (assq 'org-link buffer-invisibility-spec))
  20340. (while (setq b (text-property-any 0 (length s)
  20341. 'invisible 'org-link s))
  20342. (setq s (concat (substring s 0 b)
  20343. (substring s (or (next-single-property-change
  20344. b 'invisible s)
  20345. (length s)))))))
  20346. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20347. (setq s (concat (substring s 0 b)
  20348. (substring s (or (next-single-property-change
  20349. b 'org-cwidth s)
  20350. (length s))))))
  20351. (setq l (string-width s) b -1)
  20352. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20353. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20354. l))
  20355. (defun org-shorten-string (s maxlength)
  20356. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20357. If the string is shorter or has length MAXLENGTH, just return the
  20358. original string. If it is longer, the functions finds a space in the
  20359. string, breaks this string off at that locations and adds three dots
  20360. as ellipsis. Including the ellipsis, the string will not be longer
  20361. than MAXLENGTH. If finding a good breaking point in the string does
  20362. not work, the string is just chopped off in the middle of a word
  20363. if necessary."
  20364. (if (<= (length s) maxlength)
  20365. s
  20366. (let* ((n (max (- maxlength 4) 1))
  20367. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20368. (if (string-match re s)
  20369. (concat (match-string 1 s) "...")
  20370. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20371. (defun org-get-indentation (&optional line)
  20372. "Get the indentation of the current line, interpreting tabs.
  20373. When LINE is given, assume it represents a line and compute its indentation."
  20374. (if line
  20375. (if (string-match "^ *" (org-remove-tabs line))
  20376. (match-end 0))
  20377. (save-excursion
  20378. (beginning-of-line 1)
  20379. (skip-chars-forward " \t")
  20380. (current-column))))
  20381. (defun org-get-string-indentation (s)
  20382. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20383. (let ((n -1) (i 0) (w tab-width) c)
  20384. (catch 'exit
  20385. (while (< (setq n (1+ n)) (length s))
  20386. (setq c (aref s n))
  20387. (cond ((= c ?\ ) (setq i (1+ i)))
  20388. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20389. (t (throw 'exit t)))))
  20390. i))
  20391. (defun org-remove-tabs (s &optional width)
  20392. "Replace tabulators in S with spaces.
  20393. Assumes that s is a single line, starting in column 0."
  20394. (setq width (or width tab-width))
  20395. (while (string-match "\t" s)
  20396. (setq s (replace-match
  20397. (make-string
  20398. (- (* width (/ (+ (match-beginning 0) width) width))
  20399. (match-beginning 0)) ?\ )
  20400. t t s)))
  20401. s)
  20402. (defun org-fix-indentation (line ind)
  20403. "Fix indentation in LINE.
  20404. IND is a cons cell with target and minimum indentation.
  20405. If the current indentation in LINE is smaller than the minimum,
  20406. leave it alone. If it is larger than ind, set it to the target."
  20407. (let* ((l (org-remove-tabs line))
  20408. (i (org-get-indentation l))
  20409. (i1 (car ind)) (i2 (cdr ind)))
  20410. (if (>= i i2) (setq l (substring line i2)))
  20411. (if (> i1 0)
  20412. (concat (make-string i1 ?\ ) l)
  20413. l)))
  20414. (defun org-remove-indentation (code &optional n)
  20415. "Remove the maximum common indentation from the lines in CODE.
  20416. N may optionally be the number of spaces to remove."
  20417. (with-temp-buffer
  20418. (insert code)
  20419. (org-do-remove-indentation n)
  20420. (buffer-string)))
  20421. (defun org-do-remove-indentation (&optional n)
  20422. "Remove the maximum common indentation from the buffer."
  20423. (untabify (point-min) (point-max))
  20424. (let ((min 10000) re)
  20425. (if n
  20426. (setq min n)
  20427. (goto-char (point-min))
  20428. (while (re-search-forward "^ *[^ \n]" nil t)
  20429. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20430. (unless (or (= min 0) (= min 10000))
  20431. (setq re (format "^ \\{%d\\}" min))
  20432. (goto-char (point-min))
  20433. (while (re-search-forward re nil t)
  20434. (replace-match "")
  20435. (end-of-line 1))
  20436. min)))
  20437. (defun org-fill-template (template alist)
  20438. "Find each %key of ALIST in TEMPLATE and replace it."
  20439. (let ((case-fold-search nil))
  20440. (dolist (entry (sort (copy-sequence alist)
  20441. (lambda (a b) (< (length (car a)) (length (car b))))))
  20442. (setq template
  20443. (replace-regexp-in-string
  20444. (concat "%" (regexp-quote (car entry)))
  20445. (or (cdr entry) "") template t t)))
  20446. template))
  20447. (defun org-base-buffer (buffer)
  20448. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20449. (if (not buffer)
  20450. buffer
  20451. (or (buffer-base-buffer buffer)
  20452. buffer)))
  20453. (defun org-wrap (string &optional width lines)
  20454. "Wrap string to either a number of lines, or a width in characters.
  20455. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20456. that costs. If there is a word longer than WIDTH, the text is actually
  20457. wrapped to the length of that word.
  20458. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20459. many lines, whatever width that takes.
  20460. The return value is a list of lines, without newlines at the end."
  20461. (let* ((words (org-split-string string "[ \t\n]+"))
  20462. (maxword (apply 'max (mapcar 'org-string-width words)))
  20463. w ll)
  20464. (cond (width
  20465. (org-do-wrap words (max maxword width)))
  20466. (lines
  20467. (setq w maxword)
  20468. (setq ll (org-do-wrap words maxword))
  20469. (if (<= (length ll) lines)
  20470. ll
  20471. (setq ll words)
  20472. (while (> (length ll) lines)
  20473. (setq w (1+ w))
  20474. (setq ll (org-do-wrap words w)))
  20475. ll))
  20476. (t (error "Cannot wrap this")))))
  20477. (defun org-do-wrap (words width)
  20478. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20479. (let (lines line)
  20480. (while words
  20481. (setq line (pop words))
  20482. (while (and words (< (+ (length line) (length (car words))) width))
  20483. (setq line (concat line " " (pop words))))
  20484. (setq lines (push line lines)))
  20485. (nreverse lines)))
  20486. (defun org-split-string (string &optional separators)
  20487. "Splits STRING into substrings at SEPARATORS.
  20488. SEPARATORS is a regular expression.
  20489. No empty strings are returned if there are matches at the beginning
  20490. and end of string."
  20491. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20492. (let ((start 0) notfirst list)
  20493. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20494. (if (and notfirst
  20495. (= start (match-beginning 0))
  20496. (< start (length string)))
  20497. (1+ start) start))
  20498. (< (match-beginning 0) (length string)))
  20499. (setq notfirst t)
  20500. (or (eq (match-beginning 0) 0)
  20501. (and (eq (match-beginning 0) (match-end 0))
  20502. (eq (match-beginning 0) start))
  20503. (push (substring string start (match-beginning 0)) list))
  20504. (setq start (match-end 0)))
  20505. (or (eq start (length string))
  20506. (push (substring string start) list))
  20507. (nreverse list)))
  20508. (defun org-quote-vert (s)
  20509. "Replace \"|\" with \"\\vert\"."
  20510. (while (string-match "|" s)
  20511. (setq s (replace-match "\\vert" t t s)))
  20512. s)
  20513. (defun org-uuidgen-p (s)
  20514. "Is S an ID created by UUIDGEN?"
  20515. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20516. (defun org-in-src-block-p (&optional inside)
  20517. "Whether point is in a code source block.
  20518. When INSIDE is non-nil, don't consider we are within a src block
  20519. when point is at #+BEGIN_SRC or #+END_SRC."
  20520. (let ((case-fold-search t) ov)
  20521. (or (and (eq (get-char-property (point) 'src-block) t))
  20522. (and (not inside)
  20523. (save-match-data
  20524. (save-excursion
  20525. (beginning-of-line)
  20526. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20527. (defun org-context ()
  20528. "Return a list of contexts of the current cursor position.
  20529. If several contexts apply, all are returned.
  20530. Each context entry is a list with a symbol naming the context, and
  20531. two positions indicating start and end of the context. Possible
  20532. contexts are:
  20533. :headline anywhere in a headline
  20534. :headline-stars on the leading stars in a headline
  20535. :todo-keyword on a TODO keyword (including DONE) in a headline
  20536. :tags on the TAGS in a headline
  20537. :priority on the priority cookie in a headline
  20538. :item on the first line of a plain list item
  20539. :item-bullet on the bullet/number of a plain list item
  20540. :checkbox on the checkbox in a plain list item
  20541. :table in an org-mode table
  20542. :table-special on a special filed in a table
  20543. :table-table in a table.el table
  20544. :clocktable in a clocktable
  20545. :src-block in a source block
  20546. :link on a hyperlink
  20547. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20548. :target on a <<target>>
  20549. :radio-target on a <<<radio-target>>>
  20550. :latex-fragment on a LaTeX fragment
  20551. :latex-preview on a LaTeX fragment with overlaid preview image
  20552. This function expects the position to be visible because it uses font-lock
  20553. faces as a help to recognize the following contexts: :table-special, :link,
  20554. and :keyword."
  20555. (let* ((f (get-text-property (point) 'face))
  20556. (faces (if (listp f) f (list f)))
  20557. (case-fold-search t)
  20558. (p (point)) clist o)
  20559. ;; First the large context
  20560. (cond
  20561. ((org-at-heading-p t)
  20562. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20563. (when (progn
  20564. (beginning-of-line 1)
  20565. (looking-at org-todo-line-tags-regexp))
  20566. (push (org-point-in-group p 1 :headline-stars) clist)
  20567. (push (org-point-in-group p 2 :todo-keyword) clist)
  20568. (push (org-point-in-group p 4 :tags) clist))
  20569. (goto-char p)
  20570. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20571. (if (looking-at "\\[#[A-Z0-9]\\]")
  20572. (push (org-point-in-group p 0 :priority) clist)))
  20573. ((org-at-item-p)
  20574. (push (org-point-in-group p 2 :item-bullet) clist)
  20575. (push (list :item (point-at-bol)
  20576. (save-excursion (org-end-of-item) (point)))
  20577. clist)
  20578. (and (org-at-item-checkbox-p)
  20579. (push (org-point-in-group p 0 :checkbox) clist)))
  20580. ((org-at-table-p)
  20581. (push (list :table (org-table-begin) (org-table-end)) clist)
  20582. (if (memq 'org-formula faces)
  20583. (push (list :table-special
  20584. (previous-single-property-change p 'face)
  20585. (next-single-property-change p 'face)) clist)))
  20586. ((org-at-table-p 'any)
  20587. (push (list :table-table) clist)))
  20588. (goto-char p)
  20589. (let ((case-fold-search t))
  20590. ;; New the "medium" contexts: clocktables, source blocks
  20591. (cond ((org-in-clocktable-p)
  20592. (push (list :clocktable
  20593. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20594. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20595. (match-beginning 1))
  20596. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20597. (match-end 0))) clist))
  20598. ((org-in-src-block-p)
  20599. (push (list :src-block
  20600. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20601. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20602. (match-beginning 1))
  20603. (and (search-forward "#+END_SRC" nil t)
  20604. (match-beginning 0))) clist))))
  20605. (goto-char p)
  20606. ;; Now the small context
  20607. (cond
  20608. ((org-at-timestamp-p)
  20609. (push (org-point-in-group p 0 :timestamp) clist))
  20610. ((memq 'org-link faces)
  20611. (push (list :link
  20612. (previous-single-property-change p 'face)
  20613. (next-single-property-change p 'face)) clist))
  20614. ((memq 'org-special-keyword faces)
  20615. (push (list :keyword
  20616. (previous-single-property-change p 'face)
  20617. (next-single-property-change p 'face)) clist))
  20618. ((org-at-target-p)
  20619. (push (org-point-in-group p 0 :target) clist)
  20620. (goto-char (1- (match-beginning 0)))
  20621. (if (looking-at org-radio-target-regexp)
  20622. (push (org-point-in-group p 0 :radio-target) clist))
  20623. (goto-char p))
  20624. ((setq o (car (delq nil
  20625. (mapcar
  20626. (lambda (x)
  20627. (if (memq x org-latex-fragment-image-overlays) x))
  20628. (overlays-at (point))))))
  20629. (push (list :latex-fragment
  20630. (overlay-start o) (overlay-end o)) clist)
  20631. (push (list :latex-preview
  20632. (overlay-start o) (overlay-end o)) clist))
  20633. ((org-inside-LaTeX-fragment-p)
  20634. ;; FIXME: positions wrong.
  20635. (push (list :latex-fragment (point) (point)) clist)))
  20636. (setq clist (nreverse (delq nil clist)))
  20637. clist))
  20638. (defun org-in-regexp (re &optional nlines visually)
  20639. "Check if point is inside a match of RE.
  20640. Normally only the current line is checked, but you can include
  20641. NLINES extra lines after point into the search. If VISUALLY is
  20642. set, require that the cursor is not after the match but really
  20643. on, so that the block visually is on the match."
  20644. (catch 'exit
  20645. (let ((pos (point))
  20646. (eol (point-at-eol (+ 1 (or nlines 0))))
  20647. (inc (if visually 1 0)))
  20648. (save-excursion
  20649. (beginning-of-line (- 1 (or nlines 0)))
  20650. (while (re-search-forward re eol t)
  20651. (if (and (<= (match-beginning 0) pos)
  20652. (>= (+ inc (match-end 0)) pos))
  20653. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20654. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20655. "Org mode 8.3")
  20656. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20657. "Non-nil when point is between matches of START-RE and END-RE.
  20658. Also return a non-nil value when point is on one of the matches.
  20659. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20660. buffer positions. Default values are the positions of headlines
  20661. surrounding the point.
  20662. The functions returns a cons cell whose car (resp. cdr) is the
  20663. position before START-RE (resp. after END-RE)."
  20664. (save-match-data
  20665. (let ((pos (point))
  20666. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20667. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20668. beg end)
  20669. (save-excursion
  20670. ;; Point is on a block when on START-RE or if START-RE can be
  20671. ;; found before it...
  20672. (and (or (org-in-regexp start-re)
  20673. (re-search-backward start-re limit-up t))
  20674. (setq beg (match-beginning 0))
  20675. ;; ... and END-RE after it...
  20676. (goto-char (match-end 0))
  20677. (re-search-forward end-re limit-down t)
  20678. (> (setq end (match-end 0)) pos)
  20679. ;; ... without another START-RE in-between.
  20680. (goto-char (match-beginning 0))
  20681. (not (re-search-backward start-re (1+ beg) t))
  20682. ;; Return value.
  20683. (cons beg end))))))
  20684. (defun org-in-block-p (names)
  20685. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20686. NAMES is a list of strings containing names of blocks.
  20687. Return first block name matched, or nil. Beware that in case of
  20688. nested blocks, the returned name may not belong to the closest
  20689. block from point."
  20690. (save-match-data
  20691. (catch 'exit
  20692. (let ((case-fold-search t)
  20693. (lim-up (save-excursion (outline-previous-heading)))
  20694. (lim-down (save-excursion (outline-next-heading))))
  20695. (mapc (lambda (name)
  20696. (let ((n (regexp-quote name)))
  20697. (when (org-between-regexps-p
  20698. (concat "^[ \t]*#\\+begin_" n)
  20699. (concat "^[ \t]*#\\+end_" n)
  20700. lim-up lim-down)
  20701. (throw 'exit n))))
  20702. names))
  20703. nil)))
  20704. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20705. "Call `multi-occur' with buffers for all agenda files."
  20706. (interactive "sOrg-files matching: ")
  20707. (let* ((files (org-agenda-files))
  20708. (tnames (mapcar #'file-truename files))
  20709. (extra org-agenda-text-search-extra-files))
  20710. (when (eq (car extra) 'agenda-archives)
  20711. (setq extra (cdr extra))
  20712. (setq files (org-add-archive-files files)))
  20713. (dolist (f extra)
  20714. (unless (member (file-truename f) tnames)
  20715. (unless (member f files) (setq files (append files (list f))))
  20716. (setq tnames (append tnames (list (file-truename f))))))
  20717. (multi-occur
  20718. (mapcar (lambda (x)
  20719. (with-current-buffer
  20720. ;; FIXME: Why not just (find-file-noselect x)?
  20721. ;; Is it to avoid the "revert buffer" prompt?
  20722. (or (get-file-buffer x) (find-file-noselect x))
  20723. (widen)
  20724. (current-buffer)))
  20725. files)
  20726. regexp)))
  20727. (if (boundp 'occur-mode-find-occurrence-hook)
  20728. ;; Emacs 23
  20729. (add-hook 'occur-mode-find-occurrence-hook
  20730. (lambda ()
  20731. (when (derived-mode-p 'org-mode)
  20732. (org-reveal))))
  20733. ;; Emacs 22
  20734. (defadvice occur-mode-goto-occurrence
  20735. (after org-occur-reveal activate)
  20736. (and (derived-mode-p 'org-mode) (org-reveal)))
  20737. (defadvice occur-mode-goto-occurrence-other-window
  20738. (after org-occur-reveal activate)
  20739. (and (derived-mode-p 'org-mode) (org-reveal)))
  20740. (defadvice occur-mode-display-occurrence
  20741. (after org-occur-reveal activate)
  20742. (when (derived-mode-p 'org-mode)
  20743. (let ((pos (occur-mode-find-occurrence)))
  20744. (with-current-buffer (marker-buffer pos)
  20745. (save-excursion
  20746. (goto-char pos)
  20747. (org-reveal)))))))
  20748. (defun org-occur-link-in-agenda-files ()
  20749. "Create a link and search for it in the agendas.
  20750. The link is not stored in `org-stored-links', it is just created
  20751. for the search purpose."
  20752. (interactive)
  20753. (let ((link (condition-case nil
  20754. (org-store-link nil)
  20755. (error "Unable to create a link to here"))))
  20756. (org-occur-in-agenda-files (regexp-quote link))))
  20757. (defun org-reverse-string (string)
  20758. "Return the reverse of STRING."
  20759. (apply 'string (reverse (string-to-list string))))
  20760. ;; defsubst org-uniquify must be defined before first use
  20761. (defun org-uniquify-alist (alist)
  20762. "Merge elements of ALIST with the same key.
  20763. For example, in this alist:
  20764. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20765. => '((a 1 3) (b 2))
  20766. merge (a 1) and (a 3) into (a 1 3).
  20767. The function returns the new ALIST."
  20768. (let (rtn)
  20769. (mapc
  20770. (lambda (e)
  20771. (let (n)
  20772. (if (not (assoc (car e) rtn))
  20773. (push e rtn)
  20774. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20775. (setq rtn (assq-delete-all (car e) rtn))
  20776. (push n rtn))))
  20777. alist)
  20778. rtn))
  20779. (defun org-delete-all (elts list)
  20780. "Remove all elements in ELTS from LIST."
  20781. (while elts
  20782. (setq list (delete (pop elts) list)))
  20783. list)
  20784. (defun org-count (cl-item cl-seq)
  20785. "Count the number of occurrences of ITEM in SEQ.
  20786. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20787. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20788. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20789. (while (< cl-start cl-end)
  20790. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20791. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20792. (setq cl-start (1+ cl-start)))
  20793. cl-count))
  20794. (defun org-remove-if (predicate seq)
  20795. "Remove everything from SEQ that fulfills PREDICATE."
  20796. (let (res e)
  20797. (while seq
  20798. (setq e (pop seq))
  20799. (if (not (funcall predicate e)) (push e res)))
  20800. (nreverse res)))
  20801. (defun org-remove-if-not (predicate seq)
  20802. "Remove everything from SEQ that does not fulfill PREDICATE."
  20803. (let (res e)
  20804. (while seq
  20805. (setq e (pop seq))
  20806. (if (funcall predicate e) (push e res)))
  20807. (nreverse res)))
  20808. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20809. "Reduce two-argument FUNCTION across SEQ.
  20810. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20811. \":initial-value\" removed."
  20812. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20813. (cadr (memq :initial-value cl-keys)))
  20814. (cl-seq (pop cl-seq))
  20815. (t (funcall cl-func)))))
  20816. (while cl-seq
  20817. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20818. cl-accum))
  20819. (defun org-every (pred seq)
  20820. "Return true if PREDICATE is true of every element of SEQ.
  20821. Adapted from `every' in cl.el."
  20822. (catch 'org-every
  20823. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20824. t))
  20825. (defun org-some (pred seq)
  20826. "Return true if PREDICATE is true of any element of SEQ.
  20827. Adapted from `some' in cl.el."
  20828. (catch 'org-some
  20829. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20830. nil))
  20831. (defun org-back-over-empty-lines ()
  20832. "Move backwards over whitespace, to the beginning of the first empty line.
  20833. Returns the number of empty lines passed."
  20834. (let ((pos (point)))
  20835. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20836. (skip-chars-backward " \t\n\r")
  20837. (unless (eobp)
  20838. (forward-line -1)))
  20839. (beginning-of-line 2)
  20840. (goto-char (min (point) pos))
  20841. (count-lines (point) pos)))
  20842. (defun org-skip-whitespace ()
  20843. (skip-chars-forward " \t\n\r"))
  20844. (defun org-point-in-group (point group &optional context)
  20845. "Check if POINT is in match-group GROUP.
  20846. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20847. match. If the match group does not exist or point is not inside it,
  20848. return nil."
  20849. (and (match-beginning group)
  20850. (>= point (match-beginning group))
  20851. (<= point (match-end group))
  20852. (if context
  20853. (list context (match-beginning group) (match-end group))
  20854. t)))
  20855. (defun org-switch-to-buffer-other-window (&rest args)
  20856. "Switch to buffer in a second window on the current frame.
  20857. In particular, do not allow pop-up frames.
  20858. Returns the newly created buffer."
  20859. (org-no-popups
  20860. (apply 'switch-to-buffer-other-window args)))
  20861. (defun org-combine-plists (&rest plists)
  20862. "Create a single property list from all plists in PLISTS.
  20863. The process starts by copying the first list, and then setting properties
  20864. from the other lists. Settings in the last list are the most significant
  20865. ones and overrule settings in the other lists."
  20866. (let ((rtn (copy-sequence (pop plists)))
  20867. p v ls)
  20868. (while plists
  20869. (setq ls (pop plists))
  20870. (while ls
  20871. (setq p (pop ls) v (pop ls))
  20872. (setq rtn (plist-put rtn p v))))
  20873. rtn))
  20874. (defun org-replace-escapes (string table)
  20875. "Replace %-escapes in STRING with values in TABLE.
  20876. TABLE is an association list with keys like \"%a\" and string values.
  20877. The sequences in STRING may contain normal field width and padding information,
  20878. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20879. so values can contain further %-escapes if they are define later in TABLE."
  20880. (let ((tbl (copy-alist table))
  20881. (case-fold-search nil)
  20882. (pchg 0)
  20883. e re rpl)
  20884. (dolist (e tbl)
  20885. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20886. (when (and (cdr e) (string-match re (cdr e)))
  20887. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20888. (safe "SREF"))
  20889. (add-text-properties 0 3 (list 'sref sref) safe)
  20890. (setcdr e (replace-match safe t t (cdr e)))))
  20891. (while (string-match re string)
  20892. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20893. (cdr e)))
  20894. (setq string (replace-match rpl t t string))))
  20895. (while (setq pchg (next-property-change pchg string))
  20896. (let ((sref (get-text-property pchg 'sref string)))
  20897. (when (and sref (string-match "SREF" string pchg))
  20898. (setq string (replace-match sref t t string)))))
  20899. string))
  20900. (defun org-sublist (list start end)
  20901. "Return a section of LIST, from START to END.
  20902. Counting starts at 1."
  20903. (let (rtn (c start))
  20904. (setq list (nthcdr (1- start) list))
  20905. (while (and list (<= c end))
  20906. (push (pop list) rtn)
  20907. (setq c (1+ c)))
  20908. (nreverse rtn)))
  20909. (defun org-find-base-buffer-visiting (file)
  20910. "Like `find-buffer-visiting' but always return the base buffer and
  20911. not an indirect buffer."
  20912. (let ((buf (or (get-file-buffer file)
  20913. (find-buffer-visiting file))))
  20914. (if buf
  20915. (or (buffer-base-buffer buf) buf)
  20916. nil)))
  20917. (defun org-image-file-name-regexp (&optional extensions)
  20918. "Return regexp matching the file names of images.
  20919. If EXTENSIONS is given, only match these."
  20920. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20921. (image-file-name-regexp)
  20922. (let ((image-file-name-extensions
  20923. (or extensions
  20924. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20925. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20926. (concat "\\."
  20927. (regexp-opt (nconc (mapcar 'upcase
  20928. image-file-name-extensions)
  20929. image-file-name-extensions)
  20930. t)
  20931. "\\'"))))
  20932. (defun org-file-image-p (file &optional extensions)
  20933. "Return non-nil if FILE is an image."
  20934. (save-match-data
  20935. (string-match (org-image-file-name-regexp extensions) file)))
  20936. (defun org-get-cursor-date (&optional with-time)
  20937. "Return the date at cursor in as a time.
  20938. This works in the calendar and in the agenda, anywhere else it just
  20939. returns the current time.
  20940. If WITH-TIME is non-nil, returns the time of the event at point (in
  20941. the agenda) or the current time of the day."
  20942. (let (date day defd tp hod mod)
  20943. (when with-time
  20944. (setq tp (get-text-property (point) 'time))
  20945. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20946. (setq hod (string-to-number (match-string 1 tp))
  20947. mod (string-to-number (match-string 2 tp))))
  20948. (or tp (let ((now (decode-time)))
  20949. (setq hod (nth 2 now)
  20950. mod (nth 1 now)))))
  20951. (cond
  20952. ((eq major-mode 'calendar-mode)
  20953. (setq date (calendar-cursor-to-date)
  20954. defd (encode-time 0 (or mod 0) (or hod 0)
  20955. (nth 1 date) (nth 0 date) (nth 2 date))))
  20956. ((eq major-mode 'org-agenda-mode)
  20957. (setq day (get-text-property (point) 'day))
  20958. (if day
  20959. (setq date (calendar-gregorian-from-absolute day)
  20960. defd (encode-time 0 (or mod 0) (or hod 0)
  20961. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20962. (or defd (current-time))))
  20963. (defun org-mark-subtree (&optional up)
  20964. "Mark the current subtree.
  20965. This puts point at the start of the current subtree, and mark at
  20966. the end. If a numeric prefix UP is given, move up into the
  20967. hierarchy of headlines by UP levels before marking the subtree."
  20968. (interactive "P")
  20969. (org-with-limited-levels
  20970. (cond ((org-at-heading-p) (beginning-of-line))
  20971. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20972. (t (outline-previous-visible-heading 1))))
  20973. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20974. (if (org-called-interactively-p 'any)
  20975. (call-interactively 'org-mark-element)
  20976. (org-mark-element)))
  20977. ;;; Indentation
  20978. (defun org--get-expected-indentation (element contentsp)
  20979. "Expected indentation column for current line, according to ELEMENT.
  20980. ELEMENT is an element containing point. CONTENTSP is non-nil
  20981. when indentation is to be computed according to contents of
  20982. ELEMENT."
  20983. (let ((type (org-element-type element))
  20984. (start (org-element-property :begin element))
  20985. (post-affiliated (org-element-property :post-affiliated element)))
  20986. (org-with-wide-buffer
  20987. (cond
  20988. (contentsp
  20989. (case type
  20990. ((diary-sexp footnote-definition) 0)
  20991. ((headline inlinetask nil)
  20992. (if (not org-adapt-indentation) 0
  20993. (let ((level (org-current-level)))
  20994. (if level (1+ level) 0))))
  20995. ((item plain-list) (org-list-item-body-column post-affiliated))
  20996. (t
  20997. (goto-char start)
  20998. (org-get-indentation))))
  20999. ((memq type '(headline inlinetask nil))
  21000. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21001. (org--get-expected-indentation element t)
  21002. 0))
  21003. ((memq type '(diary-sexp footnote-definition)) 0)
  21004. ;; First paragraph of a footnote definition or an item.
  21005. ;; Indent like parent.
  21006. ((< (line-beginning-position) start)
  21007. (org--get-expected-indentation
  21008. (org-element-property :parent element) t))
  21009. ;; At first line: indent according to previous sibling, if any,
  21010. ;; ignoring footnote definitions and inline tasks, or parent's
  21011. ;; contents.
  21012. ((= (line-beginning-position) start)
  21013. (catch 'exit
  21014. (while t
  21015. (if (= (point-min) start) (throw 'exit 0)
  21016. (goto-char (1- start))
  21017. (let* ((previous (org-element-at-point))
  21018. (parent previous))
  21019. (while (and parent (<= (org-element-property :end parent) start))
  21020. (setq previous parent
  21021. parent (org-element-property :parent parent)))
  21022. (cond
  21023. ((not previous) (throw 'exit 0))
  21024. ((> (org-element-property :end previous) start)
  21025. (throw 'exit (org--get-expected-indentation previous t)))
  21026. ((memq (org-element-type previous)
  21027. '(footnote-definition inlinetask))
  21028. (setq start (org-element-property :begin previous)))
  21029. (t (goto-char (org-element-property :begin previous))
  21030. (throw 'exit
  21031. (if (bolp) (org-get-indentation)
  21032. ;; At first paragraph in an item or
  21033. ;; a footnote definition.
  21034. (org--get-expected-indentation
  21035. (org-element-property :parent previous) t))))))))))
  21036. ;; Otherwise, move to the first non-blank line above.
  21037. (t
  21038. (beginning-of-line)
  21039. (let ((pos (point)))
  21040. (skip-chars-backward " \r\t\n")
  21041. (cond
  21042. ;; Two blank lines end a footnote definition or a plain
  21043. ;; list. When we indent an empty line after them, the
  21044. ;; containing list or footnote definition is over, so it
  21045. ;; qualifies as a previous sibling. Therefore, we indent
  21046. ;; like its first line.
  21047. ((and (memq type '(footnote-definition plain-list))
  21048. (> (count-lines (point) pos) 2))
  21049. (goto-char start)
  21050. (org-get-indentation))
  21051. ;; Line above is the first one of a paragraph at the
  21052. ;; beginning of an item or a footnote definition. Indent
  21053. ;; like parent.
  21054. ((< (line-beginning-position) start)
  21055. (org--get-expected-indentation
  21056. (org-element-property :parent element) t))
  21057. ;; Line above is the beginning of an element, i.e., point
  21058. ;; was originally on the blank lines between element's start
  21059. ;; and contents.
  21060. ((= (line-beginning-position) post-affiliated)
  21061. (org--get-expected-indentation element t))
  21062. ;; POS is after contents in a greater element. Indent like
  21063. ;; the beginning of the element.
  21064. ;;
  21065. ;; As a special case, if point is at the end of a footnote
  21066. ;; definition or an item, indent like the very last element
  21067. ;; within.
  21068. ((and (not (eq type 'paragraph))
  21069. (let ((cend (org-element-property :contents-end element)))
  21070. (and cend (<= cend pos))))
  21071. (if (memq type '(footnote-definition item plain-list))
  21072. (let ((last (org-element-at-point)))
  21073. (org--get-expected-indentation
  21074. last
  21075. (memq (org-element-type last)
  21076. '(footnote-definition item plain-list))))
  21077. (goto-char start)
  21078. (org-get-indentation)))
  21079. ;; In any other case, indent like the current line.
  21080. (t (org-get-indentation)))))))))
  21081. (defun org--align-node-property ()
  21082. "Align node property at point.
  21083. Alignment is done according to `org-property-format', which see."
  21084. (when (save-excursion
  21085. (beginning-of-line)
  21086. (looking-at org-property-re))
  21087. (replace-match
  21088. (concat (match-string 4)
  21089. (org-trim
  21090. (format org-property-format (match-string 1) (match-string 3))))
  21091. t t)))
  21092. (defun org-indent-line ()
  21093. "Indent line depending on context.
  21094. Indentation is done according to the following rules:
  21095. - Footnote definitions, diary sexps, headlines and inline tasks
  21096. have to start at column 0.
  21097. - On the very first line of an element, consider, in order, the
  21098. next rules until one matches:
  21099. 1. If there's a sibling element before, ignoring footnote
  21100. definitions and inline tasks, indent like its first line.
  21101. 2. If element has a parent, indent like its contents. More
  21102. precisely, if parent is an item, indent after the
  21103. description part, if any, or the bullet (see
  21104. `org-list-description-max-indent'). Else, indent like
  21105. parent's first line.
  21106. 3. Otherwise, indent relatively to current level, if
  21107. `org-adapt-indentation' is non-nil, or to left margin.
  21108. - On a blank line at the end of an element, indent according to
  21109. the type of the element. More precisely
  21110. 1. If element is a plain list, an item, or a footnote
  21111. definition, indent like the very last element within.
  21112. 2. If element is a paragraph, indent like its last non blank
  21113. line.
  21114. 3. Otherwise, indent like its very first line.
  21115. - In the code part of a source block, use language major mode
  21116. to indent current line if `org-src-tab-acts-natively' is
  21117. non-nil. If it is nil, do nothing.
  21118. - Otherwise, indent like the first non-blank line above.
  21119. The function doesn't indent an item as it could break the whole
  21120. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21121. \\[org-shiftmetaright].
  21122. Also align node properties according to `org-property-format'."
  21123. (interactive)
  21124. (cond
  21125. (orgstruct-is-++
  21126. (let ((indent-line-function
  21127. (cadadr (assq 'indent-line-function org-fb-vars))))
  21128. (indent-according-to-mode)))
  21129. ((org-at-heading-p) 'noindent)
  21130. (t
  21131. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21132. (type (org-element-type element)))
  21133. (cond ((and (memq type '(plain-list item))
  21134. (= (line-beginning-position)
  21135. (org-element-property :post-affiliated element)))
  21136. 'noindent)
  21137. ((and (eq type 'src-block)
  21138. org-src-tab-acts-natively
  21139. (> (line-beginning-position)
  21140. (org-element-property :post-affiliated element))
  21141. (< (line-beginning-position)
  21142. (org-with-wide-buffer
  21143. (goto-char (org-element-property :end element))
  21144. (skip-chars-backward " \r\t\n")
  21145. (line-beginning-position))))
  21146. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21147. (t
  21148. (let ((column (org--get-expected-indentation element nil)))
  21149. ;; Preserve current column.
  21150. (if (<= (current-column) (current-indentation))
  21151. (org-indent-line-to column)
  21152. (save-excursion (org-indent-line-to column))))
  21153. ;; Align node property. Also preserve current column.
  21154. (when (eq type 'node-property)
  21155. (let ((column (current-column)))
  21156. (org--align-node-property)
  21157. (org-move-to-column column)))))))))
  21158. (defun org-indent-region (start end)
  21159. "Indent each non-blank line in the region.
  21160. Called from a program, START and END specify the region to
  21161. indent. The function will not indent contents of example blocks,
  21162. verse blocks and export blocks as leading white spaces are
  21163. assumed to be significant there."
  21164. (interactive "r")
  21165. (save-excursion
  21166. (goto-char start)
  21167. (skip-chars-forward " \r\t\n")
  21168. (unless (eobp) (beginning-of-line))
  21169. (let ((indent-to
  21170. (lambda (ind pos)
  21171. ;; Set IND as indentation for all lines between point and
  21172. ;; POS or END, whichever comes first. Blank lines are
  21173. ;; ignored. Leave point after POS once done.
  21174. (let ((limit (copy-marker (min end pos))))
  21175. (while (< (point) limit)
  21176. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21177. (forward-line))
  21178. (set-marker limit nil))))
  21179. (end (copy-marker end)))
  21180. (while (< (point) end)
  21181. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21182. (let* ((element (org-element-at-point))
  21183. (type (org-element-type element))
  21184. (element-end (copy-marker (org-element-property :end element)))
  21185. (ind (org--get-expected-indentation element nil)))
  21186. (cond
  21187. ((or (memq type '(paragraph table table-row))
  21188. (not (or (org-element-property :contents-begin element)
  21189. (memq type
  21190. '(example-block export-block src-block)))))
  21191. ;; Elements here are indented as a single block. Also
  21192. ;; align node properties.
  21193. (when (eq type 'node-property)
  21194. (org--align-node-property)
  21195. (beginning-of-line))
  21196. (funcall indent-to ind element-end))
  21197. (t
  21198. ;; Elements in this category consist of three parts:
  21199. ;; before the contents, the contents, and after the
  21200. ;; contents. The contents are treated specially,
  21201. ;; according to the element type, or not indented at
  21202. ;; all. Other parts are indented as a single block.
  21203. (let* ((post (copy-marker
  21204. (org-element-property :post-affiliated element)))
  21205. (cbeg
  21206. (copy-marker
  21207. (cond
  21208. ((not (org-element-property :contents-begin element))
  21209. ;; Fake contents for source blocks.
  21210. (org-with-wide-buffer
  21211. (goto-char post)
  21212. (forward-line)
  21213. (point)))
  21214. ((memq type '(footnote-definition item plain-list))
  21215. ;; Contents in these elements could start on
  21216. ;; the same line as the beginning of the
  21217. ;; element. Make sure we start indenting
  21218. ;; from the second line.
  21219. (org-with-wide-buffer
  21220. (goto-char post)
  21221. (end-of-line)
  21222. (skip-chars-forward " \r\t\n")
  21223. (if (eobp) (point) (line-beginning-position))))
  21224. (t (org-element-property :contents-begin element)))))
  21225. (cend (copy-marker
  21226. (or (org-element-property :contents-end element)
  21227. ;; Fake contents for source blocks.
  21228. (org-with-wide-buffer
  21229. (goto-char element-end)
  21230. (skip-chars-backward " \r\t\n")
  21231. (line-beginning-position)))
  21232. t)))
  21233. ;; Do not change items indentation individually as it
  21234. ;; might break the list as a whole. On the other
  21235. ;; hand, when at a plain list, indent it as a whole.
  21236. (cond ((eq type 'plain-list)
  21237. (let ((offset (- ind (org-get-indentation))))
  21238. (unless (zerop offset)
  21239. (indent-rigidly (org-element-property :begin element)
  21240. (org-element-property :end element)
  21241. offset))
  21242. (goto-char cbeg)))
  21243. ((eq type 'item) (goto-char cbeg))
  21244. (t (funcall indent-to ind cbeg)))
  21245. (when (< (point) end)
  21246. (case type
  21247. ((example-block export-block verse-block))
  21248. (src-block
  21249. ;; In a source block, indent source code
  21250. ;; according to language major mode, but only if
  21251. ;; `org-src-tab-acts-natively' is non-nil.
  21252. (when (and (< (point) end) org-src-tab-acts-natively)
  21253. (ignore-errors
  21254. (org-babel-do-in-edit-buffer
  21255. (indent-region (point-min) (point-max))))))
  21256. (t (org-indent-region (point) (min cend end))))
  21257. (goto-char (min cend end))
  21258. (when (< (point) end) (funcall indent-to ind element-end)))
  21259. (set-marker post nil)
  21260. (set-marker cbeg nil)
  21261. (set-marker cend nil))))
  21262. (set-marker element-end nil))))
  21263. (set-marker end nil))))
  21264. (defun org-indent-drawer ()
  21265. "Indent the drawer at point."
  21266. (interactive)
  21267. (unless (save-excursion
  21268. (beginning-of-line)
  21269. (org-looking-at-p org-drawer-regexp))
  21270. (user-error "Not at a drawer"))
  21271. (let ((element (org-element-at-point)))
  21272. (unless (memq (org-element-type element) '(drawer property-drawer))
  21273. (user-error "Not at a drawer"))
  21274. (org-with-wide-buffer
  21275. (org-indent-region (org-element-property :begin element)
  21276. (org-element-property :end element))))
  21277. (message "Drawer at point indented"))
  21278. (defun org-indent-block ()
  21279. "Indent the block at point."
  21280. (interactive)
  21281. (unless (save-excursion
  21282. (beginning-of-line)
  21283. (let ((case-fold-search t))
  21284. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21285. (user-error "Not at a block"))
  21286. (let ((element (org-element-at-point)))
  21287. (unless (memq (org-element-type element)
  21288. '(comment-block center-block dynamic-block example-block
  21289. export-block quote-block special-block
  21290. src-block verse-block))
  21291. (user-error "Not at a block"))
  21292. (org-with-wide-buffer
  21293. (org-indent-region (org-element-property :begin element)
  21294. (org-element-property :end element))))
  21295. (message "Block at point indented"))
  21296. ;;; Filling
  21297. ;; We use our own fill-paragraph and auto-fill functions.
  21298. ;; `org-fill-paragraph' relies on adaptive filling and context
  21299. ;; checking. Appropriate `fill-prefix' is computed with
  21300. ;; `org-adaptive-fill-function'.
  21301. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21302. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21303. ;; `org-adaptive-fill-function' value. Internally,
  21304. ;; `org-comment-line-break-function' breaks the line.
  21305. ;; `org-setup-filling' installs filling and auto-filling related
  21306. ;; variables during `org-mode' initialization.
  21307. (defvar org-element-paragraph-separate) ; org-element.el
  21308. (defun org-setup-filling ()
  21309. (require 'org-element)
  21310. ;; Prevent auto-fill from inserting unwanted new items.
  21311. (when (boundp 'fill-nobreak-predicate)
  21312. (org-set-local
  21313. 'fill-nobreak-predicate
  21314. (org-uniquify
  21315. (append fill-nobreak-predicate
  21316. '(org-fill-line-break-nobreak-p
  21317. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21318. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21319. (org-set-local 'paragraph-start paragraph-ending)
  21320. (org-set-local 'paragraph-separate paragraph-ending))
  21321. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21322. (org-set-local 'auto-fill-inhibit-regexp nil)
  21323. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21324. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21325. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21326. (defun org-fill-line-break-nobreak-p ()
  21327. "Non-nil when a new line at point would create an Org line break."
  21328. (save-excursion
  21329. (skip-chars-backward "[ \t]")
  21330. (skip-chars-backward "\\\\")
  21331. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21332. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21333. "Non-nil when a new line at point would split a timestamp."
  21334. (and (org-at-timestamp-p t)
  21335. (not (looking-at org-ts-regexp-both))))
  21336. (declare-function message-in-body-p "message" ())
  21337. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21338. (defun org-adaptive-fill-function ()
  21339. "Compute a fill prefix for the current line.
  21340. Return fill prefix, as a string, or nil if current line isn't
  21341. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21342. matches in paragraphs or comments, use it."
  21343. (catch 'exit
  21344. (when (derived-mode-p 'message-mode)
  21345. (save-excursion
  21346. (beginning-of-line)
  21347. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21348. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21349. ((looking-at message-cite-prefix-regexp)
  21350. (throw 'exit (match-string-no-properties 0)))
  21351. ((looking-at org-outline-regexp)
  21352. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21353. (org-with-wide-buffer
  21354. (unless (org-at-heading-p)
  21355. (let* ((p (line-beginning-position))
  21356. (element (save-excursion
  21357. (beginning-of-line)
  21358. (org-element-at-point)))
  21359. (type (org-element-type element))
  21360. (post-affiliated (org-element-property :post-affiliated element)))
  21361. (unless (< p post-affiliated)
  21362. (case type
  21363. (comment
  21364. (save-excursion
  21365. (beginning-of-line)
  21366. (looking-at "[ \t]*")
  21367. (concat (match-string 0) "# ")))
  21368. (footnote-definition "")
  21369. ((item plain-list)
  21370. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21371. (paragraph
  21372. ;; Fill prefix is usually the same as the current line,
  21373. ;; unless the paragraph is at the beginning of an item.
  21374. (let ((parent (org-element-property :parent element)))
  21375. (save-excursion
  21376. (beginning-of-line)
  21377. (cond ((eq (org-element-type parent) 'item)
  21378. (make-string (org-list-item-body-column
  21379. (org-element-property :begin parent))
  21380. ?\s))
  21381. ((and adaptive-fill-regexp
  21382. ;; Locally disable
  21383. ;; `adaptive-fill-function' to let
  21384. ;; `fill-context-prefix' handle
  21385. ;; `adaptive-fill-regexp' variable.
  21386. (let (adaptive-fill-function)
  21387. (fill-context-prefix
  21388. post-affiliated
  21389. (org-element-property :end element)))))
  21390. ((looking-at "[ \t]+") (match-string 0))
  21391. (t "")))))
  21392. (comment-block
  21393. ;; Only fill contents if P is within block boundaries.
  21394. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21395. (forward-line)
  21396. (point)))
  21397. (cend (save-excursion
  21398. (goto-char (org-element-property :end element))
  21399. (skip-chars-backward " \r\t\n")
  21400. (line-beginning-position))))
  21401. (when (and (>= p cbeg) (< p cend))
  21402. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21403. (match-string 0)
  21404. "")))))))))))
  21405. (declare-function message-goto-body "message" ())
  21406. (defvar message-cite-prefix-regexp) ; From message.el
  21407. (defun org-fill-paragraph (&optional justify)
  21408. "Fill element at point, when applicable.
  21409. This function only applies to comment blocks, comments, example
  21410. blocks and paragraphs. Also, as a special case, re-align table
  21411. when point is at one.
  21412. If JUSTIFY is non-nil (interactively, with prefix argument),
  21413. justify as well. If `sentence-end-double-space' is non-nil, then
  21414. period followed by one space does not end a sentence, so don't
  21415. break a line there. The variable `fill-column' controls the
  21416. width for filling.
  21417. For convenience, when point is at a plain list, an item or
  21418. a footnote definition, try to fill the first paragraph within."
  21419. (interactive)
  21420. (if (and (derived-mode-p 'message-mode)
  21421. (or (not (message-in-body-p))
  21422. (save-excursion (move-beginning-of-line 1)
  21423. (looking-at message-cite-prefix-regexp))))
  21424. ;; First ensure filling is correct in message-mode.
  21425. (let ((fill-paragraph-function
  21426. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21427. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21428. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21429. (paragraph-separate
  21430. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21431. (fill-paragraph nil))
  21432. (with-syntax-table org-mode-transpose-word-syntax-table
  21433. ;; Move to end of line in order to get the first paragraph
  21434. ;; within a plain list or a footnote definition.
  21435. (let ((element (save-excursion
  21436. (end-of-line)
  21437. (or (ignore-errors (org-element-at-point))
  21438. (user-error "An element cannot be parsed line %d"
  21439. (line-number-at-pos (point)))))))
  21440. ;; First check if point is in a blank line at the beginning of
  21441. ;; the buffer. In that case, ignore filling.
  21442. (case (org-element-type element)
  21443. ;; Use major mode filling function is src blocks.
  21444. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21445. ;; Align Org tables, leave table.el tables as-is.
  21446. (table-row (org-table-align) t)
  21447. (table
  21448. (when (eq (org-element-property :type element) 'org)
  21449. (save-excursion
  21450. (goto-char (org-element-property :post-affiliated element))
  21451. (org-table-align)))
  21452. t)
  21453. (paragraph
  21454. ;; Paragraphs may contain `line-break' type objects.
  21455. (let ((beg (max (point-min)
  21456. (org-element-property :contents-begin element)))
  21457. (end (min (point-max)
  21458. (org-element-property :contents-end element))))
  21459. ;; Do nothing if point is at an affiliated keyword.
  21460. (if (< (line-end-position) beg) t
  21461. (when (derived-mode-p 'message-mode)
  21462. ;; In `message-mode', do not fill following citation
  21463. ;; in current paragraph nor text before message body.
  21464. (let ((body-start (save-excursion (message-goto-body))))
  21465. (when body-start (setq beg (max body-start beg))))
  21466. (when (save-excursion
  21467. (re-search-forward
  21468. (concat "^" message-cite-prefix-regexp) end t))
  21469. (setq end (match-beginning 0))))
  21470. ;; Fill paragraph, taking line breaks into account.
  21471. (save-excursion
  21472. (goto-char beg)
  21473. (let ((cuts (list beg)))
  21474. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21475. (when (eq 'line-break
  21476. (org-element-type
  21477. (save-excursion (backward-char)
  21478. (org-element-context))))
  21479. (push (point) cuts)))
  21480. (dolist (c (delq end cuts))
  21481. (fill-region-as-paragraph c end justify)
  21482. (setq end c))))
  21483. t)))
  21484. ;; Contents of `comment-block' type elements should be
  21485. ;; filled as plain text, but only if point is within block
  21486. ;; markers.
  21487. (comment-block
  21488. (let* ((case-fold-search t)
  21489. (beg (save-excursion
  21490. (goto-char (org-element-property :begin element))
  21491. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21492. (forward-line)
  21493. (point)))
  21494. (end (save-excursion
  21495. (goto-char (org-element-property :end element))
  21496. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21497. (line-beginning-position))))
  21498. (if (or (< (point) beg) (> (point) end)) t
  21499. (fill-region-as-paragraph
  21500. (save-excursion (end-of-line)
  21501. (re-search-backward "^[ \t]*$" beg 'move)
  21502. (line-beginning-position))
  21503. (save-excursion (beginning-of-line)
  21504. (re-search-forward "^[ \t]*$" end 'move)
  21505. (line-beginning-position))
  21506. justify))))
  21507. ;; Fill comments.
  21508. (comment
  21509. (let ((begin (org-element-property :post-affiliated element))
  21510. (end (org-element-property :end element)))
  21511. (when (and (>= (point) begin) (<= (point) end))
  21512. (let ((begin (save-excursion
  21513. (end-of-line)
  21514. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21515. (progn (forward-line) (point))
  21516. begin)))
  21517. (end (save-excursion
  21518. (end-of-line)
  21519. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21520. (1- (line-beginning-position))
  21521. (skip-chars-backward " \r\t\n")
  21522. (line-end-position)))))
  21523. ;; Do not fill comments when at a blank line.
  21524. (when (> end begin)
  21525. (let ((fill-prefix
  21526. (save-excursion
  21527. (beginning-of-line)
  21528. (looking-at "[ \t]*#")
  21529. (let ((comment-prefix (match-string 0)))
  21530. (goto-char (match-end 0))
  21531. (if (looking-at adaptive-fill-regexp)
  21532. (concat comment-prefix (match-string 0))
  21533. (concat comment-prefix " "))))))
  21534. (save-excursion
  21535. (fill-region-as-paragraph begin end justify))))))
  21536. t))
  21537. ;; Ignore every other element.
  21538. (otherwise t))))))
  21539. (defun org-auto-fill-function ()
  21540. "Auto-fill function."
  21541. ;; Check if auto-filling is meaningful.
  21542. (let ((fc (current-fill-column)))
  21543. (when (and fc (> (current-column) fc))
  21544. (let* ((fill-prefix (org-adaptive-fill-function))
  21545. ;; Enforce empty fill prefix, if required. Otherwise, it
  21546. ;; will be computed again.
  21547. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21548. (when fill-prefix (do-auto-fill))))))
  21549. (defun org-comment-line-break-function (&optional soft)
  21550. "Break line at point and indent, continuing comment if within one.
  21551. The inserted newline is marked hard if variable
  21552. `use-hard-newlines' is true, unless optional argument SOFT is
  21553. non-nil."
  21554. (if soft (insert-and-inherit ?\n) (newline 1))
  21555. (save-excursion (forward-char -1) (delete-horizontal-space))
  21556. (delete-horizontal-space)
  21557. (indent-to-left-margin)
  21558. (insert-before-markers-and-inherit fill-prefix))
  21559. ;;; Fixed Width Areas
  21560. (defun org-toggle-fixed-width ()
  21561. "Toggle fixed-width markup.
  21562. Add or remove fixed-width markup on current line, whenever it
  21563. makes sense. Return an error otherwise.
  21564. If a region is active and if it contains only fixed-width areas
  21565. or blank lines, remove all fixed-width markup in it. If the
  21566. region contains anything else, convert all non-fixed-width lines
  21567. to fixed-width ones.
  21568. Blank lines at the end of the region are ignored unless the
  21569. region only contains such lines."
  21570. (interactive)
  21571. (if (not (org-region-active-p))
  21572. ;; No region:
  21573. ;;
  21574. ;; Remove fixed width marker only in a fixed-with element.
  21575. ;;
  21576. ;; Add fixed width maker in paragraphs, in blank lines after
  21577. ;; elements or at the beginning of a headline or an inlinetask,
  21578. ;; and before any one-line elements (e.g., a clock).
  21579. (progn
  21580. (beginning-of-line)
  21581. (let* ((element (org-element-at-point))
  21582. (type (org-element-type element)))
  21583. (cond
  21584. ((and (eq type 'fixed-width)
  21585. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21586. (replace-match
  21587. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21588. ((and (memq type '(babel-call clock comment diary-sexp headline
  21589. horizontal-rule keyword paragraph
  21590. planning))
  21591. (<= (org-element-property :post-affiliated element) (point)))
  21592. (skip-chars-forward " \t")
  21593. (insert ": "))
  21594. ((and (org-looking-at-p "[ \t]*$")
  21595. (or (eq type 'inlinetask)
  21596. (save-excursion
  21597. (skip-chars-forward " \r\t\n")
  21598. (<= (org-element-property :end element) (point)))))
  21599. (delete-region (point) (line-end-position))
  21600. (org-indent-line)
  21601. (insert ": "))
  21602. (t (user-error "Cannot insert a fixed-width line here")))))
  21603. ;; Region active.
  21604. (let* ((begin (save-excursion
  21605. (goto-char (region-beginning))
  21606. (line-beginning-position)))
  21607. (end (copy-marker
  21608. (save-excursion
  21609. (goto-char (region-end))
  21610. (unless (eolp) (beginning-of-line))
  21611. (if (save-excursion (re-search-backward "\\S-" begin t))
  21612. (progn (skip-chars-backward " \r\t\n") (point))
  21613. (point)))))
  21614. (all-fixed-width-p
  21615. (catch 'not-all-p
  21616. (save-excursion
  21617. (goto-char begin)
  21618. (skip-chars-forward " \r\t\n")
  21619. (when (eobp) (throw 'not-all-p nil))
  21620. (while (< (point) end)
  21621. (let ((element (org-element-at-point)))
  21622. (if (eq (org-element-type element) 'fixed-width)
  21623. (goto-char (org-element-property :end element))
  21624. (throw 'not-all-p nil))))
  21625. t))))
  21626. (if all-fixed-width-p
  21627. (save-excursion
  21628. (goto-char begin)
  21629. (while (< (point) end)
  21630. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21631. (replace-match
  21632. "" nil nil nil
  21633. (if (= (line-end-position) (match-end 0)) 0 1)))
  21634. (forward-line)))
  21635. (let ((min-ind (point-max)))
  21636. ;; Find minimum indentation across all lines.
  21637. (save-excursion
  21638. (goto-char begin)
  21639. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21640. (setq min-ind 0)
  21641. (catch 'zerop
  21642. (while (< (point) end)
  21643. (unless (org-looking-at-p "[ \t]*$")
  21644. (let ((ind (org-get-indentation)))
  21645. (setq min-ind (min min-ind ind))
  21646. (when (zerop ind) (throw 'zerop t))))
  21647. (forward-line)))))
  21648. ;; Loop over all lines and add fixed-width markup everywhere
  21649. ;; but in fixed-width lines.
  21650. (save-excursion
  21651. (goto-char begin)
  21652. (while (< (point) end)
  21653. (cond
  21654. ((org-at-heading-p)
  21655. (insert ": ")
  21656. (forward-line)
  21657. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21658. (insert ":")
  21659. (forward-line)))
  21660. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21661. (let* ((element (org-element-at-point))
  21662. (element-end (org-element-property :end element)))
  21663. (if (eq (org-element-type element) 'fixed-width)
  21664. (progn (goto-char element-end)
  21665. (skip-chars-backward " \r\t\n")
  21666. (forward-line))
  21667. (let ((limit (min end element-end)))
  21668. (while (< (point) limit)
  21669. (org-move-to-column min-ind t)
  21670. (insert ": ")
  21671. (forward-line))))))
  21672. (t
  21673. (org-move-to-column min-ind t)
  21674. (insert ": ")
  21675. (forward-line)))))))
  21676. (set-marker end nil))))
  21677. ;;; Comments
  21678. ;; Org comments syntax is quite complex. It requires the entire line
  21679. ;; to be just a comment. Also, even with the right syntax at the
  21680. ;; beginning of line, some some elements (i.e. verse-block or
  21681. ;; example-block) don't accept comments. Usual Emacs comment commands
  21682. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21683. ;; Org still relies on `comment-dwim', but cannot trust
  21684. ;; `comment-only-p'. So, `comment-region-function' and
  21685. ;; `uncomment-region-function' both point
  21686. ;; to`org-comment-or-uncomment-region'. Eventually,
  21687. ;; `org-insert-comment' takes care of insertion of comments at the
  21688. ;; beginning of line.
  21689. ;; `org-setup-comments-handling' install comments related variables
  21690. ;; during `org-mode' initialization.
  21691. (defun org-setup-comments-handling ()
  21692. (interactive)
  21693. (org-set-local 'comment-use-syntax nil)
  21694. (org-set-local 'comment-start "# ")
  21695. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21696. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21697. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21698. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21699. (defun org-insert-comment ()
  21700. "Insert an empty comment above current line.
  21701. If the line is empty, insert comment at its beginning. When
  21702. point is within a source block, comment according to the related
  21703. major mode."
  21704. (if (let ((element (org-element-at-point)))
  21705. (and (eq (org-element-type element) 'src-block)
  21706. (< (save-excursion
  21707. (goto-char (org-element-property :post-affiliated element))
  21708. (line-end-position))
  21709. (point))
  21710. (> (save-excursion
  21711. (goto-char (org-element-property :end element))
  21712. (skip-chars-backward " \r\t\n")
  21713. (line-beginning-position))
  21714. (point))))
  21715. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21716. (beginning-of-line)
  21717. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21718. (open-line 1))
  21719. (org-indent-line)
  21720. (insert "# ")))
  21721. (defvar comment-empty-lines) ; From newcomment.el.
  21722. (defun org-comment-or-uncomment-region (beg end &rest _)
  21723. "Comment or uncomment each non-blank line in the region.
  21724. Uncomment each non-blank line between BEG and END if it only
  21725. contains commented lines. Otherwise, comment them. If region is
  21726. strictly within a source block, use appropriate comment syntax."
  21727. (if (let ((element (org-element-at-point)))
  21728. (and (eq (org-element-type element) 'src-block)
  21729. (< (save-excursion
  21730. (goto-char (org-element-property :post-affiliated element))
  21731. (line-end-position))
  21732. beg)
  21733. (>= (save-excursion
  21734. (goto-char (org-element-property :end element))
  21735. (skip-chars-backward " \r\t\n")
  21736. (line-beginning-position))
  21737. end)))
  21738. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21739. (save-restriction
  21740. ;; Restrict region
  21741. (narrow-to-region (save-excursion (goto-char beg)
  21742. (skip-chars-forward " \r\t\n" end)
  21743. (line-beginning-position))
  21744. (save-excursion (goto-char end)
  21745. (skip-chars-backward " \r\t\n" beg)
  21746. (line-end-position)))
  21747. (let ((uncommentp
  21748. ;; UNCOMMENTP is non-nil when every non blank line between
  21749. ;; BEG and END is a comment.
  21750. (save-excursion
  21751. (goto-char (point-min))
  21752. (while (and (not (eobp))
  21753. (let ((element (org-element-at-point)))
  21754. (and (eq (org-element-type element) 'comment)
  21755. (goto-char (min (point-max)
  21756. (org-element-property
  21757. :end element)))))))
  21758. (eobp))))
  21759. (if uncommentp
  21760. ;; Only blank lines and comments in region: uncomment it.
  21761. (save-excursion
  21762. (goto-char (point-min))
  21763. (while (not (eobp))
  21764. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21765. (replace-match "" nil nil nil 1))
  21766. (forward-line)))
  21767. ;; Comment each line in region.
  21768. (let ((min-indent (point-max)))
  21769. ;; First find the minimum indentation across all lines.
  21770. (save-excursion
  21771. (goto-char (point-min))
  21772. (while (and (not (eobp)) (not (zerop min-indent)))
  21773. (unless (looking-at "[ \t]*$")
  21774. (setq min-indent (min min-indent (current-indentation))))
  21775. (forward-line)))
  21776. ;; Then loop over all lines.
  21777. (save-excursion
  21778. (goto-char (point-min))
  21779. (while (not (eobp))
  21780. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21781. ;; Don't get fooled by invisible text (e.g. link path)
  21782. ;; when moving to column MIN-INDENT.
  21783. (let ((buffer-invisibility-spec nil))
  21784. (org-move-to-column min-indent t))
  21785. (insert comment-start))
  21786. (forward-line)))))))))
  21787. (defun org-comment-dwim (arg)
  21788. "Call `comment-dwim' within a source edit buffer if needed."
  21789. (interactive "*P")
  21790. (if (org-in-src-block-p)
  21791. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21792. (call-interactively 'comment-dwim)))
  21793. ;;; Timestamps API
  21794. ;; This section contains tools to operate on timestamp objects, as
  21795. ;; returned by, e.g. `org-element-context'.
  21796. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21797. "Encode TIMESTAMP object into Emacs internal time.
  21798. Use end of date range or time range when END is non-nil."
  21799. (apply #'encode-time
  21800. (cons 0
  21801. (mapcar
  21802. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21803. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21804. '(:minute-start :hour-start :day-start :month-start
  21805. :year-start))))))
  21806. (defun org-timestamp-has-time-p (timestamp)
  21807. "Non-nil when TIMESTAMP has a time specified."
  21808. (org-element-property :hour-start timestamp))
  21809. (defun org-timestamp-format (timestamp format &optional end utc)
  21810. "Format a TIMESTAMP object into a string.
  21811. FORMAT is a format specifier to be passed to
  21812. `format-time-string'.
  21813. When optional argument END is non-nil, use end of date-range or
  21814. time-range, if possible.
  21815. When optional argument UTC is non-nil, time will be expressed as
  21816. Universal Time."
  21817. (format-time-string
  21818. format (org-timestamp--to-internal-time timestamp end) utc))
  21819. (defun org-timestamp-split-range (timestamp &optional end)
  21820. "Extract a TIMESTAMP object from a date or time range.
  21821. END, when non-nil, means extract the end of the range.
  21822. Otherwise, extract its start.
  21823. Return a new timestamp object."
  21824. (let ((type (org-element-property :type timestamp)))
  21825. (if (memq type '(active inactive diary)) timestamp
  21826. (let ((split-ts (org-element-copy timestamp)))
  21827. ;; Set new type.
  21828. (org-element-put-property
  21829. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21830. ;; Copy start properties over end properties if END is
  21831. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21832. (let ((p-alist '((:minute-start . :minute-end)
  21833. (:hour-start . :hour-end)
  21834. (:day-start . :day-end)
  21835. (:month-start . :month-end)
  21836. (:year-start . :year-end))))
  21837. (dolist (p-cell p-alist)
  21838. (org-element-put-property
  21839. split-ts
  21840. (funcall (if end #'car #'cdr) p-cell)
  21841. (org-element-property
  21842. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21843. ;; Eventually refresh `:raw-value'.
  21844. (org-element-put-property split-ts :raw-value nil)
  21845. (org-element-put-property
  21846. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21847. (defun org-timestamp-translate (timestamp &optional boundary)
  21848. "Translate TIMESTAMP object to custom format.
  21849. Format string is defined in `org-time-stamp-custom-formats',
  21850. which see.
  21851. When optional argument BOUNDARY is non-nil, it is either the
  21852. symbol `start' or `end'. In this case, only translate the
  21853. starting or ending part of TIMESTAMP if it is a date or time
  21854. range. Otherwise, translate both parts.
  21855. Return timestamp as-is if `org-display-custom-times' is nil or if
  21856. it has a `diary' type."
  21857. (let ((type (org-element-property :type timestamp)))
  21858. (if (or (not org-display-custom-times) (eq type 'diary))
  21859. (org-element-interpret-data timestamp)
  21860. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21861. org-time-stamp-custom-formats)))
  21862. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21863. (concat (org-timestamp-format timestamp fmt)
  21864. "--"
  21865. (org-timestamp-format timestamp fmt t))
  21866. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21867. ;;; Other stuff.
  21868. (defvar reftex-docstruct-symbol)
  21869. (defvar reftex-cite-format)
  21870. (defvar org--rds)
  21871. (defun org-reftex-citation ()
  21872. "Use reftex-citation to insert a citation into the buffer.
  21873. This looks for a line like
  21874. #+BIBLIOGRAPHY: foo plain option:-d
  21875. and derives from it that foo.bib is the bibliography file relevant
  21876. for this document. It then installs the necessary environment for RefTeX
  21877. to work in this buffer and calls `reftex-citation' to insert a citation
  21878. into the buffer.
  21879. Export of such citations to both LaTeX and HTML is handled by the contributed
  21880. package ox-bibtex by Taru Karttunen."
  21881. (interactive)
  21882. (let ((reftex-docstruct-symbol 'org--rds)
  21883. (reftex-cite-format "\\cite{%l}")
  21884. org--rds bib)
  21885. (save-excursion
  21886. (save-restriction
  21887. (widen)
  21888. (let ((case-fold-search t)
  21889. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21890. (if (not (save-excursion
  21891. (or (re-search-forward re nil t)
  21892. (re-search-backward re nil t))))
  21893. (user-error "No bibliography defined in file")
  21894. (setq bib (concat (match-string 1) ".bib")
  21895. org--rds (list (list 'bib bib)))))))
  21896. (call-interactively 'reftex-citation)))
  21897. ;;;; Functions extending outline functionality
  21898. (defun org-beginning-of-line (&optional arg)
  21899. "Go to the beginning of the current line. If that is invisible, continue
  21900. to a visible line beginning. This makes the function of C-a more intuitive.
  21901. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21902. first attempt, and only move to after the tags when the cursor is already
  21903. beyond the end of the headline."
  21904. (interactive "P")
  21905. (let ((pos (point))
  21906. (special (if (consp org-special-ctrl-a/e)
  21907. (car org-special-ctrl-a/e)
  21908. org-special-ctrl-a/e))
  21909. deactivate-mark refpos)
  21910. (if (org-bound-and-true-p visual-line-mode)
  21911. (beginning-of-visual-line 1)
  21912. (beginning-of-line 1))
  21913. (if (and arg (fboundp 'move-beginning-of-line))
  21914. (call-interactively 'move-beginning-of-line)
  21915. (if (bobp)
  21916. nil
  21917. (backward-char 1)
  21918. (if (org-truely-invisible-p)
  21919. (while (and (not (bobp)) (org-truely-invisible-p))
  21920. (backward-char 1)
  21921. (beginning-of-line 1))
  21922. (forward-char 1))))
  21923. (when special
  21924. (cond
  21925. ((and (looking-at org-complex-heading-regexp)
  21926. (eq (char-after (match-end 1)) ?\s))
  21927. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21928. (point-at-eol)))
  21929. (goto-char
  21930. (if (eq special t)
  21931. (cond ((> pos refpos) refpos)
  21932. ((= pos (point)) refpos)
  21933. (t (point)))
  21934. (cond ((> pos (point)) (point))
  21935. ((not (eq last-command this-command)) (point))
  21936. (t refpos)))))
  21937. ((org-at-item-p)
  21938. ;; Being at an item and not looking at an the item means point
  21939. ;; was previously moved to beginning of a visual line, which
  21940. ;; doesn't contain the item. Therefore, do nothing special,
  21941. ;; just stay here.
  21942. (when (looking-at org-list-full-item-re)
  21943. ;; Set special position at first white space character after
  21944. ;; bullet, and check-box, if any.
  21945. (let ((after-bullet
  21946. (let ((box (match-end 3)))
  21947. (if (not box) (match-end 1)
  21948. (let ((after (char-after box)))
  21949. (if (and after (= after ? )) (1+ box) box))))))
  21950. ;; Special case: Move point to special position when
  21951. ;; currently after it or at beginning of line.
  21952. (if (eq special t)
  21953. (when (or (> pos after-bullet) (= (point) pos))
  21954. (goto-char after-bullet))
  21955. ;; Reversed case: Move point to special position when
  21956. ;; point was already at beginning of line and command is
  21957. ;; repeated.
  21958. (when (and (= (point) pos) (eq last-command this-command))
  21959. (goto-char after-bullet))))))))
  21960. (org-no-warnings
  21961. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21962. (setq disable-point-adjustment
  21963. (or (not (invisible-p (point)))
  21964. (not (invisible-p (max (point-min) (1- (point))))))))
  21965. (defun org-end-of-line (&optional arg)
  21966. "Go to the end of the line.
  21967. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21968. tags on the first attempt, and only move to after the tags when
  21969. the cursor is already beyond the end of the headline."
  21970. (interactive "P")
  21971. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21972. org-special-ctrl-a/e))
  21973. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21974. 'end-of-visual-line)
  21975. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21976. (t 'end-of-line)))
  21977. deactivate-mark)
  21978. (if (or (not special) arg) (call-interactively move-fun)
  21979. (let* ((element (save-excursion (beginning-of-line)
  21980. (org-element-at-point)))
  21981. (type (org-element-type element)))
  21982. (cond
  21983. ((memq type '(headline inlinetask))
  21984. (let ((pos (point)))
  21985. (beginning-of-line 1)
  21986. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21987. (if (eq special t)
  21988. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21989. (goto-char (match-beginning 1))
  21990. (goto-char (match-end 0)))
  21991. (if (or (< pos (match-end 0))
  21992. (not (eq this-command last-command)))
  21993. (goto-char (match-end 0))
  21994. (goto-char (match-beginning 1))))
  21995. (call-interactively move-fun))))
  21996. ((outline-invisible-p (line-end-position))
  21997. ;; If element is hidden, `move-end-of-line' would put point
  21998. ;; after it. Use `end-of-line' to stay on current line.
  21999. (call-interactively 'end-of-line))
  22000. (t (call-interactively move-fun)))))
  22001. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22002. (setq disable-point-adjustment
  22003. (or (not (invisible-p (point)))
  22004. (not (invisible-p (max (point-min) (1- (point))))))))
  22005. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22006. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22007. (defun org-backward-sentence (&optional _arg)
  22008. "Go to beginning of sentence, or beginning of table field.
  22009. This will call `backward-sentence' or `org-table-beginning-of-field',
  22010. depending on context."
  22011. (interactive)
  22012. (let* ((element (org-element-at-point))
  22013. (contents-begin (org-element-property :contents-begin element))
  22014. (table (org-element-lineage element '(table) t)))
  22015. (if (and table
  22016. (> (point) contents-begin)
  22017. (<= (point) (org-element-property :contents-end table)))
  22018. (call-interactively #'org-table-beginning-of-field)
  22019. (save-restriction
  22020. (when (and contents-begin
  22021. (< (point-min) contents-begin)
  22022. (> (point) contents-begin))
  22023. (narrow-to-region contents-begin
  22024. (org-element-property :contents-end element)))
  22025. (call-interactively #'backward-sentence)))))
  22026. (defun org-forward-sentence (&optional _arg)
  22027. "Go to end of sentence, or end of table field.
  22028. This will call `forward-sentence' or `org-table-end-of-field',
  22029. depending on context."
  22030. (interactive)
  22031. (let* ((element (org-element-at-point))
  22032. (contents-end (org-element-property :contents-end element))
  22033. (table (org-element-lineage element '(table) t)))
  22034. (if (and table
  22035. (>= (point) (org-element-property :contents-begin table))
  22036. (< (point) contents-end))
  22037. (call-interactively #'org-table-end-of-field)
  22038. (save-restriction
  22039. (when (and contents-end
  22040. (> (point-max) contents-end)
  22041. ;; Skip blank lines between elements.
  22042. (< (org-element-property :end element)
  22043. (save-excursion (goto-char contents-end)
  22044. (skip-chars-forward " \r\t\n"))))
  22045. (narrow-to-region (org-element-property :contents-begin element)
  22046. contents-end))
  22047. (call-interactively #'forward-sentence)))))
  22048. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22049. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22050. (defun org-kill-line (&optional _arg)
  22051. "Kill line, to tags or end of line."
  22052. (interactive)
  22053. (cond
  22054. ((or (not org-special-ctrl-k)
  22055. (bolp)
  22056. (not (org-at-heading-p)))
  22057. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22058. org-ctrl-k-protect-subtree)
  22059. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22060. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22061. (user-error "C-k aborted as it would kill a hidden subtree")))
  22062. (call-interactively
  22063. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22064. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22065. (kill-region (point) (match-beginning 1))
  22066. (org-set-tags nil t))
  22067. (t (kill-region (point) (point-at-eol)))))
  22068. (define-key org-mode-map "\C-k" 'org-kill-line)
  22069. (defun org-yank (&optional arg)
  22070. "Yank. If the kill is a subtree, treat it specially.
  22071. This command will look at the current kill and check if is a single
  22072. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22073. cursor is at the beginning of a line or after the stars of a currently
  22074. empty headline, then the yank is handled specially. How exactly depends
  22075. on the value of the following variables, both set by default.
  22076. `org-yank-folded-subtrees'
  22077. When set, the subtree(s) will be folded after insertion, but only
  22078. if doing so would now swallow text after the yanked text.
  22079. `org-yank-adjusted-subtrees'
  22080. When set, the subtree will be promoted or demoted in order to
  22081. fit into the local outline tree structure, which means that the
  22082. level will be adjusted so that it becomes the smaller one of the
  22083. two *visible* surrounding headings.
  22084. Any prefix to this command will cause `yank' to be called directly with
  22085. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22086. will just
  22087. plainly yank the text as it is.
  22088. \[1] The test checks if the first non-white line is a heading
  22089. and if there are no other headings with fewer stars."
  22090. (interactive "P")
  22091. (org-yank-generic 'yank arg))
  22092. (defun org-yank-generic (command arg)
  22093. "Perform some yank-like command.
  22094. This function implements the behavior described in the `org-yank'
  22095. documentation. However, it has been generalized to work for any
  22096. interactive command with similar behavior."
  22097. ;; pretend to be command COMMAND
  22098. (setq this-command command)
  22099. (if arg
  22100. (call-interactively command)
  22101. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22102. (and (org-kill-is-subtree-p)
  22103. (or (bolp)
  22104. (and (looking-at "[ \t]*$")
  22105. (string-match
  22106. "\\`\\*+\\'"
  22107. (buffer-substring (point-at-bol) (point)))))))
  22108. swallowp)
  22109. (cond
  22110. ((and subtreep org-yank-folded-subtrees)
  22111. (let ((beg (point))
  22112. end)
  22113. (if (and subtreep org-yank-adjusted-subtrees)
  22114. (org-paste-subtree nil nil 'for-yank)
  22115. (call-interactively command))
  22116. (setq end (point))
  22117. (goto-char beg)
  22118. (when (and (bolp) subtreep
  22119. (not (setq swallowp
  22120. (org-yank-folding-would-swallow-text beg end))))
  22121. (org-with-limited-levels
  22122. (or (looking-at org-outline-regexp)
  22123. (re-search-forward org-outline-regexp-bol end t))
  22124. (while (and (< (point) end) (looking-at org-outline-regexp))
  22125. (hide-subtree)
  22126. (org-cycle-show-empty-lines 'folded)
  22127. (condition-case nil
  22128. (outline-forward-same-level 1)
  22129. (error (goto-char end))))))
  22130. (when swallowp
  22131. (message
  22132. "Inserted text not folded because that would swallow text"))
  22133. (goto-char end)
  22134. (skip-chars-forward " \t\n\r")
  22135. (beginning-of-line 1)
  22136. (push-mark beg 'nomsg)))
  22137. ((and subtreep org-yank-adjusted-subtrees)
  22138. (let ((beg (point-at-bol)))
  22139. (org-paste-subtree nil nil 'for-yank)
  22140. (push-mark beg 'nomsg)))
  22141. (t
  22142. (call-interactively command))))))
  22143. (defun org-yank-folding-would-swallow-text (beg end)
  22144. "Would hide-subtree at BEG swallow any text after END?"
  22145. (let (level)
  22146. (org-with-limited-levels
  22147. (save-excursion
  22148. (goto-char beg)
  22149. (when (or (looking-at org-outline-regexp)
  22150. (re-search-forward org-outline-regexp-bol end t))
  22151. (setq level (org-outline-level)))
  22152. (goto-char end)
  22153. (skip-chars-forward " \t\r\n\v\f")
  22154. (if (or (eobp)
  22155. (and (bolp) (looking-at org-outline-regexp)
  22156. (<= (org-outline-level) level)))
  22157. nil ; Nothing would be swallowed
  22158. t))))) ; something would swallow
  22159. (define-key org-mode-map "\C-y" 'org-yank)
  22160. (defun org-truely-invisible-p ()
  22161. "Check if point is at a character currently not visible.
  22162. This version does not only check the character property, but also
  22163. `visible-mode'."
  22164. ;; Early versions of noutline don't have `outline-invisible-p'.
  22165. (if (org-bound-and-true-p visible-mode)
  22166. nil
  22167. (outline-invisible-p)))
  22168. (defun org-invisible-p2 ()
  22169. "Check if point is at a character currently not visible."
  22170. (save-excursion
  22171. (if (and (eolp) (not (bobp))) (backward-char 1))
  22172. ;; Early versions of noutline don't have `outline-invisible-p'.
  22173. (outline-invisible-p)))
  22174. (defun org-back-to-heading (&optional invisible-ok)
  22175. "Call `outline-back-to-heading', but provide a better error message."
  22176. (condition-case nil
  22177. (outline-back-to-heading invisible-ok)
  22178. (error (error "Before first headline at position %d in buffer %s"
  22179. (point) (current-buffer)))))
  22180. (defun org-before-first-heading-p ()
  22181. "Before first heading?"
  22182. (save-excursion
  22183. (end-of-line)
  22184. (null (re-search-backward org-outline-regexp-bol nil t))))
  22185. (defun org-at-heading-p (&optional ignored)
  22186. (outline-on-heading-p t))
  22187. ;; Compatibility alias with Org versions < 7.8.03
  22188. (defalias 'org-on-heading-p 'org-at-heading-p)
  22189. (defun org-in-commented-heading-p (&optional no-inheritance)
  22190. "Non-nil if point is under a commented heading.
  22191. This function also checks ancestors of the current headline,
  22192. unless optional argument NO-INHERITANCE is non-nil."
  22193. (cond
  22194. ((org-before-first-heading-p) nil)
  22195. ((let ((headline (nth 4 (org-heading-components))))
  22196. (and headline
  22197. (let ((case-fold-search nil))
  22198. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22199. headline)))))
  22200. (no-inheritance nil)
  22201. (t
  22202. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22203. (defun org-at-comment-p nil
  22204. "Is cursor in a commented line?"
  22205. (save-excursion
  22206. (save-match-data
  22207. (beginning-of-line)
  22208. (looking-at "^[ \t]*# "))))
  22209. (defun org-at-drawer-p nil
  22210. "Is cursor at a drawer keyword?"
  22211. (save-excursion
  22212. (move-beginning-of-line 1)
  22213. (looking-at org-drawer-regexp)))
  22214. (defun org-at-block-p nil
  22215. "Is cursor at a block keyword?"
  22216. (save-excursion
  22217. (move-beginning-of-line 1)
  22218. (looking-at org-block-regexp)))
  22219. (defun org-point-at-end-of-empty-headline ()
  22220. "If point is at the end of an empty headline, return t, else nil.
  22221. If the heading only contains a TODO keyword, it is still still considered
  22222. empty."
  22223. (and (looking-at "[ \t]*$")
  22224. (when org-todo-line-regexp
  22225. (save-excursion
  22226. (beginning-of-line 1)
  22227. (let ((case-fold-search nil))
  22228. (looking-at org-todo-line-regexp)
  22229. (string= (match-string 3) ""))))))
  22230. (defun org-at-heading-or-item-p ()
  22231. (or (org-at-heading-p) (org-at-item-p)))
  22232. (defun org-at-target-p ()
  22233. (or (org-in-regexp org-radio-target-regexp)
  22234. (org-in-regexp org-target-regexp)))
  22235. ;; Compatibility alias with Org versions < 7.8.03
  22236. (defalias 'org-on-target-p 'org-at-target-p)
  22237. (defun org-up-heading-all (arg)
  22238. "Move to the heading line of which the present line is a subheading.
  22239. This function considers both visible and invisible heading lines.
  22240. With argument, move up ARG levels."
  22241. (if (fboundp 'outline-up-heading-all)
  22242. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22243. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22244. (defun org-up-heading-safe ()
  22245. "Move to the heading line of which the present line is a subheading.
  22246. This version will not throw an error. It will return the level of the
  22247. headline found, or nil if no higher level is found.
  22248. Also, this function will be a lot faster than `outline-up-heading',
  22249. because it relies on stars being the outline starters. This can really
  22250. make a significant difference in outlines with very many siblings."
  22251. (when (ignore-errors (org-back-to-heading t))
  22252. (let ((level-up (1- (funcall outline-level))))
  22253. (and (> level-up 0)
  22254. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22255. (funcall outline-level)))))
  22256. (defun org-first-sibling-p ()
  22257. "Is this heading the first child of its parents?"
  22258. (interactive)
  22259. (let ((re org-outline-regexp-bol)
  22260. level l)
  22261. (unless (org-at-heading-p t)
  22262. (user-error "Not at a heading"))
  22263. (setq level (funcall outline-level))
  22264. (save-excursion
  22265. (if (not (re-search-backward re nil t))
  22266. t
  22267. (setq l (funcall outline-level))
  22268. (< l level)))))
  22269. (defun org-goto-sibling (&optional previous)
  22270. "Goto the next sibling, even if it is invisible.
  22271. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22272. when a sibling was found. When none is found, return nil and don't
  22273. move point."
  22274. (let ((fun (if previous 're-search-backward 're-search-forward))
  22275. (pos (point))
  22276. (re org-outline-regexp-bol)
  22277. level l)
  22278. (when (ignore-errors (org-back-to-heading t))
  22279. (setq level (funcall outline-level))
  22280. (catch 'exit
  22281. (or previous (forward-char 1))
  22282. (while (funcall fun re nil t)
  22283. (setq l (funcall outline-level))
  22284. (when (< l level) (goto-char pos) (throw 'exit nil))
  22285. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22286. (goto-char pos)
  22287. nil))))
  22288. (defun org-show-siblings ()
  22289. "Show all siblings of the current headline."
  22290. (save-excursion
  22291. (while (org-goto-sibling) (org-flag-heading nil)))
  22292. (save-excursion
  22293. (while (org-goto-sibling 'previous)
  22294. (org-flag-heading nil))))
  22295. (defun org-goto-first-child ()
  22296. "Goto the first child, even if it is invisible.
  22297. Return t when a child was found. Otherwise don't move point and
  22298. return nil."
  22299. (let (level (pos (point)) (re org-outline-regexp-bol))
  22300. (when (ignore-errors (org-back-to-heading t))
  22301. (setq level (outline-level))
  22302. (forward-char 1)
  22303. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22304. (progn (goto-char (match-beginning 0)) t)
  22305. (goto-char pos) nil))))
  22306. (defun org-show-hidden-entry ()
  22307. "Show an entry where even the heading is hidden."
  22308. (save-excursion
  22309. (org-show-entry)))
  22310. (defun org-flag-heading (flag &optional entry)
  22311. "Flag the current heading. FLAG non-nil means make invisible.
  22312. When ENTRY is non-nil, show the entire entry."
  22313. (save-excursion
  22314. (org-back-to-heading t)
  22315. ;; Check if we should show the entire entry
  22316. (if entry
  22317. (progn
  22318. (org-show-entry)
  22319. (save-excursion
  22320. (and (outline-next-heading)
  22321. (org-flag-heading nil))))
  22322. (outline-flag-region (max (point-min) (1- (point)))
  22323. (save-excursion (outline-end-of-heading) (point))
  22324. flag))))
  22325. (defun org-get-next-sibling ()
  22326. "Move to next heading of the same level, and return point.
  22327. If there is no such heading, return nil.
  22328. This is like outline-next-sibling, but invisible headings are ok."
  22329. (let ((level (funcall outline-level)))
  22330. (outline-next-heading)
  22331. (while (and (not (eobp)) (> (funcall outline-level) level))
  22332. (outline-next-heading))
  22333. (if (or (eobp) (< (funcall outline-level) level))
  22334. nil
  22335. (point))))
  22336. (defun org-get-last-sibling ()
  22337. "Move to previous heading of the same level, and return point.
  22338. If there is no such heading, return nil."
  22339. (let ((opoint (point))
  22340. (level (funcall outline-level)))
  22341. (outline-previous-heading)
  22342. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22343. (while (and (> (funcall outline-level) level)
  22344. (not (bobp)))
  22345. (outline-previous-heading))
  22346. (if (< (funcall outline-level) level)
  22347. nil
  22348. (point)))))
  22349. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22350. "Goto to the end of a subtree."
  22351. ;; This contains an exact copy of the original function, but it uses
  22352. ;; `org-back-to-heading', to make it work also in invisible
  22353. ;; trees. And is uses an invisible-ok argument.
  22354. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22355. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22356. ;; with many children and grandchildren etc, this can be much faster
  22357. ;; than the outline version.
  22358. (org-back-to-heading invisible-ok)
  22359. (let ((first t)
  22360. (level (funcall outline-level)))
  22361. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22362. ;; A true heading (not a plain list item), in Org-mode
  22363. ;; This means we can easily find the end by looking
  22364. ;; only for the right number of stars. Using a regexp to do
  22365. ;; this is so much faster than using a Lisp loop.
  22366. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22367. (forward-char 1)
  22368. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22369. ;; something else, do it the slow way
  22370. (while (and (not (eobp))
  22371. (or first (> (funcall outline-level) level)))
  22372. (setq first nil)
  22373. (outline-next-heading)))
  22374. (unless to-heading
  22375. (if (memq (preceding-char) '(?\n ?\^M))
  22376. (progn
  22377. ;; Go to end of line before heading
  22378. (forward-char -1)
  22379. (if (memq (preceding-char) '(?\n ?\^M))
  22380. ;; leave blank line before heading
  22381. (forward-char -1))))))
  22382. (point))
  22383. (defun org-end-of-meta-data (&optional full)
  22384. "Skip planning line and properties drawer in current entry.
  22385. When optional argument FULL is non-nil, also skip empty lines,
  22386. clocking lines and regular drawers at the beginning of the
  22387. entry."
  22388. (org-back-to-heading t)
  22389. (forward-line)
  22390. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22391. (when (looking-at org-property-drawer-re)
  22392. (goto-char (match-end 0))
  22393. (forward-line))
  22394. (when (and full (not (org-at-heading-p)))
  22395. (catch 'exit
  22396. (let ((end (save-excursion (outline-next-heading) (point)))
  22397. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22398. (while (not (eobp))
  22399. (cond ((org-looking-at-p org-drawer-regexp)
  22400. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22401. (forward-line)
  22402. (throw 'exit t)))
  22403. ((org-looking-at-p re) (forward-line))
  22404. (t (throw 'exit t))))))))
  22405. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22406. "Move forward to the ARG'th subheading at same level as this one.
  22407. Stop at the first and last subheadings of a superior heading.
  22408. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22409. non-nil it will also look at invisible ones."
  22410. (interactive "p")
  22411. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22412. (if (and arg (< arg 0))
  22413. (goto-char (point-min))
  22414. (outline-next-heading))
  22415. (org-at-heading-p)
  22416. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22417. (f (if (and arg (< arg 0))
  22418. 're-search-backward
  22419. 're-search-forward))
  22420. (count (if arg (abs arg) 1))
  22421. (result (point)))
  22422. (while (and (prog1 (> count 0)
  22423. (forward-char (if (and arg (< arg 0)) -1 1)))
  22424. (funcall f org-outline-regexp-bol nil 'move))
  22425. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22426. (cond ((< l level) (setq count 0))
  22427. ((and (= l level)
  22428. (or invisible-ok
  22429. (progn
  22430. (goto-char (line-beginning-position))
  22431. (not (outline-invisible-p)))))
  22432. (setq count (1- count))
  22433. (when (eq l level)
  22434. (setq result (point)))))))
  22435. (goto-char result))
  22436. (beginning-of-line 1)))
  22437. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22438. "Move backward to the ARG'th subheading at same level as this one.
  22439. Stop at the first and last subheadings of a superior heading."
  22440. (interactive "p")
  22441. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22442. (defun org-next-visible-heading (arg)
  22443. "Move to the next visible heading.
  22444. This function wraps `outline-next-visible-heading' with
  22445. `org-with-limited-levels' in order to skip over inline tasks and
  22446. respect customization of `org-odd-levels-only'."
  22447. (interactive "p")
  22448. (org-with-limited-levels
  22449. (outline-next-visible-heading arg)))
  22450. (defun org-previous-visible-heading (arg)
  22451. "Move to the next visible heading.
  22452. This function wraps `outline-previous-visible-heading' with
  22453. `org-with-limited-levels' in order to skip over inline tasks and
  22454. respect customization of `org-odd-levels-only'."
  22455. (interactive "p")
  22456. (org-with-limited-levels
  22457. (outline-previous-visible-heading arg)))
  22458. (defun org-next-block (arg &optional backward block-regexp)
  22459. "Jump to the next block.
  22460. With a prefix argument ARG, jump forward ARG many blocks.
  22461. When BACKWARD is non-nil, jump to the previous block.
  22462. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22463. Match data is set according to this regexp when the function
  22464. returns.
  22465. Return point at beginning of the opening line of found block.
  22466. Throw an error if no block is found."
  22467. (interactive "p")
  22468. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22469. (case-fold-search t)
  22470. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22471. (count (or arg 1))
  22472. (origin (point))
  22473. last-element)
  22474. (if backward (beginning-of-line) (end-of-line))
  22475. (while (and (> count 0) (funcall search-fn re nil t))
  22476. (let ((element (save-excursion
  22477. (goto-char (match-beginning 0))
  22478. (save-match-data (org-element-at-point)))))
  22479. (when (and (memq (org-element-type element)
  22480. '(center-block comment-block dynamic-block
  22481. example-block export-block quote-block
  22482. special-block src-block verse-block))
  22483. (<= (match-beginning 0)
  22484. (org-element-property :post-affiliated element)))
  22485. (setq last-element element)
  22486. (decf count))))
  22487. (if (= count 0)
  22488. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22489. (save-match-data (org-show-context)))
  22490. (goto-char origin)
  22491. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22492. (defun org-previous-block (arg &optional block-regexp)
  22493. "Jump to the previous block.
  22494. With a prefix argument ARG, jump backward ARG many source blocks.
  22495. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22496. (interactive "p")
  22497. (org-next-block arg t block-regexp))
  22498. (defun org-forward-paragraph ()
  22499. "Move forward to beginning of next paragraph or equivalent.
  22500. The function moves point to the beginning of the next visible
  22501. structural element, which can be a paragraph, a table, a list
  22502. item, etc. It also provides some special moves for convenience:
  22503. - On an affiliated keyword, jump to the beginning of the
  22504. relative element.
  22505. - On an item or a footnote definition, move to the second
  22506. element inside, if any.
  22507. - On a table or a property drawer, jump after it.
  22508. - On a verse or source block, stop after blank lines."
  22509. (interactive)
  22510. (when (eobp) (user-error "Cannot move further down"))
  22511. (let* ((deactivate-mark nil)
  22512. (element (org-element-at-point))
  22513. (type (org-element-type element))
  22514. (post-affiliated (org-element-property :post-affiliated element))
  22515. (contents-begin (org-element-property :contents-begin element))
  22516. (contents-end (org-element-property :contents-end element))
  22517. (end (let ((end (org-element-property :end element)) (parent element))
  22518. (while (and (setq parent (org-element-property :parent parent))
  22519. (= (org-element-property :contents-end parent) end))
  22520. (setq end (org-element-property :end parent)))
  22521. end)))
  22522. (cond ((not element)
  22523. (skip-chars-forward " \r\t\n")
  22524. (or (eobp) (beginning-of-line)))
  22525. ;; On affiliated keywords, move to element's beginning.
  22526. ((< (point) post-affiliated)
  22527. (goto-char post-affiliated))
  22528. ;; At a table row, move to the end of the table. Similarly,
  22529. ;; at a node property, move to the end of the property
  22530. ;; drawer.
  22531. ((memq type '(node-property table-row))
  22532. (goto-char (org-element-property
  22533. :end (org-element-property :parent element))))
  22534. ((memq type '(property-drawer table)) (goto-char end))
  22535. ;; Consider blank lines as separators in verse and source
  22536. ;; blocks to ease editing.
  22537. ((memq type '(src-block verse-block))
  22538. (when (eq type 'src-block)
  22539. (setq contents-end
  22540. (save-excursion (goto-char end)
  22541. (skip-chars-backward " \r\t\n")
  22542. (line-beginning-position))))
  22543. (beginning-of-line)
  22544. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22545. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22546. (goto-char end)
  22547. (skip-chars-forward " \r\t\n")
  22548. (if (= (point) contents-end) (goto-char end)
  22549. (beginning-of-line))))
  22550. ;; With no contents, just skip element.
  22551. ((not contents-begin) (goto-char end))
  22552. ;; If contents are invisible, skip the element altogether.
  22553. ((outline-invisible-p (line-end-position))
  22554. (case type
  22555. (headline
  22556. (org-with-limited-levels (outline-next-visible-heading 1)))
  22557. ;; At a plain list, make sure we move to the next item
  22558. ;; instead of skipping the whole list.
  22559. (plain-list (forward-char)
  22560. (org-forward-paragraph))
  22561. (otherwise (goto-char end))))
  22562. ((>= (point) contents-end) (goto-char end))
  22563. ((>= (point) contents-begin)
  22564. ;; This can only happen on paragraphs and plain lists.
  22565. (case type
  22566. (paragraph (goto-char end))
  22567. ;; At a plain list, try to move to second element in
  22568. ;; first item, if possible.
  22569. (plain-list (end-of-line)
  22570. (org-forward-paragraph))))
  22571. ;; When contents start on the middle of a line (e.g. in
  22572. ;; items and footnote definitions), try to reach first
  22573. ;; element starting after current line.
  22574. ((> (line-end-position) contents-begin)
  22575. (end-of-line)
  22576. (org-forward-paragraph))
  22577. (t (goto-char contents-begin)))))
  22578. (defun org-backward-paragraph ()
  22579. "Move backward to start of previous paragraph or equivalent.
  22580. The function moves point to the beginning of the current
  22581. structural element, which can be a paragraph, a table, a list
  22582. item, etc., or to the beginning of the previous visible one if
  22583. point is already there. It also provides some special moves for
  22584. convenience:
  22585. - On an affiliated keyword, jump to the first one.
  22586. - On a table or a property drawer, move to its beginning.
  22587. - On a verse or source block, stop before blank lines."
  22588. (interactive)
  22589. (when (bobp) (user-error "Cannot move further up"))
  22590. (let* ((deactivate-mark nil)
  22591. (element (org-element-at-point))
  22592. (type (org-element-type element))
  22593. (contents-begin (org-element-property :contents-begin element))
  22594. (contents-end (org-element-property :contents-end element))
  22595. (post-affiliated (org-element-property :post-affiliated element))
  22596. (begin (org-element-property :begin element)))
  22597. (cond
  22598. ((not element) (goto-char (point-min)))
  22599. ((= (point) begin)
  22600. (backward-char)
  22601. (org-backward-paragraph))
  22602. ((<= (point) post-affiliated) (goto-char begin))
  22603. ((memq type '(node-property table-row))
  22604. (goto-char (org-element-property
  22605. :post-affiliated (org-element-property :parent element))))
  22606. ((memq type '(property-drawer table)) (goto-char begin))
  22607. ((memq type '(src-block verse-block))
  22608. (when (eq type 'src-block)
  22609. (setq contents-begin
  22610. (save-excursion (goto-char begin) (forward-line) (point))))
  22611. (if (= (point) contents-begin) (goto-char post-affiliated)
  22612. ;; Inside a verse block, see blank lines as paragraph
  22613. ;; separators.
  22614. (let ((origin (point)))
  22615. (skip-chars-backward " \r\t\n" contents-begin)
  22616. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22617. (skip-chars-forward " \r\t\n" origin)
  22618. (if (= (point) origin) (goto-char contents-begin)
  22619. (beginning-of-line))))))
  22620. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22621. ((eq type 'paragraph)
  22622. (goto-char contents-begin)
  22623. ;; When at first paragraph in an item or a footnote definition,
  22624. ;; move directly to beginning of line.
  22625. (let ((parent-contents
  22626. (org-element-property
  22627. :contents-begin (org-element-property :parent element))))
  22628. (when (and parent-contents (= parent-contents contents-begin))
  22629. (beginning-of-line))))
  22630. ;; At the end of a greater element, move to the beginning of the
  22631. ;; last element within.
  22632. ((>= (point) contents-end)
  22633. (goto-char (1- contents-end))
  22634. (org-backward-paragraph))
  22635. (t (goto-char (or post-affiliated begin))))
  22636. ;; Ensure we never leave point invisible.
  22637. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22638. (defun org-forward-element ()
  22639. "Move forward by one element.
  22640. Move to the next element at the same level, when possible."
  22641. (interactive)
  22642. (cond ((eobp) (user-error "Cannot move further down"))
  22643. ((org-with-limited-levels (org-at-heading-p))
  22644. (let ((origin (point)))
  22645. (goto-char (org-end-of-subtree nil t))
  22646. (unless (org-with-limited-levels (org-at-heading-p))
  22647. (goto-char origin)
  22648. (user-error "Cannot move further down"))))
  22649. (t
  22650. (let* ((elem (org-element-at-point))
  22651. (end (org-element-property :end elem))
  22652. (parent (org-element-property :parent elem)))
  22653. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22654. (goto-char (org-element-property :end parent)))
  22655. ((integer-or-marker-p end) (goto-char end))
  22656. (t (message "No element at point")))))))
  22657. (defun org-backward-element ()
  22658. "Move backward by one element.
  22659. Move to the previous element at the same level, when possible."
  22660. (interactive)
  22661. (cond ((bobp) (user-error "Cannot move further up"))
  22662. ((org-with-limited-levels (org-at-heading-p))
  22663. ;; At a headline, move to the previous one, if any, or stay
  22664. ;; here.
  22665. (let ((origin (point)))
  22666. (org-with-limited-levels (org-backward-heading-same-level 1))
  22667. ;; When current headline has no sibling above, move to its
  22668. ;; parent.
  22669. (when (= (point) origin)
  22670. (or (org-with-limited-levels (org-up-heading-safe))
  22671. (progn (goto-char origin)
  22672. (user-error "Cannot move further up"))))))
  22673. (t
  22674. (let* ((elem (org-element-at-point))
  22675. (beg (org-element-property :begin elem)))
  22676. (cond
  22677. ;; Move to beginning of current element if point isn't
  22678. ;; there already.
  22679. ((null beg) (message "No element at point"))
  22680. ((/= (point) beg) (goto-char beg))
  22681. (t (goto-char beg)
  22682. (skip-chars-backward " \r\t\n")
  22683. (unless (bobp)
  22684. (let ((prev (org-element-at-point)))
  22685. (goto-char (org-element-property :begin prev))
  22686. (while (and (setq prev (org-element-property :parent prev))
  22687. (<= (org-element-property :end prev) beg))
  22688. (goto-char (org-element-property :begin prev)))))))))))
  22689. (defun org-up-element ()
  22690. "Move to upper element."
  22691. (interactive)
  22692. (if (org-with-limited-levels (org-at-heading-p))
  22693. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22694. (let* ((elem (org-element-at-point))
  22695. (parent (org-element-property :parent elem)))
  22696. (if parent (goto-char (org-element-property :begin parent))
  22697. (if (org-with-limited-levels (org-before-first-heading-p))
  22698. (user-error "No surrounding element")
  22699. (org-with-limited-levels (org-back-to-heading)))))))
  22700. (defvar org-element-greater-elements)
  22701. (defun org-down-element ()
  22702. "Move to inner element."
  22703. (interactive)
  22704. (let ((element (org-element-at-point)))
  22705. (cond
  22706. ((memq (org-element-type element) '(plain-list table))
  22707. (goto-char (org-element-property :contents-begin element))
  22708. (forward-char))
  22709. ((memq (org-element-type element) org-element-greater-elements)
  22710. ;; If contents are hidden, first disclose them.
  22711. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22712. (goto-char (or (org-element-property :contents-begin element)
  22713. (user-error "No content for this element"))))
  22714. (t (user-error "No inner element")))))
  22715. (defun org-drag-element-backward ()
  22716. "Move backward element at point."
  22717. (interactive)
  22718. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22719. (let* ((elem (org-element-at-point))
  22720. (prev-elem
  22721. (save-excursion
  22722. (goto-char (org-element-property :begin elem))
  22723. (skip-chars-backward " \r\t\n")
  22724. (unless (bobp)
  22725. (let* ((beg (org-element-property :begin elem))
  22726. (prev (org-element-at-point))
  22727. (up prev))
  22728. (while (and (setq up (org-element-property :parent up))
  22729. (<= (org-element-property :end up) beg))
  22730. (setq prev up))
  22731. prev)))))
  22732. ;; Error out if no previous element or previous element is
  22733. ;; a parent of the current one.
  22734. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22735. (user-error "Cannot drag element backward")
  22736. (let ((pos (point)))
  22737. (org-element-swap-A-B prev-elem elem)
  22738. (goto-char (+ (org-element-property :begin prev-elem)
  22739. (- pos (org-element-property :begin elem)))))))))
  22740. (defun org-drag-element-forward ()
  22741. "Move forward element at point."
  22742. (interactive)
  22743. (let* ((pos (point))
  22744. (elem (org-element-at-point)))
  22745. (when (= (point-max) (org-element-property :end elem))
  22746. (user-error "Cannot drag element forward"))
  22747. (goto-char (org-element-property :end elem))
  22748. (let ((next-elem (org-element-at-point)))
  22749. (when (or (org-element-nested-p elem next-elem)
  22750. (and (eq (org-element-type next-elem) 'headline)
  22751. (not (eq (org-element-type elem) 'headline))))
  22752. (goto-char pos)
  22753. (user-error "Cannot drag element forward"))
  22754. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22755. ;; body's length (without final blanks) and by the length of
  22756. ;; blanks between ELEM and NEXT-ELEM.
  22757. (let ((size-next (- (save-excursion
  22758. (goto-char (org-element-property :end next-elem))
  22759. (skip-chars-backward " \r\t\n")
  22760. (forward-line)
  22761. ;; Small correction if buffer doesn't end
  22762. ;; with a newline character.
  22763. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22764. (org-element-property :begin next-elem)))
  22765. (size-blank (- (org-element-property :end elem)
  22766. (save-excursion
  22767. (goto-char (org-element-property :end elem))
  22768. (skip-chars-backward " \r\t\n")
  22769. (forward-line)
  22770. (point)))))
  22771. (org-element-swap-A-B elem next-elem)
  22772. (goto-char (+ pos size-next size-blank))))))
  22773. (defun org-drag-line-forward (arg)
  22774. "Drag the line at point ARG lines forward."
  22775. (interactive "p")
  22776. (dotimes (n (abs arg))
  22777. (let ((c (current-column)))
  22778. (if (< 0 arg)
  22779. (progn
  22780. (beginning-of-line 2)
  22781. (transpose-lines 1)
  22782. (beginning-of-line 0))
  22783. (transpose-lines 1)
  22784. (beginning-of-line -1))
  22785. (org-move-to-column c))))
  22786. (defun org-drag-line-backward (arg)
  22787. "Drag the line at point ARG lines backward."
  22788. (interactive "p")
  22789. (org-drag-line-forward (- arg)))
  22790. (defun org-mark-element ()
  22791. "Put point at beginning of this element, mark at end.
  22792. Interactively, if this command is repeated or (in Transient Mark
  22793. mode) if the mark is active, it marks the next element after the
  22794. ones already marked."
  22795. (interactive)
  22796. (let (deactivate-mark)
  22797. (if (and (org-called-interactively-p 'any)
  22798. (or (and (eq last-command this-command) (mark t))
  22799. (and transient-mark-mode mark-active)))
  22800. (set-mark
  22801. (save-excursion
  22802. (goto-char (mark))
  22803. (goto-char (org-element-property :end (org-element-at-point)))))
  22804. (let ((element (org-element-at-point)))
  22805. (end-of-line)
  22806. (push-mark (org-element-property :end element) t t)
  22807. (goto-char (org-element-property :begin element))))))
  22808. (defun org-narrow-to-element ()
  22809. "Narrow buffer to current element."
  22810. (interactive)
  22811. (let ((elem (org-element-at-point)))
  22812. (cond
  22813. ((eq (car elem) 'headline)
  22814. (narrow-to-region
  22815. (org-element-property :begin elem)
  22816. (org-element-property :end elem)))
  22817. ((memq (car elem) org-element-greater-elements)
  22818. (narrow-to-region
  22819. (org-element-property :contents-begin elem)
  22820. (org-element-property :contents-end elem)))
  22821. (t
  22822. (narrow-to-region
  22823. (org-element-property :begin elem)
  22824. (org-element-property :end elem))))))
  22825. (defun org-transpose-element ()
  22826. "Transpose current and previous elements, keeping blank lines between.
  22827. Point is moved after both elements."
  22828. (interactive)
  22829. (org-skip-whitespace)
  22830. (let ((end (org-element-property :end (org-element-at-point))))
  22831. (org-drag-element-backward)
  22832. (goto-char end)))
  22833. (defun org-unindent-buffer ()
  22834. "Un-indent the visible part of the buffer.
  22835. Relative indentation (between items, inside blocks, etc.) isn't
  22836. modified."
  22837. (interactive)
  22838. (unless (eq major-mode 'org-mode)
  22839. (user-error "Cannot un-indent a buffer not in Org mode"))
  22840. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22841. unindent-tree ; For byte-compiler.
  22842. (unindent-tree
  22843. (function
  22844. (lambda (contents)
  22845. (mapc
  22846. (lambda (element)
  22847. (if (memq (org-element-type element) '(headline section))
  22848. (funcall unindent-tree (org-element-contents element))
  22849. (save-excursion
  22850. (save-restriction
  22851. (narrow-to-region
  22852. (org-element-property :begin element)
  22853. (org-element-property :end element))
  22854. (org-do-remove-indentation)))))
  22855. (reverse contents))))))
  22856. (funcall unindent-tree (org-element-contents parse-tree))))
  22857. (defun org-show-subtree ()
  22858. "Show everything after this heading at deeper levels."
  22859. (interactive)
  22860. (outline-flag-region
  22861. (point)
  22862. (save-excursion
  22863. (org-end-of-subtree t t))
  22864. nil))
  22865. (defun org-show-entry ()
  22866. "Show the body directly following this heading.
  22867. Show the heading too, if it is currently invisible."
  22868. (interactive)
  22869. (save-excursion
  22870. (ignore-errors
  22871. (org-back-to-heading t)
  22872. (outline-flag-region
  22873. (max (point-min) (1- (point)))
  22874. (save-excursion
  22875. (if (re-search-forward
  22876. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22877. (match-beginning 1)
  22878. (point-max)))
  22879. nil)
  22880. (org-cycle-hide-drawers 'children))))
  22881. (defun org-make-options-regexp (kwds &optional extra)
  22882. "Make a regular expression for keyword lines.
  22883. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22884. when non-nil, is a regexp matching keywords names."
  22885. (concat "^[ \t]*#\\+\\("
  22886. (regexp-opt kwds)
  22887. (and extra (concat (and kwds "\\|") extra))
  22888. "\\):[ \t]*\\(.*\\)"))
  22889. ;; Make isearch reveal the necessary context
  22890. (defun org-isearch-end ()
  22891. "Reveal context after isearch exits."
  22892. (when isearch-success ; only if search was successful
  22893. (if (featurep 'xemacs)
  22894. ;; Under XEmacs, the hook is run in the correct place,
  22895. ;; we directly show the context.
  22896. (org-show-context 'isearch)
  22897. ;; In Emacs the hook runs *before* restoring the overlays.
  22898. ;; So we have to use a one-time post-command-hook to do this.
  22899. ;; (Emacs 22 has a special variable, see function `org-mode')
  22900. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22901. isearch-mode-end-hook-quit)
  22902. ;; Only when the isearch was not quitted.
  22903. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22904. 'append 'local)))))
  22905. (defun org-isearch-post-command ()
  22906. "Remove self from hook, and show context."
  22907. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22908. (org-show-context 'isearch))
  22909. ;;;; Integration with and fixes for other packages
  22910. ;;; Imenu support
  22911. (defvar org-imenu-markers nil
  22912. "All markers currently used by Imenu.")
  22913. (make-variable-buffer-local 'org-imenu-markers)
  22914. (defun org-imenu-new-marker (&optional pos)
  22915. "Return a new marker for use by Imenu, and remember the marker."
  22916. (let ((m (make-marker)))
  22917. (move-marker m (or pos (point)))
  22918. (push m org-imenu-markers)
  22919. m))
  22920. (defun org-imenu-get-tree ()
  22921. "Produce the index for Imenu."
  22922. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22923. (setq org-imenu-markers nil)
  22924. (let* ((n org-imenu-depth)
  22925. (re (concat "^" (org-get-limited-outline-regexp)))
  22926. (subs (make-vector (1+ n) nil))
  22927. (last-level 0)
  22928. m level head0 head)
  22929. (save-excursion
  22930. (save-restriction
  22931. (widen)
  22932. (goto-char (point-max))
  22933. (while (re-search-backward re nil t)
  22934. (setq level (org-reduced-level (funcall outline-level)))
  22935. (when (and (<= level n)
  22936. (looking-at org-complex-heading-regexp)
  22937. (setq head0 (org-match-string-no-properties 4)))
  22938. (setq head (org-link-display-format head0)
  22939. m (org-imenu-new-marker))
  22940. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22941. (if (>= level last-level)
  22942. (push (cons head m) (aref subs level))
  22943. (push (cons head (aref subs (1+ level))) (aref subs level))
  22944. (loop for i from (1+ level) to n do (aset subs i nil)))
  22945. (setq last-level level)))))
  22946. (aref subs 1)))
  22947. (eval-after-load "imenu"
  22948. '(progn
  22949. (add-hook 'imenu-after-jump-hook
  22950. (lambda ()
  22951. (if (derived-mode-p 'org-mode)
  22952. (org-show-context 'org-goto))))))
  22953. (defun org-link-display-format (link)
  22954. "Replace a link with its the description.
  22955. If there is no description, use the link target."
  22956. (save-match-data
  22957. (if (string-match org-bracket-link-analytic-regexp link)
  22958. (replace-match (if (match-end 5)
  22959. (match-string 5 link)
  22960. (concat (match-string 1 link)
  22961. (match-string 3 link)))
  22962. nil t link)
  22963. link)))
  22964. (defun org-toggle-link-display ()
  22965. "Toggle the literal or descriptive display of links."
  22966. (interactive)
  22967. (if org-descriptive-links
  22968. (progn (org-remove-from-invisibility-spec '(org-link))
  22969. (org-restart-font-lock)
  22970. (setq org-descriptive-links nil))
  22971. (progn (add-to-invisibility-spec '(org-link))
  22972. (org-restart-font-lock)
  22973. (setq org-descriptive-links t))))
  22974. ;; Speedbar support
  22975. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22976. "Overlay marking the agenda restriction line in speedbar.")
  22977. (overlay-put org-speedbar-restriction-lock-overlay
  22978. 'face 'org-agenda-restriction-lock)
  22979. (overlay-put org-speedbar-restriction-lock-overlay
  22980. 'help-echo "Agendas are currently limited to this item.")
  22981. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22982. (defun org-speedbar-set-agenda-restriction ()
  22983. "Restrict future agenda commands to the location at point in speedbar.
  22984. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22985. (interactive)
  22986. (require 'org-agenda)
  22987. (let (p m tp np dir txt)
  22988. (cond
  22989. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22990. 'org-imenu t))
  22991. (setq m (get-text-property p 'org-imenu-marker))
  22992. (with-current-buffer (marker-buffer m)
  22993. (goto-char m)
  22994. (org-agenda-set-restriction-lock 'subtree)))
  22995. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22996. 'speedbar-function 'speedbar-find-file))
  22997. (setq tp (previous-single-property-change
  22998. (1+ p) 'speedbar-function)
  22999. np (next-single-property-change
  23000. tp 'speedbar-function)
  23001. dir (speedbar-line-directory)
  23002. txt (buffer-substring-no-properties (or tp (point-min))
  23003. (or np (point-max))))
  23004. (with-current-buffer (find-file-noselect
  23005. (let ((default-directory dir))
  23006. (expand-file-name txt)))
  23007. (unless (derived-mode-p 'org-mode)
  23008. (user-error "Cannot restrict to non-Org-mode file"))
  23009. (org-agenda-set-restriction-lock 'file)))
  23010. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23011. (move-overlay org-speedbar-restriction-lock-overlay
  23012. (point-at-bol) (point-at-eol))
  23013. (setq current-prefix-arg nil)
  23014. (org-agenda-maybe-redo)))
  23015. (defvar speedbar-file-key-map)
  23016. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23017. (eval-after-load "speedbar"
  23018. '(progn
  23019. (speedbar-add-supported-extension ".org")
  23020. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23021. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23022. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23023. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23024. (add-hook 'speedbar-visiting-tag-hook
  23025. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23026. ;;; Fixes and Hacks for problems with other packages
  23027. (defun org--flyspell-object-check-p (element)
  23028. "Non-nil when Flyspell can check object at point.
  23029. ELEMENT is the element at point."
  23030. (let ((object (save-excursion
  23031. (when (org-looking-at-p "\\>") (backward-char))
  23032. (org-element-context element))))
  23033. (case (org-element-type object)
  23034. ;; Prevent checks in links due to keybinding conflict with
  23035. ;; Flyspell.
  23036. ((code entity export-snippet inline-babel-call
  23037. inline-src-block line-break latex-fragment link macro
  23038. statistics-cookie target timestamp verbatim)
  23039. nil)
  23040. (footnote-reference
  23041. ;; Only in inline footnotes, within the definition.
  23042. (and (eq (org-element-property :type object) 'inline)
  23043. (< (save-excursion
  23044. (goto-char (org-element-property :begin object))
  23045. (search-forward ":" nil t 2))
  23046. (point))))
  23047. (otherwise t))))
  23048. (defun org-mode-flyspell-verify ()
  23049. "Function used for `flyspell-generic-check-word-predicate'."
  23050. (if (org-at-heading-p)
  23051. ;; At a headline or an inlinetask, check title only. This is
  23052. ;; faster than relying on `org-element-at-point'.
  23053. (and (save-excursion (beginning-of-line)
  23054. (and (let ((case-fold-search t))
  23055. (not (looking-at "\\*+ END[ \t]*$")))
  23056. (looking-at org-complex-heading-regexp)))
  23057. (match-beginning 4)
  23058. (>= (point) (match-beginning 4))
  23059. (or (not (match-beginning 5))
  23060. (< (point) (match-beginning 5))))
  23061. (let* ((element (org-element-at-point))
  23062. (post-affiliated (org-element-property :post-affiliated element)))
  23063. (cond
  23064. ;; Ignore checks in all affiliated keywords but captions.
  23065. ((< (point) post-affiliated)
  23066. (and (save-excursion
  23067. (beginning-of-line)
  23068. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23069. (> (point) (match-end 0))
  23070. (org--flyspell-object-check-p element)))
  23071. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23072. ((let ((log (org-log-into-drawer)))
  23073. (and log
  23074. (let ((drawer (org-element-lineage element '(drawer))))
  23075. (and drawer
  23076. (eq (compare-strings
  23077. log nil nil
  23078. (org-element-property :drawer-name drawer) nil nil t)
  23079. t)))))
  23080. nil)
  23081. (t
  23082. (case (org-element-type element)
  23083. ((comment quote-section) t)
  23084. (comment-block
  23085. ;; Allow checks between block markers, not on them.
  23086. (and (> (line-beginning-position) post-affiliated)
  23087. (save-excursion
  23088. (end-of-line)
  23089. (skip-chars-forward " \r\t\n")
  23090. (< (point) (org-element-property :end element)))))
  23091. ;; Arbitrary list of keywords where checks are meaningful.
  23092. ;; Make sure point is on the value part of the element.
  23093. (keyword
  23094. (and (member (org-element-property :key element)
  23095. '("DESCRIPTION" "TITLE"))
  23096. (save-excursion
  23097. (search-backward ":" (line-beginning-position) t))))
  23098. ;; Check is globally allowed in paragraphs verse blocks and
  23099. ;; table rows (after affiliated keywords) but some objects
  23100. ;; must not be affected.
  23101. ((paragraph table-row verse-block)
  23102. (let ((cbeg (org-element-property :contents-begin element))
  23103. (cend (org-element-property :contents-end element)))
  23104. (and cbeg (>= (point) cbeg) (< (point) cend)
  23105. (org--flyspell-object-check-p element))))))))))
  23106. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23107. (defun org-remove-flyspell-overlays-in (beg end)
  23108. "Remove flyspell overlays in region."
  23109. (and (org-bound-and-true-p flyspell-mode)
  23110. (fboundp 'flyspell-delete-region-overlays)
  23111. (flyspell-delete-region-overlays beg end)))
  23112. (defvar flyspell-delayed-commands)
  23113. (eval-after-load "flyspell"
  23114. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23115. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23116. (eval-after-load "bookmark"
  23117. '(if (boundp 'bookmark-after-jump-hook)
  23118. ;; We can use the hook
  23119. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23120. ;; Hook not available, use advice
  23121. (defadvice bookmark-jump (after org-make-visible activate)
  23122. "Make the position visible."
  23123. (org-bookmark-jump-unhide))))
  23124. ;; Make sure saveplace shows the location if it was hidden
  23125. (eval-after-load "saveplace"
  23126. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23127. "Make the position visible."
  23128. (org-bookmark-jump-unhide)))
  23129. ;; Make sure ecb shows the location if it was hidden
  23130. (eval-after-load "ecb"
  23131. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23132. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23133. (if (derived-mode-p 'org-mode)
  23134. (org-show-context))))
  23135. (defun org-bookmark-jump-unhide ()
  23136. "Unhide the current position, to show the bookmark location."
  23137. (and (derived-mode-p 'org-mode)
  23138. (or (outline-invisible-p)
  23139. (save-excursion (goto-char (max (point-min) (1- (point))))
  23140. (outline-invisible-p)))
  23141. (org-show-context 'bookmark-jump)))
  23142. (defun org-mark-jump-unhide ()
  23143. "Make the point visible with `org-show-context' after jumping to the mark."
  23144. (when (and (derived-mode-p 'org-mode)
  23145. (outline-invisible-p))
  23146. (org-show-context 'mark-goto)))
  23147. (eval-after-load "simple"
  23148. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23149. "Make the point visible with `org-show-context'."
  23150. (org-mark-jump-unhide)))
  23151. (eval-after-load "simple"
  23152. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23153. "Make the point visible with `org-show-context'."
  23154. (org-mark-jump-unhide)))
  23155. (eval-after-load "simple"
  23156. '(defadvice pop-global-mark (after org-make-visible activate)
  23157. "Make the point visible with `org-show-context'."
  23158. (org-mark-jump-unhide)))
  23159. ;; Make session.el ignore our circular variable
  23160. (defvar session-globals-exclude)
  23161. (eval-after-load "session"
  23162. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23163. ;;;; Finish up
  23164. (provide 'org)
  23165. (run-hooks 'org-load-hook)
  23166. ;;; org.el ends here