org.el 956 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; URL: http://orgmode.org
  8. ;; Version: 8.3.1
  9. ;; Keywords: outlines, hypermedia, calendar, wp
  10. ;; Package-Requires: ((emacs "23.2"))
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'find-func)
  73. (require 'format-spec)
  74. (or (equal this-command 'eval-buffer)
  75. (condition-case nil
  76. (load (concat (file-name-directory load-file-name)
  77. "org-loaddefs.el")
  78. nil t t t)
  79. (error
  80. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  81. (sit-for 3)
  82. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  83. (sit-for 3))))
  84. (require 'org-macs)
  85. (require 'org-compat)
  86. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  87. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  88. ;;
  89. ;; In Org buffers, the value of `outline-regexp' is that of
  90. ;; `org-outline-regexp'. The only function still directly relying on
  91. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  92. ;; job when `orgstruct-mode' is active.
  93. (defvar org-outline-regexp "\\*+ "
  94. "Regexp to match Org headlines.")
  95. (defvar org-outline-regexp-bol "^\\*+ "
  96. "Regexp to match Org headlines.
  97. This is similar to `org-outline-regexp' but additionally makes
  98. sure that we are at the beginning of the line.")
  99. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  100. "Matches a headline, putting stars and text into groups.
  101. Stars are put in group 1 and the trimmed body in group 2.")
  102. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  103. (unless (boundp 'calendar-view-holidays-initially-flag)
  104. (org-defvaralias 'calendar-view-holidays-initially-flag
  105. 'view-calendar-holidays-initially))
  106. (unless (boundp 'calendar-view-diary-initially-flag)
  107. (org-defvaralias 'calendar-view-diary-initially-flag
  108. 'view-diary-entries-initially))
  109. (unless (boundp 'diary-fancy-buffer)
  110. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  111. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  112. (declare-function org-add-archive-files "org-archive" (files))
  113. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  114. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  115. (declare-function org-agenda-redo "org-agenda" (&optional all))
  116. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  117. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  118. (declare-function org-beamer-mode "ox-beamer" ())
  119. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  120. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  121. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  122. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  123. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  124. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  125. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  126. (declare-function org-clock-update-time-maybe "org-clock" ())
  127. (declare-function org-clocktable-shift "org-clock" (dir n))
  128. (declare-function org-element-at-point "org-element" ())
  129. (declare-function org-element-cache-refresh "org-element" (pos))
  130. (declare-function org-element-cache-reset "org-element" (&optional all))
  131. (declare-function org-element-contents "org-element" (element))
  132. (declare-function org-element-context "org-element" (&optional element))
  133. (declare-function org-element-copy "org-element" (datum))
  134. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  135. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  136. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  137. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  138. (declare-function org-element-property "org-element" (property element))
  139. (declare-function org-element-put-property "org-element" (element property value))
  140. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  141. (declare-function org-element-type "org-element" (element))
  142. (declare-function org-element-update-syntax "org-element" ())
  143. (declare-function org-id-find-id-file "org-id" (id))
  144. (declare-function org-id-get-create "org-id" (&optional force))
  145. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  146. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  147. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  148. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  149. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  150. (declare-function org-table-align "org-table" ())
  151. (declare-function org-table-begin "org-table" (&optional table-type))
  152. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  153. (declare-function org-table-blank-field "org-table" ())
  154. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  155. (declare-function org-table-edit-field "org-table" (arg))
  156. (declare-function org-table-end "org-table" (&optional table-type))
  157. (declare-function org-table-end-of-field "org-table" (&optional n))
  158. (declare-function org-table-insert-row "org-table" (&optional arg))
  159. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  160. (declare-function org-table-maybe-eval-formula "org-table" ())
  161. (declare-function org-table-maybe-recalculate-line "org-table" ())
  162. (declare-function org-table-next-row "org-table" ())
  163. (declare-function org-table-paste-rectangle "org-table" ())
  164. (declare-function org-table-wrap-region "org-table" (arg))
  165. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  166. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  167. (declare-function orgtbl-mode "org-table" (&optional arg))
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. ;; Tangle-file traversal returns reversed list of tangled files
  216. ;; and we want to evaluate the first target.
  217. (setq exported-file
  218. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  219. (message "%s %s"
  220. (if compile
  221. (progn (byte-compile-file exported-file 'load)
  222. "Compiled and loaded")
  223. (progn (load-file exported-file) "Loaded"))
  224. exported-file)))
  225. (defcustom org-babel-load-languages '((emacs-lisp . t))
  226. "Languages which can be evaluated in Org-mode buffers.
  227. This list can be used to load support for any of the languages
  228. below, note that each language will depend on a different set of
  229. system executables and/or Emacs modes. When a language is
  230. \"loaded\", then code blocks in that language can be evaluated
  231. with `org-babel-execute-src-block' bound by default to C-c
  232. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  233. be set to remove code block evaluation from the C-c C-c
  234. keybinding. By default only Emacs Lisp (which has no
  235. requirements) is loaded."
  236. :group 'org-babel
  237. :set 'org-babel-do-load-languages
  238. :version "24.1"
  239. :type '(alist :tag "Babel Languages"
  240. :key-type
  241. (choice
  242. (const :tag "Awk" awk)
  243. (const :tag "C" C)
  244. (const :tag "R" R)
  245. (const :tag "Asymptote" asymptote)
  246. (const :tag "Calc" calc)
  247. (const :tag "Clojure" clojure)
  248. (const :tag "CSS" css)
  249. (const :tag "Ditaa" ditaa)
  250. (const :tag "Dot" dot)
  251. (const :tag "Emacs Lisp" emacs-lisp)
  252. (const :tag "Forth" forth)
  253. (const :tag "Fortran" fortran)
  254. (const :tag "Gnuplot" gnuplot)
  255. (const :tag "Haskell" haskell)
  256. (const :tag "IO" io)
  257. (const :tag "J" J)
  258. (const :tag "Java" java)
  259. (const :tag "Javascript" js)
  260. (const :tag "LaTeX" latex)
  261. (const :tag "Ledger" ledger)
  262. (const :tag "Lilypond" lilypond)
  263. (const :tag "Lisp" lisp)
  264. (const :tag "Makefile" makefile)
  265. (const :tag "Maxima" maxima)
  266. (const :tag "Matlab" matlab)
  267. (const :tag "Mscgen" mscgen)
  268. (const :tag "Ocaml" ocaml)
  269. (const :tag "Octave" octave)
  270. (const :tag "Org" org)
  271. (const :tag "Perl" perl)
  272. (const :tag "Pico Lisp" picolisp)
  273. (const :tag "PlantUML" plantuml)
  274. (const :tag "Python" python)
  275. (const :tag "Ruby" ruby)
  276. (const :tag "Sass" sass)
  277. (const :tag "Scala" scala)
  278. (const :tag "Scheme" scheme)
  279. (const :tag "Screen" screen)
  280. (const :tag "Shell Script" shell)
  281. (const :tag "Shen" shen)
  282. (const :tag "Sql" sql)
  283. (const :tag "Sqlite" sqlite)
  284. (const :tag "ebnf2ps" ebnf2ps))
  285. :value-type (boolean :tag "Activate" :value t)))
  286. ;;;; Customization variables
  287. (defcustom org-clone-delete-id nil
  288. "Remove ID property of clones of a subtree.
  289. When non-nil, clones of a subtree don't inherit the ID property.
  290. Otherwise they inherit the ID property with a new unique
  291. identifier."
  292. :type 'boolean
  293. :version "24.1"
  294. :group 'org-id)
  295. ;;; Version
  296. (org-check-version)
  297. ;;;###autoload
  298. (defun org-version (&optional here full message)
  299. "Show the org-mode version.
  300. Interactively, or when MESSAGE is non-nil, show it in echo area.
  301. With prefix argument, or when HERE is non-nil, insert it at point.
  302. In non-interactive uses, a reduced version string is output unless
  303. FULL is given."
  304. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  305. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  306. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  307. (load-suffixes (list ".el"))
  308. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  309. (org-trash (or
  310. (and (fboundp 'org-release) (fboundp 'org-git-version))
  311. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  312. (load-suffixes save-load-suffixes)
  313. (org-version (org-release))
  314. (git-version (org-git-version))
  315. (version (format "Org-mode version %s (%s @ %s)"
  316. org-version
  317. git-version
  318. (if org-install-dir
  319. (if (string= org-dir org-install-dir)
  320. org-install-dir
  321. (concat "mixed installation! " org-install-dir " and " org-dir))
  322. "org-loaddefs.el can not be found!")))
  323. (version1 (if full version org-version)))
  324. (when here (insert version1))
  325. (when message (message "%s" version1))
  326. version1))
  327. (defconst org-version (org-version))
  328. ;;; Syntax Constants
  329. ;;;; Block
  330. (defconst org-block-regexp
  331. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  332. "Regular expression for hiding blocks.")
  333. (defconst org-dblock-start-re
  334. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  335. "Matches the start line of a dynamic block, with parameters.")
  336. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  337. "Matches the end of a dynamic block.")
  338. ;;;; Clock and Planning
  339. (defconst org-clock-string "CLOCK:"
  340. "String used as prefix for timestamps clocking work hours on an item.")
  341. (defvar org-closed-string "CLOSED:"
  342. "String used as the prefix for timestamps logging closing a TODO entry.")
  343. (defvar org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].")
  348. (defvar org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].")
  353. (defconst org-ds-keyword-length
  354. (+ 2
  355. (apply #'max
  356. (mapcar #'length
  357. (list org-deadline-string org-scheduled-string
  358. org-clock-string org-closed-string))))
  359. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  360. (defconst org-planning-line-re
  361. (concat "^[ \t]*"
  362. (regexp-opt
  363. (list org-closed-string org-deadline-string org-scheduled-string)
  364. t))
  365. "Matches a line with planning info.
  366. Matched keyword is in group 1.")
  367. (defconst org-clock-line-re
  368. (concat "^[ \t]*" org-clock-string)
  369. "Matches a line with clock info.")
  370. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  371. "Matches the DEADLINE keyword.")
  372. (defconst org-deadline-time-regexp
  373. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  374. "Matches the DEADLINE keyword together with a time stamp.")
  375. (defconst org-deadline-time-hour-regexp
  376. (concat "\\<" org-deadline-string
  377. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  378. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  379. (defconst org-deadline-line-regexp
  380. (concat "\\<\\(" org-deadline-string "\\).*")
  381. "Matches the DEADLINE keyword and the rest of the line.")
  382. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  383. "Matches the SCHEDULED keyword.")
  384. (defconst org-scheduled-time-regexp
  385. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  386. "Matches the SCHEDULED keyword together with a time stamp.")
  387. (defconst org-scheduled-time-hour-regexp
  388. (concat "\\<" org-scheduled-string
  389. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  390. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  391. (defconst org-closed-time-regexp
  392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  393. "Matches the CLOSED keyword together with a time stamp.")
  394. (defconst org-keyword-time-regexp
  395. (concat "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string
  398. org-clock-string)
  399. t)
  400. " *[[<]\\([^]>]+\\)[]>]")
  401. "Matches any of the 4 keywords, together with the time stamp.")
  402. (defconst org-keyword-time-not-clock-regexp
  403. (concat
  404. "\\<"
  405. (regexp-opt
  406. (list org-scheduled-string org-deadline-string org-closed-string) t)
  407. " *[[<]\\([^]>]+\\)[]>]")
  408. "Matches any of the 3 keywords, together with the time stamp.")
  409. (defconst org-maybe-keyword-time-regexp
  410. (concat "\\(\\<"
  411. (regexp-opt
  412. (list org-scheduled-string org-deadline-string org-closed-string
  413. org-clock-string)
  414. t)
  415. "\\)?"
  416. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  417. "\\|"
  418. "<%%([^\r\n>]*>\\)")
  419. "Matches a timestamp, possibly preceded by a keyword.")
  420. (defconst org-all-time-keywords
  421. (mapcar (lambda (w) (substring w 0 -1))
  422. (list org-scheduled-string org-deadline-string
  423. org-clock-string org-closed-string))
  424. "List of time keywords.")
  425. ;;;; Drawer
  426. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  427. "Matches first or last line of a hidden block.
  428. Group 1 contains drawer's name or \"END\".")
  429. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  430. "Regular expression matching the first line of a property drawer.")
  431. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  432. "Regular expression matching the last line of a property drawer.")
  433. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  434. "Regular expression matching the first line of a clock drawer.")
  435. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  436. "Regular expression matching the last line of a clock drawer.")
  437. (defconst org-property-drawer-re
  438. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  439. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  440. "[ \t]*:END:[ \t]*$")
  441. "Matches an entire property drawer.")
  442. (defconst org-clock-drawer-re
  443. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  444. org-clock-drawer-end-re "\\)\n?")
  445. "Matches an entire clock drawer.")
  446. ;;;; Headline
  447. (defconst org-heading-keyword-regexp-format
  448. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  449. "Printf format for a regexp matching a headline with some keyword.
  450. This regexp will match the headline of any node which has the
  451. exact keyword that is put into the format. The keyword isn't in
  452. any group by default, but the stars and the body are.")
  453. (defconst org-heading-keyword-maybe-regexp-format
  454. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  455. "Printf format for a regexp matching a headline, possibly with some keyword.
  456. This regexp can match any headline with the specified keyword, or
  457. without a keyword. The keyword isn't in any group by default,
  458. but the stars and the body are.")
  459. (defconst org-archive-tag "ARCHIVE"
  460. "The tag that marks a subtree as archived.
  461. An archived subtree does not open during visibility cycling, and does
  462. not contribute to the agenda listings.")
  463. (defconst org-comment-string "COMMENT"
  464. "Entries starting with this keyword will never be exported.
  465. An entry can be toggled between COMMENT and normal with
  466. \\[org-toggle-comment].")
  467. ;;;; LaTeX Environments and Fragments
  468. (defconst org-latex-regexps
  469. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  470. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  471. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  472. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  474. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  475. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  476. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  477. "Regular expressions for matching embedded LaTeX.")
  478. ;;;; Node Property
  479. (defconst org-effort-property "Effort"
  480. "The property that is being used to keep track of effort estimates.
  481. Effort estimates given in this property need to have the format H:MM.")
  482. ;;;; Table
  483. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  484. "Detect an org-type or table-type table.")
  485. (defconst org-table-line-regexp "^[ \t]*|"
  486. "Detect an org-type table line.")
  487. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  488. "Detect an org-type table line.")
  489. (defconst org-table-hline-regexp "^[ \t]*|-"
  490. "Detect an org-type table hline.")
  491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  492. "Detect a table-type table hline.")
  493. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  494. "Detect the first line outside a table when searching from within it.
  495. This works for both table types.")
  496. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  497. "Detect a #+TBLFM line.")
  498. ;;;; Timestamp
  499. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  500. "Regular expression for fast time stamp matching.")
  501. (defconst org-ts-regexp-inactive
  502. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  503. "Regular expression for fast inactive time stamp matching.")
  504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  505. "Regular expression for fast time stamp matching.")
  506. (defconst org-ts-regexp0
  507. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  508. "Regular expression matching time strings for analysis.
  509. This one does not require the space after the date, so it can be used
  510. on a string that terminates immediately after the date.")
  511. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  512. "Regular expression matching time strings for analysis.")
  513. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  514. "Regular expression matching time stamps, with groups.")
  515. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  516. "Regular expression matching time stamps (also [..]), with groups.")
  517. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tr-regexp-both
  520. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  521. "Regular expression matching a time stamp range.")
  522. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  523. org-ts-regexp "\\)?")
  524. "Regular expression matching a time stamp or time stamp range.")
  525. (defconst org-tsr-regexp-both
  526. (concat org-ts-regexp-both "\\(--?-?"
  527. org-ts-regexp-both "\\)?")
  528. "Regular expression matching a time stamp or time stamp range.
  529. The time stamps may be either active or inactive.")
  530. (defconst org-repeat-re
  531. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  532. "Regular expression for specifying repeated events.
  533. After a match, group 1 contains the repeat expression.")
  534. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  535. "Formats for `format-time-string' which are used for time stamps.")
  536. ;;; The custom variables
  537. (defgroup org nil
  538. "Outline-based notes management and organizer."
  539. :tag "Org"
  540. :group 'outlines
  541. :group 'calendar)
  542. (defcustom org-mode-hook nil
  543. "Mode hook for Org-mode, run after the mode was turned on."
  544. :group 'org
  545. :type 'hook)
  546. (defcustom org-load-hook nil
  547. "Hook that is run after org.el has been loaded."
  548. :group 'org
  549. :type 'hook)
  550. (defcustom org-log-buffer-setup-hook nil
  551. "Hook that is run after an Org log buffer is created."
  552. :group 'org
  553. :version "24.1"
  554. :type 'hook)
  555. (defvar org-modules) ; defined below
  556. (defvar org-modules-loaded nil
  557. "Have the modules been loaded already?")
  558. (defun org-load-modules-maybe (&optional force)
  559. "Load all extensions listed in `org-modules'."
  560. (when (or force (not org-modules-loaded))
  561. (mapc (lambda (ext)
  562. (condition-case nil (require ext)
  563. (error (message "Problems while trying to load feature `%s'" ext))))
  564. org-modules)
  565. (setq org-modules-loaded t)))
  566. (defun org-set-modules (var value)
  567. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  568. (set var value)
  569. (when (featurep 'org)
  570. (org-load-modules-maybe 'force)
  571. (org-element-cache-reset 'all)))
  572. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  573. "Modules that should always be loaded together with org.el.
  574. If a description starts with <C>, the file is not part of Emacs
  575. and loading it will require that you have downloaded and properly
  576. installed the Org mode distribution.
  577. You can also use this system to load external packages (i.e. neither Org
  578. core modules, nor modules from the CONTRIB directory). Just add symbols
  579. to the end of the list. If the package is called org-xyz.el, then you need
  580. to add the symbol `xyz', and the package must have a call to:
  581. \(provide 'org-xyz)
  582. For export specific modules, see also `org-export-backends'."
  583. :group 'org
  584. :set 'org-set-modules
  585. :version "24.4"
  586. :package-version '(Org . "8.0")
  587. :type
  588. '(set :greedy t
  589. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  590. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  591. (const :tag " crypt: Encryption of subtrees" org-crypt)
  592. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  593. (const :tag " docview: Links to doc-view buffers" org-docview)
  594. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  595. (const :tag " habit: Track your consistency with habits" org-habit)
  596. (const :tag " id: Global IDs for identifying entries" org-id)
  597. (const :tag " info: Links to Info nodes" org-info)
  598. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  599. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  600. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  601. (const :tag " mouse: Additional mouse support" org-mouse)
  602. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  603. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  604. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  605. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  606. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  607. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  608. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  609. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  610. (const :tag "C collector: Collect properties into tables" org-collector)
  611. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  612. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  613. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  614. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  615. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  616. (const :tag "C eval: Include command output as text" org-eval)
  617. (const :tag "C eww: Store link to url of eww" org-eww)
  618. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  619. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  620. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  621. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  622. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  623. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  624. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  625. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  626. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  627. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  628. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  629. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  630. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  631. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  632. (const :tag "C registry: A registry for Org-mode links" org-registry)
  633. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  634. (const :tag "C secretary: Team management with org-mode" org-secretary)
  635. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  636. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  637. (const :tag "C track: Keep up with Org-mode development" org-track)
  638. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  639. (const :tag "C vm: Links to VM folders/messages" org-vm)
  640. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  641. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  642. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  643. (defvar org-export-registered-backends) ; From ox.el.
  644. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  645. (declare-function org-export-backend-name "ox" (backend))
  646. (defcustom org-export-backends '(ascii html icalendar latex)
  647. "List of export back-ends that should be always available.
  648. If a description starts with <C>, the file is not part of Emacs
  649. and loading it will require that you have downloaded and properly
  650. installed the Org mode distribution.
  651. Unlike to `org-modules', libraries in this list will not be
  652. loaded along with Org, but only once the export framework is
  653. needed.
  654. This variable needs to be set before org.el is loaded. If you
  655. need to make a change while Emacs is running, use the customize
  656. interface or run the following code, where VAL stands for the new
  657. value of the variable, after updating it:
  658. \(progn
  659. \(setq org-export-registered-backends
  660. \(org-remove-if-not
  661. \(lambda (backend)
  662. \(let ((name (org-export-backend-name backend)))
  663. \(or (memq name val)
  664. \(catch 'parentp
  665. \(dolist (b val)
  666. \(and (org-export-derived-backend-p b name)
  667. \(throw 'parentp t)))))))
  668. org-export-registered-backends))
  669. \(let ((new-list (mapcar #'org-export-backend-name
  670. org-export-registered-backends)))
  671. \(dolist (backend val)
  672. \(cond
  673. \((not (load (format \"ox-%s\" backend) t t))
  674. \(message \"Problems while trying to load export back-end `%s'\"
  675. backend))
  676. \((not (memq backend new-list)) (push backend new-list))))
  677. \(set-default 'org-export-backends new-list)))
  678. Adding a back-end to this list will also pull the back-end it
  679. depends on, if any."
  680. :group 'org
  681. :group 'org-export
  682. :version "24.4"
  683. :package-version '(Org . "8.0")
  684. :initialize 'custom-initialize-set
  685. :set (lambda (var val)
  686. (if (not (featurep 'ox)) (set-default var val)
  687. ;; Any back-end not required anymore (not present in VAL and not
  688. ;; a parent of any back-end in the new value) is removed from the
  689. ;; list of registered back-ends.
  690. (setq org-export-registered-backends
  691. (org-remove-if-not
  692. (lambda (backend)
  693. (let ((name (org-export-backend-name backend)))
  694. (or (memq name val)
  695. (catch 'parentp
  696. (dolist (b val)
  697. (and (org-export-derived-backend-p b name)
  698. (throw 'parentp t)))))))
  699. org-export-registered-backends))
  700. ;; Now build NEW-LIST of both new back-ends and required
  701. ;; parents.
  702. (let ((new-list (mapcar #'org-export-backend-name
  703. org-export-registered-backends)))
  704. (dolist (backend val)
  705. (cond
  706. ((not (load (format "ox-%s" backend) t t))
  707. (message "Problems while trying to load export back-end `%s'"
  708. backend))
  709. ((not (memq backend new-list)) (push backend new-list))))
  710. ;; Set VAR to that list with fixed dependencies.
  711. (set-default var new-list))))
  712. :type '(set :greedy t
  713. (const :tag " ascii Export buffer to ASCII format" ascii)
  714. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  715. (const :tag " html Export buffer to HTML format" html)
  716. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  717. (const :tag " latex Export buffer to LaTeX format" latex)
  718. (const :tag " man Export buffer to MAN format" man)
  719. (const :tag " md Export buffer to Markdown format" md)
  720. (const :tag " odt Export buffer to ODT format" odt)
  721. (const :tag " org Export buffer to Org format" org)
  722. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  723. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  724. (const :tag "C deck Export buffer to deck.js presentations" deck)
  725. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  726. (const :tag "C groff Export buffer to Groff format" groff)
  727. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  728. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  729. (const :tag "C s5 Export buffer to s5 presentations" s5)
  730. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  731. (eval-after-load 'ox
  732. '(mapc
  733. (lambda (backend)
  734. (condition-case nil (require (intern (format "ox-%s" backend)))
  735. (error (message "Problems while trying to load export back-end `%s'"
  736. backend))))
  737. org-export-backends))
  738. (defcustom org-support-shift-select nil
  739. "Non-nil means make shift-cursor commands select text when possible.
  740. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  741. start selecting a region, or enlarge regions started in this way.
  742. In Org-mode, in special contexts, these same keys are used for
  743. other purposes, important enough to compete with shift selection.
  744. Org tries to balance these needs by supporting `shift-select-mode'
  745. outside these special contexts, under control of this variable.
  746. The default of this variable is nil, to avoid confusing behavior. Shifted
  747. cursor keys will then execute Org commands in the following contexts:
  748. - on a headline, changing TODO state (left/right) and priority (up/down)
  749. - on a time stamp, changing the time
  750. - in a plain list item, changing the bullet type
  751. - in a property definition line, switching between allowed values
  752. - in the BEGIN line of a clock table (changing the time block).
  753. Outside these contexts, the commands will throw an error.
  754. When this variable is t and the cursor is not in a special
  755. context, Org-mode will support shift-selection for making and
  756. enlarging regions. To make this more effective, the bullet
  757. cycling will no longer happen anywhere in an item line, but only
  758. if the cursor is exactly on the bullet.
  759. If you set this variable to the symbol `always', then the keys
  760. will not be special in headlines, property lines, and item lines,
  761. to make shift selection work there as well. If this is what you
  762. want, you can use the following alternative commands: `C-c C-t'
  763. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  764. can be used to switch TODO sets, `C-c -' to cycle item bullet
  765. types, and properties can be edited by hand or in column view.
  766. However, when the cursor is on a timestamp, shift-cursor commands
  767. will still edit the time stamp - this is just too good to give up.
  768. XEmacs user should have this variable set to nil, because
  769. `shift-select-mode' is in Emacs 23 or later only."
  770. :group 'org
  771. :type '(choice
  772. (const :tag "Never" nil)
  773. (const :tag "When outside special context" t)
  774. (const :tag "Everywhere except timestamps" always)))
  775. (defcustom org-loop-over-headlines-in-active-region nil
  776. "Shall some commands act upon headlines in the active region?
  777. When set to t, some commands will be performed in all headlines
  778. within the active region.
  779. When set to `start-level', some commands will be performed in all
  780. headlines within the active region, provided that these headlines
  781. are of the same level than the first one.
  782. When set to a string, those commands will be performed on the
  783. matching headlines within the active region. Such string must be
  784. a tags/property/todo match as it is used in the agenda tags view.
  785. The list of commands is: `org-schedule', `org-deadline',
  786. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  787. `org-archive-to-archive-sibling'. The archiving commands skip
  788. already archived entries."
  789. :type '(choice (const :tag "Don't loop" nil)
  790. (const :tag "All headlines in active region" t)
  791. (const :tag "In active region, headlines at the same level than the first one" start-level)
  792. (string :tag "Tags/Property/Todo matcher"))
  793. :version "24.1"
  794. :group 'org-todo
  795. :group 'org-archive)
  796. (defgroup org-startup nil
  797. "Options concerning startup of Org-mode."
  798. :tag "Org Startup"
  799. :group 'org)
  800. (defcustom org-startup-folded t
  801. "Non-nil means entering Org-mode will switch to OVERVIEW.
  802. This can also be configured on a per-file basis by adding one of
  803. the following lines anywhere in the buffer:
  804. #+STARTUP: fold (or `overview', this is equivalent)
  805. #+STARTUP: nofold (or `showall', this is equivalent)
  806. #+STARTUP: content
  807. #+STARTUP: showeverything
  808. By default, this option is ignored when Org opens agenda files
  809. for the first time. If you want the agenda to honor the startup
  810. option, set `org-agenda-inhibit-startup' to nil."
  811. :group 'org-startup
  812. :type '(choice
  813. (const :tag "nofold: show all" nil)
  814. (const :tag "fold: overview" t)
  815. (const :tag "content: all headlines" content)
  816. (const :tag "show everything, even drawers" showeverything)))
  817. (defcustom org-startup-truncated t
  818. "Non-nil means entering Org-mode will set `truncate-lines'.
  819. This is useful since some lines containing links can be very long and
  820. uninteresting. Also tables look terrible when wrapped."
  821. :group 'org-startup
  822. :type 'boolean)
  823. (defcustom org-startup-indented nil
  824. "Non-nil means turn on `org-indent-mode' on startup.
  825. This can also be configured on a per-file basis by adding one of
  826. the following lines anywhere in the buffer:
  827. #+STARTUP: indent
  828. #+STARTUP: noindent"
  829. :group 'org-structure
  830. :type '(choice
  831. (const :tag "Not" nil)
  832. (const :tag "Globally (slow on startup in large files)" t)))
  833. (defcustom org-use-sub-superscripts t
  834. "Non-nil means interpret \"_\" and \"^\" for display.
  835. If you want to control how Org exports those characters, see
  836. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  837. used to be an alias for `org-export-with-sub-superscripts' in
  838. Org <8.0, it is not anymore.
  839. When this option is turned on, you can use TeX-like syntax for
  840. sub- and superscripts within the buffer. Several characters after
  841. \"_\" or \"^\" will be considered as a single item - so grouping
  842. with {} is normally not needed. For example, the following things
  843. will be parsed as single sub- or superscripts:
  844. 10^24 or 10^tau several digits will be considered 1 item.
  845. 10^-12 or 10^-tau a leading sign with digits or a word
  846. x^2-y^3 will be read as x^2 - y^3, because items are
  847. terminated by almost any nonword/nondigit char.
  848. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  849. Still, ambiguity is possible. So when in doubt, use {} to enclose
  850. the sub/superscript. If you set this variable to the symbol `{}',
  851. the braces are *required* in order to trigger interpretations as
  852. sub/superscript. This can be helpful in documents that need \"_\"
  853. frequently in plain text."
  854. :group 'org-startup
  855. :version "24.4"
  856. :package-version '(Org . "8.0")
  857. :type '(choice
  858. (const :tag "Always interpret" t)
  859. (const :tag "Only with braces" {})
  860. (const :tag "Never interpret" nil)))
  861. (defcustom org-startup-with-beamer-mode nil
  862. "Non-nil means turn on `org-beamer-mode' on startup.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: beamer"
  866. :group 'org-startup
  867. :version "24.1"
  868. :type 'boolean)
  869. (defcustom org-startup-align-all-tables nil
  870. "Non-nil means align all tables when visiting a file.
  871. This is useful when the column width in tables is forced with <N> cookies
  872. in table fields. Such tables will look correct only after the first re-align.
  873. This can also be configured on a per-file basis by adding one of
  874. the following lines anywhere in the buffer:
  875. #+STARTUP: align
  876. #+STARTUP: noalign"
  877. :group 'org-startup
  878. :type 'boolean)
  879. (defcustom org-startup-with-inline-images nil
  880. "Non-nil means show inline images when loading a new Org file.
  881. This can also be configured on a per-file basis by adding one of
  882. the following lines anywhere in the buffer:
  883. #+STARTUP: inlineimages
  884. #+STARTUP: noinlineimages"
  885. :group 'org-startup
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom org-startup-with-latex-preview nil
  889. "Non-nil means preview LaTeX fragments when loading a new Org file.
  890. This can also be configured on a per-file basis by adding one of
  891. the following lines anywhere in the buffer:
  892. #+STARTUP: latexpreview
  893. #+STARTUP: nolatexpreview"
  894. :group 'org-startup
  895. :version "24.4"
  896. :package-version '(Org . "8.0")
  897. :type 'boolean)
  898. (defcustom org-insert-mode-line-in-empty-file nil
  899. "Non-nil means insert the first line setting Org-mode in empty files.
  900. When the function `org-mode' is called interactively in an empty file, this
  901. normally means that the file name does not automatically trigger Org-mode.
  902. To ensure that the file will always be in Org-mode in the future, a
  903. line enforcing Org-mode will be inserted into the buffer, if this option
  904. has been set."
  905. :group 'org-startup
  906. :type 'boolean)
  907. (defcustom org-replace-disputed-keys nil
  908. "Non-nil means use alternative key bindings for some keys.
  909. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  910. These keys are also used by other packages like shift-selection-mode'
  911. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  912. If you want to use Org-mode together with one of these other modes,
  913. or more generally if you would like to move some Org-mode commands to
  914. other keys, set this variable and configure the keys with the variable
  915. `org-disputed-keys'.
  916. This option is only relevant at load-time of Org-mode, and must be set
  917. *before* org.el is loaded. Changing it requires a restart of Emacs to
  918. become effective."
  919. :group 'org-startup
  920. :type 'boolean)
  921. (defcustom org-use-extra-keys nil
  922. "Non-nil means use extra key sequence definitions for certain commands.
  923. This happens automatically if you run XEmacs or if `window-system'
  924. is nil. This variable lets you do the same manually. You must
  925. set it before loading org.
  926. Example: on Carbon Emacs 22 running graphically, with an external
  927. keyboard on a Powerbook, the default way of setting M-left might
  928. not work for either Alt or ESC. Setting this variable will make
  929. it work for ESC."
  930. :group 'org-startup
  931. :type 'boolean)
  932. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  933. (defcustom org-disputed-keys
  934. '(([(shift up)] . [(meta p)])
  935. ([(shift down)] . [(meta n)])
  936. ([(shift left)] . [(meta -)])
  937. ([(shift right)] . [(meta +)])
  938. ([(control shift right)] . [(meta shift +)])
  939. ([(control shift left)] . [(meta shift -)]))
  940. "Keys for which Org-mode and other modes compete.
  941. This is an alist, cars are the default keys, second element specifies
  942. the alternative to use when `org-replace-disputed-keys' is t.
  943. Keys can be specified in any syntax supported by `define-key'.
  944. The value of this option takes effect only at Org-mode's startup,
  945. therefore you'll have to restart Emacs to apply it after changing."
  946. :group 'org-startup
  947. :type 'alist)
  948. (defun org-key (key)
  949. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  950. Or return the original if not disputed.
  951. Also apply the translations defined in `org-xemacs-key-equivalents'."
  952. (when org-replace-disputed-keys
  953. (let* ((nkey (key-description key))
  954. (x (org-find-if (lambda (x)
  955. (equal (key-description (car x)) nkey))
  956. org-disputed-keys)))
  957. (setq key (if x (cdr x) key))))
  958. (when (featurep 'xemacs)
  959. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  960. key)
  961. (defun org-find-if (predicate seq)
  962. (catch 'exit
  963. (while seq
  964. (if (funcall predicate (car seq))
  965. (throw 'exit (car seq))
  966. (pop seq)))))
  967. (defun org-defkey (keymap key def)
  968. "Define a key, possibly translated, as returned by `org-key'."
  969. (define-key keymap (org-key key) def))
  970. (defcustom org-ellipsis nil
  971. "The ellipsis to use in the Org-mode outline.
  972. When nil, just use the standard three dots.
  973. When a string, use that string instead.
  974. When a face, use the standard 3 dots, but with the specified face.
  975. The change affects only Org-mode (which will then use its own display table).
  976. Changing this requires executing \\[org-mode] in a buffer to become
  977. effective."
  978. :group 'org-startup
  979. :type '(choice (const :tag "Default" nil)
  980. (face :tag "Face" :value org-warning)
  981. (string :tag "String" :value "...#")))
  982. (defvar org-display-table nil
  983. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  984. (defgroup org-keywords nil
  985. "Keywords in Org-mode."
  986. :tag "Org Keywords"
  987. :group 'org)
  988. (defcustom org-closed-keep-when-no-todo nil
  989. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  990. :group 'org-todo
  991. :group 'org-keywords
  992. :version "24.4"
  993. :package-version '(Org . "8.0")
  994. :type 'boolean)
  995. (defgroup org-structure nil
  996. "Options concerning the general structure of Org-mode files."
  997. :tag "Org Structure"
  998. :group 'org)
  999. (defgroup org-reveal-location nil
  1000. "Options about how to make context of a location visible."
  1001. :tag "Org Reveal Location"
  1002. :group 'org-structure)
  1003. (defcustom org-show-context-detail '((isearch . lineage)
  1004. (bookmark-jump . lineage)
  1005. (default . ancestors))
  1006. "Alist between context and visibility span when revealing a location.
  1007. \\<org-mode-map>Some actions may move point into invisible
  1008. locations. As a consequence, Org always expose a neighborhood
  1009. around point. How much is shown depends on the initial action,
  1010. or context. Valid contexts are
  1011. agenda when exposing an entry from the agenda
  1012. org-goto when using the command `org-goto' (\\[org-goto])
  1013. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1014. tags-tree when constructing a sparse tree based on tags matches
  1015. link-search when exposing search matches associated with a link
  1016. mark-goto when exposing the jump goal of a mark
  1017. bookmark-jump when exposing a bookmark location
  1018. isearch when exiting from an incremental search
  1019. default default for all contexts not set explicitly
  1020. Allowed visibility spans are
  1021. minimal show current headline; if point is not on headline,
  1022. also show entry
  1023. local show current headline, entry and next headline
  1024. ancestors show current headline and its direct ancestors; if
  1025. point is not on headline, also show entry
  1026. lineage show current headline, its direct ancestors and all
  1027. their children; if point is not on headline, also show
  1028. entry and first child
  1029. tree show current headline, its direct ancestors and all
  1030. their children; if point is not on headline, also show
  1031. entry and all children
  1032. canonical show current headline, its direct ancestors along with
  1033. their entries and children; if point is not located on
  1034. the headline, also show current entry and all children
  1035. As special cases, a nil or t value means show all contexts in
  1036. `minimal' or `canonical' view, respectively.
  1037. Some views can make displayed information very compact, but also
  1038. make it harder to edit the location of the match. In such
  1039. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1040. more context."
  1041. :group 'org-reveal-location
  1042. :version "25.1"
  1043. :package-version '(Org . "8.3")
  1044. :type '(choice
  1045. (const :tag "Canonical" t)
  1046. (const :tag "Minimal" nil)
  1047. (repeat :greedy t :tag "Individual contexts"
  1048. (cons
  1049. (choice :tag "Context"
  1050. (const agenda)
  1051. (const org-goto)
  1052. (const occur-tree)
  1053. (const tags-tree)
  1054. (const link-search)
  1055. (const mark-goto)
  1056. (const bookmark-jump)
  1057. (const isearch)
  1058. (const default))
  1059. (choice :tag "Detail level"
  1060. (const minimal)
  1061. (const local)
  1062. (const ancestors)
  1063. (const lineage)
  1064. (const tree)
  1065. (const canonical))))))
  1066. (defcustom org-indirect-buffer-display 'other-window
  1067. "How should indirect tree buffers be displayed?
  1068. This applies to indirect buffers created with the commands
  1069. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1070. Valid values are:
  1071. current-window Display in the current window
  1072. other-window Just display in another window.
  1073. dedicated-frame Create one new frame, and re-use it each time.
  1074. new-frame Make a new frame each time. Note that in this case
  1075. previously-made indirect buffers are kept, and you need to
  1076. kill these buffers yourself."
  1077. :group 'org-structure
  1078. :group 'org-agenda-windows
  1079. :type '(choice
  1080. (const :tag "In current window" current-window)
  1081. (const :tag "In current frame, other window" other-window)
  1082. (const :tag "Each time a new frame" new-frame)
  1083. (const :tag "One dedicated frame" dedicated-frame)))
  1084. (defcustom org-use-speed-commands nil
  1085. "Non-nil means activate single letter commands at beginning of a headline.
  1086. This may also be a function to test for appropriate locations where speed
  1087. commands should be active.
  1088. For example, to activate speed commands when the point is on any
  1089. star at the beginning of the headline, you can do this:
  1090. (setq org-use-speed-commands
  1091. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1092. :group 'org-structure
  1093. :type '(choice
  1094. (const :tag "Never" nil)
  1095. (const :tag "At beginning of headline stars" t)
  1096. (function)))
  1097. (defcustom org-speed-commands-user nil
  1098. "Alist of additional speed commands.
  1099. This list will be checked before `org-speed-commands-default'
  1100. when the variable `org-use-speed-commands' is non-nil
  1101. and when the cursor is at the beginning of a headline.
  1102. The car if each entry is a string with a single letter, which must
  1103. be assigned to `self-insert-command' in the global map.
  1104. The cdr is either a command to be called interactively, a function
  1105. to be called, or a form to be evaluated.
  1106. An entry that is just a list with a single string will be interpreted
  1107. as a descriptive headline that will be added when listing the speed
  1108. commands in the Help buffer using the `?' speed command."
  1109. :group 'org-structure
  1110. :type '(repeat :value ("k" . ignore)
  1111. (choice :value ("k" . ignore)
  1112. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1113. (cons :tag "Letter and Command"
  1114. (string :tag "Command letter")
  1115. (choice
  1116. (function)
  1117. (sexp))))))
  1118. (defcustom org-bookmark-names-plist
  1119. '(:last-capture "org-capture-last-stored"
  1120. :last-refile "org-refile-last-stored"
  1121. :last-capture-marker "org-capture-last-stored-marker")
  1122. "Names for bookmarks automatically set by some Org commands.
  1123. This can provide strings as names for a number of bookmarks Org sets
  1124. automatically. The following keys are currently implemented:
  1125. :last-capture
  1126. :last-capture-marker
  1127. :last-refile
  1128. When a key does not show up in the property list, the corresponding bookmark
  1129. is not set."
  1130. :group 'org-structure
  1131. :type 'plist)
  1132. (defgroup org-cycle nil
  1133. "Options concerning visibility cycling in Org-mode."
  1134. :tag "Org Cycle"
  1135. :group 'org-structure)
  1136. (defcustom org-cycle-skip-children-state-if-no-children t
  1137. "Non-nil means skip CHILDREN state in entries that don't have any."
  1138. :group 'org-cycle
  1139. :type 'boolean)
  1140. (defcustom org-cycle-max-level nil
  1141. "Maximum level which should still be subject to visibility cycling.
  1142. Levels higher than this will, for cycling, be treated as text, not a headline.
  1143. When `org-odd-levels-only' is set, a value of N in this variable actually
  1144. means 2N-1 stars as the limiting headline.
  1145. When nil, cycle all levels.
  1146. Note that the limiting level of cycling is also influenced by
  1147. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1148. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1149. than its value."
  1150. :group 'org-cycle
  1151. :type '(choice
  1152. (const :tag "No limit" nil)
  1153. (integer :tag "Maximum level")))
  1154. (defcustom org-hide-block-startup nil
  1155. "Non-nil means entering Org-mode will fold all blocks.
  1156. This can also be set in on a per-file basis with
  1157. #+STARTUP: hideblocks
  1158. #+STARTUP: showblocks"
  1159. :group 'org-startup
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-global-at-bob nil
  1163. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1164. This makes it possible to do global cycling without having to use S-TAB or
  1165. \\[universal-argument] TAB. For this special case to work, the first line
  1166. of the buffer must not be a headline -- it may be empty or some other text.
  1167. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1168. sure the cursor stays at the beginning of the buffer. When this option is
  1169. nil, don't do anything special at the beginning of the buffer."
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-level-after-item/entry-creation t
  1173. "Non-nil means cycle entry level or item indentation in new empty entries.
  1174. When the cursor is at the end of an empty headline, i.e., with only stars
  1175. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1176. and then all possible ancestor states, before returning to the original state.
  1177. This makes data entry extremely fast: M-RET to create a new headline,
  1178. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1179. When the cursor is at the end of an empty plain list item, one TAB will
  1180. make it a subitem, two or more tabs will back up to make this an item
  1181. higher up in the item hierarchy."
  1182. :group 'org-cycle
  1183. :type 'boolean)
  1184. (defcustom org-cycle-emulate-tab t
  1185. "Where should `org-cycle' emulate TAB.
  1186. nil Never
  1187. white Only in completely white lines
  1188. whitestart Only at the beginning of lines, before the first non-white char
  1189. t Everywhere except in headlines
  1190. exc-hl-bol Everywhere except at the start of a headline
  1191. If TAB is used in a place where it does not emulate TAB, the current subtree
  1192. visibility is cycled."
  1193. :group 'org-cycle
  1194. :type '(choice (const :tag "Never" nil)
  1195. (const :tag "Only in completely white lines" white)
  1196. (const :tag "Before first char in a line" whitestart)
  1197. (const :tag "Everywhere except in headlines" t)
  1198. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1199. (defcustom org-cycle-separator-lines 2
  1200. "Number of empty lines needed to keep an empty line between collapsed trees.
  1201. If you leave an empty line between the end of a subtree and the following
  1202. headline, this empty line is hidden when the subtree is folded.
  1203. Org-mode will leave (exactly) one empty line visible if the number of
  1204. empty lines is equal or larger to the number given in this variable.
  1205. So the default 2 means at least 2 empty lines after the end of a subtree
  1206. are needed to produce free space between a collapsed subtree and the
  1207. following headline.
  1208. If the number is negative, and the number of empty lines is at least -N,
  1209. all empty lines are shown.
  1210. Special case: when 0, never leave empty lines in collapsed view."
  1211. :group 'org-cycle
  1212. :type 'integer)
  1213. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1214. (defcustom org-pre-cycle-hook nil
  1215. "Hook that is run before visibility cycling is happening.
  1216. The function(s) in this hook must accept a single argument which indicates
  1217. the new state that will be set right after running this hook. The
  1218. argument is a symbol. Before a global state change, it can have the values
  1219. `overview', `content', or `all'. Before a local state change, it can have
  1220. the values `folded', `children', or `subtree'."
  1221. :group 'org-cycle
  1222. :type 'hook)
  1223. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1224. org-cycle-hide-drawers
  1225. org-cycle-show-empty-lines
  1226. org-optimize-window-after-visibility-change)
  1227. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1228. The function(s) in this hook must accept a single argument which indicates
  1229. the new state that was set by the most recent `org-cycle' command. The
  1230. argument is a symbol. After a global state change, it can have the values
  1231. `overview', `contents', or `all'. After a local state change, it can have
  1232. the values `folded', `children', or `subtree'."
  1233. :group 'org-cycle
  1234. :type 'hook
  1235. :version "25.1"
  1236. :package-version '(Org . "8.3"))
  1237. (defgroup org-edit-structure nil
  1238. "Options concerning structure editing in Org-mode."
  1239. :tag "Org Edit Structure"
  1240. :group 'org-structure)
  1241. (defcustom org-odd-levels-only nil
  1242. "Non-nil means skip even levels and only use odd levels for the outline.
  1243. This has the effect that two stars are being added/taken away in
  1244. promotion/demotion commands. It also influences how levels are
  1245. handled by the exporters.
  1246. Changing it requires restart of `font-lock-mode' to become effective
  1247. for fontification also in regions already fontified.
  1248. You may also set this on a per-file basis by adding one of the following
  1249. lines to the buffer:
  1250. #+STARTUP: odd
  1251. #+STARTUP: oddeven"
  1252. :group 'org-edit-structure
  1253. :group 'org-appearance
  1254. :type 'boolean)
  1255. (defcustom org-adapt-indentation t
  1256. "Non-nil means adapt indentation to outline node level.
  1257. When this variable is set, Org assumes that you write outlines by
  1258. indenting text in each node to align with the headline (after the
  1259. stars). The following issues are influenced by this variable:
  1260. - The indentation is increased by one space in a demotion
  1261. command, and decreased by one in a promotion command. However,
  1262. in the latter case, if shifting some line in the entry body
  1263. would alter document structure (e.g., insert a new headline),
  1264. indentation is not changed at all.
  1265. - Property drawers and planning information is inserted indented
  1266. when this variable is set. When nil, they will not be indented.
  1267. - TAB indents a line relative to current level. The lines below
  1268. a headline will be indented when this variable is set.
  1269. Note that this is all about true indentation, by adding and
  1270. removing space characters. See also `org-indent.el' which does
  1271. level-dependent indentation in a virtual way, i.e. at display
  1272. time in Emacs."
  1273. :group 'org-edit-structure
  1274. :type 'boolean)
  1275. (defcustom org-special-ctrl-a/e nil
  1276. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1277. When t, `C-a' will bring back the cursor to the beginning of the
  1278. headline text, i.e. after the stars and after a possible TODO
  1279. keyword. In an item, this will be the position after bullet and
  1280. check-box, if any. When the cursor is already at that position,
  1281. another `C-a' will bring it to the beginning of the line.
  1282. `C-e' will jump to the end of the headline, ignoring the presence
  1283. of tags in the headline. A second `C-e' will then jump to the
  1284. true end of the line, after any tags. This also means that, when
  1285. this variable is non-nil, `C-e' also will never jump beyond the
  1286. end of the heading of a folded section, i.e. not after the
  1287. ellipses.
  1288. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1289. normally, going to the true line boundary first. Only a directly
  1290. following, identical keypress will bring the cursor to the
  1291. special positions.
  1292. This may also be a cons cell where the behavior for `C-a' and
  1293. `C-e' is set separately."
  1294. :group 'org-edit-structure
  1295. :type '(choice
  1296. (const :tag "off" nil)
  1297. (const :tag "on: after stars/bullet and before tags first" t)
  1298. (const :tag "reversed: true line boundary first" reversed)
  1299. (cons :tag "Set C-a and C-e separately"
  1300. (choice :tag "Special C-a"
  1301. (const :tag "off" nil)
  1302. (const :tag "on: after stars/bullet first" t)
  1303. (const :tag "reversed: before stars/bullet first" reversed))
  1304. (choice :tag "Special C-e"
  1305. (const :tag "off" nil)
  1306. (const :tag "on: before tags first" t)
  1307. (const :tag "reversed: after tags first" reversed)))))
  1308. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1309. (defcustom org-special-ctrl-k nil
  1310. "Non-nil means `C-k' will behave specially in headlines.
  1311. When nil, `C-k' will call the default `kill-line' command.
  1312. When t, the following will happen while the cursor is in the headline:
  1313. - When the cursor is at the beginning of a headline, kill the entire
  1314. line and possible the folded subtree below the line.
  1315. - When in the middle of the headline text, kill the headline up to the tags.
  1316. - When after the headline text, kill the tags."
  1317. :group 'org-edit-structure
  1318. :type 'boolean)
  1319. (defcustom org-ctrl-k-protect-subtree nil
  1320. "Non-nil means, do not delete a hidden subtree with C-k.
  1321. When set to the symbol `error', simply throw an error when C-k is
  1322. used to kill (part-of) a headline that has hidden text behind it.
  1323. Any other non-nil value will result in a query to the user, if it is
  1324. OK to kill that hidden subtree. When nil, kill without remorse."
  1325. :group 'org-edit-structure
  1326. :version "24.1"
  1327. :type '(choice
  1328. (const :tag "Do not protect hidden subtrees" nil)
  1329. (const :tag "Protect hidden subtrees with a security query" t)
  1330. (const :tag "Never kill a hidden subtree with C-k" error)))
  1331. (defcustom org-special-ctrl-o t
  1332. "Non-nil means, make `C-o' insert a row in tables."
  1333. :group 'org-edit-structure
  1334. :type 'boolean)
  1335. (defcustom org-catch-invisible-edits nil
  1336. "Check if in invisible region before inserting or deleting a character.
  1337. Valid values are:
  1338. nil Do not check, so just do invisible edits.
  1339. error Throw an error and do nothing.
  1340. show Make point visible, and do the requested edit.
  1341. show-and-error Make point visible, then throw an error and abort the edit.
  1342. smart Make point visible, and do insertion/deletion if it is
  1343. adjacent to visible text and the change feels predictable.
  1344. Never delete a previously invisible character or add in the
  1345. middle or right after an invisible region. Basically, this
  1346. allows insertion and backward-delete right before ellipses.
  1347. FIXME: maybe in this case we should not even show?"
  1348. :group 'org-edit-structure
  1349. :version "24.1"
  1350. :type '(choice
  1351. (const :tag "Do not check" nil)
  1352. (const :tag "Throw error when trying to edit" error)
  1353. (const :tag "Unhide, but do not do the edit" show-and-error)
  1354. (const :tag "Show invisible part and do the edit" show)
  1355. (const :tag "Be smart and do the right thing" smart)))
  1356. (defcustom org-yank-folded-subtrees t
  1357. "Non-nil means when yanking subtrees, fold them.
  1358. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1359. it starts with a heading and all other headings in it are either children
  1360. or siblings, then fold all the subtrees. However, do this only if no
  1361. text after the yank would be swallowed into a folded tree by this action."
  1362. :group 'org-edit-structure
  1363. :type 'boolean)
  1364. (defcustom org-yank-adjusted-subtrees nil
  1365. "Non-nil means when yanking subtrees, adjust the level.
  1366. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1367. this function for details."
  1368. :group 'org-edit-structure
  1369. :type 'boolean)
  1370. (defcustom org-M-RET-may-split-line '((default . t))
  1371. "Non-nil means M-RET will split the line at the cursor position.
  1372. When nil, it will go to the end of the line before making a
  1373. new line.
  1374. You may also set this option in a different way for different
  1375. contexts. Valid contexts are:
  1376. headline when creating a new headline
  1377. item when creating a new item
  1378. table in a table field
  1379. default the value to be used for all contexts not explicitly
  1380. customized"
  1381. :group 'org-structure
  1382. :group 'org-table
  1383. :type '(choice
  1384. (const :tag "Always" t)
  1385. (const :tag "Never" nil)
  1386. (repeat :greedy t :tag "Individual contexts"
  1387. (cons
  1388. (choice :tag "Context"
  1389. (const headline)
  1390. (const item)
  1391. (const table)
  1392. (const default))
  1393. (boolean)))))
  1394. (defcustom org-insert-heading-respect-content nil
  1395. "Non-nil means insert new headings after the current subtree.
  1396. When nil, the new heading is created directly after the current line.
  1397. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1398. this variable on for the duration of the command."
  1399. :group 'org-structure
  1400. :type 'boolean)
  1401. (defcustom org-blank-before-new-entry '((heading . auto)
  1402. (plain-list-item . auto))
  1403. "Should `org-insert-heading' leave a blank line before new heading/item?
  1404. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1405. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1406. which case Org will look at the surrounding headings/items and try to
  1407. make an intelligent decision whether to insert a blank line or not.
  1408. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1409. the setting here is ignored and no empty line is inserted to avoid breaking
  1410. the list structure."
  1411. :group 'org-edit-structure
  1412. :type '(list
  1413. (cons (const heading)
  1414. (choice (const :tag "Never" nil)
  1415. (const :tag "Always" t)
  1416. (const :tag "Auto" auto)))
  1417. (cons (const plain-list-item)
  1418. (choice (const :tag "Never" nil)
  1419. (const :tag "Always" t)
  1420. (const :tag "Auto" auto)))))
  1421. (defcustom org-insert-heading-hook nil
  1422. "Hook being run after inserting a new heading."
  1423. :group 'org-edit-structure
  1424. :type 'hook)
  1425. (defcustom org-enable-fixed-width-editor t
  1426. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1427. This currently only means they are never auto-wrapped.
  1428. When nil, such lines will be treated like ordinary lines."
  1429. :group 'org-edit-structure
  1430. :type 'boolean)
  1431. (defcustom org-goto-auto-isearch t
  1432. "Non-nil means typing characters in `org-goto' starts incremental search.
  1433. When nil, you can use these keybindings to navigate the buffer:
  1434. q Quit the org-goto interface
  1435. n Go to the next visible heading
  1436. p Go to the previous visible heading
  1437. f Go one heading forward on same level
  1438. b Go one heading backward on same level
  1439. u Go one heading up"
  1440. :group 'org-edit-structure
  1441. :type 'boolean)
  1442. (defgroup org-sparse-trees nil
  1443. "Options concerning sparse trees in Org-mode."
  1444. :tag "Org Sparse Trees"
  1445. :group 'org-structure)
  1446. (defcustom org-highlight-sparse-tree-matches t
  1447. "Non-nil means highlight all matches that define a sparse tree.
  1448. The highlights will automatically disappear the next time the buffer is
  1449. changed by an edit command."
  1450. :group 'org-sparse-trees
  1451. :type 'boolean)
  1452. (defcustom org-remove-highlights-with-change t
  1453. "Non-nil means any change to the buffer will remove temporary highlights.
  1454. Such highlights are created by `org-occur' and `org-clock-display'.
  1455. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1456. The highlights created by `org-toggle-latex-fragment' always need
  1457. `C-c C-x C-l' to be removed."
  1458. :group 'org-sparse-trees
  1459. :group 'org-time
  1460. :type 'boolean)
  1461. (defcustom org-occur-hook '(org-first-headline-recenter)
  1462. "Hook that is run after `org-occur' has constructed a sparse tree.
  1463. This can be used to recenter the window to show as much of the structure
  1464. as possible."
  1465. :group 'org-sparse-trees
  1466. :type 'hook)
  1467. (defgroup org-imenu-and-speedbar nil
  1468. "Options concerning imenu and speedbar in Org-mode."
  1469. :tag "Org Imenu and Speedbar"
  1470. :group 'org-structure)
  1471. (defcustom org-imenu-depth 2
  1472. "The maximum level for Imenu access to Org-mode headlines.
  1473. This also applied for speedbar access."
  1474. :group 'org-imenu-and-speedbar
  1475. :type 'integer)
  1476. (defgroup org-table nil
  1477. "Options concerning tables in Org-mode."
  1478. :tag "Org Table"
  1479. :group 'org)
  1480. (defcustom org-enable-table-editor 'optimized
  1481. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1482. When nil, such lines will be treated like ordinary lines.
  1483. When equal to the symbol `optimized', the table editor will be optimized to
  1484. do the following:
  1485. - Automatic overwrite mode in front of whitespace in table fields.
  1486. This makes the structure of the table stay in tact as long as the edited
  1487. field does not exceed the column width.
  1488. - Minimize the number of realigns. Normally, the table is aligned each time
  1489. TAB or RET are pressed to move to another field. With optimization this
  1490. happens only if changes to a field might have changed the column width.
  1491. Optimization requires replacing the functions `self-insert-command',
  1492. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1493. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1494. very good at guessing when a re-align will be necessary, but you can always
  1495. force one with \\[org-ctrl-c-ctrl-c].
  1496. If you would like to use the optimized version in Org-mode, but the
  1497. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1498. This variable can be used to turn on and off the table editor during a session,
  1499. but in order to toggle optimization, a restart is required.
  1500. See also the variable `org-table-auto-blank-field'."
  1501. :group 'org-table
  1502. :type '(choice
  1503. (const :tag "off" nil)
  1504. (const :tag "on" t)
  1505. (const :tag "on, optimized" optimized)))
  1506. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1507. (version<= emacs-version "24.1"))
  1508. "Non-nil means cluster self-insert commands for undo when possible.
  1509. If this is set, then, like in the Emacs command loop, 20 consecutive
  1510. characters will be undone together.
  1511. This is configurable, because there is some impact on typing performance."
  1512. :group 'org-table
  1513. :type 'boolean)
  1514. (defcustom org-table-tab-recognizes-table.el t
  1515. "Non-nil means TAB will automatically notice a table.el table.
  1516. When it sees such a table, it moves point into it and - if necessary -
  1517. calls `table-recognize-table'."
  1518. :group 'org-table-editing
  1519. :type 'boolean)
  1520. (defgroup org-link nil
  1521. "Options concerning links in Org-mode."
  1522. :tag "Org Link"
  1523. :group 'org)
  1524. (defvar org-link-abbrev-alist-local nil
  1525. "Buffer-local version of `org-link-abbrev-alist', which see.
  1526. The value of this is taken from the #+LINK lines.")
  1527. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1528. (defcustom org-link-abbrev-alist nil
  1529. "Alist of link abbreviations.
  1530. The car of each element is a string, to be replaced at the start of a link.
  1531. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1532. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1533. [[linkkey:tag][description]]
  1534. The 'linkkey' must be a word word, starting with a letter, followed
  1535. by letters, numbers, '-' or '_'.
  1536. If REPLACE is a string, the tag will simply be appended to create the link.
  1537. If the string contains \"%s\", the tag will be inserted there. If the string
  1538. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1539. at that point (see the function `url-hexify-string'). If the string contains
  1540. the specifier \"%(my-function)\", then the custom function `my-function' will
  1541. be invoked: this function takes the tag as its only argument and must return
  1542. a string.
  1543. REPLACE may also be a function that will be called with the tag as the
  1544. only argument to create the link, which should be returned as a string.
  1545. See the manual for examples."
  1546. :group 'org-link
  1547. :type '(repeat
  1548. (cons
  1549. (string :tag "Protocol")
  1550. (choice
  1551. (string :tag "Format")
  1552. (function)))))
  1553. (defcustom org-descriptive-links t
  1554. "Non-nil means Org will display descriptive links.
  1555. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1556. \"Org Website\", hiding the link itself and just displaying its
  1557. description. When set to nil, Org will display the full links
  1558. literally.
  1559. You can interactively set the value of this variable by calling
  1560. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1561. :group 'org-link
  1562. :type 'boolean)
  1563. (defcustom org-link-file-path-type 'adaptive
  1564. "How the path name in file links should be stored.
  1565. Valid values are:
  1566. relative Relative to the current directory, i.e. the directory of the file
  1567. into which the link is being inserted.
  1568. absolute Absolute path, if possible with ~ for home directory.
  1569. noabbrev Absolute path, no abbreviation of home directory.
  1570. adaptive Use relative path for files in the current directory and sub-
  1571. directories of it. For other files, use an absolute path."
  1572. :group 'org-link
  1573. :type '(choice
  1574. (const relative)
  1575. (const absolute)
  1576. (const noabbrev)
  1577. (const adaptive)))
  1578. (defvaralias 'org-activate-links 'org-highlight-links)
  1579. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1580. "Types of links that should be highlighted in Org-mode files.
  1581. This is a list of symbols, each one of them leading to the
  1582. highlighting of a certain link type.
  1583. You can still open links that are not highlighted.
  1584. In principle, it does not hurt to turn on highlighting for all
  1585. link types. There may be a small gain when turning off unused
  1586. link types. The types are:
  1587. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1588. angle Links in angular brackets that may contain whitespace like
  1589. <bbdb:Carsten Dominik>.
  1590. plain Plain links in normal text, no whitespace, like http://google.com.
  1591. radio Text that is matched by a radio target, see manual for details.
  1592. tag Tag settings in a headline (link to tag search).
  1593. date Time stamps (link to calendar).
  1594. footnote Footnote labels.
  1595. If you set this variable during an Emacs session, use `org-mode-restart'
  1596. in the Org buffer so that the change takes effect."
  1597. :group 'org-link
  1598. :group 'org-appearance
  1599. :type '(set :greedy t
  1600. (const :tag "Double bracket links" bracket)
  1601. (const :tag "Angular bracket links" angle)
  1602. (const :tag "Plain text links" plain)
  1603. (const :tag "Radio target matches" radio)
  1604. (const :tag "Tags" tag)
  1605. (const :tag "Timestamps" date)
  1606. (const :tag "Footnotes" footnote)))
  1607. (defcustom org-make-link-description-function nil
  1608. "Function to use for generating link descriptions from links.
  1609. When nil, the link location will be used. This function must take
  1610. two parameters: the first one is the link, the second one is the
  1611. description generated by `org-insert-link'. The function should
  1612. return the description to use."
  1613. :group 'org-link
  1614. :type '(choice (const nil) (function)))
  1615. (defgroup org-link-store nil
  1616. "Options concerning storing links in Org-mode."
  1617. :tag "Org Store Link"
  1618. :group 'org-link)
  1619. (defcustom org-url-hexify-p t
  1620. "When non-nil, hexify URL when creating a link."
  1621. :type 'boolean
  1622. :version "24.3"
  1623. :group 'org-link-store)
  1624. (defcustom org-email-link-description-format "Email %c: %.30s"
  1625. "Format of the description part of a link to an email or usenet message.
  1626. The following %-escapes will be replaced by corresponding information:
  1627. %F full \"From\" field
  1628. %f name, taken from \"From\" field, address if no name
  1629. %T full \"To\" field
  1630. %t first name in \"To\" field, address if no name
  1631. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1632. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1633. %s subject
  1634. %d date
  1635. %m message-id.
  1636. You may use normal field width specification between the % and the letter.
  1637. This is for example useful to limit the length of the subject.
  1638. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1639. :group 'org-link-store
  1640. :type 'string)
  1641. (defcustom org-from-is-user-regexp
  1642. (let (r1 r2)
  1643. (when (and user-mail-address (not (string= user-mail-address "")))
  1644. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1645. (when (and user-full-name (not (string= user-full-name "")))
  1646. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1647. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1648. "Regexp matched against the \"From:\" header of an email or usenet message.
  1649. It should match if the message is from the user him/herself."
  1650. :group 'org-link-store
  1651. :type 'regexp)
  1652. (defcustom org-context-in-file-links t
  1653. "Non-nil means file links from `org-store-link' contain context.
  1654. A search string will be added to the file name with :: as separator and
  1655. used to find the context when the link is activated by the command
  1656. `org-open-at-point'. When this option is t, the entire active region
  1657. will be placed in the search string of the file link. If set to a
  1658. positive integer, only the first n lines of context will be stored.
  1659. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1660. negates this setting for the duration of the command."
  1661. :group 'org-link-store
  1662. :type '(choice boolean integer))
  1663. (defcustom org-keep-stored-link-after-insertion nil
  1664. "Non-nil means keep link in list for entire session.
  1665. The command `org-store-link' adds a link pointing to the current
  1666. location to an internal list. These links accumulate during a session.
  1667. The command `org-insert-link' can be used to insert links into any
  1668. Org-mode file (offering completion for all stored links). When this
  1669. option is nil, every link which has been inserted once using \\[org-insert-link]
  1670. will be removed from the list, to make completing the unused links
  1671. more efficient."
  1672. :group 'org-link-store
  1673. :type 'boolean)
  1674. (defgroup org-link-follow nil
  1675. "Options concerning following links in Org-mode."
  1676. :tag "Org Follow Link"
  1677. :group 'org-link)
  1678. (defcustom org-link-translation-function nil
  1679. "Function to translate links with different syntax to Org syntax.
  1680. This can be used to translate links created for example by the Planner
  1681. or emacs-wiki packages to Org syntax.
  1682. The function must accept two parameters, a TYPE containing the link
  1683. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1684. which is everything after the link protocol. It should return a cons
  1685. with possibly modified values of type and path.
  1686. Org contains a function for this, so if you set this variable to
  1687. `org-translate-link-from-planner', you should be able follow many
  1688. links created by planner."
  1689. :group 'org-link-follow
  1690. :type '(choice (const nil) (function)))
  1691. (defcustom org-follow-link-hook nil
  1692. "Hook that is run after a link has been followed."
  1693. :group 'org-link-follow
  1694. :type 'hook)
  1695. (defcustom org-tab-follows-link nil
  1696. "Non-nil means on links TAB will follow the link.
  1697. Needs to be set before org.el is loaded.
  1698. This really should not be used, it does not make sense, and the
  1699. implementation is bad."
  1700. :group 'org-link-follow
  1701. :type 'boolean)
  1702. (defcustom org-return-follows-link nil
  1703. "Non-nil means on links RET will follow the link.
  1704. In tables, the special behavior of RET has precedence."
  1705. :group 'org-link-follow
  1706. :type 'boolean)
  1707. (defcustom org-mouse-1-follows-link
  1708. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1709. "Non-nil means mouse-1 on a link will follow the link.
  1710. A longer mouse click will still set point. Does not work on XEmacs.
  1711. Needs to be set before org.el is loaded."
  1712. :group 'org-link-follow
  1713. :version "24.4"
  1714. :package-version '(Org . "8.3")
  1715. :type '(choice
  1716. (const :tag "A double click follows the link" double)
  1717. (const :tag "Unconditionally follow the link with mouse-1" t)
  1718. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1719. (defcustom org-mark-ring-length 4
  1720. "Number of different positions to be recorded in the ring.
  1721. Changing this requires a restart of Emacs to work correctly."
  1722. :group 'org-link-follow
  1723. :type 'integer)
  1724. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1725. "Non-nil means internal links in Org files must exactly match a headline.
  1726. When nil, the link search tries to match a phrase with all words
  1727. in the search text."
  1728. :group 'org-link-follow
  1729. :version "24.1"
  1730. :type '(choice
  1731. (const :tag "Use fuzzy text search" nil)
  1732. (const :tag "Match only exact headline" t)
  1733. (const :tag "Match exact headline or query to create it"
  1734. query-to-create)))
  1735. (defcustom org-link-frame-setup
  1736. '((vm . vm-visit-folder-other-frame)
  1737. (vm-imap . vm-visit-imap-folder-other-frame)
  1738. (gnus . org-gnus-no-new-news)
  1739. (file . find-file-other-window)
  1740. (wl . wl-other-frame))
  1741. "Setup the frame configuration for following links.
  1742. When following a link with Emacs, it may often be useful to display
  1743. this link in another window or frame. This variable can be used to
  1744. set this up for the different types of links.
  1745. For VM, use any of
  1746. `vm-visit-folder'
  1747. `vm-visit-folder-other-window'
  1748. `vm-visit-folder-other-frame'
  1749. For Gnus, use any of
  1750. `gnus'
  1751. `gnus-other-frame'
  1752. `org-gnus-no-new-news'
  1753. For FILE, use any of
  1754. `find-file'
  1755. `find-file-other-window'
  1756. `find-file-other-frame'
  1757. For Wanderlust use any of
  1758. `wl'
  1759. `wl-other-frame'
  1760. For the calendar, use the variable `calendar-setup'.
  1761. For BBDB, it is currently only possible to display the matches in
  1762. another window."
  1763. :group 'org-link-follow
  1764. :type '(list
  1765. (cons (const vm)
  1766. (choice
  1767. (const vm-visit-folder)
  1768. (const vm-visit-folder-other-window)
  1769. (const vm-visit-folder-other-frame)))
  1770. (cons (const vm-imap)
  1771. (choice
  1772. (const vm-visit-imap-folder)
  1773. (const vm-visit-imap-folder-other-window)
  1774. (const vm-visit-imap-folder-other-frame)))
  1775. (cons (const gnus)
  1776. (choice
  1777. (const gnus)
  1778. (const gnus-other-frame)
  1779. (const org-gnus-no-new-news)))
  1780. (cons (const file)
  1781. (choice
  1782. (const find-file)
  1783. (const find-file-other-window)
  1784. (const find-file-other-frame)))
  1785. (cons (const wl)
  1786. (choice
  1787. (const wl)
  1788. (const wl-other-frame)))))
  1789. (defcustom org-display-internal-link-with-indirect-buffer nil
  1790. "Non-nil means use indirect buffer to display infile links.
  1791. Activating internal links (from one location in a file to another location
  1792. in the same file) normally just jumps to the location. When the link is
  1793. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1794. is displayed in
  1795. another window. When this option is set, the other window actually displays
  1796. an indirect buffer clone of the current buffer, to avoid any visibility
  1797. changes to the current buffer."
  1798. :group 'org-link-follow
  1799. :type 'boolean)
  1800. (defcustom org-open-non-existing-files nil
  1801. "Non-nil means `org-open-file' will open non-existing files.
  1802. When nil, an error will be generated.
  1803. This variable applies only to external applications because they
  1804. might choke on non-existing files. If the link is to a file that
  1805. will be opened in Emacs, the variable is ignored."
  1806. :group 'org-link-follow
  1807. :type 'boolean)
  1808. (defcustom org-open-directory-means-index-dot-org nil
  1809. "Non-nil means a link to a directory really means to index.org.
  1810. When nil, following a directory link will run dired or open a finder/explorer
  1811. window on that directory."
  1812. :group 'org-link-follow
  1813. :type 'boolean)
  1814. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1815. "Non-nil means ask for confirmation before executing shell links.
  1816. Shell links can be dangerous: just think about a link
  1817. [[shell:rm -rf ~/*][Google Search]]
  1818. This link would show up in your Org-mode document as \"Google Search\",
  1819. but really it would remove your entire home directory.
  1820. Therefore we advise against setting this variable to nil.
  1821. Just change it to `y-or-n-p' if you want to confirm with a
  1822. single keystroke rather than having to type \"yes\"."
  1823. :group 'org-link-follow
  1824. :type '(choice
  1825. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1826. (const :tag "with y-or-n (faster)" y-or-n-p)
  1827. (const :tag "no confirmation (dangerous)" nil)))
  1828. (put 'org-confirm-shell-link-function
  1829. 'safe-local-variable
  1830. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1831. (defcustom org-confirm-shell-link-not-regexp ""
  1832. "A regexp to skip confirmation for shell links."
  1833. :group 'org-link-follow
  1834. :version "24.1"
  1835. :type 'regexp)
  1836. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1837. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1838. Elisp links can be dangerous: just think about a link
  1839. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1840. This link would show up in your Org-mode document as \"Google Search\",
  1841. but really it would remove your entire home directory.
  1842. Therefore we advise against setting this variable to nil.
  1843. Just change it to `y-or-n-p' if you want to confirm with a
  1844. single keystroke rather than having to type \"yes\"."
  1845. :group 'org-link-follow
  1846. :type '(choice
  1847. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1848. (const :tag "with y-or-n (faster)" y-or-n-p)
  1849. (const :tag "no confirmation (dangerous)" nil)))
  1850. (put 'org-confirm-shell-link-function
  1851. 'safe-local-variable
  1852. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1853. (defcustom org-confirm-elisp-link-not-regexp ""
  1854. "A regexp to skip confirmation for Elisp links."
  1855. :group 'org-link-follow
  1856. :version "24.1"
  1857. :type 'regexp)
  1858. (defconst org-file-apps-defaults-gnu
  1859. '((remote . emacs)
  1860. (system . mailcap)
  1861. (t . mailcap))
  1862. "Default file applications on a UNIX or GNU/Linux system.
  1863. See `org-file-apps'.")
  1864. (defconst org-file-apps-defaults-macosx
  1865. '((remote . emacs)
  1866. (t . "open %s")
  1867. (system . "open %s")
  1868. ("ps.gz" . "gv %s")
  1869. ("eps.gz" . "gv %s")
  1870. ("dvi" . "xdvi %s")
  1871. ("fig" . "xfig %s"))
  1872. "Default file applications on a MacOS X system.
  1873. The system \"open\" is known as a default, but we use X11 applications
  1874. for some files for which the OS does not have a good default.
  1875. See `org-file-apps'.")
  1876. (defconst org-file-apps-defaults-windowsnt
  1877. (list
  1878. '(remote . emacs)
  1879. (cons t
  1880. (list (if (featurep 'xemacs)
  1881. 'mswindows-shell-execute
  1882. 'w32-shell-execute)
  1883. "open" 'file))
  1884. (cons 'system
  1885. (list (if (featurep 'xemacs)
  1886. 'mswindows-shell-execute
  1887. 'w32-shell-execute)
  1888. "open" 'file)))
  1889. "Default file applications on a Windows NT system.
  1890. The system \"open\" is used for most files.
  1891. See `org-file-apps'.")
  1892. (defcustom org-file-apps
  1893. '((auto-mode . emacs)
  1894. ("\\.mm\\'" . default)
  1895. ("\\.x?html?\\'" . default)
  1896. ("\\.pdf\\'" . default))
  1897. "External applications for opening `file:path' items in a document.
  1898. Org-mode uses system defaults for different file types, but
  1899. you can use this variable to set the application for a given file
  1900. extension. The entries in this list are cons cells where the car identifies
  1901. files and the cdr the corresponding command. Possible values for the
  1902. file identifier are
  1903. \"string\" A string as a file identifier can be interpreted in different
  1904. ways, depending on its contents:
  1905. - Alphanumeric characters only:
  1906. Match links with this file extension.
  1907. Example: (\"pdf\" . \"evince %s\")
  1908. to open PDFs with evince.
  1909. - Regular expression: Match links where the
  1910. filename matches the regexp. If you want to
  1911. use groups here, use shy groups.
  1912. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1913. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1914. to open *.html and *.xhtml with firefox.
  1915. - Regular expression which contains (non-shy) groups:
  1916. Match links where the whole link, including \"::\", and
  1917. anything after that, matches the regexp.
  1918. In a custom command string, %1, %2, etc. are replaced with
  1919. the parts of the link that were matched by the groups.
  1920. For backwards compatibility, if a command string is given
  1921. that does not use any of the group matches, this case is
  1922. handled identically to the second one (i.e. match against
  1923. file name only).
  1924. In a custom lisp form, you can access the group matches with
  1925. (match-string n link).
  1926. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1927. to open [[file:document.pdf::5]] with evince at page 5.
  1928. `directory' Matches a directory
  1929. `remote' Matches a remote file, accessible through tramp or efs.
  1930. Remote files most likely should be visited through Emacs
  1931. because external applications cannot handle such paths.
  1932. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1933. so all files Emacs knows how to handle. Using this with
  1934. command `emacs' will open most files in Emacs. Beware that this
  1935. will also open html files inside Emacs, unless you add
  1936. (\"html\" . default) to the list as well.
  1937. t Default for files not matched by any of the other options.
  1938. `system' The system command to open files, like `open' on Windows
  1939. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1940. that will be selected if you call `C-c C-o' with a double
  1941. \\[universal-argument] \\[universal-argument] prefix.
  1942. Possible values for the command are:
  1943. `emacs' The file will be visited by the current Emacs process.
  1944. `default' Use the default application for this file type, which is the
  1945. association for t in the list, most likely in the system-specific
  1946. part.
  1947. This can be used to overrule an unwanted setting in the
  1948. system-specific variable.
  1949. `system' Use the system command for opening files, like \"open\".
  1950. This command is specified by the entry whose car is `system'.
  1951. Most likely, the system-specific version of this variable
  1952. does define this command, but you can overrule/replace it
  1953. here.
  1954. string A command to be executed by a shell; %s will be replaced
  1955. by the path to the file.
  1956. sexp A Lisp form which will be evaluated. The file path will
  1957. be available in the Lisp variable `file'.
  1958. For more examples, see the system specific constants
  1959. `org-file-apps-defaults-macosx'
  1960. `org-file-apps-defaults-windowsnt'
  1961. `org-file-apps-defaults-gnu'."
  1962. :group 'org-link-follow
  1963. :type '(repeat
  1964. (cons (choice :value ""
  1965. (string :tag "Extension")
  1966. (const :tag "System command to open files" system)
  1967. (const :tag "Default for unrecognized files" t)
  1968. (const :tag "Remote file" remote)
  1969. (const :tag "Links to a directory" directory)
  1970. (const :tag "Any files that have Emacs modes"
  1971. auto-mode))
  1972. (choice :value ""
  1973. (const :tag "Visit with Emacs" emacs)
  1974. (const :tag "Use default" default)
  1975. (const :tag "Use the system command" system)
  1976. (string :tag "Command")
  1977. (sexp :tag "Lisp form")))))
  1978. (defcustom org-doi-server-url "http://dx.doi.org/"
  1979. "The URL of the DOI server."
  1980. :type 'string
  1981. :version "24.3"
  1982. :group 'org-link-follow)
  1983. (defgroup org-refile nil
  1984. "Options concerning refiling entries in Org-mode."
  1985. :tag "Org Refile"
  1986. :group 'org)
  1987. (defcustom org-directory "~/org"
  1988. "Directory with org files.
  1989. This is just a default location to look for Org files. There is no need
  1990. at all to put your files into this directory. It is only used in the
  1991. following situations:
  1992. 1. When a capture template specifies a target file that is not an
  1993. absolute path. The path will then be interpreted relative to
  1994. `org-directory'
  1995. 2. When a capture note is filed away in an interactive way (when exiting the
  1996. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1997. with `org-directory' as the default path."
  1998. :group 'org-refile
  1999. :group 'org-capture
  2000. :type 'directory)
  2001. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2002. "Default target for storing notes.
  2003. Used as a fall back file for org-capture.el, for templates that
  2004. do not specify a target file."
  2005. :group 'org-refile
  2006. :group 'org-capture
  2007. :type 'file)
  2008. (defcustom org-goto-interface 'outline
  2009. "The default interface to be used for `org-goto'.
  2010. Allowed values are:
  2011. outline The interface shows an outline of the relevant file
  2012. and the correct heading is found by moving through
  2013. the outline or by searching with incremental search.
  2014. outline-path-completion Headlines in the current buffer are offered via
  2015. completion. This is the interface also used by
  2016. the refile command."
  2017. :group 'org-refile
  2018. :type '(choice
  2019. (const :tag "Outline" outline)
  2020. (const :tag "Outline-path-completion" outline-path-completion)))
  2021. (defcustom org-goto-max-level 5
  2022. "Maximum target level when running `org-goto' with refile interface."
  2023. :group 'org-refile
  2024. :type 'integer)
  2025. (defcustom org-reverse-note-order nil
  2026. "Non-nil means store new notes at the beginning of a file or entry.
  2027. When nil, new notes will be filed to the end of a file or entry.
  2028. This can also be a list with cons cells of regular expressions that
  2029. are matched against file names, and values."
  2030. :group 'org-capture
  2031. :group 'org-refile
  2032. :type '(choice
  2033. (const :tag "Reverse always" t)
  2034. (const :tag "Reverse never" nil)
  2035. (repeat :tag "By file name regexp"
  2036. (cons regexp boolean))))
  2037. (defcustom org-log-refile nil
  2038. "Information to record when a task is refiled.
  2039. Possible values are:
  2040. nil Don't add anything
  2041. time Add a time stamp to the task
  2042. note Prompt for a note and add it with template `org-log-note-headings'
  2043. This option can also be set with on a per-file-basis with
  2044. #+STARTUP: nologrefile
  2045. #+STARTUP: logrefile
  2046. #+STARTUP: lognoterefile
  2047. You can have local logging settings for a subtree by setting the LOGGING
  2048. property to one or more of these keywords.
  2049. When bulk-refiling from the agenda, the value `note' is forbidden and
  2050. will temporarily be changed to `time'."
  2051. :group 'org-refile
  2052. :group 'org-progress
  2053. :version "24.1"
  2054. :type '(choice
  2055. (const :tag "No logging" nil)
  2056. (const :tag "Record timestamp" time)
  2057. (const :tag "Record timestamp with note." note)))
  2058. (defcustom org-refile-targets nil
  2059. "Targets for refiling entries with \\[org-refile].
  2060. This is a list of cons cells. Each cell contains:
  2061. - a specification of the files to be considered, either a list of files,
  2062. or a symbol whose function or variable value will be used to retrieve
  2063. a file name or a list of file names. If you use `org-agenda-files' for
  2064. that, all agenda files will be scanned for targets. Nil means consider
  2065. headings in the current buffer.
  2066. - A specification of how to find candidate refile targets. This may be
  2067. any of:
  2068. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2069. This tag has to be present in all target headlines, inheritance will
  2070. not be considered.
  2071. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2072. todo keyword.
  2073. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2074. headlines that are refiling targets.
  2075. - a cons cell (:level . N). Any headline of level N is considered a target.
  2076. Note that, when `org-odd-levels-only' is set, level corresponds to
  2077. order in hierarchy, not to the number of stars.
  2078. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2079. Note that, when `org-odd-levels-only' is set, level corresponds to
  2080. order in hierarchy, not to the number of stars.
  2081. Each element of this list generates a set of possible targets.
  2082. The union of these sets is presented (with completion) to
  2083. the user by `org-refile'.
  2084. You can set the variable `org-refile-target-verify-function' to a function
  2085. to verify each headline found by the simple criteria above.
  2086. When this variable is nil, all top-level headlines in the current buffer
  2087. are used, equivalent to the value `((nil . (:level . 1))'."
  2088. :group 'org-refile
  2089. :type '(repeat
  2090. (cons
  2091. (choice :value org-agenda-files
  2092. (const :tag "All agenda files" org-agenda-files)
  2093. (const :tag "Current buffer" nil)
  2094. (function) (variable) (file))
  2095. (choice :tag "Identify target headline by"
  2096. (cons :tag "Specific tag" (const :value :tag) (string))
  2097. (cons :tag "TODO keyword" (const :value :todo) (string))
  2098. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2099. (cons :tag "Level number" (const :value :level) (integer))
  2100. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2101. (defcustom org-refile-target-verify-function nil
  2102. "Function to verify if the headline at point should be a refile target.
  2103. The function will be called without arguments, with point at the
  2104. beginning of the headline. It should return t and leave point
  2105. where it is if the headline is a valid target for refiling.
  2106. If the target should not be selected, the function must return nil.
  2107. In addition to this, it may move point to a place from where the search
  2108. should be continued. For example, the function may decide that the entire
  2109. subtree of the current entry should be excluded and move point to the end
  2110. of the subtree."
  2111. :group 'org-refile
  2112. :type '(choice
  2113. (const nil)
  2114. (function)))
  2115. (defcustom org-refile-use-cache nil
  2116. "Non-nil means cache refile targets to speed up the process.
  2117. The cache for a particular file will be updated automatically when
  2118. the buffer has been killed, or when any of the marker used for flagging
  2119. refile targets no longer points at a live buffer.
  2120. If you have added new entries to a buffer that might themselves be targets,
  2121. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2122. find that easier, `C-u C-u C-u C-c C-w'."
  2123. :group 'org-refile
  2124. :version "24.1"
  2125. :type 'boolean)
  2126. (defcustom org-refile-use-outline-path nil
  2127. "Non-nil means provide refile targets as paths.
  2128. So a level 3 headline will be available as level1/level2/level3.
  2129. When the value is `file', also include the file name (without directory)
  2130. into the path. In this case, you can also stop the completion after
  2131. the file name, to get entries inserted as top level in the file.
  2132. When `full-file-path', include the full file path."
  2133. :group 'org-refile
  2134. :type '(choice
  2135. (const :tag "Not" nil)
  2136. (const :tag "Yes" t)
  2137. (const :tag "Start with file name" file)
  2138. (const :tag "Start with full file path" full-file-path)))
  2139. (defcustom org-outline-path-complete-in-steps t
  2140. "Non-nil means complete the outline path in hierarchical steps.
  2141. When Org-mode uses the refile interface to select an outline path
  2142. \(see variable `org-refile-use-outline-path'), the completion of
  2143. the path can be done in a single go, or it can be done in steps down
  2144. the headline hierarchy. Going in steps is probably the best if you
  2145. do not use a special completion package like `ido' or `icicles'.
  2146. However, when using these packages, going in one step can be very
  2147. fast, while still showing the whole path to the entry."
  2148. :group 'org-refile
  2149. :type 'boolean)
  2150. (defcustom org-refile-allow-creating-parent-nodes nil
  2151. "Non-nil means allow to create new nodes as refile targets.
  2152. New nodes are then created by adding \"/new node name\" to the completion
  2153. of an existing node. When the value of this variable is `confirm',
  2154. new node creation must be confirmed by the user (recommended).
  2155. When nil, the completion must match an existing entry.
  2156. Note that, if the new heading is not seen by the criteria
  2157. listed in `org-refile-targets', multiple instances of the same
  2158. heading would be created by trying again to file under the new
  2159. heading."
  2160. :group 'org-refile
  2161. :type '(choice
  2162. (const :tag "Never" nil)
  2163. (const :tag "Always" t)
  2164. (const :tag "Prompt for confirmation" confirm)))
  2165. (defcustom org-refile-active-region-within-subtree nil
  2166. "Non-nil means also refile active region within a subtree.
  2167. By default `org-refile' doesn't allow refiling regions if they
  2168. don't contain a set of subtrees, but it might be convenient to
  2169. do so sometimes: in that case, the first line of the region is
  2170. converted to a headline before refiling."
  2171. :group 'org-refile
  2172. :version "24.1"
  2173. :type 'boolean)
  2174. (defgroup org-todo nil
  2175. "Options concerning TODO items in Org-mode."
  2176. :tag "Org TODO"
  2177. :group 'org)
  2178. (defgroup org-progress nil
  2179. "Options concerning Progress logging in Org-mode."
  2180. :tag "Org Progress"
  2181. :group 'org-time)
  2182. (defvar org-todo-interpretation-widgets
  2183. '((:tag "Sequence (cycling hits every state)" sequence)
  2184. (:tag "Type (cycling directly to DONE)" type))
  2185. "The available interpretation symbols for customizing `org-todo-keywords'.
  2186. Interested libraries should add to this list.")
  2187. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2188. "List of TODO entry keyword sequences and their interpretation.
  2189. \\<org-mode-map>This is a list of sequences.
  2190. Each sequence starts with a symbol, either `sequence' or `type',
  2191. indicating if the keywords should be interpreted as a sequence of
  2192. action steps, or as different types of TODO items. The first
  2193. keywords are states requiring action - these states will select a headline
  2194. for inclusion into the global TODO list Org-mode produces. If one of
  2195. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2196. signify that no further action is necessary. If \"|\" is not found,
  2197. the last keyword is treated as the only DONE state of the sequence.
  2198. The command \\[org-todo] cycles an entry through these states, and one
  2199. additional state where no keyword is present. For details about this
  2200. cycling, see the manual.
  2201. TODO keywords and interpretation can also be set on a per-file basis with
  2202. the special #+SEQ_TODO and #+TYP_TODO lines.
  2203. Each keyword can optionally specify a character for fast state selection
  2204. \(in combination with the variable `org-use-fast-todo-selection')
  2205. and specifiers for state change logging, using the same syntax that
  2206. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2207. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2208. indicates to record a time stamp each time this state is selected.
  2209. Each keyword may also specify if a timestamp or a note should be
  2210. recorded when entering or leaving the state, by adding additional
  2211. characters in the parenthesis after the keyword. This looks like this:
  2212. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2213. record only the time of the state change. With X and Y being either
  2214. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2215. Y when leaving the state if and only if the *target* state does not
  2216. define X. You may omit any of the fast-selection key or X or /Y,
  2217. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2218. For backward compatibility, this variable may also be just a list
  2219. of keywords. In this case the interpretation (sequence or type) will be
  2220. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2221. :group 'org-todo
  2222. :group 'org-keywords
  2223. :type '(choice
  2224. (repeat :tag "Old syntax, just keywords"
  2225. (string :tag "Keyword"))
  2226. (repeat :tag "New syntax"
  2227. (cons
  2228. (choice
  2229. :tag "Interpretation"
  2230. ;;Quick and dirty way to see
  2231. ;;`org-todo-interpretations'. This takes the
  2232. ;;place of item arguments
  2233. :convert-widget
  2234. (lambda (widget)
  2235. (widget-put widget
  2236. :args (mapcar
  2237. (lambda (x)
  2238. (widget-convert
  2239. (cons 'const x)))
  2240. org-todo-interpretation-widgets))
  2241. widget))
  2242. (repeat
  2243. (string :tag "Keyword"))))))
  2244. (defvar org-todo-keywords-1 nil
  2245. "All TODO and DONE keywords active in a buffer.")
  2246. (make-variable-buffer-local 'org-todo-keywords-1)
  2247. (defvar org-todo-keywords-for-agenda nil)
  2248. (defvar org-done-keywords-for-agenda nil)
  2249. (defvar org-todo-keyword-alist-for-agenda nil)
  2250. (defvar org-tag-alist-for-agenda nil
  2251. "Alist of all tags from all agenda files.")
  2252. (defvar org-tag-groups-alist-for-agenda nil
  2253. "Alist of all groups tags from all current agenda files.")
  2254. (defvar org-tag-groups-alist nil)
  2255. (make-variable-buffer-local 'org-tag-groups-alist)
  2256. (defvar org-agenda-contributing-files nil)
  2257. (defvar org-not-done-keywords nil)
  2258. (make-variable-buffer-local 'org-not-done-keywords)
  2259. (defvar org-done-keywords nil)
  2260. (make-variable-buffer-local 'org-done-keywords)
  2261. (defvar org-todo-heads nil)
  2262. (make-variable-buffer-local 'org-todo-heads)
  2263. (defvar org-todo-sets nil)
  2264. (make-variable-buffer-local 'org-todo-sets)
  2265. (defvar org-todo-log-states nil)
  2266. (make-variable-buffer-local 'org-todo-log-states)
  2267. (defvar org-todo-kwd-alist nil)
  2268. (make-variable-buffer-local 'org-todo-kwd-alist)
  2269. (defvar org-todo-key-alist nil)
  2270. (make-variable-buffer-local 'org-todo-key-alist)
  2271. (defvar org-todo-key-trigger nil)
  2272. (make-variable-buffer-local 'org-todo-key-trigger)
  2273. (defcustom org-todo-interpretation 'sequence
  2274. "Controls how TODO keywords are interpreted.
  2275. This variable is in principle obsolete and is only used for
  2276. backward compatibility, if the interpretation of todo keywords is
  2277. not given already in `org-todo-keywords'. See that variable for
  2278. more information."
  2279. :group 'org-todo
  2280. :group 'org-keywords
  2281. :type '(choice (const sequence)
  2282. (const type)))
  2283. (defcustom org-use-fast-todo-selection t
  2284. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2285. This variable describes if and under what circumstances the cycling
  2286. mechanism for TODO keywords will be replaced by a single-key, direct
  2287. selection scheme.
  2288. When nil, fast selection is never used.
  2289. When the symbol `prefix', it will be used when `org-todo' is called
  2290. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2291. `C-u t' in an agenda buffer.
  2292. When t, fast selection is used by default. In this case, the prefix
  2293. argument forces cycling instead.
  2294. In all cases, the special interface is only used if access keys have
  2295. actually been assigned by the user, i.e. if keywords in the configuration
  2296. are followed by a letter in parenthesis, like TODO(t)."
  2297. :group 'org-todo
  2298. :type '(choice
  2299. (const :tag "Never" nil)
  2300. (const :tag "By default" t)
  2301. (const :tag "Only with C-u C-c C-t" prefix)))
  2302. (defcustom org-provide-todo-statistics t
  2303. "Non-nil means update todo statistics after insert and toggle.
  2304. ALL-HEADLINES means update todo statistics by including headlines
  2305. with no TODO keyword as well, counting them as not done.
  2306. A list of TODO keywords means the same, but skip keywords that are
  2307. not in this list.
  2308. When set to a list of two lists, the first list contains keywords
  2309. to consider as TODO keywords, the second list contains keywords
  2310. to consider as DONE keywords.
  2311. When this is set, todo statistics is updated in the parent of the
  2312. current entry each time a todo state is changed."
  2313. :group 'org-todo
  2314. :type '(choice
  2315. (const :tag "Yes, only for TODO entries" t)
  2316. (const :tag "Yes, including all entries" all-headlines)
  2317. (repeat :tag "Yes, for TODOs in this list"
  2318. (string :tag "TODO keyword"))
  2319. (list :tag "Yes, for TODOs and DONEs in these lists"
  2320. (repeat (string :tag "TODO keyword"))
  2321. (repeat (string :tag "DONE keyword")))
  2322. (other :tag "No TODO statistics" nil)))
  2323. (defcustom org-hierarchical-todo-statistics t
  2324. "Non-nil means TODO statistics covers just direct children.
  2325. When nil, all entries in the subtree are considered.
  2326. This has only an effect if `org-provide-todo-statistics' is set.
  2327. To set this to nil for only a single subtree, use a COOKIE_DATA
  2328. property and include the word \"recursive\" into the value."
  2329. :group 'org-todo
  2330. :type 'boolean)
  2331. (defcustom org-after-todo-state-change-hook nil
  2332. "Hook which is run after the state of a TODO item was changed.
  2333. The new state (a string with a TODO keyword, or nil) is available in the
  2334. Lisp variable `org-state'."
  2335. :group 'org-todo
  2336. :type 'hook)
  2337. (defvar org-blocker-hook nil
  2338. "Hook for functions that are allowed to block a state change.
  2339. Functions in this hook should not modify the buffer.
  2340. Each function gets as its single argument a property list,
  2341. see `org-trigger-hook' for more information about this list.
  2342. If any of the functions in this hook returns nil, the state change
  2343. is blocked.")
  2344. (defvar org-trigger-hook nil
  2345. "Hook for functions that are triggered by a state change.
  2346. Each function gets as its single argument a property list with at
  2347. least the following elements:
  2348. (:type type-of-change :position pos-at-entry-start
  2349. :from old-state :to new-state)
  2350. Depending on the type, more properties may be present.
  2351. This mechanism is currently implemented for:
  2352. TODO state changes
  2353. ------------------
  2354. :type todo-state-change
  2355. :from previous state (keyword as a string), or nil, or a symbol
  2356. 'todo' or 'done', to indicate the general type of state.
  2357. :to new state, like in :from")
  2358. (defcustom org-enforce-todo-dependencies nil
  2359. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2360. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2361. be blocked if any prior sibling is not yet done.
  2362. Finally, if the parent is blocked because of ordered siblings of its own,
  2363. the child will also be blocked."
  2364. :set (lambda (var val)
  2365. (set var val)
  2366. (if val
  2367. (add-hook 'org-blocker-hook
  2368. 'org-block-todo-from-children-or-siblings-or-parent)
  2369. (remove-hook 'org-blocker-hook
  2370. 'org-block-todo-from-children-or-siblings-or-parent)))
  2371. :group 'org-todo
  2372. :type 'boolean)
  2373. (defcustom org-enforce-todo-checkbox-dependencies nil
  2374. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2375. When this is nil, checkboxes have no influence on switching TODO states.
  2376. When non-nil, you first need to check off all check boxes before the TODO
  2377. entry can be switched to DONE.
  2378. This variable needs to be set before org.el is loaded, and you need to
  2379. restart Emacs after a change to make the change effective. The only way
  2380. to change is while Emacs is running is through the customize interface."
  2381. :set (lambda (var val)
  2382. (set var val)
  2383. (if val
  2384. (add-hook 'org-blocker-hook
  2385. 'org-block-todo-from-checkboxes)
  2386. (remove-hook 'org-blocker-hook
  2387. 'org-block-todo-from-checkboxes)))
  2388. :group 'org-todo
  2389. :type 'boolean)
  2390. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2391. "Non-nil means inserting a TODO heading is treated as state change.
  2392. So when the command \\[org-insert-todo-heading] is used, state change
  2393. logging will apply if appropriate. When nil, the new TODO item will
  2394. be inserted directly, and no logging will take place."
  2395. :group 'org-todo
  2396. :type 'boolean)
  2397. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2398. "Non-nil means switching TODO states with S-cursor counts as state change.
  2399. This is the default behavior. However, setting this to nil allows a
  2400. convenient way to select a TODO state and bypass any logging associated
  2401. with that."
  2402. :group 'org-todo
  2403. :type 'boolean)
  2404. (defcustom org-todo-state-tags-triggers nil
  2405. "Tag changes that should be triggered by TODO state changes.
  2406. This is a list. Each entry is
  2407. (state-change (tag . flag) .......)
  2408. State-change can be a string with a state, and empty string to indicate the
  2409. state that has no TODO keyword, or it can be one of the symbols `todo'
  2410. or `done', meaning any not-done or done state, respectively."
  2411. :group 'org-todo
  2412. :group 'org-tags
  2413. :type '(repeat
  2414. (cons (choice :tag "When changing to"
  2415. (const :tag "Not-done state" todo)
  2416. (const :tag "Done state" done)
  2417. (string :tag "State"))
  2418. (repeat
  2419. (cons :tag "Tag action"
  2420. (string :tag "Tag")
  2421. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2422. (defcustom org-log-done nil
  2423. "Information to record when a task moves to the DONE state.
  2424. Possible values are:
  2425. nil Don't add anything, just change the keyword
  2426. time Add a time stamp to the task
  2427. note Prompt for a note and add it with template `org-log-note-headings'
  2428. This option can also be set with on a per-file-basis with
  2429. #+STARTUP: nologdone
  2430. #+STARTUP: logdone
  2431. #+STARTUP: lognotedone
  2432. You can have local logging settings for a subtree by setting the LOGGING
  2433. property to one or more of these keywords."
  2434. :group 'org-todo
  2435. :group 'org-progress
  2436. :type '(choice
  2437. (const :tag "No logging" nil)
  2438. (const :tag "Record CLOSED timestamp" time)
  2439. (const :tag "Record CLOSED timestamp with note." note)))
  2440. ;; Normalize old uses of org-log-done.
  2441. (cond
  2442. ((eq org-log-done t) (setq org-log-done 'time))
  2443. ((and (listp org-log-done) (memq 'done org-log-done))
  2444. (setq org-log-done 'note)))
  2445. (defcustom org-log-reschedule nil
  2446. "Information to record when the scheduling date of a tasks is modified.
  2447. Possible values are:
  2448. nil Don't add anything, just change the date
  2449. time Add a time stamp to the task
  2450. note Prompt for a note and add it with template `org-log-note-headings'
  2451. This option can also be set with on a per-file-basis with
  2452. #+STARTUP: nologreschedule
  2453. #+STARTUP: logreschedule
  2454. #+STARTUP: lognotereschedule"
  2455. :group 'org-todo
  2456. :group 'org-progress
  2457. :type '(choice
  2458. (const :tag "No logging" nil)
  2459. (const :tag "Record timestamp" time)
  2460. (const :tag "Record timestamp with note." note)))
  2461. (defcustom org-log-redeadline nil
  2462. "Information to record when the deadline date of a tasks is modified.
  2463. Possible values are:
  2464. nil Don't add anything, just change the date
  2465. time Add a time stamp to the task
  2466. note Prompt for a note and add it with template `org-log-note-headings'
  2467. This option can also be set with on a per-file-basis with
  2468. #+STARTUP: nologredeadline
  2469. #+STARTUP: logredeadline
  2470. #+STARTUP: lognoteredeadline
  2471. You can have local logging settings for a subtree by setting the LOGGING
  2472. property to one or more of these keywords."
  2473. :group 'org-todo
  2474. :group 'org-progress
  2475. :type '(choice
  2476. (const :tag "No logging" nil)
  2477. (const :tag "Record timestamp" time)
  2478. (const :tag "Record timestamp with note." note)))
  2479. (defcustom org-log-note-clock-out nil
  2480. "Non-nil means record a note when clocking out of an item.
  2481. This can also be configured on a per-file basis by adding one of
  2482. the following lines anywhere in the buffer:
  2483. #+STARTUP: lognoteclock-out
  2484. #+STARTUP: nolognoteclock-out"
  2485. :group 'org-todo
  2486. :group 'org-progress
  2487. :type 'boolean)
  2488. (defcustom org-log-done-with-time t
  2489. "Non-nil means the CLOSED time stamp will contain date and time.
  2490. When nil, only the date will be recorded."
  2491. :group 'org-progress
  2492. :type 'boolean)
  2493. (defcustom org-log-note-headings
  2494. '((done . "CLOSING NOTE %t")
  2495. (state . "State %-12s from %-12S %t")
  2496. (note . "Note taken on %t")
  2497. (reschedule . "Rescheduled from %S on %t")
  2498. (delschedule . "Not scheduled, was %S on %t")
  2499. (redeadline . "New deadline from %S on %t")
  2500. (deldeadline . "Removed deadline, was %S on %t")
  2501. (refile . "Refiled on %t")
  2502. (clock-out . ""))
  2503. "Headings for notes added to entries.
  2504. The value is an alist, with the car being a symbol indicating the
  2505. note context, and the cdr is the heading to be used. The heading
  2506. may also be the empty string. The following placeholders can be
  2507. used:
  2508. %t a time stamp.
  2509. %T an active time stamp instead the default inactive one
  2510. %d a short-format time stamp.
  2511. %D an active short-format time stamp.
  2512. %s the new TODO state or time stamp (inactive), in double quotes.
  2513. %S the old TODO state or time stamp (inactive), in double quotes.
  2514. %u the user name.
  2515. %U full user name.
  2516. In fact, it is not a good idea to change the `state' entry,
  2517. because Agenda Log mode depends on the format of these entries."
  2518. :group 'org-todo
  2519. :group 'org-progress
  2520. :type '(list :greedy t
  2521. (cons (const :tag "Heading when closing an item" done) string)
  2522. (cons (const :tag
  2523. "Heading when changing todo state (todo sequence only)"
  2524. state) string)
  2525. (cons (const :tag "Heading when just taking a note" note) string)
  2526. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2527. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2528. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2529. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2530. (cons (const :tag "Heading when refiling" refile) string)
  2531. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2532. (unless (assq 'note org-log-note-headings)
  2533. (push '(note . "%t") org-log-note-headings))
  2534. (defcustom org-log-into-drawer nil
  2535. "Non-nil means insert state change notes and time stamps into a drawer.
  2536. When nil, state changes notes will be inserted after the headline and
  2537. any scheduling and clock lines, but not inside a drawer.
  2538. The value of this variable should be the name of the drawer to use.
  2539. LOGBOOK is proposed as the default drawer for this purpose, you can
  2540. also set this to a string to define the drawer of your choice.
  2541. A value of t is also allowed, representing \"LOGBOOK\".
  2542. A value of t or nil can also be set with on a per-file-basis with
  2543. #+STARTUP: logdrawer
  2544. #+STARTUP: nologdrawer
  2545. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2546. will be ignored.
  2547. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2548. a subtree.
  2549. Do not check directly this variable in a Lisp program. Call
  2550. function `org-log-into-drawer' instead."
  2551. :group 'org-todo
  2552. :group 'org-progress
  2553. :type '(choice
  2554. (const :tag "Not into a drawer" nil)
  2555. (const :tag "LOGBOOK" t)
  2556. (string :tag "Other")))
  2557. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2558. (defun org-log-into-drawer ()
  2559. "Name of the log drawer, as a string, or nil.
  2560. This is the value of `org-log-into-drawer'. However, if the
  2561. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2562. be used instead of the default value."
  2563. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2564. (cond ((equal p "nil") nil)
  2565. ((equal p "t") "LOGBOOK")
  2566. ((stringp p) p)
  2567. (p "LOGBOOK")
  2568. ((stringp org-log-into-drawer) org-log-into-drawer)
  2569. (org-log-into-drawer "LOGBOOK"))))
  2570. (defcustom org-log-state-notes-insert-after-drawers nil
  2571. "Non-nil means insert state change notes after any drawers in entry.
  2572. Only the drawers that *immediately* follow the headline and the
  2573. deadline/scheduled line are skipped.
  2574. When nil, insert notes right after the heading and perhaps the line
  2575. with deadline/scheduling if present.
  2576. This variable will have no effect if `org-log-into-drawer' is
  2577. set."
  2578. :group 'org-todo
  2579. :group 'org-progress
  2580. :type 'boolean)
  2581. (defcustom org-log-states-order-reversed t
  2582. "Non-nil means the latest state note will be directly after heading.
  2583. When nil, the state change notes will be ordered according to time.
  2584. This option can also be set with on a per-file-basis with
  2585. #+STARTUP: logstatesreversed
  2586. #+STARTUP: nologstatesreversed"
  2587. :group 'org-todo
  2588. :group 'org-progress
  2589. :type 'boolean)
  2590. (defcustom org-todo-repeat-to-state nil
  2591. "The TODO state to which a repeater should return the repeating task.
  2592. By default this is the first task in a TODO sequence, or the previous state
  2593. in a TODO_TYP set. But you can specify another task here.
  2594. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2595. :group 'org-todo
  2596. :version "24.1"
  2597. :type '(choice (const :tag "Head of sequence" nil)
  2598. (string :tag "Specific state")))
  2599. (defcustom org-log-repeat 'time
  2600. "Non-nil means record moving through the DONE state when triggering repeat.
  2601. An auto-repeating task is immediately switched back to TODO when
  2602. marked DONE. If you are not logging state changes (by adding \"@\"
  2603. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2604. record a closing note, there will be no record of the task moving
  2605. through DONE. This variable forces taking a note anyway.
  2606. nil Don't force a record
  2607. time Record a time stamp
  2608. note Prompt for a note and add it with template `org-log-note-headings'
  2609. This option can also be set with on a per-file-basis with
  2610. #+STARTUP: nologrepeat
  2611. #+STARTUP: logrepeat
  2612. #+STARTUP: lognoterepeat
  2613. You can have local logging settings for a subtree by setting the LOGGING
  2614. property to one or more of these keywords."
  2615. :group 'org-todo
  2616. :group 'org-progress
  2617. :type '(choice
  2618. (const :tag "Don't force a record" nil)
  2619. (const :tag "Force recording the DONE state" time)
  2620. (const :tag "Force recording a note with the DONE state" note)))
  2621. (defgroup org-priorities nil
  2622. "Priorities in Org-mode."
  2623. :tag "Org Priorities"
  2624. :group 'org-todo)
  2625. (defcustom org-enable-priority-commands t
  2626. "Non-nil means priority commands are active.
  2627. When nil, these commands will be disabled, so that you never accidentally
  2628. set a priority."
  2629. :group 'org-priorities
  2630. :type 'boolean)
  2631. (defcustom org-highest-priority ?A
  2632. "The highest priority of TODO items. A character like ?A, ?B etc.
  2633. Must have a smaller ASCII number than `org-lowest-priority'."
  2634. :group 'org-priorities
  2635. :type 'character)
  2636. (defcustom org-lowest-priority ?C
  2637. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2638. Must have a larger ASCII number than `org-highest-priority'."
  2639. :group 'org-priorities
  2640. :type 'character)
  2641. (defcustom org-default-priority ?B
  2642. "The default priority of TODO items.
  2643. This is the priority an item gets if no explicit priority is given.
  2644. When starting to cycle on an empty priority the first step in the cycle
  2645. depends on `org-priority-start-cycle-with-default'. The resulting first
  2646. step priority must not exceed the range from `org-highest-priority' to
  2647. `org-lowest-priority' which means that `org-default-priority' has to be
  2648. in this range exclusive or inclusive the range boundaries. Else the
  2649. first step refuses to set the default and the second will fall back
  2650. to (depending on the command used) the highest or lowest priority."
  2651. :group 'org-priorities
  2652. :type 'character)
  2653. (defcustom org-priority-start-cycle-with-default t
  2654. "Non-nil means start with default priority when starting to cycle.
  2655. When this is nil, the first step in the cycle will be (depending on the
  2656. command used) one higher or lower than the default priority.
  2657. See also `org-default-priority'."
  2658. :group 'org-priorities
  2659. :type 'boolean)
  2660. (defcustom org-get-priority-function nil
  2661. "Function to extract the priority from a string.
  2662. The string is normally the headline. If this is nil Org computes the
  2663. priority from the priority cookie like [#A] in the headline. It returns
  2664. an integer, increasing by 1000 for each priority level.
  2665. The user can set a different function here, which should take a string
  2666. as an argument and return the numeric priority."
  2667. :group 'org-priorities
  2668. :version "24.1"
  2669. :type '(choice
  2670. (const nil)
  2671. (function)))
  2672. (defgroup org-time nil
  2673. "Options concerning time stamps and deadlines in Org-mode."
  2674. :tag "Org Time"
  2675. :group 'org)
  2676. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2677. "Number of minutes to round time stamps to.
  2678. These are two values, the first applies when first creating a time stamp.
  2679. The second applies when changing it with the commands `S-up' and `S-down'.
  2680. When changing the time stamp, this means that it will change in steps
  2681. of N minutes, as given by the second value.
  2682. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2683. numbers should be factors of 60, so for example 5, 10, 15.
  2684. When this is larger than 1, you can still force an exact time stamp by using
  2685. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2686. and by using a prefix arg to `S-up/down' to specify the exact number
  2687. of minutes to shift."
  2688. :group 'org-time
  2689. :get (lambda (var) ; Make sure both elements are there
  2690. (if (integerp (default-value var))
  2691. (list (default-value var) 5)
  2692. (default-value var)))
  2693. :type '(list
  2694. (integer :tag "when inserting times")
  2695. (integer :tag "when modifying times")))
  2696. ;; Normalize old customizations of this variable.
  2697. (when (integerp org-time-stamp-rounding-minutes)
  2698. (setq org-time-stamp-rounding-minutes
  2699. (list org-time-stamp-rounding-minutes
  2700. org-time-stamp-rounding-minutes)))
  2701. (defcustom org-display-custom-times nil
  2702. "Non-nil means overlay custom formats over all time stamps.
  2703. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2704. To turn this on on a per-file basis, insert anywhere in the file:
  2705. #+STARTUP: customtime"
  2706. :group 'org-time
  2707. :set 'set-default
  2708. :type 'sexp)
  2709. (make-variable-buffer-local 'org-display-custom-times)
  2710. (defcustom org-time-stamp-custom-formats
  2711. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2712. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2713. These are overlaid over the default ISO format if the variable
  2714. `org-display-custom-times' is set. Time like %H:%M should be at the
  2715. end of the second format. The custom formats are also honored by export
  2716. commands, if custom time display is turned on at the time of export."
  2717. :group 'org-time
  2718. :type 'sexp)
  2719. (defun org-time-stamp-format (&optional long inactive)
  2720. "Get the right format for a time string."
  2721. (let ((f (if long (cdr org-time-stamp-formats)
  2722. (car org-time-stamp-formats))))
  2723. (if inactive
  2724. (concat "[" (substring f 1 -1) "]")
  2725. f)))
  2726. (defcustom org-time-clocksum-format
  2727. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2728. "The format string used when creating CLOCKSUM lines.
  2729. This is also used when Org mode generates a time duration.
  2730. The value can be a single format string containing two
  2731. %-sequences, which will be filled with the number of hours and
  2732. minutes in that order.
  2733. Alternatively, the value can be a plist associating any of the
  2734. keys :years, :months, :weeks, :days, :hours or :minutes with
  2735. format strings. The time duration is formatted using only the
  2736. time components that are needed and concatenating the results.
  2737. If a time unit in absent, it falls back to the next smallest
  2738. unit.
  2739. The keys :require-years, :require-months, :require-days,
  2740. :require-weeks, :require-hours, :require-minutes are also
  2741. meaningful. A non-nil value for these keys indicates that the
  2742. corresponding time component should always be included, even if
  2743. its value is 0.
  2744. For example,
  2745. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2746. :require-minutes t)
  2747. means durations longer than a day will be expressed in days,
  2748. hours and minutes, and durations less than a day will always be
  2749. expressed in hours and minutes (even for durations less than an
  2750. hour).
  2751. The value
  2752. \(:days \"%dd\" :minutes \"%dm\")
  2753. means durations longer than a day will be expressed in days and
  2754. minutes, and durations less than a day will be expressed entirely
  2755. in minutes (even for durations longer than an hour)."
  2756. :group 'org-time
  2757. :group 'org-clock
  2758. :version "24.4"
  2759. :package-version '(Org . "8.0")
  2760. :type '(choice (string :tag "Format string")
  2761. (set :tag "Plist"
  2762. (group :inline t (const :tag "Years" :years)
  2763. (string :tag "Format string"))
  2764. (group :inline t
  2765. (const :tag "Always show years" :require-years)
  2766. (const t))
  2767. (group :inline t (const :tag "Months" :months)
  2768. (string :tag "Format string"))
  2769. (group :inline t
  2770. (const :tag "Always show months" :require-months)
  2771. (const t))
  2772. (group :inline t (const :tag "Weeks" :weeks)
  2773. (string :tag "Format string"))
  2774. (group :inline t
  2775. (const :tag "Always show weeks" :require-weeks)
  2776. (const t))
  2777. (group :inline t (const :tag "Days" :days)
  2778. (string :tag "Format string"))
  2779. (group :inline t
  2780. (const :tag "Always show days" :require-days)
  2781. (const t))
  2782. (group :inline t (const :tag "Hours" :hours)
  2783. (string :tag "Format string"))
  2784. (group :inline t
  2785. (const :tag "Always show hours" :require-hours)
  2786. (const t))
  2787. (group :inline t (const :tag "Minutes" :minutes)
  2788. (string :tag "Format string"))
  2789. (group :inline t
  2790. (const :tag "Always show minutes" :require-minutes)
  2791. (const t)))))
  2792. (defcustom org-time-clocksum-use-fractional nil
  2793. "When non-nil, \\[org-clock-display] uses fractional times.
  2794. See `org-time-clocksum-format' for more on time clock formats."
  2795. :group 'org-time
  2796. :group 'org-clock
  2797. :version "24.3"
  2798. :type 'boolean)
  2799. (defcustom org-time-clocksum-use-effort-durations nil
  2800. "When non-nil, \\[org-clock-display] uses effort durations.
  2801. E.g. by default, one day is considered to be a 8 hours effort,
  2802. so a task that has been clocked for 16 hours will be displayed
  2803. as during 2 days in the clock display or in the clocktable.
  2804. See `org-effort-durations' on how to set effort durations
  2805. and `org-time-clocksum-format' for more on time clock formats."
  2806. :group 'org-time
  2807. :group 'org-clock
  2808. :version "24.4"
  2809. :package-version '(Org . "8.0")
  2810. :type 'boolean)
  2811. (defcustom org-time-clocksum-fractional-format "%.2f"
  2812. "The format string used when creating CLOCKSUM lines,
  2813. or when Org mode generates a time duration, if
  2814. `org-time-clocksum-use-fractional' is enabled.
  2815. The value can be a single format string containing one
  2816. %-sequence, which will be filled with the number of hours as
  2817. a float.
  2818. Alternatively, the value can be a plist associating any of the
  2819. keys :years, :months, :weeks, :days, :hours or :minutes with
  2820. a format string. The time duration is formatted using the
  2821. largest time unit which gives a non-zero integer part. If all
  2822. specified formats have zero integer part, the smallest time unit
  2823. is used."
  2824. :group 'org-time
  2825. :type '(choice (string :tag "Format string")
  2826. (set (group :inline t (const :tag "Years" :years)
  2827. (string :tag "Format string"))
  2828. (group :inline t (const :tag "Months" :months)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Weeks" :weeks)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Days" :days)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Hours" :hours)
  2835. (string :tag "Format string"))
  2836. (group :inline t (const :tag "Minutes" :minutes)
  2837. (string :tag "Format string")))))
  2838. (defcustom org-deadline-warning-days 14
  2839. "Number of days before expiration during which a deadline becomes active.
  2840. This variable governs the display in sparse trees and in the agenda.
  2841. When 0 or negative, it means use this number (the absolute value of it)
  2842. even if a deadline has a different individual lead time specified.
  2843. Custom commands can set this variable in the options section."
  2844. :group 'org-time
  2845. :group 'org-agenda-daily/weekly
  2846. :type 'integer)
  2847. (defcustom org-scheduled-delay-days 0
  2848. "Number of days before a scheduled item becomes active.
  2849. This variable governs the display in sparse trees and in the agenda.
  2850. The default value (i.e. 0) means: don't delay scheduled item.
  2851. When negative, it means use this number (the absolute value of it)
  2852. even if a scheduled item has a different individual delay time
  2853. specified.
  2854. Custom commands can set this variable in the options section."
  2855. :group 'org-time
  2856. :group 'org-agenda-daily/weekly
  2857. :version "24.4"
  2858. :package-version '(Org . "8.0")
  2859. :type 'integer)
  2860. (defcustom org-read-date-prefer-future t
  2861. "Non-nil means assume future for incomplete date input from user.
  2862. This affects the following situations:
  2863. 1. The user gives a month but not a year.
  2864. For example, if it is April and you enter \"feb 2\", this will be read
  2865. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2866. 2. The user gives a day, but no month.
  2867. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2868. read this as the third of *next* month. However, if you enter \"17\",
  2869. it will be considered as *this* month.
  2870. If you set this variable to the symbol `time', then also the following
  2871. will work:
  2872. 3. If the user gives a time.
  2873. If the time is before now, it will be interpreted as tomorrow.
  2874. Currently none of this works for ISO week specifications.
  2875. When this option is nil, the current day, month and year will always be
  2876. used as defaults.
  2877. See also `org-agenda-jump-prefer-future'."
  2878. :group 'org-time
  2879. :type '(choice
  2880. (const :tag "Never" nil)
  2881. (const :tag "Check month and day" t)
  2882. (const :tag "Check month, day, and time" time)))
  2883. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2884. "Should the agenda jump command prefer the future for incomplete dates?
  2885. The default is to do the same as configured in `org-read-date-prefer-future'.
  2886. But you can also set a deviating value here.
  2887. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2888. :group 'org-agenda
  2889. :group 'org-time
  2890. :version "24.1"
  2891. :type '(choice
  2892. (const :tag "Use org-read-date-prefer-future"
  2893. org-read-date-prefer-future)
  2894. (const :tag "Never" nil)
  2895. (const :tag "Always" t)))
  2896. (defcustom org-read-date-force-compatible-dates t
  2897. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2898. Depending on the system Emacs is running on, certain dates cannot
  2899. be represented with the type used internally to represent time.
  2900. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2901. correctly. Some systems allow for earlier dates, some for later,
  2902. some for both. One way to find out it to insert any date into an
  2903. Org buffer, putting the cursor on the year and hitting S-up and
  2904. S-down to test the range.
  2905. When this variable is set to t, the date/time prompt will not let
  2906. you specify dates outside the 1970-2037 range, so it is certain that
  2907. these dates will work in whatever version of Emacs you are
  2908. running, and also that you can move a file from one Emacs implementation
  2909. to another. WHenever Org is forcing the year for you, it will display
  2910. a message and beep.
  2911. When this variable is nil, Org will check if the date is
  2912. representable in the specific Emacs implementation you are using.
  2913. If not, it will force a year, usually the current year, and beep
  2914. to remind you. Currently this setting is not recommended because
  2915. the likelihood that you will open your Org files in an Emacs that
  2916. has limited date range is not negligible.
  2917. A workaround for this problem is to use diary sexp dates for time
  2918. stamps outside of this range."
  2919. :group 'org-time
  2920. :version "24.1"
  2921. :type 'boolean)
  2922. (defcustom org-read-date-display-live t
  2923. "Non-nil means display current interpretation of date prompt live.
  2924. This display will be in an overlay, in the minibuffer."
  2925. :group 'org-time
  2926. :type 'boolean)
  2927. (defcustom org-read-date-popup-calendar t
  2928. "Non-nil means pop up a calendar when prompting for a date.
  2929. In the calendar, the date can be selected with mouse-1. However, the
  2930. minibuffer will also be active, and you can simply enter the date as well.
  2931. When nil, only the minibuffer will be available."
  2932. :group 'org-time
  2933. :type 'boolean)
  2934. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2935. 'org-read-date-popup-calendar)
  2936. (make-obsolete-variable
  2937. 'org-read-date-minibuffer-setup-hook
  2938. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2939. (defcustom org-read-date-minibuffer-setup-hook nil
  2940. "Hook to be used to set up keys for the date/time interface.
  2941. Add key definitions to `minibuffer-local-map', which will be a
  2942. temporary copy.
  2943. WARNING: This option is obsolete, you should use
  2944. `org-read-date-minibuffer-local-map' to set up keys."
  2945. :group 'org-time
  2946. :type 'hook)
  2947. (defcustom org-extend-today-until 0
  2948. "The hour when your day really ends. Must be an integer.
  2949. This has influence for the following applications:
  2950. - When switching the agenda to \"today\". It it is still earlier than
  2951. the time given here, the day recognized as TODAY is actually yesterday.
  2952. - When a date is read from the user and it is still before the time given
  2953. here, the current date and time will be assumed to be yesterday, 23:59.
  2954. Also, timestamps inserted in capture templates follow this rule.
  2955. IMPORTANT: This is a feature whose implementation is and likely will
  2956. remain incomplete. Really, it is only here because past midnight seems to
  2957. be the favorite working time of John Wiegley :-)"
  2958. :group 'org-time
  2959. :type 'integer)
  2960. (defcustom org-use-effective-time nil
  2961. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2962. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2963. \"effective time\" of any timestamps between midnight and 8am will be
  2964. 23:59 of the previous day."
  2965. :group 'org-time
  2966. :version "24.1"
  2967. :type 'boolean)
  2968. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2969. "When non-nil, use the last clock out time for `org-todo'.
  2970. Note that this option has precedence over the combined use of
  2971. `org-use-effective-time' and `org-extend-today-until'."
  2972. :group 'org-time
  2973. :version "24.4"
  2974. :package-version '(Org . "8.0")
  2975. :type 'boolean)
  2976. (defcustom org-edit-timestamp-down-means-later nil
  2977. "Non-nil means S-down will increase the time in a time stamp.
  2978. When nil, S-up will increase."
  2979. :group 'org-time
  2980. :type 'boolean)
  2981. (defcustom org-calendar-follow-timestamp-change t
  2982. "Non-nil means make the calendar window follow timestamp changes.
  2983. When a timestamp is modified and the calendar window is visible, it will be
  2984. moved to the new date."
  2985. :group 'org-time
  2986. :type 'boolean)
  2987. (defgroup org-tags nil
  2988. "Options concerning tags in Org-mode."
  2989. :tag "Org Tags"
  2990. :group 'org)
  2991. (defcustom org-tag-alist nil
  2992. "List of tags allowed in Org-mode files.
  2993. When this list is nil, Org-mode will base TAG input on what is already in the
  2994. buffer.
  2995. The value of this variable is an alist, the car of each entry must be a
  2996. keyword as a string, the cdr may be a character that is used to select
  2997. that tag through the fast-tag-selection interface.
  2998. See the manual for details."
  2999. :group 'org-tags
  3000. :type '(repeat
  3001. (choice
  3002. (cons (string :tag "Tag name")
  3003. (character :tag "Access char"))
  3004. (list :tag "Start radio group"
  3005. (const :startgroup)
  3006. (option (string :tag "Group description")))
  3007. (list :tag "Start tag group, non distinct"
  3008. (const :startgrouptag)
  3009. (option (string :tag "Group description")))
  3010. (list :tag "Group tags delimiter"
  3011. (const :grouptags))
  3012. (list :tag "End radio group"
  3013. (const :endgroup)
  3014. (option (string :tag "Group description")))
  3015. (list :tag "End tag group, non distinct"
  3016. (const :endgrouptag)
  3017. (option (string :tag "Group description")))
  3018. (const :tag "New line" (:newline)))))
  3019. (defcustom org-tag-persistent-alist nil
  3020. "List of tags that will always appear in all Org-mode files.
  3021. This is in addition to any in buffer settings or customizations
  3022. of `org-tag-alist'.
  3023. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3024. The value of this variable is an alist, the car of each entry must be a
  3025. keyword as a string, the cdr may be a character that is used to select
  3026. that tag through the fast-tag-selection interface.
  3027. See the manual for details.
  3028. To disable these tags on a per-file basis, insert anywhere in the file:
  3029. #+STARTUP: noptag"
  3030. :group 'org-tags
  3031. :type '(repeat
  3032. (choice
  3033. (cons (string :tag "Tag name")
  3034. (character :tag "Access char"))
  3035. (const :tag "Start radio group" (:startgroup))
  3036. (const :tag "Group tags delimiter" (:grouptags))
  3037. (const :tag "End radio group" (:endgroup))
  3038. (const :tag "New line" (:newline)))))
  3039. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3040. "If non-nil, always offer completion for all tags of all agenda files.
  3041. Instead of customizing this variable directly, you might want to
  3042. set it locally for capture buffers, because there no list of
  3043. tags in that file can be created dynamically (there are none).
  3044. (add-hook 'org-capture-mode-hook
  3045. (lambda ()
  3046. (set (make-local-variable
  3047. 'org-complete-tags-always-offer-all-agenda-tags)
  3048. t)))"
  3049. :group 'org-tags
  3050. :version "24.1"
  3051. :type 'boolean)
  3052. (defvar org-file-tags nil
  3053. "List of tags that can be inherited by all entries in the file.
  3054. The tags will be inherited if the variable `org-use-tag-inheritance'
  3055. says they should be.
  3056. This variable is populated from #+FILETAGS lines.")
  3057. (defcustom org-use-fast-tag-selection 'auto
  3058. "Non-nil means use fast tag selection scheme.
  3059. This is a special interface to select and deselect tags with single keys.
  3060. When nil, fast selection is never used.
  3061. When the symbol `auto', fast selection is used if and only if selection
  3062. characters for tags have been configured, either through the variable
  3063. `org-tag-alist' or through a #+TAGS line in the buffer.
  3064. When t, fast selection is always used and selection keys are assigned
  3065. automatically if necessary."
  3066. :group 'org-tags
  3067. :type '(choice
  3068. (const :tag "Always" t)
  3069. (const :tag "Never" nil)
  3070. (const :tag "When selection characters are configured" auto)))
  3071. (defcustom org-fast-tag-selection-single-key nil
  3072. "Non-nil means fast tag selection exits after first change.
  3073. When nil, you have to press RET to exit it.
  3074. During fast tag selection, you can toggle this flag with `C-c'.
  3075. This variable can also have the value `expert'. In this case, the window
  3076. displaying the tags menu is not even shown, until you press C-c again."
  3077. :group 'org-tags
  3078. :type '(choice
  3079. (const :tag "No" nil)
  3080. (const :tag "Yes" t)
  3081. (const :tag "Expert" expert)))
  3082. (defvar org-fast-tag-selection-include-todo nil
  3083. "Non-nil means fast tags selection interface will also offer TODO states.
  3084. This is an undocumented feature, you should not rely on it.")
  3085. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3086. "The column to which tags should be indented in a headline.
  3087. If this number is positive, it specifies the column. If it is negative,
  3088. it means that the tags should be flushright to that column. For example,
  3089. -80 works well for a normal 80 character screen.
  3090. When 0, place tags directly after headline text, with only one space in
  3091. between."
  3092. :group 'org-tags
  3093. :type 'integer)
  3094. (defcustom org-auto-align-tags t
  3095. "Non-nil keeps tags aligned when modifying headlines.
  3096. Some operations (i.e. demoting) change the length of a headline and
  3097. therefore shift the tags around. With this option turned on, after
  3098. each such operation the tags are again aligned to `org-tags-column'."
  3099. :group 'org-tags
  3100. :type 'boolean)
  3101. (defcustom org-use-tag-inheritance t
  3102. "Non-nil means tags in levels apply also for sublevels.
  3103. When nil, only the tags directly given in a specific line apply there.
  3104. This may also be a list of tags that should be inherited, or a regexp that
  3105. matches tags that should be inherited. Additional control is possible
  3106. with the variable `org-tags-exclude-from-inheritance' which gives an
  3107. explicit list of tags to be excluded from inheritance, even if the value of
  3108. `org-use-tag-inheritance' would select it for inheritance.
  3109. If this option is t, a match early-on in a tree can lead to a large
  3110. number of matches in the subtree when constructing the agenda or creating
  3111. a sparse tree. If you only want to see the first match in a tree during
  3112. a search, check out the variable `org-tags-match-list-sublevels'."
  3113. :group 'org-tags
  3114. :type '(choice
  3115. (const :tag "Not" nil)
  3116. (const :tag "Always" t)
  3117. (repeat :tag "Specific tags" (string :tag "Tag"))
  3118. (regexp :tag "Tags matched by regexp")))
  3119. (defcustom org-tags-exclude-from-inheritance nil
  3120. "List of tags that should never be inherited.
  3121. This is a way to exclude a few tags from inheritance. For way to do
  3122. the opposite, to actively allow inheritance for selected tags,
  3123. see the variable `org-use-tag-inheritance'."
  3124. :group 'org-tags
  3125. :type '(repeat (string :tag "Tag")))
  3126. (defun org-tag-inherit-p (tag)
  3127. "Check if TAG is one that should be inherited."
  3128. (cond
  3129. ((member tag org-tags-exclude-from-inheritance) nil)
  3130. ((eq org-use-tag-inheritance t) t)
  3131. ((not org-use-tag-inheritance) nil)
  3132. ((stringp org-use-tag-inheritance)
  3133. (string-match org-use-tag-inheritance tag))
  3134. ((listp org-use-tag-inheritance)
  3135. (member tag org-use-tag-inheritance))
  3136. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3137. (defcustom org-tags-match-list-sublevels t
  3138. "Non-nil means list also sublevels of headlines matching a search.
  3139. This variable applies to tags/property searches, and also to stuck
  3140. projects because this search is based on a tags match as well.
  3141. When set to the symbol `indented', sublevels are indented with
  3142. leading dots.
  3143. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3144. the sublevels of a headline matching a tag search often also match
  3145. the same search. Listing all of them can create very long lists.
  3146. Setting this variable to nil causes subtrees of a match to be skipped.
  3147. This variable is semi-obsolete and probably should always be true. It
  3148. is better to limit inheritance to certain tags using the variables
  3149. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3150. :group 'org-tags
  3151. :type '(choice
  3152. (const :tag "No, don't list them" nil)
  3153. (const :tag "Yes, do list them" t)
  3154. (const :tag "List them, indented with leading dots" indented)))
  3155. (defcustom org-tags-sort-function nil
  3156. "When set, tags are sorted using this function as a comparator."
  3157. :group 'org-tags
  3158. :type '(choice
  3159. (const :tag "No sorting" nil)
  3160. (const :tag "Alphabetical" string<)
  3161. (const :tag "Reverse alphabetical" string>)
  3162. (function :tag "Custom function" nil)))
  3163. (defvar org-tags-history nil
  3164. "History of minibuffer reads for tags.")
  3165. (defvar org-last-tags-completion-table nil
  3166. "The last used completion table for tags.")
  3167. (defvar org-after-tags-change-hook nil
  3168. "Hook that is run after the tags in a line have changed.")
  3169. (defgroup org-properties nil
  3170. "Options concerning properties in Org-mode."
  3171. :tag "Org Properties"
  3172. :group 'org)
  3173. (defcustom org-property-format "%-10s %s"
  3174. "How property key/value pairs should be formatted by `indent-line'.
  3175. When `indent-line' hits a property definition, it will format the line
  3176. according to this format, mainly to make sure that the values are
  3177. lined-up with respect to each other."
  3178. :group 'org-properties
  3179. :type 'string)
  3180. (defcustom org-properties-postprocess-alist nil
  3181. "Alist of properties and functions to adjust inserted values.
  3182. Elements of this alist must be of the form
  3183. ([string] [function])
  3184. where [string] must be a property name and [function] must be a
  3185. lambda expression: this lambda expression must take one argument,
  3186. the value to adjust, and return the new value as a string.
  3187. For example, this element will allow the property \"Remaining\"
  3188. to be updated wrt the relation between the \"Effort\" property
  3189. and the clock summary:
  3190. ((\"Remaining\" (lambda(value)
  3191. (let ((clocksum (org-clock-sum-current-item))
  3192. (effort (org-duration-string-to-minutes
  3193. (org-entry-get (point) \"Effort\"))))
  3194. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3195. :group 'org-properties
  3196. :version "24.1"
  3197. :type '(alist :key-type (string :tag "Property")
  3198. :value-type (function :tag "Function")))
  3199. (defcustom org-use-property-inheritance nil
  3200. "Non-nil means properties apply also for sublevels.
  3201. This setting is chiefly used during property searches. Turning it on can
  3202. cause significant overhead when doing a search, which is why it is not
  3203. on by default.
  3204. When nil, only the properties directly given in the current entry count.
  3205. When t, every property is inherited. The value may also be a list of
  3206. properties that should have inheritance, or a regular expression matching
  3207. properties that should be inherited.
  3208. However, note that some special properties use inheritance under special
  3209. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3210. and the properties ending in \"_ALL\" when they are used as descriptor
  3211. for valid values of a property.
  3212. Note for programmers:
  3213. When querying an entry with `org-entry-get', you can control if inheritance
  3214. should be used. By default, `org-entry-get' looks only at the local
  3215. properties. You can request inheritance by setting the inherit argument
  3216. to t (to force inheritance) or to `selective' (to respect the setting
  3217. in this variable)."
  3218. :group 'org-properties
  3219. :type '(choice
  3220. (const :tag "Not" nil)
  3221. (const :tag "Always" t)
  3222. (repeat :tag "Specific properties" (string :tag "Property"))
  3223. (regexp :tag "Properties matched by regexp")))
  3224. (defun org-property-inherit-p (property)
  3225. "Check if PROPERTY is one that should be inherited."
  3226. (cond
  3227. ((eq org-use-property-inheritance t) t)
  3228. ((not org-use-property-inheritance) nil)
  3229. ((stringp org-use-property-inheritance)
  3230. (string-match org-use-property-inheritance property))
  3231. ((listp org-use-property-inheritance)
  3232. (member property org-use-property-inheritance))
  3233. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3234. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3235. "The default column format, if no other format has been defined.
  3236. This variable can be set on the per-file basis by inserting a line
  3237. #+COLUMNS: %25ITEM ....."
  3238. :group 'org-properties
  3239. :type 'string)
  3240. (defcustom org-columns-ellipses ".."
  3241. "The ellipses to be used when a field in column view is truncated.
  3242. When this is the empty string, as many characters as possible are shown,
  3243. but then there will be no visual indication that the field has been truncated.
  3244. When this is a string of length N, the last N characters of a truncated
  3245. field are replaced by this string. If the column is narrower than the
  3246. ellipses string, only part of the ellipses string will be shown."
  3247. :group 'org-properties
  3248. :type 'string)
  3249. (defcustom org-columns-modify-value-for-display-function nil
  3250. "Function that modifies values for display in column view.
  3251. For example, it can be used to cut out a certain part from a time stamp.
  3252. The function must take 2 arguments:
  3253. column-title The title of the column (*not* the property name)
  3254. value The value that should be modified.
  3255. The function should return the value that should be displayed,
  3256. or nil if the normal value should be used."
  3257. :group 'org-properties
  3258. :type '(choice (const nil) (function)))
  3259. (defconst org-global-properties-fixed
  3260. '(("VISIBILITY_ALL" . "folded children content all")
  3261. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3262. "List of property/value pairs that can be inherited by any entry.
  3263. These are fixed values, for the preset properties. The user variable
  3264. that can be used to add to this list is `org-global-properties'.
  3265. The entries in this list are cons cells where the car is a property
  3266. name and cdr is a string with the value. If the value represents
  3267. multiple items like an \"_ALL\" property, separate the items by
  3268. spaces.")
  3269. (defcustom org-global-properties nil
  3270. "List of property/value pairs that can be inherited by any entry.
  3271. This list will be combined with the constant `org-global-properties-fixed'.
  3272. The entries in this list are cons cells where the car is a property
  3273. name and cdr is a string with the value.
  3274. You can set buffer-local values for the same purpose in the variable
  3275. `org-file-properties' this by adding lines like
  3276. #+PROPERTY: NAME VALUE"
  3277. :group 'org-properties
  3278. :type '(repeat
  3279. (cons (string :tag "Property")
  3280. (string :tag "Value"))))
  3281. (defvar org-file-properties nil
  3282. "List of property/value pairs that can be inherited by any entry.
  3283. Valid for the current buffer.
  3284. This variable is populated from #+PROPERTY lines.")
  3285. (make-variable-buffer-local 'org-file-properties)
  3286. (defgroup org-agenda nil
  3287. "Options concerning agenda views in Org-mode."
  3288. :tag "Org Agenda"
  3289. :group 'org)
  3290. (defvar org-category nil
  3291. "Variable used by org files to set a category for agenda display.
  3292. Such files should use a file variable to set it, for example
  3293. # -*- mode: org; org-category: \"ELisp\"
  3294. or contain a special line
  3295. #+CATEGORY: ELisp
  3296. If the file does not specify a category, then file's base name
  3297. is used instead.")
  3298. (make-variable-buffer-local 'org-category)
  3299. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3300. (defcustom org-agenda-files nil
  3301. "The files to be used for agenda display.
  3302. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3303. \\[org-remove-file]. You can also use customize to edit the list.
  3304. If an entry is a directory, all files in that directory that are matched by
  3305. `org-agenda-file-regexp' will be part of the file list.
  3306. If the value of the variable is not a list but a single file name, then
  3307. the list of agenda files is actually stored and maintained in that file, one
  3308. agenda file per line. In this file paths can be given relative to
  3309. `org-directory'. Tilde expansion and environment variable substitution
  3310. are also made."
  3311. :group 'org-agenda
  3312. :type '(choice
  3313. (repeat :tag "List of files and directories" file)
  3314. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3315. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3316. "Regular expression to match files for `org-agenda-files'.
  3317. If any element in the list in that variable contains a directory instead
  3318. of a normal file, all files in that directory that are matched by this
  3319. regular expression will be included."
  3320. :group 'org-agenda
  3321. :type 'regexp)
  3322. (defcustom org-agenda-text-search-extra-files nil
  3323. "List of extra files to be searched by text search commands.
  3324. These files will be searched in addition to the agenda files by the
  3325. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3326. Note that these files will only be searched for text search commands,
  3327. not for the other agenda views like todo lists, tag searches or the weekly
  3328. agenda. This variable is intended to list notes and possibly archive files
  3329. that should also be searched by these two commands.
  3330. In fact, if the first element in the list is the symbol `agenda-archives',
  3331. then all archive files of all agenda files will be added to the search
  3332. scope."
  3333. :group 'org-agenda
  3334. :type '(set :greedy t
  3335. (const :tag "Agenda Archives" agenda-archives)
  3336. (repeat :inline t (file))))
  3337. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3338. 'org-agenda-text-search-extra-files)
  3339. (defcustom org-agenda-skip-unavailable-files nil
  3340. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3341. A nil value means to remove them, after a query, from the list."
  3342. :group 'org-agenda
  3343. :type 'boolean)
  3344. (defcustom org-calendar-to-agenda-key [?c]
  3345. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3346. The command `org-calendar-goto-agenda' will be bound to this key. The
  3347. default is the character `c' because then `c' can be used to switch back and
  3348. forth between agenda and calendar."
  3349. :group 'org-agenda
  3350. :type 'sexp)
  3351. (defcustom org-calendar-insert-diary-entry-key [?i]
  3352. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3353. This option is irrelevant until `org-agenda-diary-file' has been configured
  3354. to point to an Org-mode file. When that is the case, the command
  3355. `org-agenda-diary-entry' will be bound to the key given here, by default
  3356. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3357. if you want to continue doing this, you need to change this to a different
  3358. key."
  3359. :group 'org-agenda
  3360. :type 'sexp)
  3361. (defcustom org-agenda-diary-file 'diary-file
  3362. "File to which to add new entries with the `i' key in agenda and calendar.
  3363. When this is the symbol `diary-file', the functionality in the Emacs
  3364. calendar will be used to add entries to the `diary-file'. But when this
  3365. points to a file, `org-agenda-diary-entry' will be used instead."
  3366. :group 'org-agenda
  3367. :type '(choice
  3368. (const :tag "The standard Emacs diary file" diary-file)
  3369. (file :tag "Special Org file diary entries")))
  3370. (eval-after-load "calendar"
  3371. '(progn
  3372. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3373. 'org-calendar-goto-agenda)
  3374. (add-hook 'calendar-mode-hook
  3375. (lambda ()
  3376. (unless (eq org-agenda-diary-file 'diary-file)
  3377. (define-key calendar-mode-map
  3378. org-calendar-insert-diary-entry-key
  3379. 'org-agenda-diary-entry))))))
  3380. (defgroup org-latex nil
  3381. "Options for embedding LaTeX code into Org-mode."
  3382. :tag "Org LaTeX"
  3383. :group 'org)
  3384. (defcustom org-format-latex-options
  3385. '(:foreground default :background default :scale 1.0
  3386. :html-foreground "Black" :html-background "Transparent"
  3387. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3388. "Options for creating images from LaTeX fragments.
  3389. This is a property list with the following properties:
  3390. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3391. `default' means use the foreground of the default face.
  3392. `auto' means use the foreground from the text face.
  3393. :background the background color, or \"Transparent\".
  3394. `default' means use the background of the default face.
  3395. `auto' means use the background from the text face.
  3396. :scale a scaling factor for the size of the images, to get more pixels
  3397. :html-foreground, :html-background, :html-scale
  3398. the same numbers for HTML export.
  3399. :matchers a list indicating which matchers should be used to
  3400. find LaTeX fragments. Valid members of this list are:
  3401. \"begin\" find environments
  3402. \"$1\" find single characters surrounded by $.$
  3403. \"$\" find math expressions surrounded by $...$
  3404. \"$$\" find math expressions surrounded by $$....$$
  3405. \"\\(\" find math expressions surrounded by \\(...\\)
  3406. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3407. :group 'org-latex
  3408. :type 'plist)
  3409. (defcustom org-format-latex-signal-error t
  3410. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3411. When nil, just push out a message."
  3412. :group 'org-latex
  3413. :version "24.1"
  3414. :type 'boolean)
  3415. (defcustom org-latex-to-mathml-jar-file nil
  3416. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3417. Use this to specify additional executable file say a jar file.
  3418. When using MathToWeb as the converter, specify the full-path to
  3419. your mathtoweb.jar file."
  3420. :group 'org-latex
  3421. :version "24.1"
  3422. :type '(choice
  3423. (const :tag "None" nil)
  3424. (file :tag "JAR file" :must-match t)))
  3425. (defcustom org-latex-to-mathml-convert-command nil
  3426. "Command to convert LaTeX fragments to MathML.
  3427. Replace format-specifiers in the command as noted below and use
  3428. `shell-command' to convert LaTeX to MathML.
  3429. %j: Executable file in fully expanded form as specified by
  3430. `org-latex-to-mathml-jar-file'.
  3431. %I: Input LaTeX file in fully expanded form.
  3432. %i: The latex fragment to be converted.
  3433. %o: Output MathML file.
  3434. This command is used by `org-create-math-formula'.
  3435. When using MathToWeb as the converter, set this option to
  3436. \"java -jar %j -unicode -force -df %o %I\".
  3437. When using LaTeXML set this option to
  3438. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3439. :group 'org-latex
  3440. :version "24.1"
  3441. :type '(choice
  3442. (const :tag "None" nil)
  3443. (string :tag "\nShell command")))
  3444. (defcustom org-latex-create-formula-image-program 'dvipng
  3445. "Program to convert LaTeX fragments with.
  3446. dvipng Process the LaTeX fragments to dvi file, then convert
  3447. dvi files to png files using dvipng.
  3448. This will also include processing of non-math environments.
  3449. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3450. to convert pdf files to png files"
  3451. :group 'org-latex
  3452. :version "24.1"
  3453. :type '(choice
  3454. (const :tag "dvipng" dvipng)
  3455. (const :tag "imagemagick" imagemagick)))
  3456. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3457. "Path to store latex preview images.
  3458. A relative path here creates many directories relative to the
  3459. processed org files paths. An absolute path puts all preview
  3460. images at the same place."
  3461. :group 'org-latex
  3462. :version "24.3"
  3463. :type 'string)
  3464. (defun org-format-latex-mathml-available-p ()
  3465. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3466. (save-match-data
  3467. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3468. org-latex-to-mathml-convert-command)
  3469. (let ((executable (car (split-string
  3470. org-latex-to-mathml-convert-command))))
  3471. (when (executable-find executable)
  3472. (if (string-match
  3473. "%j" org-latex-to-mathml-convert-command)
  3474. (file-readable-p org-latex-to-mathml-jar-file)
  3475. t))))))
  3476. (defcustom org-format-latex-header "\\documentclass{article}
  3477. \\usepackage[usenames]{color}
  3478. \[PACKAGES]
  3479. \[DEFAULT-PACKAGES]
  3480. \\pagestyle{empty} % do not remove
  3481. % The settings below are copied from fullpage.sty
  3482. \\setlength{\\textwidth}{\\paperwidth}
  3483. \\addtolength{\\textwidth}{-3cm}
  3484. \\setlength{\\oddsidemargin}{1.5cm}
  3485. \\addtolength{\\oddsidemargin}{-2.54cm}
  3486. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3487. \\setlength{\\textheight}{\\paperheight}
  3488. \\addtolength{\\textheight}{-\\headheight}
  3489. \\addtolength{\\textheight}{-\\headsep}
  3490. \\addtolength{\\textheight}{-\\footskip}
  3491. \\addtolength{\\textheight}{-3cm}
  3492. \\setlength{\\topmargin}{1.5cm}
  3493. \\addtolength{\\topmargin}{-2.54cm}"
  3494. "The document header used for processing LaTeX fragments.
  3495. It is imperative that this header make sure that no page number
  3496. appears on the page. The package defined in the variables
  3497. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3498. will either replace the placeholder \"[PACKAGES]\" in this
  3499. header, or they will be appended."
  3500. :group 'org-latex
  3501. :type 'string)
  3502. (defun org-set-packages-alist (var val)
  3503. "Set the packages alist and make sure it has 3 elements per entry."
  3504. (set var (mapcar (lambda (x)
  3505. (if (and (consp x) (= (length x) 2))
  3506. (list (car x) (nth 1 x) t)
  3507. x))
  3508. val)))
  3509. (defun org-get-packages-alist (var)
  3510. "Get the packages alist and make sure it has 3 elements per entry."
  3511. (mapcar (lambda (x)
  3512. (if (and (consp x) (= (length x) 2))
  3513. (list (car x) (nth 1 x) t)
  3514. x))
  3515. (default-value var)))
  3516. (defcustom org-latex-default-packages-alist
  3517. '(("AUTO" "inputenc" t)
  3518. ("T1" "fontenc" t)
  3519. ("" "fixltx2e" nil)
  3520. ("" "graphicx" t)
  3521. ("" "grffile" t)
  3522. ("" "longtable" nil)
  3523. ("" "wrapfig" nil)
  3524. ("" "rotating" nil)
  3525. ("normalem" "ulem" t)
  3526. ("" "amsmath" t)
  3527. ("" "textcomp" t)
  3528. ("" "amssymb" t)
  3529. ("" "capt-of" nil)
  3530. ("" "hyperref" nil))
  3531. "Alist of default packages to be inserted in the header.
  3532. Change this only if one of the packages here causes an
  3533. incompatibility with another package you are using.
  3534. The packages in this list are needed by one part or another of
  3535. Org mode to function properly:
  3536. - inputenc, fontenc: for basic font and character selection
  3537. - fixltx2e: Important patches of LaTeX itself
  3538. - graphicx: for including images
  3539. - grffile: allow periods and spaces in graphics file names
  3540. - longtable: For multipage tables
  3541. - wrapfig: for figure placement
  3542. - rotating: for sideways figures and tables
  3543. - ulem: for underline and strike-through
  3544. - amsmath: for subscript and superscript and math environments
  3545. - textcomp, amssymb: for various symbols used
  3546. for interpreting the entities in `org-entities'. You can skip
  3547. some of these packages if you don't use any of their symbols.
  3548. - capt-of: for captions outside of floats
  3549. - hyperref: for cross references
  3550. Therefore you should not modify this variable unless you know
  3551. what you are doing. The one reason to change it anyway is that
  3552. you might be loading some other package that conflicts with one
  3553. of the default packages. Each element is either a cell or
  3554. a string.
  3555. A cell is of the format
  3556. \(\"options\" \"package\" SNIPPET-FLAG)
  3557. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3558. when compiling LaTeX snippets into images for inclusion into
  3559. non-LaTeX output.
  3560. A string will be inserted as-is in the header of the document."
  3561. :group 'org-latex
  3562. :group 'org-export-latex
  3563. :set 'org-set-packages-alist
  3564. :get 'org-get-packages-alist
  3565. :version "25.1"
  3566. :package-version '(Org . "8.3")
  3567. :type '(repeat
  3568. (choice
  3569. (list :tag "options/package pair"
  3570. (string :tag "options")
  3571. (string :tag "package")
  3572. (boolean :tag "Snippet"))
  3573. (string :tag "A line of LaTeX"))))
  3574. (defcustom org-latex-packages-alist nil
  3575. "Alist of packages to be inserted in every LaTeX header.
  3576. These will be inserted after `org-latex-default-packages-alist'.
  3577. Each element is either a cell or a string.
  3578. A cell is of the format:
  3579. \(\"options\" \"package\" SNIPPET-FLAG)
  3580. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3581. needed when turning LaTeX snippets into images for inclusion into
  3582. non-LaTeX output.
  3583. A string will be inserted as-is in the header of the document.
  3584. Make sure that you only list packages here which:
  3585. - you want in every file;
  3586. - do not conflict with the setup in `org-format-latex-header';
  3587. - do not conflict with the default packages in
  3588. `org-latex-default-packages-alist'."
  3589. :group 'org-latex
  3590. :group 'org-export-latex
  3591. :set 'org-set-packages-alist
  3592. :get 'org-get-packages-alist
  3593. :type '(repeat
  3594. (choice
  3595. (list :tag "options/package pair"
  3596. (string :tag "options")
  3597. (string :tag "package")
  3598. (boolean :tag "Snippet"))
  3599. (string :tag "A line of LaTeX"))))
  3600. (defgroup org-appearance nil
  3601. "Settings for Org-mode appearance."
  3602. :tag "Org Appearance"
  3603. :group 'org)
  3604. (defcustom org-level-color-stars-only nil
  3605. "Non-nil means fontify only the stars in each headline.
  3606. When nil, the entire headline is fontified.
  3607. Changing it requires restart of `font-lock-mode' to become effective
  3608. also in regions already fontified."
  3609. :group 'org-appearance
  3610. :type 'boolean)
  3611. (defcustom org-hide-leading-stars nil
  3612. "Non-nil means hide the first N-1 stars in a headline.
  3613. This works by using the face `org-hide' for these stars. This
  3614. face is white for a light background, and black for a dark
  3615. background. You may have to customize the face `org-hide' to
  3616. make this work.
  3617. Changing it requires restart of `font-lock-mode' to become effective
  3618. also in regions already fontified.
  3619. You may also set this on a per-file basis by adding one of the following
  3620. lines to the buffer:
  3621. #+STARTUP: hidestars
  3622. #+STARTUP: showstars"
  3623. :group 'org-appearance
  3624. :type 'boolean)
  3625. (defcustom org-hidden-keywords nil
  3626. "List of symbols corresponding to keywords to be hidden the org buffer.
  3627. For example, a value '(title) for this list will make the document's title
  3628. appear in the buffer without the initial #+TITLE: keyword."
  3629. :group 'org-appearance
  3630. :version "24.1"
  3631. :type '(set (const :tag "#+AUTHOR" author)
  3632. (const :tag "#+DATE" date)
  3633. (const :tag "#+EMAIL" email)
  3634. (const :tag "#+TITLE" title)))
  3635. (defcustom org-custom-properties nil
  3636. "List of properties (as strings) with a special meaning.
  3637. The default use of these custom properties is to let the user
  3638. hide them with `org-toggle-custom-properties-visibility'."
  3639. :group 'org-properties
  3640. :group 'org-appearance
  3641. :version "24.3"
  3642. :type '(repeat (string :tag "Property Name")))
  3643. (defcustom org-fontify-done-headline nil
  3644. "Non-nil means change the face of a headline if it is marked DONE.
  3645. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3646. When this is non-nil, the headline after the keyword is set to the
  3647. `org-headline-done' as an additional indication."
  3648. :group 'org-appearance
  3649. :type 'boolean)
  3650. (defcustom org-fontify-emphasized-text t
  3651. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3652. Changing this variable requires a restart of Emacs to take effect."
  3653. :group 'org-appearance
  3654. :type 'boolean)
  3655. (defcustom org-fontify-whole-heading-line nil
  3656. "Non-nil means fontify the whole line for headings.
  3657. This is useful when setting a background color for the
  3658. org-level-* faces."
  3659. :group 'org-appearance
  3660. :type 'boolean)
  3661. (defcustom org-highlight-latex-and-related nil
  3662. "Non-nil means highlight LaTeX related syntax in the buffer.
  3663. When non nil, the value should be a list containing any of the
  3664. following symbols:
  3665. `latex' Highlight LaTeX snippets and environments.
  3666. `script' Highlight subscript and superscript.
  3667. `entities' Highlight entities."
  3668. :group 'org-appearance
  3669. :version "24.4"
  3670. :package-version '(Org . "8.0")
  3671. :type '(choice
  3672. (const :tag "No highlighting" nil)
  3673. (set :greedy t :tag "Highlight"
  3674. (const :tag "LaTeX snippets and environments" latex)
  3675. (const :tag "Subscript and superscript" script)
  3676. (const :tag "Entities" entities))))
  3677. (defcustom org-hide-emphasis-markers nil
  3678. "Non-nil mean font-lock should hide the emphasis marker characters."
  3679. :group 'org-appearance
  3680. :type 'boolean)
  3681. (defcustom org-hide-macro-markers nil
  3682. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3683. :group 'org-appearance
  3684. :type 'boolean)
  3685. (defcustom org-pretty-entities nil
  3686. "Non-nil means show entities as UTF8 characters.
  3687. When nil, the \\name form remains in the buffer."
  3688. :group 'org-appearance
  3689. :version "24.1"
  3690. :type 'boolean)
  3691. (defcustom org-pretty-entities-include-sub-superscripts t
  3692. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3693. :group 'org-appearance
  3694. :version "24.1"
  3695. :type 'boolean)
  3696. (defvar org-emph-re nil
  3697. "Regular expression for matching emphasis.
  3698. After a match, the match groups contain these elements:
  3699. 0 The match of the full regular expression, including the characters
  3700. before and after the proper match
  3701. 1 The character before the proper match, or empty at beginning of line
  3702. 2 The proper match, including the leading and trailing markers
  3703. 3 The leading marker like * or /, indicating the type of highlighting
  3704. 4 The text between the emphasis markers, not including the markers
  3705. 5 The character after the match, empty at the end of a line")
  3706. (defvar org-verbatim-re nil
  3707. "Regular expression for matching verbatim text.")
  3708. (defvar org-emphasis-regexp-components) ; defined just below
  3709. (defvar org-emphasis-alist) ; defined just below
  3710. (defun org-set-emph-re (var val)
  3711. "Set variable and compute the emphasis regular expression."
  3712. (set var val)
  3713. (when (and (boundp 'org-emphasis-alist)
  3714. (boundp 'org-emphasis-regexp-components)
  3715. org-emphasis-alist org-emphasis-regexp-components)
  3716. (let* ((e org-emphasis-regexp-components)
  3717. (pre (car e))
  3718. (post (nth 1 e))
  3719. (border (nth 2 e))
  3720. (body (nth 3 e))
  3721. (nl (nth 4 e))
  3722. (body1 (concat body "*?"))
  3723. (markers (mapconcat 'car org-emphasis-alist ""))
  3724. (vmarkers (mapconcat
  3725. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3726. org-emphasis-alist "")))
  3727. ;; make sure special characters appear at the right position in the class
  3728. (if (string-match "\\^" markers)
  3729. (setq markers (concat (replace-match "" t t markers) "^")))
  3730. (if (string-match "-" markers)
  3731. (setq markers (concat (replace-match "" t t markers) "-")))
  3732. (if (string-match "\\^" vmarkers)
  3733. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3734. (if (string-match "-" vmarkers)
  3735. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3736. (if (> nl 0)
  3737. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3738. (int-to-string nl) "\\}")))
  3739. ;; Make the regexp
  3740. (setq org-emph-re
  3741. (concat "\\([" pre "]\\|^\\)"
  3742. "\\("
  3743. "\\([" markers "]\\)"
  3744. "\\("
  3745. "[^" border "]\\|"
  3746. "[^" border "]"
  3747. body1
  3748. "[^" border "]"
  3749. "\\)"
  3750. "\\3\\)"
  3751. "\\([" post "]\\|$\\)"))
  3752. (setq org-verbatim-re
  3753. (concat "\\([" pre "]\\|^\\)"
  3754. "\\("
  3755. "\\([" vmarkers "]\\)"
  3756. "\\("
  3757. "[^" border "]\\|"
  3758. "[^" border "]"
  3759. body1
  3760. "[^" border "]"
  3761. "\\)"
  3762. "\\3\\)"
  3763. "\\([" post "]\\|$\\)")))))
  3764. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3765. ;; set this option proved cumbersome. See this message/thread:
  3766. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3767. (defvar org-emphasis-regexp-components
  3768. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3769. "Components used to build the regular expression for emphasis.
  3770. This is a list with five entries. Terminology: In an emphasis string
  3771. like \" *strong word* \", we call the initial space PREMATCH, the final
  3772. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3773. and \"trong wor\" is the body. The different components in this variable
  3774. specify what is allowed/forbidden in each part:
  3775. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3776. post Chars allowed as postmatch. End of line will be allowed too.
  3777. border The chars *forbidden* as border characters.
  3778. body-regexp A regexp like \".\" to match a body character. Don't use
  3779. non-shy groups here, and don't allow newline here.
  3780. newline The maximum number of newlines allowed in an emphasis exp.
  3781. You need to reload Org or to restart Emacs after customizing this.")
  3782. (defcustom org-emphasis-alist
  3783. `(("*" bold)
  3784. ("/" italic)
  3785. ("_" underline)
  3786. ("=" org-verbatim verbatim)
  3787. ("~" org-code verbatim)
  3788. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3789. "Alist of characters and faces to emphasize text.
  3790. Text starting and ending with a special character will be emphasized,
  3791. for example *bold*, _underlined_ and /italic/. This variable sets the
  3792. marker characters and the face to be used by font-lock for highlighting
  3793. in Org-mode Emacs buffers.
  3794. You need to reload Org or to restart Emacs after customizing this."
  3795. :group 'org-appearance
  3796. :set 'org-set-emph-re
  3797. :version "24.4"
  3798. :package-version '(Org . "8.0")
  3799. :type '(repeat
  3800. (list
  3801. (string :tag "Marker character")
  3802. (choice
  3803. (face :tag "Font-lock-face")
  3804. (plist :tag "Face property list"))
  3805. (option (const verbatim)))))
  3806. (defvar org-protecting-blocks
  3807. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3808. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3809. This is needed for font-lock setup.")
  3810. ;;; Miscellaneous options
  3811. (defgroup org-completion nil
  3812. "Completion in Org-mode."
  3813. :tag "Org Completion"
  3814. :group 'org)
  3815. (defcustom org-completion-use-ido nil
  3816. "Non-nil means use ido completion wherever possible.
  3817. Note that `ido-mode' must be active for this variable to be relevant.
  3818. If you decide to turn this variable on, you might well want to turn off
  3819. `org-outline-path-complete-in-steps'.
  3820. See also `org-completion-use-iswitchb'."
  3821. :group 'org-completion
  3822. :type 'boolean)
  3823. (defcustom org-completion-use-iswitchb nil
  3824. "Non-nil means use iswitchb completion wherever possible.
  3825. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3826. If you decide to turn this variable on, you might well want to turn off
  3827. `org-outline-path-complete-in-steps'.
  3828. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3829. :group 'org-completion
  3830. :type 'boolean)
  3831. (defcustom org-completion-fallback-command 'hippie-expand
  3832. "The expansion command called by \\[pcomplete] in normal context.
  3833. Normal means, no org-mode-specific context."
  3834. :group 'org-completion
  3835. :type 'function)
  3836. ;;; Functions and variables from their packages
  3837. ;; Declared here to avoid compiler warnings
  3838. ;; XEmacs only
  3839. (defvar outline-mode-menu-heading)
  3840. (defvar outline-mode-menu-show)
  3841. (defvar outline-mode-menu-hide)
  3842. (defvar zmacs-regions) ; XEmacs regions
  3843. ;; Emacs only
  3844. (defvar mark-active)
  3845. ;; Various packages
  3846. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3847. (declare-function calendar-forward-day "cal-move" (arg))
  3848. (declare-function calendar-goto-date "cal-move" (date))
  3849. (declare-function calendar-goto-today "cal-move" ())
  3850. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3851. (defvar calc-embedded-close-formula)
  3852. (defvar calc-embedded-open-formula)
  3853. (declare-function cdlatex-tab "ext:cdlatex" ())
  3854. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3855. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3856. (defvar font-lock-unfontify-region-function)
  3857. (declare-function iswitchb-read-buffer "iswitchb"
  3858. (prompt &optional default require-match start matches-set))
  3859. (defvar iswitchb-temp-buflist)
  3860. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3861. (defvar org-agenda-tags-todo-honor-ignore-options)
  3862. (declare-function org-agenda-skip "org-agenda" ())
  3863. (declare-function
  3864. org-agenda-format-item "org-agenda"
  3865. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3866. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3867. (declare-function org-agenda-change-all-lines "org-agenda"
  3868. (newhead hdmarker &optional fixface just-this))
  3869. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3870. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3871. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3872. (beg end))
  3873. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3874. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3875. "org-agenda" (&optional end))
  3876. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3877. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3878. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3879. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3880. (declare-function org-indent-mode "org-indent" (&optional arg))
  3881. (declare-function parse-time-string "parse-time" (string))
  3882. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3883. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3884. (defvar remember-data-file)
  3885. (defvar texmathp-why)
  3886. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3887. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3888. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3889. ;;;###autoload
  3890. (defun turn-on-orgtbl ()
  3891. "Unconditionally turn on `orgtbl-mode'."
  3892. (require 'org-table)
  3893. (orgtbl-mode 1))
  3894. (defun org-at-table-p (&optional table-type)
  3895. "Non-nil if the cursor is inside an Org table.
  3896. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3897. If `org-enable-table-editor' is nil, return nil unconditionally."
  3898. (and
  3899. org-enable-table-editor
  3900. (save-excursion
  3901. (beginning-of-line)
  3902. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3903. (or (not (derived-mode-p 'org-mode))
  3904. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3905. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3906. (defsubst org-table-p () (org-at-table-p))
  3907. (defun org-at-table.el-p ()
  3908. "Non-nil when point is at a table.el table."
  3909. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3910. (let ((element (org-element-at-point)))
  3911. (and (eq (org-element-type element) 'table)
  3912. (eq (org-element-property :type element) 'table.el)))))
  3913. (defun org-table-recognize-table.el ()
  3914. "If there is a table.el table nearby, recognize it and move into it."
  3915. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3916. (beginning-of-line)
  3917. (unless (or (looking-at org-table-dataline-regexp)
  3918. (not (looking-at org-table1-hline-regexp)))
  3919. (forward-line)
  3920. (when (looking-at org-table-any-border-regexp)
  3921. (forward-line -2)))
  3922. (if (re-search-forward "|" (org-table-end t) t)
  3923. (progn
  3924. (require 'table)
  3925. (if (table--at-cell-p (point)) t
  3926. (message "recognizing table.el table...")
  3927. (table-recognize-table)
  3928. (message "recognizing table.el table...done")))
  3929. (error "This should not happen"))))
  3930. (defun org-at-table-hline-p ()
  3931. "Non-nil when point is inside a hline in a table.
  3932. Assume point is already in a table. If `org-enable-table-editor'
  3933. is nil, return nil unconditionally."
  3934. (and org-enable-table-editor
  3935. (save-excursion
  3936. (beginning-of-line)
  3937. (looking-at org-table-hline-regexp))))
  3938. (defun org-table-map-tables (function &optional quietly)
  3939. "Apply FUNCTION to the start of all tables in the buffer."
  3940. (save-excursion
  3941. (save-restriction
  3942. (widen)
  3943. (goto-char (point-min))
  3944. (while (re-search-forward org-table-any-line-regexp nil t)
  3945. (unless quietly
  3946. (message "Mapping tables: %d%%"
  3947. (floor (* 100.0 (point)) (buffer-size))))
  3948. (beginning-of-line 1)
  3949. (when (and (looking-at org-table-line-regexp)
  3950. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3951. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3952. (save-excursion (funcall function))
  3953. (or (looking-at org-table-line-regexp)
  3954. (forward-char 1)))
  3955. (re-search-forward org-table-any-border-regexp nil 1))))
  3956. (unless quietly (message "Mapping tables: done")))
  3957. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3958. (declare-function org-clock-update-mode-line "org-clock" ())
  3959. (declare-function org-resolve-clocks "org-clock"
  3960. (&optional also-non-dangling-p prompt last-valid))
  3961. (defun org-at-TBLFM-p (&optional pos)
  3962. "Non-nil when point (or POS) is in #+TBLFM line."
  3963. (save-excursion
  3964. (goto-char (or pos (point)))
  3965. (beginning-of-line)
  3966. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3967. (eq (org-element-type (org-element-at-point)) 'table))))
  3968. (defvar org-clock-start-time)
  3969. (defvar org-clock-marker (make-marker)
  3970. "Marker recording the last clock-in.")
  3971. (defvar org-clock-hd-marker (make-marker)
  3972. "Marker recording the last clock-in, but the headline position.")
  3973. (defvar org-clock-heading ""
  3974. "The heading of the current clock entry.")
  3975. (defun org-clock-is-active ()
  3976. "Return the buffer where the clock is currently running.
  3977. Return nil if no clock is running."
  3978. (marker-buffer org-clock-marker))
  3979. (defun org-check-running-clock ()
  3980. "Check if the current buffer contains the running clock.
  3981. If yes, offer to stop it and to save the buffer with the changes."
  3982. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3983. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3984. (buffer-name))))
  3985. (org-clock-out)
  3986. (when (y-or-n-p "Save changed buffer?")
  3987. (save-buffer))))
  3988. (defun org-clocktable-try-shift (dir n)
  3989. "Check if this line starts a clock table, if yes, shift the time block."
  3990. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3991. (org-clocktable-shift dir n)))
  3992. ;;;###autoload
  3993. (defun org-clock-persistence-insinuate ()
  3994. "Set up hooks for clock persistence."
  3995. (require 'org-clock)
  3996. (add-hook 'org-mode-hook 'org-clock-load)
  3997. (add-hook 'kill-emacs-hook 'org-clock-save))
  3998. (defgroup org-archive nil
  3999. "Options concerning archiving in Org-mode."
  4000. :tag "Org Archive"
  4001. :group 'org-structure)
  4002. (defcustom org-archive-location "%s_archive::"
  4003. "The location where subtrees should be archived.
  4004. The value of this variable is a string, consisting of two parts,
  4005. separated by a double-colon. The first part is a filename and
  4006. the second part is a headline.
  4007. When the filename is omitted, archiving happens in the same file.
  4008. %s in the filename will be replaced by the current file
  4009. name (without the directory part). Archiving to a different file
  4010. is useful to keep archived entries from contributing to the
  4011. Org-mode Agenda.
  4012. The archived entries will be filed as subtrees of the specified
  4013. headline. When the headline is omitted, the subtrees are simply
  4014. filed away at the end of the file, as top-level entries. Also in
  4015. the heading you can use %s to represent the file name, this can be
  4016. useful when using the same archive for a number of different files.
  4017. Here are a few examples:
  4018. \"%s_archive::\"
  4019. If the current file is Projects.org, archive in file
  4020. Projects.org_archive, as top-level trees. This is the default.
  4021. \"::* Archived Tasks\"
  4022. Archive in the current file, under the top-level headline
  4023. \"* Archived Tasks\".
  4024. \"~/org/archive.org::\"
  4025. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4026. \"~/org/archive.org::* From %s\"
  4027. Archive in file ~/org/archive.org (absolute path), under headlines
  4028. \"From FILENAME\" where file name is the current file name.
  4029. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4030. The \"datetree/\" string is special, signifying to archive
  4031. items to the datetree. Items are placed in either the CLOSED
  4032. date of the item, or the current date if there is no CLOSED date.
  4033. The heading will be a subentry to the current date. There doesn't
  4034. need to be a heading, but there always needs to be a slash after
  4035. datetree. For example, to store archived items directly in the
  4036. datetree, use \"~/org/datetree.org::datetree/\".
  4037. \"basement::** Finished Tasks\"
  4038. Archive in file ./basement (relative path), as level 3 trees
  4039. below the level 2 heading \"** Finished Tasks\".
  4040. You may set this option on a per-file basis by adding to the buffer a
  4041. line like
  4042. #+ARCHIVE: basement::** Finished Tasks
  4043. You may also define it locally for a subtree by setting an ARCHIVE property
  4044. in the entry. If such a property is found in an entry, or anywhere up
  4045. the hierarchy, it will be used."
  4046. :group 'org-archive
  4047. :type 'string)
  4048. (defcustom org-agenda-skip-archived-trees t
  4049. "Non-nil means the agenda will skip any items located in archived trees.
  4050. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4051. variable is no longer recommended, you should leave it at the value t.
  4052. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4053. :group 'org-archive
  4054. :group 'org-agenda-skip
  4055. :type 'boolean)
  4056. (defcustom org-columns-skip-archived-trees t
  4057. "Non-nil means ignore archived trees when creating column view."
  4058. :group 'org-archive
  4059. :group 'org-properties
  4060. :type 'boolean)
  4061. (defcustom org-cycle-open-archived-trees nil
  4062. "Non-nil means `org-cycle' will open archived trees.
  4063. An archived tree is a tree marked with the tag ARCHIVE.
  4064. When nil, archived trees will stay folded. You can still open them with
  4065. normal outline commands like `show-all', but not with the cycling commands."
  4066. :group 'org-archive
  4067. :group 'org-cycle
  4068. :type 'boolean)
  4069. (defcustom org-sparse-tree-open-archived-trees nil
  4070. "Non-nil means sparse tree construction shows matches in archived trees.
  4071. When nil, matches in these trees are highlighted, but the trees are kept in
  4072. collapsed state."
  4073. :group 'org-archive
  4074. :group 'org-sparse-trees
  4075. :type 'boolean)
  4076. (defcustom org-sparse-tree-default-date-type nil
  4077. "The default date type when building a sparse tree.
  4078. When this is nil, a date is a scheduled or a deadline timestamp.
  4079. Otherwise, these types are allowed:
  4080. all: all timestamps
  4081. active: only active timestamps (<...>)
  4082. inactive: only inactive timestamps ([...])
  4083. scheduled: only scheduled timestamps
  4084. deadline: only deadline timestamps"
  4085. :type '(choice (const :tag "Scheduled or deadline" nil)
  4086. (const :tag "All timestamps" all)
  4087. (const :tag "Only active timestamps" active)
  4088. (const :tag "Only inactive timestamps" inactive)
  4089. (const :tag "Only scheduled timestamps" scheduled)
  4090. (const :tag "Only deadline timestamps" deadline)
  4091. (const :tag "Only closed timestamps" closed))
  4092. :version "25.1"
  4093. :package-version '(Org . "8.3")
  4094. :group 'org-sparse-trees)
  4095. (defun org-cycle-hide-archived-subtrees (state)
  4096. "Re-hide all archived subtrees after a visibility state change."
  4097. (when (and (not org-cycle-open-archived-trees)
  4098. (not (memq state '(overview folded))))
  4099. (save-excursion
  4100. (let* ((globalp (memq state '(contents all)))
  4101. (beg (if globalp (point-min) (point)))
  4102. (end (if globalp (point-max) (org-end-of-subtree t))))
  4103. (org-hide-archived-subtrees beg end)
  4104. (goto-char beg)
  4105. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4106. (message "%s" (substitute-command-keys
  4107. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4108. (defun org-force-cycle-archived ()
  4109. "Cycle subtree even if it is archived."
  4110. (interactive)
  4111. (setq this-command 'org-cycle)
  4112. (let ((org-cycle-open-archived-trees t))
  4113. (call-interactively 'org-cycle)))
  4114. (defun org-hide-archived-subtrees (beg end)
  4115. "Re-hide all archived subtrees after a visibility state change."
  4116. (org-with-wide-buffer
  4117. (let ((case-fold-search nil)
  4118. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4119. (goto-char beg)
  4120. (while (and (< (point) end) (re-search-forward re end t))
  4121. (when (member org-archive-tag (org-get-tags))
  4122. (org-flag-subtree t)
  4123. (org-end-of-subtree t))))))
  4124. (declare-function outline-end-of-heading "outline" ())
  4125. (declare-function outline-flag-region "outline" (from to flag))
  4126. (defun org-flag-subtree (flag)
  4127. (save-excursion
  4128. (org-back-to-heading t)
  4129. (outline-end-of-heading)
  4130. (outline-flag-region (point)
  4131. (progn (org-end-of-subtree t) (point))
  4132. flag)))
  4133. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4134. ;; Declare Column View Code
  4135. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4136. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4137. (declare-function org-columns-compute "org-colview" (property))
  4138. ;; Declare ID code
  4139. (declare-function org-id-store-link "org-id")
  4140. (declare-function org-id-locations-load "org-id")
  4141. (declare-function org-id-locations-save "org-id")
  4142. (defvar org-id-track-globally)
  4143. ;;; Variables for pre-computed regular expressions, all buffer local
  4144. (defvar org-todo-regexp nil
  4145. "Matches any of the TODO state keywords.")
  4146. (make-variable-buffer-local 'org-todo-regexp)
  4147. (defvar org-not-done-regexp nil
  4148. "Matches any of the TODO state keywords except the last one.")
  4149. (make-variable-buffer-local 'org-not-done-regexp)
  4150. (defvar org-not-done-heading-regexp nil
  4151. "Matches a TODO headline that is not done.")
  4152. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4153. (defvar org-todo-line-regexp nil
  4154. "Matches a headline and puts TODO state into group 2 if present.")
  4155. (make-variable-buffer-local 'org-todo-line-regexp)
  4156. (defvar org-complex-heading-regexp nil
  4157. "Matches a headline and puts everything into groups:
  4158. group 1: the stars
  4159. group 2: The todo keyword, maybe
  4160. group 3: Priority cookie
  4161. group 4: True headline
  4162. group 5: Tags")
  4163. (make-variable-buffer-local 'org-complex-heading-regexp)
  4164. (defvar org-complex-heading-regexp-format nil
  4165. "Printf format to make regexp to match an exact headline.
  4166. This regexp will match the headline of any node which has the
  4167. exact headline text that is put into the format, but may have any
  4168. TODO state, priority and tags.")
  4169. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4170. (defvar org-todo-line-tags-regexp nil
  4171. "Matches a headline and puts TODO state into group 2 if present.
  4172. Also put tags into group 4 if tags are present.")
  4173. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4174. (defconst org-plain-time-of-day-regexp
  4175. (concat
  4176. "\\(\\<[012]?[0-9]"
  4177. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4178. "\\(--?"
  4179. "\\(\\<[012]?[0-9]"
  4180. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4181. "\\)?")
  4182. "Regular expression to match a plain time or time range.
  4183. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4184. groups carry important information:
  4185. 0 the full match
  4186. 1 the first time, range or not
  4187. 8 the second time, if it is a range.")
  4188. (defconst org-plain-time-extension-regexp
  4189. (concat
  4190. "\\(\\<[012]?[0-9]"
  4191. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4192. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4193. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4194. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4195. groups carry important information:
  4196. 0 the full match
  4197. 7 hours of duration
  4198. 9 minutes of duration")
  4199. (defconst org-stamp-time-of-day-regexp
  4200. (concat
  4201. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4202. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4203. "\\(--?"
  4204. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4205. "Regular expression to match a timestamp time or time range.
  4206. After a match, the following groups carry important information:
  4207. 0 the full match
  4208. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4209. 2 the first time, range or not
  4210. 4 the second time, if it is a range.")
  4211. (defconst org-startup-options
  4212. '(("fold" org-startup-folded t)
  4213. ("overview" org-startup-folded t)
  4214. ("nofold" org-startup-folded nil)
  4215. ("showall" org-startup-folded nil)
  4216. ("showeverything" org-startup-folded showeverything)
  4217. ("content" org-startup-folded content)
  4218. ("indent" org-startup-indented t)
  4219. ("noindent" org-startup-indented nil)
  4220. ("hidestars" org-hide-leading-stars t)
  4221. ("showstars" org-hide-leading-stars nil)
  4222. ("odd" org-odd-levels-only t)
  4223. ("oddeven" org-odd-levels-only nil)
  4224. ("align" org-startup-align-all-tables t)
  4225. ("noalign" org-startup-align-all-tables nil)
  4226. ("inlineimages" org-startup-with-inline-images t)
  4227. ("noinlineimages" org-startup-with-inline-images nil)
  4228. ("latexpreview" org-startup-with-latex-preview t)
  4229. ("nolatexpreview" org-startup-with-latex-preview nil)
  4230. ("customtime" org-display-custom-times t)
  4231. ("logdone" org-log-done time)
  4232. ("lognotedone" org-log-done note)
  4233. ("nologdone" org-log-done nil)
  4234. ("lognoteclock-out" org-log-note-clock-out t)
  4235. ("nolognoteclock-out" org-log-note-clock-out nil)
  4236. ("logrepeat" org-log-repeat state)
  4237. ("lognoterepeat" org-log-repeat note)
  4238. ("logdrawer" org-log-into-drawer t)
  4239. ("nologdrawer" org-log-into-drawer nil)
  4240. ("logstatesreversed" org-log-states-order-reversed t)
  4241. ("nologstatesreversed" org-log-states-order-reversed nil)
  4242. ("nologrepeat" org-log-repeat nil)
  4243. ("logreschedule" org-log-reschedule time)
  4244. ("lognotereschedule" org-log-reschedule note)
  4245. ("nologreschedule" org-log-reschedule nil)
  4246. ("logredeadline" org-log-redeadline time)
  4247. ("lognoteredeadline" org-log-redeadline note)
  4248. ("nologredeadline" org-log-redeadline nil)
  4249. ("logrefile" org-log-refile time)
  4250. ("lognoterefile" org-log-refile note)
  4251. ("nologrefile" org-log-refile nil)
  4252. ("fninline" org-footnote-define-inline t)
  4253. ("nofninline" org-footnote-define-inline nil)
  4254. ("fnlocal" org-footnote-section nil)
  4255. ("fnauto" org-footnote-auto-label t)
  4256. ("fnprompt" org-footnote-auto-label nil)
  4257. ("fnconfirm" org-footnote-auto-label confirm)
  4258. ("fnplain" org-footnote-auto-label plain)
  4259. ("fnadjust" org-footnote-auto-adjust t)
  4260. ("nofnadjust" org-footnote-auto-adjust nil)
  4261. ("constcgs" constants-unit-system cgs)
  4262. ("constSI" constants-unit-system SI)
  4263. ("noptag" org-tag-persistent-alist nil)
  4264. ("hideblocks" org-hide-block-startup t)
  4265. ("nohideblocks" org-hide-block-startup nil)
  4266. ("beamer" org-startup-with-beamer-mode t)
  4267. ("entitiespretty" org-pretty-entities t)
  4268. ("entitiesplain" org-pretty-entities nil))
  4269. "Variable associated with STARTUP options for org-mode.
  4270. Each element is a list of three items: the startup options (as written
  4271. in the #+STARTUP line), the corresponding variable, and the value to set
  4272. this variable to if the option is found. An optional forth element PUSH
  4273. means to push this value onto the list in the variable.")
  4274. (defcustom org-group-tags t
  4275. "When non-nil (the default), use group tags.
  4276. This can be turned on/off through `org-toggle-tags-groups'."
  4277. :group 'org-tags
  4278. :group 'org-startup
  4279. :type 'boolean)
  4280. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4281. (defun org-toggle-tags-groups ()
  4282. "Toggle support for group tags.
  4283. Support for group tags is controlled by the option
  4284. `org-group-tags', which is non-nil by default."
  4285. (interactive)
  4286. (setq org-group-tags (not org-group-tags))
  4287. (cond ((and (derived-mode-p 'org-agenda-mode)
  4288. org-group-tags)
  4289. (org-agenda-redo))
  4290. ((derived-mode-p 'org-mode)
  4291. (let ((org-inhibit-startup t)) (org-mode))))
  4292. (message "Groups tags support has been turned %s"
  4293. (if org-group-tags "on" "off")))
  4294. (defun org-set-regexps-and-options (&optional tags-only)
  4295. "Precompute regular expressions used in the current buffer.
  4296. When optional argument TAGS-ONLY is non-nil, only compute tags
  4297. related expressions."
  4298. (when (derived-mode-p 'org-mode)
  4299. (let ((alist (org--setup-collect-keywords
  4300. (org-make-options-regexp
  4301. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4302. (and (not tags-only)
  4303. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4304. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4305. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4306. (org--setup-process-tags
  4307. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4308. (unless tags-only
  4309. ;; File properties.
  4310. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4311. ;; Archive location.
  4312. (let ((archive (cdr (assq 'archive alist))))
  4313. (when archive (org-set-local 'org-archive-location archive)))
  4314. ;; Category.
  4315. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4316. (when cat
  4317. (org-set-local 'org-category (intern cat))
  4318. (org-set-local 'org-file-properties
  4319. (org--update-property-plist
  4320. "CATEGORY" cat org-file-properties))))
  4321. ;; Columns.
  4322. (let ((column (cdr (assq 'columns alist))))
  4323. (when column (org-set-local 'org-columns-default-format column)))
  4324. ;; Constants.
  4325. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4326. ;; Link abbreviations.
  4327. (let ((links (cdr (assq 'link alist))))
  4328. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4329. ;; Priorities.
  4330. (let ((priorities (cdr (assq 'priorities alist))))
  4331. (when priorities
  4332. (org-set-local 'org-highest-priority (nth 0 priorities))
  4333. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4334. (org-set-local 'org-default-priority (nth 2 priorities))))
  4335. ;; Scripts.
  4336. (let ((scripts (assq 'scripts alist)))
  4337. (when scripts
  4338. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4339. ;; Startup options.
  4340. (let ((startup (cdr (assq 'startup alist))))
  4341. (dolist (option startup)
  4342. (let ((entry (assoc-string option org-startup-options t)))
  4343. (when entry
  4344. (let ((var (nth 1 entry))
  4345. (val (nth 2 entry)))
  4346. (if (not (nth 3 entry)) (org-set-local var val)
  4347. (unless (listp (symbol-value var))
  4348. (org-set-local var nil))
  4349. (add-to-list var val)))))))
  4350. ;; TODO keywords.
  4351. (org-set-local 'org-todo-kwd-alist nil)
  4352. (org-set-local 'org-todo-key-alist nil)
  4353. (org-set-local 'org-todo-key-trigger nil)
  4354. (org-set-local 'org-todo-keywords-1 nil)
  4355. (org-set-local 'org-done-keywords nil)
  4356. (org-set-local 'org-todo-heads nil)
  4357. (org-set-local 'org-todo-sets nil)
  4358. (org-set-local 'org-todo-log-states nil)
  4359. (let ((todo-sequences
  4360. (or (nreverse (cdr (assq 'todo alist)))
  4361. (let ((d (default-value 'org-todo-keywords)))
  4362. (if (not (stringp (car d))) d
  4363. ;; XXX: Backward compatibility code.
  4364. (list (cons org-todo-interpretation d)))))))
  4365. (dolist (sequence todo-sequences)
  4366. (let* ((sequence (or (run-hook-with-args-until-success
  4367. 'org-todo-setup-filter-hook sequence)
  4368. sequence))
  4369. (sequence-type (car sequence))
  4370. (keywords (cdr sequence))
  4371. (sep (member "|" keywords))
  4372. names alist)
  4373. (dolist (k (remove "|" keywords))
  4374. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4375. k)
  4376. (error "Invalid TODO keyword %s" k))
  4377. (let ((name (match-string 1 k))
  4378. (key (match-string 2 k))
  4379. (log (org-extract-log-state-settings k)))
  4380. (push name names)
  4381. (push (cons name (and key (string-to-char key))) alist)
  4382. (when log (push log org-todo-log-states))))
  4383. (let* ((names (nreverse names))
  4384. (done (if sep (org-remove-keyword-keys (cdr sep))
  4385. (last names)))
  4386. (head (car names))
  4387. (tail (list sequence-type head (car done) (org-last done))))
  4388. (add-to-list 'org-todo-heads head 'append)
  4389. (push names org-todo-sets)
  4390. (setq org-done-keywords (append org-done-keywords done nil))
  4391. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4392. (setq org-todo-key-alist
  4393. (append org-todo-key-alist
  4394. (and alist
  4395. (append '((:startgroup))
  4396. (nreverse alist)
  4397. '((:endgroup))))))
  4398. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4399. (setq org-todo-sets (nreverse org-todo-sets)
  4400. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4401. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4402. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4403. ;; Compute the regular expressions and other local variables.
  4404. ;; Using `org-outline-regexp-bol' would complicate them much,
  4405. ;; because of the fixed white space at the end of that string.
  4406. (if (not org-done-keywords)
  4407. (setq org-done-keywords
  4408. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4409. (setq org-not-done-keywords
  4410. (org-delete-all org-done-keywords
  4411. (copy-sequence org-todo-keywords-1))
  4412. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4413. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4414. org-not-done-heading-regexp
  4415. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4416. org-todo-line-regexp
  4417. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4418. org-complex-heading-regexp
  4419. (concat "^\\(\\*+\\)"
  4420. "\\(?: +" org-todo-regexp "\\)?"
  4421. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4422. "\\(?: +\\(.*?\\)\\)??"
  4423. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4424. "[ \t]*$")
  4425. org-complex-heading-regexp-format
  4426. (concat "^\\(\\*+\\)"
  4427. "\\(?: +" org-todo-regexp "\\)?"
  4428. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4429. "\\(?: +"
  4430. ;; Stats cookies can be stuck to body.
  4431. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4432. "\\(%s\\)"
  4433. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4434. "\\)"
  4435. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4436. "[ \t]*$")
  4437. org-todo-line-tags-regexp
  4438. (concat "^\\(\\*+\\)"
  4439. "\\(?: +" org-todo-regexp "\\)?"
  4440. "\\(?: +\\(.*?\\)\\)??"
  4441. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4442. "[ \t]*$"))
  4443. (org-compute-latex-and-related-regexp)))))
  4444. (defun org--setup-collect-keywords (regexp &optional files alist)
  4445. "Return setup keywords values as an alist.
  4446. REGEXP matches a subset of setup keywords. FILES is a list of
  4447. file names already visited. It is used to avoid circular setup
  4448. files. ALIST, when non-nil, is the alist computed so far.
  4449. Return value contains the following keys: `archive', `category',
  4450. `columns', `constants', `filetags', `link', `priorities',
  4451. `property', `scripts', `startup', `tags' and `todo'."
  4452. (org-with-wide-buffer
  4453. (goto-char (point-min))
  4454. (let ((case-fold-search t))
  4455. (while (re-search-forward regexp nil t)
  4456. (let ((element (org-element-at-point)))
  4457. (when (eq (org-element-type element) 'keyword)
  4458. (let ((key (org-element-property :key element))
  4459. (value (org-element-property :value element)))
  4460. (cond
  4461. ((equal key "ARCHIVE")
  4462. (when (org-string-nw-p value)
  4463. (push (cons 'archive value) alist)))
  4464. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4465. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4466. ((equal key "CONSTANTS")
  4467. (let* ((constants (assq 'constants alist))
  4468. (store (cdr constants)))
  4469. (dolist (pair (org-split-string value))
  4470. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4471. pair)
  4472. (let* ((name (match-string 1 pair))
  4473. (value (match-string 2 pair))
  4474. (old (assoc name store)))
  4475. (if old (setcdr old value)
  4476. (push (cons name value) store)))))
  4477. (if constants (setcdr constants store)
  4478. (push (cons 'constants store) alist))))
  4479. ((equal key "FILETAGS")
  4480. (when (org-string-nw-p value)
  4481. (let ((old (assq 'filetags alist))
  4482. (new (apply #'nconc
  4483. (mapcar (lambda (x) (org-split-string x ":"))
  4484. (org-split-string value)))))
  4485. (if old (setcdr old (append new (cdr old)))
  4486. (push (cons 'filetags new) alist)))))
  4487. ((equal key "LINK")
  4488. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4489. (let ((links (assq 'link alist))
  4490. (pair (cons (org-match-string-no-properties 1 value)
  4491. (org-match-string-no-properties 2 value))))
  4492. (if links (push pair (cdr links))
  4493. (push (list 'link pair) alist)))))
  4494. ((equal key "OPTIONS")
  4495. (when (and (org-string-nw-p value)
  4496. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4497. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4498. ((equal key "PRIORITIES")
  4499. (push (cons 'priorities
  4500. (let ((prio (org-split-string value)))
  4501. (if (< (length prio) 3) '(?A ?C ?B)
  4502. (mapcar #'string-to-char prio))))
  4503. alist))
  4504. ((equal key "PROPERTY")
  4505. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4506. (let* ((property (assq 'property alist))
  4507. (value (org--update-property-plist
  4508. (org-match-string-no-properties 1 value)
  4509. (org-match-string-no-properties 2 value)
  4510. (cdr property))))
  4511. (if property (setcdr property value)
  4512. (push (cons 'property value) alist)))))
  4513. ((equal key "STARTUP")
  4514. (let ((startup (assq 'startup alist)))
  4515. (if startup
  4516. (setcdr startup
  4517. (append (cdr startup) (org-split-string value)))
  4518. (push (cons 'startup (org-split-string value)) alist))))
  4519. ((equal key "TAGS")
  4520. (let ((tag-cell (assq 'tags alist)))
  4521. (if tag-cell
  4522. (setcdr tag-cell
  4523. (append (cdr tag-cell)
  4524. '("\\n")
  4525. (org-split-string value)))
  4526. (push (cons 'tags (org-split-string value)) alist))))
  4527. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4528. (let ((todo (assq 'todo alist))
  4529. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4530. (org-split-string value))))
  4531. (if todo (push value (cdr todo))
  4532. (push (list 'todo value) alist))))
  4533. ((equal key "SETUPFILE")
  4534. (unless buffer-read-only ; Do not check in Gnus messages.
  4535. (let ((f (and (org-string-nw-p value)
  4536. (expand-file-name
  4537. (org-remove-double-quotes value)))))
  4538. (when (and f (file-readable-p f) (not (member f files)))
  4539. (with-temp-buffer
  4540. (insert-file-contents f)
  4541. (setq alist
  4542. ;; Fake Org mode to benefit from cache
  4543. ;; without recurring needlessly.
  4544. (let ((major-mode 'org-mode))
  4545. (org--setup-collect-keywords
  4546. regexp (cons f files) alist)))))))))))))))
  4547. alist)
  4548. (defun org--setup-process-tags (tags filetags)
  4549. "Precompute variables used for tags.
  4550. TAGS is a list of tags and tag group symbols, as strings.
  4551. FILETAGS is a list of tags, as strings."
  4552. ;; Process the file tags.
  4553. (org-set-local 'org-file-tags
  4554. (mapcar #'org-add-prop-inherited filetags))
  4555. ;; Provide default tags if no local tags are found.
  4556. (when (and (not tags) org-tag-alist)
  4557. (setq tags
  4558. (mapcar (lambda (tag)
  4559. (case (car tag)
  4560. (:startgroup "{")
  4561. (:endgroup "}")
  4562. (:startgrouptag "[")
  4563. (:endgrouptag "]")
  4564. (:grouptags ":")
  4565. (:newline "\\n")
  4566. (otherwise (concat (car tag)
  4567. (and (characterp (cdr tag))
  4568. (format "(%c)" (cdr tag)))))))
  4569. org-tag-alist)))
  4570. ;; Process the tags.
  4571. (org-set-local 'org-tag-groups-alist nil)
  4572. (org-set-local 'org-tag-alist nil)
  4573. (let (group-flag)
  4574. (while tags
  4575. (let ((e (car tags)))
  4576. (setq tags (cdr tags))
  4577. (cond
  4578. ((equal e "{")
  4579. (push '(:startgroup) org-tag-alist)
  4580. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4581. ((equal e "}")
  4582. (push '(:endgroup) org-tag-alist)
  4583. (setq group-flag nil))
  4584. ((equal e "[")
  4585. (push '(:startgrouptag) org-tag-alist)
  4586. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4587. ((equal e "]")
  4588. (push '(:endgrouptag) org-tag-alist)
  4589. (setq group-flag nil))
  4590. ((equal e ":")
  4591. (push '(:grouptags) org-tag-alist)
  4592. (setq group-flag 'append))
  4593. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4594. ((string-match
  4595. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4596. "\\|{.+?}\\)" ; regular expression
  4597. "\\(?:(\\(.\\))\\)?\\'")) e)
  4598. (let ((tag (match-string 1 e))
  4599. (key (and (match-beginning 2)
  4600. (string-to-char (match-string 2 e)))))
  4601. (cond ((eq group-flag 'append)
  4602. (setcar org-tag-groups-alist
  4603. (append (car org-tag-groups-alist) (list tag))))
  4604. (group-flag (push (list tag) org-tag-groups-alist)))
  4605. ;; Push all tags in groups, no matter if they already exist.
  4606. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4607. (push (cons tag key) org-tag-alist))))))))
  4608. (setq org-tag-alist (nreverse org-tag-alist)))
  4609. (defun org-file-contents (file &optional noerror)
  4610. "Return the contents of FILE, as a string."
  4611. (if (and file (file-readable-p file))
  4612. (with-temp-buffer
  4613. (insert-file-contents file)
  4614. (buffer-string))
  4615. (funcall (if noerror 'message 'error)
  4616. "Cannot read file \"%s\"%s"
  4617. file
  4618. (let ((from (buffer-file-name (buffer-base-buffer))))
  4619. (if from (concat " (referenced in file \"" from "\")") "")))))
  4620. (defun org-extract-log-state-settings (x)
  4621. "Extract the log state setting from a TODO keyword string.
  4622. This will extract info from a string like \"WAIT(w@/!)\"."
  4623. (let (kw key log1 log2)
  4624. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4625. (setq kw (match-string 1 x)
  4626. key (and (match-end 2) (match-string 2 x))
  4627. log1 (and (match-end 3) (match-string 3 x))
  4628. log2 (and (match-end 4) (match-string 4 x)))
  4629. (and (or log1 log2)
  4630. (list kw
  4631. (and log1 (if (equal log1 "!") 'time 'note))
  4632. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4633. (defun org-remove-keyword-keys (list)
  4634. "Remove a pair of parenthesis at the end of each string in LIST."
  4635. (mapcar (lambda (x)
  4636. (if (string-match "(.*)$" x)
  4637. (substring x 0 (match-beginning 0))
  4638. x))
  4639. list))
  4640. (defun org-assign-fast-keys (alist)
  4641. "Assign fast keys to a keyword-key alist.
  4642. Respect keys that are already there."
  4643. (let (new e (alt ?0))
  4644. (while (setq e (pop alist))
  4645. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4646. (cdr e)) ;; Key already assigned.
  4647. (push e new)
  4648. (let ((clist (string-to-list (downcase (car e))))
  4649. (used (append new alist)))
  4650. (when (= (car clist) ?@)
  4651. (pop clist))
  4652. (while (and clist (rassoc (car clist) used))
  4653. (pop clist))
  4654. (unless clist
  4655. (while (rassoc alt used)
  4656. (incf alt)))
  4657. (push (cons (car e) (or (car clist) alt)) new))))
  4658. (nreverse new)))
  4659. ;;; Some variables used in various places
  4660. (defvar org-window-configuration nil
  4661. "Used in various places to store a window configuration.")
  4662. (defvar org-selected-window nil
  4663. "Used in various places to store a window configuration.")
  4664. (defvar org-finish-function nil
  4665. "Function to be called when `C-c C-c' is used.
  4666. This is for getting out of special buffers like capture.")
  4667. ;; FIXME: Occasionally check by commenting these, to make sure
  4668. ;; no other functions uses these, forgetting to let-bind them.
  4669. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4670. (defvar org-last-state)
  4671. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4672. ;; Defined somewhere in this file, but used before definition.
  4673. (defvar org-entities) ;; defined in org-entities.el
  4674. (defvar org-struct-menu)
  4675. (defvar org-org-menu)
  4676. (defvar org-tbl-menu)
  4677. ;;;; Define the Org-mode
  4678. ;; We use a before-change function to check if a table might need
  4679. ;; an update.
  4680. (defvar org-table-may-need-update t
  4681. "Indicates that a table might need an update.
  4682. This variable is set by `org-before-change-function'.
  4683. `org-table-align' sets it back to nil.")
  4684. (defun org-before-change-function (beg end)
  4685. "Every change indicates that a table might need an update."
  4686. (setq org-table-may-need-update t))
  4687. (defvar org-mode-map)
  4688. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4689. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4690. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4691. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4692. (defvar org-table-buffer-is-an nil)
  4693. (defvar bidi-paragraph-direction)
  4694. (defvar buffer-face-mode-face)
  4695. (require 'outline)
  4696. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4697. (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"))
  4698. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4699. ;; Other stuff we need.
  4700. (require 'time-date)
  4701. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4702. (require 'easymenu)
  4703. (autoload 'easy-menu-add "easymenu")
  4704. (require 'overlay)
  4705. ;; (require 'org-macs) moved higher up in the file before it is first used
  4706. (require 'org-entities)
  4707. ;; (require 'org-compat) moved higher up in the file before it is first used
  4708. (require 'org-faces)
  4709. (require 'org-list)
  4710. (require 'org-pcomplete)
  4711. (require 'org-src)
  4712. (require 'org-footnote)
  4713. (require 'org-macro)
  4714. ;; babel
  4715. (require 'ob)
  4716. ;;;###autoload
  4717. (define-derived-mode org-mode outline-mode "Org"
  4718. "Outline-based notes management and organizer, alias
  4719. \"Carsten's outline-mode for keeping track of everything.\"
  4720. Org-mode develops organizational tasks around a NOTES file which
  4721. contains information about projects as plain text. Org-mode is
  4722. implemented on top of outline-mode, which is ideal to keep the content
  4723. of large files well structured. It supports ToDo items, deadlines and
  4724. time stamps, which magically appear in the diary listing of the Emacs
  4725. calendar. Tables are easily created with a built-in table editor.
  4726. Plain text URL-like links connect to websites, emails (VM), Usenet
  4727. messages (Gnus), BBDB entries, and any files related to the project.
  4728. For printing and sharing of notes, an Org-mode file (or a part of it)
  4729. can be exported as a structured ASCII or HTML file.
  4730. The following commands are available:
  4731. \\{org-mode-map}"
  4732. ;; Get rid of Outline menus, they are not needed
  4733. ;; Need to do this here because define-derived-mode sets up
  4734. ;; the keymap so late. Still, it is a waste to call this each time
  4735. ;; we switch another buffer into org-mode.
  4736. (if (featurep 'xemacs)
  4737. (when (boundp 'outline-mode-menu-heading)
  4738. ;; Assume this is Greg's port, it uses easymenu
  4739. (easy-menu-remove outline-mode-menu-heading)
  4740. (easy-menu-remove outline-mode-menu-show)
  4741. (easy-menu-remove outline-mode-menu-hide))
  4742. (define-key org-mode-map [menu-bar headings] 'undefined)
  4743. (define-key org-mode-map [menu-bar hide] 'undefined)
  4744. (define-key org-mode-map [menu-bar show] 'undefined))
  4745. (org-load-modules-maybe)
  4746. (when (featurep 'xemacs)
  4747. (easy-menu-add org-org-menu)
  4748. (easy-menu-add org-tbl-menu))
  4749. (org-install-agenda-files-menu)
  4750. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4751. (add-to-invisibility-spec '(org-cwidth))
  4752. (add-to-invisibility-spec '(org-hide-block . t))
  4753. (when (featurep 'xemacs)
  4754. (org-set-local 'line-move-ignore-invisible t))
  4755. (org-set-local 'outline-regexp org-outline-regexp)
  4756. (org-set-local 'outline-level 'org-outline-level)
  4757. (setq bidi-paragraph-direction 'left-to-right)
  4758. (when (and org-ellipsis
  4759. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4760. (fboundp 'make-glyph-code))
  4761. (unless org-display-table
  4762. (setq org-display-table (make-display-table)))
  4763. (set-display-table-slot
  4764. org-display-table 4
  4765. (vconcat (mapcar
  4766. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4767. org-ellipsis)))
  4768. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4769. (setq buffer-display-table org-display-table))
  4770. (org-set-regexps-and-options)
  4771. (org-set-font-lock-defaults)
  4772. (when (and org-tag-faces (not org-tags-special-faces-re))
  4773. ;; tag faces set outside customize.... force initialization.
  4774. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4775. ;; Calc embedded
  4776. (org-set-local 'calc-embedded-open-mode "# ")
  4777. ;; Modify a few syntax entries
  4778. (modify-syntax-entry ?@ "w")
  4779. (modify-syntax-entry ?\" "\"")
  4780. (modify-syntax-entry ?\\ "_")
  4781. (modify-syntax-entry ?~ "_")
  4782. (if org-startup-truncated (setq truncate-lines t))
  4783. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4784. (org-set-local 'font-lock-unfontify-region-function
  4785. 'org-unfontify-region)
  4786. ;; Activate before-change-function
  4787. (org-set-local 'org-table-may-need-update t)
  4788. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4789. 'local)
  4790. ;; Check for running clock before killing a buffer
  4791. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4792. ;; Initialize macros templates.
  4793. (org-macro-initialize-templates)
  4794. ;; Initialize radio targets.
  4795. (org-update-radio-target-regexp)
  4796. ;; Indentation.
  4797. (org-set-local 'indent-line-function 'org-indent-line)
  4798. (org-set-local 'indent-region-function 'org-indent-region)
  4799. ;; Filling and auto-filling.
  4800. (org-setup-filling)
  4801. ;; Comments.
  4802. (org-setup-comments-handling)
  4803. ;; Initialize cache.
  4804. (org-element-cache-reset)
  4805. ;; Beginning/end of defun
  4806. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4807. (org-set-local 'end-of-defun-function
  4808. (lambda ()
  4809. (if (not (org-at-heading-p))
  4810. (org-forward-element)
  4811. (org-forward-element)
  4812. (forward-char -1))))
  4813. ;; Next error for sparse trees
  4814. (org-set-local 'next-error-function 'org-occur-next-match)
  4815. ;; Make sure dependence stuff works reliably, even for users who set it
  4816. ;; too late :-(
  4817. (if org-enforce-todo-dependencies
  4818. (add-hook 'org-blocker-hook
  4819. 'org-block-todo-from-children-or-siblings-or-parent)
  4820. (remove-hook 'org-blocker-hook
  4821. 'org-block-todo-from-children-or-siblings-or-parent))
  4822. (if org-enforce-todo-checkbox-dependencies
  4823. (add-hook 'org-blocker-hook
  4824. 'org-block-todo-from-checkboxes)
  4825. (remove-hook 'org-blocker-hook
  4826. 'org-block-todo-from-checkboxes))
  4827. ;; Align options lines
  4828. (org-set-local
  4829. 'align-mode-rules-list
  4830. '((org-in-buffer-settings
  4831. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4832. (modes . '(org-mode)))))
  4833. ;; Imenu
  4834. (org-set-local 'imenu-create-index-function
  4835. 'org-imenu-get-tree)
  4836. ;; Make isearch reveal context
  4837. (if (or (featurep 'xemacs)
  4838. (not (boundp 'outline-isearch-open-invisible-function)))
  4839. ;; Emacs 21 and XEmacs make use of the hook
  4840. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4841. ;; Emacs 22 deals with this through a special variable
  4842. (org-set-local 'outline-isearch-open-invisible-function
  4843. (lambda (&rest ignore) (org-show-context 'isearch))))
  4844. ;; Setup the pcomplete hooks
  4845. (set (make-local-variable 'pcomplete-command-completion-function)
  4846. 'org-pcomplete-initial)
  4847. (set (make-local-variable 'pcomplete-command-name-function)
  4848. 'org-command-at-point)
  4849. (set (make-local-variable 'pcomplete-default-completion-function)
  4850. 'ignore)
  4851. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4852. 'org-parse-arguments)
  4853. (set (make-local-variable 'pcomplete-termination-string) "")
  4854. (when (>= emacs-major-version 23)
  4855. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4856. ;; If empty file that did not turn on org-mode automatically, make it to.
  4857. (if (and org-insert-mode-line-in-empty-file
  4858. (org-called-interactively-p 'any)
  4859. (= (point-min) (point-max)))
  4860. (insert "# -*- mode: org -*-\n\n"))
  4861. (unless org-inhibit-startup
  4862. (org-unmodified
  4863. (and org-startup-with-beamer-mode (org-beamer-mode))
  4864. (when org-startup-align-all-tables
  4865. (org-table-map-tables 'org-table-align 'quietly))
  4866. (when org-startup-with-inline-images
  4867. (org-display-inline-images))
  4868. (when org-startup-with-latex-preview
  4869. (org-toggle-latex-fragment))
  4870. (unless org-inhibit-startup-visibility-stuff
  4871. (org-set-startup-visibility))
  4872. (org-refresh-effort-properties)))
  4873. ;; Try to set org-hide correctly
  4874. (let ((foreground (org-find-invisible-foreground)))
  4875. (if foreground
  4876. (set-face-foreground 'org-hide foreground))))
  4877. ;; Update `customize-package-emacs-version-alist'
  4878. (add-to-list 'customize-package-emacs-version-alist
  4879. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4880. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4881. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4882. (defvar org-mode-transpose-word-syntax-table
  4883. (let ((st (make-syntax-table text-mode-syntax-table)))
  4884. (mapc (lambda(c) (modify-syntax-entry
  4885. (string-to-char (car c)) "w p" st))
  4886. org-emphasis-alist)
  4887. st))
  4888. (when (fboundp 'abbrev-table-put)
  4889. (abbrev-table-put org-mode-abbrev-table
  4890. :parents (list text-mode-abbrev-table)))
  4891. (defun org-find-invisible-foreground ()
  4892. (let ((candidates (remove
  4893. "unspecified-bg"
  4894. (nconc
  4895. (list (face-background 'default)
  4896. (face-background 'org-default))
  4897. (mapcar
  4898. (lambda (alist)
  4899. (when (boundp alist)
  4900. (cdr (assoc 'background-color (symbol-value alist)))))
  4901. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4902. (list (face-foreground 'org-hide))))))
  4903. (car (remove nil candidates))))
  4904. (defun org-current-time (&optional rounding-minutes past)
  4905. "Current time, possibly rounded to ROUNDING-MINUTES.
  4906. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4907. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4908. the rounding returns a past time."
  4909. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4910. (car org-time-stamp-rounding-minutes)))
  4911. (time (decode-time)) res)
  4912. (if (< r 1)
  4913. (current-time)
  4914. (setq res
  4915. (apply 'encode-time
  4916. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4917. (nthcdr 2 time))))
  4918. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4919. (seconds-to-time (- (org-float-time res) (* r 60)))
  4920. res))))
  4921. (defun org-today ()
  4922. "Return today date, considering `org-extend-today-until'."
  4923. (time-to-days
  4924. (time-subtract (current-time)
  4925. (list 0 (* 3600 org-extend-today-until) 0))))
  4926. ;;;; Font-Lock stuff, including the activators
  4927. (defvar org-mouse-map (make-sparse-keymap))
  4928. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4929. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4930. (when org-mouse-1-follows-link
  4931. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4932. (when org-tab-follows-link
  4933. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4934. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4935. (require 'font-lock)
  4936. (defconst org-non-link-chars "]\t\n\r<>")
  4937. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4938. "file+sys" "news" "shell" "elisp" "doi" "message"
  4939. "help"))
  4940. (defvar org-link-types-re nil
  4941. "Matches a link that has a url-like prefix like \"http:\"")
  4942. (defvar org-link-re-with-space nil
  4943. "Matches a link with spaces, optional angular brackets around it.")
  4944. (defvar org-link-re-with-space2 nil
  4945. "Matches a link with spaces, optional angular brackets around it.")
  4946. (defvar org-link-re-with-space3 nil
  4947. "Matches a link with spaces, only for internal part in bracket links.")
  4948. (defvar org-angle-link-re nil
  4949. "Matches link with angular brackets, spaces are allowed.")
  4950. (defvar org-plain-link-re nil
  4951. "Matches plain link, without spaces.")
  4952. (defvar org-bracket-link-regexp nil
  4953. "Matches a link in double brackets.")
  4954. (defvar org-bracket-link-analytic-regexp nil
  4955. "Regular expression used to analyze links.
  4956. Here is what the match groups contain after a match:
  4957. 1: http:
  4958. 2: http
  4959. 3: path
  4960. 4: [desc]
  4961. 5: desc")
  4962. (defvar org-bracket-link-analytic-regexp++ nil
  4963. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4964. (defvar org-any-link-re nil
  4965. "Regular expression matching any link.")
  4966. (defconst org-match-sexp-depth 3
  4967. "Number of stacked braces for sub/superscript matching.")
  4968. (defun org-create-multibrace-regexp (left right n)
  4969. "Create a regular expression which will match a balanced sexp.
  4970. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4971. as single character strings.
  4972. The regexp returned will match the entire expression including the
  4973. delimiters. It will also define a single group which contains the
  4974. match except for the outermost delimiters. The maximum depth of
  4975. stacked delimiters is N. Escaping delimiters is not possible."
  4976. (let* ((nothing (concat "[^" left right "]*?"))
  4977. (or "\\|")
  4978. (re nothing)
  4979. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4980. (while (> n 1)
  4981. (setq n (1- n)
  4982. re (concat re or next)
  4983. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4984. (concat left "\\(" re "\\)" right)))
  4985. (defconst org-match-substring-regexp
  4986. (concat
  4987. "\\(\\S-\\)\\([_^]\\)\\("
  4988. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4989. "\\|"
  4990. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4991. "\\|"
  4992. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4993. "The regular expression matching a sub- or superscript.")
  4994. (defconst org-match-substring-with-braces-regexp
  4995. (concat
  4996. "\\(\\S-\\)\\([_^]\\)"
  4997. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4998. "The regular expression matching a sub- or superscript, forcing braces.")
  4999. (defun org-make-link-regexps ()
  5000. "Update the link regular expressions.
  5001. This should be called after the variable `org-link-types' has changed."
  5002. (let ((types-re (regexp-opt org-link-types t)))
  5003. (setq org-link-types-re
  5004. (concat "\\`" types-re ":")
  5005. org-link-re-with-space
  5006. (concat "<?" types-re ":"
  5007. "\\([^" org-non-link-chars " ]"
  5008. "[^" org-non-link-chars "]*"
  5009. "[^" org-non-link-chars " ]\\)>?")
  5010. org-link-re-with-space2
  5011. (concat "<?" types-re ":"
  5012. "\\([^" org-non-link-chars " ]"
  5013. "[^\t\n\r]*"
  5014. "[^" org-non-link-chars " ]\\)>?")
  5015. org-link-re-with-space3
  5016. (concat "<?" types-re ":"
  5017. "\\([^" org-non-link-chars " ]"
  5018. "[^\t\n\r]*\\)")
  5019. org-angle-link-re
  5020. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5021. types-re)
  5022. org-plain-link-re
  5023. (concat
  5024. "\\<" types-re ":"
  5025. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5026. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5027. org-bracket-link-regexp
  5028. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5029. org-bracket-link-analytic-regexp
  5030. (concat
  5031. "\\[\\["
  5032. "\\(" types-re ":\\)?"
  5033. "\\([^]]+\\)"
  5034. "\\]"
  5035. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5036. "\\]")
  5037. org-bracket-link-analytic-regexp++
  5038. (concat
  5039. "\\[\\["
  5040. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5041. "\\([^]]+\\)"
  5042. "\\]"
  5043. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5044. "\\]")
  5045. org-any-link-re
  5046. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5047. org-angle-link-re "\\)\\|\\("
  5048. org-plain-link-re "\\)"))))
  5049. (org-make-link-regexps)
  5050. (defvar org-emph-face nil)
  5051. (defun org-do-emphasis-faces (limit)
  5052. "Run through the buffer and emphasize strings."
  5053. (let (rtn a)
  5054. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5055. (let* ((border (char-after (match-beginning 3)))
  5056. (bre (regexp-quote (char-to-string border))))
  5057. (if (and (not (= border (char-after (match-beginning 4))))
  5058. (not (save-match-data
  5059. (string-match (concat bre ".*" bre)
  5060. (replace-regexp-in-string
  5061. "\n" " "
  5062. (substring (match-string 2) 1 -1))))))
  5063. (progn
  5064. (setq rtn t)
  5065. (setq a (assoc (match-string 3) org-emphasis-alist))
  5066. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5067. 'face
  5068. (nth 1 a))
  5069. (and (nth 2 a)
  5070. (org-remove-flyspell-overlays-in
  5071. (match-beginning 0) (match-end 0)))
  5072. (add-text-properties (match-beginning 2) (match-end 2)
  5073. '(font-lock-multiline t org-emphasis t))
  5074. (when org-hide-emphasis-markers
  5075. (add-text-properties (match-end 4) (match-beginning 5)
  5076. '(invisible org-link))
  5077. (add-text-properties (match-beginning 3) (match-end 3)
  5078. '(invisible org-link))))))
  5079. (goto-char (1+ (match-beginning 0))))
  5080. rtn))
  5081. (defun org-emphasize (&optional char)
  5082. "Insert or change an emphasis, i.e. a font like bold or italic.
  5083. If there is an active region, change that region to a new emphasis.
  5084. If there is no region, just insert the marker characters and position
  5085. the cursor between them.
  5086. CHAR should be the marker character. If it is a space, it means to
  5087. remove the emphasis of the selected region.
  5088. If CHAR is not given (for example in an interactive call) it will be
  5089. prompted for."
  5090. (interactive)
  5091. (let ((erc org-emphasis-regexp-components)
  5092. (prompt "")
  5093. (string "") beg end move c s)
  5094. (if (org-region-active-p)
  5095. (setq beg (region-beginning) end (region-end)
  5096. string (buffer-substring beg end))
  5097. (setq move t))
  5098. (unless char
  5099. (message "Emphasis marker or tag: [%s]"
  5100. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5101. (setq char (read-char-exclusive)))
  5102. (if (equal char ?\ )
  5103. (setq s "" move nil)
  5104. (unless (assoc (char-to-string char) org-emphasis-alist)
  5105. (user-error "No such emphasis marker: \"%c\"" char))
  5106. (setq s (char-to-string char)))
  5107. (while (and (> (length string) 1)
  5108. (equal (substring string 0 1) (substring string -1))
  5109. (assoc (substring string 0 1) org-emphasis-alist))
  5110. (setq string (substring string 1 -1)))
  5111. (setq string (concat s string s))
  5112. (if beg (delete-region beg end))
  5113. (unless (or (bolp)
  5114. (string-match (concat "[" (nth 0 erc) "\n]")
  5115. (char-to-string (char-before (point)))))
  5116. (insert " "))
  5117. (unless (or (eobp)
  5118. (string-match (concat "[" (nth 1 erc) "\n]")
  5119. (char-to-string (char-after (point)))))
  5120. (insert " ") (backward-char 1))
  5121. (insert string)
  5122. (and move (backward-char 1))))
  5123. (defconst org-nonsticky-props
  5124. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5125. (defsubst org-rear-nonsticky-at (pos)
  5126. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5127. (defun org-activate-plain-links (limit)
  5128. "Add link properties for plain links."
  5129. (let (f hl)
  5130. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5131. (not (member 'org-tag
  5132. (get-text-property (1- (match-beginning 0)) 'face)))
  5133. (not (org-in-src-block-p)))
  5134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5135. (setq f (get-text-property (match-beginning 0) 'face))
  5136. (setq hl (org-match-string-no-properties 0))
  5137. (if (or (eq f 'org-tag)
  5138. (and (listp f) (memq 'org-tag f)))
  5139. nil
  5140. (add-text-properties (match-beginning 0) (match-end 0)
  5141. (list 'mouse-face 'highlight
  5142. 'face 'org-link
  5143. 'htmlize-link `(:uri ,hl)
  5144. 'keymap org-mouse-map))
  5145. (org-rear-nonsticky-at (match-end 0)))
  5146. t)))
  5147. (defun org-activate-code (limit)
  5148. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5149. (progn
  5150. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5151. (remove-text-properties (match-beginning 0) (match-end 0)
  5152. '(display t invisible t intangible t))
  5153. t)))
  5154. (defcustom org-src-fontify-natively t
  5155. "When non-nil, fontify code in code blocks."
  5156. :type 'boolean
  5157. :version "24.4"
  5158. :package-version '(Org . "8.3")
  5159. :group 'org-appearance
  5160. :group 'org-babel)
  5161. (defcustom org-allow-promoting-top-level-subtree nil
  5162. "When non-nil, allow promoting a top level subtree.
  5163. The leading star of the top level headline will be replaced
  5164. by a #."
  5165. :type 'boolean
  5166. :version "24.1"
  5167. :group 'org-appearance)
  5168. (defun org-fontify-meta-lines-and-blocks (limit)
  5169. (condition-case nil
  5170. (org-fontify-meta-lines-and-blocks-1 limit)
  5171. (error (message "org-mode fontification error"))))
  5172. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5173. "Fontify #+ lines and blocks."
  5174. (let ((case-fold-search t))
  5175. (if (re-search-forward
  5176. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5177. limit t)
  5178. (let ((beg (match-beginning 0))
  5179. (block-start (match-end 0))
  5180. (block-end nil)
  5181. (lang (match-string 7))
  5182. (beg1 (line-beginning-position 2))
  5183. (dc1 (downcase (match-string 2)))
  5184. (dc3 (downcase (match-string 3)))
  5185. end end1 quoting block-type ovl)
  5186. (cond
  5187. ((and (match-end 4) (equal dc3 "+begin"))
  5188. ;; Truly a block
  5189. (setq block-type (downcase (match-string 5))
  5190. quoting (member block-type org-protecting-blocks))
  5191. (when (re-search-forward
  5192. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5193. nil t) ;; on purpose, we look further than LIMIT
  5194. (setq end (min (point-max) (match-end 0))
  5195. end1 (min (point-max) (1- (match-beginning 0))))
  5196. (setq block-end (match-beginning 0))
  5197. (when quoting
  5198. (org-remove-flyspell-overlays-in beg1 end1)
  5199. (remove-text-properties beg end
  5200. '(display t invisible t intangible t)))
  5201. (add-text-properties
  5202. beg end '(font-lock-fontified t font-lock-multiline t))
  5203. (add-text-properties beg beg1 '(face org-meta-line))
  5204. (org-remove-flyspell-overlays-in beg beg1)
  5205. (add-text-properties ; For end_src
  5206. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5207. (org-remove-flyspell-overlays-in end1 end)
  5208. (cond
  5209. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5210. (org-src-font-lock-fontify-block lang block-start block-end)
  5211. (add-text-properties beg1 block-end '(src-block t)))
  5212. (quoting
  5213. (add-text-properties beg1 (min (point-max) (1+ end1))
  5214. '(face org-block))) ; end of source block
  5215. ((not org-fontify-quote-and-verse-blocks))
  5216. ((string= block-type "quote")
  5217. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5218. ((string= block-type "verse")
  5219. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5220. (add-text-properties beg beg1 '(face org-block-begin-line))
  5221. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5222. '(face org-block-end-line))
  5223. t))
  5224. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5225. (org-remove-flyspell-overlays-in
  5226. (match-beginning 0)
  5227. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5228. (add-text-properties
  5229. beg (match-end 3)
  5230. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5231. '(font-lock-fontified t invisible t)
  5232. '(font-lock-fontified t face org-document-info-keyword)))
  5233. (add-text-properties
  5234. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5235. (if (string-equal dc1 "+title:")
  5236. '(font-lock-fontified t face org-document-title)
  5237. '(font-lock-fontified t face org-document-info))))
  5238. ((equal dc1 "+caption:")
  5239. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5240. (remove-text-properties (match-beginning 0) (match-end 0)
  5241. '(display t invisible t intangible t))
  5242. (add-text-properties (match-beginning 1) (match-end 3)
  5243. '(font-lock-fontified t face org-meta-line))
  5244. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5245. '(font-lock-fontified t face org-block))
  5246. t)
  5247. ((member dc3 '(" " ""))
  5248. (org-remove-flyspell-overlays-in beg (match-end 0))
  5249. (add-text-properties
  5250. beg (match-end 0)
  5251. '(font-lock-fontified t face font-lock-comment-face)))
  5252. (t ;; just any other in-buffer setting, but not indented
  5253. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5254. (remove-text-properties (match-beginning 0) (match-end 0)
  5255. '(display t invisible t intangible t))
  5256. (add-text-properties beg (match-end 0)
  5257. '(font-lock-fontified t face org-meta-line))
  5258. t))))))
  5259. (defun org-fontify-drawers (limit)
  5260. "Fontify drawers."
  5261. (when (re-search-forward org-drawer-regexp limit t)
  5262. (add-text-properties
  5263. (match-beginning 0) (match-end 0)
  5264. '(font-lock-fontified t face org-special-keyword))
  5265. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5266. t))
  5267. (defun org-fontify-macros (limit)
  5268. "Fontify macros."
  5269. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5270. (add-text-properties
  5271. (match-beginning 0) (match-end 0)
  5272. '(font-lock-fontified t face org-macro))
  5273. (when org-hide-macro-markers
  5274. (add-text-properties (match-end 2) (match-beginning 2)
  5275. '(invisible t))
  5276. (add-text-properties (match-beginning 1) (match-end 1)
  5277. '(invisible t)))
  5278. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5279. t))
  5280. (defun org-activate-angle-links (limit)
  5281. "Add text properties for angle links."
  5282. (if (and (re-search-forward org-angle-link-re limit t)
  5283. (not (org-in-src-block-p)))
  5284. (progn
  5285. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5286. (add-text-properties (match-beginning 0) (match-end 0)
  5287. (list 'mouse-face 'highlight
  5288. 'keymap org-mouse-map
  5289. 'font-lock-multiline t))
  5290. (org-rear-nonsticky-at (match-end 0))
  5291. t)))
  5292. (defun org-activate-footnote-links (limit)
  5293. "Add text properties for footnotes."
  5294. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5295. (when fn
  5296. (let* ((beg (nth 1 fn))
  5297. (end (nth 2 fn))
  5298. (label (car fn))
  5299. (referencep (/= (line-beginning-position) beg)))
  5300. (when (and referencep (nth 3 fn))
  5301. (save-excursion
  5302. (goto-char beg)
  5303. (search-forward (or label "fn:"))
  5304. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5305. (add-text-properties beg end
  5306. (list 'mouse-face 'highlight
  5307. 'keymap org-mouse-map
  5308. 'help-echo
  5309. (if referencep "Footnote reference"
  5310. "Footnote definition")
  5311. 'font-lock-fontified t
  5312. 'font-lock-multiline t
  5313. 'face 'org-footnote))))))
  5314. (defun org-activate-bracket-links (limit)
  5315. "Add text properties for bracketed links."
  5316. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5317. (not (org-in-src-block-p)))
  5318. (let* ((hl (org-match-string-no-properties 1))
  5319. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5320. (ip (org-maybe-intangible
  5321. (list 'invisible 'org-link
  5322. 'keymap org-mouse-map 'mouse-face 'highlight
  5323. 'font-lock-multiline t 'help-echo help
  5324. 'htmlize-link `(:uri ,hl))))
  5325. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5326. 'font-lock-multiline t 'help-echo help
  5327. 'htmlize-link `(:uri ,hl))))
  5328. ;; We need to remove the invisible property here. Table narrowing
  5329. ;; may have made some of this invisible.
  5330. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5331. (remove-text-properties (match-beginning 0) (match-end 0)
  5332. '(invisible nil))
  5333. (if (match-end 3)
  5334. (progn
  5335. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5336. (org-rear-nonsticky-at (match-beginning 3))
  5337. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5338. (org-rear-nonsticky-at (match-end 3))
  5339. (add-text-properties (match-end 3) (match-end 0) ip)
  5340. (org-rear-nonsticky-at (match-end 0)))
  5341. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5342. (org-rear-nonsticky-at (match-beginning 1))
  5343. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5344. (org-rear-nonsticky-at (match-end 1))
  5345. (add-text-properties (match-end 1) (match-end 0) ip)
  5346. (org-rear-nonsticky-at (match-end 0)))
  5347. t)))
  5348. (defun org-activate-dates (limit)
  5349. "Add text properties for dates."
  5350. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5351. (not (equal (char-before (match-beginning 0)) 91)))
  5352. (progn
  5353. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5354. (add-text-properties (match-beginning 0) (match-end 0)
  5355. (list 'mouse-face 'highlight
  5356. 'keymap org-mouse-map))
  5357. (org-rear-nonsticky-at (match-end 0))
  5358. (when org-display-custom-times
  5359. (if (match-end 3)
  5360. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5361. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5362. t)))
  5363. (defvar org-target-link-regexp nil
  5364. "Regular expression matching radio targets in plain text.")
  5365. (make-variable-buffer-local 'org-target-link-regexp)
  5366. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5367. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5368. border border border))
  5369. "Regular expression matching a link target.")
  5370. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5371. "Regular expression matching a radio target.")
  5372. (defconst org-any-target-regexp
  5373. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5374. "Regular expression matching any target.")
  5375. (defun org-activate-target-links (limit)
  5376. "Add text properties for target matches."
  5377. (when org-target-link-regexp
  5378. (let ((case-fold-search t))
  5379. (if (re-search-forward org-target-link-regexp limit t)
  5380. (progn
  5381. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5382. (add-text-properties (match-beginning 1) (match-end 1)
  5383. (list 'mouse-face 'highlight
  5384. 'keymap org-mouse-map
  5385. 'help-echo "Radio target link"
  5386. 'org-linked-text t))
  5387. (org-rear-nonsticky-at (match-end 1))
  5388. t)))))
  5389. (defun org-update-radio-target-regexp ()
  5390. "Find all radio targets in this file and update the regular expression.
  5391. Also refresh fontification if needed."
  5392. (interactive)
  5393. (let ((old-regexp org-target-link-regexp)
  5394. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5395. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5396. (targets
  5397. (org-with-wide-buffer
  5398. (goto-char (point-min))
  5399. (let (rtn)
  5400. (while (re-search-forward org-radio-target-regexp nil t)
  5401. ;; Make sure point is really within the object.
  5402. (backward-char)
  5403. (let ((obj (org-element-context)))
  5404. (when (eq (org-element-type obj) 'radio-target)
  5405. (add-to-list 'rtn (org-element-property :value obj)))))
  5406. rtn))))
  5407. (setq org-target-link-regexp
  5408. (and targets
  5409. (concat before-re
  5410. (mapconcat
  5411. (lambda (x)
  5412. (replace-regexp-in-string
  5413. " +" "\\s-+" (regexp-quote x) t t))
  5414. targets
  5415. "\\|")
  5416. after-re)))
  5417. (unless (equal old-regexp org-target-link-regexp)
  5418. ;; Clean-up cache.
  5419. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5420. ((not org-target-link-regexp) old-regexp)
  5421. (t
  5422. (concat before-re
  5423. (mapconcat
  5424. (lambda (re)
  5425. (substring re (length before-re)
  5426. (- (length after-re))))
  5427. (list old-regexp org-target-link-regexp)
  5428. "\\|")
  5429. after-re)))))
  5430. (org-with-wide-buffer
  5431. (goto-char (point-min))
  5432. (while (re-search-forward regexp nil t)
  5433. (org-element-cache-refresh (match-beginning 1)))))
  5434. ;; Re fontify buffer.
  5435. (when (memq 'radio org-highlight-links)
  5436. (org-restart-font-lock)))))
  5437. (defun org-hide-wide-columns (limit)
  5438. (let (s e)
  5439. (setq s (text-property-any (point) (or limit (point-max))
  5440. 'org-cwidth t))
  5441. (when s
  5442. (setq e (next-single-property-change s 'org-cwidth))
  5443. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5444. (goto-char e)
  5445. t)))
  5446. (defvar org-latex-and-related-regexp nil
  5447. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5448. (defun org-compute-latex-and-related-regexp ()
  5449. "Compute regular expression for LaTeX, entities and sub/superscript.
  5450. Result depends on variable `org-highlight-latex-and-related'."
  5451. (org-set-local
  5452. 'org-latex-and-related-regexp
  5453. (let* ((re-sub
  5454. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5455. ((eq org-use-sub-superscripts '{})
  5456. (list org-match-substring-with-braces-regexp))
  5457. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5458. (re-latex
  5459. (when (memq 'latex org-highlight-latex-and-related)
  5460. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5461. (delq nil
  5462. (mapcar (lambda (x)
  5463. (and (member (car x) matchers) (nth 1 x)))
  5464. org-latex-regexps)))))
  5465. (re-entities
  5466. (when (memq 'entities org-highlight-latex-and-related)
  5467. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5468. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5469. (defun org-do-latex-and-related (limit)
  5470. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5471. LIMIT bounds the search for syntax to highlight. Stop at first
  5472. highlighted object, if any. Return t if some highlighting was
  5473. done, nil otherwise."
  5474. (when (org-string-nw-p org-latex-and-related-regexp)
  5475. (catch 'found
  5476. (while (re-search-forward org-latex-and-related-regexp limit t)
  5477. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5478. 'face))
  5479. '(org-code org-verbatim underline))
  5480. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5481. '(?_ ?^))
  5482. 1
  5483. 0)))
  5484. (font-lock-prepend-text-property
  5485. (+ offset (match-beginning 0)) (match-end 0)
  5486. 'face 'org-latex-and-related)
  5487. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5488. '(font-lock-multiline t)))
  5489. (throw 'found t)))
  5490. nil)))
  5491. (defun org-restart-font-lock ()
  5492. "Restart `font-lock-mode', to force refontification."
  5493. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5494. (font-lock-mode -1)
  5495. (font-lock-mode 1)))
  5496. (defun org-activate-tags (limit)
  5497. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5498. (progn
  5499. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5500. (add-text-properties (match-beginning 1) (match-end 1)
  5501. (list 'mouse-face 'highlight
  5502. 'keymap org-mouse-map))
  5503. (org-rear-nonsticky-at (match-end 1))
  5504. t)))
  5505. (defun org-outline-level ()
  5506. "Compute the outline level of the heading at point.
  5507. If this is called at a normal headline, the level is the number
  5508. of stars. Use `org-reduced-level' to remove the effect of
  5509. `org-odd-levels'. Unlike to `org-current-level', this function
  5510. takes into consideration inlinetasks."
  5511. (org-with-wide-buffer
  5512. (end-of-line)
  5513. (if (re-search-backward org-outline-regexp-bol nil t)
  5514. (1- (- (match-end 0) (match-beginning 0)))
  5515. 0)))
  5516. (defvar org-font-lock-keywords nil)
  5517. (defsubst org-re-property (property &optional literal allow-null value)
  5518. "Return a regexp matching a PROPERTY line.
  5519. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5520. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5521. non-nil, match properties even without a value.
  5522. Match group 3 is set to the value when it exists. If there is no
  5523. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5524. With optional argument VALUE, match only property lines with
  5525. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5526. unless LITERAL is non-nil."
  5527. (concat
  5528. "^\\(?4:[ \t]*\\)"
  5529. (format "\\(?1::\\(?2:%s\\):\\)"
  5530. (if literal property (regexp-quote property)))
  5531. (cond (value
  5532. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5533. (if literal value (regexp-quote value))))
  5534. (allow-null
  5535. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5536. (t
  5537. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5538. (defconst org-property-re
  5539. (org-re-property "\\S-+" 'literal t)
  5540. "Regular expression matching a property line.
  5541. There are four matching groups:
  5542. 1: :PROPKEY: including the leading and trailing colon,
  5543. 2: PROPKEY without the leading and trailing colon,
  5544. 3: PROPVAL without leading or trailing spaces,
  5545. 4: the indentation of the current line,
  5546. 5: trailing whitespace.")
  5547. (defvar org-font-lock-hook nil
  5548. "Functions to be called for special font lock stuff.")
  5549. (defvar org-font-lock-set-keywords-hook nil
  5550. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5551. This is called after `org-font-lock-extra-keywords' is defined, but before
  5552. it is installed to be used by font lock. This can be useful if something
  5553. needs to be inserted at a specific position in the font-lock sequence.")
  5554. (defun org-font-lock-hook (limit)
  5555. "Run `org-font-lock-hook' within LIMIT."
  5556. (run-hook-with-args 'org-font-lock-hook limit))
  5557. (defun org-set-font-lock-defaults ()
  5558. "Set font lock defaults for the current buffer."
  5559. (let* ((em org-fontify-emphasized-text)
  5560. (lk org-highlight-links)
  5561. (org-font-lock-extra-keywords
  5562. (list
  5563. ;; Call the hook
  5564. '(org-font-lock-hook)
  5565. ;; Headlines
  5566. `(,(if org-fontify-whole-heading-line
  5567. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5568. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5569. (1 (org-get-level-face 1))
  5570. (2 (org-get-level-face 2))
  5571. (3 (org-get-level-face 3)))
  5572. ;; Table lines
  5573. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5574. (1 'org-table t))
  5575. ;; Table internals
  5576. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5577. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5578. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5579. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5580. ;; Drawers
  5581. '(org-fontify-drawers)
  5582. ;; Properties
  5583. (list org-property-re
  5584. '(1 'org-special-keyword t)
  5585. '(3 'org-property-value t))
  5586. ;; Links
  5587. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5588. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5589. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5590. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5591. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5592. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5593. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5594. ;; Targets.
  5595. (list org-any-target-regexp '(0 'org-target t))
  5596. ;; Diary sexps.
  5597. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5598. ;; Macro
  5599. '(org-fontify-macros)
  5600. '(org-hide-wide-columns (0 nil append))
  5601. ;; TODO keyword
  5602. (list (format org-heading-keyword-regexp-format
  5603. org-todo-regexp)
  5604. '(2 (org-get-todo-face 2) t))
  5605. ;; DONE
  5606. (if org-fontify-done-headline
  5607. (list (format org-heading-keyword-regexp-format
  5608. (concat
  5609. "\\(?:"
  5610. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5611. "\\)"))
  5612. '(2 'org-headline-done t))
  5613. nil)
  5614. ;; Priorities
  5615. '(org-font-lock-add-priority-faces)
  5616. ;; Tags
  5617. '(org-font-lock-add-tag-faces)
  5618. ;; Tags groups
  5619. (if (and org-group-tags org-tag-groups-alist)
  5620. (list (concat org-outline-regexp-bol ".+\\(:"
  5621. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5622. ":\\).*$")
  5623. '(1 'org-tag-group prepend)))
  5624. ;; Special keywords
  5625. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5626. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5628. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5629. ;; Emphasis
  5630. (if em
  5631. (if (featurep 'xemacs)
  5632. '(org-do-emphasis-faces (0 nil append))
  5633. '(org-do-emphasis-faces)))
  5634. ;; Checkboxes
  5635. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5636. 1 'org-checkbox prepend)
  5637. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5638. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5639. (0 (org-get-checkbox-statistics-face) t)))
  5640. ;; Description list items
  5641. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5642. 1 'org-list-dt prepend)
  5643. ;; ARCHIVEd headings
  5644. (list (concat
  5645. org-outline-regexp-bol
  5646. "\\(.*:" org-archive-tag ":.*\\)")
  5647. '(1 'org-archived prepend))
  5648. ;; Specials
  5649. '(org-do-latex-and-related)
  5650. '(org-fontify-entities)
  5651. '(org-raise-scripts)
  5652. ;; Code
  5653. '(org-activate-code (1 'org-code t))
  5654. ;; COMMENT
  5655. (list (format
  5656. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5657. org-todo-regexp
  5658. org-comment-string)
  5659. '(9 'org-special-keyword t))
  5660. ;; Blocks and meta lines
  5661. '(org-fontify-meta-lines-and-blocks))))
  5662. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5663. (run-hooks 'org-font-lock-set-keywords-hook)
  5664. ;; Now set the full font-lock-keywords
  5665. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5666. (org-set-local 'font-lock-defaults
  5667. '(org-font-lock-keywords t nil nil backward-paragraph))
  5668. (kill-local-variable 'font-lock-keywords) nil))
  5669. (defun org-toggle-pretty-entities ()
  5670. "Toggle the composition display of entities as UTF8 characters."
  5671. (interactive)
  5672. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5673. (org-restart-font-lock)
  5674. (if org-pretty-entities
  5675. (message "Entities are now displayed as UTF8 characters")
  5676. (save-restriction
  5677. (widen)
  5678. (org-decompose-region (point-min) (point-max))
  5679. (message "Entities are now displayed as plain text"))))
  5680. (defvar org-custom-properties-overlays nil
  5681. "List of overlays used for custom properties.")
  5682. (make-variable-buffer-local 'org-custom-properties-overlays)
  5683. (defun org-toggle-custom-properties-visibility ()
  5684. "Display or hide properties in `org-custom-properties'."
  5685. (interactive)
  5686. (if org-custom-properties-overlays
  5687. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5688. (setq org-custom-properties-overlays nil))
  5689. (when org-custom-properties
  5690. (org-with-wide-buffer
  5691. (goto-char (point-min))
  5692. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5693. (while (re-search-forward regexp nil t)
  5694. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5695. (when (and end (< (point) end))
  5696. ;; Hide first custom property in current drawer.
  5697. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5698. (overlay-put o 'invisible t)
  5699. (overlay-put o 'org-custom-property t)
  5700. (push o org-custom-properties-overlays))
  5701. ;; Hide additional custom properties in the same drawer.
  5702. (while (re-search-forward regexp end t)
  5703. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5704. (overlay-put o 'invisible t)
  5705. (overlay-put o 'org-custom-property t)
  5706. (push o org-custom-properties-overlays)))))
  5707. ;; Each entry is limited to a single property drawer.
  5708. (outline-next-heading)))))))
  5709. (defun org-fontify-entities (limit)
  5710. "Find an entity to fontify."
  5711. (let (ee)
  5712. (when org-pretty-entities
  5713. (catch 'match
  5714. ;; "\_ "-family is left out on purpose. Only the first one,
  5715. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5716. ;; confusing when adding a second white space character.
  5717. (while (re-search-forward
  5718. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5719. limit t)
  5720. (if (and (not (org-at-comment-p))
  5721. (setq ee (org-entity-get (match-string 1)))
  5722. (= (length (nth 6 ee)) 1))
  5723. (let*
  5724. ((end (if (equal (match-string 2) "{}")
  5725. (match-end 2)
  5726. (match-end 1))))
  5727. (add-text-properties
  5728. (match-beginning 0) end
  5729. (list 'font-lock-fontified t))
  5730. (compose-region (match-beginning 0) end
  5731. (nth 6 ee) nil)
  5732. (backward-char 1)
  5733. (throw 'match t))))
  5734. nil))))
  5735. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5736. "Fontify string S like in Org-mode."
  5737. (with-temp-buffer
  5738. (insert s)
  5739. (let ((org-odd-levels-only odd-levels))
  5740. (org-mode)
  5741. (font-lock-ensure)
  5742. (buffer-string))))
  5743. (defvar org-m nil)
  5744. (defvar org-l nil)
  5745. (defvar org-f nil)
  5746. (defun org-get-level-face (n)
  5747. "Get the right face for match N in font-lock matching of headlines."
  5748. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5749. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5750. (if org-cycle-level-faces
  5751. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5752. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5753. (cond
  5754. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5755. ((eq n 2) org-f)
  5756. (t (if org-level-color-stars-only nil org-f))))
  5757. (defun org-face-from-face-or-color (context inherit face-or-color)
  5758. "Create a face list that inherits INHERIT, but sets the foreground color.
  5759. When FACE-OR-COLOR is not a string, just return it."
  5760. (if (stringp face-or-color)
  5761. (list :inherit inherit
  5762. (cdr (assoc context org-faces-easy-properties))
  5763. face-or-color)
  5764. face-or-color))
  5765. (defun org-get-todo-face (kwd)
  5766. "Get the right face for a TODO keyword KWD.
  5767. If KWD is a number, get the corresponding match group."
  5768. (if (numberp kwd) (setq kwd (match-string kwd)))
  5769. (or (org-face-from-face-or-color
  5770. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5771. (and (member kwd org-done-keywords) 'org-done)
  5772. 'org-todo))
  5773. (defun org-get-priority-face (priority)
  5774. "Get the right face for PRIORITY.
  5775. PRIORITY is a character."
  5776. (or (org-face-from-face-or-color
  5777. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5778. 'org-priority))
  5779. (defun org-get-tag-face (tag)
  5780. "Get the right face for TAG.
  5781. If TAG is a number, get the corresponding match group."
  5782. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5783. (or (org-face-from-face-or-color
  5784. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5785. 'org-tag)))
  5786. (defun org-font-lock-add-priority-faces (limit)
  5787. "Add the special priority faces."
  5788. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5789. (add-text-properties
  5790. (match-beginning 1) (match-end 1)
  5791. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5792. 'font-lock-fontified t))))
  5793. (defun org-font-lock-add-tag-faces (limit)
  5794. "Add the special tag faces."
  5795. (when (and org-tag-faces org-tags-special-faces-re)
  5796. (while (re-search-forward org-tags-special-faces-re limit t)
  5797. (add-text-properties (match-beginning 1) (match-end 1)
  5798. (list 'face (org-get-tag-face 1)
  5799. 'font-lock-fontified t))
  5800. (backward-char 1))))
  5801. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5802. "Remove fontification and activation overlays from links."
  5803. (font-lock-default-unfontify-region beg end)
  5804. (let* ((buffer-undo-list t)
  5805. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5806. (inhibit-modification-hooks t)
  5807. deactivate-mark buffer-file-name buffer-file-truename)
  5808. (org-decompose-region beg end)
  5809. (remove-text-properties beg end
  5810. '(mouse-face t keymap t org-linked-text t
  5811. invisible t intangible t
  5812. org-emphasis t))
  5813. (org-remove-font-lock-display-properties beg end)))
  5814. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5815. ((raise 0.3) (height 0.7))
  5816. ((raise -0.5))
  5817. ((raise 0.5)))
  5818. "Display properties for showing superscripts and subscripts.")
  5819. (defun org-remove-font-lock-display-properties (beg end)
  5820. "Remove specific display properties that have been added by font lock.
  5821. The will remove the raise properties that are used to show superscripts
  5822. and subscripts."
  5823. (let (next prop)
  5824. (while (< beg end)
  5825. (setq next (next-single-property-change beg 'display nil end)
  5826. prop (get-text-property beg 'display))
  5827. (if (member prop org-script-display)
  5828. (put-text-property beg next 'display nil))
  5829. (setq beg next))))
  5830. (defun org-raise-scripts (limit)
  5831. "Add raise properties to sub/superscripts."
  5832. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5833. (if (re-search-forward
  5834. (if (eq org-use-sub-superscripts t)
  5835. org-match-substring-regexp
  5836. org-match-substring-with-braces-regexp)
  5837. limit t)
  5838. (let* ((pos (point)) table-p comment-p
  5839. (mpos (match-beginning 3))
  5840. (emph-p (get-text-property mpos 'org-emphasis))
  5841. (link-p (get-text-property mpos 'mouse-face))
  5842. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5843. (goto-char (point-at-bol))
  5844. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5845. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5846. (goto-char pos)
  5847. ;; Handle a_b^c
  5848. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5849. (if (or comment-p emph-p link-p keyw-p)
  5850. t
  5851. (put-text-property (match-beginning 3) (match-end 0)
  5852. 'display
  5853. (if (equal (char-after (match-beginning 2)) ?^)
  5854. (nth (if table-p 3 1) org-script-display)
  5855. (nth (if table-p 2 0) org-script-display)))
  5856. (add-text-properties (match-beginning 2) (match-end 2)
  5857. (list 'invisible t
  5858. 'org-dwidth t 'org-dwidth-n 1))
  5859. (if (and (eq (char-after (match-beginning 3)) ?{)
  5860. (eq (char-before (match-end 3)) ?}))
  5861. (progn
  5862. (add-text-properties
  5863. (match-beginning 3) (1+ (match-beginning 3))
  5864. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5865. (add-text-properties
  5866. (1- (match-end 3)) (match-end 3)
  5867. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5868. t)))))
  5869. ;;;; Visibility cycling, including org-goto and indirect buffer
  5870. ;;; Cycling
  5871. (defvar org-cycle-global-status nil)
  5872. (make-variable-buffer-local 'org-cycle-global-status)
  5873. (put 'org-cycle-global-status 'org-state t)
  5874. (defvar org-cycle-subtree-status nil)
  5875. (make-variable-buffer-local 'org-cycle-subtree-status)
  5876. (put 'org-cycle-subtree-status 'org-state t)
  5877. (defvar org-inlinetask-min-level)
  5878. (defun org-unlogged-message (&rest args)
  5879. "Display a message, but avoid logging it in the *Messages* buffer."
  5880. (let ((message-log-max nil))
  5881. (apply 'message args)))
  5882. ;;;###autoload
  5883. (defun org-cycle (&optional arg)
  5884. "TAB-action and visibility cycling for Org-mode.
  5885. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5886. is outline visibility cycling, but it also invokes other actions
  5887. in special contexts.
  5888. - When this function is called with a prefix argument, rotate the entire
  5889. buffer through 3 states (global cycling)
  5890. 1. OVERVIEW: Show only top-level headlines.
  5891. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5892. 3. SHOW ALL: Show everything.
  5893. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5894. determined by the variable `org-startup-folded', and by any VISIBILITY
  5895. properties in the buffer.
  5896. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5897. including any drawers.
  5898. - When inside a table, re-align the table and move to the next field.
  5899. - When point is at the beginning of a headline, rotate the subtree started
  5900. by this line through 3 different states (local cycling)
  5901. 1. FOLDED: Only the main headline is shown.
  5902. 2. CHILDREN: The main headline and the direct children are shown.
  5903. From this state, you can move to one of the children
  5904. and zoom in further.
  5905. 3. SUBTREE: Show the entire subtree, including body text.
  5906. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5907. - When point is at the beginning of an empty headline and the variable
  5908. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5909. of the headline by demoting and promoting it to likely levels. This
  5910. speeds up creation document structure by pressing TAB once or several
  5911. times right after creating a new headline.
  5912. - When there is a numeric prefix, go up to a heading with level ARG, do
  5913. a `show-subtree' and return to the previous cursor position. If ARG
  5914. is negative, go up that many levels.
  5915. - When point is not at the beginning of a headline, execute the global
  5916. binding for TAB, which is re-indenting the line. See the option
  5917. `org-cycle-emulate-tab' for details.
  5918. - Special case: if point is at the beginning of the buffer and there is
  5919. no headline in line 1, this function will act as if called with prefix arg
  5920. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5921. But only if also the variable `org-cycle-global-at-bob' is t."
  5922. (interactive "P")
  5923. (org-load-modules-maybe)
  5924. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5925. (and org-cycle-level-after-item/entry-creation
  5926. (or (org-cycle-level)
  5927. (org-cycle-item-indentation))))
  5928. (let* ((limit-level
  5929. (or org-cycle-max-level
  5930. (and (boundp 'org-inlinetask-min-level)
  5931. org-inlinetask-min-level
  5932. (1- org-inlinetask-min-level))))
  5933. (nstars (and limit-level
  5934. (if org-odd-levels-only
  5935. (and limit-level (1- (* limit-level 2)))
  5936. limit-level)))
  5937. (org-outline-regexp
  5938. (if (not (derived-mode-p 'org-mode))
  5939. outline-regexp
  5940. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5941. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5942. (not (looking-at org-outline-regexp))))
  5943. (org-cycle-hook
  5944. (if bob-special
  5945. (delq 'org-optimize-window-after-visibility-change
  5946. (copy-sequence org-cycle-hook))
  5947. org-cycle-hook))
  5948. (pos (point)))
  5949. (if (or bob-special (equal arg '(4)))
  5950. ;; special case: use global cycling
  5951. (setq arg t))
  5952. (cond
  5953. ((equal arg '(16))
  5954. (setq last-command 'dummy)
  5955. (org-set-startup-visibility)
  5956. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5957. ((equal arg '(64))
  5958. (show-all)
  5959. (org-unlogged-message "Entire buffer visible, including drawers"))
  5960. ;; Try cdlatex TAB completion
  5961. ((org-try-cdlatex-tab))
  5962. ;; Table: enter it or move to the next field.
  5963. ((org-at-table-p 'any)
  5964. (if (org-at-table.el-p)
  5965. (message "Use C-c ' to edit table.el tables")
  5966. (if arg (org-table-edit-field t)
  5967. (org-table-justify-field-maybe)
  5968. (call-interactively 'org-table-next-field))))
  5969. ((run-hook-with-args-until-success
  5970. 'org-tab-after-check-for-table-hook))
  5971. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5972. ((eq arg t) (org-cycle-internal-global))
  5973. ;; Drawers: delegate to `org-flag-drawer'.
  5974. ((save-excursion
  5975. (beginning-of-line 1)
  5976. (looking-at org-drawer-regexp))
  5977. (org-flag-drawer ; toggle block visibility
  5978. (not (get-char-property (match-end 0) 'invisible))))
  5979. ;; Show-subtree, ARG levels up from here.
  5980. ((integerp arg)
  5981. (save-excursion
  5982. (org-back-to-heading)
  5983. (outline-up-heading (if (< arg 0) (- arg)
  5984. (- (funcall outline-level) arg)))
  5985. (org-show-subtree)))
  5986. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5987. ((and (featurep 'org-inlinetask)
  5988. (org-inlinetask-at-task-p)
  5989. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5990. (org-inlinetask-toggle-visibility))
  5991. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5992. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5993. (save-excursion (move-beginning-of-line 1)
  5994. (looking-at org-outline-regexp)))
  5995. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5996. (org-cycle-internal-local))
  5997. ;; From there: TAB emulation and template completion.
  5998. (buffer-read-only (org-back-to-heading))
  5999. ((run-hook-with-args-until-success
  6000. 'org-tab-after-check-for-cycling-hook))
  6001. ((org-try-structure-completion))
  6002. ((run-hook-with-args-until-success
  6003. 'org-tab-before-tab-emulation-hook))
  6004. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6005. (or (not (bolp))
  6006. (not (looking-at org-outline-regexp))))
  6007. (call-interactively (global-key-binding "\t")))
  6008. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6009. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6010. (or (and (eq org-cycle-emulate-tab 'white)
  6011. (= (match-end 0) (point-at-eol)))
  6012. (and (eq org-cycle-emulate-tab 'whitestart)
  6013. (>= (match-end 0) pos))))
  6014. t
  6015. (eq org-cycle-emulate-tab t))
  6016. (call-interactively (global-key-binding "\t")))
  6017. (t (save-excursion
  6018. (org-back-to-heading)
  6019. (org-cycle)))))))
  6020. (defun org-cycle-internal-global ()
  6021. "Do the global cycling action."
  6022. ;; Hack to avoid display of messages for .org attachments in Gnus
  6023. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6024. (cond
  6025. ((and (eq last-command this-command)
  6026. (eq org-cycle-global-status 'overview))
  6027. ;; We just created the overview - now do table of contents
  6028. ;; This can be slow in very large buffers, so indicate action
  6029. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6030. (unless ga (org-unlogged-message "CONTENTS..."))
  6031. (org-content)
  6032. (unless ga (org-unlogged-message "CONTENTS...done"))
  6033. (setq org-cycle-global-status 'contents)
  6034. (run-hook-with-args 'org-cycle-hook 'contents))
  6035. ((and (eq last-command this-command)
  6036. (eq org-cycle-global-status 'contents))
  6037. ;; We just showed the table of contents - now show everything
  6038. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6039. (show-all)
  6040. (unless ga (org-unlogged-message "SHOW ALL"))
  6041. (setq org-cycle-global-status 'all)
  6042. (run-hook-with-args 'org-cycle-hook 'all))
  6043. (t
  6044. ;; Default action: go to overview
  6045. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6046. (org-overview)
  6047. (unless ga (org-unlogged-message "OVERVIEW"))
  6048. (setq org-cycle-global-status 'overview)
  6049. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6050. (defvar org-called-with-limited-levels nil
  6051. "Non-nil when `org-with-limited-levels' is currently active.")
  6052. (defun org-cycle-internal-local ()
  6053. "Do the local cycling action."
  6054. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6055. ;; First, determine end of headline (EOH), end of subtree or item
  6056. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6057. (save-excursion
  6058. (if (org-at-item-p)
  6059. (progn
  6060. (beginning-of-line)
  6061. (setq struct (org-list-struct))
  6062. (setq eoh (point-at-eol))
  6063. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6064. (setq has-children (org-list-has-child-p (point) struct)))
  6065. (org-back-to-heading)
  6066. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6067. (setq eos (save-excursion (org-end-of-subtree t t)
  6068. (when (bolp) (backward-char)) (point)))
  6069. (setq has-children
  6070. (or (save-excursion
  6071. (let ((level (funcall outline-level)))
  6072. (outline-next-heading)
  6073. (and (org-at-heading-p t)
  6074. (> (funcall outline-level) level))))
  6075. (save-excursion
  6076. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6077. ;; Determine end invisible part of buffer (EOL)
  6078. (beginning-of-line 2)
  6079. ;; XEmacs doesn't have `next-single-char-property-change'
  6080. (if (featurep 'xemacs)
  6081. (while (and (not (eobp)) ;; this is like `next-line'
  6082. (get-char-property (1- (point)) 'invisible))
  6083. (beginning-of-line 2))
  6084. (while (and (not (eobp)) ;; this is like `next-line'
  6085. (get-char-property (1- (point)) 'invisible))
  6086. (goto-char (next-single-char-property-change (point) 'invisible))
  6087. (and (eolp) (beginning-of-line 2))))
  6088. (setq eol (point)))
  6089. ;; Find out what to do next and set `this-command'
  6090. (cond
  6091. ((= eos eoh)
  6092. ;; Nothing is hidden behind this heading
  6093. (unless (org-before-first-heading-p)
  6094. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6095. (org-unlogged-message "EMPTY ENTRY")
  6096. (setq org-cycle-subtree-status nil)
  6097. (save-excursion
  6098. (goto-char eos)
  6099. (outline-next-heading)
  6100. (if (outline-invisible-p) (org-flag-heading nil))))
  6101. ((and (or (>= eol eos)
  6102. (not (string-match "\\S-" (buffer-substring eol eos))))
  6103. (or has-children
  6104. (not (setq children-skipped
  6105. org-cycle-skip-children-state-if-no-children))))
  6106. ;; Entire subtree is hidden in one line: children view
  6107. (unless (org-before-first-heading-p)
  6108. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6109. (if (org-at-item-p)
  6110. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6111. (org-show-entry)
  6112. (org-with-limited-levels (show-children))
  6113. ;; FIXME: This slows down the func way too much.
  6114. ;; How keep drawers hidden in subtree anyway?
  6115. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6116. ;; (org-cycle-hide-drawers 'subtree))
  6117. ;; Fold every list in subtree to top-level items.
  6118. (when (eq org-cycle-include-plain-lists 'integrate)
  6119. (save-excursion
  6120. (org-back-to-heading)
  6121. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6122. (beginning-of-line 1)
  6123. (let* ((struct (org-list-struct))
  6124. (prevs (org-list-prevs-alist struct))
  6125. (end (org-list-get-bottom-point struct)))
  6126. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6127. (org-list-get-all-items (point) struct prevs))
  6128. (goto-char (if (< end eos) end eos)))))))
  6129. (org-unlogged-message "CHILDREN")
  6130. (save-excursion
  6131. (goto-char eos)
  6132. (outline-next-heading)
  6133. (if (outline-invisible-p) (org-flag-heading nil)))
  6134. (setq org-cycle-subtree-status 'children)
  6135. (unless (org-before-first-heading-p)
  6136. (run-hook-with-args 'org-cycle-hook 'children)))
  6137. ((or children-skipped
  6138. (and (eq last-command this-command)
  6139. (eq org-cycle-subtree-status 'children)))
  6140. ;; We just showed the children, or no children are there,
  6141. ;; now show everything.
  6142. (unless (org-before-first-heading-p)
  6143. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6144. (outline-flag-region eoh eos nil)
  6145. (org-unlogged-message
  6146. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6147. (setq org-cycle-subtree-status 'subtree)
  6148. (unless (org-before-first-heading-p)
  6149. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6150. (t
  6151. ;; Default action: hide the subtree.
  6152. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6153. (outline-flag-region eoh eos t)
  6154. (org-unlogged-message "FOLDED")
  6155. (setq org-cycle-subtree-status 'folded)
  6156. (unless (org-before-first-heading-p)
  6157. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6158. ;;;###autoload
  6159. (defun org-global-cycle (&optional arg)
  6160. "Cycle the global visibility. For details see `org-cycle'.
  6161. With \\[universal-argument] prefix arg, switch to startup visibility.
  6162. With a numeric prefix, show all headlines up to that level."
  6163. (interactive "P")
  6164. (let ((org-cycle-include-plain-lists
  6165. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6166. (cond
  6167. ((integerp arg)
  6168. (show-all)
  6169. (hide-sublevels arg)
  6170. (setq org-cycle-global-status 'contents))
  6171. ((equal arg '(4))
  6172. (org-set-startup-visibility)
  6173. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6174. (t
  6175. (org-cycle '(4))))))
  6176. (defun org-set-startup-visibility ()
  6177. "Set the visibility required by startup options and properties."
  6178. (cond
  6179. ((eq org-startup-folded t)
  6180. (org-overview))
  6181. ((eq org-startup-folded 'content)
  6182. (org-content))
  6183. ((or (eq org-startup-folded 'showeverything)
  6184. (eq org-startup-folded nil))
  6185. (show-all)))
  6186. (unless (eq org-startup-folded 'showeverything)
  6187. (if org-hide-block-startup (org-hide-block-all))
  6188. (org-set-visibility-according-to-property 'no-cleanup)
  6189. (org-cycle-hide-archived-subtrees 'all)
  6190. (org-cycle-hide-drawers 'all)
  6191. (org-cycle-show-empty-lines t)))
  6192. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6193. "Switch subtree visibilities according to :VISIBILITY: property."
  6194. (interactive)
  6195. (let (org-show-entry-below)
  6196. (org-with-wide-buffer
  6197. (goto-char (point-min))
  6198. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6199. (if (not (org-at-property-p)) (outline-next-heading)
  6200. (let ((state (match-string 3)))
  6201. (save-excursion
  6202. (org-back-to-heading t)
  6203. (hide-subtree)
  6204. (org-reveal)
  6205. (cond
  6206. ((equal state "folded")
  6207. (hide-subtree))
  6208. ((equal state "children")
  6209. (org-show-hidden-entry)
  6210. (show-children))
  6211. ((equal state "content")
  6212. (save-excursion
  6213. (save-restriction
  6214. (org-narrow-to-subtree)
  6215. (org-content))))
  6216. ((member state '("all" "showall"))
  6217. (show-subtree)))))))
  6218. (unless no-cleanup
  6219. (org-cycle-hide-archived-subtrees 'all)
  6220. (org-cycle-hide-drawers 'all)
  6221. (org-cycle-show-empty-lines 'all)))))
  6222. ;; This function uses outline-regexp instead of the more fundamental
  6223. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6224. ;; buffers, where outline-regexp is needed.
  6225. (defun org-overview ()
  6226. "Switch to overview mode, showing only top-level headlines.
  6227. This shows all headlines with a level equal or greater than the level
  6228. of the first headline in the buffer. This is important, because if the
  6229. first headline is not level one, then (hide-sublevels 1) gives confusing
  6230. results."
  6231. (interactive)
  6232. (save-excursion
  6233. (let ((level
  6234. (save-excursion
  6235. (goto-char (point-min))
  6236. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6237. (progn
  6238. (goto-char (match-beginning 0))
  6239. (funcall outline-level))))))
  6240. (and level (hide-sublevels level)))))
  6241. (defun org-content (&optional arg)
  6242. "Show all headlines in the buffer, like a table of contents.
  6243. With numerical argument N, show content up to level N."
  6244. (interactive "P")
  6245. (org-overview)
  6246. (save-excursion
  6247. ;; Visit all headings and show their offspring
  6248. (and (integerp arg) (org-overview))
  6249. (goto-char (point-max))
  6250. (catch 'exit
  6251. (while (and (progn (condition-case nil
  6252. (outline-previous-visible-heading 1)
  6253. (error (goto-char (point-min))))
  6254. t)
  6255. (looking-at org-outline-regexp))
  6256. (if (integerp arg)
  6257. (show-children (1- arg))
  6258. (show-branches))
  6259. (if (bobp) (throw 'exit nil))))))
  6260. (defun org-optimize-window-after-visibility-change (state)
  6261. "Adjust the window after a change in outline visibility.
  6262. This function is the default value of the hook `org-cycle-hook'."
  6263. (when (get-buffer-window (current-buffer))
  6264. (cond
  6265. ((eq state 'content) nil)
  6266. ((eq state 'all) nil)
  6267. ((eq state 'folded) nil)
  6268. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6269. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6270. (defun org-remove-empty-overlays-at (pos)
  6271. "Remove outline overlays that do not contain non-white stuff."
  6272. (mapc
  6273. (lambda (o)
  6274. (and (eq 'outline (overlay-get o 'invisible))
  6275. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6276. (overlay-end o))))
  6277. (delete-overlay o)))
  6278. (overlays-at pos)))
  6279. (defun org-clean-visibility-after-subtree-move ()
  6280. "Fix visibility issues after moving a subtree."
  6281. ;; First, find a reasonable region to look at:
  6282. ;; Start two siblings above, end three below
  6283. (let* ((beg (save-excursion
  6284. (and (org-get-last-sibling)
  6285. (org-get-last-sibling))
  6286. (point)))
  6287. (end (save-excursion
  6288. (and (org-get-next-sibling)
  6289. (org-get-next-sibling)
  6290. (org-get-next-sibling))
  6291. (if (org-at-heading-p)
  6292. (point-at-eol)
  6293. (point))))
  6294. (level (looking-at "\\*+"))
  6295. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6296. (save-excursion
  6297. (save-restriction
  6298. (narrow-to-region beg end)
  6299. (when re
  6300. ;; Properly fold already folded siblings
  6301. (goto-char (point-min))
  6302. (while (re-search-forward re nil t)
  6303. (if (and (not (outline-invisible-p))
  6304. (save-excursion
  6305. (goto-char (point-at-eol)) (outline-invisible-p)))
  6306. (hide-entry))))
  6307. (org-cycle-show-empty-lines 'overview)
  6308. (org-cycle-hide-drawers 'overview)))))
  6309. (defun org-cycle-show-empty-lines (state)
  6310. "Show empty lines above all visible headlines.
  6311. The region to be covered depends on STATE when called through
  6312. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6313. entire buffer covered. Note that an empty line is only shown if there
  6314. are at least `org-cycle-separator-lines' empty lines before the headline."
  6315. (when (/= org-cycle-separator-lines 0)
  6316. (save-excursion
  6317. (let* ((n (abs org-cycle-separator-lines))
  6318. (re (cond
  6319. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6320. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6321. (t (let ((ns (number-to-string (- n 2))))
  6322. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6323. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6324. beg end)
  6325. (cond
  6326. ((memq state '(overview contents t))
  6327. (setq beg (point-min) end (point-max)))
  6328. ((memq state '(children folded))
  6329. (setq beg (point)
  6330. end (progn (org-end-of-subtree t t)
  6331. (line-beginning-position 2)))))
  6332. (when beg
  6333. (goto-char beg)
  6334. (while (re-search-forward re end t)
  6335. (unless (get-char-property (match-end 1) 'invisible)
  6336. (let ((e (match-end 1))
  6337. (b (if (>= org-cycle-separator-lines 0)
  6338. (match-beginning 1)
  6339. (save-excursion
  6340. (goto-char (match-beginning 0))
  6341. (skip-chars-backward " \t\n")
  6342. (line-end-position)))))
  6343. (outline-flag-region b e nil))))))))
  6344. ;; Never hide empty lines at the end of the file.
  6345. (save-excursion
  6346. (goto-char (point-max))
  6347. (outline-previous-heading)
  6348. (outline-end-of-heading)
  6349. (if (and (looking-at "[ \t\n]+")
  6350. (= (match-end 0) (point-max)))
  6351. (outline-flag-region (point) (match-end 0) nil))))
  6352. (defun org-show-empty-lines-in-parent ()
  6353. "Move to the parent and re-show empty lines before visible headlines."
  6354. (save-excursion
  6355. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6356. (org-cycle-show-empty-lines context))))
  6357. (defun org-files-list ()
  6358. "Return `org-agenda-files' list, plus all open org-mode files.
  6359. This is useful for operations that need to scan all of a user's
  6360. open and agenda-wise Org files."
  6361. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6362. (dolist (buf (buffer-list))
  6363. (with-current-buffer buf
  6364. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6365. (let ((file (expand-file-name (buffer-file-name))))
  6366. (unless (member file files)
  6367. (push file files))))))
  6368. files))
  6369. (defsubst org-entry-beginning-position ()
  6370. "Return the beginning position of the current entry."
  6371. (save-excursion (outline-back-to-heading t) (point)))
  6372. (defsubst org-entry-end-position ()
  6373. "Return the end position of the current entry."
  6374. (save-excursion (outline-next-heading) (point)))
  6375. (defun org-cycle-hide-drawers (state &optional exceptions)
  6376. "Re-hide all drawers after a visibility state change.
  6377. When non-nil, optional argument EXCEPTIONS is a list of strings
  6378. specifying which drawers should not be hidden."
  6379. (when (and (derived-mode-p 'org-mode)
  6380. (not (memq state '(overview folded contents))))
  6381. (save-excursion
  6382. (let* ((globalp (memq state '(contents all)))
  6383. (beg (if globalp (point-min) (point)))
  6384. (end (if globalp (point-max)
  6385. (if (eq state 'children)
  6386. (save-excursion (outline-next-heading) (point))
  6387. (org-end-of-subtree t)))))
  6388. (goto-char beg)
  6389. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6390. (unless (member-ignore-case (match-string 1) exceptions)
  6391. (let ((drawer (org-element-at-point)))
  6392. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6393. (org-flag-drawer t drawer)
  6394. ;; Make sure to skip drawer entirely or we might flag
  6395. ;; it another time when matching its ending line with
  6396. ;; `org-drawer-regexp'.
  6397. (goto-char (org-element-property :end drawer))))))))))
  6398. (defun org-flag-drawer (flag &optional element)
  6399. "When FLAG is non-nil, hide the drawer we are at.
  6400. Otherwise make it visible. When optional argument ELEMENT is
  6401. a parsed drawer, as returned by `org-element-at-point', hide or
  6402. show that drawer instead."
  6403. (when (save-excursion
  6404. (beginning-of-line)
  6405. (org-looking-at-p org-drawer-regexp))
  6406. (let ((drawer (or element (org-element-at-point))))
  6407. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6408. (let ((post (org-element-property :post-affiliated drawer)))
  6409. (save-excursion
  6410. (outline-flag-region
  6411. (progn (goto-char post) (line-end-position))
  6412. (progn (goto-char (org-element-property :end drawer))
  6413. (skip-chars-backward " \r\t\n")
  6414. (line-end-position))
  6415. flag))
  6416. ;; When the drawer is hidden away, make sure point lies in
  6417. ;; a visible part of the buffer.
  6418. (when (and flag (> (line-beginning-position) post))
  6419. (goto-char post)))))))
  6420. (defun org-subtree-end-visible-p ()
  6421. "Is the end of the current subtree visible?"
  6422. (pos-visible-in-window-p
  6423. (save-excursion (org-end-of-subtree t) (point))))
  6424. (defun org-first-headline-recenter ()
  6425. "Move cursor to the first headline and recenter the headline."
  6426. (goto-char (point-min))
  6427. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6428. (set-window-start (selected-window) (point-at-bol))))
  6429. ;;; Saving and restoring visibility
  6430. (defun org-outline-overlay-data (&optional use-markers)
  6431. "Return a list of the locations of all outline overlays.
  6432. These are overlays with the `invisible' property value `outline'.
  6433. The return value is a list of cons cells, with start and stop
  6434. positions for each overlay.
  6435. If USE-MARKERS is set, return the positions as markers."
  6436. (let (beg end)
  6437. (save-excursion
  6438. (save-restriction
  6439. (widen)
  6440. (delq nil
  6441. (mapcar (lambda (o)
  6442. (when (eq (overlay-get o 'invisible) 'outline)
  6443. (setq beg (overlay-start o)
  6444. end (overlay-end o))
  6445. (and beg end (> end beg)
  6446. (if use-markers
  6447. (cons (copy-marker beg)
  6448. (copy-marker end t))
  6449. (cons beg end)))))
  6450. (overlays-in (point-min) (point-max))))))))
  6451. (defun org-set-outline-overlay-data (data)
  6452. "Create visibility overlays for all positions in DATA.
  6453. DATA should have been made by `org-outline-overlay-data'."
  6454. (let (o)
  6455. (save-excursion
  6456. (save-restriction
  6457. (widen)
  6458. (show-all)
  6459. (mapc (lambda (c)
  6460. (outline-flag-region (car c) (cdr c) t))
  6461. data)))))
  6462. ;;; Folding of blocks
  6463. (defvar org-hide-block-overlays nil
  6464. "Overlays hiding blocks.")
  6465. (make-variable-buffer-local 'org-hide-block-overlays)
  6466. (defun org-block-map (function &optional start end)
  6467. "Call FUNCTION at the head of all source blocks in the current buffer.
  6468. Optional arguments START and END can be used to limit the range."
  6469. (let ((start (or start (point-min)))
  6470. (end (or end (point-max))))
  6471. (save-excursion
  6472. (goto-char start)
  6473. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6474. (save-excursion
  6475. (save-match-data
  6476. (goto-char (match-beginning 0))
  6477. (funcall function)))))))
  6478. (defun org-hide-block-toggle-all ()
  6479. "Toggle the visibility of all blocks in the current buffer."
  6480. (org-block-map 'org-hide-block-toggle))
  6481. (defun org-hide-block-all ()
  6482. "Fold all blocks in the current buffer."
  6483. (interactive)
  6484. (org-show-block-all)
  6485. (org-block-map 'org-hide-block-toggle-maybe))
  6486. (defun org-show-block-all ()
  6487. "Unfold all blocks in the current buffer."
  6488. (interactive)
  6489. (mapc 'delete-overlay org-hide-block-overlays)
  6490. (setq org-hide-block-overlays nil))
  6491. (defun org-hide-block-toggle-maybe ()
  6492. "Toggle visibility of block at point.
  6493. Unlike to `org-hide-block-toggle', this function does not throw
  6494. an error. Return a non-nil value when toggling is successful."
  6495. (interactive)
  6496. (ignore-errors (org-hide-block-toggle)))
  6497. (defun org-hide-block-toggle (&optional force)
  6498. "Toggle the visibility of the current block.
  6499. When optional argument FORCE is `off', make block visible. If it
  6500. is non-nil, hide it unconditionally. Throw an error when not at
  6501. a block. Return a non-nil value when toggling is successful."
  6502. (interactive)
  6503. (let ((element (org-element-at-point)))
  6504. (unless (memq (org-element-type element)
  6505. '(center-block comment-block dynamic-block example-block
  6506. export-block quote-block special-block
  6507. src-block verse-block))
  6508. (user-error "Not at a block"))
  6509. (let* ((start (save-excursion
  6510. (goto-char (org-element-property :post-affiliated element))
  6511. (line-end-position)))
  6512. (end (save-excursion
  6513. (goto-char (org-element-property :end element))
  6514. (skip-chars-backward " \r\t\n")
  6515. (line-end-position)))
  6516. (overlays (overlays-at start)))
  6517. (cond
  6518. ;; Do nothing when not before or at the block opening line or
  6519. ;; at the block closing line.
  6520. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6521. ((and (not (eq force 'off))
  6522. (not (memq t (mapcar
  6523. (lambda (o)
  6524. (eq (overlay-get o 'invisible) 'org-hide-block))
  6525. overlays))))
  6526. (let ((ov (make-overlay start end)))
  6527. (overlay-put ov 'invisible 'org-hide-block)
  6528. ;; Make the block accessible to `isearch'.
  6529. (overlay-put
  6530. ov 'isearch-open-invisible
  6531. (lambda (ov)
  6532. (when (memq ov org-hide-block-overlays)
  6533. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6534. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6535. (delete-overlay ov))))
  6536. (push ov org-hide-block-overlays)
  6537. ;; When the block is hidden away, make sure point is left in
  6538. ;; a visible part of the buffer.
  6539. (when (> (line-beginning-position) start)
  6540. (goto-char start)
  6541. (beginning-of-line))
  6542. ;; Signal successful toggling.
  6543. t))
  6544. ((or (not force) (eq force 'off))
  6545. (dolist (ov overlays t)
  6546. (when (memq ov org-hide-block-overlays)
  6547. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6548. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6549. (delete-overlay ov))))))))
  6550. ;; org-tab-after-check-for-cycling-hook
  6551. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6552. ;; Remove overlays when changing major mode
  6553. (add-hook 'org-mode-hook
  6554. (lambda () (org-add-hook 'change-major-mode-hook
  6555. 'org-show-block-all 'append 'local)))
  6556. ;;; Org-goto
  6557. (defvar org-goto-window-configuration nil)
  6558. (defvar org-goto-marker nil)
  6559. (defvar org-goto-map)
  6560. (defun org-goto-map ()
  6561. "Set the keymap `org-goto'."
  6562. (setq org-goto-map
  6563. (let ((map (make-sparse-keymap)))
  6564. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6565. mouse-drag-region universal-argument org-occur)))
  6566. (dolist (cmd cmds)
  6567. (substitute-key-definition cmd cmd map global-map)))
  6568. (suppress-keymap map)
  6569. (org-defkey map "\C-m" 'org-goto-ret)
  6570. (org-defkey map [(return)] 'org-goto-ret)
  6571. (org-defkey map [(left)] 'org-goto-left)
  6572. (org-defkey map [(right)] 'org-goto-right)
  6573. (org-defkey map [(control ?g)] 'org-goto-quit)
  6574. (org-defkey map "\C-i" 'org-cycle)
  6575. (org-defkey map [(tab)] 'org-cycle)
  6576. (org-defkey map [(down)] 'outline-next-visible-heading)
  6577. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6578. (if org-goto-auto-isearch
  6579. (if (fboundp 'define-key-after)
  6580. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6581. nil)
  6582. (org-defkey map "q" 'org-goto-quit)
  6583. (org-defkey map "n" 'outline-next-visible-heading)
  6584. (org-defkey map "p" 'outline-previous-visible-heading)
  6585. (org-defkey map "f" 'outline-forward-same-level)
  6586. (org-defkey map "b" 'outline-backward-same-level)
  6587. (org-defkey map "u" 'outline-up-heading))
  6588. (org-defkey map "/" 'org-occur)
  6589. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6590. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6591. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6592. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6593. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6594. map)))
  6595. (defconst org-goto-help
  6596. "Browse buffer copy, to find location or copy text.%s
  6597. RET=jump to location C-g=quit and return to previous location
  6598. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6599. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6600. (defun org-goto (&optional alternative-interface)
  6601. "Look up a different location in the current file, keeping current visibility.
  6602. When you want look-up or go to a different location in a
  6603. document, the fastest way is often to fold the entire buffer and
  6604. then dive into the tree. This method has the disadvantage, that
  6605. the previous location will be folded, which may not be what you
  6606. want.
  6607. This command works around this by showing a copy of the current
  6608. buffer in an indirect buffer, in overview mode. You can dive
  6609. into the tree in that copy, use org-occur and incremental search
  6610. to find a location. When pressing RET or `Q', the command
  6611. returns to the original buffer in which the visibility is still
  6612. unchanged. After RET it will also jump to the location selected
  6613. in the indirect buffer and expose the headline hierarchy above.
  6614. With a prefix argument, use the alternative interface: e.g. if
  6615. `org-goto-interface' is 'outline use 'outline-path-completion."
  6616. (interactive "P")
  6617. (org-goto-map)
  6618. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6619. (org-refile-use-outline-path t)
  6620. (org-refile-target-verify-function nil)
  6621. (interface
  6622. (if (not alternative-interface)
  6623. org-goto-interface
  6624. (if (eq org-goto-interface 'outline)
  6625. 'outline-path-completion
  6626. 'outline)))
  6627. (org-goto-start-pos (point))
  6628. (selected-point
  6629. (if (eq interface 'outline)
  6630. (car (org-get-location (current-buffer) org-goto-help))
  6631. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6632. (org-refile-check-position pa)
  6633. (nth 3 pa)))))
  6634. (if selected-point
  6635. (progn
  6636. (org-mark-ring-push org-goto-start-pos)
  6637. (goto-char selected-point)
  6638. (if (or (outline-invisible-p) (org-invisible-p2))
  6639. (org-show-context 'org-goto)))
  6640. (message "Quit"))))
  6641. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6642. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6643. (defvar org-goto-local-auto-isearch-map) ; defined below
  6644. (defun org-get-location (buf help)
  6645. "Let the user select a location in the Org-mode buffer BUF.
  6646. This function uses a recursive edit. It returns the selected position
  6647. or nil."
  6648. (org-no-popups
  6649. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6650. (isearch-hide-immediately nil)
  6651. (isearch-search-fun-function
  6652. (lambda () 'org-goto-local-search-headings))
  6653. (org-goto-selected-point org-goto-exit-command))
  6654. (save-excursion
  6655. (save-window-excursion
  6656. (delete-other-windows)
  6657. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6658. (org-pop-to-buffer-same-window
  6659. (condition-case nil
  6660. (make-indirect-buffer (current-buffer) "*org-goto*")
  6661. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6662. (with-output-to-temp-buffer "*Org Help*"
  6663. (princ (format help (if org-goto-auto-isearch
  6664. " Just type for auto-isearch."
  6665. " n/p/f/b/u to navigate, q to quit."))))
  6666. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6667. (setq buffer-read-only nil)
  6668. (let ((org-startup-truncated t)
  6669. (org-startup-folded nil)
  6670. (org-startup-align-all-tables nil))
  6671. (org-mode)
  6672. (org-overview))
  6673. (setq buffer-read-only t)
  6674. (if (and (boundp 'org-goto-start-pos)
  6675. (integer-or-marker-p org-goto-start-pos))
  6676. (progn (goto-char org-goto-start-pos)
  6677. (when (outline-invisible-p)
  6678. (org-show-set-visibility 'lineage)))
  6679. (goto-char (point-min)))
  6680. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6681. (message "Select location and press RET")
  6682. (use-local-map org-goto-map)
  6683. (recursive-edit)))
  6684. (kill-buffer "*org-goto*")
  6685. (cons org-goto-selected-point org-goto-exit-command))))
  6686. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6687. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6688. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6689. (if (fboundp 'isearch-other-control-char)
  6690. (progn
  6691. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6692. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6693. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6694. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6695. (define-key org-goto-local-auto-isearch-map [return] nil))
  6696. (defun org-goto-local-search-headings (string bound noerror)
  6697. "Search and make sure that any matches are in headlines."
  6698. (catch 'return
  6699. (while (if isearch-forward
  6700. (search-forward string bound noerror)
  6701. (search-backward string bound noerror))
  6702. (when (save-match-data
  6703. (and (save-excursion
  6704. (beginning-of-line)
  6705. (looking-at org-complex-heading-regexp))
  6706. (or (not (match-beginning 5))
  6707. (< (point) (match-beginning 5)))))
  6708. (throw 'return (point))))))
  6709. (defun org-goto-local-auto-isearch ()
  6710. "Start isearch."
  6711. (interactive)
  6712. (goto-char (point-min))
  6713. (let ((keys (this-command-keys)))
  6714. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6715. (isearch-mode t)
  6716. (isearch-process-search-char (string-to-char keys)))))
  6717. (defun org-goto-ret (&optional arg)
  6718. "Finish `org-goto' by going to the new location."
  6719. (interactive "P")
  6720. (setq org-goto-selected-point (point)
  6721. org-goto-exit-command 'return)
  6722. (throw 'exit nil))
  6723. (defun org-goto-left ()
  6724. "Finish `org-goto' by going to the new location."
  6725. (interactive)
  6726. (if (org-at-heading-p)
  6727. (progn
  6728. (beginning-of-line 1)
  6729. (setq org-goto-selected-point (point)
  6730. org-goto-exit-command 'left)
  6731. (throw 'exit nil))
  6732. (user-error "Not on a heading")))
  6733. (defun org-goto-right ()
  6734. "Finish `org-goto' by going to the new location."
  6735. (interactive)
  6736. (if (org-at-heading-p)
  6737. (progn
  6738. (setq org-goto-selected-point (point)
  6739. org-goto-exit-command 'right)
  6740. (throw 'exit nil))
  6741. (user-error "Not on a heading")))
  6742. (defun org-goto-quit ()
  6743. "Finish `org-goto' without cursor motion."
  6744. (interactive)
  6745. (setq org-goto-selected-point nil)
  6746. (setq org-goto-exit-command 'quit)
  6747. (throw 'exit nil))
  6748. ;;; Indirect buffer display of subtrees
  6749. (defvar org-indirect-dedicated-frame nil
  6750. "This is the frame being used for indirect tree display.")
  6751. (defvar org-last-indirect-buffer nil)
  6752. (defun org-tree-to-indirect-buffer (&optional arg)
  6753. "Create indirect buffer and narrow it to current subtree.
  6754. With a numerical prefix ARG, go up to this level and then take that tree.
  6755. If ARG is negative, go up that many levels.
  6756. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6757. indirect buffer previously made with this command, to avoid proliferation of
  6758. indirect buffers. However, when you call the command with a \
  6759. \\[universal-argument] prefix, or
  6760. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6761. is kept so that you can work with several indirect buffers at the same time.
  6762. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6763. \\[universal-argument] prefix also
  6764. requests that a new frame be made for the new buffer, so that the dedicated
  6765. frame is not changed."
  6766. (interactive "P")
  6767. (let ((cbuf (current-buffer))
  6768. (cwin (selected-window))
  6769. (pos (point))
  6770. beg end level heading ibuf)
  6771. (save-excursion
  6772. (org-back-to-heading t)
  6773. (when (numberp arg)
  6774. (setq level (org-outline-level))
  6775. (if (< arg 0) (setq arg (+ level arg)))
  6776. (while (> (setq level (org-outline-level)) arg)
  6777. (org-up-heading-safe)))
  6778. (setq beg (point)
  6779. heading (org-get-heading))
  6780. (org-end-of-subtree t t)
  6781. (if (org-at-heading-p) (backward-char 1))
  6782. (setq end (point)))
  6783. (if (and (buffer-live-p org-last-indirect-buffer)
  6784. (not (eq org-indirect-buffer-display 'new-frame))
  6785. (not arg))
  6786. (kill-buffer org-last-indirect-buffer))
  6787. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6788. org-last-indirect-buffer ibuf)
  6789. (cond
  6790. ((or (eq org-indirect-buffer-display 'new-frame)
  6791. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6792. (select-frame (make-frame))
  6793. (delete-other-windows)
  6794. (org-pop-to-buffer-same-window ibuf)
  6795. (org-set-frame-title heading))
  6796. ((eq org-indirect-buffer-display 'dedicated-frame)
  6797. (raise-frame
  6798. (select-frame (or (and org-indirect-dedicated-frame
  6799. (frame-live-p org-indirect-dedicated-frame)
  6800. org-indirect-dedicated-frame)
  6801. (setq org-indirect-dedicated-frame (make-frame)))))
  6802. (delete-other-windows)
  6803. (org-pop-to-buffer-same-window ibuf)
  6804. (org-set-frame-title (concat "Indirect: " heading)))
  6805. ((eq org-indirect-buffer-display 'current-window)
  6806. (org-pop-to-buffer-same-window ibuf))
  6807. ((eq org-indirect-buffer-display 'other-window)
  6808. (pop-to-buffer ibuf))
  6809. (t (error "Invalid value")))
  6810. (if (featurep 'xemacs)
  6811. (save-excursion (org-mode) (turn-on-font-lock)))
  6812. (narrow-to-region beg end)
  6813. (show-all)
  6814. (goto-char pos)
  6815. (run-hook-with-args 'org-cycle-hook 'all)
  6816. (and (window-live-p cwin) (select-window cwin))))
  6817. (defun org-get-indirect-buffer (&optional buffer heading)
  6818. (setq buffer (or buffer (current-buffer)))
  6819. (let ((n 1) (base (buffer-name buffer)) bname)
  6820. (while (buffer-live-p
  6821. (get-buffer
  6822. (setq bname
  6823. (concat base "-"
  6824. (if heading (concat heading "-" (number-to-string n))
  6825. (number-to-string n))))))
  6826. (setq n (1+ n)))
  6827. (condition-case nil
  6828. (make-indirect-buffer buffer bname 'clone)
  6829. (error (make-indirect-buffer buffer bname)))))
  6830. (defun org-set-frame-title (title)
  6831. "Set the title of the current frame to the string TITLE."
  6832. ;; FIXME: how to name a single frame in XEmacs???
  6833. (unless (featurep 'xemacs)
  6834. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6835. ;;;; Structure editing
  6836. ;;; Inserting headlines
  6837. (defun org-previous-line-empty-p (&optional next)
  6838. "Is the previous line a blank line?
  6839. When NEXT is non-nil, check the next line instead."
  6840. (save-excursion
  6841. (and (not (bobp))
  6842. (or (beginning-of-line (if next 2 0)) t)
  6843. (save-match-data
  6844. (looking-at "[ \t]*$")))))
  6845. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6846. "Insert a new heading or an item with the same depth at point.
  6847. If point is at the beginning of a heading or a list item, insert
  6848. a new heading or a new item above the current one. If point is
  6849. at the beginning of a normal line, turn the line into a heading.
  6850. If point is in the middle of a headline or a list item, split the
  6851. headline or the item and create a new headline/item with the text
  6852. in the current line after point \(see `org-M-RET-may-split-line'
  6853. on how to modify this behavior).
  6854. With one universal prefix argument, set the user option
  6855. `org-insert-heading-respect-content' to t for the duration of
  6856. the command. This modifies the behavior described above in this
  6857. ways: on list items and at the beginning of normal lines, force
  6858. the insertion of a heading after the current subtree.
  6859. With two universal prefix arguments, insert the heading at the
  6860. end of the grandparent subtree. For example, if point is within
  6861. a 2nd-level heading, then it will insert a 2nd-level heading at
  6862. the end of the 1st-level parent heading.
  6863. If point is at the beginning of a headline, insert a sibling
  6864. before the current headline. If point is not at the beginning,
  6865. split the line and create a new headline with the text in the
  6866. current line after point \(see `org-M-RET-may-split-line' on how
  6867. to modify this behavior).
  6868. If point is at the beginning of a normal line, turn this line
  6869. into a heading.
  6870. When INVISIBLE-OK is set, stop at invisible headlines when going
  6871. back. This is important for non-interactive uses of the
  6872. command.
  6873. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6874. heading, unconditionally."
  6875. (interactive "P")
  6876. (if (org-called-interactively-p 'any) (org-reveal))
  6877. (let ((itemp (and (not top-level) (org-in-item-p)))
  6878. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6879. (respect-content (or org-insert-heading-respect-content
  6880. (equal arg '(4))))
  6881. (initial-content ""))
  6882. (cond
  6883. ((or (= (buffer-size) 0)
  6884. (and (not (save-excursion
  6885. (and (ignore-errors (org-back-to-heading invisible-ok))
  6886. (org-at-heading-p))))
  6887. (or arg (not itemp))))
  6888. ;; At beginning of buffer or so high up that only a heading
  6889. ;; makes sense.
  6890. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6891. ((not respect-content)
  6892. (unless may-split (end-of-line))
  6893. (insert "\n* "))
  6894. ((re-search-forward org-outline-regexp-bol nil t)
  6895. (beginning-of-line)
  6896. (insert "* \n")
  6897. (backward-char))
  6898. (t (goto-char (point-max))
  6899. (insert "\n* ")))
  6900. (run-hooks 'org-insert-heading-hook))
  6901. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6902. (t
  6903. ;; Maybe move at the end of the subtree
  6904. (when (equal arg '(16))
  6905. (org-up-heading-safe)
  6906. (org-end-of-subtree t))
  6907. ;; Insert a heading
  6908. (save-restriction
  6909. (widen)
  6910. (let* ((level nil)
  6911. (on-heading (org-at-heading-p))
  6912. (empty-line-p (if on-heading
  6913. (org-previous-line-empty-p)
  6914. ;; We will decide later
  6915. nil))
  6916. ;; Get a level string to fall back on.
  6917. (fix-level
  6918. (if (org-before-first-heading-p) "*"
  6919. (save-excursion
  6920. (org-back-to-heading t)
  6921. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6922. (looking-at org-outline-regexp)
  6923. (make-string (1- (length (match-string 0))) ?*))))
  6924. (stars
  6925. (save-excursion
  6926. (condition-case nil
  6927. (if top-level "* "
  6928. (org-back-to-heading invisible-ok)
  6929. (when (and (not on-heading)
  6930. (featurep 'org-inlinetask)
  6931. (integerp org-inlinetask-min-level)
  6932. (>= (length (match-string 0))
  6933. org-inlinetask-min-level))
  6934. ;; Find a heading level before the inline
  6935. ;; task.
  6936. (while (and (setq level (org-up-heading-safe))
  6937. (>= level org-inlinetask-min-level)))
  6938. (if (org-at-heading-p)
  6939. (org-back-to-heading invisible-ok)
  6940. (error "This should not happen")))
  6941. (unless (and (save-excursion
  6942. (save-match-data
  6943. (org-backward-heading-same-level
  6944. 1 invisible-ok))
  6945. (= (point) (match-beginning 0)))
  6946. (not (org-previous-line-empty-p t)))
  6947. (setq empty-line-p (or empty-line-p
  6948. (org-previous-line-empty-p))))
  6949. (match-string 0))
  6950. (error (or fix-level "* ")))))
  6951. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6952. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6953. pos hide-previous previous-pos)
  6954. ;; If we insert after content, move there and clean up
  6955. ;; whitespace.
  6956. (when (and respect-content
  6957. (not (org-looking-at-p org-outline-regexp-bol)))
  6958. (if (not (org-before-first-heading-p))
  6959. (org-end-of-subtree nil t)
  6960. (re-search-forward org-outline-regexp-bol)
  6961. (beginning-of-line 0))
  6962. (skip-chars-backward " \r\t\n")
  6963. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6964. (looking-at "[ \t]+") (replace-match ""))
  6965. (unless (eobp) (forward-char 1))
  6966. (when (looking-at "^\\*")
  6967. (unless (bobp) (backward-char 1))
  6968. (insert "\n")))
  6969. ;; If we are splitting, grab the text that should be moved
  6970. ;; to the new headline.
  6971. (when may-split
  6972. (if (org-on-heading-p)
  6973. ;; This is a heading: split intelligently (keeping
  6974. ;; tags).
  6975. (let ((pos (point)))
  6976. (beginning-of-line)
  6977. (unless (looking-at org-complex-heading-regexp)
  6978. (error "This should not happen"))
  6979. (when (and (match-beginning 4)
  6980. (> pos (match-beginning 4))
  6981. (< pos (match-end 4)))
  6982. (setq initial-content (buffer-substring pos (match-end 4)))
  6983. (goto-char pos)
  6984. (delete-region (point) (match-end 4))
  6985. (if (looking-at "[ \t]*$")
  6986. (replace-match "")
  6987. (insert (make-string (length initial-content) ?\s)))
  6988. (setq initial-content (org-trim initial-content)))
  6989. (goto-char pos))
  6990. ;; A normal line.
  6991. (setq initial-content
  6992. (org-trim
  6993. (delete-and-extract-region (point) (line-end-position))))))
  6994. ;; If we are at the beginning of the line, insert before it.
  6995. ;; Otherwise, after it.
  6996. (cond
  6997. ((and (bolp) (looking-at "[ \t]*$")))
  6998. ((bolp) (save-excursion (insert "\n")))
  6999. (t (end-of-line)
  7000. (insert "\n")))
  7001. ;; Insert the new heading
  7002. (insert stars)
  7003. (just-one-space)
  7004. (insert initial-content)
  7005. (unless (and blank (org-previous-line-empty-p))
  7006. (org-N-empty-lines-before-current (if blank 1 0)))
  7007. ;; Adjust visibility, which may be messed up if we removed
  7008. ;; blank lines while previous entry was hidden.
  7009. (let ((bol (line-beginning-position)))
  7010. (dolist (o (overlays-at (1- bol)))
  7011. (when (and (eq (overlay-get o 'invisible) 'outline)
  7012. (eq (overlay-end o) bol))
  7013. (move-overlay o (overlay-start o) (1- bol)))))
  7014. (run-hooks 'org-insert-heading-hook)))))))
  7015. (defun org-N-empty-lines-before-current (N)
  7016. "Make the number of empty lines before current exactly N.
  7017. So this will delete or add empty lines."
  7018. (save-excursion
  7019. (beginning-of-line)
  7020. (let ((p (point)))
  7021. (skip-chars-backward " \r\t\n")
  7022. (unless (bolp) (forward-line))
  7023. (delete-region (point) p))
  7024. (when (> N 0) (insert (make-string N ?\n)))))
  7025. (defun org-get-heading (&optional no-tags no-todo)
  7026. "Return the heading of the current entry, without the stars.
  7027. When NO-TAGS is non-nil, don't include tags.
  7028. When NO-TODO is non-nil, don't include TODO keywords."
  7029. (save-excursion
  7030. (org-back-to-heading t)
  7031. (cond
  7032. ((and no-tags no-todo)
  7033. (looking-at org-complex-heading-regexp)
  7034. (match-string 4))
  7035. (no-tags
  7036. (looking-at (concat org-outline-regexp
  7037. "\\(.*?\\)"
  7038. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7039. (match-string 1))
  7040. (no-todo
  7041. (looking-at org-todo-line-regexp)
  7042. (match-string 3))
  7043. (t (looking-at org-heading-regexp)
  7044. (match-string 2)))))
  7045. (defvar orgstruct-mode) ; defined below
  7046. (defun org-heading-components ()
  7047. "Return the components of the current heading.
  7048. This is a list with the following elements:
  7049. - the level as an integer
  7050. - the reduced level, different if `org-odd-levels-only' is set.
  7051. - the TODO keyword, or nil
  7052. - the priority character, like ?A, or nil if no priority is given
  7053. - the headline text itself, or the tags string if no headline text
  7054. - the tags string, or nil."
  7055. (save-excursion
  7056. (org-back-to-heading t)
  7057. (if (let (case-fold-search)
  7058. (looking-at
  7059. (if orgstruct-mode
  7060. org-heading-regexp
  7061. org-complex-heading-regexp)))
  7062. (if orgstruct-mode
  7063. (list (length (match-string 1))
  7064. (org-reduced-level (length (match-string 1)))
  7065. nil
  7066. nil
  7067. (match-string 2)
  7068. nil)
  7069. (list (length (match-string 1))
  7070. (org-reduced-level (length (match-string 1)))
  7071. (org-match-string-no-properties 2)
  7072. (and (match-end 3) (aref (match-string 3) 2))
  7073. (org-match-string-no-properties 4)
  7074. (org-match-string-no-properties 5))))))
  7075. (defun org-get-entry ()
  7076. "Get the entry text, after heading, entire subtree."
  7077. (save-excursion
  7078. (org-back-to-heading t)
  7079. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7080. (defun org-insert-heading-after-current ()
  7081. "Insert a new heading with same level as current, after current subtree."
  7082. (interactive)
  7083. (org-back-to-heading)
  7084. (org-insert-heading)
  7085. (org-move-subtree-down)
  7086. (end-of-line 1))
  7087. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7088. "Insert heading with `org-insert-heading-respect-content' set to t."
  7089. (interactive)
  7090. (org-insert-heading '(4) invisible-ok))
  7091. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7092. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7093. (interactive)
  7094. (org-insert-todo-heading force-state '(4)))
  7095. (defun org-insert-todo-heading (arg &optional force-heading)
  7096. "Insert a new heading with the same level and TODO state as current heading.
  7097. If the heading has no TODO state, or if the state is DONE, use the first
  7098. state (TODO by default). Also with one prefix arg, force first state. With
  7099. two prefix args, force inserting at the end of the parent subtree."
  7100. (interactive "P")
  7101. (when (or force-heading (not (org-insert-item 'checkbox)))
  7102. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7103. force-heading))
  7104. (save-excursion
  7105. (org-back-to-heading)
  7106. (outline-previous-heading)
  7107. (looking-at org-todo-line-regexp))
  7108. (let*
  7109. ((new-mark-x
  7110. (if (or (equal arg '(4))
  7111. (not (match-beginning 2))
  7112. (member (match-string 2) org-done-keywords))
  7113. (car org-todo-keywords-1)
  7114. (match-string 2)))
  7115. (new-mark
  7116. (or
  7117. (run-hook-with-args-until-success
  7118. 'org-todo-get-default-hook new-mark-x nil)
  7119. new-mark-x)))
  7120. (beginning-of-line 1)
  7121. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7122. (if org-treat-insert-todo-heading-as-state-change
  7123. (org-todo new-mark)
  7124. (insert new-mark " "))))
  7125. (when org-provide-todo-statistics
  7126. (org-update-parent-todo-statistics))))
  7127. (defun org-insert-subheading (arg)
  7128. "Insert a new subheading and demote it.
  7129. Works for outline headings and for plain lists alike."
  7130. (interactive "P")
  7131. (org-insert-heading arg)
  7132. (cond
  7133. ((org-at-heading-p) (org-do-demote))
  7134. ((org-at-item-p) (org-indent-item))))
  7135. (defun org-insert-todo-subheading (arg)
  7136. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7137. Works for outline headings and for plain lists alike."
  7138. (interactive "P")
  7139. (org-insert-todo-heading arg)
  7140. (cond
  7141. ((org-at-heading-p) (org-do-demote))
  7142. ((org-at-item-p) (org-indent-item))))
  7143. ;;; Promotion and Demotion
  7144. (defvar org-after-demote-entry-hook nil
  7145. "Hook run after an entry has been demoted.
  7146. The cursor will be at the beginning of the entry.
  7147. When a subtree is being demoted, the hook will be called for each node.")
  7148. (defvar org-after-promote-entry-hook nil
  7149. "Hook run after an entry has been promoted.
  7150. The cursor will be at the beginning of the entry.
  7151. When a subtree is being promoted, the hook will be called for each node.")
  7152. (defun org-promote-subtree ()
  7153. "Promote the entire subtree.
  7154. See also `org-promote'."
  7155. (interactive)
  7156. (save-excursion
  7157. (org-with-limited-levels (org-map-tree 'org-promote)))
  7158. (org-fix-position-after-promote))
  7159. (defun org-demote-subtree ()
  7160. "Demote the entire subtree. See `org-demote'.
  7161. See also `org-promote'."
  7162. (interactive)
  7163. (save-excursion
  7164. (org-with-limited-levels (org-map-tree 'org-demote)))
  7165. (org-fix-position-after-promote))
  7166. (defun org-do-promote ()
  7167. "Promote the current heading higher up the tree.
  7168. If the region is active in `transient-mark-mode', promote all headings
  7169. in the region."
  7170. (interactive)
  7171. (save-excursion
  7172. (if (org-region-active-p)
  7173. (org-map-region 'org-promote (region-beginning) (region-end))
  7174. (org-promote)))
  7175. (org-fix-position-after-promote))
  7176. (defun org-do-demote ()
  7177. "Demote the current heading lower down the tree.
  7178. If the region is active in `transient-mark-mode', demote all headings
  7179. in the region."
  7180. (interactive)
  7181. (save-excursion
  7182. (if (org-region-active-p)
  7183. (org-map-region 'org-demote (region-beginning) (region-end))
  7184. (org-demote)))
  7185. (org-fix-position-after-promote))
  7186. (defun org-fix-position-after-promote ()
  7187. "Make sure that after pro/demotion cursor position is right."
  7188. (let ((pos (point)))
  7189. (when (save-excursion
  7190. (beginning-of-line 1)
  7191. (looking-at org-todo-line-regexp)
  7192. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7193. (cond ((eobp) (insert " "))
  7194. ((eolp) (insert " "))
  7195. ((equal (char-after) ?\ ) (forward-char 1))))))
  7196. (defun org-current-level ()
  7197. "Return the level of the current entry, or nil if before the first headline.
  7198. The level is the number of stars at the beginning of the
  7199. headline. Use `org-reduced-level' to remove the effect of
  7200. `org-odd-levels'. Unlike to `org-outline-level', this function
  7201. ignores inlinetasks."
  7202. (let ((level (org-with-limited-levels (org-outline-level))))
  7203. (and (> level 0) level)))
  7204. (defun org-get-previous-line-level ()
  7205. "Return the outline depth of the last headline before the current line.
  7206. Returns 0 for the first headline in the buffer, and nil if before the
  7207. first headline."
  7208. (and (org-current-level)
  7209. (or (and (/= (line-beginning-position) (point-min))
  7210. (save-excursion (beginning-of-line 0) (org-current-level)))
  7211. 0)))
  7212. (defun org-reduced-level (l)
  7213. "Compute the effective level of a heading.
  7214. This takes into account the setting of `org-odd-levels-only'."
  7215. (cond
  7216. ((zerop l) 0)
  7217. (org-odd-levels-only (1+ (floor (/ l 2))))
  7218. (t l)))
  7219. (defun org-level-increment ()
  7220. "Return the number of stars that will be added or removed at a
  7221. time to headlines when structure editing, based on the value of
  7222. `org-odd-levels-only'."
  7223. (if org-odd-levels-only 2 1))
  7224. (defun org-get-valid-level (level &optional change)
  7225. "Rectify a level change under the influence of `org-odd-levels-only'
  7226. LEVEL is a current level, CHANGE is by how much the level should be
  7227. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7228. even level numbers will become the next higher odd number."
  7229. (if org-odd-levels-only
  7230. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7231. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7232. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7233. (max 1 (+ level (or change 0)))))
  7234. (if (boundp 'define-obsolete-function-alias)
  7235. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7236. (define-obsolete-function-alias 'org-get-legal-level
  7237. 'org-get-valid-level)
  7238. (define-obsolete-function-alias 'org-get-legal-level
  7239. 'org-get-valid-level "23.1")))
  7240. (defun org-promote ()
  7241. "Promote the current heading higher up the tree."
  7242. (org-with-wide-buffer
  7243. (org-back-to-heading t)
  7244. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7245. after-change-functions))
  7246. (level (save-match-data (funcall outline-level)))
  7247. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7248. (diff (abs (- level (length up-head) -1))))
  7249. (cond
  7250. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7251. (replace-match "# " nil t))
  7252. ((= level 1)
  7253. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7254. (t (replace-match up-head nil t)))
  7255. (unless (= level 1)
  7256. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7257. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7258. (run-hooks 'org-after-promote-entry-hook))))
  7259. (defun org-demote ()
  7260. "Demote the current heading lower down the tree."
  7261. (org-with-wide-buffer
  7262. (org-back-to-heading t)
  7263. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7264. after-change-functions))
  7265. (level (save-match-data (funcall outline-level)))
  7266. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7267. (diff (abs (- level (length down-head) -1))))
  7268. (replace-match down-head nil t)
  7269. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7270. (when org-adapt-indentation (org-fixup-indentation diff))
  7271. (run-hooks 'org-after-demote-entry-hook))))
  7272. (defun org-cycle-level ()
  7273. "Cycle the level of an empty headline through possible states.
  7274. This goes first to child, then to parent, level, then up the hierarchy.
  7275. After top level, it switches back to sibling level."
  7276. (interactive)
  7277. (let ((org-adapt-indentation nil))
  7278. (when (org-point-at-end-of-empty-headline)
  7279. (setq this-command 'org-cycle-level) ; Only needed for caching
  7280. (let ((cur-level (org-current-level))
  7281. (prev-level (org-get-previous-line-level)))
  7282. (cond
  7283. ;; If first headline in file, promote to top-level.
  7284. ((= prev-level 0)
  7285. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7286. do (org-do-promote)))
  7287. ;; If same level as prev, demote one.
  7288. ((= prev-level cur-level)
  7289. (org-do-demote))
  7290. ;; If parent is top-level, promote to top level if not already.
  7291. ((= prev-level 1)
  7292. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7293. do (org-do-promote)))
  7294. ;; If top-level, return to prev-level.
  7295. ((= cur-level 1)
  7296. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7297. do (org-do-demote)))
  7298. ;; If less than prev-level, promote one.
  7299. ((< cur-level prev-level)
  7300. (org-do-promote))
  7301. ;; If deeper than prev-level, promote until higher than
  7302. ;; prev-level.
  7303. ((> cur-level prev-level)
  7304. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7305. do (org-do-promote))))
  7306. t))))
  7307. (defun org-map-tree (fun)
  7308. "Call FUN for every heading underneath the current one."
  7309. (org-back-to-heading)
  7310. (let ((level (funcall outline-level)))
  7311. (save-excursion
  7312. (funcall fun)
  7313. (while (and (progn
  7314. (outline-next-heading)
  7315. (> (funcall outline-level) level))
  7316. (not (eobp)))
  7317. (funcall fun)))))
  7318. (defun org-map-region (fun beg end)
  7319. "Call FUN for every heading between BEG and END."
  7320. (let ((org-ignore-region t))
  7321. (save-excursion
  7322. (setq end (copy-marker end))
  7323. (goto-char beg)
  7324. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7325. (< (point) end))
  7326. (funcall fun))
  7327. (while (and (progn
  7328. (outline-next-heading)
  7329. (< (point) end))
  7330. (not (eobp)))
  7331. (funcall fun)))))
  7332. (defun org-fixup-indentation (diff)
  7333. "Change the indentation in the current entry by DIFF.
  7334. DIFF is an integer. Indentation is done according to the
  7335. following rules:
  7336. - Planning information and property drawers are always indented
  7337. according to the new level of the headline;
  7338. - Footnote definitions and their contents are ignored;
  7339. - Inlinetasks' boundaries are not shifted;
  7340. - Empty lines are ignored;
  7341. - Other lines' indentation are shifted by DIFF columns, unless
  7342. it would introduce a structural change in the document, in
  7343. which case no shifting is done at all.
  7344. Assume point is at a heading or an inlinetask beginning."
  7345. (org-with-wide-buffer
  7346. (narrow-to-region (line-beginning-position)
  7347. (save-excursion
  7348. (if (org-with-limited-levels (org-at-heading-p))
  7349. (org-with-limited-levels (outline-next-heading))
  7350. (org-inlinetask-goto-end))
  7351. (point)))
  7352. (forward-line)
  7353. ;; Indent properly planning info and property drawer.
  7354. (when (org-looking-at-p org-planning-line-re)
  7355. (org-indent-line)
  7356. (forward-line))
  7357. (when (looking-at org-property-drawer-re)
  7358. (goto-char (match-end 0))
  7359. (forward-line)
  7360. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7361. (catch 'no-shift
  7362. (when (zerop diff) (throw 'no-shift nil))
  7363. ;; If DIFF is negative, first check if a shift is possible at all
  7364. ;; (e.g., it doesn't break structure). This can only happen if
  7365. ;; some contents are not properly indented.
  7366. (let ((case-fold-search t))
  7367. (when (< diff 0)
  7368. (let ((diff (- diff))
  7369. (forbidden-re (concat org-outline-regexp
  7370. "\\|"
  7371. (substring org-footnote-definition-re 1))))
  7372. (save-excursion
  7373. (while (not (eobp))
  7374. (cond
  7375. ((org-looking-at-p "[ \t]*$") (forward-line))
  7376. ((and (org-looking-at-p org-footnote-definition-re)
  7377. (let ((e (org-element-at-point)))
  7378. (and (eq (org-element-type e) 'footnote-definition)
  7379. (goto-char (org-element-property :end e))))))
  7380. ((org-looking-at-p org-outline-regexp) (forward-line))
  7381. ;; Give up if shifting would move before column 0 or
  7382. ;; if it would introduce a headline or a footnote
  7383. ;; definition.
  7384. (t
  7385. (skip-chars-forward " \t")
  7386. (let ((ind (current-column)))
  7387. (when (or (< ind diff)
  7388. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7389. (throw 'no-shift nil)))
  7390. ;; Ignore contents of example blocks and source
  7391. ;; blocks if their indentation is meant to be
  7392. ;; preserved. Jump to block's closing line.
  7393. (beginning-of-line)
  7394. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7395. (let ((e (org-element-at-point)))
  7396. (and (memq (org-element-type e)
  7397. '(example-block src-block))
  7398. (or org-src-preserve-indentation
  7399. (org-element-property :preserve-indent e))
  7400. (goto-char (org-element-property :end e))
  7401. (progn (skip-chars-backward " \r\t\n")
  7402. (beginning-of-line)
  7403. t))))
  7404. (forward-line))))))))
  7405. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7406. ;; by DIFF. Also skip contents of source or example blocks
  7407. ;; when indentation is meant to be preserved.
  7408. (while (not (eobp))
  7409. (cond
  7410. ((and (org-looking-at-p org-footnote-definition-re)
  7411. (let ((e (org-element-at-point)))
  7412. (and (eq (org-element-type e) 'footnote-definition)
  7413. (goto-char (org-element-property :end e))))))
  7414. ((org-looking-at-p org-outline-regexp) (forward-line))
  7415. ((org-looking-at-p "[ \t]*$") (forward-line))
  7416. (t
  7417. (org-indent-line-to (+ (org-get-indentation) diff))
  7418. (beginning-of-line)
  7419. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7420. (let ((e (org-element-at-point)))
  7421. (and (memq (org-element-type e)
  7422. '(example-block src-block))
  7423. (or org-src-preserve-indentation
  7424. (org-element-property :preserve-indent e))
  7425. (goto-char (org-element-property :end e))
  7426. (progn (skip-chars-backward " \r\t\n")
  7427. (beginning-of-line)
  7428. t))))
  7429. (forward-line)))))))))
  7430. (defun org-convert-to-odd-levels ()
  7431. "Convert an org-mode file with all levels allowed to one with odd levels.
  7432. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7433. level 5 etc."
  7434. (interactive)
  7435. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7436. (let ((outline-level 'org-outline-level)
  7437. (org-odd-levels-only nil) n)
  7438. (save-excursion
  7439. (goto-char (point-min))
  7440. (while (re-search-forward "^\\*\\*+ " nil t)
  7441. (setq n (- (length (match-string 0)) 2))
  7442. (while (>= (setq n (1- n)) 0)
  7443. (org-demote))
  7444. (end-of-line 1))))))
  7445. (defun org-convert-to-oddeven-levels ()
  7446. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7447. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7448. file contains a section with an even level, conversion would
  7449. destroy the structure of the file. An error is signaled in this
  7450. case."
  7451. (interactive)
  7452. (goto-char (point-min))
  7453. ;; First check if there are no even levels
  7454. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7455. (org-show-set-visibility 'canonical)
  7456. (error "Not all levels are odd in this file. Conversion not possible"))
  7457. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7458. (let ((outline-regexp org-outline-regexp)
  7459. (outline-level 'org-outline-level)
  7460. (org-odd-levels-only nil) n)
  7461. (save-excursion
  7462. (goto-char (point-min))
  7463. (while (re-search-forward "^\\*\\*+ " nil t)
  7464. (setq n (/ (1- (length (match-string 0))) 2))
  7465. (while (>= (setq n (1- n)) 0)
  7466. (org-promote))
  7467. (end-of-line 1))))))
  7468. (defun org-tr-level (n)
  7469. "Make N odd if required."
  7470. (if org-odd-levels-only (1+ (/ n 2)) n))
  7471. ;;; Vertical tree motion, cutting and pasting of subtrees
  7472. (defun org-move-subtree-up (&optional arg)
  7473. "Move the current subtree up past ARG headlines of the same level."
  7474. (interactive "p")
  7475. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7476. (defun org-move-subtree-down (&optional arg)
  7477. "Move the current subtree down past ARG headlines of the same level."
  7478. (interactive "p")
  7479. (setq arg (prefix-numeric-value arg))
  7480. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7481. 'org-get-last-sibling))
  7482. (ins-point (make-marker))
  7483. (cnt (abs arg))
  7484. (col (current-column))
  7485. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7486. ;; Select the tree
  7487. (org-back-to-heading)
  7488. (setq beg0 (point))
  7489. (save-excursion
  7490. (setq ne-beg (org-back-over-empty-lines))
  7491. (setq beg (point)))
  7492. (save-match-data
  7493. (save-excursion (outline-end-of-heading)
  7494. (setq folded (outline-invisible-p)))
  7495. (progn (org-end-of-subtree nil t)
  7496. (unless (eobp) (backward-char))))
  7497. (outline-next-heading)
  7498. (setq ne-end (org-back-over-empty-lines))
  7499. (setq end (point))
  7500. (goto-char beg0)
  7501. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7502. ;; include less whitespace
  7503. (save-excursion
  7504. (goto-char beg)
  7505. (forward-line (- ne-beg ne-end))
  7506. (setq beg (point))))
  7507. ;; Find insertion point, with error handling
  7508. (while (> cnt 0)
  7509. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7510. (progn (goto-char beg0)
  7511. (user-error "Cannot move past superior level or buffer limit")))
  7512. (setq cnt (1- cnt)))
  7513. (if (> arg 0)
  7514. ;; Moving forward - still need to move over subtree
  7515. (progn (org-end-of-subtree t t)
  7516. (save-excursion
  7517. (org-back-over-empty-lines)
  7518. (or (bolp) (newline)))))
  7519. (setq ne-ins (org-back-over-empty-lines))
  7520. (move-marker ins-point (point))
  7521. (setq txt (buffer-substring beg end))
  7522. (org-save-markers-in-region beg end)
  7523. (delete-region beg end)
  7524. (org-remove-empty-overlays-at beg)
  7525. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7526. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7527. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7528. (let ((bbb (point)))
  7529. (insert-before-markers txt)
  7530. (org-reinstall-markers-in-region bbb)
  7531. (move-marker ins-point bbb))
  7532. (or (bolp) (insert "\n"))
  7533. (setq ins-end (point))
  7534. (goto-char ins-point)
  7535. (org-skip-whitespace)
  7536. (when (and (< arg 0)
  7537. (org-first-sibling-p)
  7538. (> ne-ins ne-beg))
  7539. ;; Move whitespace back to beginning
  7540. (save-excursion
  7541. (goto-char ins-end)
  7542. (let ((kill-whole-line t))
  7543. (kill-line (- ne-ins ne-beg)) (point)))
  7544. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7545. (move-marker ins-point nil)
  7546. (if folded
  7547. (hide-subtree)
  7548. (org-show-entry)
  7549. (show-children)
  7550. (org-cycle-hide-drawers 'children))
  7551. (org-clean-visibility-after-subtree-move)
  7552. ;; move back to the initial column we were at
  7553. (move-to-column col)))
  7554. (defvar org-subtree-clip ""
  7555. "Clipboard for cut and paste of subtrees.
  7556. This is actually only a copy of the kill, because we use the normal kill
  7557. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7558. (defvar org-subtree-clip-folded nil
  7559. "Was the last copied subtree folded?
  7560. This is used to fold the tree back after pasting.")
  7561. (defun org-cut-subtree (&optional n)
  7562. "Cut the current subtree into the clipboard.
  7563. With prefix arg N, cut this many sequential subtrees.
  7564. This is a short-hand for marking the subtree and then cutting it."
  7565. (interactive "p")
  7566. (org-copy-subtree n 'cut))
  7567. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7568. "Copy the current subtree it in the clipboard.
  7569. With prefix arg N, copy this many sequential subtrees.
  7570. This is a short-hand for marking the subtree and then copying it.
  7571. If CUT is non-nil, actually cut the subtree.
  7572. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7573. of some markers in the region, even if CUT is non-nil. This is
  7574. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7575. (interactive "p")
  7576. (let (beg end folded (beg0 (point)))
  7577. (if (org-called-interactively-p 'any)
  7578. (org-back-to-heading nil) ; take what looks like a subtree
  7579. (org-back-to-heading t)) ; take what is really there
  7580. (setq beg (point))
  7581. (skip-chars-forward " \t\r\n")
  7582. (save-match-data
  7583. (if nosubtrees
  7584. (outline-next-heading)
  7585. (save-excursion (outline-end-of-heading)
  7586. (setq folded (outline-invisible-p)))
  7587. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7588. (org-end-of-subtree t t)))
  7589. ;; Include the end of an inlinetask
  7590. (when (and (featurep 'org-inlinetask)
  7591. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7592. "END[ \t]*$")))
  7593. (end-of-line))
  7594. (setq end (point))
  7595. (goto-char beg0)
  7596. (when (> end beg)
  7597. (setq org-subtree-clip-folded folded)
  7598. (when (or cut force-store-markers)
  7599. (org-save-markers-in-region beg end))
  7600. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7601. (setq org-subtree-clip (current-kill 0))
  7602. (message "%s: Subtree(s) with %d characters"
  7603. (if cut "Cut" "Copied")
  7604. (length org-subtree-clip)))))
  7605. (defun org-paste-subtree (&optional level tree for-yank remove)
  7606. "Paste the clipboard as a subtree, with modification of headline level.
  7607. The entire subtree is promoted or demoted in order to match a new headline
  7608. level.
  7609. If the cursor is at the beginning of a headline, the same level as
  7610. that headline is used to paste the tree.
  7611. If not, the new level is derived from the *visible* headings
  7612. before and after the insertion point, and taken to be the inferior headline
  7613. level of the two. So if the previous visible heading is level 3 and the
  7614. next is level 4 (or vice versa), level 4 will be used for insertion.
  7615. This makes sure that the subtree remains an independent subtree and does
  7616. not swallow low level entries.
  7617. You can also force a different level, either by using a numeric prefix
  7618. argument, or by inserting the heading marker by hand. For example, if the
  7619. cursor is after \"*****\", then the tree will be shifted to level 5.
  7620. If optional TREE is given, use this text instead of the kill ring.
  7621. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7622. move back over whitespace before inserting, and move point to the end of
  7623. the inserted text when done.
  7624. When REMOVE is non-nil, remove the subtree from the clipboard."
  7625. (interactive "P")
  7626. (setq tree (or tree (and kill-ring (current-kill 0))))
  7627. (unless (org-kill-is-subtree-p tree)
  7628. (user-error "%s"
  7629. (substitute-command-keys
  7630. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7631. (org-with-limited-levels
  7632. (let* ((visp (not (outline-invisible-p)))
  7633. (txt tree)
  7634. (^re_ "\\(\\*+\\)[ \t]*")
  7635. (old-level (if (string-match org-outline-regexp-bol txt)
  7636. (- (match-end 0) (match-beginning 0) 1)
  7637. -1))
  7638. (force-level (cond (level (prefix-numeric-value level))
  7639. ((and (looking-at "[ \t]*$")
  7640. (string-match
  7641. "^\\*+$" (buffer-substring
  7642. (point-at-bol) (point))))
  7643. (- (match-end 0) (match-beginning 0)))
  7644. ((and (bolp)
  7645. (looking-at org-outline-regexp))
  7646. (- (match-end 0) (point) 1))))
  7647. (previous-level (save-excursion
  7648. (condition-case nil
  7649. (progn
  7650. (outline-previous-visible-heading 1)
  7651. (if (looking-at ^re_)
  7652. (- (match-end 0) (match-beginning 0) 1)
  7653. 1))
  7654. (error 1))))
  7655. (next-level (save-excursion
  7656. (condition-case nil
  7657. (progn
  7658. (or (looking-at org-outline-regexp)
  7659. (outline-next-visible-heading 1))
  7660. (if (looking-at ^re_)
  7661. (- (match-end 0) (match-beginning 0) 1)
  7662. 1))
  7663. (error 1))))
  7664. (new-level (or force-level (max previous-level next-level)))
  7665. (shift (if (or (= old-level -1)
  7666. (= new-level -1)
  7667. (= old-level new-level))
  7668. 0
  7669. (- new-level old-level)))
  7670. (delta (if (> shift 0) -1 1))
  7671. (func (if (> shift 0) 'org-demote 'org-promote))
  7672. (org-odd-levels-only nil)
  7673. beg end newend)
  7674. ;; Remove the forced level indicator
  7675. (if force-level
  7676. (delete-region (point-at-bol) (point)))
  7677. ;; Paste
  7678. (beginning-of-line (if (bolp) 1 2))
  7679. (setq beg (point))
  7680. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7681. (insert-before-markers txt)
  7682. (unless (string-match "\n\\'" txt) (insert "\n"))
  7683. (setq newend (point))
  7684. (org-reinstall-markers-in-region beg)
  7685. (setq end (point))
  7686. (goto-char beg)
  7687. (skip-chars-forward " \t\n\r")
  7688. (setq beg (point))
  7689. (if (and (outline-invisible-p) visp)
  7690. (save-excursion (outline-show-heading)))
  7691. ;; Shift if necessary
  7692. (unless (= shift 0)
  7693. (save-restriction
  7694. (narrow-to-region beg end)
  7695. (while (not (= shift 0))
  7696. (org-map-region func (point-min) (point-max))
  7697. (setq shift (+ delta shift)))
  7698. (goto-char (point-min))
  7699. (setq newend (point-max))))
  7700. (when (or (org-called-interactively-p 'interactive) for-yank)
  7701. (message "Clipboard pasted as level %d subtree" new-level))
  7702. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7703. kill-ring
  7704. (eq org-subtree-clip (current-kill 0))
  7705. org-subtree-clip-folded)
  7706. ;; The tree was folded before it was killed/copied
  7707. (hide-subtree))
  7708. (and for-yank (goto-char newend))
  7709. (and remove (setq kill-ring (cdr kill-ring))))))
  7710. (defun org-kill-is-subtree-p (&optional txt)
  7711. "Check if the current kill is an outline subtree, or a set of trees.
  7712. Returns nil if kill does not start with a headline, or if the first
  7713. headline level is not the largest headline level in the tree.
  7714. So this will actually accept several entries of equal levels as well,
  7715. which is OK for `org-paste-subtree'.
  7716. If optional TXT is given, check this string instead of the current kill."
  7717. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7718. (re (org-get-limited-outline-regexp))
  7719. (^re (concat "^" re))
  7720. (start-level (and kill
  7721. (string-match
  7722. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7723. kill)
  7724. (- (match-end 2) (match-beginning 2) 1)))
  7725. (start (1+ (or (match-beginning 2) -1))))
  7726. (if (not start-level)
  7727. (progn
  7728. nil) ;; does not even start with a heading
  7729. (catch 'exit
  7730. (while (setq start (string-match ^re kill (1+ start)))
  7731. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7732. (throw 'exit nil)))
  7733. t))))
  7734. (defvar org-markers-to-move nil
  7735. "Markers that should be moved with a cut-and-paste operation.
  7736. Those markers are stored together with their positions relative to
  7737. the start of the region.")
  7738. (defun org-save-markers-in-region (beg end)
  7739. "Check markers in region.
  7740. If these markers are between BEG and END, record their position relative
  7741. to BEG, so that after moving the block of text, we can put the markers back
  7742. into place.
  7743. This function gets called just before an entry or tree gets cut from the
  7744. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7745. called immediately, to move the markers with the entries."
  7746. (setq org-markers-to-move nil)
  7747. (when (featurep 'org-clock)
  7748. (org-clock-save-markers-for-cut-and-paste beg end))
  7749. (when (featurep 'org-agenda)
  7750. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7751. (defun org-check-and-save-marker (marker beg end)
  7752. "Check if MARKER is between BEG and END.
  7753. If yes, remember the marker and the distance to BEG."
  7754. (when (and (marker-buffer marker)
  7755. (equal (marker-buffer marker) (current-buffer)))
  7756. (if (and (>= marker beg) (< marker end))
  7757. (push (cons marker (- marker beg)) org-markers-to-move))))
  7758. (defun org-reinstall-markers-in-region (beg)
  7759. "Move all remembered markers to their position relative to BEG."
  7760. (mapc (lambda (x)
  7761. (move-marker (car x) (+ beg (cdr x))))
  7762. org-markers-to-move)
  7763. (setq org-markers-to-move nil))
  7764. (defun org-narrow-to-subtree ()
  7765. "Narrow buffer to the current subtree."
  7766. (interactive)
  7767. (save-excursion
  7768. (save-match-data
  7769. (org-with-limited-levels
  7770. (narrow-to-region
  7771. (progn (org-back-to-heading t) (point))
  7772. (progn (org-end-of-subtree t t)
  7773. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7774. (point)))))))
  7775. (defun org-narrow-to-block ()
  7776. "Narrow buffer to the current block."
  7777. (interactive)
  7778. (let* ((case-fold-search t)
  7779. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7780. "^[ \t]*#\\+end_.*")))
  7781. (if blockp
  7782. (narrow-to-region (car blockp) (cdr blockp))
  7783. (user-error "Not in a block"))))
  7784. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7785. "Clone the task (subtree) at point N times.
  7786. The clones will be inserted as siblings.
  7787. In interactive use, the user will be prompted for the number of
  7788. clones to be produced. If the entry has a timestamp, the user
  7789. will also be prompted for a time shift, which may be a repeater
  7790. as used in time stamps, for example `+3d'. To disable this,
  7791. you can call the function with a universal prefix argument.
  7792. When a valid repeater is given and the entry contains any time
  7793. stamps, the clones will become a sequence in time, with time
  7794. stamps in the subtree shifted for each clone produced. If SHIFT
  7795. is nil or the empty string, time stamps will be left alone. The
  7796. ID property of the original subtree is removed.
  7797. If the original subtree did contain time stamps with a repeater,
  7798. the following will happen:
  7799. - the repeater will be removed in each clone
  7800. - an additional clone will be produced, with the current, unshifted
  7801. date(s) in the entry.
  7802. - the original entry will be placed *after* all the clones, with
  7803. repeater intact.
  7804. - the start days in the repeater in the original entry will be shifted
  7805. to past the last clone.
  7806. In this way you can spell out a number of instances of a repeating task,
  7807. and still retain the repeater to cover future instances of the task.
  7808. As described above, N+1 clones are produced when the original
  7809. subtree has a repeater. Setting N to 0, then, can be used to
  7810. remove the repeater from a subtree and create a shifted clone
  7811. with the original repeater."
  7812. (interactive "nNumber of clones to produce: ")
  7813. (let ((shift
  7814. (or shift
  7815. (if (and (not (equal current-prefix-arg '(4)))
  7816. (save-excursion
  7817. (re-search-forward org-ts-regexp-both
  7818. (save-excursion
  7819. (org-end-of-subtree t)
  7820. (point)) t)))
  7821. (read-from-minibuffer
  7822. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7823. ""))) ;; No time shift
  7824. (n-no-remove -1)
  7825. (drawer-re org-drawer-regexp)
  7826. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7827. beg end template task idprop
  7828. shift-n shift-what doshift nmin nmax)
  7829. (unless (wholenump n)
  7830. (user-error "Invalid number of replications %s" n))
  7831. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7832. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7833. shift)))
  7834. (user-error "Invalid shift specification %s" shift))
  7835. (when doshift
  7836. (setq shift-n (string-to-number (match-string 1 shift))
  7837. shift-what (cdr (assoc (match-string 2 shift)
  7838. '(("d" . day) ("w" . week)
  7839. ("m" . month) ("y" . year))))))
  7840. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7841. (setq nmin 1 nmax n)
  7842. (org-back-to-heading t)
  7843. (setq beg (point))
  7844. (setq idprop (org-entry-get nil "ID"))
  7845. (org-end-of-subtree t t)
  7846. (or (bolp) (insert "\n"))
  7847. (setq end (point))
  7848. (setq template (buffer-substring beg end))
  7849. (when (and doshift
  7850. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7851. (delete-region beg end)
  7852. (setq end beg)
  7853. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7854. (goto-char end)
  7855. (loop for n from nmin to nmax do
  7856. ;; prepare clone
  7857. (with-temp-buffer
  7858. (insert template)
  7859. (org-mode)
  7860. (goto-char (point-min))
  7861. (org-show-subtree)
  7862. (and idprop (if org-clone-delete-id
  7863. (org-entry-delete nil "ID")
  7864. (org-id-get-create t)))
  7865. (unless (= n 0)
  7866. (while (re-search-forward org-clock-re nil t)
  7867. (kill-whole-line))
  7868. (goto-char (point-min))
  7869. (while (re-search-forward drawer-re nil t)
  7870. (org-remove-empty-drawer-at (point))))
  7871. (goto-char (point-min))
  7872. (when doshift
  7873. (while (re-search-forward org-ts-regexp-both nil t)
  7874. (org-timestamp-change (* n shift-n) shift-what))
  7875. (unless (= n n-no-remove)
  7876. (goto-char (point-min))
  7877. (while (re-search-forward org-ts-regexp nil t)
  7878. (save-excursion
  7879. (goto-char (match-beginning 0))
  7880. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7881. (delete-region (match-beginning 1) (match-end 1)))))))
  7882. (setq task (buffer-string)))
  7883. (insert task))
  7884. (goto-char beg)))
  7885. ;;; Outline Sorting
  7886. (defun org-sort (with-case)
  7887. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7888. Optional argument WITH-CASE means sort case-sensitively."
  7889. (interactive "P")
  7890. (cond
  7891. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7892. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7893. (t
  7894. (org-call-with-arg 'org-sort-entries with-case))))
  7895. (defun org-sort-remove-invisible (s)
  7896. "Remove invisible links from string S."
  7897. (remove-text-properties 0 (length s) org-rm-props s)
  7898. (while (string-match org-bracket-link-regexp s)
  7899. (setq s (replace-match (if (match-end 2)
  7900. (match-string 3 s)
  7901. (match-string 1 s))
  7902. t t s)))
  7903. (let ((st (format " %s " s)))
  7904. (while (string-match org-emph-re st)
  7905. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7906. (setq s (substring st 1 -1)))
  7907. s)
  7908. (defvar org-priority-regexp) ; defined later in the file
  7909. (defvar org-after-sorting-entries-or-items-hook nil
  7910. "Hook that is run after a bunch of entries or items have been sorted.
  7911. When children are sorted, the cursor is in the parent line when this
  7912. hook gets called. When a region or a plain list is sorted, the cursor
  7913. will be in the first entry of the sorted region/list.")
  7914. (defun org-sort-entries
  7915. (&optional with-case sorting-type getkey-func compare-func property)
  7916. "Sort entries on a certain level of an outline tree.
  7917. If there is an active region, the entries in the region are sorted.
  7918. Else, if the cursor is before the first entry, sort the top-level items.
  7919. Else, the children of the entry at point are sorted.
  7920. Sorting can be alphabetically, numerically, by date/time as given by
  7921. a time stamp, by a property, by priority order, or by a custom function.
  7922. The command prompts for the sorting type unless it has been given to the
  7923. function through the SORTING-TYPE argument, which needs to be a character,
  7924. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7925. the precise meaning of each character:
  7926. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7927. c By creation time, which is assumed to be the first inactive time stamp
  7928. at the beginning of a line.
  7929. d By deadline date/time.
  7930. k By clocking time.
  7931. n Numerically, by converting the beginning of the entry/item to a number.
  7932. o By order of TODO keywords.
  7933. p By priority according to the cookie.
  7934. r By the value of a property.
  7935. s By scheduled date/time.
  7936. t By date/time, either the first active time stamp in the entry, or, if
  7937. none exist, by the first inactive one.
  7938. Capital letters will reverse the sort order.
  7939. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7940. called with point at the beginning of the record. It must return either
  7941. a string or a number that should serve as the sorting key for that record.
  7942. Comparing entries ignores case by default. However, with an optional argument
  7943. WITH-CASE, the sorting considers case as well.
  7944. Sorting is done against the visible part of the headlines, it ignores hidden
  7945. links.
  7946. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7947. (interactive "P")
  7948. (let ((case-func (if with-case 'identity 'downcase))
  7949. (cmstr
  7950. ;; The clock marker is lost when using `sort-subr', let's
  7951. ;; store the clocking string.
  7952. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7953. (save-excursion
  7954. (goto-char org-clock-marker)
  7955. (buffer-substring-no-properties (line-beginning-position)
  7956. (point)))))
  7957. start beg end stars re re2
  7958. txt what tmp)
  7959. ;; Find beginning and end of region to sort
  7960. (cond
  7961. ((org-region-active-p)
  7962. ;; we will sort the region
  7963. (setq end (region-end)
  7964. what "region")
  7965. (goto-char (region-beginning))
  7966. (if (not (org-at-heading-p)) (outline-next-heading))
  7967. (setq start (point)))
  7968. ((or (org-at-heading-p)
  7969. (ignore-errors (progn (org-back-to-heading) t)))
  7970. ;; we will sort the children of the current headline
  7971. (org-back-to-heading)
  7972. (setq start (point)
  7973. end (progn (org-end-of-subtree t t)
  7974. (or (bolp) (insert "\n"))
  7975. (when (>= (org-back-over-empty-lines) 1)
  7976. (forward-line 1))
  7977. (point))
  7978. what "children")
  7979. (goto-char start)
  7980. (show-subtree)
  7981. (outline-next-heading))
  7982. (t
  7983. ;; we will sort the top-level entries in this file
  7984. (goto-char (point-min))
  7985. (or (org-at-heading-p) (outline-next-heading))
  7986. (setq start (point))
  7987. (goto-char (point-max))
  7988. (beginning-of-line 1)
  7989. (when (looking-at ".*?\\S-")
  7990. ;; File ends in a non-white line
  7991. (end-of-line 1)
  7992. (insert "\n"))
  7993. (setq end (point-max))
  7994. (setq what "top-level")
  7995. (goto-char start)
  7996. (show-all)))
  7997. (setq beg (point))
  7998. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7999. (looking-at "\\(\\*+\\)")
  8000. (setq stars (match-string 1)
  8001. re (concat "^" (regexp-quote stars) " +")
  8002. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8003. txt (buffer-substring beg end))
  8004. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8005. (if (and (not (equal stars "*")) (string-match re2 txt))
  8006. (user-error "Region to sort contains a level above the first entry"))
  8007. (unless sorting-type
  8008. (message
  8009. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8010. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8011. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8012. what)
  8013. (setq sorting-type (read-char-exclusive))
  8014. (unless getkey-func
  8015. (and (= (downcase sorting-type) ?f)
  8016. (setq getkey-func
  8017. (org-icompleting-read "Sort using function: "
  8018. obarray 'fboundp t nil nil))
  8019. (setq getkey-func (intern getkey-func))))
  8020. (and (= (downcase sorting-type) ?r)
  8021. (not property)
  8022. (setq property
  8023. (org-icompleting-read "Property: "
  8024. (mapcar 'list (org-buffer-property-keys t))
  8025. nil t))))
  8026. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8027. (message "Sorting entries...")
  8028. (save-restriction
  8029. (narrow-to-region start end)
  8030. (let ((dcst (downcase sorting-type))
  8031. (case-fold-search nil)
  8032. (now (current-time)))
  8033. (sort-subr
  8034. (/= dcst sorting-type)
  8035. ;; This function moves to the beginning character of the "record" to
  8036. ;; be sorted.
  8037. (lambda nil
  8038. (if (re-search-forward re nil t)
  8039. (goto-char (match-beginning 0))
  8040. (goto-char (point-max))))
  8041. ;; This function moves to the last character of the "record" being
  8042. ;; sorted.
  8043. (lambda nil
  8044. (save-match-data
  8045. (condition-case nil
  8046. (outline-forward-same-level 1)
  8047. (error
  8048. (goto-char (point-max))))))
  8049. ;; This function returns the value that gets sorted against.
  8050. (lambda nil
  8051. (cond
  8052. ((= dcst ?n)
  8053. (if (looking-at org-complex-heading-regexp)
  8054. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8055. nil))
  8056. ((= dcst ?a)
  8057. (if (looking-at org-complex-heading-regexp)
  8058. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8059. nil))
  8060. ((= dcst ?k)
  8061. (or (get-text-property (point) :org-clock-minutes) 0))
  8062. ((= dcst ?t)
  8063. (let ((end (save-excursion (outline-next-heading) (point))))
  8064. (if (or (re-search-forward org-ts-regexp end t)
  8065. (re-search-forward org-ts-regexp-both end t))
  8066. (org-time-string-to-seconds (match-string 0))
  8067. (org-float-time now))))
  8068. ((= dcst ?c)
  8069. (let ((end (save-excursion (outline-next-heading) (point))))
  8070. (if (re-search-forward
  8071. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8072. end t)
  8073. (org-time-string-to-seconds (match-string 0))
  8074. (org-float-time now))))
  8075. ((= dcst ?s)
  8076. (let ((end (save-excursion (outline-next-heading) (point))))
  8077. (if (re-search-forward org-scheduled-time-regexp end t)
  8078. (org-time-string-to-seconds (match-string 1))
  8079. (org-float-time now))))
  8080. ((= dcst ?d)
  8081. (let ((end (save-excursion (outline-next-heading) (point))))
  8082. (if (re-search-forward org-deadline-time-regexp end t)
  8083. (org-time-string-to-seconds (match-string 1))
  8084. (org-float-time now))))
  8085. ((= dcst ?p)
  8086. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8087. (string-to-char (match-string 2))
  8088. org-default-priority))
  8089. ((= dcst ?r)
  8090. (or (org-entry-get nil property) ""))
  8091. ((= dcst ?o)
  8092. (if (looking-at org-complex-heading-regexp)
  8093. (let* ((m (match-string 2))
  8094. (s (if (member m org-done-keywords) '- '+)))
  8095. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8096. ((= dcst ?f)
  8097. (if getkey-func
  8098. (progn
  8099. (setq tmp (funcall getkey-func))
  8100. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8101. tmp)
  8102. (error "Invalid key function `%s'" getkey-func)))
  8103. (t (error "Invalid sorting type `%c'" sorting-type))))
  8104. nil
  8105. (cond
  8106. ((= dcst ?a) 'string<)
  8107. ((= dcst ?f) compare-func)
  8108. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8109. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8110. ;; Reset the clock marker if needed
  8111. (when cmstr
  8112. (save-excursion
  8113. (goto-char start)
  8114. (search-forward cmstr nil t)
  8115. (move-marker org-clock-marker (point))))
  8116. (message "Sorting entries...done")))
  8117. ;;; The orgstruct minor mode
  8118. ;; Define a minor mode which can be used in other modes in order to
  8119. ;; integrate the org-mode structure editing commands.
  8120. ;; This is really a hack, because the org-mode structure commands use
  8121. ;; keys which normally belong to the major mode. Here is how it
  8122. ;; works: The minor mode defines all the keys necessary to operate the
  8123. ;; structure commands, but wraps the commands into a function which
  8124. ;; tests if the cursor is currently at a headline or a plain list
  8125. ;; item. If that is the case, the structure command is used,
  8126. ;; temporarily setting many Org-mode variables like regular
  8127. ;; expressions for filling etc. However, when any of those keys is
  8128. ;; used at a different location, function uses `key-binding' to look
  8129. ;; up if the key has an associated command in another currently active
  8130. ;; keymap (minor modes, major mode, global), and executes that
  8131. ;; command. There might be problems if any of the keys is otherwise
  8132. ;; used as a prefix key.
  8133. (defcustom orgstruct-heading-prefix-regexp ""
  8134. "Regexp that matches the custom prefix of Org headlines in
  8135. orgstruct(++)-mode."
  8136. :group 'org
  8137. :version "24.4"
  8138. :package-version '(Org . "8.3")
  8139. :type 'regexp)
  8140. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8141. (defcustom orgstruct-setup-hook nil
  8142. "Hook run after orgstruct-mode-map is filled."
  8143. :group 'org
  8144. :version "24.4"
  8145. :package-version '(Org . "8.0")
  8146. :type 'hook)
  8147. (defvar orgstruct-initialized nil)
  8148. (defvar org-local-vars nil
  8149. "List of local variables, for use by `orgstruct-mode'.")
  8150. ;;;###autoload
  8151. (define-minor-mode orgstruct-mode
  8152. "Toggle the minor mode `orgstruct-mode'.
  8153. This mode is for using Org-mode structure commands in other
  8154. modes. The following keys behave as if Org-mode were active, if
  8155. the cursor is on a headline, or on a plain list item (both as
  8156. defined by Org-mode)."
  8157. nil " OrgStruct" (make-sparse-keymap)
  8158. (funcall (if orgstruct-mode
  8159. 'add-to-invisibility-spec
  8160. 'remove-from-invisibility-spec)
  8161. '(outline . t))
  8162. (when orgstruct-mode
  8163. (org-load-modules-maybe)
  8164. (unless orgstruct-initialized
  8165. (orgstruct-setup)
  8166. (setq orgstruct-initialized t))))
  8167. ;;;###autoload
  8168. (defun turn-on-orgstruct ()
  8169. "Unconditionally turn on `orgstruct-mode'."
  8170. (orgstruct-mode 1))
  8171. (defvar org-fb-vars nil)
  8172. (make-variable-buffer-local 'org-fb-vars)
  8173. (defun orgstruct++-mode (&optional arg)
  8174. "Toggle `orgstruct-mode', the enhanced version of it.
  8175. In addition to setting orgstruct-mode, this also exports all
  8176. indentation and autofilling variables from org-mode into the
  8177. buffer. It will also recognize item context in multiline items."
  8178. (interactive "P")
  8179. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8180. (if (< arg 1)
  8181. (progn (orgstruct-mode -1)
  8182. (mapc (lambda(v)
  8183. (org-set-local (car v)
  8184. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8185. org-fb-vars))
  8186. (orgstruct-mode 1)
  8187. (setq org-fb-vars nil)
  8188. (unless org-local-vars
  8189. (setq org-local-vars (org-get-local-variables)))
  8190. (let (var val)
  8191. (mapc
  8192. (lambda (x)
  8193. (when (string-match
  8194. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8195. (symbol-name (car x)))
  8196. (setq var (car x) val (nth 1 x))
  8197. (push (list var `(quote ,(eval var))) org-fb-vars)
  8198. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8199. org-local-vars)
  8200. (org-set-local 'orgstruct-is-++ t))))
  8201. (defvar orgstruct-is-++ nil
  8202. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8203. (make-variable-buffer-local 'orgstruct-is-++)
  8204. ;;;###autoload
  8205. (defun turn-on-orgstruct++ ()
  8206. "Unconditionally turn on `orgstruct++-mode'."
  8207. (orgstruct++-mode 1))
  8208. (defun orgstruct-error ()
  8209. "Error when there is no default binding for a structure key."
  8210. (interactive)
  8211. (funcall (if (fboundp 'user-error)
  8212. 'user-error
  8213. 'error)
  8214. "This key has no function outside structure elements"))
  8215. (defun orgstruct-setup ()
  8216. "Setup orgstruct keymap."
  8217. (dolist (cell '((org-demote . t)
  8218. (org-metaleft . t)
  8219. (org-metaright . t)
  8220. (org-promote . t)
  8221. (org-shiftmetaleft . t)
  8222. (org-shiftmetaright . t)
  8223. org-backward-element
  8224. org-backward-heading-same-level
  8225. org-ctrl-c-ret
  8226. org-ctrl-c-minus
  8227. org-ctrl-c-star
  8228. org-cycle
  8229. org-forward-heading-same-level
  8230. org-insert-heading
  8231. org-insert-heading-respect-content
  8232. org-kill-note-or-show-branches
  8233. org-mark-subtree
  8234. org-meta-return
  8235. org-metadown
  8236. org-metaup
  8237. org-narrow-to-subtree
  8238. org-promote-subtree
  8239. org-reveal
  8240. org-shiftdown
  8241. org-shiftleft
  8242. org-shiftmetadown
  8243. org-shiftmetaup
  8244. org-shiftright
  8245. org-shifttab
  8246. org-shifttab
  8247. org-shiftup
  8248. org-show-subtree
  8249. org-sort
  8250. org-up-element
  8251. outline-demote
  8252. outline-next-visible-heading
  8253. outline-previous-visible-heading
  8254. outline-promote
  8255. outline-up-heading
  8256. show-children))
  8257. (let ((f (or (car-safe cell) cell))
  8258. (disable-when-heading-prefix (cdr-safe cell)))
  8259. (when (fboundp f)
  8260. (let ((new-bindings))
  8261. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8262. (where-is-internal f outline-mode-map)))
  8263. (push binding new-bindings)
  8264. ;; TODO use local-function-key-map
  8265. (dolist (rep '(("<tab>" . "TAB")
  8266. ("<return>" . "RET")
  8267. ("<escape>" . "ESC")
  8268. ("<delete>" . "DEL")))
  8269. (setq binding (read-kbd-macro
  8270. (let ((case-fold-search))
  8271. (replace-regexp-in-string
  8272. (regexp-quote (cdr rep))
  8273. (car rep)
  8274. (key-description binding)))))
  8275. (pushnew binding new-bindings :test 'equal)))
  8276. (dolist (binding new-bindings)
  8277. (let ((key (lookup-key orgstruct-mode-map binding)))
  8278. (when (or (not key) (numberp key))
  8279. (ignore-errors
  8280. (org-defkey orgstruct-mode-map
  8281. binding
  8282. (orgstruct-make-binding
  8283. f binding disable-when-heading-prefix))))))))))
  8284. (run-hooks 'orgstruct-setup-hook))
  8285. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8286. "Create a function for binding in the structure minor mode.
  8287. FUN is the command to call inside a table. KEY is the key that
  8288. should be checked in for a command to execute outside of tables.
  8289. Non-nil `disable-when-heading-prefix' means to disable the command
  8290. if `orgstruct-heading-prefix-regexp' is not empty."
  8291. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8292. (let ((nname name)
  8293. (i 0))
  8294. (while (fboundp (intern nname))
  8295. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8296. (setq name (intern nname)))
  8297. (eval
  8298. (let ((bindings '((org-heading-regexp
  8299. (concat "^"
  8300. orgstruct-heading-prefix-regexp
  8301. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8302. (org-outline-regexp
  8303. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8304. (org-outline-regexp-bol
  8305. (concat "^" org-outline-regexp))
  8306. (outline-regexp org-outline-regexp)
  8307. (outline-heading-end-regexp "\n")
  8308. (outline-level 'org-outline-level)
  8309. (outline-heading-alist))))
  8310. `(defun ,name (arg)
  8311. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8312. "Outside of structure, run the binding of `"
  8313. (key-description key) "'."
  8314. (when disable-when-heading-prefix
  8315. (concat
  8316. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8317. "back to the default binding due to limitations of Org's implementation of\n"
  8318. "`" (symbol-name fun) "'.")))
  8319. (interactive "p")
  8320. (let* ((disable
  8321. ,(and disable-when-heading-prefix
  8322. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8323. (fallback
  8324. (or disable
  8325. (not
  8326. (let* ,bindings
  8327. (org-context-p 'headline 'item
  8328. ,(when (memq fun
  8329. '(org-insert-heading
  8330. org-insert-heading-respect-content
  8331. org-meta-return))
  8332. '(when orgstruct-is-++
  8333. 'item-body))))))))
  8334. (if fallback
  8335. (let* ((orgstruct-mode)
  8336. (binding
  8337. (let ((key ,key))
  8338. (catch 'exit
  8339. (dolist
  8340. (rep
  8341. '(nil
  8342. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8343. ("<\\([^>]*\\)return>" . "\\1RET")
  8344. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8345. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8346. nil)
  8347. (when rep
  8348. (setq key (read-kbd-macro
  8349. (let ((case-fold-search))
  8350. (replace-regexp-in-string
  8351. (car rep)
  8352. (cdr rep)
  8353. (key-description key))))))
  8354. (when (key-binding key)
  8355. (throw 'exit (key-binding key))))))))
  8356. (if (keymapp binding)
  8357. (org-set-transient-map binding)
  8358. (let ((func (or binding
  8359. (unless disable
  8360. 'orgstruct-error))))
  8361. (when func
  8362. (call-interactively func)))))
  8363. (org-run-like-in-org-mode
  8364. (lambda ()
  8365. (interactive)
  8366. (let* ,bindings
  8367. (call-interactively ',fun)))))))))
  8368. name))
  8369. (defun org-contextualize-keys (alist contexts)
  8370. "Return valid elements in ALIST depending on CONTEXTS.
  8371. `org-agenda-custom-commands' or `org-capture-templates' are the
  8372. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8373. or `org-capture-templates-contexts' are the associated contexts
  8374. definitions."
  8375. (let ((contexts
  8376. ;; normalize contexts
  8377. (mapcar
  8378. (lambda(c) (cond ((listp (cadr c))
  8379. (list (car c) (car c) (cadr c)))
  8380. ((string= "" (cadr c))
  8381. (list (car c) (car c) (caddr c)))
  8382. (t c)))
  8383. contexts))
  8384. (a alist) r s)
  8385. ;; loop over all commands or templates
  8386. (dolist (c a)
  8387. (let (vrules repl)
  8388. (cond
  8389. ((not (assoc (car c) contexts))
  8390. (push c r))
  8391. ((and (assoc (car c) contexts)
  8392. (setq vrules (org-contextualize-validate-key
  8393. (car c) contexts)))
  8394. (mapc (lambda (vr)
  8395. (when (not (equal (car vr) (cadr vr)))
  8396. (setq repl vr)))
  8397. vrules)
  8398. (if (not repl) (push c r)
  8399. (push (cadr repl) s)
  8400. (push
  8401. (cons (car c)
  8402. (cdr (or (assoc (cadr repl) alist)
  8403. (error "Undefined key `%s' as contextual replacement for `%s'"
  8404. (cadr repl) (car c)))))
  8405. r))))))
  8406. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8407. (delq
  8408. nil
  8409. (delete-dups
  8410. (mapcar (lambda (x)
  8411. (let ((tpl (car x)))
  8412. (when (not (delq
  8413. nil
  8414. (mapcar (lambda (y)
  8415. (equal y tpl))
  8416. s)))
  8417. x)))
  8418. (reverse r))))))
  8419. (defun org-contextualize-validate-key (key contexts)
  8420. "Check CONTEXTS for agenda or capture KEY."
  8421. (let (rr res)
  8422. (dolist (r contexts)
  8423. (mapc
  8424. (lambda (rr)
  8425. (when
  8426. (and (equal key (car r))
  8427. (if (functionp rr) (funcall rr)
  8428. (or (and (eq (car rr) 'in-file)
  8429. (buffer-file-name)
  8430. (string-match (cdr rr) (buffer-file-name)))
  8431. (and (eq (car rr) 'in-mode)
  8432. (string-match (cdr rr) (symbol-name major-mode)))
  8433. (and (eq (car rr) 'in-buffer)
  8434. (string-match (cdr rr) (buffer-name)))
  8435. (when (and (eq (car rr) 'not-in-file)
  8436. (buffer-file-name))
  8437. (not (string-match (cdr rr) (buffer-file-name))))
  8438. (when (eq (car rr) 'not-in-mode)
  8439. (not (string-match (cdr rr) (symbol-name major-mode))))
  8440. (when (eq (car rr) 'not-in-buffer)
  8441. (not (string-match (cdr rr) (buffer-name)))))))
  8442. (push r res)))
  8443. (car (last r))))
  8444. (delete-dups (delq nil res))))
  8445. (defun org-context-p (&rest contexts)
  8446. "Check if local context is any of CONTEXTS.
  8447. Possible values in the list of contexts are `table', `headline', and `item'."
  8448. (let ((pos (point)))
  8449. (goto-char (point-at-bol))
  8450. (prog1 (or (and (memq 'table contexts)
  8451. (looking-at "[ \t]*|"))
  8452. (and (memq 'headline contexts)
  8453. (looking-at org-outline-regexp))
  8454. (and (memq 'item contexts)
  8455. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8456. (and (memq 'item-body contexts)
  8457. (org-in-item-p)))
  8458. (goto-char pos))))
  8459. (defun org-get-local-variables ()
  8460. "Return a list of all local variables in an Org mode buffer."
  8461. (let (varlist)
  8462. (with-current-buffer (get-buffer-create "*Org tmp*")
  8463. (erase-buffer)
  8464. (org-mode)
  8465. (setq varlist (buffer-local-variables)))
  8466. (kill-buffer "*Org tmp*")
  8467. (delq nil
  8468. (mapcar
  8469. (lambda (x)
  8470. (setq x
  8471. (if (symbolp x)
  8472. (list x)
  8473. (list (car x) (cdr x))))
  8474. (if (and (not (get (car x) 'org-state))
  8475. (string-match
  8476. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8477. (symbol-name (car x))))
  8478. x nil))
  8479. varlist))))
  8480. (defun org-clone-local-variables (from-buffer &optional regexp)
  8481. "Clone local variables from FROM-BUFFER.
  8482. Optional argument REGEXP selects variables to clone."
  8483. (mapc
  8484. (lambda (pair)
  8485. (and (symbolp (car pair))
  8486. (or (null regexp)
  8487. (string-match regexp (symbol-name (car pair))))
  8488. (set (make-local-variable (car pair))
  8489. (cdr pair))))
  8490. (buffer-local-variables from-buffer)))
  8491. ;;;###autoload
  8492. (defun org-run-like-in-org-mode (cmd)
  8493. "Run a command, pretending that the current buffer is in Org-mode.
  8494. This will temporarily bind local variables that are typically bound in
  8495. Org-mode to the values they have in Org-mode, and then interactively
  8496. call CMD."
  8497. (org-load-modules-maybe)
  8498. (unless org-local-vars
  8499. (setq org-local-vars (org-get-local-variables)))
  8500. (let (binds)
  8501. (dolist (var org-local-vars)
  8502. (when (or (not (boundp (car var)))
  8503. (eq (symbol-value (car var))
  8504. (default-value (car var))))
  8505. (push (list (car var) `(quote ,(cadr var))) binds)))
  8506. (eval `(let ,binds
  8507. (call-interactively (quote ,cmd))))))
  8508. (defun org-get-category (&optional pos force-refresh)
  8509. "Get the category applying to position POS."
  8510. (save-match-data
  8511. (if force-refresh (org-refresh-category-properties))
  8512. (let ((pos (or pos (point))))
  8513. (or (get-text-property pos 'org-category)
  8514. (progn (org-refresh-category-properties)
  8515. (get-text-property pos 'org-category))))))
  8516. ;;; Refresh properties
  8517. (defun org-refresh-properties (dprop tprop)
  8518. "Refresh buffer text properties.
  8519. DPROP is the drawer property and TPROP is either the
  8520. corresponding text property to set, or an alist with each element
  8521. being a text property (as a symbol) and a function to apply to
  8522. the value of the drawer property."
  8523. (let ((case-fold-search t)
  8524. (inhibit-read-only t))
  8525. (org-with-silent-modifications
  8526. (save-excursion
  8527. (save-restriction
  8528. (widen)
  8529. (goto-char (point-min))
  8530. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8531. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8532. (defun org-refresh-property (tprop p)
  8533. "Refresh the buffer text property TPROP from the drawer property P.
  8534. The refresh happens only for the current tree (not subtree)."
  8535. (unless (org-before-first-heading-p)
  8536. (save-excursion
  8537. (org-back-to-heading t)
  8538. (if (symbolp tprop)
  8539. ;; TPROP is a text property symbol
  8540. (put-text-property
  8541. (point) (or (outline-next-heading) (point-max)) tprop p)
  8542. ;; TPROP is an alist with (properties . function) elements
  8543. (dolist (al tprop)
  8544. (save-excursion
  8545. (put-text-property
  8546. (line-beginning-position) (or (outline-next-heading) (point-max))
  8547. (car al)
  8548. (funcall (cdr al) p))))))))
  8549. (defun org-refresh-category-properties ()
  8550. "Refresh category text properties in the buffer."
  8551. (let ((case-fold-search t)
  8552. (inhibit-read-only t)
  8553. (default-category
  8554. (cond ((null org-category)
  8555. (if buffer-file-name
  8556. (file-name-sans-extension
  8557. (file-name-nondirectory buffer-file-name))
  8558. "???"))
  8559. ((symbolp org-category) (symbol-name org-category))
  8560. (t org-category))))
  8561. (org-with-silent-modifications
  8562. (org-with-wide-buffer
  8563. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8564. ;; This is the default category for the buffer. If none is
  8565. ;; found, fall-back to `org-category' or buffer file name.
  8566. (put-text-property
  8567. (point-min) (point-max)
  8568. 'org-category
  8569. (catch 'buffer-category
  8570. (goto-char (point-max))
  8571. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8572. (let ((element (org-element-at-point)))
  8573. (when (eq (org-element-type element) 'keyword)
  8574. (throw 'buffer-category
  8575. (org-element-property :value element)))))
  8576. default-category))
  8577. ;; Set sub-tree specific categories.
  8578. (goto-char (point-min))
  8579. (let ((regexp (org-re-property "CATEGORY")))
  8580. (while (re-search-forward regexp nil t)
  8581. (let ((value (org-match-string-no-properties 3)))
  8582. (when (org-at-property-p)
  8583. (put-text-property
  8584. (save-excursion (org-back-to-heading t) (point))
  8585. (save-excursion (org-end-of-subtree t t) (point))
  8586. 'org-category
  8587. value)))))))))
  8588. (defun org-refresh-stats-properties ()
  8589. "Refresh stats text properties in the buffer."
  8590. (let (stats)
  8591. (org-with-silent-modifications
  8592. (save-excursion
  8593. (save-restriction
  8594. (widen)
  8595. (goto-char (point-min))
  8596. (while (re-search-forward
  8597. (concat org-outline-regexp-bol ".*"
  8598. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8599. nil t)
  8600. (setq stats (cond ((equal (match-string 3) "0") 0)
  8601. ((match-string 2)
  8602. (/ (* (string-to-number (match-string 2)) 100)
  8603. (string-to-number (match-string 3))))
  8604. (t (string-to-number (match-string 1)))))
  8605. (org-back-to-heading t)
  8606. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8607. 'org-stats stats)))))))
  8608. (defun org-refresh-effort-properties ()
  8609. "Refresh effort properties"
  8610. (org-refresh-properties
  8611. org-effort-property
  8612. '((effort . identity)
  8613. (effort-minutes . org-duration-string-to-minutes))))
  8614. ;;;; Link Stuff
  8615. ;;; Link abbreviations
  8616. (defun org-link-expand-abbrev (link)
  8617. "Apply replacements as defined in `org-link-abbrev-alist'."
  8618. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8619. (let* ((key (match-string 1 link))
  8620. (as (or (assoc key org-link-abbrev-alist-local)
  8621. (assoc key org-link-abbrev-alist)))
  8622. (tag (and (match-end 2) (match-string 3 link)))
  8623. rpl)
  8624. (if (not as)
  8625. link
  8626. (setq rpl (cdr as))
  8627. (cond
  8628. ((symbolp rpl) (funcall rpl tag))
  8629. ((string-match "%(\\([^)]+\\))" rpl)
  8630. (replace-match
  8631. (save-match-data
  8632. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8633. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8634. ((string-match "%h" rpl)
  8635. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8636. (t (concat rpl tag)))))
  8637. link))
  8638. ;;; Storing and inserting links
  8639. (defvar org-insert-link-history nil
  8640. "Minibuffer history for links inserted with `org-insert-link'.")
  8641. (defvar org-stored-links nil
  8642. "Contains the links stored with `org-store-link'.")
  8643. (defvar org-store-link-plist nil
  8644. "Plist with info about the most recently link created with `org-store-link'.")
  8645. (defvar org-link-protocols nil
  8646. "Link protocols added to Org-mode using `org-add-link-type'.")
  8647. (defvar org-store-link-functions nil
  8648. "List of functions that are called to create and store a link.
  8649. Each function will be called in turn until one returns a non-nil
  8650. value. Each function should check if it is responsible for creating
  8651. this link (for example by looking at the major mode).
  8652. If not, it must exit and return nil.
  8653. If yes, it should return a non-nil value after a calling
  8654. `org-store-link-props' with a list of properties and values.
  8655. Special properties are:
  8656. :type The link prefix, like \"http\". This must be given.
  8657. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8658. This is obligatory as well.
  8659. :description Optional default description for the second pair
  8660. of brackets in an Org-mode link. The user can still change
  8661. this when inserting this link into an Org-mode buffer.
  8662. In addition to these, any additional properties can be specified
  8663. and then used in capture templates.")
  8664. (defun org-add-link-type (type &optional follow export)
  8665. "Add TYPE to the list of `org-link-types'.
  8666. Re-compute all regular expressions depending on `org-link-types'
  8667. FOLLOW and EXPORT are two functions.
  8668. FOLLOW should take the link path as the single argument and do whatever
  8669. is necessary to follow the link, for example find a file or display
  8670. a mail message.
  8671. EXPORT should format the link path for export to one of the export formats.
  8672. It should be a function accepting three arguments:
  8673. path the path of the link, the text after the prefix (like \"http:\")
  8674. desc the description of the link, if any
  8675. format the export format, a symbol like `html' or `latex' or `ascii'.
  8676. The function may use the FORMAT information to return different values
  8677. depending on the format. The return value will be put literally into
  8678. the exported file. If the return value is nil, this means Org should
  8679. do what it normally does with links which do not have EXPORT defined.
  8680. Org mode has a built-in default for exporting links. If you are happy with
  8681. this default, there is no need to define an export function for the link
  8682. type. For a simple example of an export function, see `org-bbdb.el'."
  8683. (add-to-list 'org-link-types type t)
  8684. (org-make-link-regexps)
  8685. (org-element-update-syntax)
  8686. (if (assoc type org-link-protocols)
  8687. (setcdr (assoc type org-link-protocols) (list follow export))
  8688. (push (list type follow export) org-link-protocols)))
  8689. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8690. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8691. ;;;###autoload
  8692. (defun org-store-link (arg)
  8693. "\\<org-mode-map>Store an org-link to the current location.
  8694. This link is added to `org-stored-links' and can later be inserted
  8695. into an Org buffer with \\[org-insert-link].
  8696. For some link types, a prefix ARG is interpreted.
  8697. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8698. For file links, ARG negates `org-context-in-file-links'.
  8699. A double prefix ARG force skipping storing functions that are not
  8700. part of Org's core.
  8701. A triple prefix ARG force storing a link for each line in the
  8702. active region."
  8703. (interactive "P")
  8704. (org-load-modules-maybe)
  8705. (if (and (equal arg '(64)) (org-region-active-p))
  8706. (save-excursion
  8707. (let ((end (region-end)))
  8708. (goto-char (region-beginning))
  8709. (set-mark (point))
  8710. (while (< (point-at-eol) end)
  8711. (move-end-of-line 1) (activate-mark)
  8712. (let (current-prefix-arg)
  8713. (call-interactively 'org-store-link))
  8714. (move-beginning-of-line 2)
  8715. (set-mark (point)))))
  8716. (org-with-limited-levels
  8717. (setq org-store-link-plist nil)
  8718. (let (link cpltxt desc description search
  8719. txt custom-id agenda-link sfuns sfunsn)
  8720. (cond
  8721. ;; Store a link using an external link type
  8722. ((and (not (equal arg '(16)))
  8723. (setq sfuns
  8724. (delq
  8725. nil (mapcar (lambda (f)
  8726. (let (fs) (if (funcall f) (push f fs))))
  8727. org-store-link-functions))
  8728. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8729. (or (and (cdr sfuns)
  8730. (funcall (intern
  8731. (completing-read
  8732. "Which function for creating the link? "
  8733. sfunsn nil t (car sfunsn)))))
  8734. (funcall (caar sfuns)))
  8735. (setq link (plist-get org-store-link-plist :link)
  8736. desc (or (plist-get org-store-link-plist
  8737. :description)
  8738. link))))
  8739. ;; Store a link from a source code buffer.
  8740. ((org-src-edit-buffer-p)
  8741. (cond
  8742. ((save-excursion
  8743. (beginning-of-line)
  8744. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8745. "[ \t]*$")))
  8746. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8747. ((org-called-interactively-p 'any)
  8748. (let (label)
  8749. (while (or (not label)
  8750. (org-with-wide-buffer
  8751. (goto-char (point-min))
  8752. (re-search-forward
  8753. (regexp-quote (format org-coderef-label-format label))
  8754. nil t)))
  8755. (when label (message "Label exists already") (sit-for 2))
  8756. (setq label (read-string "Code line label: " label)))
  8757. (end-of-line)
  8758. (setq link (format org-coderef-label-format label))
  8759. (let ((gc (- 79 (length link))))
  8760. (if (< (current-column) gc) (org-move-to-column gc t)
  8761. (insert " ")))
  8762. (insert link)
  8763. (setq link (concat "(" label ")") desc nil)))
  8764. (t (setq link nil))))
  8765. ;; We are in the agenda, link to referenced location
  8766. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8767. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8768. (get-text-property (point) 'org-marker))))
  8769. (when m
  8770. (org-with-point-at m
  8771. (setq agenda-link
  8772. (if (org-called-interactively-p 'any)
  8773. (call-interactively 'org-store-link)
  8774. (org-store-link nil)))))))
  8775. ((eq major-mode 'calendar-mode)
  8776. (let ((cd (calendar-cursor-to-date)))
  8777. (setq link
  8778. (format-time-string
  8779. (car org-time-stamp-formats)
  8780. (apply 'encode-time
  8781. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8782. nil nil nil))))
  8783. (org-store-link-props :type "calendar" :date cd)))
  8784. ((eq major-mode 'help-mode)
  8785. (setq link (concat "help:" (save-excursion
  8786. (goto-char (point-min))
  8787. (looking-at "^[^ ]+")
  8788. (match-string 0))))
  8789. (org-store-link-props :type "help"))
  8790. ((eq major-mode 'w3-mode)
  8791. (setq cpltxt (if (and (buffer-name)
  8792. (not (string-match "Untitled" (buffer-name))))
  8793. (buffer-name)
  8794. (url-view-url t))
  8795. link (url-view-url t))
  8796. (org-store-link-props :type "w3" :url (url-view-url t)))
  8797. ((eq major-mode 'image-mode)
  8798. (setq cpltxt (concat "file:"
  8799. (abbreviate-file-name buffer-file-name))
  8800. link cpltxt)
  8801. (org-store-link-props :type "image" :file buffer-file-name))
  8802. ;; In dired, store a link to the file of the current line
  8803. ((derived-mode-p 'dired-mode)
  8804. (let ((file (dired-get-filename nil t)))
  8805. (setq file (if file
  8806. (abbreviate-file-name
  8807. (expand-file-name (dired-get-filename nil t)))
  8808. ;; otherwise, no file so use current directory.
  8809. default-directory))
  8810. (setq cpltxt (concat "file:" file)
  8811. link cpltxt)))
  8812. ((setq search (run-hook-with-args-until-success
  8813. 'org-create-file-search-functions))
  8814. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8815. "::" search))
  8816. (setq cpltxt (or description link)))
  8817. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8818. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8819. (cond
  8820. ;; Store a link using the target at point
  8821. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8822. (setq cpltxt
  8823. (concat "file:"
  8824. (abbreviate-file-name
  8825. (buffer-file-name (buffer-base-buffer)))
  8826. "::" (match-string 1))
  8827. link cpltxt))
  8828. ((and (featurep 'org-id)
  8829. (or (eq org-id-link-to-org-use-id t)
  8830. (and (org-called-interactively-p 'any)
  8831. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8832. (and (eq org-id-link-to-org-use-id
  8833. 'create-if-interactive-and-no-custom-id)
  8834. (not custom-id))))
  8835. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8836. ;; Store a link using the ID at point
  8837. (setq link (condition-case nil
  8838. (prog1 (org-id-store-link)
  8839. (setq desc (or (plist-get org-store-link-plist
  8840. :description)
  8841. "")))
  8842. (error
  8843. ;; Probably before first headline, link only to file
  8844. (concat "file:"
  8845. (abbreviate-file-name
  8846. (buffer-file-name (buffer-base-buffer))))))))
  8847. (t
  8848. ;; Just link to current headline
  8849. (setq cpltxt (concat "file:"
  8850. (abbreviate-file-name
  8851. (buffer-file-name (buffer-base-buffer)))))
  8852. ;; Add a context search string
  8853. (when (org-xor org-context-in-file-links arg)
  8854. (let* ((ee (org-element-at-point))
  8855. (et (org-element-type ee))
  8856. (ev (plist-get (cadr ee) :value))
  8857. (ek (plist-get (cadr ee) :key))
  8858. (eok (and (stringp ek) (string-match "name" ek))))
  8859. (setq txt (cond
  8860. ((org-at-heading-p) nil)
  8861. ((and (eq et 'keyword) eok) ev)
  8862. ((org-region-active-p)
  8863. (buffer-substring (region-beginning) (region-end)))))
  8864. (when (or (null txt) (string-match "\\S-" txt))
  8865. (setq cpltxt
  8866. (concat cpltxt "::"
  8867. (condition-case nil
  8868. (org-make-org-heading-search-string txt)
  8869. (error "")))
  8870. desc (or (and (eq et 'keyword) eok ev)
  8871. (nth 4 (ignore-errors (org-heading-components)))
  8872. "NONE")))))
  8873. (if (string-match "::\\'" cpltxt)
  8874. (setq cpltxt (substring cpltxt 0 -2)))
  8875. (setq link cpltxt))))
  8876. ((buffer-file-name (buffer-base-buffer))
  8877. ;; Just link to this file here.
  8878. (setq cpltxt (concat "file:"
  8879. (abbreviate-file-name
  8880. (buffer-file-name (buffer-base-buffer)))))
  8881. ;; Add a context string.
  8882. (when (org-xor org-context-in-file-links arg)
  8883. (setq txt (if (org-region-active-p)
  8884. (buffer-substring (region-beginning) (region-end))
  8885. (buffer-substring (point-at-bol) (point-at-eol))))
  8886. ;; Only use search option if there is some text.
  8887. (when (string-match "\\S-" txt)
  8888. (setq cpltxt
  8889. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8890. desc "NONE")))
  8891. (setq link cpltxt))
  8892. ((org-called-interactively-p 'interactive)
  8893. (user-error "No method for storing a link from this buffer"))
  8894. (t (setq link nil)))
  8895. ;; We're done setting link and desc, clean up
  8896. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8897. (setq link (or link cpltxt)
  8898. desc (or desc cpltxt))
  8899. (cond ((equal desc "NONE") (setq desc nil))
  8900. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8901. (let ((d0 (match-string 3 desc))
  8902. (p0 (match-string 5 desc)))
  8903. (setq desc
  8904. (replace-regexp-in-string
  8905. org-bracket-link-regexp
  8906. (concat (or p0 d0)
  8907. (if (equal (length (match-string 0 desc))
  8908. (length desc)) "*" "")) desc)))))
  8909. ;; Return the link
  8910. (if (not (and (or (org-called-interactively-p 'any)
  8911. executing-kbd-macro)
  8912. link))
  8913. (or agenda-link (and link (org-make-link-string link desc)))
  8914. (push (list link desc) org-stored-links)
  8915. (message "Stored: %s" (or desc link))
  8916. (when custom-id
  8917. (setq link (concat "file:" (abbreviate-file-name
  8918. (buffer-file-name)) "::#" custom-id))
  8919. (push (list link desc) org-stored-links))
  8920. (car org-stored-links))))))
  8921. (defun org-store-link-props (&rest plist)
  8922. "Store link properties, extract names and addresses."
  8923. (let (x adr)
  8924. (when (setq x (plist-get plist :from))
  8925. (setq adr (mail-extract-address-components x))
  8926. (setq plist (plist-put plist :fromname (car adr)))
  8927. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8928. (when (setq x (plist-get plist :to))
  8929. (setq adr (mail-extract-address-components x))
  8930. (setq plist (plist-put plist :toname (car adr)))
  8931. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8932. (let ((from (plist-get plist :from))
  8933. (to (plist-get plist :to)))
  8934. (when (and from to org-from-is-user-regexp)
  8935. (setq plist
  8936. (plist-put plist :fromto
  8937. (if (string-match org-from-is-user-regexp from)
  8938. (concat "to %t")
  8939. (concat "from %f"))))))
  8940. (setq org-store-link-plist plist))
  8941. (defun org-add-link-props (&rest plist)
  8942. "Add these properties to the link property list."
  8943. (let (key value)
  8944. (while plist
  8945. (setq key (pop plist) value (pop plist))
  8946. (setq org-store-link-plist
  8947. (plist-put org-store-link-plist key value)))))
  8948. (defun org-email-link-description (&optional fmt)
  8949. "Return the description part of an email link.
  8950. This takes information from `org-store-link-plist' and formats it
  8951. according to FMT (default from `org-email-link-description-format')."
  8952. (setq fmt (or fmt org-email-link-description-format))
  8953. (let* ((p org-store-link-plist)
  8954. (to (plist-get p :toaddress))
  8955. (from (plist-get p :fromaddress))
  8956. (table
  8957. (list
  8958. (cons "%c" (plist-get p :fromto))
  8959. (cons "%F" (plist-get p :from))
  8960. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8961. (cons "%T" (plist-get p :to))
  8962. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8963. (cons "%s" (plist-get p :subject))
  8964. (cons "%d" (plist-get p :date))
  8965. (cons "%m" (plist-get p :message-id)))))
  8966. (when (string-match "%c" fmt)
  8967. ;; Check if the user wrote this message
  8968. (if (and org-from-is-user-regexp from to
  8969. (save-match-data (string-match org-from-is-user-regexp from)))
  8970. (setq fmt (replace-match "to %t" t t fmt))
  8971. (setq fmt (replace-match "from %f" t t fmt))))
  8972. (org-replace-escapes fmt table)))
  8973. (defun org-make-org-heading-search-string (&optional string)
  8974. "Make search string for the current headline or STRING."
  8975. (let ((s (or string
  8976. (and (derived-mode-p 'org-mode)
  8977. (save-excursion
  8978. (org-back-to-heading t)
  8979. (org-element-property :raw-value (org-element-at-point))))))
  8980. (lines org-context-in-file-links))
  8981. (or string (setq s (concat "*" s))) ; Add * for headlines
  8982. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8983. (when (and string (integerp lines) (> lines 0))
  8984. (let ((slines (org-split-string s "\n")))
  8985. (when (< lines (length slines))
  8986. (setq s (mapconcat
  8987. 'identity
  8988. (reverse (nthcdr (- (length slines) lines)
  8989. (reverse slines))) "\n")))))
  8990. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8991. (defun org-make-link-string (link &optional description)
  8992. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8993. (unless (org-string-nw-p link) (error "Empty link"))
  8994. (let ((uri (cond ((string-match org-link-types-re link)
  8995. (concat (match-string 1 link)
  8996. (org-link-escape (substring link (match-end 1)))))
  8997. ;; For readability, url-encode internal links only
  8998. ;; when absolutely needed (i.e, when they contain
  8999. ;; square brackets). File links however, are
  9000. ;; encoded since, e.g., spaces are significant.
  9001. ((or (file-name-absolute-p link)
  9002. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9003. (org-link-escape link))
  9004. (t link)))
  9005. (description
  9006. (and (org-string-nw-p description)
  9007. ;; Remove brackets from description, as they are fatal.
  9008. (replace-regexp-in-string
  9009. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9010. (org-trim description)))))
  9011. (format "[[%s]%s]"
  9012. uri
  9013. (if description (format "[%s]" description) ""))))
  9014. (defconst org-link-escape-chars
  9015. ;;%20 %5B %5D %25
  9016. '(?\s ?\[ ?\] ?%)
  9017. "List of characters that should be escaped in a link when stored to Org.
  9018. This is the list that is used for internal purposes.")
  9019. (defconst org-link-escape-chars-browser
  9020. ;;%20 %22
  9021. '(?\s ?\")
  9022. "List of characters to be escaped before handing over to the browser.
  9023. If you consider using this constant then you probably want to use
  9024. the function `org-link-escape-browser' instead. See there why
  9025. this constant is a candidate to be removed once Org drops support
  9026. for Emacs 24.1 and 24.2.")
  9027. (defun org-link-escape (text &optional table merge)
  9028. "Return percent escaped representation of TEXT.
  9029. TEXT is a string with the text to escape.
  9030. Optional argument TABLE is a list with characters that should be
  9031. escaped. When nil, `org-link-escape-chars' is used.
  9032. If optional argument MERGE is set, merge TABLE into
  9033. `org-link-escape-chars'."
  9034. (let ((characters-to-encode
  9035. (cond ((null table) org-link-escape-chars)
  9036. (merge (append org-link-escape-chars table))
  9037. (t table))))
  9038. (mapconcat
  9039. (lambda (c)
  9040. (if (or (memq c characters-to-encode)
  9041. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9042. (mapconcat (lambda (e) (format "%%%.2X" e))
  9043. (or (encode-coding-char c 'utf-8)
  9044. (error "Unable to percent escape character: %c" c))
  9045. "")
  9046. (char-to-string c)))
  9047. text "")))
  9048. (defun org-link-escape-browser (text)
  9049. "Escape some characters before handing over to the browser.
  9050. This function is a candidate to be removed together with the
  9051. constant `org-link-escape-chars-browser' once Org drops support
  9052. for Emacs 24.1 and 24.2. All calls to this function will have to
  9053. be replaced with `url-encode-url' which is available since Emacs
  9054. 24.3.1."
  9055. ;; Example with the Org link
  9056. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9057. ;; to open the browser with +subject:"Release 8.2" filled into the
  9058. ;; query field: In this case the variable TEXT contains the
  9059. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9060. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9061. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9062. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9063. (if (fboundp 'url-encode-url)
  9064. (url-encode-url text)
  9065. (if (org-string-match-p
  9066. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9067. text)
  9068. (org-link-escape text org-link-escape-chars-browser)
  9069. text)))
  9070. (defun org-link-unescape (str)
  9071. "Unhex hexified Unicode parts in string STR.
  9072. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9073. reciprocal of `org-link-escape', which see."
  9074. (if (org-string-nw-p str)
  9075. (replace-regexp-in-string
  9076. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9077. str))
  9078. (defun org-link-unescape-compound (hex)
  9079. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9080. Note: this function also decodes single byte encodings like
  9081. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9082. (save-match-data
  9083. (let* ((bytes (cdr (split-string hex "%")))
  9084. (ret "")
  9085. (eat 0)
  9086. (sum 0))
  9087. (while bytes
  9088. (let* ((val (string-to-number (pop bytes) 16))
  9089. (shift-xor
  9090. (if (= 0 eat)
  9091. (cond
  9092. ((>= val 252) (cons 6 252))
  9093. ((>= val 248) (cons 5 248))
  9094. ((>= val 240) (cons 4 240))
  9095. ((>= val 224) (cons 3 224))
  9096. ((>= val 192) (cons 2 192))
  9097. (t (cons 0 0)))
  9098. (cons 6 128))))
  9099. (if (>= val 192) (setq eat (car shift-xor)))
  9100. (setq val (logxor val (cdr shift-xor)))
  9101. (setq sum (+ (lsh sum (car shift-xor)) val))
  9102. (if (> eat 0) (setq eat (- eat 1)))
  9103. (cond
  9104. ((= 0 eat) ;multi byte
  9105. (setq ret (concat ret (org-char-to-string sum)))
  9106. (setq sum 0))
  9107. ((not bytes) ; single byte(s)
  9108. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9109. ret)))
  9110. (defun org-link-unescape-single-byte-sequence (hex)
  9111. "Unhexify hex-encoded single byte character sequences."
  9112. (mapconcat (lambda (byte)
  9113. (char-to-string (string-to-number byte 16)))
  9114. (cdr (split-string hex "%")) ""))
  9115. (defun org-xor (a b)
  9116. "Exclusive or."
  9117. (if a (not b) b))
  9118. (defun org-fixup-message-id-for-http (s)
  9119. "Replace special characters in a message id, so it can be used in an http query."
  9120. (when (string-match "%" s)
  9121. (setq s (mapconcat (lambda (c)
  9122. (if (eq c ?%)
  9123. "%25"
  9124. (char-to-string c)))
  9125. s "")))
  9126. (while (string-match "<" s)
  9127. (setq s (replace-match "%3C" t t s)))
  9128. (while (string-match ">" s)
  9129. (setq s (replace-match "%3E" t t s)))
  9130. (while (string-match "@" s)
  9131. (setq s (replace-match "%40" t t s)))
  9132. s)
  9133. (defun org-link-prettify (link)
  9134. "Return a human-readable representation of LINK.
  9135. The car of LINK must be a raw link.
  9136. The cdr of LINK must be either a link description or nil."
  9137. (let ((desc (or (cadr link) "<no description>")))
  9138. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9139. "<" (car link) ">")))
  9140. ;;;###autoload
  9141. (defun org-insert-link-global ()
  9142. "Insert a link like Org-mode does.
  9143. This command can be called in any mode to insert a link in Org-mode syntax."
  9144. (interactive)
  9145. (org-load-modules-maybe)
  9146. (org-run-like-in-org-mode 'org-insert-link))
  9147. (defun org-insert-all-links (arg &optional pre post)
  9148. "Insert all links in `org-stored-links'.
  9149. When a universal prefix, do not delete the links from `org-stored-links'.
  9150. When `ARG' is a number, insert the last N link(s).
  9151. `PRE' and `POST' are optional arguments to define a string to
  9152. prepend or to append."
  9153. (interactive "P")
  9154. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9155. (links (copy-seq org-stored-links))
  9156. (pr (or pre "- "))
  9157. (po (or post "\n"))
  9158. (cnt 1) l)
  9159. (if (null org-stored-links)
  9160. (message "No link to insert")
  9161. (while (and (or (listp arg) (>= arg cnt))
  9162. (setq l (if (listp arg)
  9163. (pop links)
  9164. (pop org-stored-links))))
  9165. (setq cnt (1+ cnt))
  9166. (insert pr)
  9167. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9168. (insert po)))))
  9169. (defun org-insert-last-stored-link (arg)
  9170. "Insert the last link stored in `org-stored-links'."
  9171. (interactive "p")
  9172. (org-insert-all-links arg "" "\n"))
  9173. (defun org-link-fontify-links-to-this-file ()
  9174. "Fontify links to the current file in `org-stored-links'."
  9175. (let ((f (buffer-file-name)) a b)
  9176. (setq a (mapcar (lambda(l)
  9177. (let ((ll (car l)))
  9178. (when (and (string-match "^file:\\(.+\\)::" ll)
  9179. (equal f (expand-file-name (match-string 1 ll))))
  9180. ll)))
  9181. org-stored-links))
  9182. (when (featurep 'org-id)
  9183. (setq b (mapcar (lambda(l)
  9184. (let ((ll (car l)))
  9185. (when (and (string-match "^id:\\(.+\\)$" ll)
  9186. (equal f (expand-file-name
  9187. (or (org-id-find-id-file
  9188. (match-string 1 ll)) ""))))
  9189. ll)))
  9190. org-stored-links)))
  9191. (mapcar (lambda(l)
  9192. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9193. (delq nil (append a b)))))
  9194. (defvar org-link-links-in-this-file nil)
  9195. (defun org-insert-link (&optional complete-file link-location default-description)
  9196. "Insert a link. At the prompt, enter the link.
  9197. Completion can be used to insert any of the link protocol prefixes like
  9198. http or ftp in use.
  9199. The history can be used to select a link previously stored with
  9200. `org-store-link'. When the empty string is entered (i.e. if you just
  9201. press RET at the prompt), the link defaults to the most recently
  9202. stored link. As SPC triggers completion in the minibuffer, you need to
  9203. use M-SPC or C-q SPC to force the insertion of a space character.
  9204. You will also be prompted for a description, and if one is given, it will
  9205. be displayed in the buffer instead of the link.
  9206. If there is already a link at point, this command will allow you to edit link
  9207. and description parts.
  9208. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9209. be selected using completion. The path to the file will be relative to the
  9210. current directory if the file is in the current directory or a subdirectory.
  9211. Otherwise, the link will be the absolute path as completed in the minibuffer
  9212. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9213. option `org-link-file-path-type'.
  9214. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9215. the current directory or below.
  9216. With three \\[universal-argument] prefixes, negate the meaning of
  9217. `org-keep-stored-link-after-insertion'.
  9218. If `org-make-link-description-function' is non-nil, this function will be
  9219. called with the link target, and the result will be the default
  9220. link description.
  9221. If the LINK-LOCATION parameter is non-nil, this value will be
  9222. used as the link location instead of reading one interactively.
  9223. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9224. be used as the default description."
  9225. (interactive "P")
  9226. (let* ((wcf (current-window-configuration))
  9227. (origbuf (current-buffer))
  9228. (region (if (org-region-active-p)
  9229. (buffer-substring (region-beginning) (region-end))))
  9230. (remove (and region (list (region-beginning) (region-end))))
  9231. (desc region)
  9232. tmphist ; byte-compile incorrectly complains about this
  9233. (link link-location)
  9234. (abbrevs org-link-abbrev-alist-local)
  9235. entry file all-prefixes auto-desc)
  9236. (cond
  9237. (link-location) ; specified by arg, just use it.
  9238. ((org-in-regexp org-bracket-link-regexp 1)
  9239. ;; We do have a link at point, and we are going to edit it.
  9240. (setq remove (list (match-beginning 0) (match-end 0)))
  9241. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9242. (setq link (read-string "Link: "
  9243. (org-link-unescape
  9244. (org-match-string-no-properties 1)))))
  9245. ((or (org-in-regexp org-angle-link-re)
  9246. (org-in-regexp org-plain-link-re))
  9247. ;; Convert to bracket link
  9248. (setq remove (list (match-beginning 0) (match-end 0))
  9249. link (read-string "Link: "
  9250. (org-remove-angle-brackets (match-string 0)))))
  9251. ((member complete-file '((4) (16)))
  9252. ;; Completing read for file names.
  9253. (setq link (org-file-complete-link complete-file)))
  9254. (t
  9255. ;; Read link, with completion for stored links.
  9256. (org-link-fontify-links-to-this-file)
  9257. (org-switch-to-buffer-other-window "*Org Links*")
  9258. (with-current-buffer "*Org Links*"
  9259. (erase-buffer)
  9260. (insert "Insert a link.
  9261. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9262. (when org-stored-links
  9263. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9264. (insert (mapconcat 'org-link-prettify
  9265. (reverse org-stored-links) "\n")))
  9266. (goto-char (point-min)))
  9267. (let ((cw (selected-window)))
  9268. (select-window (get-buffer-window "*Org Links*" 'visible))
  9269. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9270. (unless (pos-visible-in-window-p (point-max))
  9271. (org-fit-window-to-buffer))
  9272. (and (window-live-p cw) (select-window cw)))
  9273. ;; Fake a link history, containing the stored links.
  9274. (setq tmphist (append (mapcar 'car org-stored-links)
  9275. org-insert-link-history))
  9276. (setq all-prefixes (append (mapcar 'car abbrevs)
  9277. (mapcar 'car org-link-abbrev-alist)
  9278. org-link-types))
  9279. (unwind-protect
  9280. (progn
  9281. (setq link
  9282. (org-completing-read
  9283. "Link: "
  9284. (append
  9285. (mapcar (lambda (x) (concat x ":"))
  9286. all-prefixes)
  9287. (mapcar 'car org-stored-links))
  9288. nil nil nil
  9289. 'tmphist
  9290. (caar org-stored-links)))
  9291. (if (not (string-match "\\S-" link))
  9292. (user-error "No link selected"))
  9293. (mapc (lambda(l)
  9294. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9295. org-stored-links)
  9296. (if (or (member link all-prefixes)
  9297. (and (equal ":" (substring link -1))
  9298. (member (substring link 0 -1) all-prefixes)
  9299. (setq link (substring link 0 -1))))
  9300. (setq link (with-current-buffer origbuf
  9301. (org-link-try-special-completion link)))))
  9302. (set-window-configuration wcf)
  9303. (kill-buffer "*Org Links*"))
  9304. (setq entry (assoc link org-stored-links))
  9305. (or entry (push link org-insert-link-history))
  9306. (setq desc (or desc (nth 1 entry)))))
  9307. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9308. (not org-keep-stored-link-after-insertion))
  9309. (setq org-stored-links (delq (assoc link org-stored-links)
  9310. org-stored-links)))
  9311. (if (and (string-match org-plain-link-re link)
  9312. (not (string-match org-ts-regexp link)))
  9313. ;; URL-like link, normalize the use of angular brackets.
  9314. (setq link (org-remove-angle-brackets link)))
  9315. ;; Check if we are linking to the current file with a search
  9316. ;; option If yes, simplify the link by using only the search
  9317. ;; option.
  9318. (when (and buffer-file-name
  9319. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9320. (let* ((path (match-string 1 link))
  9321. (case-fold-search nil)
  9322. (search (match-string 2 link)))
  9323. (save-match-data
  9324. (if (equal (file-truename buffer-file-name) (file-truename path))
  9325. ;; We are linking to this same file, with a search option
  9326. (setq link search)))))
  9327. ;; Check if we can/should use a relative path. If yes, simplify the link
  9328. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9329. (let* ((type (match-string 1 link))
  9330. (path (match-string 2 link))
  9331. (origpath path)
  9332. (case-fold-search nil))
  9333. (cond
  9334. ((or (eq org-link-file-path-type 'absolute)
  9335. (equal complete-file '(16)))
  9336. (setq path (abbreviate-file-name (expand-file-name path))))
  9337. ((eq org-link-file-path-type 'noabbrev)
  9338. (setq path (expand-file-name path)))
  9339. ((eq org-link-file-path-type 'relative)
  9340. (setq path (file-relative-name path)))
  9341. (t
  9342. (save-match-data
  9343. (if (string-match (concat "^" (regexp-quote
  9344. (expand-file-name
  9345. (file-name-as-directory
  9346. default-directory))))
  9347. (expand-file-name path))
  9348. ;; We are linking a file with relative path name.
  9349. (setq path (substring (expand-file-name path)
  9350. (match-end 0)))
  9351. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9352. (setq link (concat type path))
  9353. (if (equal desc origpath)
  9354. (setq desc path))))
  9355. (if org-make-link-description-function
  9356. (setq desc
  9357. (or (condition-case nil
  9358. (funcall org-make-link-description-function link desc)
  9359. (error (progn (message "Can't get link description from `%s'"
  9360. (symbol-name org-make-link-description-function))
  9361. (sit-for 2) nil)))
  9362. (read-string "Description: " default-description)))
  9363. (if default-description (setq desc default-description)
  9364. (setq desc (or (and auto-desc desc)
  9365. (read-string "Description: " desc)))))
  9366. (unless (string-match "\\S-" desc) (setq desc nil))
  9367. (if remove (apply 'delete-region remove))
  9368. (insert (org-make-link-string link desc))))
  9369. (defun org-link-try-special-completion (type)
  9370. "If there is completion support for link type TYPE, offer it."
  9371. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9372. (if (functionp fun)
  9373. (funcall fun)
  9374. (read-string "Link (no completion support): " (concat type ":")))))
  9375. (defun org-file-complete-link (&optional arg)
  9376. "Create a file link using completion."
  9377. (let ((file (org-iread-file-name "File: "))
  9378. (pwd (file-name-as-directory (expand-file-name ".")))
  9379. (pwd1 (file-name-as-directory (abbreviate-file-name
  9380. (expand-file-name ".")))))
  9381. (cond ((equal arg '(16))
  9382. (concat "file:"
  9383. (abbreviate-file-name (expand-file-name file))))
  9384. ((string-match
  9385. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9386. (concat "file:" (match-string 1 file)))
  9387. ((string-match
  9388. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9389. (expand-file-name file))
  9390. (concat "file:"
  9391. (match-string 1 (expand-file-name file))))
  9392. (t (concat "file:" file)))))
  9393. (defun org-iread-file-name (&rest args)
  9394. "Read-file-name using `ido-mode' speedup if available.
  9395. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9396. See `read-file-name' for a description of parameters."
  9397. (org-without-partial-completion
  9398. (if (and org-completion-use-ido
  9399. (fboundp 'ido-read-file-name)
  9400. (org-bound-and-true-p ido-mode)
  9401. (listp (nth 1 args)))
  9402. (let ((ido-enter-matching-directory nil))
  9403. (apply #'ido-read-file-name args))
  9404. (apply #'read-file-name args))))
  9405. (defun org-completing-read (&rest args)
  9406. "Completing-read with SPACE being a normal character."
  9407. (let ((enable-recursive-minibuffers t)
  9408. (minibuffer-local-completion-map
  9409. (copy-keymap minibuffer-local-completion-map)))
  9410. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9411. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9412. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9413. (apply 'org-icompleting-read args)))
  9414. (defun org-completing-read-no-i (&rest args)
  9415. (let (org-completion-use-ido org-completion-use-iswitchb)
  9416. (apply 'org-completing-read args)))
  9417. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9418. "Use iswitch as a completing-read replacement to choose from choices.
  9419. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9420. from."
  9421. (let* ((iswitchb-use-virtual-buffers nil)
  9422. (iswitchb-make-buflist-hook
  9423. (lambda ()
  9424. (setq iswitchb-temp-buflist choices))))
  9425. (iswitchb-read-buffer prompt)))
  9426. (defun org-icompleting-read (&rest args)
  9427. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9428. Should be called like `completing-read'."
  9429. (org-without-partial-completion
  9430. (if (not (listp (nth 1 args)))
  9431. ;; Ido only supports lists as the COLLECTION argument. Use
  9432. ;; default completion function when second argument is not
  9433. ;; a list.
  9434. (apply #'completing-read args)
  9435. (let ((ido-enter-matching-directory nil))
  9436. (apply (cond ((and org-completion-use-ido
  9437. (fboundp 'ido-completing-read)
  9438. (org-bound-and-true-p ido-mode))
  9439. #'ido-completing-read)
  9440. ((and org-completion-use-iswitchb
  9441. (org-bound-and-true-p iswitchb-mode))
  9442. #'org-iswitchb-completing-read)
  9443. (t #'completing-read))
  9444. args)))))
  9445. ;;; Opening/following a link
  9446. (defvar org-link-search-failed nil)
  9447. (defvar org-open-link-functions nil
  9448. "Hook for functions finding a plain text link.
  9449. These functions must take a single argument, the link content.
  9450. They will be called for links that look like [[link text][description]]
  9451. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9452. like a filename (e.g. \"./blue.png\").
  9453. These functions will be called *before* Org attempts to resolve the
  9454. link by doing text searches in the current buffer - so if you want a
  9455. link \"[[target]]\" to still find \"<<target>>\", your function should
  9456. handle this as a special case.
  9457. When the function does handle the link, it must return a non-nil value.
  9458. If it decides that it is not responsible for this link, it must return
  9459. nil to indicate that that Org-mode can continue with other options
  9460. like exact and fuzzy text search.")
  9461. (defun org-next-link (&optional search-backward)
  9462. "Move forward to the next link.
  9463. If the link is in hidden text, expose it."
  9464. (interactive "P")
  9465. (when (and org-link-search-failed (eq this-command last-command))
  9466. (goto-char (point-min))
  9467. (message "Link search wrapped back to beginning of buffer"))
  9468. (setq org-link-search-failed nil)
  9469. (let* ((pos (point))
  9470. (ct (org-context))
  9471. (a (assoc :link ct))
  9472. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9473. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9474. ((looking-at org-any-link-re)
  9475. ;; Don't stay stuck at link without an org-link face
  9476. (forward-char (if search-backward -1 1))))
  9477. (if (funcall srch-fun org-any-link-re nil t)
  9478. (progn
  9479. (goto-char (match-beginning 0))
  9480. (if (outline-invisible-p) (org-show-context)))
  9481. (goto-char pos)
  9482. (setq org-link-search-failed t)
  9483. (message "No further link found"))))
  9484. (defun org-previous-link ()
  9485. "Move backward to the previous link.
  9486. If the link is in hidden text, expose it."
  9487. (interactive)
  9488. (funcall 'org-next-link t))
  9489. (defun org-translate-link (s)
  9490. "Translate a link string if a translation function has been defined."
  9491. (if (and org-link-translation-function
  9492. (fboundp org-link-translation-function)
  9493. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9494. (progn
  9495. (setq s (funcall org-link-translation-function
  9496. (match-string 1 s) (match-string 2 s)))
  9497. (concat (car s) ":" (cdr s)))
  9498. s))
  9499. (defun org-translate-link-from-planner (type path)
  9500. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9501. This is still an experimental function, your mileage may vary."
  9502. (cond
  9503. ((member type '("http" "https" "news" "ftp"))
  9504. ;; standard Internet links are the same.
  9505. nil)
  9506. ((and (equal type "irc") (string-match "^//" path))
  9507. ;; Planner has two / at the beginning of an irc link, we have 1.
  9508. ;; We should have zero, actually....
  9509. (setq path (substring path 1)))
  9510. ((and (equal type "lisp") (string-match "^/" path))
  9511. ;; Planner has a slash, we do not.
  9512. (setq type "elisp" path (substring path 1)))
  9513. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9514. ;; A typical message link. Planner has the id after the final slash,
  9515. ;; we separate it with a hash mark
  9516. (setq path (concat (match-string 1 path) "#"
  9517. (org-remove-angle-brackets (match-string 2 path))))))
  9518. (cons type path))
  9519. (defun org-find-file-at-mouse (ev)
  9520. "Open file link or URL at mouse."
  9521. (interactive "e")
  9522. (mouse-set-point ev)
  9523. (org-open-at-point 'in-emacs))
  9524. (defun org-open-at-mouse (ev)
  9525. "Open file link or URL at mouse.
  9526. See the docstring of `org-open-file' for details."
  9527. (interactive "e")
  9528. (mouse-set-point ev)
  9529. (if (eq major-mode 'org-agenda-mode)
  9530. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9531. (org-open-at-point))
  9532. (defvar org-window-config-before-follow-link nil
  9533. "The window configuration before following a link.
  9534. This is saved in case the need arises to restore it.")
  9535. ;;;###autoload
  9536. (defun org-open-at-point-global ()
  9537. "Follow a link like Org-mode does.
  9538. This command can be called in any mode to follow a link that has
  9539. Org-mode syntax."
  9540. (interactive)
  9541. (org-run-like-in-org-mode 'org-open-at-point))
  9542. ;;;###autoload
  9543. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9544. "Open a link in the string S, as if it was in Org-mode."
  9545. (interactive "sLink: \nP")
  9546. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9547. (with-temp-buffer
  9548. (let ((org-inhibit-startup (not reference-buffer)))
  9549. (org-mode)
  9550. (insert s)
  9551. (goto-char (point-min))
  9552. (when reference-buffer
  9553. (setq org-link-abbrev-alist-local
  9554. (with-current-buffer reference-buffer
  9555. org-link-abbrev-alist-local)))
  9556. (org-open-at-point arg reference-buffer)))))
  9557. (defvar org-open-at-point-functions nil
  9558. "Hook that is run when following a link at point.
  9559. Functions in this hook must return t if they identify and follow
  9560. a link at point. If they don't find anything interesting at point,
  9561. they must return nil.")
  9562. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9563. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9564. (defun org-open-at-point (&optional arg reference-buffer)
  9565. "Open link, timestamp, footnote or tags at point.
  9566. When point is on a link, follow it. Normally, files will be
  9567. opened by an appropriate application. If the optional prefix
  9568. argument ARG is non-nil, Emacs will visit the file. With
  9569. a double prefix argument, try to open outside of Emacs, in the
  9570. application the system uses for this file type.
  9571. When point is on a timestamp, open the agenda at the day
  9572. specified.
  9573. When point is a footnote definition, move to the first reference
  9574. found. If it is on a reference, move to the associated
  9575. definition.
  9576. When point is on a headline, display a list of every link in the
  9577. entry, so it is possible to pick one, or all, of them. If point
  9578. is on a tag, call `org-tags-view' instead.
  9579. When optional argument REFERENCE-BUFFER is non-nil, it should
  9580. specify a buffer from where the link search should happen. This
  9581. is used internally by `org-open-link-from-string'.
  9582. On top of syntactically correct links, this function will open
  9583. the link at point in comments or comment blocks and the first
  9584. link in a property drawer line."
  9585. (interactive "P")
  9586. ;; On a code block, open block's results.
  9587. (unless (call-interactively 'org-babel-open-src-block-result)
  9588. (org-load-modules-maybe)
  9589. (setq org-window-config-before-follow-link (current-window-configuration))
  9590. (org-remove-occur-highlights nil nil t)
  9591. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9592. (let* ((context
  9593. ;; Only consider supported types, even if they are not
  9594. ;; the closest one.
  9595. (org-element-lineage
  9596. (org-element-context)
  9597. '(clock comment comment-block footnote-definition
  9598. footnote-reference headline inlinetask keyword link
  9599. node-property timestamp)
  9600. t))
  9601. (type (org-element-type context))
  9602. (value (org-element-property :value context)))
  9603. (cond
  9604. ((not context) (user-error "No link found"))
  9605. ;; Exception: open timestamps and links in properties
  9606. ;; drawers, keywords and comments.
  9607. ((memq type '(comment comment-block keyword node-property))
  9608. (cond ((org-in-regexp org-any-link-re)
  9609. (org-open-link-from-string (match-string-no-properties 0)))
  9610. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9611. (org-follow-timestamp-link))
  9612. (t (user-error "No link found"))))
  9613. ;; On a headline or an inlinetask, but not on a timestamp,
  9614. ;; a link, a footnote reference or on tags.
  9615. ((and (memq type '(headline inlinetask))
  9616. ;; Not on tags.
  9617. (progn (save-excursion (beginning-of-line)
  9618. (looking-at org-complex-heading-regexp))
  9619. (or (not (match-beginning 5))
  9620. (< (point) (match-beginning 5)))))
  9621. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9622. (links (car data))
  9623. (links-end (cdr data)))
  9624. (if links
  9625. (dolist (link (if (stringp links) (list links) links))
  9626. (search-forward link nil links-end)
  9627. (goto-char (match-beginning 0))
  9628. (org-open-at-point))
  9629. (require 'org-attach)
  9630. (org-attach-reveal 'if-exists))))
  9631. ;; On a clock line, make sure point is on the timestamp
  9632. ;; before opening it.
  9633. ((and (eq type 'clock)
  9634. value
  9635. (>= (point) (org-element-property :begin value))
  9636. (<= (point) (org-element-property :end value)))
  9637. (org-follow-timestamp-link))
  9638. ;; Do nothing on white spaces after an object, unless point
  9639. ;; is right after it.
  9640. ((> (point)
  9641. (save-excursion
  9642. (goto-char (org-element-property :end context))
  9643. (skip-chars-backward " \t")
  9644. (point)))
  9645. (user-error "No link found"))
  9646. ((eq type 'timestamp) (org-follow-timestamp-link))
  9647. ;; On tags within a headline or an inlinetask.
  9648. ((and (memq type '(headline inlinetask))
  9649. (progn (save-excursion (beginning-of-line)
  9650. (looking-at org-complex-heading-regexp))
  9651. (and (match-beginning 5)
  9652. (>= (point) (match-beginning 5)))))
  9653. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9654. ((eq type 'link)
  9655. ;; When link is located within the description of another
  9656. ;; link (e.g., an inline image), always open the parent
  9657. ;; link.
  9658. (let*((link (let ((up (org-element-property :parent context)))
  9659. (if (eq (org-element-type up) 'link) up context)))
  9660. (type (org-element-property :type link))
  9661. (path (org-link-unescape (org-element-property :path link))))
  9662. ;; Switch back to REFERENCE-BUFFER needed when called in
  9663. ;; a temporary buffer through `org-open-link-from-string'.
  9664. (with-current-buffer (or reference-buffer (current-buffer))
  9665. (cond
  9666. ((equal type "file")
  9667. (if (string-match "[*?{]" (file-name-nondirectory path))
  9668. (dired path)
  9669. ;; Look into `org-link-protocols' in order to find
  9670. ;; a DEDICATED-FUNCTION to open file. The function
  9671. ;; will be applied on raw link instead of parsed
  9672. ;; link due to the limitation in `org-add-link-type'
  9673. ;; ("open" function called with a single argument).
  9674. ;; If no such function is found, fallback to
  9675. ;; `org-open-file'.
  9676. ;;
  9677. ;; Note : "file+emacs" and "file+sys" types are
  9678. ;; hard-coded in order to escape the previous
  9679. ;; limitation.
  9680. (let* ((option (org-element-property :search-option link))
  9681. (app (org-element-property :application link))
  9682. (dedicated-function
  9683. (nth 1 (assoc app org-link-protocols))))
  9684. (if dedicated-function
  9685. (funcall dedicated-function
  9686. (concat path
  9687. (and option (concat "::" option))))
  9688. (apply #'org-open-file
  9689. path
  9690. (cond (arg)
  9691. ((equal app "emacs") 'emacs)
  9692. ((equal app "sys") 'system))
  9693. (cond ((not option) nil)
  9694. ((org-string-match-p "\\`[0-9]+\\'" option)
  9695. (list (string-to-number option)))
  9696. (t (list nil
  9697. (org-link-unescape option)))))))))
  9698. ((assoc type org-link-protocols)
  9699. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9700. ((equal type "help")
  9701. (let ((f-or-v (intern path)))
  9702. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9703. ((boundp f-or-v) (describe-variable f-or-v))
  9704. (t (error "Not a known function or variable")))))
  9705. ((member type '("http" "https" "ftp" "mailto" "news"))
  9706. (browse-url (org-link-escape-browser (concat type ":" path))))
  9707. ((equal type "doi")
  9708. (browse-url
  9709. (org-link-escape-browser (concat org-doi-server-url path))))
  9710. ((equal type "message") (browse-url (concat type ":" path)))
  9711. ((equal type "shell")
  9712. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9713. (cmd path))
  9714. (if (or (and (org-string-nw-p
  9715. org-confirm-shell-link-not-regexp)
  9716. (string-match
  9717. org-confirm-shell-link-not-regexp cmd))
  9718. (not org-confirm-shell-link-function)
  9719. (funcall org-confirm-shell-link-function
  9720. (format "Execute \"%s\" in shell? "
  9721. (org-add-props cmd nil
  9722. 'face 'org-warning))))
  9723. (progn
  9724. (message "Executing %s" cmd)
  9725. (shell-command cmd buf)
  9726. (when (featurep 'midnight)
  9727. (setq clean-buffer-list-kill-buffer-names
  9728. (cons (buffer-name buf)
  9729. clean-buffer-list-kill-buffer-names))))
  9730. (user-error "Abort"))))
  9731. ((equal type "elisp")
  9732. (let ((cmd path))
  9733. (if (or (and (org-string-nw-p
  9734. org-confirm-elisp-link-not-regexp)
  9735. (org-string-match-p
  9736. org-confirm-elisp-link-not-regexp cmd))
  9737. (not org-confirm-elisp-link-function)
  9738. (funcall org-confirm-elisp-link-function
  9739. (format "Execute \"%s\" as elisp? "
  9740. (org-add-props cmd nil
  9741. 'face 'org-warning))))
  9742. (message "%s => %s" cmd
  9743. (if (eq (string-to-char cmd) ?\()
  9744. (eval (read cmd))
  9745. (call-interactively (read cmd))))
  9746. (user-error "Abort"))))
  9747. ((equal type "id")
  9748. (require 'ord-id)
  9749. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9750. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9751. (unless (run-hook-with-args-until-success
  9752. 'org-open-link-functions path)
  9753. (if (not arg) (org-mark-ring-push)
  9754. (switch-to-buffer-other-window
  9755. (org-get-buffer-for-internal-link (current-buffer))))
  9756. (let ((destination
  9757. (org-with-wide-buffer
  9758. (if (equal type "radio")
  9759. (org-search-radio-target
  9760. (org-element-property :path link))
  9761. (org-link-search
  9762. (if (member type '("custom-id" "coderef"))
  9763. (org-element-property :raw-link link)
  9764. path)
  9765. ;; Prevent fuzzy links from matching
  9766. ;; themselves.
  9767. (and (equal type "fuzzy")
  9768. (+ 2 (org-element-property :begin link)))))
  9769. (point))))
  9770. (unless (and (<= (point-min) destination)
  9771. (>= (point-max) destination))
  9772. (widen))
  9773. (goto-char destination))))
  9774. (t (browse-url-at-point))))))
  9775. ;; On a footnote reference or at a footnote definition's label.
  9776. ((or (eq type 'footnote-reference)
  9777. (and (eq type 'footnote-definition)
  9778. (save-excursion
  9779. ;; Do not validate action when point is on the
  9780. ;; spaces right after the footnote label, in
  9781. ;; order to be on par with behaviour on links.
  9782. (skip-chars-forward " \t")
  9783. (let ((begin
  9784. (org-element-property :contents-begin context)))
  9785. (if begin (< (point) begin)
  9786. (= (org-element-property :post-affiliated context)
  9787. (line-beginning-position)))))))
  9788. (org-footnote-action))
  9789. (t (user-error "No link found")))))
  9790. (run-hook-with-args 'org-follow-link-hook)))
  9791. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9792. "Offer links in the current entry and return the selected link.
  9793. If there is only one link, return it.
  9794. If NTH is an integer, return the NTH link found.
  9795. If ZERO is a string, check also this string for a link, and if
  9796. there is one, return it."
  9797. (with-current-buffer buffer
  9798. (save-excursion
  9799. (save-restriction
  9800. (widen)
  9801. (goto-char marker)
  9802. (let ((cnt ?0)
  9803. (in-emacs (if (integerp nth) nil nth))
  9804. have-zero end links link c)
  9805. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9806. (push (match-string 0 zero) links)
  9807. (setq cnt (1- cnt) have-zero t))
  9808. (save-excursion
  9809. (org-back-to-heading t)
  9810. (setq end (save-excursion (outline-next-heading) (point)))
  9811. (while (re-search-forward org-any-link-re end t)
  9812. (push (match-string 0) links))
  9813. (setq links (org-uniquify (reverse links))))
  9814. (cond
  9815. ((null links)
  9816. (message "No links"))
  9817. ((equal (length links) 1)
  9818. (setq link (car links)))
  9819. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9820. (setq link (nth (if have-zero nth (1- nth)) links)))
  9821. (t ; we have to select a link
  9822. (save-excursion
  9823. (save-window-excursion
  9824. (delete-other-windows)
  9825. (with-output-to-temp-buffer "*Select Link*"
  9826. (mapc (lambda (l)
  9827. (if (not (string-match org-bracket-link-regexp l))
  9828. (princ (format "[%c] %s\n" (incf cnt)
  9829. (org-remove-angle-brackets l)))
  9830. (if (match-end 3)
  9831. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9832. (match-string 3 l) (match-string 1 l)))
  9833. (princ (format "[%c] %s\n" (incf cnt)
  9834. (match-string 1 l))))))
  9835. links))
  9836. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9837. (message "Select link to open, RET to open all:")
  9838. (setq c (read-char-exclusive))
  9839. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9840. (when (equal c ?q) (user-error "Abort"))
  9841. (if (equal c ?\C-m)
  9842. (setq link links)
  9843. (setq nth (- c ?0))
  9844. (if have-zero (setq nth (1+ nth)))
  9845. (unless (and (integerp nth) (>= (length links) nth))
  9846. (user-error "Invalid link selection"))
  9847. (setq link (nth (1- nth) links)))))
  9848. (cons link end))))))
  9849. ;; TODO: These functions are deprecated since `org-open-at-point'
  9850. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9851. (defun org-open-file-with-system (path)
  9852. "Open file at PATH using the system way of opening it."
  9853. (org-open-file path 'system))
  9854. (defun org-open-file-with-emacs (path)
  9855. "Open file at PATH in Emacs."
  9856. (org-open-file path 'emacs))
  9857. ;;; File search
  9858. (defvar org-create-file-search-functions nil
  9859. "List of functions to construct the right search string for a file link.
  9860. These functions are called in turn with point at the location to
  9861. which the link should point.
  9862. A function in the hook should first test if it would like to
  9863. handle this file type, for example by checking the `major-mode'
  9864. or the file extension. If it decides not to handle this file, it
  9865. should just return nil to give other functions a chance. If it
  9866. does handle the file, it must return the search string to be used
  9867. when following the link. The search string will be part of the
  9868. file link, given after a double colon, and `org-open-at-point'
  9869. will automatically search for it. If special measures must be
  9870. taken to make the search successful, another function should be
  9871. added to the companion hook `org-execute-file-search-functions',
  9872. which see.
  9873. A function in this hook may also use `setq' to set the variable
  9874. `description' to provide a suggestion for the descriptive text to
  9875. be used for this link when it gets inserted into an Org-mode
  9876. buffer with \\[org-insert-link].")
  9877. (defvar org-execute-file-search-functions nil
  9878. "List of functions to execute a file search triggered by a link.
  9879. Functions added to this hook must accept a single argument, the
  9880. search string that was part of the file link, the part after the
  9881. double colon. The function must first check if it would like to
  9882. handle this search, for example by checking the `major-mode' or
  9883. the file extension. If it decides not to handle this search, it
  9884. should just return nil to give other functions a chance. If it
  9885. does handle the search, it must return a non-nil value to keep
  9886. other functions from trying.
  9887. Each function can access the current prefix argument through the
  9888. variable `current-prefix-arg'. Note that a single prefix is used
  9889. to force opening a link in Emacs, so it may be good to only use a
  9890. numeric or double prefix to guide the search function.
  9891. In case this is needed, a function in this hook can also restore
  9892. the window configuration before `org-open-at-point' was called using:
  9893. (set-window-configuration org-window-config-before-follow-link)")
  9894. (defun org-search-radio-target (target)
  9895. "Search a radio target matching TARGET in current buffer.
  9896. White spaces are not significant."
  9897. (let ((re (format "<<<%s>>>"
  9898. (mapconcat #'regexp-quote
  9899. (org-split-string target "[ \t\n]+")
  9900. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9901. (origin (point)))
  9902. (goto-char (point-min))
  9903. (catch :radio-match
  9904. (while (re-search-forward re nil t)
  9905. (backward-char)
  9906. (let ((object (org-element-context)))
  9907. (when (eq (org-element-type object) 'radio-target)
  9908. (goto-char (org-element-property :begin object))
  9909. (org-show-context 'link-search)
  9910. (throw :radio-match nil))))
  9911. (goto-char origin)
  9912. (user-error "No match for radio target: %s" target))))
  9913. (defun org-link-search (s &optional avoid-pos stealth)
  9914. "Search for a search string S.
  9915. If S starts with \"#\", it triggers a custom ID search.
  9916. If S is enclosed within parenthesis, it initiates a coderef
  9917. search.
  9918. If S is surrounded by forward slashes, it is interpreted as
  9919. a regular expression. In Org mode files, this will create an
  9920. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9921. to list matches. If the current buffer is in `dired-mode', grep
  9922. will be used to search in all files.
  9923. When AVOID-POS is given, ignore matches near that position.
  9924. When optional argument STEALTH is non-nil, do not modify
  9925. visibility around point, thus ignoring `org-show-context-detail'
  9926. variable.
  9927. Search is case-insensitive and ignores white spaces. Return type
  9928. of matched result, with is either `dedicated' or `fuzzy'."
  9929. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9930. (let* ((case-fold-search t)
  9931. (origin (point))
  9932. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9933. (words (org-split-string s "[ \t\n]+"))
  9934. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9935. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9936. type)
  9937. (cond
  9938. ;; Check if there are any special search functions.
  9939. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9940. ((eq (string-to-char s) ?#)
  9941. ;; Look for a custom ID S if S starts with "#".
  9942. (let* ((id (substring normalized 1))
  9943. (match (org-find-property "CUSTOM_ID" id)))
  9944. (if match (progn (goto-char match) (setf type 'dedicated))
  9945. (error "No match for custom ID: %s" id))))
  9946. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9947. ;; Look for coderef targets if S is enclosed within parenthesis.
  9948. (let ((coderef (match-string-no-properties 1 normalized))
  9949. (re (substring s-single-re 1 -1)))
  9950. (goto-char (point-min))
  9951. (catch :coderef-match
  9952. (while (re-search-forward re nil t)
  9953. (let ((element (org-element-at-point)))
  9954. (when (and (memq (org-element-type element)
  9955. '(example-block src-block))
  9956. ;; Build proper regexp according to current
  9957. ;; block's label format.
  9958. (let ((label-fmt
  9959. (regexp-quote
  9960. (or (org-element-property :label-fmt element)
  9961. org-coderef-label-format))))
  9962. (save-excursion
  9963. (beginning-of-line)
  9964. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9965. (format label-fmt coderef))))))
  9966. (setq type 'dedicated)
  9967. (goto-char (match-beginning 1))
  9968. (throw :coderef-match nil))))
  9969. (goto-char origin)
  9970. (error "No match for coderef: %s" coderef))))
  9971. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9972. ;; Look for a regular expression.
  9973. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9974. (match-string 1 s)))
  9975. ;; Fuzzy links.
  9976. (t
  9977. (let* ((starred (eq (string-to-char normalized) ?*))
  9978. (headline-search (and (derived-mode-p 'org-mode)
  9979. (or org-link-search-must-match-exact-headline
  9980. starred))))
  9981. (cond
  9982. ;; Look for targets, only if not in a headline search.
  9983. ((and (not starred)
  9984. (let ((target (format "<<%s>>" s-multi-re)))
  9985. (catch :target-match
  9986. (goto-char (point-min))
  9987. (while (re-search-forward target nil t)
  9988. (backward-char)
  9989. (let ((context (org-element-context)))
  9990. (when (eq (org-element-type context) 'target)
  9991. (setq type 'dedicated)
  9992. (goto-char (org-element-property :begin context))
  9993. (throw :target-match t))))
  9994. nil))))
  9995. ;; Look for elements named after S, only if not in a headline
  9996. ;; search.
  9997. ((and (not starred)
  9998. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9999. (catch :name-match
  10000. (goto-char (point-min))
  10001. (while (re-search-forward name nil t)
  10002. (let ((element (org-element-at-point)))
  10003. (when (equal (org-split-string
  10004. (org-element-property :name element)
  10005. "[ \t]+")
  10006. words)
  10007. (setq type 'dedicated)
  10008. (beginning-of-line)
  10009. (throw :name-match t))))
  10010. nil))))
  10011. ;; Regular text search. Prefer headlines in Org mode
  10012. ;; buffers.
  10013. ((and (derived-mode-p 'org-mode)
  10014. (let* ((wspace "[ \t]")
  10015. (wspaceopt (concat wspace "*"))
  10016. (cookie (concat "\\(?:"
  10017. wspaceopt
  10018. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10019. wspaceopt
  10020. "\\)"))
  10021. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10022. (re (concat
  10023. org-outline-regexp-bol
  10024. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10025. "\\(?:\\[#.\\][ \t]+\\)?"
  10026. "\\(?:" org-comment-string "[ \t]+\\)?"
  10027. sep "?"
  10028. (let ((title (mapconcat #'regexp-quote
  10029. words
  10030. sep)))
  10031. (if starred (substring title 1) title))
  10032. sep "?"
  10033. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10034. "[ \t]*$")))
  10035. (goto-char (point-min))
  10036. (re-search-forward re nil t)))
  10037. (goto-char (match-beginning 0))
  10038. (setq type 'dedicated))
  10039. ;; Offer to create non-existent headline depending on
  10040. ;; `org-link-search-must-match-exact-headline'.
  10041. ((and (derived-mode-p 'org-mode)
  10042. (not org-link-search-inhibit-query)
  10043. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10044. (yes-or-no-p "No match - create this as a new heading? "))
  10045. (goto-char (point-max))
  10046. (unless (bolp) (newline))
  10047. (org-insert-heading nil t t)
  10048. (insert s "\n")
  10049. (beginning-of-line 0))
  10050. ;; Only headlines are looked after. No need to process
  10051. ;; further: throw an error.
  10052. ((and (derived-mode-p 'org-mode)
  10053. (or starred org-link-search-must-match-exact-headline))
  10054. (goto-char origin)
  10055. (error "No match for fuzzy expression: %s" normalized))
  10056. ;; Regular text search.
  10057. ((catch :fuzzy-match
  10058. (goto-char (point-min))
  10059. (while (re-search-forward s-multi-re nil t)
  10060. ;; Skip match if it contains AVOID-POS or it is included
  10061. ;; in a link with a description but outside the
  10062. ;; description.
  10063. (unless (or (and avoid-pos
  10064. (<= (match-beginning 0) avoid-pos)
  10065. (> (match-end 0) avoid-pos))
  10066. (and (save-match-data
  10067. (org-in-regexp org-bracket-link-regexp))
  10068. (match-beginning 3)
  10069. (or (> (match-beginning 3) (point))
  10070. (<= (match-end 3) (point)))
  10071. (org-element-lineage
  10072. (save-match-data (org-element-context))
  10073. '(link) t)))
  10074. (goto-char (match-beginning 0))
  10075. (setq type 'fuzzy)
  10076. (throw :fuzzy-match t)))
  10077. nil))
  10078. ;; All failed. Throw an error.
  10079. (t (goto-char origin)
  10080. (error "No match for fuzzy expression: %s" normalized))))))
  10081. ;; Disclose surroundings of match, if appropriate.
  10082. (when (and (derived-mode-p 'org-mode) (not stealth))
  10083. (org-show-context 'link-search))
  10084. type))
  10085. (defun org-get-buffer-for-internal-link (buffer)
  10086. "Return a buffer to be used for displaying the link target of internal links."
  10087. (cond
  10088. ((not org-display-internal-link-with-indirect-buffer)
  10089. buffer)
  10090. ((string-match "(Clone)$" (buffer-name buffer))
  10091. (message "Buffer is already a clone, not making another one")
  10092. ;; we also do not modify visibility in this case
  10093. buffer)
  10094. (t ; make a new indirect buffer for displaying the link
  10095. (let* ((bn (buffer-name buffer))
  10096. (ibn (concat bn "(Clone)"))
  10097. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10098. (with-current-buffer ib (org-overview))
  10099. ib))))
  10100. (defun org-do-occur (regexp &optional cleanup)
  10101. "Call the Emacs command `occur'.
  10102. If CLEANUP is non-nil, remove the printout of the regular expression
  10103. in the *Occur* buffer. This is useful if the regex is long and not useful
  10104. to read."
  10105. (occur regexp)
  10106. (when cleanup
  10107. (let ((cwin (selected-window)) win beg end)
  10108. (when (setq win (get-buffer-window "*Occur*"))
  10109. (select-window win))
  10110. (goto-char (point-min))
  10111. (when (re-search-forward "match[a-z]+" nil t)
  10112. (setq beg (match-end 0))
  10113. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10114. (setq end (1- (match-beginning 0)))))
  10115. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10116. (goto-char (point-min))
  10117. (select-window cwin))))
  10118. ;;; The mark ring for links jumps
  10119. (defvar org-mark-ring nil
  10120. "Mark ring for positions before jumps in Org-mode.")
  10121. (defvar org-mark-ring-last-goto nil
  10122. "Last position in the mark ring used to go back.")
  10123. ;; Fill and close the ring
  10124. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10125. (loop for i from 1 to org-mark-ring-length do
  10126. (push (make-marker) org-mark-ring))
  10127. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10128. org-mark-ring)
  10129. (defun org-mark-ring-push (&optional pos buffer)
  10130. "Put the current position or POS into the mark ring and rotate it."
  10131. (interactive)
  10132. (setq pos (or pos (point)))
  10133. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10134. (move-marker (car org-mark-ring)
  10135. (or pos (point))
  10136. (or buffer (current-buffer)))
  10137. (message "%s"
  10138. (substitute-command-keys
  10139. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10140. (defun org-mark-ring-goto (&optional n)
  10141. "Jump to the previous position in the mark ring.
  10142. With prefix arg N, jump back that many stored positions. When
  10143. called several times in succession, walk through the entire ring.
  10144. Org-mode commands jumping to a different position in the current file,
  10145. or to another Org-mode file, automatically push the old position
  10146. onto the ring."
  10147. (interactive "p")
  10148. (let (p m)
  10149. (if (eq last-command this-command)
  10150. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10151. (setq p org-mark-ring))
  10152. (setq org-mark-ring-last-goto p)
  10153. (setq m (car p))
  10154. (org-pop-to-buffer-same-window (marker-buffer m))
  10155. (goto-char m)
  10156. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10157. (defun org-remove-angle-brackets (s)
  10158. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10159. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10160. s)
  10161. (defun org-add-angle-brackets (s)
  10162. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10163. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10164. s)
  10165. (defun org-remove-double-quotes (s)
  10166. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10167. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10168. s)
  10169. ;;; Following specific links
  10170. (defun org-follow-timestamp-link ()
  10171. "Open an agenda view for the time-stamp date/range at point."
  10172. (cond
  10173. ((org-at-date-range-p t)
  10174. (let ((org-agenda-start-on-weekday)
  10175. (t1 (match-string 1))
  10176. (t2 (match-string 2)) tt1 tt2)
  10177. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10178. tt2 (time-to-days (org-time-string-to-time t2)))
  10179. (let ((org-agenda-buffer-tmp-name
  10180. (format "*Org Agenda(a:%s)"
  10181. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10182. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10183. ((org-at-timestamp-p t)
  10184. (let ((org-agenda-buffer-tmp-name
  10185. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10186. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10187. (substring (match-string 1) 0 10)))
  10188. 1)))
  10189. (t (error "This should not happen"))))
  10190. ;;; Following file links
  10191. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10192. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10193. (declare-function mailcap-mime-info
  10194. "mailcap" (string &optional request no-decode))
  10195. (defvar org-wait nil)
  10196. (defun org-open-file (path &optional in-emacs line search)
  10197. "Open the file at PATH.
  10198. First, this expands any special file name abbreviations. Then the
  10199. configuration variable `org-file-apps' is checked if it contains an
  10200. entry for this file type, and if yes, the corresponding command is launched.
  10201. If no application is found, Emacs simply visits the file.
  10202. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10203. With a double \\[universal-argument] \\[universal-argument] \
  10204. prefix arg, Org tries to avoid opening in Emacs
  10205. and to use an external application to visit the file.
  10206. Optional LINE specifies a line to go to, optional SEARCH a string
  10207. to search for. If LINE or SEARCH is given, the file will be
  10208. opened in Emacs, unless an entry from org-file-apps that makes
  10209. use of groups in a regexp matches.
  10210. If you want to change the way frames are used when following a
  10211. link, please customize `org-link-frame-setup'.
  10212. If the file does not exist, an error is thrown."
  10213. (let* ((file (if (equal path "")
  10214. buffer-file-name
  10215. (substitute-in-file-name (expand-file-name path))))
  10216. (file-apps (append org-file-apps (org-default-apps)))
  10217. (apps (org-remove-if
  10218. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10219. (apps-dlink (org-remove-if-not
  10220. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10221. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10222. (dirp (if remp nil (file-directory-p file)))
  10223. (file (if (and dirp org-open-directory-means-index-dot-org)
  10224. (concat (file-name-as-directory file) "index.org")
  10225. file))
  10226. (a-m-a-p (assq 'auto-mode apps))
  10227. (dfile (downcase file))
  10228. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10229. (link (cond ((and (eq line nil)
  10230. (eq search nil))
  10231. file)
  10232. (line
  10233. (concat file "::" (number-to-string line)))
  10234. (search
  10235. (concat file "::" search))))
  10236. (dlink (downcase link))
  10237. (old-buffer (current-buffer))
  10238. (old-pos (point))
  10239. (old-mode major-mode)
  10240. ext cmd link-match-data)
  10241. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10242. (setq ext (match-string 1 dfile))
  10243. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10244. (setq ext (match-string 1 dfile))))
  10245. (cond
  10246. ((member in-emacs '((16) system))
  10247. (setq cmd (cdr (assoc 'system apps))))
  10248. (in-emacs (setq cmd 'emacs))
  10249. (t
  10250. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10251. (and dirp (cdr (assoc 'directory apps)))
  10252. ; first, try matching against apps-dlink
  10253. ; if we get a match here, store the match data for later
  10254. (let ((match (assoc-default dlink apps-dlink
  10255. 'string-match)))
  10256. (if match
  10257. (progn (setq link-match-data (match-data))
  10258. match)
  10259. (progn (setq in-emacs (or in-emacs line search))
  10260. nil))) ; if we have no match in apps-dlink,
  10261. ; always open the file in emacs if line or search
  10262. ; is given (for backwards compatibility)
  10263. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10264. 'string-match)
  10265. (cdr (assoc ext apps))
  10266. (cdr (assoc t apps))))))
  10267. (when (eq cmd 'system)
  10268. (setq cmd (cdr (assoc 'system apps))))
  10269. (when (eq cmd 'default)
  10270. (setq cmd (cdr (assoc t apps))))
  10271. (when (eq cmd 'mailcap)
  10272. (require 'mailcap)
  10273. (mailcap-parse-mailcaps)
  10274. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10275. (command (mailcap-mime-info mime-type)))
  10276. (if (stringp command)
  10277. (setq cmd command)
  10278. (setq cmd 'emacs))))
  10279. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10280. (not (file-exists-p file))
  10281. (not org-open-non-existing-files))
  10282. (user-error "No such file: %s" file))
  10283. (cond
  10284. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10285. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10286. (while (string-match "['\"]%s['\"]" cmd)
  10287. (setq cmd (replace-match "%s" t t cmd)))
  10288. (while (string-match "%s" cmd)
  10289. (setq cmd (replace-match
  10290. (save-match-data
  10291. (shell-quote-argument
  10292. (convert-standard-filename file)))
  10293. t t cmd)))
  10294. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10295. (save-match-data
  10296. (let ((match-index 1)
  10297. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10298. (set-match-data link-match-data)
  10299. (while (<= match-index number-of-groups)
  10300. (let ((regex (concat "%" (number-to-string match-index)))
  10301. (replace-with (match-string match-index dlink)))
  10302. (while (string-match regex cmd)
  10303. (setq cmd (replace-match replace-with t t cmd))))
  10304. (setq match-index (+ match-index 1)))))
  10305. (save-window-excursion
  10306. (message "Running %s...done" cmd)
  10307. (start-process-shell-command cmd nil cmd)
  10308. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10309. ((or (stringp cmd)
  10310. (eq cmd 'emacs))
  10311. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10312. (widen)
  10313. (if line (progn (org-goto-line line)
  10314. (if (derived-mode-p 'org-mode)
  10315. (org-reveal)))
  10316. (if search (org-link-search search))))
  10317. ((consp cmd)
  10318. (let ((file (convert-standard-filename file)))
  10319. (save-match-data
  10320. (set-match-data link-match-data)
  10321. (eval cmd))))
  10322. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10323. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10324. (or (not (equal old-buffer (current-buffer)))
  10325. (not (equal old-pos (point))))
  10326. (org-mark-ring-push old-pos old-buffer))))
  10327. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10328. "This function returns non-nil if `entry' uses a regular
  10329. expression which should be matched against the whole link by
  10330. org-open-file.
  10331. It assumes that is the case when the entry uses a regular
  10332. expression which has at least one grouping construct and the
  10333. action is either a lisp form or a command string containing
  10334. '%1', i.e. using at least one subexpression match as a
  10335. parameter."
  10336. (let ((selector (car entry))
  10337. (action (cdr entry)))
  10338. (if (stringp selector)
  10339. (and (> (regexp-opt-depth selector) 0)
  10340. (or (and (stringp action)
  10341. (string-match "%[0-9]" action))
  10342. (consp action)))
  10343. nil)))
  10344. (defun org-default-apps ()
  10345. "Return the default applications for this operating system."
  10346. (cond
  10347. ((eq system-type 'darwin)
  10348. org-file-apps-defaults-macosx)
  10349. ((eq system-type 'windows-nt)
  10350. org-file-apps-defaults-windowsnt)
  10351. (t org-file-apps-defaults-gnu)))
  10352. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10353. "Convert extensions to regular expressions in the cars of LIST.
  10354. Also, weed out any non-string entries, because the return value is used
  10355. only for regexp matching.
  10356. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10357. point to the symbol `emacs', indicating that the file should
  10358. be opened in Emacs."
  10359. (append
  10360. (delq nil
  10361. (mapcar (lambda (x)
  10362. (if (not (stringp (car x)))
  10363. nil
  10364. (if (string-match "\\W" (car x))
  10365. x
  10366. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10367. list))
  10368. (if add-auto-mode
  10369. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10370. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10371. (defun org-file-remote-p (file)
  10372. "Test whether FILE specifies a location on a remote system.
  10373. Return non-nil if the location is indeed remote.
  10374. For example, the filename \"/user@host:/foo\" specifies a location
  10375. on the system \"/user@host:\"."
  10376. (cond ((fboundp 'file-remote-p)
  10377. (file-remote-p file))
  10378. ((fboundp 'tramp-handle-file-remote-p)
  10379. (tramp-handle-file-remote-p file))
  10380. ((and (boundp 'ange-ftp-name-format)
  10381. (string-match (car ange-ftp-name-format) file))
  10382. t)))
  10383. ;;;; Refiling
  10384. (defun org-get-org-file ()
  10385. "Read a filename, with default directory `org-directory'."
  10386. (let ((default (or org-default-notes-file remember-data-file)))
  10387. (read-file-name (format "File name [%s]: " default)
  10388. (file-name-as-directory org-directory)
  10389. default)))
  10390. (defun org-notes-order-reversed-p ()
  10391. "Check if the current file should receive notes in reversed order."
  10392. (cond
  10393. ((not org-reverse-note-order) nil)
  10394. ((eq t org-reverse-note-order) t)
  10395. ((not (listp org-reverse-note-order)) nil)
  10396. (t (catch 'exit
  10397. (dolist (entry org-reverse-note-order)
  10398. (if (string-match (car entry) buffer-file-name)
  10399. (throw 'exit (cdr entry))))))))
  10400. (defvar org-refile-target-table nil
  10401. "The list of refile targets, created by `org-refile'.")
  10402. (defvar org-agenda-new-buffers nil
  10403. "Buffers created to visit agenda files.")
  10404. (defvar org-refile-cache nil
  10405. "Cache for refile targets.")
  10406. (defvar org-refile-markers nil
  10407. "All the markers used for caching refile locations.")
  10408. (defun org-refile-marker (pos)
  10409. "Get a new refile marker, but only if caching is in use."
  10410. (if (not org-refile-use-cache)
  10411. pos
  10412. (let ((m (make-marker)))
  10413. (move-marker m pos)
  10414. (push m org-refile-markers)
  10415. m)))
  10416. (defun org-refile-cache-clear ()
  10417. "Clear the refile cache and disable all the markers."
  10418. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10419. (setq org-refile-markers nil)
  10420. (setq org-refile-cache nil)
  10421. (message "Refile cache has been cleared"))
  10422. (defun org-refile-cache-check-set (set)
  10423. "Check if all the markers in the cache still have live buffers."
  10424. (let (marker)
  10425. (catch 'exit
  10426. (while (and set (setq marker (nth 3 (pop set))))
  10427. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10428. (when (and marker (null (marker-buffer marker)))
  10429. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10430. (sit-for 3)
  10431. (throw 'exit nil)))
  10432. t)))
  10433. (defun org-refile-cache-put (set &rest identifiers)
  10434. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10435. (let* ((key (sha1 (prin1-to-string identifiers)))
  10436. (entry (assoc key org-refile-cache)))
  10437. (if entry
  10438. (setcdr entry set)
  10439. (push (cons key set) org-refile-cache))))
  10440. (defun org-refile-cache-get (&rest identifiers)
  10441. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10442. (cond
  10443. ((not org-refile-cache) nil)
  10444. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10445. (t
  10446. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10447. org-refile-cache))))
  10448. (and set (org-refile-cache-check-set set) set)))))
  10449. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10450. "Produce a table with refile targets."
  10451. (let ((case-fold-search nil)
  10452. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10453. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10454. targets tgs txt re files desc descre fast-path-p level pos0)
  10455. (message "Getting targets...")
  10456. (with-current-buffer (or default-buffer (current-buffer))
  10457. (dolist (entry entries)
  10458. (setq files (car entry) desc (cdr entry))
  10459. (setq fast-path-p nil)
  10460. (cond
  10461. ((null files) (setq files (list (current-buffer))))
  10462. ((eq files 'org-agenda-files)
  10463. (setq files (org-agenda-files 'unrestricted)))
  10464. ((and (symbolp files) (fboundp files))
  10465. (setq files (funcall files)))
  10466. ((and (symbolp files) (boundp files))
  10467. (setq files (symbol-value files))))
  10468. (if (stringp files) (setq files (list files)))
  10469. (cond
  10470. ((eq (car desc) :tag)
  10471. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10472. ((eq (car desc) :todo)
  10473. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10474. ((eq (car desc) :regexp)
  10475. (setq descre (cdr desc)))
  10476. ((eq (car desc) :level)
  10477. (setq descre (concat "^\\*\\{" (number-to-string
  10478. (if org-odd-levels-only
  10479. (1- (* 2 (cdr desc)))
  10480. (cdr desc)))
  10481. "\\}[ \t]")))
  10482. ((eq (car desc) :maxlevel)
  10483. (setq fast-path-p t)
  10484. (setq descre (concat "^\\*\\{1," (number-to-string
  10485. (if org-odd-levels-only
  10486. (1- (* 2 (cdr desc)))
  10487. (cdr desc)))
  10488. "\\}[ \t]")))
  10489. (t (error "Bad refiling target description %s" desc)))
  10490. (dolist (f files)
  10491. (with-current-buffer
  10492. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10493. (or
  10494. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10495. (progn
  10496. (if (bufferp f) (setq f (buffer-file-name
  10497. (buffer-base-buffer f))))
  10498. (setq f (and f (expand-file-name f)))
  10499. (if (eq org-refile-use-outline-path 'file)
  10500. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10501. (save-excursion
  10502. (save-restriction
  10503. (widen)
  10504. (goto-char (point-min))
  10505. (while (re-search-forward descre nil t)
  10506. (goto-char (setq pos0 (point-at-bol)))
  10507. (catch 'next
  10508. (when org-refile-target-verify-function
  10509. (save-match-data
  10510. (or (funcall org-refile-target-verify-function)
  10511. (throw 'next t))))
  10512. (when (and (looking-at org-complex-heading-regexp)
  10513. (not (member (match-string 4) excluded-entries))
  10514. (match-string 4))
  10515. (setq level (org-reduced-level
  10516. (- (match-end 1) (match-beginning 1)))
  10517. txt (org-link-display-format (match-string 4))
  10518. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10519. re (format org-complex-heading-regexp-format
  10520. (regexp-quote (match-string 4))))
  10521. (when org-refile-use-outline-path
  10522. (setq txt (mapconcat
  10523. 'org-protect-slash
  10524. (append
  10525. (if (eq org-refile-use-outline-path
  10526. 'file)
  10527. (list (file-name-nondirectory
  10528. (buffer-file-name
  10529. (buffer-base-buffer))))
  10530. (if (eq org-refile-use-outline-path
  10531. 'full-file-path)
  10532. (list (buffer-file-name
  10533. (buffer-base-buffer)))))
  10534. (org-get-outline-path fast-path-p
  10535. level txt)
  10536. (list txt))
  10537. "/")))
  10538. (push (list txt f re (org-refile-marker (point)))
  10539. tgs)))
  10540. (when (= (point) pos0)
  10541. ;; verification function has not moved point
  10542. (goto-char (point-at-eol))))))))
  10543. (when org-refile-use-cache
  10544. (org-refile-cache-put tgs (buffer-file-name) descre))
  10545. (setq targets (append tgs targets))))))
  10546. (message "Getting targets...done")
  10547. (nreverse targets)))
  10548. (defun org-protect-slash (s)
  10549. (while (string-match "/" s)
  10550. (setq s (replace-match "\\" t t s)))
  10551. s)
  10552. (defvar org-olpa (make-vector 20 nil))
  10553. (defun org-get-outline-path (&optional fastp level heading)
  10554. "Return the outline path to the current entry, as a list.
  10555. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10556. routine which makes outline path derivations for an entire file,
  10557. avoiding backtracing. Refile target collection makes use of that."
  10558. (if fastp
  10559. (progn
  10560. (if (> level 19)
  10561. (error "Outline path failure, more than 19 levels"))
  10562. (loop for i from level upto 19 do
  10563. (aset org-olpa i nil))
  10564. (prog1
  10565. (delq nil (append org-olpa nil))
  10566. (aset org-olpa level heading)))
  10567. (let (rtn case-fold-search)
  10568. (save-excursion
  10569. (save-restriction
  10570. (widen)
  10571. (while (org-up-heading-safe)
  10572. (when (looking-at org-complex-heading-regexp)
  10573. (push (org-trim
  10574. (replace-regexp-in-string
  10575. ;; Remove statistical/checkboxes cookies
  10576. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10577. (org-match-string-no-properties 4)))
  10578. rtn)))
  10579. rtn)))))
  10580. (defun org-format-outline-path (path &optional width prefix separator)
  10581. "Format the outline path PATH for display.
  10582. WIDTH is the maximum number of characters that is available.
  10583. PREFIX is a prefix to be included in the returned string,
  10584. such as the file name.
  10585. SEPARATOR is inserted between the different parts of the path,
  10586. the default is \"/\"."
  10587. (setq width (or width 79))
  10588. (if prefix (setq width (- width (length prefix))))
  10589. (if (not path)
  10590. (or prefix "")
  10591. (let* ((nsteps (length path))
  10592. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10593. (maxwidth (if (<= total-width width)
  10594. 10000 ;; everything fits
  10595. ;; we need to shorten the level headings
  10596. (/ (- width nsteps) nsteps)))
  10597. (org-odd-levels-only nil)
  10598. (n 0)
  10599. (total (1+ (length prefix))))
  10600. (setq maxwidth (max maxwidth 10))
  10601. (concat prefix
  10602. (if prefix (or separator "/"))
  10603. (mapconcat
  10604. (lambda (h)
  10605. (setq n (1+ n))
  10606. (if (and (= n nsteps) (< maxwidth 10000))
  10607. (setq maxwidth (- total-width total)))
  10608. (if (< (length h) maxwidth)
  10609. (progn (setq total (+ total (length h) 1)) h)
  10610. (setq h (substring h 0 (- maxwidth 2))
  10611. total (+ total maxwidth 1))
  10612. (if (string-match "[ \t]+\\'" h)
  10613. (setq h (substring h 0 (match-beginning 0))))
  10614. (setq h (concat h "..")))
  10615. (org-add-props h nil 'face
  10616. (nth (% (1- n) org-n-level-faces)
  10617. org-level-faces))
  10618. h)
  10619. path (or separator "/"))))))
  10620. (defun org-display-outline-path (&optional file current separator just-return-string)
  10621. "Display the current outline path in the echo area.
  10622. If FILE is non-nil, prepend the output with the file name.
  10623. If CURRENT is non-nil, append the current heading to the output.
  10624. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10625. the different parts of the path and defaults to \"/\".
  10626. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10627. (interactive "P")
  10628. (let* (case-fold-search
  10629. (bfn (buffer-file-name (buffer-base-buffer)))
  10630. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10631. res)
  10632. (if current (setq path (append path
  10633. (save-excursion
  10634. (org-back-to-heading t)
  10635. (if (looking-at org-complex-heading-regexp)
  10636. (list (match-string 4)))))))
  10637. (setq res
  10638. (org-format-outline-path
  10639. path
  10640. (1- (frame-width))
  10641. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10642. separator))
  10643. (if just-return-string
  10644. (org-no-properties res)
  10645. (org-unlogged-message "%s" res))))
  10646. (defvar org-refile-history nil
  10647. "History for refiling operations.")
  10648. (defvar org-after-refile-insert-hook nil
  10649. "Hook run after `org-refile' has inserted its stuff at the new location.
  10650. Note that this is still *before* the stuff will be removed from
  10651. the *old* location.")
  10652. (defvar org-capture-last-stored-marker)
  10653. (defvar org-refile-keep nil
  10654. "Non-nil means `org-refile' will copy instead of refile.")
  10655. (defun org-copy ()
  10656. "Like `org-refile', but copy."
  10657. (interactive)
  10658. (let ((org-refile-keep t))
  10659. (funcall 'org-refile nil nil nil "Copy")))
  10660. (defun org-refile (&optional arg default-buffer rfloc msg)
  10661. "Move the entry or entries at point to another heading.
  10662. The list of target headings is compiled using the information in
  10663. `org-refile-targets', which see.
  10664. At the target location, the entry is filed as a subitem of the
  10665. target heading. Depending on `org-reverse-note-order', the new
  10666. subitem will either be the first or the last subitem.
  10667. If there is an active region, all entries in that region will be
  10668. refiled. However, the region must fulfill the requirement that
  10669. the first heading sets the top-level of the moved text.
  10670. With prefix arg ARG, the command will only visit the target
  10671. location and not actually move anything.
  10672. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10673. refiling operation has put the subtree.
  10674. With a numeric prefix argument of `2', refile to the running clock.
  10675. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10676. being set to t and copy to the target location, don't move it.
  10677. Beware that keeping refiled entries may result in duplicated ID
  10678. properties.
  10679. RFLOC can be a refile location obtained in a different way.
  10680. MSG is a string to replace \"Refile\" in the default prompt with
  10681. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10682. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10683. If you are using target caching (see `org-refile-use-cache'), you
  10684. have to clear the target cache in order to find new targets.
  10685. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10686. prefix argument (`C-u C-u C-u C-c C-w')."
  10687. (interactive "P")
  10688. (if (member arg '(0 (64)))
  10689. (org-refile-cache-clear)
  10690. (let* ((actionmsg (cond (msg msg)
  10691. ((equal arg 3) "Refile (and keep)")
  10692. (t "Refile")))
  10693. (cbuf (current-buffer))
  10694. (regionp (org-region-active-p))
  10695. (region-start (and regionp (region-beginning)))
  10696. (region-end (and regionp (region-end)))
  10697. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10698. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10699. pos it nbuf file re level reversed)
  10700. (setq last-command nil)
  10701. (when regionp
  10702. (goto-char region-start)
  10703. (or (bolp) (goto-char (point-at-bol)))
  10704. (setq region-start (point))
  10705. (unless (or (org-kill-is-subtree-p
  10706. (buffer-substring region-start region-end))
  10707. (prog1 org-refile-active-region-within-subtree
  10708. (let ((s (point-at-eol)))
  10709. (org-toggle-heading)
  10710. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10711. (user-error "The region is not a (sequence of) subtree(s)")))
  10712. (if (equal arg '(16))
  10713. (org-refile-goto-last-stored)
  10714. (when (or
  10715. (and (equal arg 2)
  10716. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10717. (prog1
  10718. (setq it (list (or org-clock-heading "running clock")
  10719. (buffer-file-name
  10720. (marker-buffer org-clock-hd-marker))
  10721. ""
  10722. (marker-position org-clock-hd-marker)))
  10723. (setq arg nil)))
  10724. (setq it (or rfloc
  10725. (let (heading-text)
  10726. (save-excursion
  10727. (unless (and arg (listp arg))
  10728. (org-back-to-heading t)
  10729. (setq heading-text
  10730. (replace-regexp-in-string
  10731. org-bracket-link-regexp
  10732. "\\3"
  10733. (nth 4 (org-heading-components)))))
  10734. (org-refile-get-location
  10735. (cond ((and arg (listp arg)) "Goto")
  10736. (regionp (concat actionmsg " region to"))
  10737. (t (concat actionmsg " subtree \""
  10738. heading-text "\" to")))
  10739. default-buffer
  10740. (and (not (equal '(4) arg))
  10741. org-refile-allow-creating-parent-nodes)
  10742. arg))))))
  10743. (setq file (nth 1 it)
  10744. re (nth 2 it)
  10745. pos (nth 3 it))
  10746. (if (and (not arg)
  10747. pos
  10748. (equal (buffer-file-name) file)
  10749. (if regionp
  10750. (and (>= pos region-start)
  10751. (<= pos region-end))
  10752. (and (>= pos (point))
  10753. (< pos (save-excursion
  10754. (org-end-of-subtree t t))))))
  10755. (error "Cannot refile to position inside the tree or region"))
  10756. (setq nbuf (or (find-buffer-visiting file)
  10757. (find-file-noselect file)))
  10758. (if (and arg (not (equal arg 3)))
  10759. (progn
  10760. (org-pop-to-buffer-same-window nbuf)
  10761. (goto-char pos)
  10762. (org-show-context 'org-goto))
  10763. (if regionp
  10764. (progn
  10765. (org-kill-new (buffer-substring region-start region-end))
  10766. (org-save-markers-in-region region-start region-end))
  10767. (org-copy-subtree 1 nil t))
  10768. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10769. (find-file-noselect file)))
  10770. (setq reversed (org-notes-order-reversed-p))
  10771. (save-excursion
  10772. (save-restriction
  10773. (widen)
  10774. (if pos
  10775. (progn
  10776. (goto-char pos)
  10777. (looking-at org-outline-regexp)
  10778. (setq level (org-get-valid-level (funcall outline-level) 1))
  10779. (goto-char
  10780. (if reversed
  10781. (or (outline-next-heading) (point-max))
  10782. (or (save-excursion (org-get-next-sibling))
  10783. (org-end-of-subtree t t)
  10784. (point-max)))))
  10785. (setq level 1)
  10786. (if (not reversed)
  10787. (goto-char (point-max))
  10788. (goto-char (point-min))
  10789. (or (outline-next-heading) (goto-char (point-max)))))
  10790. (if (not (bolp)) (newline))
  10791. (org-paste-subtree level nil nil t)
  10792. (when org-log-refile
  10793. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10794. (unless (eq org-log-refile 'note)
  10795. (save-excursion (org-add-log-note))))
  10796. (and org-auto-align-tags
  10797. (let ((org-loop-over-headlines-in-active-region nil))
  10798. (org-set-tags nil t)))
  10799. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10800. :last-refile)))
  10801. (when bookmark-name
  10802. (with-demoted-errors
  10803. (bookmark-set bookmark-name))))
  10804. ;; If we are refiling for capture, make sure that the
  10805. ;; last-capture pointers point here
  10806. (when (org-bound-and-true-p org-refile-for-capture)
  10807. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10808. :last-capture-marker)))
  10809. (when bookmark-name
  10810. (with-demoted-errors
  10811. (bookmark-set bookmark-name))))
  10812. (move-marker org-capture-last-stored-marker (point)))
  10813. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10814. (run-hooks 'org-after-refile-insert-hook))))
  10815. (unless org-refile-keep
  10816. (if regionp
  10817. (delete-region (point) (+ (point) (- region-end region-start)))
  10818. (delete-region
  10819. (and (org-back-to-heading t) (point))
  10820. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10821. (when (featurep 'org-inlinetask)
  10822. (org-inlinetask-remove-END-maybe))
  10823. (setq org-markers-to-move nil)
  10824. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10825. (defun org-refile-goto-last-stored ()
  10826. "Go to the location where the last refile was stored."
  10827. (interactive)
  10828. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10829. (message "This is the location of the last refile"))
  10830. (defun org-refile--get-location (refloc tbl)
  10831. "When user refile to REFLOC, find the associated target in TBL.
  10832. Also check `org-refile-target-table'."
  10833. (car (delq
  10834. nil
  10835. (mapcar
  10836. (lambda (r) (or (assoc r tbl)
  10837. (assoc r org-refile-target-table)))
  10838. (list (replace-regexp-in-string "/$" "" refloc)
  10839. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10840. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10841. no-exclude)
  10842. "Prompt the user for a refile location, using PROMPT.
  10843. PROMPT should not be suffixed with a colon and a space, because
  10844. this function appends the default value from
  10845. `org-refile-history' automatically, if that is not empty.
  10846. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10847. this is used for the GOTO interface."
  10848. (let ((org-refile-targets org-refile-targets)
  10849. (org-refile-use-outline-path org-refile-use-outline-path)
  10850. excluded-entries)
  10851. (when (and (derived-mode-p 'org-mode)
  10852. (not org-refile-use-cache)
  10853. (not no-exclude))
  10854. (org-map-tree
  10855. (lambda()
  10856. (setq excluded-entries
  10857. (append excluded-entries (list (org-get-heading t t)))))))
  10858. (setq org-refile-target-table
  10859. (org-refile-get-targets default-buffer excluded-entries)))
  10860. (unless org-refile-target-table
  10861. (user-error "No refile targets"))
  10862. (let* ((cbuf (current-buffer))
  10863. (partial-completion-mode nil)
  10864. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10865. (cfunc (if (and org-refile-use-outline-path
  10866. org-outline-path-complete-in-steps)
  10867. 'org-olpath-completing-read
  10868. 'org-icompleting-read))
  10869. (extra (if org-refile-use-outline-path "/" ""))
  10870. (cbnex (concat (buffer-name) extra))
  10871. (filename (and cfn (expand-file-name cfn)))
  10872. (tbl (mapcar
  10873. (lambda (x)
  10874. (if (and (not (member org-refile-use-outline-path
  10875. '(file full-file-path)))
  10876. (not (equal filename (nth 1 x))))
  10877. (cons (concat (car x) extra " ("
  10878. (file-name-nondirectory (nth 1 x)) ")")
  10879. (cdr x))
  10880. (cons (concat (car x) extra) (cdr x))))
  10881. org-refile-target-table))
  10882. (completion-ignore-case t)
  10883. cdef
  10884. (prompt (concat prompt
  10885. (or (and (car org-refile-history)
  10886. (concat " (default " (car org-refile-history) ")"))
  10887. (and (assoc cbnex tbl) (setq cdef cbnex)
  10888. (concat " (default " cbnex ")"))) ": "))
  10889. pa answ parent-target child parent old-hist)
  10890. (setq old-hist org-refile-history)
  10891. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10892. nil 'org-refile-history (or cdef (car org-refile-history))))
  10893. (if (setq pa (org-refile--get-location answ tbl))
  10894. (progn
  10895. (org-refile-check-position pa)
  10896. (when (or (not org-refile-history)
  10897. (not (eq old-hist org-refile-history))
  10898. (not (equal (car pa) (car org-refile-history))))
  10899. (setq org-refile-history
  10900. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10901. org-refile-history
  10902. (cdr org-refile-history))))
  10903. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10904. (pop org-refile-history)))
  10905. pa)
  10906. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10907. (progn
  10908. (setq parent (match-string 1 answ)
  10909. child (match-string 2 answ))
  10910. (setq parent-target (org-refile--get-location parent tbl))
  10911. (when (and parent-target
  10912. (or (eq new-nodes t)
  10913. (and (eq new-nodes 'confirm)
  10914. (y-or-n-p (format "Create new node \"%s\"? "
  10915. child)))))
  10916. (org-refile-new-child parent-target child)))
  10917. (user-error "Invalid target location")))))
  10918. (declare-function org-string-nw-p "org-macs" (s))
  10919. (defun org-refile-check-position (refile-pointer)
  10920. "Check if the refile pointer matches the headline to which it points."
  10921. (let* ((file (nth 1 refile-pointer))
  10922. (re (nth 2 refile-pointer))
  10923. (pos (nth 3 refile-pointer))
  10924. buffer)
  10925. (if (and (not (markerp pos)) (not file))
  10926. (user-error "Please indicate a target file in the refile path")
  10927. (when (org-string-nw-p re)
  10928. (setq buffer (if (markerp pos)
  10929. (marker-buffer pos)
  10930. (or (find-buffer-visiting file)
  10931. (find-file-noselect file))))
  10932. (with-current-buffer buffer
  10933. (save-excursion
  10934. (save-restriction
  10935. (widen)
  10936. (goto-char pos)
  10937. (beginning-of-line 1)
  10938. (unless (org-looking-at-p re)
  10939. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10940. (defun org-refile-new-child (parent-target child)
  10941. "Use refile target PARENT-TARGET to add new CHILD below it."
  10942. (unless parent-target
  10943. (error "Cannot find parent for new node"))
  10944. (let ((file (nth 1 parent-target))
  10945. (pos (nth 3 parent-target))
  10946. level)
  10947. (with-current-buffer (or (find-buffer-visiting file)
  10948. (find-file-noselect file))
  10949. (save-excursion
  10950. (save-restriction
  10951. (widen)
  10952. (if pos
  10953. (goto-char pos)
  10954. (goto-char (point-max))
  10955. (if (not (bolp)) (newline)))
  10956. (when (looking-at org-outline-regexp)
  10957. (setq level (funcall outline-level))
  10958. (org-end-of-subtree t t))
  10959. (org-back-over-empty-lines)
  10960. (insert "\n" (make-string
  10961. (if pos (org-get-valid-level level 1) 1) ?*)
  10962. " " child "\n")
  10963. (beginning-of-line 0)
  10964. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10965. (defun org-olpath-completing-read (prompt collection &rest args)
  10966. "Read an outline path like a file name."
  10967. (let ((thetable collection)
  10968. (org-completion-use-ido nil) ; does not work with ido.
  10969. (org-completion-use-iswitchb nil)) ; or iswitchb
  10970. (apply #'org-icompleting-read
  10971. prompt
  10972. (lambda (string predicate &optional flag)
  10973. (cond
  10974. ((eq flag nil) (try-completion string thetable))
  10975. ((eq flag t)
  10976. (let ((l (length string)))
  10977. (mapcar (lambda (x)
  10978. (let ((r (substring x l))
  10979. (f (if (string-match " ([^)]*)$" x)
  10980. (match-string 0 x)
  10981. "")))
  10982. (if (string-match "/" r)
  10983. (concat string (substring r 0 (match-end 0)) f)
  10984. x)))
  10985. (all-completions string thetable predicate))))
  10986. ;; Exact match?
  10987. ((eq flag 'lambda) (assoc string thetable))))
  10988. args)))
  10989. ;;;; Dynamic blocks
  10990. (defun org-find-dblock (name)
  10991. "Find the first dynamic block with name NAME in the buffer.
  10992. If not found, stay at current position and return nil."
  10993. (let ((case-fold-search t) pos)
  10994. (save-excursion
  10995. (goto-char (point-min))
  10996. (setq pos (and (re-search-forward
  10997. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10998. (match-beginning 0))))
  10999. (if pos (goto-char pos))
  11000. pos))
  11001. (defun org-create-dblock (plist)
  11002. "Create a dynamic block section, with parameters taken from PLIST.
  11003. PLIST must contain a :name entry which is used as the name of the block."
  11004. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11005. (end-of-line 1)
  11006. (newline))
  11007. (let ((col (current-column))
  11008. (name (plist-get plist :name)))
  11009. (insert "#+BEGIN: " name)
  11010. (while plist
  11011. (if (eq (car plist) :name)
  11012. (setq plist (cddr plist))
  11013. (insert " " (prin1-to-string (pop plist)))))
  11014. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11015. (beginning-of-line -2)))
  11016. (defun org-prepare-dblock ()
  11017. "Prepare dynamic block for refresh.
  11018. This empties the block, puts the cursor at the insert position and returns
  11019. the property list including an extra property :name with the block name."
  11020. (unless (looking-at org-dblock-start-re)
  11021. (user-error "Not at a dynamic block"))
  11022. (let* ((begdel (1+ (match-end 0)))
  11023. (name (org-no-properties (match-string 1)))
  11024. (params (append (list :name name)
  11025. (read (concat "(" (match-string 3) ")")))))
  11026. (save-excursion
  11027. (beginning-of-line 1)
  11028. (skip-chars-forward " \t")
  11029. (setq params (plist-put params :indentation-column (current-column))))
  11030. (unless (re-search-forward org-dblock-end-re nil t)
  11031. (error "Dynamic block not terminated"))
  11032. (setq params
  11033. (append params
  11034. (list :content (buffer-substring
  11035. begdel (match-beginning 0)))))
  11036. (delete-region begdel (match-beginning 0))
  11037. (goto-char begdel)
  11038. (open-line 1)
  11039. params))
  11040. (defun org-map-dblocks (&optional command)
  11041. "Apply COMMAND to all dynamic blocks in the current buffer.
  11042. If COMMAND is not given, use `org-update-dblock'."
  11043. (let ((cmd (or command 'org-update-dblock)))
  11044. (save-excursion
  11045. (goto-char (point-min))
  11046. (while (re-search-forward org-dblock-start-re nil t)
  11047. (goto-char (match-beginning 0))
  11048. (save-excursion
  11049. (condition-case nil
  11050. (funcall cmd)
  11051. (error (message "Error during update of dynamic block"))))
  11052. (unless (re-search-forward org-dblock-end-re nil t)
  11053. (error "Dynamic block not terminated"))))))
  11054. (defun org-dblock-update (&optional arg)
  11055. "User command for updating dynamic blocks.
  11056. Update the dynamic block at point. With prefix ARG, update all dynamic
  11057. blocks in the buffer."
  11058. (interactive "P")
  11059. (if arg
  11060. (org-update-all-dblocks)
  11061. (or (looking-at org-dblock-start-re)
  11062. (org-beginning-of-dblock))
  11063. (org-update-dblock)))
  11064. (defun org-update-dblock ()
  11065. "Update the dynamic block at point.
  11066. This means to empty the block, parse for parameters and then call
  11067. the correct writing function."
  11068. (interactive)
  11069. (save-excursion
  11070. (let* ((win (selected-window))
  11071. (pos (point))
  11072. (line (org-current-line))
  11073. (params (org-prepare-dblock))
  11074. (name (plist-get params :name))
  11075. (indent (plist-get params :indentation-column))
  11076. (cmd (intern (concat "org-dblock-write:" name))))
  11077. (message "Updating dynamic block `%s' at line %d..." name line)
  11078. (funcall cmd params)
  11079. (message "Updating dynamic block `%s' at line %d...done" name line)
  11080. (goto-char pos)
  11081. (when (and indent (> indent 0))
  11082. (setq indent (make-string indent ?\ ))
  11083. (save-excursion
  11084. (select-window win)
  11085. (org-beginning-of-dblock)
  11086. (forward-line 1)
  11087. (while (not (looking-at org-dblock-end-re))
  11088. (insert indent)
  11089. (beginning-of-line 2))
  11090. (when (looking-at org-dblock-end-re)
  11091. (and (looking-at "[ \t]+")
  11092. (replace-match ""))
  11093. (insert indent)))))))
  11094. (defun org-beginning-of-dblock ()
  11095. "Find the beginning of the dynamic block at point.
  11096. Error if there is no such block at point."
  11097. (let ((pos (point))
  11098. beg)
  11099. (end-of-line 1)
  11100. (if (and (re-search-backward org-dblock-start-re nil t)
  11101. (setq beg (match-beginning 0))
  11102. (re-search-forward org-dblock-end-re nil t)
  11103. (> (match-end 0) pos))
  11104. (goto-char beg)
  11105. (goto-char pos)
  11106. (error "Not in a dynamic block"))))
  11107. (defun org-update-all-dblocks ()
  11108. "Update all dynamic blocks in the buffer.
  11109. This function can be used in a hook."
  11110. (interactive)
  11111. (when (derived-mode-p 'org-mode)
  11112. (org-map-dblocks 'org-update-dblock)))
  11113. ;;;; Completion
  11114. (declare-function org-export-backend-name "org-export" (cl-x))
  11115. (declare-function org-export-backend-options "org-export" (cl-x))
  11116. (defun org-get-export-keywords ()
  11117. "Return a list of all currently understood export keywords.
  11118. Export keywords include options, block names, attributes and
  11119. keywords relative to each registered export back-end."
  11120. (let (keywords)
  11121. (dolist (backend
  11122. (org-bound-and-true-p org-export-registered-backends)
  11123. (delq nil keywords))
  11124. ;; Back-end name (for keywords, like #+LATEX:)
  11125. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11126. (dolist (option-entry (org-export-backend-options backend))
  11127. ;; Back-end options.
  11128. (push (nth 1 option-entry) keywords)))))
  11129. (defconst org-options-keywords
  11130. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11131. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11132. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11133. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11134. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11135. (defcustom org-structure-template-alist
  11136. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11137. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11138. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11139. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11140. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11141. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11142. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11143. ("L" "#+LaTeX: ")
  11144. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11145. ("H" "#+HTML: ")
  11146. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11147. ("A" "#+ASCII: ")
  11148. ("i" "#+INDEX: ?")
  11149. ("I" "#+INCLUDE: %file ?"))
  11150. "Structure completion elements.
  11151. This is a list of abbreviation keys and values. The value gets inserted
  11152. if you type `<' followed by the key and then press the completion key,
  11153. usually `TAB'. %file will be replaced by a file name after prompting
  11154. for the file using completion. The cursor will be placed at the position
  11155. of the `?' in the template.
  11156. There are two templates for each key, the first uses the original Org syntax,
  11157. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11158. the default when the /org-mtags.el/ module has been loaded. See also the
  11159. variable `org-mtags-prefer-muse-templates'."
  11160. :group 'org-completion
  11161. :type '(repeat
  11162. (list
  11163. (string :tag "Key")
  11164. (string :tag "Template")))
  11165. :version "25.1"
  11166. :package-version '(Org . "8.3"))
  11167. (defun org-try-structure-completion ()
  11168. "Try to complete a structure template before point.
  11169. This looks for strings like \"<e\" on an otherwise empty line and
  11170. expands them."
  11171. (let ((l (buffer-substring (point-at-bol) (point)))
  11172. a)
  11173. (when (and (looking-at "[ \t]*$")
  11174. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11175. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11176. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11177. (match-beginning 1)) a)
  11178. t)))
  11179. (defun org-complete-expand-structure-template (start cell)
  11180. "Expand a structure template."
  11181. (let ((rpl (nth 1 cell))
  11182. (ind ""))
  11183. (delete-region start (point))
  11184. (when (string-match "\\`[ \t]*#\\+" rpl)
  11185. (cond
  11186. ((bolp))
  11187. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11188. (setq ind (buffer-substring (point-at-bol) (point))))
  11189. (t (newline))))
  11190. (setq start (point))
  11191. (if (string-match "%file" rpl)
  11192. (setq rpl (replace-match
  11193. (concat
  11194. "\""
  11195. (save-match-data
  11196. (abbreviate-file-name (read-file-name "Include file: ")))
  11197. "\"")
  11198. t t rpl)))
  11199. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11200. (concat "\n" ind)))
  11201. (insert rpl)
  11202. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11203. ;;;; TODO, DEADLINE, Comments
  11204. (defun org-toggle-comment ()
  11205. "Change the COMMENT state of an entry."
  11206. (interactive)
  11207. (save-excursion
  11208. (org-back-to-heading)
  11209. (looking-at org-complex-heading-regexp)
  11210. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11211. (skip-chars-forward " \t")
  11212. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11213. (if (org-in-commented-heading-p t)
  11214. (delete-region (point)
  11215. (progn (search-forward " " (line-end-position) 'move)
  11216. (skip-chars-forward " \t")
  11217. (point)))
  11218. (insert org-comment-string)
  11219. (unless (eolp) (insert " ")))))
  11220. (defvar org-last-todo-state-is-todo nil
  11221. "This is non-nil when the last TODO state change led to a TODO state.
  11222. If the last change removed the TODO tag or switched to DONE, then
  11223. this is nil.")
  11224. (defvar org-setting-tags nil) ; dynamically skipped
  11225. (defvar org-todo-setup-filter-hook nil
  11226. "Hook for functions that pre-filter todo specs.
  11227. Each function takes a todo spec and returns either nil or the spec
  11228. transformed into canonical form." )
  11229. (defvar org-todo-get-default-hook nil
  11230. "Hook for functions that get a default item for todo.
  11231. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11232. nil or a string to be used for the todo mark." )
  11233. (defvar org-agenda-headline-snapshot-before-repeat)
  11234. (defun org-current-effective-time ()
  11235. "Return current time adjusted for `org-extend-today-until' variable."
  11236. (let* ((ct (org-current-time))
  11237. (dct (decode-time ct))
  11238. (ct1
  11239. (cond
  11240. (org-use-last-clock-out-time-as-effective-time
  11241. (or (org-clock-get-last-clock-out-time) ct))
  11242. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11243. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11244. (t ct))))
  11245. ct1))
  11246. (defun org-todo-yesterday (&optional arg)
  11247. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11248. (interactive "P")
  11249. (if (eq major-mode 'org-agenda-mode)
  11250. (apply 'org-agenda-todo-yesterday arg)
  11251. (let* ((org-use-effective-time t)
  11252. (hour (third (decode-time
  11253. (org-current-time))))
  11254. (org-extend-today-until (1+ hour)))
  11255. (org-todo arg))))
  11256. (defvar org-block-entry-blocking ""
  11257. "First entry preventing the TODO state change.")
  11258. (defun org-cancel-repeater ()
  11259. "Cancel a repeater by setting its numeric value to zero."
  11260. (interactive)
  11261. (save-excursion
  11262. (org-back-to-heading t)
  11263. (let ((bound1 (point))
  11264. (bound0 (save-excursion (outline-next-heading) (point))))
  11265. (when (re-search-forward
  11266. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11267. org-deadline-time-regexp "\\)\\|\\("
  11268. org-ts-regexp "\\)")
  11269. bound0 t)
  11270. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11271. (replace-match "0" t nil nil 1))))))
  11272. (defun org-todo (&optional arg)
  11273. "Change the TODO state of an item.
  11274. The state of an item is given by a keyword at the start of the heading,
  11275. like
  11276. *** TODO Write paper
  11277. *** DONE Call mom
  11278. The different keywords are specified in the variable `org-todo-keywords'.
  11279. By default the available states are \"TODO\" and \"DONE\".
  11280. So for this example: when the item starts with TODO, it is changed to DONE.
  11281. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11282. DONE are present, add TODO at the beginning of the heading.
  11283. With \\[universal-argument] prefix arg, use completion to determine the new \
  11284. state.
  11285. With numeric prefix arg, switch to that state.
  11286. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11287. keywords (nextset).
  11288. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11289. With a numeric prefix arg of 0, inhibit note taking for the change.
  11290. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11291. When called through ELisp, arg is also interpreted in the following way:
  11292. 'none -> empty state
  11293. \"\"(empty string) -> switch to empty state
  11294. 'done -> switch to DONE
  11295. 'nextset -> switch to the next set of keywords
  11296. 'previousset -> switch to the previous set of keywords
  11297. \"WAITING\" -> switch to the specified keyword, but only if it
  11298. really is a member of `org-todo-keywords'."
  11299. (interactive "P")
  11300. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11301. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11302. 'region-start-level 'region))
  11303. org-loop-over-headlines-in-active-region)
  11304. (org-map-entries
  11305. `(org-todo ,arg)
  11306. org-loop-over-headlines-in-active-region
  11307. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11308. (if (equal arg '(16)) (setq arg 'nextset))
  11309. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11310. (let ((org-blocker-hook org-blocker-hook)
  11311. commentp
  11312. case-fold-search)
  11313. (when (equal arg '(64))
  11314. (setq arg nil org-blocker-hook nil))
  11315. (when (and org-blocker-hook
  11316. (or org-inhibit-blocking
  11317. (org-entry-get nil "NOBLOCKING")))
  11318. (setq org-blocker-hook nil))
  11319. (save-excursion
  11320. (catch 'exit
  11321. (org-back-to-heading t)
  11322. (when (org-in-commented-heading-p t)
  11323. (org-toggle-comment)
  11324. (setq commentp t))
  11325. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11326. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11327. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11328. (let* ((match-data (match-data))
  11329. (startpos (point-at-bol))
  11330. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11331. (org-log-done org-log-done)
  11332. (org-log-repeat org-log-repeat)
  11333. (org-todo-log-states org-todo-log-states)
  11334. (org-inhibit-logging
  11335. (if (equal arg 0)
  11336. (progn (setq arg nil) 'note) org-inhibit-logging))
  11337. (this (match-string 1))
  11338. (hl-pos (match-beginning 0))
  11339. (head (org-get-todo-sequence-head this))
  11340. (ass (assoc head org-todo-kwd-alist))
  11341. (interpret (nth 1 ass))
  11342. (done-word (nth 3 ass))
  11343. (final-done-word (nth 4 ass))
  11344. (org-last-state (or this ""))
  11345. (completion-ignore-case t)
  11346. (member (member this org-todo-keywords-1))
  11347. (tail (cdr member))
  11348. (org-state (cond
  11349. ((and org-todo-key-trigger
  11350. (or (and (equal arg '(4))
  11351. (eq org-use-fast-todo-selection 'prefix))
  11352. (and (not arg) org-use-fast-todo-selection
  11353. (not (eq org-use-fast-todo-selection
  11354. 'prefix)))))
  11355. ;; Use fast selection
  11356. (org-fast-todo-selection))
  11357. ((and (equal arg '(4))
  11358. (or (not org-use-fast-todo-selection)
  11359. (not org-todo-key-trigger)))
  11360. ;; Read a state with completion
  11361. (org-icompleting-read
  11362. "State: " (mapcar 'list org-todo-keywords-1)
  11363. nil t))
  11364. ((eq arg 'right)
  11365. (if this
  11366. (if tail (car tail) nil)
  11367. (car org-todo-keywords-1)))
  11368. ((eq arg 'left)
  11369. (if (equal member org-todo-keywords-1)
  11370. nil
  11371. (if this
  11372. (nth (- (length org-todo-keywords-1)
  11373. (length tail) 2)
  11374. org-todo-keywords-1)
  11375. (org-last org-todo-keywords-1))))
  11376. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11377. (setq arg nil))) ; hack to fall back to cycling
  11378. (arg
  11379. ;; user or caller requests a specific state
  11380. (cond
  11381. ((equal arg "") nil)
  11382. ((eq arg 'none) nil)
  11383. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11384. ((eq arg 'nextset)
  11385. (or (car (cdr (member head org-todo-heads)))
  11386. (car org-todo-heads)))
  11387. ((eq arg 'previousset)
  11388. (let ((org-todo-heads (reverse org-todo-heads)))
  11389. (or (car (cdr (member head org-todo-heads)))
  11390. (car org-todo-heads))))
  11391. ((car (member arg org-todo-keywords-1)))
  11392. ((stringp arg)
  11393. (user-error "State `%s' not valid in this file" arg))
  11394. ((nth (1- (prefix-numeric-value arg))
  11395. org-todo-keywords-1))))
  11396. ((null member) (or head (car org-todo-keywords-1)))
  11397. ((equal this final-done-word) nil) ;; -> make empty
  11398. ((null tail) nil) ;; -> first entry
  11399. ((memq interpret '(type priority))
  11400. (if (eq this-command last-command)
  11401. (car tail)
  11402. (if (> (length tail) 0)
  11403. (or done-word (car org-done-keywords))
  11404. nil)))
  11405. (t
  11406. (car tail))))
  11407. (org-state (or
  11408. (run-hook-with-args-until-success
  11409. 'org-todo-get-default-hook org-state org-last-state)
  11410. org-state))
  11411. (next (if org-state (concat " " org-state " ") " "))
  11412. (change-plist (list :type 'todo-state-change :from this :to org-state
  11413. :position startpos))
  11414. dolog now-done-p)
  11415. (when org-blocker-hook
  11416. (setq org-last-todo-state-is-todo
  11417. (not (member this org-done-keywords)))
  11418. (unless (save-excursion
  11419. (save-match-data
  11420. (org-with-wide-buffer
  11421. (run-hook-with-args-until-failure
  11422. 'org-blocker-hook change-plist))))
  11423. (if (org-called-interactively-p 'interactive)
  11424. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11425. this org-state org-block-entry-blocking)
  11426. ;; fail silently
  11427. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11428. this org-state org-block-entry-blocking)
  11429. (throw 'exit nil))))
  11430. (store-match-data match-data)
  11431. (replace-match next t t)
  11432. (cond ((equal this org-state)
  11433. (message "TODO state was already %s" (org-trim next)))
  11434. ((pos-visible-in-window-p hl-pos)
  11435. (message "TODO state changed to %s" (org-trim next))))
  11436. (unless head
  11437. (setq head (org-get-todo-sequence-head org-state)
  11438. ass (assoc head org-todo-kwd-alist)
  11439. interpret (nth 1 ass)
  11440. done-word (nth 3 ass)
  11441. final-done-word (nth 4 ass)))
  11442. (when (memq arg '(nextset previousset))
  11443. (message "Keyword-Set %d/%d: %s"
  11444. (- (length org-todo-sets) -1
  11445. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11446. (length org-todo-sets)
  11447. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11448. (setq org-last-todo-state-is-todo
  11449. (not (member org-state org-done-keywords)))
  11450. (setq now-done-p (and (member org-state org-done-keywords)
  11451. (not (member this org-done-keywords))))
  11452. (and logging (org-local-logging logging))
  11453. (when (and (or org-todo-log-states org-log-done)
  11454. (not (eq org-inhibit-logging t))
  11455. (not (memq arg '(nextset previousset))))
  11456. ;; we need to look at recording a time and note
  11457. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11458. (nth 2 (assoc this org-todo-log-states))))
  11459. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11460. (setq dolog 'time))
  11461. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11462. (and org-state
  11463. (member org-state org-not-done-keywords)
  11464. (not (member this org-not-done-keywords))))
  11465. ;; This is now a todo state and was not one before
  11466. ;; If there was a CLOSED time stamp, get rid of it.
  11467. (org-add-planning-info nil nil 'closed))
  11468. (when (and now-done-p org-log-done)
  11469. ;; It is now done, and it was not done before
  11470. (org-add-planning-info 'closed (org-current-effective-time))
  11471. (if (and (not dolog) (eq 'note org-log-done))
  11472. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11473. (when (and org-state dolog)
  11474. ;; This is a non-nil state, and we need to log it
  11475. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11476. ;; Fixup tag positioning
  11477. (org-todo-trigger-tag-changes org-state)
  11478. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11479. (when org-provide-todo-statistics
  11480. (org-update-parent-todo-statistics))
  11481. (run-hooks 'org-after-todo-state-change-hook)
  11482. (if (and arg (not (member org-state org-done-keywords)))
  11483. (setq head (org-get-todo-sequence-head org-state)))
  11484. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11485. ;; Do we need to trigger a repeat?
  11486. (when now-done-p
  11487. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11488. ;; This is for the agenda, take a snapshot of the headline.
  11489. (save-match-data
  11490. (setq org-agenda-headline-snapshot-before-repeat
  11491. (org-get-heading))))
  11492. (org-auto-repeat-maybe org-state))
  11493. ;; Fixup cursor location if close to the keyword
  11494. (if (and (outline-on-heading-p)
  11495. (not (bolp))
  11496. (save-excursion (beginning-of-line 1)
  11497. (looking-at org-todo-line-regexp))
  11498. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11499. (progn
  11500. (goto-char (or (match-end 2) (match-end 1)))
  11501. (and (looking-at " ") (just-one-space))))
  11502. (when org-trigger-hook
  11503. (save-excursion
  11504. (run-hook-with-args 'org-trigger-hook change-plist)))
  11505. (when commentp (org-toggle-comment))))))))
  11506. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11507. "Block turning an entry into a TODO, using the hierarchy.
  11508. This checks whether the current task should be blocked from state
  11509. changes. Such blocking occurs when:
  11510. 1. The task has children which are not all in a completed state.
  11511. 2. A task has a parent with the property :ORDERED:, and there
  11512. are siblings prior to the current task with incomplete
  11513. status.
  11514. 3. The parent of the task is blocked because it has siblings that should
  11515. be done first, or is child of a block grandparent TODO entry."
  11516. (if (not org-enforce-todo-dependencies)
  11517. t ; if locally turned off don't block
  11518. (catch 'dont-block
  11519. ;; If this is not a todo state change, or if this entry is already DONE,
  11520. ;; do not block
  11521. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11522. (member (plist-get change-plist :from)
  11523. (cons 'done org-done-keywords))
  11524. (member (plist-get change-plist :to)
  11525. (cons 'todo org-not-done-keywords))
  11526. (not (plist-get change-plist :to)))
  11527. (throw 'dont-block t))
  11528. ;; If this task has children, and any are undone, it's blocked
  11529. (save-excursion
  11530. (org-back-to-heading t)
  11531. (let ((this-level (funcall outline-level)))
  11532. (outline-next-heading)
  11533. (let ((child-level (funcall outline-level)))
  11534. (while (and (not (eobp))
  11535. (> child-level this-level))
  11536. ;; this todo has children, check whether they are all
  11537. ;; completed
  11538. (if (and (not (org-entry-is-done-p))
  11539. (org-entry-is-todo-p))
  11540. (progn (setq org-block-entry-blocking (org-get-heading))
  11541. (throw 'dont-block nil)))
  11542. (outline-next-heading)
  11543. (setq child-level (funcall outline-level))))))
  11544. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11545. ;; any previous siblings are undone, it's blocked
  11546. (save-excursion
  11547. (org-back-to-heading t)
  11548. (let* ((pos (point))
  11549. (parent-pos (and (org-up-heading-safe) (point))))
  11550. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11551. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11552. (forward-line 1)
  11553. (re-search-forward org-not-done-heading-regexp pos t))
  11554. (setq org-block-entry-blocking (match-string 0))
  11555. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11556. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11557. (while t
  11558. (goto-char parent-pos)
  11559. (if (not (looking-at org-not-done-heading-regexp))
  11560. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11561. (setq pos (point))
  11562. (setq parent-pos (and (org-up-heading-safe) (point)))
  11563. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11564. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11565. (forward-line 1)
  11566. (re-search-forward org-not-done-heading-regexp pos t)
  11567. (setq org-block-entry-blocking (org-get-heading)))
  11568. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11569. (defcustom org-track-ordered-property-with-tag nil
  11570. "Should the ORDERED property also be shown as a tag?
  11571. The ORDERED property decides if an entry should require subtasks to be
  11572. completed in sequence. Since a property is not very visible, setting
  11573. this option means that toggling the ORDERED property with the command
  11574. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11575. not relevant for the behavior, but it makes things more visible.
  11576. Note that toggling the tag with tags commands will not change the property
  11577. and therefore not influence behavior!
  11578. This can be t, meaning the tag ORDERED should be used, It can also be a
  11579. string to select a different tag for this task."
  11580. :group 'org-todo
  11581. :type '(choice
  11582. (const :tag "No tracking" nil)
  11583. (const :tag "Track with ORDERED tag" t)
  11584. (string :tag "Use other tag")))
  11585. (defun org-toggle-ordered-property ()
  11586. "Toggle the ORDERED property of the current entry.
  11587. For better visibility, you can track the value of this property with a tag.
  11588. See variable `org-track-ordered-property-with-tag'."
  11589. (interactive)
  11590. (let* ((t1 org-track-ordered-property-with-tag)
  11591. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11592. (save-excursion
  11593. (org-back-to-heading)
  11594. (if (org-entry-get nil "ORDERED")
  11595. (progn
  11596. (org-delete-property "ORDERED")
  11597. (and tag (org-toggle-tag tag 'off))
  11598. (message "Subtasks can be completed in arbitrary order"))
  11599. (org-entry-put nil "ORDERED" "t")
  11600. (and tag (org-toggle-tag tag 'on))
  11601. (message "Subtasks must be completed in sequence")))))
  11602. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11603. (defun org-block-todo-from-checkboxes (change-plist)
  11604. "Block turning an entry into a TODO, using checkboxes.
  11605. This checks whether the current task should be blocked from state
  11606. changes because there are unchecked boxes in this entry."
  11607. (if (not org-enforce-todo-checkbox-dependencies)
  11608. t ; if locally turned off don't block
  11609. (catch 'dont-block
  11610. ;; If this is not a todo state change, or if this entry is already DONE,
  11611. ;; do not block
  11612. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11613. (member (plist-get change-plist :from)
  11614. (cons 'done org-done-keywords))
  11615. (member (plist-get change-plist :to)
  11616. (cons 'todo org-not-done-keywords))
  11617. (not (plist-get change-plist :to)))
  11618. (throw 'dont-block t))
  11619. ;; If this task has checkboxes that are not checked, it's blocked
  11620. (save-excursion
  11621. (org-back-to-heading t)
  11622. (let ((beg (point)) end)
  11623. (outline-next-heading)
  11624. (setq end (point))
  11625. (goto-char beg)
  11626. (if (org-list-search-forward
  11627. (concat (org-item-beginning-re)
  11628. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11629. "\\[[- ]\\]")
  11630. end t)
  11631. (progn
  11632. (if (boundp 'org-blocked-by-checkboxes)
  11633. (setq org-blocked-by-checkboxes t))
  11634. (throw 'dont-block nil)))))
  11635. t))) ; do not block
  11636. (defun org-entry-blocked-p ()
  11637. "Non-nil if entry at point is blocked."
  11638. (and (not (org-entry-get nil "NOBLOCKING"))
  11639. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11640. (not (run-hook-with-args-until-failure
  11641. 'org-blocker-hook
  11642. (list :type 'todo-state-change
  11643. :position (point)
  11644. :from 'todo
  11645. :to 'done)))))
  11646. (defun org-update-statistics-cookies (all)
  11647. "Update the statistics cookie, either from TODO or from checkboxes.
  11648. This should be called with the cursor in a line with a statistics cookie."
  11649. (interactive "P")
  11650. (if all
  11651. (progn
  11652. (org-update-checkbox-count 'all)
  11653. (org-map-entries 'org-update-parent-todo-statistics))
  11654. (if (not (org-at-heading-p))
  11655. (org-update-checkbox-count)
  11656. (let ((pos (point-marker))
  11657. end l1 l2)
  11658. (ignore-errors (org-back-to-heading t))
  11659. (if (not (org-at-heading-p))
  11660. (org-update-checkbox-count)
  11661. (setq l1 (org-outline-level))
  11662. (setq end (save-excursion
  11663. (outline-next-heading)
  11664. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11665. (point)))
  11666. (if (and (save-excursion
  11667. (re-search-forward
  11668. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11669. (not (save-excursion (re-search-forward
  11670. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11671. (org-update-checkbox-count)
  11672. (if (and l2 (> l2 l1))
  11673. (progn
  11674. (goto-char end)
  11675. (org-update-parent-todo-statistics))
  11676. (goto-char pos)
  11677. (beginning-of-line 1)
  11678. (while (re-search-forward
  11679. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11680. (point-at-eol) t)
  11681. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11682. (goto-char pos)
  11683. (move-marker pos nil)))))
  11684. (defvar org-entry-property-inherited-from) ;; defined below
  11685. (defun org-update-parent-todo-statistics ()
  11686. "Update any statistics cookie in the parent of the current headline.
  11687. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11688. the entire subtree and this will travel up the hierarchy and update
  11689. statistics everywhere."
  11690. (let* ((prop (save-excursion (org-up-heading-safe)
  11691. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11692. (recursive (or (not org-hierarchical-todo-statistics)
  11693. (and prop (string-match "\\<recursive\\>" prop))))
  11694. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11695. 0))
  11696. (first t)
  11697. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11698. level ltoggle l1 new ndel
  11699. (cnt-all 0) (cnt-done 0) is-percent kwd
  11700. checkbox-beg ov ovs ove cookie-present)
  11701. (catch 'exit
  11702. (save-excursion
  11703. (beginning-of-line 1)
  11704. (setq ltoggle (funcall outline-level))
  11705. ;; Three situations are to consider:
  11706. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11707. ;; to the top-level ancestor on the headline;
  11708. ;; 2. If parent has "recursive" property, repeat up to the
  11709. ;; headline setting that property, taking inheritance into
  11710. ;; account;
  11711. ;; 3. Else, move up to direct parent and proceed only once.
  11712. (while (and (setq level (org-up-heading-safe))
  11713. (or recursive first)
  11714. (>= (point) lim))
  11715. (setq first nil cookie-present nil)
  11716. (unless (and level
  11717. (not (string-match
  11718. "\\<checkbox\\>"
  11719. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11720. "")))))
  11721. (throw 'exit nil))
  11722. (while (re-search-forward box-re (point-at-eol) t)
  11723. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11724. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11725. (save-match-data
  11726. (unless (outline-next-heading) (throw 'exit nil))
  11727. (while (and (looking-at org-complex-heading-regexp)
  11728. (> (setq l1 (length (match-string 1))) level))
  11729. (setq kwd (and (or recursive (= l1 ltoggle))
  11730. (match-string 2)))
  11731. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11732. (and (eq org-provide-todo-statistics t)
  11733. (or (member kwd org-done-keywords)))
  11734. (and (listp org-provide-todo-statistics)
  11735. (stringp (car org-provide-todo-statistics))
  11736. (or (member kwd org-provide-todo-statistics)
  11737. (member kwd org-done-keywords)))
  11738. (and (listp org-provide-todo-statistics)
  11739. (listp (car org-provide-todo-statistics))
  11740. (or (member kwd (car org-provide-todo-statistics))
  11741. (and (member kwd org-done-keywords)
  11742. (member kwd (cadr org-provide-todo-statistics))))))
  11743. (setq cnt-all (1+ cnt-all))
  11744. (if (eq org-provide-todo-statistics t)
  11745. (and kwd (setq cnt-all (1+ cnt-all)))))
  11746. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11747. (member kwd org-done-keywords))
  11748. (and (listp org-provide-todo-statistics)
  11749. (listp (car org-provide-todo-statistics))
  11750. (member kwd org-done-keywords)
  11751. (member kwd (cadr org-provide-todo-statistics)))
  11752. (and (listp org-provide-todo-statistics)
  11753. (stringp (car org-provide-todo-statistics))
  11754. (member kwd org-done-keywords)))
  11755. (setq cnt-done (1+ cnt-done)))
  11756. (outline-next-heading)))
  11757. (setq new
  11758. (if is-percent
  11759. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11760. (max 1 cnt-all)))
  11761. (format "[%d/%d]" cnt-done cnt-all))
  11762. ndel (- (match-end 0) checkbox-beg))
  11763. ;; handle overlays when updating cookie from column view
  11764. (when (setq ov (car (overlays-at checkbox-beg)))
  11765. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11766. (delete-overlay ov))
  11767. (goto-char checkbox-beg)
  11768. (insert new)
  11769. (delete-region (point) (+ (point) ndel))
  11770. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11771. (when ov (move-overlay ov ovs ove)))
  11772. (when cookie-present
  11773. (run-hook-with-args 'org-after-todo-statistics-hook
  11774. cnt-done (- cnt-all cnt-done))))))
  11775. (run-hooks 'org-todo-statistics-hook)))
  11776. (defvar org-after-todo-statistics-hook nil
  11777. "Hook that is called after a TODO statistics cookie has been updated.
  11778. Each function is called with two arguments: the number of not-done entries
  11779. and the number of done entries.
  11780. For example, the following function, when added to this hook, will switch
  11781. an entry to DONE when all children are done, and back to TODO when new
  11782. entries are set to a TODO status. Note that this hook is only called
  11783. when there is a statistics cookie in the headline!
  11784. (defun org-summary-todo (n-done n-not-done)
  11785. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11786. (let (org-log-done org-log-states) ; turn off logging
  11787. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11788. ")
  11789. (defvar org-todo-statistics-hook nil
  11790. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11791. This hook runs even if there is no statistics cookie present, in which case
  11792. `org-after-todo-statistics-hook' would not run.")
  11793. (defun org-todo-trigger-tag-changes (state)
  11794. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11795. (let ((l org-todo-state-tags-triggers)
  11796. changes)
  11797. (when (or (not state) (equal state ""))
  11798. (setq changes (append changes (cdr (assoc "" l)))))
  11799. (when (and (stringp state) (> (length state) 0))
  11800. (setq changes (append changes (cdr (assoc state l)))))
  11801. (when (member state org-not-done-keywords)
  11802. (setq changes (append changes (cdr (assoc 'todo l)))))
  11803. (when (member state org-done-keywords)
  11804. (setq changes (append changes (cdr (assoc 'done l)))))
  11805. (dolist (c changes)
  11806. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11807. (defun org-local-logging (value)
  11808. "Get logging settings from a property VALUE."
  11809. ;; Directly set the variables, they are already local.
  11810. (setq org-log-done nil
  11811. org-log-repeat nil
  11812. org-todo-log-states nil)
  11813. (dolist (w (org-split-string value))
  11814. (let (a)
  11815. (cond
  11816. ((setq a (assoc w org-startup-options))
  11817. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11818. (set (nth 1 a) (nth 2 a))))
  11819. ((setq a (org-extract-log-state-settings w))
  11820. (and (member (car a) org-todo-keywords-1)
  11821. (push a org-todo-log-states)))))))
  11822. (defun org-get-todo-sequence-head (kwd)
  11823. "Return the head of the TODO sequence to which KWD belongs.
  11824. If KWD is not set, check if there is a text property remembering the
  11825. right sequence."
  11826. (let (p)
  11827. (cond
  11828. ((not kwd)
  11829. (or (get-text-property (point-at-bol) 'org-todo-head)
  11830. (progn
  11831. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11832. nil (point-at-eol)))
  11833. (get-text-property p 'org-todo-head))))
  11834. ((not (member kwd org-todo-keywords-1))
  11835. (car org-todo-keywords-1))
  11836. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11837. (defun org-fast-todo-selection ()
  11838. "Fast TODO keyword selection with single keys.
  11839. Returns the new TODO keyword, or nil if no state change should occur."
  11840. (let* ((fulltable org-todo-key-alist)
  11841. (done-keywords org-done-keywords) ;; needed for the faces.
  11842. (maxlen (apply 'max (mapcar
  11843. (lambda (x)
  11844. (if (stringp (car x)) (string-width (car x)) 0))
  11845. fulltable)))
  11846. (expert nil)
  11847. (fwidth (+ maxlen 3 1 3))
  11848. (ncol (/ (- (window-width) 4) fwidth))
  11849. tg cnt e c tbl
  11850. groups ingroup)
  11851. (save-excursion
  11852. (save-window-excursion
  11853. (if expert
  11854. (set-buffer (get-buffer-create " *Org todo*"))
  11855. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11856. (erase-buffer)
  11857. (org-set-local 'org-done-keywords done-keywords)
  11858. (setq tbl fulltable cnt 0)
  11859. (while (setq e (pop tbl))
  11860. (cond
  11861. ((equal e '(:startgroup))
  11862. (push '() groups) (setq ingroup t)
  11863. (when (not (= cnt 0))
  11864. (setq cnt 0)
  11865. (insert "\n"))
  11866. (insert "{ "))
  11867. ((equal e '(:endgroup))
  11868. (setq ingroup nil cnt 0)
  11869. (insert "}\n"))
  11870. ((equal e '(:newline))
  11871. (when (not (= cnt 0))
  11872. (setq cnt 0)
  11873. (insert "\n")
  11874. (setq e (car tbl))
  11875. (while (equal (car tbl) '(:newline))
  11876. (insert "\n")
  11877. (setq tbl (cdr tbl)))))
  11878. (t
  11879. (setq tg (car e) c (cdr e))
  11880. (if ingroup (push tg (car groups)))
  11881. (setq tg (org-add-props tg nil 'face
  11882. (org-get-todo-face tg)))
  11883. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11884. (insert "[" c "] " tg (make-string
  11885. (- fwidth 4 (length tg)) ?\ ))
  11886. (when (= (setq cnt (1+ cnt)) ncol)
  11887. (insert "\n")
  11888. (if ingroup (insert " "))
  11889. (setq cnt 0)))))
  11890. (insert "\n")
  11891. (goto-char (point-min))
  11892. (if (not expert) (org-fit-window-to-buffer))
  11893. (message "[a-z..]:Set [SPC]:clear")
  11894. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11895. (cond
  11896. ((or (= c ?\C-g)
  11897. (and (= c ?q) (not (rassoc c fulltable))))
  11898. (setq quit-flag t))
  11899. ((= c ?\ ) nil)
  11900. ((setq e (rassoc c fulltable) tg (car e))
  11901. tg)
  11902. (t (setq quit-flag t)))))))
  11903. (defun org-entry-is-todo-p ()
  11904. (member (org-get-todo-state) org-not-done-keywords))
  11905. (defun org-entry-is-done-p ()
  11906. (member (org-get-todo-state) org-done-keywords))
  11907. (defun org-get-todo-state ()
  11908. "Return the TODO keyword of the current subtree."
  11909. (save-excursion
  11910. (org-back-to-heading t)
  11911. (and (looking-at org-todo-line-regexp)
  11912. (match-end 2)
  11913. (match-string 2))))
  11914. (defun org-at-date-range-p (&optional inactive-ok)
  11915. "Non-nil if point is inside a date range.
  11916. When optional argument INACTIVE-OK is non-nil, also consider
  11917. inactive time ranges.
  11918. When this function returns a non-nil value, match data is set
  11919. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11920. on INACTIVE-OK."
  11921. (interactive)
  11922. (save-excursion
  11923. (catch 'exit
  11924. (let ((pos (point)))
  11925. (skip-chars-backward "^[<\r\n")
  11926. (skip-chars-backward "<[")
  11927. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11928. (>= (match-end 0) pos)
  11929. (throw 'exit t))
  11930. (skip-chars-backward "^<[\r\n")
  11931. (skip-chars-backward "<[")
  11932. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11933. (>= (match-end 0) pos)
  11934. (throw 'exit t)))
  11935. nil)))
  11936. (defun org-get-repeat (&optional tagline)
  11937. "Check if there is a deadline/schedule with repeater in this entry."
  11938. (save-match-data
  11939. (save-excursion
  11940. (org-back-to-heading t)
  11941. (and (re-search-forward (if tagline
  11942. (concat tagline "\\s-*" org-repeat-re)
  11943. org-repeat-re)
  11944. (org-entry-end-position) t)
  11945. (match-string-no-properties 1)))))
  11946. (defvar org-last-changed-timestamp)
  11947. (defvar org-last-inserted-timestamp)
  11948. (defvar org-log-post-message)
  11949. (defvar org-log-note-purpose)
  11950. (defvar org-log-note-how nil)
  11951. (defvar org-log-note-extra)
  11952. (defun org-auto-repeat-maybe (done-word)
  11953. "Check if the current headline contains a repeated deadline/schedule.
  11954. If yes, set TODO state back to what it was and change the base date
  11955. of repeating deadline/scheduled time stamps to new date.
  11956. This function is run automatically after each state change to a DONE state."
  11957. ;; last-state is dynamically scoped into this function
  11958. (let* ((repeat (org-get-repeat))
  11959. (aa (assoc org-last-state org-todo-kwd-alist))
  11960. (interpret (nth 1 aa))
  11961. (head (nth 2 aa))
  11962. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11963. (msg "Entry repeats: ")
  11964. (org-log-done nil)
  11965. (org-todo-log-states nil)
  11966. re type n what ts time to-state)
  11967. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11968. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11969. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11970. org-todo-repeat-to-state))
  11971. (unless (and to-state (member to-state org-todo-keywords-1))
  11972. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11973. (org-todo to-state)
  11974. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11975. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11976. (org-time-stamp-format t t))))
  11977. (when org-log-repeat
  11978. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11979. (memq 'org-add-log-note post-command-hook))
  11980. ;; OK, we are already setup for some record
  11981. (if (eq org-log-repeat 'note)
  11982. ;; make sure we take a note, not only a time stamp
  11983. (setq org-log-note-how 'note))
  11984. ;; Set up for taking a record
  11985. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11986. org-last-state
  11987. 'findpos org-log-repeat)))
  11988. (org-back-to-heading t)
  11989. (org-add-planning-info nil nil 'closed)
  11990. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11991. org-deadline-time-regexp "\\)\\|\\("
  11992. org-ts-regexp "\\)"))
  11993. (while (re-search-forward
  11994. re (save-excursion (outline-next-heading) (point)) t)
  11995. (setq type (if (match-end 1) org-scheduled-string
  11996. (if (match-end 3) org-deadline-string "Plain:"))
  11997. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11998. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11999. (setq n (string-to-number (match-string 2 ts))
  12000. what (match-string 3 ts))
  12001. (if (equal what "w") (setq n (* n 7) what "d"))
  12002. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12003. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12004. ;; Preparation, see if we need to modify the start date for the change
  12005. (when (match-end 1)
  12006. (setq time (save-match-data (org-time-string-to-time ts)))
  12007. (cond
  12008. ((equal (match-string 1 ts) ".")
  12009. ;; Shift starting date to today
  12010. (org-timestamp-change
  12011. (- (org-today) (time-to-days time))
  12012. 'day))
  12013. ((equal (match-string 1 ts) "+")
  12014. (let ((nshiftmax 10) (nshift 0))
  12015. (while (or (= nshift 0)
  12016. (<= (time-to-days time)
  12017. (time-to-days (current-time))))
  12018. (when (= (incf nshift) nshiftmax)
  12019. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12020. (user-error "Abort")))
  12021. (org-timestamp-change n (cdr (assoc what whata)))
  12022. (org-at-timestamp-p t)
  12023. (setq ts (match-string 1))
  12024. (setq time (save-match-data (org-time-string-to-time ts)))))
  12025. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12026. ;; rematch, so that we have everything in place for the real shift
  12027. (org-at-timestamp-p t)
  12028. (setq ts (match-string 1))
  12029. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12030. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12031. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12032. (setq org-log-post-message msg)
  12033. (message "%s" msg))))
  12034. (defun org-show-todo-tree (arg)
  12035. "Make a compact tree which shows all headlines marked with TODO.
  12036. The tree will show the lines where the regexp matches, and all higher
  12037. headlines above the match.
  12038. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12039. With a numeric prefix N, construct a sparse tree for the Nth element
  12040. of `org-todo-keywords-1'."
  12041. (interactive "P")
  12042. (let ((case-fold-search nil)
  12043. (kwd-re
  12044. (cond ((null arg) org-not-done-regexp)
  12045. ((equal arg '(4))
  12046. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12047. (mapcar 'list org-todo-keywords-1))))
  12048. (concat "\\("
  12049. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12050. "\\)\\>")))
  12051. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12052. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12053. org-todo-keywords-1)))
  12054. (t (user-error "Invalid prefix argument: %s" arg)))))
  12055. (message "%d TODO entries found"
  12056. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12057. (defun org-deadline (arg &optional time)
  12058. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12059. With one universal prefix argument, remove any deadline from the item.
  12060. With two universal prefix arguments, prompt for a warning delay.
  12061. With argument TIME, set the deadline at the corresponding date. TIME
  12062. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12063. (interactive "P")
  12064. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12065. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12066. 'region-start-level 'region))
  12067. org-loop-over-headlines-in-active-region)
  12068. (org-map-entries
  12069. `(org-deadline ',arg ,time)
  12070. org-loop-over-headlines-in-active-region
  12071. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12072. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12073. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12074. (repeater (and old-date
  12075. (string-match
  12076. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12077. old-date)
  12078. (match-string 1 old-date))))
  12079. (cond
  12080. ((equal arg '(4))
  12081. (when (and old-date org-log-redeadline)
  12082. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12083. org-log-redeadline))
  12084. (org-remove-timestamp-with-keyword org-deadline-string)
  12085. (message "Item no longer has a deadline."))
  12086. ((equal arg '(16))
  12087. (save-excursion
  12088. (org-back-to-heading t)
  12089. (if (re-search-forward
  12090. org-deadline-time-regexp
  12091. (save-excursion (outline-next-heading) (point)) t)
  12092. (let* ((rpl0 (match-string 1))
  12093. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12094. (replace-match
  12095. (concat org-deadline-string
  12096. " <" rpl
  12097. (format " -%dd"
  12098. (abs
  12099. (- (time-to-days
  12100. (save-match-data
  12101. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12102. (time-to-days old-date-time))))
  12103. ">") t t))
  12104. (user-error "No deadline information to update"))))
  12105. (t
  12106. (org-add-planning-info 'deadline time 'closed)
  12107. (when (and old-date org-log-redeadline
  12108. (not (equal old-date
  12109. (substring org-last-inserted-timestamp 1 -1))))
  12110. (org-add-log-setup 'redeadline nil old-date 'findpos
  12111. org-log-redeadline))
  12112. (when repeater
  12113. (save-excursion
  12114. (org-back-to-heading t)
  12115. (when (re-search-forward (concat org-deadline-string " "
  12116. org-last-inserted-timestamp)
  12117. (save-excursion
  12118. (outline-next-heading) (point)) t)
  12119. (goto-char (1- (match-end 0)))
  12120. (insert " " repeater)
  12121. (setq org-last-inserted-timestamp
  12122. (concat (substring org-last-inserted-timestamp 0 -1)
  12123. " " repeater
  12124. (substring org-last-inserted-timestamp -1))))))
  12125. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12126. (defun org-schedule (arg &optional time)
  12127. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12128. With one universal prefix argument, remove any scheduling date from the item.
  12129. With two universal prefix arguments, prompt for a delay cookie.
  12130. With argument TIME, scheduled at the corresponding date. TIME can
  12131. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12132. (interactive "P")
  12133. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12134. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12135. 'region-start-level 'region))
  12136. org-loop-over-headlines-in-active-region)
  12137. (org-map-entries
  12138. `(org-schedule ',arg ,time)
  12139. org-loop-over-headlines-in-active-region
  12140. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12141. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12142. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12143. (repeater (and old-date
  12144. (string-match
  12145. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12146. old-date)
  12147. (match-string 1 old-date))))
  12148. (cond
  12149. ((equal arg '(4))
  12150. (progn
  12151. (when (and old-date org-log-reschedule)
  12152. (org-add-log-setup 'delschedule nil old-date 'findpos
  12153. org-log-reschedule))
  12154. (org-remove-timestamp-with-keyword org-scheduled-string)
  12155. (message "Item is no longer scheduled.")))
  12156. ((equal arg '(16))
  12157. (save-excursion
  12158. (org-back-to-heading t)
  12159. (if (re-search-forward
  12160. org-scheduled-time-regexp
  12161. (save-excursion (outline-next-heading) (point)) t)
  12162. (let* ((rpl0 (match-string 1))
  12163. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12164. (replace-match
  12165. (concat org-scheduled-string
  12166. " <" rpl
  12167. (format " -%dd"
  12168. (abs
  12169. (- (time-to-days
  12170. (save-match-data
  12171. (org-read-date nil t nil "Delay until" old-date-time)))
  12172. (time-to-days old-date-time))))
  12173. ">") t t))
  12174. (user-error "No scheduled information to update"))))
  12175. (t
  12176. (org-add-planning-info 'scheduled time 'closed)
  12177. (when (and old-date org-log-reschedule
  12178. (not (equal old-date
  12179. (substring org-last-inserted-timestamp 1 -1))))
  12180. (org-add-log-setup 'reschedule nil old-date 'findpos
  12181. org-log-reschedule))
  12182. (when repeater
  12183. (save-excursion
  12184. (org-back-to-heading t)
  12185. (when (re-search-forward (concat org-scheduled-string " "
  12186. org-last-inserted-timestamp)
  12187. (save-excursion
  12188. (outline-next-heading) (point)) t)
  12189. (goto-char (1- (match-end 0)))
  12190. (insert " " repeater)
  12191. (setq org-last-inserted-timestamp
  12192. (concat (substring org-last-inserted-timestamp 0 -1)
  12193. " " repeater
  12194. (substring org-last-inserted-timestamp -1))))))
  12195. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12196. (defun org-get-scheduled-time (pom &optional inherit)
  12197. "Get the scheduled time as a time tuple, of a format suitable
  12198. for calling org-schedule with, or if there is no scheduling,
  12199. returns nil."
  12200. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12201. (when time
  12202. (apply 'encode-time (org-parse-time-string time)))))
  12203. (defun org-get-deadline-time (pom &optional inherit)
  12204. "Get the deadline as a time tuple, of a format suitable for
  12205. calling org-deadline with, or if there is no scheduling, returns
  12206. nil."
  12207. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12208. (when time
  12209. (apply 'encode-time (org-parse-time-string time)))))
  12210. (defun org-remove-timestamp-with-keyword (keyword)
  12211. "Remove all time stamps with KEYWORD in the current entry."
  12212. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12213. beg)
  12214. (save-excursion
  12215. (org-back-to-heading t)
  12216. (setq beg (point))
  12217. (outline-next-heading)
  12218. (while (re-search-backward re beg t)
  12219. (replace-match "")
  12220. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12221. (equal (char-before) ?\ ))
  12222. (backward-delete-char 1)
  12223. (if (string-match "^[ \t]*$" (buffer-substring
  12224. (point-at-bol) (point-at-eol)))
  12225. (delete-region (point-at-bol)
  12226. (min (point-max) (1+ (point-at-eol))))))))))
  12227. (defvar org-time-was-given) ; dynamically scoped parameter
  12228. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12229. (defun org-at-planning-p ()
  12230. "Non-nil when point is on a planning info line."
  12231. ;; This is as accurate and faster than `org-element-at-point' since
  12232. ;; planning info location is fixed in the section.
  12233. (org-with-wide-buffer
  12234. (beginning-of-line)
  12235. (and (org-looking-at-p org-planning-line-re)
  12236. (eq (point)
  12237. (ignore-errors
  12238. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12239. (org-back-to-heading t)
  12240. (org-with-limited-levels (org-back-to-heading t)))
  12241. (line-beginning-position 2))))))
  12242. (defun org-add-planning-info (what &optional time &rest remove)
  12243. "Insert new timestamp with keyword in the planning line.
  12244. WHAT indicates what kind of time stamp to add. It is a symbol
  12245. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12246. the time to use. If none is given, the user is prompted for
  12247. a date. REMOVE indicates what kind of entries to remove. An old
  12248. WHAT entry will also be removed."
  12249. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12250. (catch 'exit
  12251. (when (and (memq what '(scheduled deadline))
  12252. (or (not time)
  12253. (and (stringp time)
  12254. (string-match "^[-+]+[0-9]" time))))
  12255. ;; Try to get a default date/time from existing timestamp
  12256. (save-excursion
  12257. (org-back-to-heading t)
  12258. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12259. (when (re-search-forward (if (eq what 'scheduled)
  12260. org-scheduled-time-regexp
  12261. org-deadline-time-regexp)
  12262. end t)
  12263. (setq ts (match-string 1)
  12264. default-time (apply 'encode-time (org-parse-time-string ts))
  12265. default-input (and ts (org-get-compact-tod ts)))))))
  12266. (when what
  12267. (setq time
  12268. (if (stringp time)
  12269. ;; This is a string (relative or absolute), set
  12270. ;; proper date.
  12271. (apply #'encode-time
  12272. (org-read-date-analyze
  12273. time default-time (decode-time default-time)))
  12274. ;; If necessary, get the time from the user
  12275. (or time (org-read-date nil 'to-time nil nil
  12276. default-time default-input)))))
  12277. (org-with-wide-buffer
  12278. (org-back-to-heading t)
  12279. (forward-line)
  12280. (unless (bolp) (insert "\n"))
  12281. (cond ((org-looking-at-p org-planning-line-re)
  12282. ;; Move to current indentation.
  12283. (skip-chars-forward " \t")
  12284. ;; Check if we have to remove something.
  12285. (dolist (type (if what (cons what remove) remove))
  12286. (save-excursion
  12287. (when (re-search-forward
  12288. (concat
  12289. " *"
  12290. (case type
  12291. (closed org-closed-time-regexp)
  12292. (deadline org-deadline-time-regexp)
  12293. (scheduled org-scheduled-time-regexp)
  12294. (otherwise
  12295. (error "Invalid planning type: %s" type))))
  12296. (line-end-position) t)
  12297. (replace-match "")
  12298. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12299. (when (and (not what) (eq type 'closed))
  12300. (save-excursion
  12301. (beginning-of-line)
  12302. (when (looking-at "[ \t]*$")
  12303. (delete-region (point)
  12304. (line-beginning-position 2)))))))
  12305. ;; Remove leading white spaces.
  12306. (when (looking-at "[ \t]+") (replace-match ""))))
  12307. ((not what) (throw 'exit nil)) ; Nothing to do.
  12308. (t (insert-before-markers "\n")
  12309. (backward-char 1)
  12310. (when org-adapt-indentation
  12311. (org-indent-to-column (1+ (org-outline-level))))))
  12312. (when what
  12313. ;; Insert planning keyword.
  12314. (insert (case what
  12315. (closed org-closed-string)
  12316. (deadline org-deadline-string)
  12317. (scheduled org-scheduled-string)
  12318. (otherwise (error "Invalid planning type: %s" what)))
  12319. " ")
  12320. ;; Insert associated timestamp.
  12321. (let ((ts (org-insert-time-stamp
  12322. time
  12323. (or org-time-was-given
  12324. (and (eq what 'closed) org-log-done-with-time))
  12325. (eq what 'closed)
  12326. nil nil (list org-end-time-was-given))))
  12327. (unless (eolp) (insert " "))
  12328. ts))))))
  12329. (defvar org-log-note-marker (make-marker))
  12330. (defvar org-log-note-purpose nil)
  12331. (defvar org-log-note-state nil)
  12332. (defvar org-log-note-previous-state nil)
  12333. (defvar org-log-note-extra nil)
  12334. (defvar org-log-note-window-configuration nil)
  12335. (defvar org-log-note-return-to (make-marker))
  12336. (defvar org-log-note-effective-time nil
  12337. "Remembered current time so that dynamically scoped
  12338. `org-extend-today-until' affects tha timestamps in state change
  12339. log")
  12340. (defvar org-log-post-message nil
  12341. "Message to be displayed after a log note has been stored.
  12342. The auto-repeater uses this.")
  12343. (defun org-add-note ()
  12344. "Add a note to the current entry.
  12345. This is done in the same way as adding a state change note."
  12346. (interactive)
  12347. (org-add-log-setup 'note nil nil 'findpos nil))
  12348. (defun org-log-beginning (&optional create)
  12349. "Return expected start of log notes in current entry.
  12350. When optional argument CREATE is non-nil, the function creates
  12351. a drawer to store notes, if necessary. Returned position ignores
  12352. narrowing."
  12353. (org-with-wide-buffer
  12354. (org-end-of-meta-data)
  12355. (let ((end (if (org-at-heading-p) (point)
  12356. (save-excursion (outline-next-heading) (point))))
  12357. (drawer (org-log-into-drawer)))
  12358. (cond
  12359. (drawer
  12360. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12361. (case-fold-search t))
  12362. (catch 'exit
  12363. ;; Try to find existing drawer.
  12364. (while (re-search-forward regexp end t)
  12365. (let ((element (org-element-at-point)))
  12366. (when (eq (org-element-type element) 'drawer)
  12367. (let ((cend (org-element-property :contents-end element)))
  12368. (when (and (not org-log-states-order-reversed) cend)
  12369. (goto-char cend)))
  12370. (throw 'exit nil))))
  12371. ;; No drawer found. Create one, if permitted.
  12372. (when create
  12373. (unless (bolp) (insert "\n"))
  12374. (let ((beg (point)))
  12375. (insert ":" drawer ":\n:END:\n")
  12376. (org-indent-region beg (point)))
  12377. (end-of-line -1)))))
  12378. (org-log-state-notes-insert-after-drawers
  12379. (while (and (looking-at org-drawer-regexp)
  12380. (progn (goto-char (match-end 0))
  12381. (re-search-forward org-property-end-re end t)))
  12382. (forward-line)))))
  12383. (if (bolp) (point) (line-beginning-position 2))))
  12384. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12385. "Set up the post command hook to take a note.
  12386. If this is about to TODO state change, the new state is expected in STATE.
  12387. When FINDPOS is non-nil, find the correct position for the note in
  12388. the current entry. If not, assume that it can be inserted at point.
  12389. HOW is an indicator what kind of note should be created.
  12390. EXTRA is additional text that will be inserted into the notes buffer."
  12391. (org-with-wide-buffer
  12392. (when findpos
  12393. (goto-char (org-log-beginning t))
  12394. (unless org-log-states-order-reversed
  12395. (org-skip-over-state-notes)
  12396. (skip-chars-backward " \t\n\r")
  12397. (forward-line)))
  12398. (move-marker org-log-note-marker (point))
  12399. ;; Preserve position even if a property drawer is inserted in the
  12400. ;; process.
  12401. (set-marker-insertion-type org-log-note-marker t)
  12402. (setq org-log-note-purpose purpose
  12403. org-log-note-state state
  12404. org-log-note-previous-state prev-state
  12405. org-log-note-how how
  12406. org-log-note-extra extra
  12407. org-log-note-effective-time (org-current-effective-time))
  12408. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12409. (defun org-skip-over-state-notes ()
  12410. "Skip past the list of State notes in an entry."
  12411. (when (ignore-errors (goto-char (org-in-item-p)))
  12412. (let* ((struct (org-list-struct))
  12413. (prevs (org-list-prevs-alist struct))
  12414. (regexp
  12415. (concat "[ \t]*- +"
  12416. (replace-regexp-in-string
  12417. " +" " +"
  12418. (org-replace-escapes
  12419. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12420. `(("%d" . ,org-ts-regexp-inactive)
  12421. ("%D" . ,org-ts-regexp)
  12422. ("%s" . "\"\\S-+\"")
  12423. ("%S" . "\"\\S-+\"")
  12424. ("%t" . ,org-ts-regexp-inactive)
  12425. ("%T" . ,org-ts-regexp)
  12426. ("%u" . ".*?")
  12427. ("%U" . ".*?")))))))
  12428. (while (org-looking-at-p regexp)
  12429. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12430. (org-list-get-item-end (point) struct)))))))
  12431. (defun org-add-log-note (&optional purpose)
  12432. "Pop up a window for taking a note, and add this note later at point."
  12433. (remove-hook 'post-command-hook 'org-add-log-note)
  12434. (setq org-log-note-window-configuration (current-window-configuration))
  12435. (delete-other-windows)
  12436. (move-marker org-log-note-return-to (point))
  12437. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12438. (goto-char org-log-note-marker)
  12439. (org-switch-to-buffer-other-window "*Org Note*")
  12440. (erase-buffer)
  12441. (if (memq org-log-note-how '(time state))
  12442. (let (current-prefix-arg) (org-store-log-note))
  12443. (let ((org-inhibit-startup t)) (org-mode))
  12444. (insert (format "# Insert note for %s.
  12445. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12446. (cond
  12447. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12448. ((eq org-log-note-purpose 'done) "closed todo item")
  12449. ((eq org-log-note-purpose 'state)
  12450. (format "state change from \"%s\" to \"%s\""
  12451. (or org-log-note-previous-state "")
  12452. (or org-log-note-state "")))
  12453. ((eq org-log-note-purpose 'reschedule)
  12454. "rescheduling")
  12455. ((eq org-log-note-purpose 'delschedule)
  12456. "no longer scheduled")
  12457. ((eq org-log-note-purpose 'redeadline)
  12458. "changing deadline")
  12459. ((eq org-log-note-purpose 'deldeadline)
  12460. "removing deadline")
  12461. ((eq org-log-note-purpose 'refile)
  12462. "refiling")
  12463. ((eq org-log-note-purpose 'note)
  12464. "this entry")
  12465. (t (error "This should not happen")))))
  12466. (if org-log-note-extra (insert org-log-note-extra))
  12467. (org-set-local 'org-finish-function 'org-store-log-note)
  12468. (run-hooks 'org-log-buffer-setup-hook)))
  12469. (defvar org-note-abort nil) ; dynamically scoped
  12470. (defun org-store-log-note ()
  12471. "Finish taking a log note, and insert it to where it belongs."
  12472. (let ((txt (buffer-string)))
  12473. (kill-buffer (current-buffer))
  12474. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12475. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12476. (setq txt (replace-match "" t t txt)))
  12477. (if (string-match "\\s-+\\'" txt)
  12478. (setq txt (replace-match "" t t txt)))
  12479. (setq lines (org-split-string txt "\n"))
  12480. (when (and note (string-match "\\S-" note))
  12481. (setq note
  12482. (org-replace-escapes
  12483. note
  12484. (list (cons "%u" (user-login-name))
  12485. (cons "%U" user-full-name)
  12486. (cons "%t" (format-time-string
  12487. (org-time-stamp-format 'long 'inactive)
  12488. org-log-note-effective-time))
  12489. (cons "%T" (format-time-string
  12490. (org-time-stamp-format 'long nil)
  12491. org-log-note-effective-time))
  12492. (cons "%d" (format-time-string
  12493. (org-time-stamp-format nil 'inactive)
  12494. org-log-note-effective-time))
  12495. (cons "%D" (format-time-string
  12496. (org-time-stamp-format nil nil)
  12497. org-log-note-effective-time))
  12498. (cons "%s" (cond
  12499. ((not org-log-note-state) "")
  12500. ((org-string-match-p org-ts-regexp
  12501. org-log-note-state)
  12502. (format "\"[%s]\""
  12503. (substring org-log-note-state 1 -1)))
  12504. (t (format "\"%s\"" org-log-note-state))))
  12505. (cons "%S"
  12506. (cond
  12507. ((not org-log-note-previous-state) "")
  12508. ((org-string-match-p org-ts-regexp
  12509. org-log-note-previous-state)
  12510. (format "\"[%s]\""
  12511. (substring
  12512. org-log-note-previous-state 1 -1)))
  12513. (t (format "\"%s\""
  12514. org-log-note-previous-state)))))))
  12515. (when lines (setq note (concat note " \\\\")))
  12516. (push note lines))
  12517. (when (or current-prefix-arg org-note-abort)
  12518. (when (org-log-into-drawer)
  12519. (org-remove-empty-drawer-at org-log-note-marker))
  12520. (setq lines nil))
  12521. (when lines
  12522. (with-current-buffer (marker-buffer org-log-note-marker)
  12523. (org-with-wide-buffer
  12524. (goto-char org-log-note-marker)
  12525. (move-marker org-log-note-marker nil)
  12526. ;; Make sure point is at the beginning of an empty line.
  12527. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12528. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12529. ;; In an existing list, add a new item at the top level.
  12530. ;; Otherwise, indent line like a regular one.
  12531. (let ((itemp (org-in-item-p)))
  12532. (if itemp
  12533. (org-indent-line-to
  12534. (let ((struct (save-excursion
  12535. (goto-char itemp) (org-list-struct))))
  12536. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12537. (org-indent-line)))
  12538. (insert (org-list-bullet-string "-") (pop lines))
  12539. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12540. (dolist (line lines)
  12541. (insert "\n")
  12542. (org-indent-line-to ind)
  12543. (insert line)))
  12544. (message "Note stored")
  12545. (org-back-to-heading t)
  12546. (org-cycle-hide-drawers 'children))
  12547. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12548. ;; from within `org-add-log-note' because `buffer-undo-list'
  12549. ;; is then modified outside of `org-with-remote-undo'.
  12550. (when (eq this-command 'org-agenda-todo)
  12551. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12552. ;; Don't add undo information when called from `org-agenda-todo'
  12553. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12554. (set-window-configuration org-log-note-window-configuration)
  12555. (with-current-buffer (marker-buffer org-log-note-return-to)
  12556. (goto-char org-log-note-return-to))
  12557. (move-marker org-log-note-return-to nil)
  12558. (and org-log-post-message (message "%s" org-log-post-message))))
  12559. (defun org-remove-empty-drawer-at (pos)
  12560. "Remove an empty drawer at position POS.
  12561. POS may also be a marker."
  12562. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12563. (org-with-wide-buffer
  12564. (goto-char pos)
  12565. (let ((drawer (org-element-at-point)))
  12566. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12567. (not (org-element-property :contents-begin drawer)))
  12568. (delete-region (org-element-property :begin drawer)
  12569. (progn (goto-char (org-element-property :end drawer))
  12570. (skip-chars-backward " \r\t\n")
  12571. (forward-line)
  12572. (point))))))))
  12573. (defvar org-ts-type nil)
  12574. (defun org-sparse-tree (&optional arg type)
  12575. "Create a sparse tree, prompt for the details.
  12576. This command can create sparse trees. You first need to select the type
  12577. of match used to create the tree:
  12578. t Show all TODO entries.
  12579. T Show entries with a specific TODO keyword.
  12580. m Show entries selected by a tags/property match.
  12581. p Enter a property name and its value (both with completion on existing
  12582. names/values) and show entries with that property.
  12583. r Show entries matching a regular expression (`/' can be used as well).
  12584. b Show deadlines and scheduled items before a date.
  12585. a Show deadlines and scheduled items after a date.
  12586. d Show deadlines due within `org-deadline-warning-days'.
  12587. D Show deadlines and scheduled items between a date range."
  12588. (interactive "P")
  12589. (setq type (or type org-sparse-tree-default-date-type))
  12590. (setq org-ts-type type)
  12591. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12592. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12593. [c]ycle through date types: %s"
  12594. (case type
  12595. (all "all timestamps")
  12596. (scheduled "only scheduled")
  12597. (deadline "only deadline")
  12598. (active "only active timestamps")
  12599. (inactive "only inactive timestamps")
  12600. (closed "with a closed time-stamp")
  12601. (otherwise "scheduled/deadline")))
  12602. (let ((answer (read-char-exclusive)))
  12603. (case answer
  12604. (?c
  12605. (org-sparse-tree
  12606. arg
  12607. (cadr
  12608. (memq type '(nil all scheduled deadline active inactive closed)))))
  12609. (?d (call-interactively 'org-check-deadlines))
  12610. (?b (call-interactively 'org-check-before-date))
  12611. (?a (call-interactively 'org-check-after-date))
  12612. (?D (call-interactively 'org-check-dates-range))
  12613. (?t (call-interactively 'org-show-todo-tree))
  12614. (?T (org-show-todo-tree '(4)))
  12615. (?m (call-interactively 'org-match-sparse-tree))
  12616. ((?p ?P)
  12617. (let* ((kwd (org-icompleting-read
  12618. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12619. (value (org-icompleting-read
  12620. "Value: " (mapcar 'list (org-property-values kwd)))))
  12621. (unless (string-match "\\`{.*}\\'" value)
  12622. (setq value (concat "\"" value "\"")))
  12623. (org-match-sparse-tree arg (concat kwd "=" value))))
  12624. ((?r ?R ?/) (call-interactively 'org-occur))
  12625. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12626. (defvar org-occur-highlights nil
  12627. "List of overlays used for occur matches.")
  12628. (make-variable-buffer-local 'org-occur-highlights)
  12629. (defvar org-occur-parameters nil
  12630. "Parameters of the active org-occur calls.
  12631. This is a list, each call to org-occur pushes as cons cell,
  12632. containing the regular expression and the callback, onto the list.
  12633. The list can contain several entries if `org-occur' has been called
  12634. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12635. will only contain one set of parameters. When the highlights are
  12636. removed (for example with `C-c C-c', or with the next edit (depending
  12637. on `org-remove-highlights-with-change'), this variable is emptied
  12638. as well.")
  12639. (make-variable-buffer-local 'org-occur-parameters)
  12640. (defun org-occur (regexp &optional keep-previous callback)
  12641. "Make a compact tree which shows all matches of REGEXP.
  12642. The tree will show the lines where the regexp matches, and all higher
  12643. headlines above the match. It will also show the heading after the match,
  12644. to make sure editing the matching entry is easy.
  12645. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12646. call to `org-occur' will be kept, to allow stacking of calls to this
  12647. command.
  12648. If CALLBACK is non-nil, it is a function which is called to confirm
  12649. that the match should indeed be shown."
  12650. (interactive "sRegexp: \nP")
  12651. (when (equal regexp "")
  12652. (user-error "Regexp cannot be empty"))
  12653. (unless keep-previous
  12654. (org-remove-occur-highlights nil nil t))
  12655. (push (cons regexp callback) org-occur-parameters)
  12656. (let ((cnt 0))
  12657. (save-excursion
  12658. (goto-char (point-min))
  12659. (if (or (not keep-previous) ; do not want to keep
  12660. (not org-occur-highlights)) ; no previous matches
  12661. ;; hide everything
  12662. (org-overview))
  12663. (while (re-search-forward regexp nil t)
  12664. (when (or (not callback)
  12665. (save-match-data (funcall callback)))
  12666. (setq cnt (1+ cnt))
  12667. (when org-highlight-sparse-tree-matches
  12668. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12669. (org-show-context 'occur-tree))))
  12670. (when org-remove-highlights-with-change
  12671. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12672. nil 'local))
  12673. (unless org-sparse-tree-open-archived-trees
  12674. (org-hide-archived-subtrees (point-min) (point-max)))
  12675. (run-hooks 'org-occur-hook)
  12676. (if (org-called-interactively-p 'interactive)
  12677. (message "%d match(es) for regexp %s" cnt regexp))
  12678. cnt))
  12679. (defun org-occur-next-match (&optional n reset)
  12680. "Function for `next-error-function' to find sparse tree matches.
  12681. N is the number of matches to move, when negative move backwards.
  12682. RESET is entirely ignored - this function always goes back to the
  12683. starting point when no match is found."
  12684. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12685. (search-func (if (< n 0)
  12686. 'previous-single-char-property-change
  12687. 'next-single-char-property-change))
  12688. (n (abs n))
  12689. (pos (point))
  12690. p1)
  12691. (catch 'exit
  12692. (while (setq p1 (funcall search-func (point) 'org-type))
  12693. (when (equal p1 limit)
  12694. (goto-char pos)
  12695. (user-error "No more matches"))
  12696. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12697. (setq n (1- n))
  12698. (when (= n 0)
  12699. (goto-char p1)
  12700. (throw 'exit (point))))
  12701. (goto-char p1))
  12702. (goto-char p1)
  12703. (user-error "No more matches"))))
  12704. (defun org-show-context (&optional key)
  12705. "Make sure point and context are visible.
  12706. Optional argument KEY, when non-nil, is a symbol. See
  12707. `org-show-context-detail' for allowed values and how much is to
  12708. be shown."
  12709. (org-show-set-visibility
  12710. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12711. ((cdr (assq key org-show-context-detail)))
  12712. (t (cdr (assq 'default org-show-context-detail))))))
  12713. (defun org-show-set-visibility (detail)
  12714. "Set visibility around point according to DETAIL.
  12715. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12716. `tree', `canonical' or t. See `org-show-context-detail' for more
  12717. information."
  12718. (unless (org-before-first-heading-p)
  12719. ;; Show current heading and possibly its entry, following headline
  12720. ;; or all children.
  12721. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12722. (org-flag-heading nil)
  12723. (org-show-entry)
  12724. (org-with-limited-levels
  12725. (case detail
  12726. ((tree canonical t) (show-children))
  12727. ((nil minimal ancestors))
  12728. (t (save-excursion
  12729. (outline-next-heading)
  12730. (org-flag-heading nil))))))
  12731. ;; Show all siblings.
  12732. (when (eq detail 'lineage) (org-show-siblings))
  12733. ;; Show ancestors, possibly with their children.
  12734. (when (memq detail '(ancestors lineage tree canonical t))
  12735. (save-excursion
  12736. (while (org-up-heading-safe)
  12737. (org-flag-heading nil)
  12738. (when (memq detail '(canonical t)) (org-show-entry))
  12739. (when (memq detail '(tree canonical t)) (show-children)))))))
  12740. (defvar org-reveal-start-hook nil
  12741. "Hook run before revealing a location.")
  12742. (defun org-reveal (&optional siblings)
  12743. "Show current entry, hierarchy above it, and the following headline.
  12744. This can be used to show a consistent set of context around
  12745. locations exposed with `org-show-context'.
  12746. With optional argument SIBLINGS, on each level of the hierarchy all
  12747. siblings are shown. This repairs the tree structure to what it would
  12748. look like when opened with hierarchical calls to `org-cycle'.
  12749. With double optional argument \\[universal-argument] \\[universal-argument], \
  12750. go to the parent and show the
  12751. entire tree."
  12752. (interactive "P")
  12753. (run-hooks 'org-reveal-start-hook)
  12754. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12755. ((equal siblings '(16))
  12756. (save-excursion
  12757. (when (org-up-heading-safe)
  12758. (org-show-subtree)
  12759. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12760. (t (org-show-set-visibility 'lineage))))
  12761. (defun org-highlight-new-match (beg end)
  12762. "Highlight from BEG to END and mark the highlight is an occur headline."
  12763. (let ((ov (make-overlay beg end)))
  12764. (overlay-put ov 'face 'secondary-selection)
  12765. (overlay-put ov 'org-type 'org-occur)
  12766. (push ov org-occur-highlights)))
  12767. (defun org-remove-occur-highlights (&optional beg end noremove)
  12768. "Remove the occur highlights from the buffer.
  12769. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12770. from the `before-change-functions' in the current buffer."
  12771. (interactive)
  12772. (unless org-inhibit-highlight-removal
  12773. (mapc 'delete-overlay org-occur-highlights)
  12774. (setq org-occur-highlights nil)
  12775. (setq org-occur-parameters nil)
  12776. (unless noremove
  12777. (remove-hook 'before-change-functions
  12778. 'org-remove-occur-highlights 'local))))
  12779. ;;;; Priorities
  12780. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12781. "Regular expression matching the priority indicator.")
  12782. (defvar org-remove-priority-next-time nil)
  12783. (defun org-priority-up ()
  12784. "Increase the priority of the current item."
  12785. (interactive)
  12786. (org-priority 'up))
  12787. (defun org-priority-down ()
  12788. "Decrease the priority of the current item."
  12789. (interactive)
  12790. (org-priority 'down))
  12791. (defun org-priority (&optional action show)
  12792. "Change the priority of an item.
  12793. ACTION can be `set', `up', `down', or a character."
  12794. (interactive "P")
  12795. (if (equal action '(4))
  12796. (org-show-priority)
  12797. (unless org-enable-priority-commands
  12798. (user-error "Priority commands are disabled"))
  12799. (setq action (or action 'set))
  12800. (let (current new news have remove)
  12801. (save-excursion
  12802. (org-back-to-heading t)
  12803. (if (looking-at org-priority-regexp)
  12804. (setq current (string-to-char (match-string 2))
  12805. have t))
  12806. (cond
  12807. ((eq action 'remove)
  12808. (setq remove t new ?\ ))
  12809. ((or (eq action 'set)
  12810. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12811. (if (not (eq action 'set))
  12812. (setq new action)
  12813. (message "Priority %c-%c, SPC to remove: "
  12814. org-highest-priority org-lowest-priority)
  12815. (save-match-data
  12816. (setq new (read-char-exclusive))))
  12817. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12818. (= (upcase org-lowest-priority) org-lowest-priority))
  12819. (setq new (upcase new)))
  12820. (cond ((equal new ?\ ) (setq remove t))
  12821. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12822. (user-error "Priority must be between `%c' and `%c'"
  12823. org-highest-priority org-lowest-priority))))
  12824. ((eq action 'up)
  12825. (setq new (if have
  12826. (1- current) ; normal cycling
  12827. ;; last priority was empty
  12828. (if (eq last-command this-command)
  12829. org-lowest-priority ; wrap around empty to lowest
  12830. ;; default
  12831. (if org-priority-start-cycle-with-default
  12832. org-default-priority
  12833. (1- org-default-priority))))))
  12834. ((eq action 'down)
  12835. (setq new (if have
  12836. (1+ current) ; normal cycling
  12837. ;; last priority was empty
  12838. (if (eq last-command this-command)
  12839. org-highest-priority ; wrap around empty to highest
  12840. ;; default
  12841. (if org-priority-start-cycle-with-default
  12842. org-default-priority
  12843. (1+ org-default-priority))))))
  12844. (t (user-error "Invalid action")))
  12845. (if (or (< (upcase new) org-highest-priority)
  12846. (> (upcase new) org-lowest-priority))
  12847. (if (and (memq action '(up down))
  12848. (not have) (not (eq last-command this-command)))
  12849. ;; `new' is from default priority
  12850. (error
  12851. "The default can not be set, see `org-default-priority' why")
  12852. ;; normal cycling: `new' is beyond highest/lowest priority
  12853. ;; and is wrapped around to the empty priority
  12854. (setq remove t)))
  12855. (setq news (format "%c" new))
  12856. (if have
  12857. (if remove
  12858. (replace-match "" t t nil 1)
  12859. (replace-match news t t nil 2))
  12860. (if remove
  12861. (user-error "No priority cookie found in line")
  12862. (let ((case-fold-search nil))
  12863. (looking-at org-todo-line-regexp))
  12864. (if (match-end 2)
  12865. (progn
  12866. (goto-char (match-end 2))
  12867. (insert " [#" news "]"))
  12868. (goto-char (match-beginning 3))
  12869. (insert "[#" news "] "))))
  12870. (org-set-tags nil 'align))
  12871. (if remove
  12872. (message "Priority removed")
  12873. (message "Priority of current item set to %s" news)))))
  12874. (defun org-show-priority ()
  12875. "Show the priority of the current item.
  12876. This priority is composed of the main priority given with the [#A] cookies,
  12877. and by additional input from the age of a schedules or deadline entry."
  12878. (interactive)
  12879. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12880. (org-get-at-bol 'priority)
  12881. (save-excursion
  12882. (save-match-data
  12883. (beginning-of-line)
  12884. (and (looking-at org-heading-regexp)
  12885. (org-get-priority (match-string 0))))))))
  12886. (message "Priority is %d" (if pri pri -1000))))
  12887. (defun org-get-priority (s)
  12888. "Find priority cookie and return priority."
  12889. (save-match-data
  12890. (if (functionp org-get-priority-function)
  12891. (funcall org-get-priority-function)
  12892. (if (not (string-match org-priority-regexp s))
  12893. (* 1000 (- org-lowest-priority org-default-priority))
  12894. (* 1000 (- org-lowest-priority
  12895. (string-to-char (match-string 2 s))))))))
  12896. ;;;; Tags
  12897. (defvar org-agenda-archives-mode)
  12898. (defvar org-map-continue-from nil
  12899. "Position from where mapping should continue.
  12900. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12901. (defvar org-scanner-tags nil
  12902. "The current tag list while the tags scanner is running.")
  12903. (defvar org-trust-scanner-tags nil
  12904. "Should `org-get-tags-at' use the tags for the scanner.
  12905. This is for internal dynamical scoping only.
  12906. When this is non-nil, the function `org-get-tags-at' will return the value
  12907. of `org-scanner-tags' instead of building the list by itself. This
  12908. can lead to large speed-ups when the tags scanner is used in a file with
  12909. many entries, and when the list of tags is retrieved, for example to
  12910. obtain a list of properties. Building the tags list for each entry in such
  12911. a file becomes an N^2 operation - but with this variable set, it scales
  12912. as N.")
  12913. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12914. "Scan headline tags with inheritance and produce output ACTION.
  12915. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12916. or `agenda' to produce an entry list for an agenda view. It can also be
  12917. a Lisp form or a function that should be called at each matched headline, in
  12918. this case the return value is a list of all return values from these calls.
  12919. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12920. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12921. only lines with a not-done TODO keyword are included in the output.
  12922. This should be the same variable that was scoped into
  12923. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12924. START-LEVEL can be a string with asterisks, reducing the scope to
  12925. headlines matching this string."
  12926. (require 'org-agenda)
  12927. (let* ((re (concat "^"
  12928. (if start-level
  12929. ;; Get the correct level to match
  12930. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12931. org-outline-regexp)
  12932. " *\\(\\<\\("
  12933. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12934. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12935. (props (list 'face 'default
  12936. 'done-face 'org-agenda-done
  12937. 'undone-face 'default
  12938. 'mouse-face 'highlight
  12939. 'org-not-done-regexp org-not-done-regexp
  12940. 'org-todo-regexp org-todo-regexp
  12941. 'org-complex-heading-regexp org-complex-heading-regexp
  12942. 'help-echo
  12943. (format "mouse-2 or RET jump to org file %s"
  12944. (abbreviate-file-name
  12945. (or (buffer-file-name (buffer-base-buffer))
  12946. (buffer-name (buffer-base-buffer)))))))
  12947. (org-map-continue-from nil)
  12948. lspos tags tags-list
  12949. (tags-alist (list (cons 0 org-file-tags)))
  12950. (llast 0) rtn rtn1 level category i txt
  12951. todo marker entry priority
  12952. ts-date ts-date-type ts-date-pair)
  12953. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12954. (setq action (list 'lambda nil action)))
  12955. (save-excursion
  12956. (goto-char (point-min))
  12957. (when (eq action 'sparse-tree)
  12958. (org-overview)
  12959. (org-remove-occur-highlights))
  12960. (while (let (case-fold-search)
  12961. (re-search-forward re nil t))
  12962. (setq org-map-continue-from nil)
  12963. (catch :skip
  12964. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12965. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12966. (goto-char (setq lspos (match-beginning 0)))
  12967. (setq level (org-reduced-level (org-outline-level))
  12968. category (org-get-category))
  12969. (when (eq action 'agenda)
  12970. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12971. ts-date (car ts-date-pair)
  12972. ts-date-type (cdr ts-date-pair)))
  12973. (setq i llast llast level)
  12974. ;; remove tag lists from same and sublevels
  12975. (while (>= i level)
  12976. (when (setq entry (assoc i tags-alist))
  12977. (setq tags-alist (delete entry tags-alist)))
  12978. (setq i (1- i)))
  12979. ;; add the next tags
  12980. (when tags
  12981. (setq tags (org-split-string tags ":")
  12982. tags-alist
  12983. (cons (cons level tags) tags-alist)))
  12984. ;; compile tags for current headline
  12985. (setq tags-list
  12986. (if org-use-tag-inheritance
  12987. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12988. tags)
  12989. org-scanner-tags tags-list)
  12990. (when org-use-tag-inheritance
  12991. (setcdr (car tags-alist)
  12992. (mapcar (lambda (x)
  12993. (setq x (copy-sequence x))
  12994. (org-add-prop-inherited x))
  12995. (cdar tags-alist))))
  12996. (when (and tags org-use-tag-inheritance
  12997. (or (not (eq t org-use-tag-inheritance))
  12998. org-tags-exclude-from-inheritance))
  12999. ;; selective inheritance, remove uninherited ones
  13000. (setcdr (car tags-alist)
  13001. (org-remove-uninherited-tags (cdar tags-alist))))
  13002. (when (and
  13003. ;; eval matcher only when the todo condition is OK
  13004. (and (or (not todo-only) (member todo org-not-done-keywords))
  13005. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13006. (eval matcher)))
  13007. ;; Call the skipper, but return t if it does not skip,
  13008. ;; so that the `and' form continues evaluating
  13009. (progn
  13010. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13011. t)
  13012. ;; Check if timestamps are deselecting this entry
  13013. (or (not todo-only)
  13014. (and (member todo org-not-done-keywords)
  13015. (or (not org-agenda-tags-todo-honor-ignore-options)
  13016. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13017. ;; select this headline
  13018. (cond
  13019. ((eq action 'sparse-tree)
  13020. (and org-highlight-sparse-tree-matches
  13021. (org-get-heading) (match-end 0)
  13022. (org-highlight-new-match
  13023. (match-beginning 1) (match-end 1)))
  13024. (org-show-context 'tags-tree))
  13025. ((eq action 'agenda)
  13026. (setq txt (org-agenda-format-item
  13027. ""
  13028. (concat
  13029. (if (eq org-tags-match-list-sublevels 'indented)
  13030. (make-string (1- level) ?.) "")
  13031. (org-get-heading))
  13032. (make-string level ?\s)
  13033. category
  13034. tags-list)
  13035. priority (org-get-priority txt))
  13036. (goto-char lspos)
  13037. (setq marker (org-agenda-new-marker))
  13038. (org-add-props txt props
  13039. 'org-marker marker 'org-hd-marker marker 'org-category category
  13040. 'todo-state todo
  13041. 'ts-date ts-date
  13042. 'priority priority
  13043. 'type (concat "tagsmatch" ts-date-type))
  13044. (push txt rtn))
  13045. ((functionp action)
  13046. (setq org-map-continue-from nil)
  13047. (save-excursion
  13048. (setq rtn1 (funcall action))
  13049. (push rtn1 rtn)))
  13050. (t (user-error "Invalid action")))
  13051. ;; if we are to skip sublevels, jump to end of subtree
  13052. (unless org-tags-match-list-sublevels
  13053. (org-end-of-subtree t)
  13054. (backward-char 1))))
  13055. ;; Get the correct position from where to continue
  13056. (if org-map-continue-from
  13057. (goto-char org-map-continue-from)
  13058. (and (= (point) lspos) (end-of-line 1)))))
  13059. (when (and (eq action 'sparse-tree)
  13060. (not org-sparse-tree-open-archived-trees))
  13061. (org-hide-archived-subtrees (point-min) (point-max)))
  13062. (nreverse rtn)))
  13063. (defun org-remove-uninherited-tags (tags)
  13064. "Remove all tags that are not inherited from the list TAGS."
  13065. (cond
  13066. ((eq org-use-tag-inheritance t)
  13067. (if org-tags-exclude-from-inheritance
  13068. (org-delete-all org-tags-exclude-from-inheritance tags)
  13069. tags))
  13070. ((not org-use-tag-inheritance) nil)
  13071. ((stringp org-use-tag-inheritance)
  13072. (delq nil (mapcar
  13073. (lambda (x)
  13074. (if (and (string-match org-use-tag-inheritance x)
  13075. (not (member x org-tags-exclude-from-inheritance)))
  13076. x nil))
  13077. tags)))
  13078. ((listp org-use-tag-inheritance)
  13079. (delq nil (mapcar
  13080. (lambda (x)
  13081. (if (member x org-use-tag-inheritance) x nil))
  13082. tags)))))
  13083. (defun org-match-sparse-tree (&optional todo-only match)
  13084. "Create a sparse tree according to tags string MATCH.
  13085. MATCH can contain positive and negative selection of tags, like
  13086. \"+WORK+URGENT-WITHBOSS\".
  13087. If optional argument TODO-ONLY is non-nil, only select lines that are
  13088. also TODO lines."
  13089. (interactive "P")
  13090. (org-agenda-prepare-buffers (list (current-buffer)))
  13091. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13092. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13093. (defvar org-cached-props nil)
  13094. (defun org-cached-entry-get (pom property)
  13095. (if (or (eq t org-use-property-inheritance)
  13096. (and (stringp org-use-property-inheritance)
  13097. (let ((case-fold-search t))
  13098. (org-string-match-p org-use-property-inheritance property)))
  13099. (and (listp org-use-property-inheritance)
  13100. (member-ignore-case property org-use-property-inheritance)))
  13101. ;; Caching is not possible, check it directly.
  13102. (org-entry-get pom property 'inherit)
  13103. ;; Get all properties, so we can do complicated checks easily.
  13104. (cdr (assoc-string property
  13105. (or org-cached-props
  13106. (setq org-cached-props (org-entry-properties pom)))
  13107. t))))
  13108. (defun org-global-tags-completion-table (&optional files)
  13109. "Return the list of all tags in all agenda buffer/files.
  13110. Optional FILES argument is a list of files which can be used
  13111. instead of the agenda files."
  13112. (save-excursion
  13113. (org-uniquify
  13114. (delq nil
  13115. (apply 'append
  13116. (mapcar
  13117. (lambda (file)
  13118. (set-buffer (find-file-noselect file))
  13119. (append (org-get-buffer-tags)
  13120. (mapcar (lambda (x) (if (stringp (car-safe x))
  13121. (list (car-safe x)) nil))
  13122. org-tag-alist)))
  13123. (if (and files (car files))
  13124. files
  13125. (org-agenda-files))))))))
  13126. (defun org-make-tags-matcher (match)
  13127. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13128. The variable `todo-only' is scoped dynamically into this function.
  13129. It will be set to t if the matcher restricts matching to TODO entries,
  13130. otherwise will not be touched.
  13131. Returns a cons of the selection string MATCH and the constructed
  13132. lisp form implementing the matcher. The matcher is to be evaluated
  13133. at an Org entry, with point on the headline, and returns t if the
  13134. entry matches the selection string MATCH. The returned lisp form
  13135. references two variables with information about the entry, which
  13136. must be bound around the form's evaluation: todo, the TODO keyword
  13137. at the entry (or nil of none); and tags-list, the list of all tags
  13138. at the entry including inherited ones. Additionally, the category
  13139. of the entry (if any) must be specified as the text property
  13140. 'org-category on the headline.
  13141. See also `org-scan-tags'.
  13142. "
  13143. (declare (special todo-only))
  13144. (unless (boundp 'todo-only)
  13145. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13146. (unless match
  13147. ;; Get a new match request, with completion against the global
  13148. ;; tags table and the local tags in current buffer
  13149. (let ((org-last-tags-completion-table
  13150. (org-uniquify
  13151. (delq nil (append (org-get-buffer-tags)
  13152. (org-global-tags-completion-table))))))
  13153. (setq match (org-completing-read-no-i
  13154. "Match: " 'org-tags-completion-function nil nil nil
  13155. 'org-tags-history))))
  13156. ;; Parse the string and create a lisp form
  13157. (let ((match0 match)
  13158. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13159. minus tag mm
  13160. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13161. orterms term orlist re-p str-p level-p level-op time-p
  13162. prop-p pn pv po gv rest (start 0) (ss 0))
  13163. ;; Expand group tags
  13164. (setq match (org-tags-expand match))
  13165. ;; Check if there is a TODO part of this match, which would be the
  13166. ;; part after a "/". TO make sure that this slash is not part of
  13167. ;; a property value to be matched against, we also check that there
  13168. ;; is no " after that slash.
  13169. ;; First, find the last slash
  13170. (while (string-match "/+" match ss)
  13171. (setq start (match-beginning 0) ss (match-end 0)))
  13172. (if (and (string-match "/+" match start)
  13173. (not (save-match-data (string-match "\"" match start))))
  13174. ;; match contains also a todo-matching request
  13175. (progn
  13176. (setq tagsmatch (substring match 0 (match-beginning 0))
  13177. todomatch (substring match (match-end 0)))
  13178. (if (string-match "^!" todomatch)
  13179. (setq todo-only t todomatch (substring todomatch 1)))
  13180. (if (string-match "^\\s-*$" todomatch)
  13181. (setq todomatch nil)))
  13182. ;; only matching tags
  13183. (setq tagsmatch match todomatch nil))
  13184. ;; Make the tags matcher
  13185. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13186. (setq tagsmatcher t)
  13187. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13188. (while (setq term (pop orterms))
  13189. (while (and (equal (substring term -1) "\\") orterms)
  13190. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13191. (while (string-match re term)
  13192. (setq rest (substring term (match-end 0))
  13193. minus (and (match-end 1)
  13194. (equal (match-string 1 term) "-"))
  13195. tag (save-match-data (replace-regexp-in-string
  13196. "\\\\-" "-"
  13197. (match-string 2 term)))
  13198. re-p (equal (string-to-char tag) ?{)
  13199. level-p (match-end 4)
  13200. prop-p (match-end 5)
  13201. mm (cond
  13202. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13203. (level-p
  13204. (setq level-op (org-op-to-function (match-string 3 term)))
  13205. `(,level-op level ,(string-to-number
  13206. (match-string 4 term))))
  13207. (prop-p
  13208. (setq pn (match-string 5 term)
  13209. po (match-string 6 term)
  13210. pv (match-string 7 term)
  13211. re-p (equal (string-to-char pv) ?{)
  13212. str-p (equal (string-to-char pv) ?\")
  13213. time-p (save-match-data
  13214. (string-match "^\"[[<].*[]>]\"$" pv))
  13215. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13216. (if time-p (setq pv (org-matcher-time pv)))
  13217. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13218. (cond
  13219. ((equal pn "CATEGORY")
  13220. (setq gv '(get-text-property (point) 'org-category)))
  13221. ((equal pn "TODO")
  13222. (setq gv 'todo))
  13223. (t
  13224. (setq gv `(org-cached-entry-get nil ,pn))))
  13225. (if re-p
  13226. (if (eq po 'org<>)
  13227. `(not (string-match ,pv (or ,gv "")))
  13228. `(string-match ,pv (or ,gv "")))
  13229. (if str-p
  13230. `(,po (or ,gv "") ,pv)
  13231. `(,po (string-to-number (or ,gv ""))
  13232. ,(string-to-number pv) ))))
  13233. (t `(member ,tag tags-list)))
  13234. mm (if minus (list 'not mm) mm)
  13235. term rest)
  13236. (push mm tagsmatcher))
  13237. (push (if (> (length tagsmatcher) 1)
  13238. (cons 'and tagsmatcher)
  13239. (car tagsmatcher))
  13240. orlist)
  13241. (setq tagsmatcher nil))
  13242. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13243. (setq tagsmatcher
  13244. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13245. ;; Make the todo matcher
  13246. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13247. (setq todomatcher t)
  13248. (setq orterms (org-split-string todomatch "|") orlist nil)
  13249. (dolist (term orterms)
  13250. (while (string-match re term)
  13251. (setq minus (and (match-end 1)
  13252. (equal (match-string 1 term) "-"))
  13253. kwd (match-string 2 term)
  13254. re-p (equal (string-to-char kwd) ?{)
  13255. term (substring term (match-end 0))
  13256. mm (if re-p
  13257. `(string-match ,(substring kwd 1 -1) todo)
  13258. (list 'equal 'todo kwd))
  13259. mm (if minus (list 'not mm) mm))
  13260. (push mm todomatcher))
  13261. (push (if (> (length todomatcher) 1)
  13262. (cons 'and todomatcher)
  13263. (car todomatcher))
  13264. orlist)
  13265. (setq todomatcher nil))
  13266. (setq todomatcher (if (> (length orlist) 1)
  13267. (cons 'or orlist) (car orlist))))
  13268. ;; Return the string and lisp forms of the matcher
  13269. (setq matcher (if todomatcher
  13270. (list 'and tagsmatcher todomatcher)
  13271. tagsmatcher))
  13272. (when todo-only
  13273. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13274. matcher)))
  13275. (cons match0 matcher)))
  13276. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13277. "Expand group tags in MATCH.
  13278. This replaces every group tag in MATCH with a regexp tag search.
  13279. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13280. will be replaced like this:
  13281. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13282. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13283. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13284. Replacing by a regexp preserves the structure of the match.
  13285. E.g., this expansion
  13286. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13287. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13288. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13289. A group tag in MATCH can contain regular expressions of its own.
  13290. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13291. will be replaced like this:
  13292. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13293. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13294. assumed to be a single group tag, and the function will return
  13295. the list of tags in this group.
  13296. When DOWNCASE is non-nil, expand downcased TAGS."
  13297. (if org-group-tags
  13298. (let* ((case-fold-search t)
  13299. (stable org-mode-syntax-table)
  13300. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13301. (taggroups (if downcased
  13302. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13303. taggroups)
  13304. taggroups))
  13305. (taggroups-keys (mapcar #'car taggroups))
  13306. (return-match (if downcased (downcase match) match))
  13307. (count 0)
  13308. (work-already-expanded tags-already-expanded)
  13309. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13310. ;; @ and _ are allowed as word-components in tags.
  13311. (modify-syntax-entry ?@ "w" stable)
  13312. (modify-syntax-entry ?_ "w" stable)
  13313. ;; Temporarily replace regexp-expressions in the match-expression.
  13314. (while (string-match "{.+?}" return-match)
  13315. (incf count)
  13316. (push (match-string 0 return-match) regexps-in-match)
  13317. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13318. (while (and taggroups-keys
  13319. (with-syntax-table stable
  13320. (string-match
  13321. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13322. (regexp-opt taggroups-keys) "\\>\\)")
  13323. return-match)))
  13324. (let* ((dir (match-string 1 return-match))
  13325. (tag (match-string 2 return-match))
  13326. (tag (if downcased (downcase tag) tag)))
  13327. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13328. (member tag work-already-expanded))
  13329. (setq tags-in-group (assoc tag taggroups))
  13330. (push tag work-already-expanded)
  13331. ;; Recursively expand each tag in the group, if the tag hasn't
  13332. ;; already been expanded. Restore the match-data after all recursive calls.
  13333. (save-match-data
  13334. (let (tags-expanded)
  13335. (dolist (x (cdr tags-in-group))
  13336. (if (and (member x taggroups-keys)
  13337. (not (member x work-already-expanded)))
  13338. (setq tags-expanded
  13339. (delete-dups
  13340. (append
  13341. (org-tags-expand x t downcased
  13342. work-already-expanded)
  13343. tags-expanded)))
  13344. (setq tags-expanded
  13345. (append (list x) tags-expanded)))
  13346. (setq work-already-expanded
  13347. (delete-dups
  13348. (append tags-expanded
  13349. work-already-expanded))))
  13350. (setq tags-in-group
  13351. (delete-dups (cons (car tags-in-group)
  13352. tags-expanded)))))
  13353. ;; Filter tag-regexps from tags.
  13354. (setq regexp-in-group-escaped
  13355. (delq nil (mapcar (lambda (x)
  13356. (if (stringp x)
  13357. (and (equal "{" (substring x 0 1))
  13358. (equal "}" (substring x -1))
  13359. x)
  13360. x))
  13361. tags-in-group))
  13362. regexp-in-group
  13363. (mapcar (lambda (x)
  13364. (substring x 1 -1))
  13365. regexp-in-group-escaped)
  13366. tags-in-group
  13367. (delq nil (mapcar (lambda (x)
  13368. (if (stringp x)
  13369. (and (not (equal "{" (substring x 0 1)))
  13370. (not (equal "}" (substring x -1)))
  13371. x)
  13372. x))
  13373. tags-in-group)))
  13374. ;; If single-as-list, do no more in the while-loop.
  13375. (if (not single-as-list)
  13376. (progn
  13377. (when regexp-in-group
  13378. (setq regexp-in-group
  13379. (concat "\\|"
  13380. (mapconcat 'identity regexp-in-group
  13381. "\\|"))))
  13382. (setq tags-in-group
  13383. (concat dir
  13384. "{\\<"
  13385. (regexp-opt tags-in-group)
  13386. "\\>"
  13387. regexp-in-group
  13388. "}"))
  13389. (when (stringp tags-in-group)
  13390. (org-add-props tags-in-group '(grouptag t)))
  13391. (setq return-match
  13392. (replace-match tags-in-group t t return-match)))
  13393. (setq tags-in-group
  13394. (append regexp-in-group-escaped tags-in-group))))
  13395. (setq taggroups-keys (delete tag taggroups-keys))))
  13396. ;; Add the regular expressions back into the match-expression again.
  13397. (while regexps-in-match
  13398. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13399. (pop regexps-in-match)
  13400. return-match t t))
  13401. (decf count))
  13402. (if single-as-list
  13403. (if tags-in-group tags-in-group (list return-match))
  13404. return-match))
  13405. (if single-as-list
  13406. (list (if downcased (downcase match) match))
  13407. match)))
  13408. (defun org-op-to-function (op &optional stringp)
  13409. "Turn an operator into the appropriate function."
  13410. (setq op
  13411. (cond
  13412. ((equal op "<" ) '(< string< org-time<))
  13413. ((equal op ">" ) '(> org-string> org-time>))
  13414. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13415. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13416. ((member op '("=" "==")) '(= string= org-time=))
  13417. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13418. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13419. (defun org<> (a b) (not (= a b)))
  13420. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13421. (defun org-string>= (a b) (not (string< a b)))
  13422. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13423. (defun org-string<> (a b) (not (string= 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) (> a b)))
  13428. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13429. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13430. (defun org-2ft (s)
  13431. "Convert S to a floating point time.
  13432. If S is already a number, just return it. If it is a string, parse
  13433. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13434. (cond
  13435. ((numberp s) s)
  13436. ((stringp s)
  13437. (condition-case nil
  13438. (float-time (apply 'encode-time (org-parse-time-string s)))
  13439. (error 0.)))
  13440. (t 0.)))
  13441. (defun org-time-today ()
  13442. "Time in seconds today at 0:00.
  13443. Returns the float number of seconds since the beginning of the
  13444. epoch to the beginning of today (00:00)."
  13445. (float-time (apply 'encode-time
  13446. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13447. (defun org-matcher-time (s)
  13448. "Interpret a time comparison value."
  13449. (save-match-data
  13450. (cond
  13451. ((string= s "<now>") (float-time))
  13452. ((string= s "<today>") (org-time-today))
  13453. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13454. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13455. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13456. (+ (org-time-today)
  13457. (* (string-to-number (match-string 1 s))
  13458. (cdr (assoc (match-string 2 s)
  13459. '(("d" . 86400.0) ("w" . 604800.0)
  13460. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13461. (t (org-2ft s)))))
  13462. (defun org-match-any-p (re list)
  13463. "Does re match any element of list?"
  13464. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13465. (delq nil list))
  13466. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13467. (defvar org-tags-overlay (make-overlay 1 1))
  13468. (org-detach-overlay org-tags-overlay)
  13469. (defun org-get-local-tags-at (&optional pos)
  13470. "Get a list of tags defined in the current headline."
  13471. (org-get-tags-at pos 'local))
  13472. (defun org-get-local-tags ()
  13473. "Get a list of tags defined in the current headline."
  13474. (org-get-tags-at nil 'local))
  13475. (defun org-get-tags-at (&optional pos local)
  13476. "Get a list of all headline tags applicable at POS.
  13477. POS defaults to point. If tags are inherited, the list contains
  13478. the targets in the same sequence as the headlines appear, i.e.
  13479. the tags of the current headline come last.
  13480. When LOCAL is non-nil, only return tags from the current headline,
  13481. ignore inherited ones."
  13482. (interactive)
  13483. (if (and org-trust-scanner-tags
  13484. (or (not pos) (equal pos (point)))
  13485. (not local))
  13486. org-scanner-tags
  13487. (let (tags ltags lastpos parent)
  13488. (save-excursion
  13489. (save-restriction
  13490. (widen)
  13491. (goto-char (or pos (point)))
  13492. (save-match-data
  13493. (catch 'done
  13494. (condition-case nil
  13495. (progn
  13496. (org-back-to-heading t)
  13497. (while (not (equal lastpos (point)))
  13498. (setq lastpos (point))
  13499. (when (looking-at
  13500. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13501. (setq ltags (org-split-string
  13502. (org-match-string-no-properties 1) ":"))
  13503. (when parent
  13504. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13505. (setq tags (append
  13506. (if parent
  13507. (org-remove-uninherited-tags ltags)
  13508. ltags)
  13509. tags)))
  13510. (or org-use-tag-inheritance (throw 'done t))
  13511. (if local (throw 'done t))
  13512. (or (org-up-heading-safe) (error nil))
  13513. (setq parent t)))
  13514. (error nil)))))
  13515. (if local
  13516. tags
  13517. (reverse (delete-dups
  13518. (reverse (append
  13519. (org-remove-uninherited-tags
  13520. org-file-tags)
  13521. tags)))))))))
  13522. (defun org-add-prop-inherited (s)
  13523. (add-text-properties 0 (length s) '(inherited t) s)
  13524. s)
  13525. (defun org-toggle-tag (tag &optional onoff)
  13526. "Toggle the tag TAG for the current line.
  13527. If ONOFF is `on' or `off', don't toggle but set to this state."
  13528. (let (res current)
  13529. (save-excursion
  13530. (org-back-to-heading t)
  13531. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13532. (point-at-eol) t)
  13533. (progn
  13534. (setq current (match-string 1))
  13535. (replace-match ""))
  13536. (setq current ""))
  13537. (setq current (nreverse (org-split-string current ":")))
  13538. (cond
  13539. ((eq onoff 'on)
  13540. (setq res t)
  13541. (or (member tag current) (push tag current)))
  13542. ((eq onoff 'off)
  13543. (or (not (member tag current)) (setq current (delete tag current))))
  13544. (t (if (member tag current)
  13545. (setq current (delete tag current))
  13546. (setq res t)
  13547. (push tag current))))
  13548. (end-of-line 1)
  13549. (if current
  13550. (progn
  13551. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13552. (org-set-tags nil t))
  13553. (delete-horizontal-space))
  13554. (run-hooks 'org-after-tags-change-hook))
  13555. res))
  13556. (defun org-align-tags-here (to-col)
  13557. ;; Assumes that this is a headline
  13558. "Align tags on the current headline to TO-COL."
  13559. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13560. (beginning-of-line 1)
  13561. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13562. (< pos (match-beginning 2)))
  13563. (progn
  13564. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13565. (goto-char (match-beginning 1))
  13566. (insert " ")
  13567. (delete-region (point) (1+ (match-beginning 2)))
  13568. (setq ncol (max (current-column)
  13569. (1+ col)
  13570. (if (> to-col 0)
  13571. to-col
  13572. (- (abs to-col) tags-l))))
  13573. (setq p (point))
  13574. (insert (make-string (- ncol (current-column)) ?\ ))
  13575. (setq ncol (current-column))
  13576. (when indent-tabs-mode (tabify p (point-at-eol)))
  13577. (org-move-to-column (min ncol col)))
  13578. (goto-char pos))))
  13579. (defun org-set-tags-command (&optional arg just-align)
  13580. "Call the set-tags command for the current entry."
  13581. (interactive "P")
  13582. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13583. (org-set-tags arg just-align)
  13584. (save-excursion
  13585. (unless (and (org-region-active-p)
  13586. org-loop-over-headlines-in-active-region)
  13587. (org-back-to-heading t))
  13588. (org-set-tags arg just-align))))
  13589. (defun org-set-tags-to (data)
  13590. "Set the tags of the current entry to DATA, replacing the current tags.
  13591. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13592. If DATA is nil or the empty string, any tags will be removed."
  13593. (interactive "sTags: ")
  13594. (setq data
  13595. (cond
  13596. ((eq data nil) "")
  13597. ((equal data "") "")
  13598. ((stringp data)
  13599. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13600. ":"))
  13601. ((listp data)
  13602. (concat ":" (mapconcat 'identity data ":") ":"))))
  13603. (when data
  13604. (save-excursion
  13605. (org-back-to-heading t)
  13606. (when (looking-at org-complex-heading-regexp)
  13607. (if (match-end 5)
  13608. (progn
  13609. (goto-char (match-beginning 5))
  13610. (insert data)
  13611. (delete-region (point) (point-at-eol))
  13612. (org-set-tags nil 'align))
  13613. (goto-char (point-at-eol))
  13614. (insert " " data)
  13615. (org-set-tags nil 'align)))
  13616. (beginning-of-line 1)
  13617. (if (looking-at ".*?\\([ \t]+\\)$")
  13618. (delete-region (match-beginning 1) (match-end 1))))))
  13619. (defun org-align-all-tags ()
  13620. "Align the tags i all headings."
  13621. (interactive)
  13622. (save-excursion
  13623. (or (ignore-errors (org-back-to-heading t))
  13624. (outline-next-heading))
  13625. (if (org-at-heading-p)
  13626. (org-set-tags t)
  13627. (message "No headings"))))
  13628. (defvar org-indent-indentation-per-level)
  13629. (defun org-set-tags (&optional arg just-align)
  13630. "Set the tags for the current headline.
  13631. With prefix ARG, realign all tags in headings in the current buffer.
  13632. When JUST-ALIGN is non-nil, only align tags."
  13633. (interactive "P")
  13634. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13635. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13636. 'region-start-level
  13637. 'region))
  13638. org-loop-over-headlines-in-active-region)
  13639. (org-map-entries
  13640. ;; We don't use ARG and JUST-ALIGN here because these args
  13641. ;; are not useful when looping over headlines.
  13642. #'org-set-tags
  13643. org-loop-over-headlines-in-active-region
  13644. cl
  13645. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13646. (let ((org-setting-tags t))
  13647. (if arg
  13648. (save-excursion
  13649. (goto-char (point-min))
  13650. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13651. (while (re-search-forward org-outline-regexp-bol nil t)
  13652. (org-set-tags nil t)
  13653. (end-of-line)))
  13654. (message "All tags realigned to column %d" org-tags-column))
  13655. (let* ((current (org-get-tags-string))
  13656. (col (current-column))
  13657. (tags
  13658. (if just-align current
  13659. ;; Get a new set of tags from the user.
  13660. (save-excursion
  13661. (let* ((table
  13662. (setq
  13663. org-last-tags-completion-table
  13664. (append
  13665. org-tag-persistent-alist
  13666. (or org-tag-alist (org-get-buffer-tags))
  13667. (and
  13668. org-complete-tags-always-offer-all-agenda-tags
  13669. (org-global-tags-completion-table
  13670. (org-agenda-files))))))
  13671. (current-tags (org-split-string current ":"))
  13672. (inherited-tags
  13673. (nreverse (nthcdr (length current-tags)
  13674. (nreverse (org-get-tags-at))))))
  13675. (replace-regexp-in-string
  13676. "\\([-+&]+\\|,\\)"
  13677. ":"
  13678. (if (or (eq t org-use-fast-tag-selection)
  13679. (and org-use-fast-tag-selection
  13680. (delq nil (mapcar #'cdr table))))
  13681. (org-fast-tag-selection
  13682. current-tags inherited-tags table
  13683. (and org-fast-tag-selection-include-todo
  13684. org-todo-key-alist))
  13685. (let ((org-add-colon-after-tag-completion
  13686. (< 1 (length table))))
  13687. (org-trim
  13688. (completing-read
  13689. "Tags: "
  13690. #'org-tags-completion-function
  13691. nil nil current 'org-tags-history))))))))))
  13692. (when org-tags-sort-function
  13693. (setq tags
  13694. (mapconcat
  13695. #'identity
  13696. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13697. org-tags-sort-function)
  13698. ":")))
  13699. (if (not (org-string-nw-p tags)) (setq tags "")
  13700. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13701. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13702. ;; Insert new tags at the correct column
  13703. (beginning-of-line)
  13704. (let ((level (if (looking-at org-outline-regexp)
  13705. (- (match-end 0) (point) 1)
  13706. 1)))
  13707. (cond
  13708. ((and (equal current "") (equal tags "")))
  13709. ((re-search-forward
  13710. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13711. (line-end-position)
  13712. t)
  13713. (if (equal tags "") (replace-match "" t t)
  13714. (goto-char (match-beginning 0))
  13715. (let* ((c0 (current-column))
  13716. ;; Compute offset for the case of org-indent-mode
  13717. ;; active.
  13718. (di (if (org-bound-and-true-p org-indent-mode)
  13719. (* (1- org-indent-indentation-per-level)
  13720. (1- level))
  13721. 0))
  13722. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13723. (tc (+ org-tags-column
  13724. (if (> org-tags-column 0) (- di) di)))
  13725. (c1 (max (1+ c0)
  13726. (if (> tc 0) tc
  13727. (- (- tc) (string-width tags)))))
  13728. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13729. (replace-match rpl t t)
  13730. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13731. (tabify p0 (point))))))
  13732. (t (error "Tags alignment failed"))))
  13733. (org-move-to-column col))
  13734. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13735. (defun org-change-tag-in-region (beg end tag off)
  13736. "Add or remove TAG for each entry in the region.
  13737. This works in the agenda, and also in an org-mode buffer."
  13738. (interactive
  13739. (list (region-beginning) (region-end)
  13740. (let ((org-last-tags-completion-table
  13741. (if (derived-mode-p 'org-mode)
  13742. (org-uniquify
  13743. (delq nil (append (org-get-buffer-tags)
  13744. (org-global-tags-completion-table))))
  13745. (org-global-tags-completion-table))))
  13746. (org-icompleting-read
  13747. "Tag: " 'org-tags-completion-function nil nil nil
  13748. 'org-tags-history))
  13749. (progn
  13750. (message "[s]et or [r]emove? ")
  13751. (equal (read-char-exclusive) ?r))))
  13752. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13753. (let ((agendap (equal major-mode 'org-agenda-mode))
  13754. l1 l2 m buf pos newhead (cnt 0))
  13755. (goto-char end)
  13756. (setq l2 (1- (org-current-line)))
  13757. (goto-char beg)
  13758. (setq l1 (org-current-line))
  13759. (loop for l from l1 to l2 do
  13760. (org-goto-line l)
  13761. (setq m (get-text-property (point) 'org-hd-marker))
  13762. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13763. (and agendap m))
  13764. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13765. pos (if agendap m (point)))
  13766. (with-current-buffer buf
  13767. (save-excursion
  13768. (save-restriction
  13769. (goto-char pos)
  13770. (setq cnt (1+ cnt))
  13771. (org-toggle-tag tag (if off 'off 'on))
  13772. (setq newhead (org-get-heading)))))
  13773. (and agendap (org-agenda-change-all-lines newhead m))))
  13774. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13775. (defun org-tags-completion-function (string predicate &optional flag)
  13776. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13777. (confirm (lambda (x) (stringp (car x)))))
  13778. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13779. (setq s1 (match-string 1 string)
  13780. s2 (match-string 2 string))
  13781. (setq s1 "" s2 string))
  13782. (cond
  13783. ((eq flag nil)
  13784. ;; try completion
  13785. (setq rtn (try-completion s2 ctable confirm))
  13786. (if (stringp rtn)
  13787. (setq rtn
  13788. (concat s1 s2 (substring rtn (length s2))
  13789. (if (and org-add-colon-after-tag-completion
  13790. (assoc rtn ctable))
  13791. ":" ""))))
  13792. rtn)
  13793. ((eq flag t)
  13794. ;; all-completions
  13795. (all-completions s2 ctable confirm))
  13796. ((eq flag 'lambda)
  13797. ;; exact match?
  13798. (assoc s2 ctable)))))
  13799. (defun org-fast-tag-insert (kwd tags face &optional end)
  13800. "Insert KDW, and the TAGS, the latter with face FACE.
  13801. Also insert END."
  13802. (insert (format "%-12s" (concat kwd ":"))
  13803. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13804. (or end "")))
  13805. (defun org-fast-tag-show-exit (flag)
  13806. (save-excursion
  13807. (org-goto-line 3)
  13808. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13809. (replace-match ""))
  13810. (when flag
  13811. (end-of-line 1)
  13812. (org-move-to-column (- (window-width) 19) t)
  13813. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13814. (defun org-set-current-tags-overlay (current prefix)
  13815. "Add an overlay to CURRENT tag with PREFIX."
  13816. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13817. (if (featurep 'xemacs)
  13818. (org-overlay-display org-tags-overlay (concat prefix s)
  13819. 'secondary-selection)
  13820. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13821. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13822. (defvar org-last-tag-selection-key nil)
  13823. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13824. "Fast tag selection with single keys.
  13825. CURRENT is the current list of tags in the headline, INHERITED is the
  13826. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13827. possibly with grouping information. TODO-TABLE is a similar table with
  13828. TODO keywords, should these have keys assigned to them.
  13829. If the keys are nil, a-z are automatically assigned.
  13830. Returns the new tags string, or nil to not change the current settings."
  13831. (let* ((fulltable (append table todo-table))
  13832. (maxlen (apply 'max (mapcar
  13833. (lambda (x)
  13834. (if (stringp (car x)) (string-width (car x)) 0))
  13835. fulltable)))
  13836. (buf (current-buffer))
  13837. (expert (eq org-fast-tag-selection-single-key 'expert))
  13838. (buffer-tags nil)
  13839. (fwidth (+ maxlen 3 1 3))
  13840. (ncol (/ (- (window-width) 4) fwidth))
  13841. (i-face 'org-done)
  13842. (c-face 'org-todo)
  13843. tg cnt e c char c1 c2 ntable tbl rtn
  13844. ov-start ov-end ov-prefix
  13845. (exit-after-next org-fast-tag-selection-single-key)
  13846. (done-keywords org-done-keywords)
  13847. groups ingroup intaggroup)
  13848. (save-excursion
  13849. (beginning-of-line 1)
  13850. (if (looking-at
  13851. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13852. (setq ov-start (match-beginning 1)
  13853. ov-end (match-end 1)
  13854. ov-prefix "")
  13855. (setq ov-start (1- (point-at-eol))
  13856. ov-end (1+ ov-start))
  13857. (skip-chars-forward "^\n\r")
  13858. (setq ov-prefix
  13859. (concat
  13860. (buffer-substring (1- (point)) (point))
  13861. (if (> (current-column) org-tags-column)
  13862. " "
  13863. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13864. (move-overlay org-tags-overlay ov-start ov-end)
  13865. (save-window-excursion
  13866. (if expert
  13867. (set-buffer (get-buffer-create " *Org tags*"))
  13868. (delete-other-windows)
  13869. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13870. (org-switch-to-buffer-other-window " *Org tags*"))
  13871. (erase-buffer)
  13872. (org-set-local 'org-done-keywords done-keywords)
  13873. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13874. (org-fast-tag-insert "Current" current c-face "\n\n")
  13875. (org-fast-tag-show-exit exit-after-next)
  13876. (org-set-current-tags-overlay current ov-prefix)
  13877. (setq tbl fulltable char ?a cnt 0)
  13878. (while (setq e (pop tbl))
  13879. (cond
  13880. ((eq (car e) :startgroup)
  13881. (push '() groups) (setq ingroup t)
  13882. (unless (zerop cnt)
  13883. (setq cnt 0)
  13884. (insert "\n"))
  13885. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13886. ((eq (car e) :endgroup)
  13887. (setq ingroup nil cnt 0)
  13888. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13889. ((eq (car e) :startgrouptag)
  13890. (setq intaggroup t)
  13891. (unless (zerop cnt)
  13892. (setq cnt 0)
  13893. (insert "\n"))
  13894. (insert "[ "))
  13895. ((eq (car e) :endgrouptag)
  13896. (setq intaggroup nil cnt 0)
  13897. (insert "]\n"))
  13898. ((equal e '(:newline))
  13899. (unless (zerop cnt)
  13900. (setq cnt 0)
  13901. (insert "\n")
  13902. (setq e (car tbl))
  13903. (while (equal (car tbl) '(:newline))
  13904. (insert "\n")
  13905. (setq tbl (cdr tbl)))))
  13906. ((equal e '(:grouptags)) (insert " : "))
  13907. (t
  13908. (setq tg (copy-sequence (car e)) c2 nil)
  13909. (if (cdr e)
  13910. (setq c (cdr e))
  13911. ;; automatically assign a character.
  13912. (setq c1 (string-to-char
  13913. (downcase (substring
  13914. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13915. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13916. (while (or (rassoc char ntable) (rassoc char table))
  13917. (setq char (1+ char)))
  13918. (setq c2 c1))
  13919. (setq c (or c2 char)))
  13920. (when ingroup (push tg (car groups)))
  13921. (setq tg (org-add-props tg nil 'face
  13922. (cond
  13923. ((not (assoc tg table))
  13924. (org-get-todo-face tg))
  13925. ((member tg current) c-face)
  13926. ((member tg inherited) i-face))))
  13927. (when (equal (caar tbl) :grouptags)
  13928. (org-add-props tg nil 'face 'org-tag-group))
  13929. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13930. (insert "[" c "] " tg (make-string
  13931. (- fwidth 4 (length tg)) ?\ ))
  13932. (push (cons tg c) ntable)
  13933. (when (= (incf cnt) ncol)
  13934. (insert "\n")
  13935. (when (or ingroup intaggroup) (insert " "))
  13936. (setq cnt 0)))))
  13937. (setq ntable (nreverse ntable))
  13938. (insert "\n")
  13939. (goto-char (point-min))
  13940. (unless expert (org-fit-window-to-buffer))
  13941. (setq rtn
  13942. (catch 'exit
  13943. (while t
  13944. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13945. (if (not groups) "no " "")
  13946. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13947. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13948. (setq org-last-tag-selection-key c)
  13949. (cond
  13950. ((= c ?\r) (throw 'exit t))
  13951. ((= c ?!)
  13952. (setq groups (not groups))
  13953. (goto-char (point-min))
  13954. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13955. ((= c ?\C-c)
  13956. (if (not expert)
  13957. (org-fast-tag-show-exit
  13958. (setq exit-after-next (not exit-after-next)))
  13959. (setq expert nil)
  13960. (delete-other-windows)
  13961. (set-window-buffer (split-window-vertically) " *Org tags*")
  13962. (org-switch-to-buffer-other-window " *Org tags*")
  13963. (org-fit-window-to-buffer)))
  13964. ((or (= c ?\C-g)
  13965. (and (= c ?q) (not (rassoc c ntable))))
  13966. (org-detach-overlay org-tags-overlay)
  13967. (setq quit-flag t))
  13968. ((= c ?\ )
  13969. (setq current nil)
  13970. (when exit-after-next (setq exit-after-next 'now)))
  13971. ((= c ?\t)
  13972. (condition-case nil
  13973. (setq tg (org-icompleting-read
  13974. "Tag: "
  13975. (or buffer-tags
  13976. (with-current-buffer buf
  13977. (setq buffer-tags
  13978. (org-get-buffer-tags))))))
  13979. (quit (setq tg "")))
  13980. (when (string-match "\\S-" tg)
  13981. (add-to-list 'buffer-tags (list tg))
  13982. (if (member tg current)
  13983. (setq current (delete tg current))
  13984. (push tg current)))
  13985. (when exit-after-next (setq exit-after-next 'now)))
  13986. ((setq e (rassoc c todo-table) tg (car e))
  13987. (with-current-buffer buf
  13988. (save-excursion (org-todo tg)))
  13989. (when exit-after-next (setq exit-after-next 'now)))
  13990. ((setq e (rassoc c ntable) tg (car e))
  13991. (if (member tg current)
  13992. (setq current (delete tg current))
  13993. (loop for g in groups do
  13994. (when (member tg g)
  13995. (dolist (x g) (setq current (delete x current)))))
  13996. (push tg current))
  13997. (when exit-after-next (setq exit-after-next 'now))))
  13998. ;; Create a sorted list
  13999. (setq current
  14000. (sort current
  14001. (lambda (a b)
  14002. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14003. (when (eq exit-after-next 'now) (throw 'exit t))
  14004. (goto-char (point-min))
  14005. (beginning-of-line 2)
  14006. (delete-region (point) (point-at-eol))
  14007. (org-fast-tag-insert "Current" current c-face)
  14008. (org-set-current-tags-overlay current ov-prefix)
  14009. (while (re-search-forward
  14010. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14011. (setq tg (match-string 1))
  14012. (add-text-properties
  14013. (match-beginning 1) (match-end 1)
  14014. (list 'face
  14015. (cond
  14016. ((member tg current) c-face)
  14017. ((member tg inherited) i-face)
  14018. (t (get-text-property (match-beginning 1) 'face))))))
  14019. (goto-char (point-min)))))
  14020. (org-detach-overlay org-tags-overlay)
  14021. (if rtn
  14022. (mapconcat 'identity current ":")
  14023. nil))))
  14024. (defun org-get-tags-string ()
  14025. "Get the TAGS string in the current headline."
  14026. (unless (org-at-heading-p t)
  14027. (user-error "Not on a heading"))
  14028. (save-excursion
  14029. (beginning-of-line 1)
  14030. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14031. (org-match-string-no-properties 1)
  14032. "")))
  14033. (defun org-get-tags ()
  14034. "Get the list of tags specified in the current headline."
  14035. (org-split-string (org-get-tags-string) ":"))
  14036. (defun org-get-buffer-tags ()
  14037. "Get a table of all tags used in the buffer, for completion."
  14038. (org-with-wide-buffer
  14039. (goto-char (point-min))
  14040. (let ((tag-re (concat org-outline-regexp-bol
  14041. "\\(?:.*?[ \t]\\)?"
  14042. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14043. tags)
  14044. (while (re-search-forward tag-re nil t)
  14045. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14046. (push tag tags)))
  14047. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14048. ;;;; The mapping API
  14049. (defun org-map-entries (func &optional match scope &rest skip)
  14050. "Call FUNC at each headline selected by MATCH in SCOPE.
  14051. FUNC is a function or a lisp form. The function will be called without
  14052. arguments, with the cursor positioned at the beginning of the headline.
  14053. The return values of all calls to the function will be collected and
  14054. returned as a list.
  14055. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14056. does not need to preserve point. After evaluation, the cursor will be
  14057. moved to the end of the line (presumably of the headline of the
  14058. processed entry) and search continues from there. Under some
  14059. circumstances, this may not produce the wanted results. For example,
  14060. if you have removed (e.g. archived) the current (sub)tree it could
  14061. mean that the next entry will be skipped entirely. In such cases, you
  14062. can specify the position from where search should continue by making
  14063. FUNC set the variable `org-map-continue-from' to the desired buffer
  14064. position.
  14065. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14066. Only headlines that are matched by this query will be considered during
  14067. the iteration. When MATCH is nil or t, all headlines will be
  14068. visited by the iteration.
  14069. SCOPE determines the scope of this command. It can be any of:
  14070. nil The current buffer, respecting the restriction if any
  14071. tree The subtree started with the entry at point
  14072. region The entries within the active region, if any
  14073. region-start-level
  14074. The entries within the active region, but only those at
  14075. the same level than the first one.
  14076. file The current buffer, without restriction
  14077. file-with-archives
  14078. The current buffer, and any archives associated with it
  14079. agenda All agenda files
  14080. agenda-with-archives
  14081. All agenda files with any archive files associated with them
  14082. \(file1 file2 ...)
  14083. If this is a list, all files in the list will be scanned
  14084. The remaining args are treated as settings for the skipping facilities of
  14085. the scanner. The following items can be given here:
  14086. archive skip trees with the archive tag
  14087. comment skip trees with the COMMENT keyword
  14088. function or Emacs Lisp form:
  14089. will be used as value for `org-agenda-skip-function', so
  14090. whenever the function returns a position, FUNC will not be
  14091. called for that entry and search will continue from the
  14092. position returned
  14093. If your function needs to retrieve the tags including inherited tags
  14094. at the *current* entry, you can use the value of the variable
  14095. `org-scanner-tags' which will be much faster than getting the value
  14096. with `org-get-tags-at'. If your function gets properties with
  14097. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14098. to t around the call to `org-entry-properties' to get the same speedup.
  14099. Note that if your function moves around to retrieve tags and properties at
  14100. a *different* entry, you cannot use these techniques."
  14101. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14102. (not (org-region-active-p)))
  14103. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14104. (org-agenda-skip-archived-trees (memq 'archive skip))
  14105. (org-agenda-skip-comment-trees (memq 'comment skip))
  14106. (org-agenda-skip-function
  14107. (car (org-delete-all '(comment archive) skip)))
  14108. (org-tags-match-list-sublevels t)
  14109. (start-level (eq scope 'region-start-level))
  14110. matcher file res
  14111. org-todo-keywords-for-agenda
  14112. org-done-keywords-for-agenda
  14113. org-todo-keyword-alist-for-agenda
  14114. org-tag-alist-for-agenda
  14115. todo-only)
  14116. (cond
  14117. ((eq match t) (setq matcher t))
  14118. ((eq match nil) (setq matcher t))
  14119. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14120. (save-excursion
  14121. (save-restriction
  14122. (cond ((eq scope 'tree)
  14123. (org-back-to-heading t)
  14124. (org-narrow-to-subtree)
  14125. (setq scope nil))
  14126. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14127. (org-region-active-p))
  14128. ;; If needed, set start-level to a string like "2"
  14129. (when start-level
  14130. (save-excursion
  14131. (goto-char (region-beginning))
  14132. (unless (org-at-heading-p) (outline-next-heading))
  14133. (setq start-level (org-current-level))))
  14134. (narrow-to-region (region-beginning)
  14135. (save-excursion
  14136. (goto-char (region-end))
  14137. (unless (and (bolp) (org-at-heading-p))
  14138. (outline-next-heading))
  14139. (point)))
  14140. (setq scope nil)))
  14141. (if (not scope)
  14142. (progn
  14143. (org-agenda-prepare-buffers
  14144. (and buffer-file-name (list buffer-file-name)))
  14145. (setq res (org-scan-tags func matcher todo-only start-level)))
  14146. ;; Get the right scope
  14147. (cond
  14148. ((and scope (listp scope) (symbolp (car scope)))
  14149. (setq scope (eval scope)))
  14150. ((eq scope 'agenda)
  14151. (setq scope (org-agenda-files t)))
  14152. ((eq scope 'agenda-with-archives)
  14153. (setq scope (org-agenda-files t))
  14154. (setq scope (org-add-archive-files scope)))
  14155. ((eq scope 'file)
  14156. (setq scope (and buffer-file-name (list buffer-file-name))))
  14157. ((eq scope 'file-with-archives)
  14158. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14159. (org-agenda-prepare-buffers scope)
  14160. (dolist (file scope)
  14161. (with-current-buffer (org-find-base-buffer-visiting file)
  14162. (save-excursion
  14163. (save-restriction
  14164. (widen)
  14165. (goto-char (point-min))
  14166. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14167. res)))
  14168. ;;; Properties API
  14169. (defconst org-special-properties
  14170. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14171. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14172. "The special properties valid in Org mode.
  14173. These are properties that are not defined in the property drawer,
  14174. but in some other way.")
  14175. (defconst org-default-properties
  14176. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14177. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14178. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14179. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14180. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14181. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14182. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14183. "Some properties that are used by Org mode for various purposes.
  14184. Being in this list makes sure that they are offered for completion.")
  14185. (defun org--update-property-plist (key val props)
  14186. "Associate KEY to VAL in alist PROPS.
  14187. Modifications are made by side-effect. Return new alist."
  14188. (let* ((appending (string= (substring key -1) "+"))
  14189. (key (if appending (substring key 0 -1) key))
  14190. (old (assoc-string key props t)))
  14191. (if (not old) (cons (cons key val) props)
  14192. (setcdr old (if appending (concat (cdr old) " " val) val))
  14193. props)))
  14194. (defun org-get-property-block (&optional beg force)
  14195. "Return the (beg . end) range of the body of the property drawer.
  14196. BEG is the beginning of the current subtree, or of the part
  14197. before the first headline. If it is not given, it will be found.
  14198. If the drawer does not exist, create it if FORCE is non-nil, or
  14199. return nil."
  14200. (org-with-wide-buffer
  14201. (when beg (goto-char beg))
  14202. (unless (org-before-first-heading-p)
  14203. (let ((beg (cond (beg)
  14204. ((or (not (featurep 'org-inlinetask))
  14205. (org-inlinetask-in-task-p))
  14206. (org-back-to-heading t))
  14207. (t (org-with-limited-levels (org-back-to-heading t))))))
  14208. (forward-line)
  14209. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14210. (cond ((looking-at org-property-drawer-re)
  14211. (forward-line)
  14212. (cons (point) (progn (goto-char (match-end 0))
  14213. (line-beginning-position))))
  14214. (force
  14215. (goto-char beg)
  14216. (org-insert-property-drawer)
  14217. (let ((pos (save-excursion (search-forward ":END:")
  14218. (line-beginning-position))))
  14219. (cons pos pos))))))))
  14220. (defun org-at-property-p ()
  14221. "Non-nil when point is inside a property drawer.
  14222. See `org-property-re' for match data, if applicable."
  14223. (save-excursion
  14224. (beginning-of-line)
  14225. (and (looking-at org-property-re)
  14226. (let ((property-drawer (save-match-data (org-get-property-block))))
  14227. (and property-drawer
  14228. (>= (point) (car property-drawer))
  14229. (< (point) (cdr property-drawer)))))))
  14230. (defun org-property-action ()
  14231. "Do an action on properties."
  14232. (interactive)
  14233. (unless (org-at-property-p) (user-error "Not at a property"))
  14234. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14235. (let ((c (read-char-exclusive)))
  14236. (case c
  14237. (?s (call-interactively #'org-set-property))
  14238. (?d (call-interactively #'org-delete-property))
  14239. (?D (call-interactively #'org-delete-property-globally))
  14240. (?c (call-interactively #'org-compute-property-at-point))
  14241. (otherwise (user-error "No such property action %c" c)))))
  14242. (defun org-inc-effort ()
  14243. "Increment the value of the effort property in the current entry."
  14244. (interactive)
  14245. (org-set-effort nil t))
  14246. (defvar org-clock-effort) ; Defined in org-clock.el.
  14247. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14248. (defun org-set-effort (&optional value increment)
  14249. "Set the effort property of the current entry.
  14250. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14251. 10th allowed value.
  14252. When INCREMENT is non-nil, set the property to the next allowed value."
  14253. (interactive "P")
  14254. (if (equal value 0) (setq value 10))
  14255. (let* ((completion-ignore-case t)
  14256. (prop org-effort-property)
  14257. (cur (org-entry-get nil prop))
  14258. (allowed (org-property-get-allowed-values nil prop 'table))
  14259. (existing (mapcar 'list (org-property-values prop)))
  14260. (heading (nth 4 (org-heading-components)))
  14261. rpl
  14262. (val (cond
  14263. ((stringp value) value)
  14264. ((and allowed (integerp value))
  14265. (or (car (nth (1- value) allowed))
  14266. (car (org-last allowed))))
  14267. ((and allowed increment)
  14268. (or (caadr (member (list cur) allowed))
  14269. (user-error "Allowed effort values are not set")))
  14270. (allowed
  14271. (message "Select 1-9,0, [RET%s]: %s"
  14272. (if cur (concat "=" cur) "")
  14273. (mapconcat 'car allowed " "))
  14274. (setq rpl (read-char-exclusive))
  14275. (if (equal rpl ?\r)
  14276. cur
  14277. (setq rpl (- rpl ?0))
  14278. (if (equal rpl 0) (setq rpl 10))
  14279. (if (and (> rpl 0) (<= rpl (length allowed)))
  14280. (car (nth (1- rpl) allowed))
  14281. (org-completing-read "Effort: " allowed nil))))
  14282. (t
  14283. (let (org-completion-use-ido org-completion-use-iswitchb)
  14284. (org-completing-read
  14285. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14286. (concat "[" cur "]") "")
  14287. ": ")
  14288. existing nil nil "" nil cur))))))
  14289. (unless (equal (org-entry-get nil prop) val)
  14290. (org-entry-put nil prop val))
  14291. (org-refresh-property
  14292. '((effort . identity)
  14293. (effort-minutes . org-duration-string-to-minutes))
  14294. val)
  14295. (when (string= heading org-clock-current-task)
  14296. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14297. (org-clock-update-mode-line))
  14298. (message "%s is now %s" prop val)))
  14299. (defun org-entry-properties (&optional pom which)
  14300. "Get all properties of the current entry.
  14301. When POM is a buffer position, get all properties from the entry
  14302. there instead.
  14303. This includes the TODO keyword, the tags, time strings for
  14304. deadline, scheduled, and clocking, and any additional properties
  14305. defined in the entry.
  14306. If WHICH is nil or `all', get all properties. If WHICH is
  14307. `special' or `standard', only get that subclass. If WHICH is
  14308. a string, only get that property.
  14309. Return value is an alist. Keys are properties, as upcased
  14310. strings."
  14311. (org-with-point-at pom
  14312. (when (and (derived-mode-p 'org-mode)
  14313. (ignore-errors (org-back-to-heading t)))
  14314. (catch 'exit
  14315. (let* ((beg (point))
  14316. (specific (and (stringp which) (upcase which)))
  14317. (which (cond ((not specific) which)
  14318. ((member specific org-special-properties) 'special)
  14319. (t 'standard)))
  14320. props)
  14321. ;; Get the special properties, like TODO and TAGS.
  14322. (when (memq which '(nil all special))
  14323. (when (or (not specific) (string= specific "CLOCKSUM"))
  14324. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14325. (when clocksum
  14326. (push (cons "CLOCKSUM"
  14327. (org-columns-number-to-string
  14328. (/ (float clocksum) 60.) 'add_times))
  14329. props)))
  14330. (when specific (throw 'exit props)))
  14331. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14332. (let ((clocksumt (get-text-property (point)
  14333. :org-clock-minutes-today)))
  14334. (when clocksumt
  14335. (push (cons "CLOCKSUM_T"
  14336. (org-columns-number-to-string
  14337. (/ (float clocksumt) 60.) 'add_times))
  14338. props)))
  14339. (when specific (throw 'exit props)))
  14340. (when (or (not specific) (string= specific "ITEM"))
  14341. (when (looking-at org-complex-heading-regexp)
  14342. (push (cons "ITEM"
  14343. (concat
  14344. (org-match-string-no-properties 1)
  14345. (let ((title (org-match-string-no-properties 4)))
  14346. (when (org-string-nw-p title)
  14347. (concat " " (org-remove-tabs title))))))
  14348. props))
  14349. (when specific (throw 'exit props)))
  14350. (when (or (not specific) (string= specific "TODO"))
  14351. (let ((case-fold-search nil))
  14352. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14353. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14354. (when specific (throw 'exit props)))
  14355. (when (or (not specific) (string= specific "PRIORITY"))
  14356. (when (looking-at org-priority-regexp)
  14357. (push (cons "PRIORITY" (org-match-string-no-properties 2))
  14358. props))
  14359. (when specific (throw 'exit props)))
  14360. (when (or (not specific) (string= specific "FILE"))
  14361. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14362. props)
  14363. (when specific (throw 'exit props)))
  14364. (when (or (not specific) (string= specific "TAGS"))
  14365. (let ((value (org-string-nw-p (org-get-tags-string))))
  14366. (when value (push (cons "TAGS" value) props)))
  14367. (when specific (throw 'exit props)))
  14368. (when (or (not specific) (string= specific "ALLTAGS"))
  14369. (let ((value (org-get-tags-at)))
  14370. (when value
  14371. (push (cons "ALLTAGS"
  14372. (format ":%s:" (mapconcat #'identity value ":")))
  14373. props)))
  14374. (when specific (throw 'exit props)))
  14375. (when (or (not specific) (string= specific "BLOCKED"))
  14376. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14377. (when specific (throw 'exit props)))
  14378. (when (or (not specific)
  14379. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14380. (forward-line)
  14381. (when (org-looking-at-p org-planning-line-re)
  14382. (end-of-line)
  14383. (let ((bol (line-beginning-position))
  14384. ;; Backward compatibility: time keywords used to
  14385. ;; be configurable (before 8.3). Make sure we
  14386. ;; get the correct keyword.
  14387. (key-assoc `(("CLOSED" . ,org-closed-string)
  14388. ("DEADLINE" . ,org-deadline-string)
  14389. ("SCHEDULED" . ,org-scheduled-string))))
  14390. (dolist (pair (if specific (list (assoc specific key-assoc))
  14391. key-assoc))
  14392. (save-excursion
  14393. (when (search-backward (cdr pair) bol t)
  14394. (goto-char (match-end 0))
  14395. (skip-chars-forward " \t")
  14396. (and (looking-at org-ts-regexp-both)
  14397. (push (cons (car pair)
  14398. (org-match-string-no-properties 0))
  14399. props)))))))
  14400. (when specific (throw 'exit props)))
  14401. (when (or (not specific)
  14402. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14403. (let ((find-ts
  14404. (lambda (end ts)
  14405. (let ((regexp (cond
  14406. ((string= specific "TIMESTAMP")
  14407. org-ts-regexp)
  14408. ((string= specific "TIMESTAMP_IA")
  14409. org-ts-regexp-inactive)
  14410. ((assoc "TIMESTAMP_IA" ts)
  14411. org-ts-regexp)
  14412. ((assoc "TIMESTAMP" ts)
  14413. org-ts-regexp-inactive)
  14414. (t org-ts-regexp-both))))
  14415. (catch 'next
  14416. (while (re-search-forward regexp end t)
  14417. (backward-char)
  14418. (let ((object (org-element-context)))
  14419. ;; Accept to match timestamps in node
  14420. ;; properties, too.
  14421. (when (memq (org-element-type object)
  14422. '(node-property timestamp))
  14423. (let ((type
  14424. (org-element-property :type object)))
  14425. (cond
  14426. ((and (memq type '(active active-range))
  14427. (not (equal specific "TIMESTAMP_IA")))
  14428. (unless (assoc "TIMESTAMP" ts)
  14429. (push (cons "TIMESTAMP"
  14430. (org-element-property
  14431. :raw-value object))
  14432. ts)
  14433. (when specific (throw 'exit ts))))
  14434. ((and (memq type '(inactive inactive-range))
  14435. (not (string= specific "TIMESTAMP")))
  14436. (unless (assoc "TIMESTAMP_IA" ts)
  14437. (push (cons "TIMESTAMP_IA"
  14438. (org-element-property
  14439. :raw-value object))
  14440. ts)
  14441. (when specific (throw 'exit ts))))))
  14442. ;; Both timestamp types are found,
  14443. ;; move to next part.
  14444. (when (= (length ts) 2) (throw 'next ts)))))
  14445. ts)))))
  14446. (goto-char beg)
  14447. ;; First look for timestamps within headline.
  14448. (let ((ts (funcall find-ts (line-end-position) nil)))
  14449. (if (= (length ts) 2) (setq props (nconc ts props))
  14450. (forward-line)
  14451. ;; Then find timestamps in the section, skipping
  14452. ;; planning line.
  14453. (when (org-looking-at-p org-planning-line-re)
  14454. (forward-line))
  14455. (let ((end (save-excursion (outline-next-heading))))
  14456. (setq props (nconc (funcall find-ts end ts) props))))))))
  14457. ;; Get the standard properties, like :PROP:.
  14458. (when (memq which '(nil all standard))
  14459. ;; If we are looking after a specific property, delegate
  14460. ;; to `org-entry-get', which is faster. However, make an
  14461. ;; exception for "CATEGORY", since it can be also set
  14462. ;; through keywords (i.e. #+CATEGORY).
  14463. (if (and specific (not (equal specific "CATEGORY")))
  14464. (let ((value (org-entry-get beg specific nil t)))
  14465. (throw 'exit (and value (list (cons specific value)))))
  14466. (let ((range (org-get-property-block beg)))
  14467. (when range
  14468. (let ((end (cdr range)) seen-base)
  14469. (goto-char (car range))
  14470. ;; Unlike to `org--update-property-plist', we
  14471. ;; handle the case where base values is found
  14472. ;; after its extension. We also forbid standard
  14473. ;; properties to be named as special properties.
  14474. (while (re-search-forward org-property-re end t)
  14475. (let* ((key (upcase (org-match-string-no-properties 2)))
  14476. (extendp (org-string-match-p "\\+\\'" key))
  14477. (key-base (if extendp (substring key 0 -1) key))
  14478. (value (org-match-string-no-properties 3)))
  14479. (cond
  14480. ((member-ignore-case key-base org-special-properties))
  14481. (extendp
  14482. (setq props
  14483. (org--update-property-plist key value props)))
  14484. ((member key seen-base))
  14485. (t (push key seen-base)
  14486. (let ((p (assoc-string key props t)))
  14487. (if p (setcdr p (concat value " " (cdr p)))
  14488. (push (cons key value) props))))))))))))
  14489. (unless (assoc "CATEGORY" props)
  14490. (push (cons "CATEGORY" (org-get-category beg)) props)
  14491. (when (string= specific "CATEGORY") (throw 'exit props)))
  14492. ;; Return value.
  14493. props)))))
  14494. (defun org-property--local-values (property literal-nil)
  14495. "Return value for PROPERTY in current entry.
  14496. Value is a list whose care is the base value for PROPERTY and cdr
  14497. a list of accumulated values. Return nil if neither is found in
  14498. the entry. Also return nil when PROPERTY is set to \"nil\",
  14499. unless LITERAL-NIL is non-nil."
  14500. (let ((range (org-get-property-block)))
  14501. (when range
  14502. (goto-char (car range))
  14503. (let* ((case-fold-search t)
  14504. (end (cdr range))
  14505. (value
  14506. ;; Base value.
  14507. (save-excursion
  14508. (let ((v (and (re-search-forward
  14509. (org-re-property property nil t) end t)
  14510. (org-match-string-no-properties 3))))
  14511. (list (if literal-nil v (org-not-nil v)))))))
  14512. ;; Find additional values.
  14513. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14514. (while (re-search-forward property+ end t)
  14515. (push (org-match-string-no-properties 3) value)))
  14516. ;; Return final values.
  14517. (and (not (equal value '(nil))) (nreverse value))))))
  14518. (defun org-entry-get (pom property &optional inherit literal-nil)
  14519. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14520. If INHERIT is non-nil and the entry does not have the property,
  14521. then also check higher levels of the hierarchy. If INHERIT is
  14522. the symbol `selective', use inheritance only if the setting in
  14523. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14524. If the property is present but empty, the return value is the
  14525. empty string. If the property is not present at all, nil is
  14526. returned. In any other case, return the value as a string.
  14527. Search is case-insensitive.
  14528. If LITERAL-NIL is set, return the string value \"nil\" as
  14529. a string, do not interpret it as the list atom nil. This is used
  14530. for inheritance when a \"nil\" value can supersede a non-nil
  14531. value higher up the hierarchy."
  14532. (org-with-point-at pom
  14533. (cond
  14534. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14535. ;; We need a special property. Use `org-entry-properties' to
  14536. ;; retrieve it, but specify the wanted property.
  14537. (cdr (assoc-string property (org-entry-properties nil property))))
  14538. ((and inherit
  14539. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14540. (org-entry-get-with-inheritance property literal-nil))
  14541. (t
  14542. (let* ((local (org-property--local-values property literal-nil))
  14543. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14544. (if literal-nil value (org-not-nil value)))))))
  14545. (defun org-property-or-variable-value (var &optional inherit)
  14546. "Check if there is a property fixing the value of VAR.
  14547. If yes, return this value. If not, return the current value of the variable."
  14548. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14549. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14550. (read prop)
  14551. (symbol-value var))))
  14552. (defun org-entry-delete (pom property)
  14553. "Delete PROPERTY from entry at point-or-marker POM.
  14554. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14555. non-nil when a property was removed."
  14556. (unless (member property org-special-properties)
  14557. (org-with-point-at pom
  14558. (let ((range (org-get-property-block)))
  14559. (when range
  14560. (let* ((begin (car range))
  14561. (origin (cdr range))
  14562. (end (copy-marker origin))
  14563. (re (org-re-property
  14564. (concat (regexp-quote property) "\\+?") t t)))
  14565. (goto-char begin)
  14566. (while (re-search-forward re end t)
  14567. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14568. ;; If drawer is empty, remove it altogether.
  14569. (when (= begin end)
  14570. (delete-region (line-beginning-position 0)
  14571. (line-beginning-position 2)))
  14572. ;; Return non-nil if some property was removed.
  14573. (prog1 (/= end origin) (set-marker end nil))))))))
  14574. ;; Multi-values properties are properties that contain multiple values
  14575. ;; These values are assumed to be single words, separated by whitespace.
  14576. (defun org-entry-add-to-multivalued-property (pom property value)
  14577. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14578. (let* ((old (org-entry-get pom property))
  14579. (values (and old (org-split-string old "[ \t]"))))
  14580. (setq value (org-entry-protect-space value))
  14581. (unless (member value values)
  14582. (setq values (append values (list value)))
  14583. (org-entry-put pom property
  14584. (mapconcat 'identity values " ")))))
  14585. (defun org-entry-remove-from-multivalued-property (pom property value)
  14586. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14587. (let* ((old (org-entry-get pom property))
  14588. (values (and old (org-split-string old "[ \t]"))))
  14589. (setq value (org-entry-protect-space value))
  14590. (when (member value values)
  14591. (setq values (delete value values))
  14592. (org-entry-put pom property
  14593. (mapconcat 'identity values " ")))))
  14594. (defun org-entry-member-in-multivalued-property (pom property value)
  14595. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14596. (let* ((old (org-entry-get pom property))
  14597. (values (and old (org-split-string old "[ \t]"))))
  14598. (setq value (org-entry-protect-space value))
  14599. (member value values)))
  14600. (defun org-entry-get-multivalued-property (pom property)
  14601. "Return a list of values in a multivalued property."
  14602. (let* ((value (org-entry-get pom property))
  14603. (values (and value (org-split-string value "[ \t]"))))
  14604. (mapcar 'org-entry-restore-space values)))
  14605. (defun org-entry-put-multivalued-property (pom property &rest values)
  14606. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14607. VALUES should be a list of strings. Spaces will be protected."
  14608. (org-entry-put pom property
  14609. (mapconcat 'org-entry-protect-space values " "))
  14610. (let* ((value (org-entry-get pom property))
  14611. (values (and value (org-split-string value "[ \t]"))))
  14612. (mapcar 'org-entry-restore-space values)))
  14613. (defun org-entry-protect-space (s)
  14614. "Protect spaces and newline in string S."
  14615. (while (string-match " " s)
  14616. (setq s (replace-match "%20" t t s)))
  14617. (while (string-match "\n" s)
  14618. (setq s (replace-match "%0A" t t s)))
  14619. s)
  14620. (defun org-entry-restore-space (s)
  14621. "Restore spaces and newline in string S."
  14622. (while (string-match "%20" s)
  14623. (setq s (replace-match " " t t s)))
  14624. (while (string-match "%0A" s)
  14625. (setq s (replace-match "\n" t t s)))
  14626. s)
  14627. (defvar org-entry-property-inherited-from (make-marker)
  14628. "Marker pointing to the entry from where a property was inherited.
  14629. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14630. location of the entry where the inheritance search matched. If there was
  14631. no match, the marker will point nowhere.
  14632. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14633. is set.")
  14634. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14635. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14636. The search will stop at the first ancestor which has the property defined.
  14637. If the value found is \"nil\", return nil to show that the property
  14638. should be considered as undefined (this is the meaning of nil here).
  14639. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14640. (move-marker org-entry-property-inherited-from nil)
  14641. (org-with-wide-buffer
  14642. (let (value)
  14643. (catch 'exit
  14644. (while t
  14645. (let ((v (org-property--local-values property literal-nil)))
  14646. (when v
  14647. (setq value
  14648. (concat (mapconcat #'identity (delq nil v) " ")
  14649. (and value " ")
  14650. value)))
  14651. (cond
  14652. ((car v)
  14653. (org-back-to-heading t)
  14654. (move-marker org-entry-property-inherited-from (point))
  14655. (throw 'exit nil))
  14656. ((org-up-heading-safe))
  14657. (t
  14658. (let ((global
  14659. (cdr (or (assoc-string property org-file-properties t)
  14660. (assoc-string property org-global-properties t)
  14661. (assoc-string property org-global-properties-fixed t)))))
  14662. (cond ((not global))
  14663. (value (setq value (concat global " " value)))
  14664. (t (setq value global))))
  14665. (throw 'exit nil))))))
  14666. (if literal-nil value (org-not-nil value)))))
  14667. (defvar org-property-changed-functions nil
  14668. "Hook called when the value of a property has changed.
  14669. Each hook function should accept two arguments, the name of the property
  14670. and the new value.")
  14671. (defun org-entry-put (pom property value)
  14672. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14673. If the value is nil, it is converted to the empty string. If
  14674. it is not a string, an error is raised.
  14675. PROPERTY can be any regular property (see
  14676. `org-special-properties'). It can also be \"TODO\",
  14677. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14678. For the last two properties, VALUE may have any of the special
  14679. values \"earlier\" and \"later\". The function then increases or
  14680. decreases scheduled or deadline date by one day."
  14681. (cond ((null value) (setq value ""))
  14682. ((not (stringp value)) (error "Properties values should be strings")))
  14683. (org-with-point-at pom
  14684. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14685. (org-back-to-heading t)
  14686. (org-with-limited-levels (org-back-to-heading t)))
  14687. (let ((beg (point)))
  14688. (cond
  14689. ((equal property "TODO")
  14690. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14691. ((not (member value org-todo-keywords-1))
  14692. (user-error "\"%s\" is not a valid TODO state" value)))
  14693. (org-todo value)
  14694. (org-set-tags nil 'align))
  14695. ((equal property "PRIORITY")
  14696. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14697. (org-set-tags nil 'align))
  14698. ((equal property "SCHEDULED")
  14699. (forward-line)
  14700. (if (and (org-looking-at-p org-planning-line-re)
  14701. (re-search-forward
  14702. org-scheduled-time-regexp (line-end-position) t))
  14703. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14704. ((string= value "later") (org-timestamp-change 1 'day))
  14705. ((string= value "") (org-schedule '(4)))
  14706. (t (org-schedule nil value)))
  14707. (if (member value '("earlier" "later" ""))
  14708. (call-interactively #'org-schedule)
  14709. (org-schedule nil value))))
  14710. ((equal property "DEADLINE")
  14711. (forward-line)
  14712. (if (and (org-looking-at-p org-planning-line-re)
  14713. (re-search-forward
  14714. org-deadline-time-regexp (line-end-position) t))
  14715. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14716. ((string= value "later") (org-timestamp-change 1 'day))
  14717. ((string= value "") (org-deadline '(4)))
  14718. (t (org-deadline nil value)))
  14719. (if (member value '("earlier" "later" ""))
  14720. (call-interactively #'org-deadline)
  14721. (org-deadline nil value))))
  14722. ((member property org-special-properties)
  14723. (error "The %s property cannot be set with `org-entry-put'" property))
  14724. (t
  14725. (let* ((range (org-get-property-block beg 'force))
  14726. (end (cdr range))
  14727. (case-fold-search t))
  14728. (goto-char (car range))
  14729. (if (re-search-forward (org-re-property property nil t) end t)
  14730. (progn (delete-region (match-beginning 0) (match-end 0))
  14731. (goto-char (match-beginning 0)))
  14732. (goto-char end)
  14733. (insert "\n")
  14734. (backward-char))
  14735. (insert ":" property ":")
  14736. (when value (insert " " value))
  14737. (org-indent-line)))))
  14738. (run-hook-with-args 'org-property-changed-functions property value)))
  14739. (defun org-buffer-property-keys (&optional specials defaults columns)
  14740. "Get all property keys in the current buffer.
  14741. When SPECIALS is non-nil, also list the special properties that
  14742. reflect things like tags and TODO state.
  14743. When DEFAULTS is non-nil, also include properties that has
  14744. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14745. DESCRIPTION, LOCATION, and LOGGING and others.
  14746. When COLUMNS in non-nil, also include property names given in
  14747. COLUMN formats in the current buffer."
  14748. (let ((case-fold-search t)
  14749. (props (append
  14750. (and specials org-special-properties)
  14751. (and defaults (cons org-effort-property org-default-properties))
  14752. nil)))
  14753. (org-with-wide-buffer
  14754. (goto-char (point-min))
  14755. (while (re-search-forward org-property-start-re nil t)
  14756. (let ((range (org-get-property-block)))
  14757. (catch 'skip
  14758. (unless range
  14759. (when (and (not (org-before-first-heading-p))
  14760. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14761. (line-beginning-position))))
  14762. (org-get-property-block nil t))
  14763. (throw 'skip nil))
  14764. (goto-char (car range))
  14765. (let ((begin (car range))
  14766. (end (cdr range)))
  14767. ;; Make sure that found property block is not located
  14768. ;; before current point, as it would generate an infloop.
  14769. ;; It can happen, for example, in the following
  14770. ;; situation:
  14771. ;;
  14772. ;; * Headline
  14773. ;; :PROPERTIES:
  14774. ;; ...
  14775. ;; :END:
  14776. ;; *************** Inlinetask
  14777. ;; #+BEGIN_EXAMPLE
  14778. ;; :PROPERTIES:
  14779. ;; #+END_EXAMPLE
  14780. ;;
  14781. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14782. (while (< (point) end)
  14783. (let ((p (progn (looking-at org-property-re)
  14784. (org-match-string-no-properties 2))))
  14785. ;; Only add true property name, not extension symbol.
  14786. (add-to-list 'props
  14787. (if (not (org-string-match-p "\\+\\'" p)) p
  14788. (substring p 0 -1))))
  14789. (forward-line))))
  14790. (outline-next-heading)))
  14791. (when columns
  14792. (goto-char (point-min))
  14793. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14794. (let ((element (org-element-at-point)))
  14795. (when (memq (org-element-type element) '(keyword node-property))
  14796. (let ((value (org-element-property :value element))
  14797. (start 0))
  14798. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14799. (setq start (match-end 0))
  14800. (let ((p (org-match-string-no-properties 1 value)))
  14801. (unless (member-ignore-case p org-special-properties)
  14802. (add-to-list 'props p))))))))))
  14803. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14804. (defun org-property-values (key)
  14805. "List all non-nil values of property KEY in current buffer."
  14806. (org-with-wide-buffer
  14807. (goto-char (point-min))
  14808. (let ((case-fold-search t)
  14809. (re (org-re-property key))
  14810. values)
  14811. (while (re-search-forward re nil t)
  14812. (add-to-list 'values (org-entry-get (point) key)))
  14813. values)))
  14814. (defun org-insert-property-drawer ()
  14815. "Insert a property drawer into the current entry."
  14816. (org-with-wide-buffer
  14817. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14818. (org-back-to-heading t)
  14819. (org-with-limited-levels (org-back-to-heading t)))
  14820. (forward-line)
  14821. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14822. (unless (org-looking-at-p org-property-drawer-re)
  14823. (let ((inhibit-read-only t))
  14824. (unless (bolp) (insert "\n"))
  14825. (let ((begin (point)))
  14826. (insert ":PROPERTIES:\n:END:\n")
  14827. (org-indent-region begin (point)))))))
  14828. (defun org-insert-drawer (&optional arg drawer)
  14829. "Insert a drawer at point.
  14830. When optional argument ARG is non-nil, insert a property drawer.
  14831. Optional argument DRAWER, when non-nil, is a string representing
  14832. drawer's name. Otherwise, the user is prompted for a name.
  14833. If a region is active, insert the drawer around that region
  14834. instead.
  14835. Point is left between drawer's boundaries."
  14836. (interactive "P")
  14837. (let* ((drawer (if arg "PROPERTIES"
  14838. (or drawer (read-from-minibuffer "Drawer: ")))))
  14839. (cond
  14840. ;; With C-u, fall back on `org-insert-property-drawer'
  14841. (arg (org-insert-property-drawer))
  14842. ;; Check validity of suggested drawer's name.
  14843. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14844. (user-error "Invalid drawer name"))
  14845. ;; With an active region, insert a drawer at point.
  14846. ((not (org-region-active-p))
  14847. (progn
  14848. (unless (bolp) (insert "\n"))
  14849. (insert (format ":%s:\n\n:END:\n" drawer))
  14850. (forward-line -2)))
  14851. ;; Otherwise, insert the drawer at point
  14852. (t
  14853. (let ((rbeg (region-beginning))
  14854. (rend (copy-marker (region-end))))
  14855. (unwind-protect
  14856. (progn
  14857. (goto-char rbeg)
  14858. (beginning-of-line)
  14859. (when (save-excursion
  14860. (re-search-forward org-outline-regexp-bol rend t))
  14861. (user-error "Drawers cannot contain headlines"))
  14862. ;; Position point at the beginning of the first
  14863. ;; non-blank line in region. Insert drawer's opening
  14864. ;; there, then indent it.
  14865. (org-skip-whitespace)
  14866. (beginning-of-line)
  14867. (insert ":" drawer ":\n")
  14868. (forward-line -1)
  14869. (indent-for-tab-command)
  14870. ;; Move point to the beginning of the first blank line
  14871. ;; after the last non-blank line in region. Insert
  14872. ;; drawer's closing, then indent it.
  14873. (goto-char rend)
  14874. (skip-chars-backward " \r\t\n")
  14875. (insert "\n:END:")
  14876. (deactivate-mark t)
  14877. (indent-for-tab-command)
  14878. (unless (eolp) (insert "\n")))
  14879. ;; Clear marker, whatever the outcome of insertion is.
  14880. (set-marker rend nil)))))))
  14881. (defvar org-property-set-functions-alist nil
  14882. "Property set function alist.
  14883. Each entry should have the following format:
  14884. (PROPERTY . READ-FUNCTION)
  14885. The read function will be called with the same argument as
  14886. `org-completing-read'.")
  14887. (defun org-set-property-function (property)
  14888. "Get the function that should be used to set PROPERTY.
  14889. This is computed according to `org-property-set-functions-alist'."
  14890. (or (cdr (assoc property org-property-set-functions-alist))
  14891. 'org-completing-read))
  14892. (defun org-read-property-value (property)
  14893. "Read PROPERTY value from user."
  14894. (let* ((completion-ignore-case t)
  14895. (allowed (org-property-get-allowed-values nil property 'table))
  14896. (cur (org-entry-get nil property))
  14897. (prompt (concat property " value"
  14898. (if (and cur (string-match "\\S-" cur))
  14899. (concat " [" cur "]") "") ": "))
  14900. (set-function (org-set-property-function property))
  14901. (val (if allowed
  14902. (funcall set-function prompt allowed nil
  14903. (not (get-text-property 0 'org-unrestricted
  14904. (caar allowed))))
  14905. (let (org-completion-use-ido org-completion-use-iswitchb)
  14906. (funcall set-function prompt
  14907. (mapcar 'list (org-property-values property))
  14908. nil nil "" nil cur)))))
  14909. (org-trim val)))
  14910. (defvar org-last-set-property nil)
  14911. (defvar org-last-set-property-value nil)
  14912. (defun org-read-property-name ()
  14913. "Read a property name."
  14914. (let ((completion-ignore-case t)
  14915. (default-prop (or (and (org-at-property-p)
  14916. (org-match-string-no-properties 2))
  14917. org-last-set-property)))
  14918. (org-completing-read
  14919. (concat "Property"
  14920. (if default-prop (concat " [" default-prop "]") "")
  14921. ": ")
  14922. (mapcar #'list (org-buffer-property-keys nil t t))
  14923. nil nil nil nil default-prop)))
  14924. (defun org-set-property-and-value (use-last)
  14925. "Allow to set [PROPERTY]: [value] direction from prompt.
  14926. When use-default, don't even ask, just use the last
  14927. \"[PROPERTY]: [value]\" string from the history."
  14928. (interactive "P")
  14929. (let* ((completion-ignore-case t)
  14930. (pv (or (and use-last org-last-set-property-value)
  14931. (org-completing-read
  14932. "Enter a \"[Property]: [value]\" pair: "
  14933. nil nil nil nil nil
  14934. org-last-set-property-value)))
  14935. prop val)
  14936. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14937. (setq prop (match-string 1 pv)
  14938. val (match-string 2 pv))
  14939. (org-set-property prop val))))
  14940. (defun org-set-property (property value)
  14941. "In the current entry, set PROPERTY to VALUE.
  14942. When called interactively, this will prompt for a property name, offering
  14943. completion on existing and default properties. And then it will prompt
  14944. for a value, offering completion either on allowed values (via an inherited
  14945. xxx_ALL property) or on existing values in other instances of this property
  14946. in the current file."
  14947. (interactive (list nil nil))
  14948. (let* ((property (or property (org-read-property-name)))
  14949. (value (or value (org-read-property-value property)))
  14950. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14951. (setq org-last-set-property property)
  14952. (setq org-last-set-property-value (concat property ": " value))
  14953. ;; Possibly postprocess the inserted value:
  14954. (when fn (setq value (funcall fn value)))
  14955. (unless (equal (org-entry-get nil property) value)
  14956. (org-entry-put nil property value))))
  14957. (defun org-find-property (property &optional value)
  14958. "Find first entry in buffer that sets PROPERTY.
  14959. When optional argument VALUE is non-nil, only consider an entry
  14960. if it contains PROPERTY set to this value. If PROPERTY should be
  14961. explicitly set to nil, use string \"nil\" for VALUE.
  14962. Return position where the entry begins, or nil if there is no
  14963. such entry. If narrowing is in effect, only search the visible
  14964. part of the buffer."
  14965. (save-excursion
  14966. (goto-char (point-min))
  14967. (let ((case-fold-search t)
  14968. (re (org-re-property property nil (not value) value)))
  14969. (catch 'exit
  14970. (while (re-search-forward re nil t)
  14971. (when (if value (org-at-property-p)
  14972. (org-entry-get (point) property nil t))
  14973. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14974. (defun org-delete-property (property)
  14975. "In the current entry, delete PROPERTY."
  14976. (interactive
  14977. (let* ((completion-ignore-case t)
  14978. (cat (org-entry-get (point) "CATEGORY"))
  14979. (props0 (org-entry-properties nil 'standard))
  14980. (props (if cat props0
  14981. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14982. (prop (if (< 1 (length props))
  14983. (org-icompleting-read "Property: " props nil t)
  14984. (caar props))))
  14985. (list prop)))
  14986. (if (not property)
  14987. (message "No property to delete in this entry")
  14988. (org-entry-delete nil property)
  14989. (message "Property \"%s\" deleted" property)))
  14990. (defun org-delete-property-globally (property)
  14991. "Remove PROPERTY globally, from all entries.
  14992. This function ignores narrowing, if any."
  14993. (interactive
  14994. (let* ((completion-ignore-case t)
  14995. (prop (org-icompleting-read
  14996. "Globally remove property: "
  14997. (mapcar #'list (org-buffer-property-keys)))))
  14998. (list prop)))
  14999. (org-with-wide-buffer
  15000. (goto-char (point-min))
  15001. (let ((count 0)
  15002. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15003. (while (re-search-forward re nil t)
  15004. (when (org-entry-delete (point) property) (incf count)))
  15005. (message "Property \"%s\" removed from %d entries" property count))))
  15006. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15007. (defun org-compute-property-at-point ()
  15008. "Compute the property at point.
  15009. This looks for an enclosing column format, extracts the operator and
  15010. then applies it to the property in the column format's scope."
  15011. (interactive)
  15012. (unless (org-at-property-p)
  15013. (user-error "Not at a property"))
  15014. (let ((prop (org-match-string-no-properties 2)))
  15015. (org-columns-get-format-and-top-level)
  15016. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15017. (user-error "No operator defined for property %s" prop))
  15018. (org-columns-compute prop)))
  15019. (defvar org-property-allowed-value-functions nil
  15020. "Hook for functions supplying allowed values for a specific property.
  15021. The functions must take a single argument, the name of the property, and
  15022. return a flat list of allowed values. If \":ETC\" is one of
  15023. the values, this means that these values are intended as defaults for
  15024. completion, but that other values should be allowed too.
  15025. The functions must return nil if they are not responsible for this
  15026. property.")
  15027. (defun org-property-get-allowed-values (pom property &optional table)
  15028. "Get allowed values for the property PROPERTY.
  15029. When TABLE is non-nil, return an alist that can directly be used for
  15030. completion."
  15031. (let (vals)
  15032. (cond
  15033. ((equal property "TODO")
  15034. (setq vals (org-with-point-at pom
  15035. (append org-todo-keywords-1 '("")))))
  15036. ((equal property "PRIORITY")
  15037. (let ((n org-lowest-priority))
  15038. (while (>= n org-highest-priority)
  15039. (push (char-to-string n) vals)
  15040. (setq n (1- n)))))
  15041. ((equal property "CATEGORY"))
  15042. ((member property org-special-properties))
  15043. ((setq vals (run-hook-with-args-until-success
  15044. 'org-property-allowed-value-functions property)))
  15045. (t
  15046. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15047. (when (and vals (string-match "\\S-" vals))
  15048. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15049. (setq vals (mapcar (lambda (x)
  15050. (cond ((stringp x) x)
  15051. ((numberp x) (number-to-string x))
  15052. ((symbolp x) (symbol-name x))
  15053. (t "???")))
  15054. vals)))))
  15055. (when (member ":ETC" vals)
  15056. (setq vals (remove ":ETC" vals))
  15057. (org-add-props (car vals) '(org-unrestricted t)))
  15058. (if table (mapcar 'list vals) vals)))
  15059. (defun org-property-previous-allowed-value (&optional previous)
  15060. "Switch to the next allowed value for this property."
  15061. (interactive)
  15062. (org-property-next-allowed-value t))
  15063. (defun org-property-next-allowed-value (&optional previous)
  15064. "Switch to the next allowed value for this property."
  15065. (interactive)
  15066. (unless (org-at-property-p)
  15067. (user-error "Not at a property"))
  15068. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15069. (key (match-string 2))
  15070. (value (match-string 3))
  15071. (allowed (or (org-property-get-allowed-values (point) key)
  15072. (and (member value '("[ ]" "[-]" "[X]"))
  15073. '("[ ]" "[X]"))))
  15074. (heading (save-match-data (nth 4 (org-heading-components))))
  15075. nval)
  15076. (unless allowed
  15077. (user-error "Allowed values for this property have not been defined"))
  15078. (if previous (setq allowed (reverse allowed)))
  15079. (if (member value allowed)
  15080. (setq nval (car (cdr (member value allowed)))))
  15081. (setq nval (or nval (car allowed)))
  15082. (if (equal nval value)
  15083. (user-error "Only one allowed value for this property"))
  15084. (org-at-property-p)
  15085. (replace-match (concat " :" key ": " nval) t t)
  15086. (org-indent-line)
  15087. (beginning-of-line 1)
  15088. (skip-chars-forward " \t")
  15089. (when (equal prop org-effort-property)
  15090. (org-refresh-property
  15091. '((effort . identity)
  15092. (effort-minutes . org-duration-string-to-minutes))
  15093. nval)
  15094. (when (string= org-clock-current-task heading)
  15095. (setq org-clock-effort nval)
  15096. (org-clock-update-mode-line)))
  15097. (run-hook-with-args 'org-property-changed-functions key nval)))
  15098. (defun org-find-olp (path &optional this-buffer)
  15099. "Return a marker pointing to the entry at outline path OLP.
  15100. If anything goes wrong, throw an error.
  15101. You can wrap this call to catch the error like this:
  15102. (condition-case msg
  15103. (org-mobile-locate-entry (match-string 4))
  15104. (error (nth 1 msg)))
  15105. The return value will then be either a string with the error message,
  15106. or a marker if everything is OK.
  15107. If THIS-BUFFER is set, the outline path does not contain a file,
  15108. only headings."
  15109. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15110. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15111. (level 1)
  15112. (lmin 1)
  15113. (lmax 1)
  15114. limit re end found pos heading cnt flevel)
  15115. (unless buffer (error "File not found :%s" file))
  15116. (with-current-buffer buffer
  15117. (save-excursion
  15118. (save-restriction
  15119. (widen)
  15120. (setq limit (point-max))
  15121. (goto-char (point-min))
  15122. (dolist (heading path)
  15123. (setq re (format org-complex-heading-regexp-format
  15124. (regexp-quote heading)))
  15125. (setq cnt 0 pos (point))
  15126. (while (re-search-forward re end t)
  15127. (setq level (- (match-end 1) (match-beginning 1)))
  15128. (if (and (>= level lmin) (<= level lmax))
  15129. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15130. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15131. lmax heading))
  15132. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15133. lmax heading))
  15134. (goto-char found)
  15135. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15136. (setq end (save-excursion (org-end-of-subtree t t))))
  15137. (when (org-at-heading-p)
  15138. (point-marker)))))))
  15139. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15140. "Find node HEADING in BUFFER.
  15141. Return a marker to the heading if it was found, or nil if not.
  15142. If POS-ONLY is set, return just the position instead of a marker.
  15143. The heading text must match exact, but it may have a TODO keyword,
  15144. a priority cookie and tags in the standard locations."
  15145. (with-current-buffer (or buffer (current-buffer))
  15146. (save-excursion
  15147. (save-restriction
  15148. (widen)
  15149. (goto-char (point-min))
  15150. (let (case-fold-search)
  15151. (if (re-search-forward
  15152. (format org-complex-heading-regexp-format
  15153. (regexp-quote heading)) nil t)
  15154. (if pos-only
  15155. (match-beginning 0)
  15156. (move-marker (make-marker) (match-beginning 0)))))))))
  15157. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15158. "Find Org node headline HEADING in all .org files in directory DIR.
  15159. When the target headline is found, return a marker to this location."
  15160. (let ((files (directory-files (or dir default-directory)
  15161. t "\\`[^.#].*\\.org\\'"))
  15162. visiting m buffer)
  15163. (catch 'found
  15164. (dolist (file files)
  15165. (message "trying %s" file)
  15166. (setq visiting (org-find-base-buffer-visiting file))
  15167. (setq buffer (or visiting (find-file-noselect file)))
  15168. (setq m (org-find-exact-headline-in-buffer
  15169. heading buffer))
  15170. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15171. (and m (throw 'found m))))))
  15172. (defun org-find-entry-with-id (ident)
  15173. "Locate the entry that contains the ID property with exact value IDENT.
  15174. IDENT can be a string, a symbol or a number, this function will search for
  15175. the string representation of it.
  15176. Return the position where this entry starts, or nil if there is no such entry."
  15177. (interactive "sID: ")
  15178. (let ((id (cond
  15179. ((stringp ident) ident)
  15180. ((symbolp ident) (symbol-name ident))
  15181. ((numberp ident) (number-to-string ident))
  15182. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15183. (org-with-wide-buffer (org-find-property "ID" id))))
  15184. ;;;; Timestamps
  15185. (defvar org-last-changed-timestamp nil)
  15186. (defvar org-last-inserted-timestamp nil
  15187. "The last time stamp inserted with `org-insert-time-stamp'.")
  15188. (defvar org-ts-what) ; dynamically scoped parameter
  15189. (defun org-time-stamp (arg &optional inactive)
  15190. "Prompt for a date/time and insert a time stamp.
  15191. If the user specifies a time like HH:MM or if this command is
  15192. called with at least one prefix argument, the time stamp contains
  15193. the date and the time. Otherwise, only the date is included.
  15194. All parts of a date not specified by the user are filled in from
  15195. the timestamp at point, if any, or the current date/time
  15196. otherwise.
  15197. If there is already a timestamp at the cursor, it is replaced.
  15198. With two universal prefix arguments, insert an active timestamp
  15199. with the current time without prompting the user.
  15200. When called from lisp, the timestamp is inactive if INACTIVE is
  15201. non-nil."
  15202. (interactive "P")
  15203. (let* ((ts (cond
  15204. ((org-at-date-range-p t)
  15205. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15206. ((org-at-timestamp-p t) (match-string 0))))
  15207. ;; Default time is either the timestamp at point or today.
  15208. ;; When entering a range, only the range start is considered.
  15209. (default-time (if (not ts) (current-time)
  15210. (apply #'encode-time (org-parse-time-string ts))))
  15211. (default-input (and ts (org-get-compact-tod ts)))
  15212. (repeater (and ts
  15213. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15214. (match-string 0 ts)))
  15215. org-time-was-given
  15216. org-end-time-was-given
  15217. (time
  15218. (and (if (equal arg '(16)) (current-time)
  15219. ;; Preserve `this-command' and `last-command'.
  15220. (let ((this-command this-command)
  15221. (last-command last-command))
  15222. (org-read-date
  15223. arg 'totime nil nil default-time default-input
  15224. inactive))))))
  15225. (cond
  15226. ((and ts
  15227. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15228. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15229. (insert "--")
  15230. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15231. (ts
  15232. ;; Make sure we're on a timestamp. When in the middle of a date
  15233. ;; range, move arbitrarily to range end.
  15234. (unless (org-at-timestamp-p t)
  15235. (skip-chars-forward "-")
  15236. (org-at-timestamp-p t))
  15237. (replace-match "")
  15238. (setq org-last-changed-timestamp
  15239. (org-insert-time-stamp
  15240. time (or org-time-was-given arg)
  15241. inactive nil nil (list org-end-time-was-given)))
  15242. (when repeater
  15243. (backward-char)
  15244. (insert " " repeater)
  15245. (setq org-last-changed-timestamp
  15246. (concat (substring org-last-inserted-timestamp 0 -1)
  15247. " " repeater ">")))
  15248. (message "Timestamp updated"))
  15249. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15250. (t (org-insert-time-stamp
  15251. time (or org-time-was-given arg) inactive nil nil
  15252. (list org-end-time-was-given))))))
  15253. ;; FIXME: can we use this for something else, like computing time differences?
  15254. (defun org-get-compact-tod (s)
  15255. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15256. (let* ((t1 (match-string 1 s))
  15257. (h1 (string-to-number (match-string 2 s)))
  15258. (m1 (string-to-number (match-string 3 s)))
  15259. (t2 (and (match-end 4) (match-string 5 s)))
  15260. (h2 (and t2 (string-to-number (match-string 6 s))))
  15261. (m2 (and t2 (string-to-number (match-string 7 s))))
  15262. dh dm)
  15263. (if (not t2)
  15264. t1
  15265. (setq dh (- h2 h1) dm (- m2 m1))
  15266. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15267. (concat t1 "+" (number-to-string dh)
  15268. (and (/= 0 dm) (format ":%02d" dm)))))))
  15269. (defun org-time-stamp-inactive (&optional arg)
  15270. "Insert an inactive time stamp.
  15271. An inactive time stamp is enclosed in square brackets instead of angle
  15272. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15273. does not link to the calendar and cannot be changed with the S-cursor keys.
  15274. So these are more for recording a certain time/date."
  15275. (interactive "P")
  15276. (org-time-stamp arg 'inactive))
  15277. (defvar org-date-ovl (make-overlay 1 1))
  15278. (overlay-put org-date-ovl 'face 'org-date-selected)
  15279. (org-detach-overlay org-date-ovl)
  15280. (defvar org-ans1) ; dynamically scoped parameter
  15281. (defvar org-ans2) ; dynamically scoped parameter
  15282. (defvar org-plain-time-of-day-regexp) ; defined below
  15283. (defvar org-overriding-default-time nil) ; dynamically scoped
  15284. (defvar org-read-date-overlay nil)
  15285. (defvar org-dcst nil) ; dynamically scoped
  15286. (defvar org-read-date-history nil)
  15287. (defvar org-read-date-final-answer nil)
  15288. (defvar org-read-date-analyze-futurep nil)
  15289. (defvar org-read-date-analyze-forced-year nil)
  15290. (defvar org-read-date-inactive)
  15291. (defvar org-read-date-minibuffer-local-map
  15292. (let* ((map (make-sparse-keymap)))
  15293. (set-keymap-parent map minibuffer-local-map)
  15294. (org-defkey map (kbd ".")
  15295. (lambda () (interactive)
  15296. ;; Are we at the beginning of the prompt?
  15297. (if (looking-back "^[^:]+: ")
  15298. (org-eval-in-calendar '(calendar-goto-today))
  15299. (insert "."))))
  15300. (org-defkey map (kbd "C-.")
  15301. (lambda () (interactive)
  15302. (org-eval-in-calendar '(calendar-goto-today))))
  15303. (org-defkey map [(meta shift left)]
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar '(calendar-backward-month 1))))
  15306. (org-defkey map [(meta shift right)]
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(calendar-forward-month 1))))
  15309. (org-defkey map [(meta shift up)]
  15310. (lambda () (interactive)
  15311. (org-eval-in-calendar '(calendar-backward-year 1))))
  15312. (org-defkey map [(meta shift down)]
  15313. (lambda () (interactive)
  15314. (org-eval-in-calendar '(calendar-forward-year 1))))
  15315. (org-defkey map [?\e (shift left)]
  15316. (lambda () (interactive)
  15317. (org-eval-in-calendar '(calendar-backward-month 1))))
  15318. (org-defkey map [?\e (shift right)]
  15319. (lambda () (interactive)
  15320. (org-eval-in-calendar '(calendar-forward-month 1))))
  15321. (org-defkey map [?\e (shift up)]
  15322. (lambda () (interactive)
  15323. (org-eval-in-calendar '(calendar-backward-year 1))))
  15324. (org-defkey map [?\e (shift down)]
  15325. (lambda () (interactive)
  15326. (org-eval-in-calendar '(calendar-forward-year 1))))
  15327. (org-defkey map [(shift up)]
  15328. (lambda () (interactive)
  15329. (org-eval-in-calendar '(calendar-backward-week 1))))
  15330. (org-defkey map [(shift down)]
  15331. (lambda () (interactive)
  15332. (org-eval-in-calendar '(calendar-forward-week 1))))
  15333. (org-defkey map [(shift left)]
  15334. (lambda () (interactive)
  15335. (org-eval-in-calendar '(calendar-backward-day 1))))
  15336. (org-defkey map [(shift right)]
  15337. (lambda () (interactive)
  15338. (org-eval-in-calendar '(calendar-forward-day 1))))
  15339. (org-defkey map "!"
  15340. (lambda () (interactive)
  15341. (org-eval-in-calendar '(diary-view-entries))
  15342. (message "")))
  15343. (org-defkey map ">"
  15344. (lambda () (interactive)
  15345. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15346. (org-defkey map "<"
  15347. (lambda () (interactive)
  15348. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15349. (org-defkey map "\C-v"
  15350. (lambda () (interactive)
  15351. (org-eval-in-calendar
  15352. '(calendar-scroll-left-three-months 1))))
  15353. (org-defkey map "\M-v"
  15354. (lambda () (interactive)
  15355. (org-eval-in-calendar
  15356. '(calendar-scroll-right-three-months 1))))
  15357. map)
  15358. "Keymap for minibuffer commands when using `org-read-date'.")
  15359. (defvar org-def)
  15360. (defvar org-defdecode)
  15361. (defvar org-with-time)
  15362. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15363. default-time default-input inactive)
  15364. "Read a date, possibly a time, and make things smooth for the user.
  15365. The prompt will suggest to enter an ISO date, but you can also enter anything
  15366. which will at least partially be understood by `parse-time-string'.
  15367. Unrecognized parts of the date will default to the current day, month, year,
  15368. hour and minute. If this command is called to replace a timestamp at point,
  15369. or to enter the second timestamp of a range, the default time is taken
  15370. from the existing stamp. Furthermore, the command prefers the future,
  15371. so if you are giving a date where the year is not given, and the day-month
  15372. combination is already past in the current year, it will assume you
  15373. mean next year. For details, see the manual. A few examples:
  15374. 3-2-5 --> 2003-02-05
  15375. feb 15 --> currentyear-02-15
  15376. 2/15 --> currentyear-02-15
  15377. sep 12 9 --> 2009-09-12
  15378. 12:45 --> today 12:45
  15379. 22 sept 0:34 --> currentyear-09-22 0:34
  15380. 12 --> currentyear-currentmonth-12
  15381. Fri --> nearest Friday after today
  15382. -Tue --> last Tuesday
  15383. etc.
  15384. Furthermore you can specify a relative date by giving, as the *first* thing
  15385. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15386. change in days weeks, months, years.
  15387. With a single plus or minus, the date is relative to today. With a double
  15388. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15389. +4d --> four days from today
  15390. +4 --> same as above
  15391. +2w --> two weeks from today
  15392. ++5 --> five days from default date
  15393. The function understands only English month and weekday abbreviations.
  15394. While prompting, a calendar is popped up - you can also select the
  15395. date with the mouse (button 1). The calendar shows a period of three
  15396. months. To scroll it to other months, use the keys `>' and `<'.
  15397. If you don't like the calendar, turn it off with
  15398. \(setq org-read-date-popup-calendar nil)
  15399. With optional argument TO-TIME, the date will immediately be converted
  15400. to an internal time.
  15401. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15402. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15403. still enter a time, and this function will inform the calling routine
  15404. about this change. The calling routine may then choose to change the
  15405. format used to insert the time stamp into the buffer to include the time.
  15406. With optional argument FROM-STRING, read from this string instead from
  15407. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15408. the time/date that is used for everything that is not specified by the
  15409. user."
  15410. (require 'parse-time)
  15411. (let* ((org-time-stamp-rounding-minutes
  15412. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15413. (org-dcst org-display-custom-times)
  15414. (ct (org-current-time))
  15415. (org-def (or org-overriding-default-time default-time ct))
  15416. (org-defdecode (decode-time org-def))
  15417. (dummy (progn
  15418. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15419. (setcar (nthcdr 2 org-defdecode) -1)
  15420. (setcar (nthcdr 1 org-defdecode) 59)
  15421. (setq org-def (apply 'encode-time org-defdecode)
  15422. org-defdecode (decode-time org-def)))))
  15423. (cur-frame (selected-frame))
  15424. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15425. (calendar-frame-setup nil)
  15426. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15427. (calendar-move-hook nil)
  15428. (calendar-view-diary-initially-flag nil)
  15429. (calendar-view-holidays-initially-flag nil)
  15430. (timestr (format-time-string
  15431. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15432. (prompt (concat (if prompt (concat prompt " ") "")
  15433. (format "Date+time [%s]: " timestr)))
  15434. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15435. (cond
  15436. (from-string (setq ans from-string))
  15437. (org-read-date-popup-calendar
  15438. (save-excursion
  15439. (save-window-excursion
  15440. (calendar)
  15441. (when (eq calendar-setup 'calendar-only)
  15442. (setq cal-frame
  15443. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15444. (select-frame cal-frame))
  15445. (org-eval-in-calendar '(setq cursor-type nil) t)
  15446. (unwind-protect
  15447. (progn
  15448. (calendar-forward-day (- (time-to-days org-def)
  15449. (calendar-absolute-from-gregorian
  15450. (calendar-current-date))))
  15451. (org-eval-in-calendar nil t)
  15452. (let* ((old-map (current-local-map))
  15453. (map (copy-keymap calendar-mode-map))
  15454. (minibuffer-local-map
  15455. (copy-keymap org-read-date-minibuffer-local-map)))
  15456. (org-defkey map (kbd "RET") 'org-calendar-select)
  15457. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15458. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15459. (unwind-protect
  15460. (progn
  15461. (use-local-map map)
  15462. (setq org-read-date-inactive inactive)
  15463. (add-hook 'post-command-hook 'org-read-date-display)
  15464. (setq org-ans0 (read-string prompt default-input
  15465. 'org-read-date-history nil))
  15466. ;; org-ans0: from prompt
  15467. ;; org-ans1: from mouse click
  15468. ;; org-ans2: from calendar motion
  15469. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15470. (remove-hook 'post-command-hook 'org-read-date-display)
  15471. (use-local-map old-map)
  15472. (when org-read-date-overlay
  15473. (delete-overlay org-read-date-overlay)
  15474. (setq org-read-date-overlay nil)))))
  15475. (bury-buffer "*Calendar*")
  15476. (when cal-frame
  15477. (delete-frame cal-frame)
  15478. (select-frame-set-input-focus cur-frame))))))
  15479. (t ; Naked prompt only
  15480. (unwind-protect
  15481. (setq ans (read-string prompt default-input
  15482. 'org-read-date-history timestr))
  15483. (when org-read-date-overlay
  15484. (delete-overlay org-read-date-overlay)
  15485. (setq org-read-date-overlay nil)))))
  15486. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15487. (when org-read-date-analyze-forced-year
  15488. (message "Year was forced into %s"
  15489. (if org-read-date-force-compatible-dates
  15490. "compatible range (1970-2037)"
  15491. "range representable on this machine"))
  15492. (ding))
  15493. ;; One round trip to get rid of 34th of August and stuff like that....
  15494. (setq final (decode-time (apply 'encode-time final)))
  15495. (setq org-read-date-final-answer ans)
  15496. (if to-time
  15497. (apply 'encode-time final)
  15498. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15499. (format "%04d-%02d-%02d %02d:%02d"
  15500. (nth 5 final) (nth 4 final) (nth 3 final)
  15501. (nth 2 final) (nth 1 final))
  15502. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15503. (defun org-read-date-display ()
  15504. "Display the current date prompt interpretation in the minibuffer."
  15505. (when org-read-date-display-live
  15506. (when org-read-date-overlay
  15507. (delete-overlay org-read-date-overlay))
  15508. (when (minibufferp (current-buffer))
  15509. (save-excursion
  15510. (end-of-line 1)
  15511. (while (not (equal (buffer-substring
  15512. (max (point-min) (- (point) 4)) (point))
  15513. " "))
  15514. (insert " ")))
  15515. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15516. " " (or org-ans1 org-ans2)))
  15517. (org-end-time-was-given nil)
  15518. (f (org-read-date-analyze ans org-def org-defdecode))
  15519. (fmts (if org-dcst
  15520. org-time-stamp-custom-formats
  15521. org-time-stamp-formats))
  15522. (fmt (if (or org-with-time
  15523. (and (boundp 'org-time-was-given) org-time-was-given))
  15524. (cdr fmts)
  15525. (car fmts)))
  15526. (txt (format-time-string fmt (apply 'encode-time f)))
  15527. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15528. (txt (concat "=> " txt)))
  15529. (when (and org-end-time-was-given
  15530. (string-match org-plain-time-of-day-regexp txt))
  15531. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15532. org-end-time-was-given
  15533. (substring txt (match-end 0)))))
  15534. (when org-read-date-analyze-futurep
  15535. (setq txt (concat txt " (=>F)")))
  15536. (setq org-read-date-overlay
  15537. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15538. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15539. (defun org-read-date-analyze (ans org-def org-defdecode)
  15540. "Analyze the combined answer of the date prompt."
  15541. ;; FIXME: cleanup and comment
  15542. ;; Pass `current-time' result to `decode-time' (instead of calling
  15543. ;; without arguments) so that only `current-time' has to be
  15544. ;; overriden in tests.
  15545. (let ((nowdecode (decode-time (current-time)))
  15546. delta deltan deltaw deltadef year month day
  15547. hour minute second wday pm h2 m2 tl wday1
  15548. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15549. (setq org-read-date-analyze-futurep nil
  15550. org-read-date-analyze-forced-year nil)
  15551. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15552. (setq ans "+0"))
  15553. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15554. (setq ans (replace-match "" t t ans)
  15555. deltan (car delta)
  15556. deltaw (nth 1 delta)
  15557. deltadef (nth 2 delta)))
  15558. ;; Check if there is an iso week date in there. If yes, store the
  15559. ;; info and postpone interpreting it until the rest of the parsing
  15560. ;; is done.
  15561. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15562. (setq iso-year (if (match-end 1)
  15563. (org-small-year-to-year
  15564. (string-to-number (match-string 1 ans))))
  15565. iso-weekday (if (match-end 3)
  15566. (string-to-number (match-string 3 ans)))
  15567. iso-week (string-to-number (match-string 2 ans)))
  15568. (setq ans (replace-match "" t t ans)))
  15569. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15570. (when (string-match
  15571. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15572. (setq year (if (match-end 2)
  15573. (string-to-number (match-string 2 ans))
  15574. (progn (setq kill-year t)
  15575. (string-to-number (format-time-string "%Y"))))
  15576. month (string-to-number (match-string 3 ans))
  15577. day (string-to-number (match-string 4 ans)))
  15578. (if (< year 100) (setq year (+ 2000 year)))
  15579. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15580. t nil ans)))
  15581. ;; Help matching dotted european dates
  15582. (when (string-match
  15583. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15584. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15585. (setq kill-year t)
  15586. (string-to-number (format-time-string "%Y")))
  15587. day (string-to-number (match-string 1 ans))
  15588. month (string-to-number (match-string 2 ans))
  15589. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15590. t nil ans)))
  15591. ;; Help matching american dates, like 5/30 or 5/30/7
  15592. (when (string-match
  15593. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15594. (setq year (if (match-end 4)
  15595. (string-to-number (match-string 4 ans))
  15596. (progn (setq kill-year t)
  15597. (string-to-number (format-time-string "%Y"))))
  15598. month (string-to-number (match-string 1 ans))
  15599. day (string-to-number (match-string 2 ans)))
  15600. (if (< year 100) (setq year (+ 2000 year)))
  15601. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15602. t nil ans)))
  15603. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15604. ;; If there is a time with am/pm, and *no* time without it, we convert
  15605. ;; so that matching will be successful.
  15606. (loop for i from 1 to 2 do ; twice, for end time as well
  15607. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15608. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15609. (setq hour (string-to-number (match-string 1 ans))
  15610. minute (if (match-end 3)
  15611. (string-to-number (match-string 3 ans))
  15612. 0)
  15613. pm (equal ?p
  15614. (string-to-char (downcase (match-string 4 ans)))))
  15615. (if (and (= hour 12) (not pm))
  15616. (setq hour 0)
  15617. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15618. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15619. t t ans))))
  15620. ;; Check if a time range is given as a duration
  15621. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15622. (setq hour (string-to-number (match-string 1 ans))
  15623. h2 (+ hour (string-to-number (match-string 3 ans)))
  15624. minute (string-to-number (match-string 2 ans))
  15625. m2 (+ minute (if (match-end 5) (string-to-number
  15626. (match-string 5 ans))0)))
  15627. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15628. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15629. t t ans)))
  15630. ;; Check if there is a time range
  15631. (when (boundp 'org-end-time-was-given)
  15632. (setq org-time-was-given nil)
  15633. (when (and (string-match org-plain-time-of-day-regexp ans)
  15634. (match-end 8))
  15635. (setq org-end-time-was-given (match-string 8 ans))
  15636. (setq ans (concat (substring ans 0 (match-beginning 7))
  15637. (substring ans (match-end 7))))))
  15638. (setq tl (parse-time-string ans)
  15639. day (or (nth 3 tl) (nth 3 org-defdecode))
  15640. month
  15641. (cond ((nth 4 tl))
  15642. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15643. ;; Day was specified. Make sure DAY+MONTH
  15644. ;; combination happens in the future.
  15645. ((nth 3 tl)
  15646. (setq futurep t)
  15647. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15648. (nth 4 nowdecode)))
  15649. (t (nth 4 org-defdecode)))
  15650. year
  15651. (cond ((and (not kill-year) (nth 5 tl)))
  15652. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15653. ;; Month was guessed in the future and is at least
  15654. ;; equal to NOWDECODE's. Fix year accordingly.
  15655. (futurep
  15656. (if (or (> month (nth 4 nowdecode))
  15657. (>= day (nth 3 nowdecode)))
  15658. (nth 5 nowdecode)
  15659. (1+ (nth 5 nowdecode))))
  15660. ;; Month was specified. Make sure MONTH+YEAR
  15661. ;; combination happens in the future.
  15662. ((nth 4 tl)
  15663. (setq futurep t)
  15664. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15665. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15666. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15667. (t (nth 5 nowdecode))))
  15668. (t (nth 5 org-defdecode)))
  15669. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15670. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15671. second (or (nth 0 tl) 0)
  15672. wday (nth 6 tl))
  15673. (when (and (eq org-read-date-prefer-future 'time)
  15674. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15675. (equal day (nth 3 nowdecode))
  15676. (equal month (nth 4 nowdecode))
  15677. (equal year (nth 5 nowdecode))
  15678. (nth 2 tl)
  15679. (or (< (nth 2 tl) (nth 2 nowdecode))
  15680. (and (= (nth 2 tl) (nth 2 nowdecode))
  15681. (nth 1 tl)
  15682. (< (nth 1 tl) (nth 1 nowdecode)))))
  15683. (setq day (1+ day)
  15684. futurep t))
  15685. ;; Special date definitions below
  15686. (cond
  15687. (iso-week
  15688. ;; There was an iso week
  15689. (require 'cal-iso)
  15690. (setq futurep nil)
  15691. (setq year (or iso-year year)
  15692. day (or iso-weekday wday 1)
  15693. wday nil ; to make sure that the trigger below does not match
  15694. iso-date (calendar-gregorian-from-absolute
  15695. (calendar-iso-to-absolute
  15696. (list iso-week day year))))
  15697. ; FIXME: Should we also push ISO weeks into the future?
  15698. ; (when (and org-read-date-prefer-future
  15699. ; (not iso-year)
  15700. ; (< (calendar-absolute-from-gregorian iso-date)
  15701. ; (time-to-days (current-time))))
  15702. ; (setq year (1+ year)
  15703. ; iso-date (calendar-gregorian-from-absolute
  15704. ; (calendar-iso-to-absolute
  15705. ; (list iso-week day year)))))
  15706. (setq month (car iso-date)
  15707. year (nth 2 iso-date)
  15708. day (nth 1 iso-date)))
  15709. (deltan
  15710. (setq futurep nil)
  15711. (unless deltadef
  15712. ;; Pass `current-time' result to `decode-time' (instead of
  15713. ;; calling without arguments) so that only `current-time' has
  15714. ;; to be overriden in tests.
  15715. (let ((now (decode-time (current-time))))
  15716. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15717. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15718. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15719. ((equal deltaw "m") (setq month (+ month deltan)))
  15720. ((equal deltaw "y") (setq year (+ year deltan)))))
  15721. ((and wday (not (nth 3 tl)))
  15722. ;; Weekday was given, but no day, so pick that day in the week
  15723. ;; on or after the derived date.
  15724. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15725. (unless (equal wday wday1)
  15726. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15727. (if (and (boundp 'org-time-was-given)
  15728. (nth 2 tl))
  15729. (setq org-time-was-given t))
  15730. (if (< year 100) (setq year (+ 2000 year)))
  15731. ;; Check of the date is representable
  15732. (if org-read-date-force-compatible-dates
  15733. (progn
  15734. (if (< year 1970)
  15735. (setq year 1970 org-read-date-analyze-forced-year t))
  15736. (if (> year 2037)
  15737. (setq year 2037 org-read-date-analyze-forced-year t)))
  15738. (condition-case nil
  15739. (ignore (encode-time second minute hour day month year))
  15740. (error
  15741. (setq year (nth 5 org-defdecode))
  15742. (setq org-read-date-analyze-forced-year t))))
  15743. (setq org-read-date-analyze-futurep futurep)
  15744. (list second minute hour day month year)))
  15745. (defvar parse-time-weekdays)
  15746. (defun org-read-date-get-relative (s today default)
  15747. "Check string S for special relative date string.
  15748. TODAY and DEFAULT are internal times, for today and for a default.
  15749. Return shift list (N what def-flag)
  15750. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15751. N is the number of WHATs to shift.
  15752. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15753. the DEFAULT date rather than TODAY."
  15754. (require 'parse-time)
  15755. (when (and
  15756. (string-match
  15757. (concat
  15758. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15759. "\\([0-9]+\\)?"
  15760. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15761. "\\([ \t]\\|$\\)") s)
  15762. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15763. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15764. (string-to-char (substring (match-string 1 s) -1))
  15765. ?+))
  15766. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15767. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15768. (what (if (match-end 3) (match-string 3 s) "d"))
  15769. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15770. (date (if rel default today))
  15771. (wday (nth 6 (decode-time date)))
  15772. delta)
  15773. (if wday1
  15774. (progn
  15775. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15776. (if (= delta 0) (setq delta 7))
  15777. (if (= dir ?-)
  15778. (progn
  15779. (setq delta (- delta 7))
  15780. (if (= delta 0) (setq delta -7))))
  15781. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15782. (list delta "d" rel))
  15783. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15784. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15785. "Turn a user-specified date into the internal representation.
  15786. The internal representation needed by the calendar is (month day year).
  15787. This is a wrapper to handle the brain-dead convention in calendar that
  15788. user function argument order change dependent on argument order."
  15789. (if (boundp 'calendar-date-style)
  15790. (cond
  15791. ((eq calendar-date-style 'american)
  15792. (list arg1 arg2 arg3))
  15793. ((eq calendar-date-style 'european)
  15794. (list arg2 arg1 arg3))
  15795. ((eq calendar-date-style 'iso)
  15796. (list arg2 arg3 arg1)))
  15797. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15798. (if (org-bound-and-true-p european-calendar-style)
  15799. (list arg2 arg1 arg3)
  15800. (list arg1 arg2 arg3)))))
  15801. (defun org-eval-in-calendar (form &optional keepdate)
  15802. "Eval FORM in the calendar window and return to current window.
  15803. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15804. otherwise stick to the current value of `org-ans2'."
  15805. (let ((sf (selected-frame))
  15806. (sw (selected-window)))
  15807. (select-window (get-buffer-window "*Calendar*" t))
  15808. (eval form)
  15809. (when (and (not keepdate) (calendar-cursor-to-date))
  15810. (let* ((date (calendar-cursor-to-date))
  15811. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15812. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15813. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15814. (select-window sw)
  15815. (org-select-frame-set-input-focus sf)))
  15816. (defun org-calendar-select ()
  15817. "Return to `org-read-date' with the date currently selected.
  15818. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15819. (interactive)
  15820. (when (calendar-cursor-to-date)
  15821. (let* ((date (calendar-cursor-to-date))
  15822. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15823. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15824. (if (active-minibuffer-window) (exit-minibuffer))))
  15825. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15826. "Insert a date stamp for the date given by the internal TIME.
  15827. See `format-time-string' for the format of TIME.
  15828. WITH-HM means use the stamp format that includes the time of the day.
  15829. INACTIVE means use square brackets instead of angular ones, so that the
  15830. stamp will not contribute to the agenda.
  15831. PRE and POST are optional strings to be inserted before and after the
  15832. stamp.
  15833. The command returns the inserted time stamp."
  15834. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15835. stamp)
  15836. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15837. (insert-before-markers (or pre ""))
  15838. (when (listp extra)
  15839. (setq extra (car extra))
  15840. (if (and (stringp extra)
  15841. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15842. (setq extra (format "-%02d:%02d"
  15843. (string-to-number (match-string 1 extra))
  15844. (string-to-number (match-string 2 extra))))
  15845. (setq extra nil)))
  15846. (when extra
  15847. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15848. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15849. (insert-before-markers (or post ""))
  15850. (setq org-last-inserted-timestamp stamp)))
  15851. (defun org-toggle-time-stamp-overlays ()
  15852. "Toggle the use of custom time stamp formats."
  15853. (interactive)
  15854. (setq org-display-custom-times (not org-display-custom-times))
  15855. (unless org-display-custom-times
  15856. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15857. (while (setq p (next-single-property-change p 'display))
  15858. (if (and (get-text-property p 'display)
  15859. (eq (get-text-property p 'face) 'org-date))
  15860. (remove-text-properties
  15861. p (setq p (next-single-property-change p 'display))
  15862. '(display t))))
  15863. (set-buffer-modified-p bmp)))
  15864. (if (featurep 'xemacs)
  15865. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15866. (org-restart-font-lock)
  15867. (setq org-table-may-need-update t)
  15868. (if org-display-custom-times
  15869. (message "Time stamps are overlaid with custom format")
  15870. (message "Time stamp overlays removed")))
  15871. (defun org-display-custom-time (beg end)
  15872. "Overlay modified time stamp format over timestamp between BEG and END."
  15873. (let* ((ts (buffer-substring beg end))
  15874. t1 w1 with-hm tf time str w2 (off 0))
  15875. (save-match-data
  15876. (setq t1 (org-parse-time-string ts t))
  15877. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15878. (setq off (- (match-end 0) (match-beginning 0)))))
  15879. (setq end (- end off))
  15880. (setq w1 (- end beg)
  15881. with-hm (and (nth 1 t1) (nth 2 t1))
  15882. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15883. time (org-fix-decoded-time t1)
  15884. str (org-add-props
  15885. (format-time-string
  15886. (substring tf 1 -1) (apply 'encode-time time))
  15887. nil 'mouse-face 'highlight)
  15888. w2 (length str))
  15889. (if (not (= w2 w1))
  15890. (add-text-properties (1+ beg) (+ 2 beg)
  15891. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15892. (if (featurep 'xemacs)
  15893. (progn
  15894. (put-text-property beg end 'invisible t)
  15895. (put-text-property beg end 'end-glyph (make-glyph str)))
  15896. (put-text-property beg end 'display str))))
  15897. (defun org-fix-decoded-time (time)
  15898. "Set 0 instead of nil for the first 6 elements of time.
  15899. Don't touch the rest."
  15900. (let ((n 0))
  15901. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15902. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15903. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15904. "Difference between TIMESTAMP-STRING and now in days.
  15905. If SECONDS is non-nil, return the difference in seconds."
  15906. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15907. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15908. (funcall fdiff (current-time)))))
  15909. (defun org-deadline-close (timestamp-string &optional ndays)
  15910. "Is the time in TIMESTAMP-STRING close to the current date?"
  15911. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15912. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15913. (not (org-entry-is-done-p))))
  15914. (defun org-get-wdays (ts &optional delay zero-delay)
  15915. "Get the deadline lead time appropriate for timestring TS.
  15916. When DELAY is non-nil, get the delay time for scheduled items
  15917. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15918. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15919. don't try to find the delay cookie in the scheduled timestamp."
  15920. (let ((tv (if delay org-scheduled-delay-days
  15921. org-deadline-warning-days)))
  15922. (cond
  15923. ((or (and delay (< tv 0))
  15924. (and delay zero-delay (<= tv 0))
  15925. (and (not delay) (<= tv 0)))
  15926. ;; Enforce this value no matter what
  15927. (- tv))
  15928. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15929. ;; lead time is specified.
  15930. (floor (* (string-to-number (match-string 1 ts))
  15931. (cdr (assoc (match-string 2 ts)
  15932. '(("d" . 1) ("w" . 7)
  15933. ("m" . 30.4) ("y" . 365.25)
  15934. ("h" . 0.041667)))))))
  15935. ;; go for the default.
  15936. (t tv))))
  15937. (defun org-calendar-select-mouse (ev)
  15938. "Return to `org-read-date' with the date currently selected.
  15939. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15940. (interactive "e")
  15941. (mouse-set-point ev)
  15942. (when (calendar-cursor-to-date)
  15943. (let* ((date (calendar-cursor-to-date))
  15944. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15945. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15946. (if (active-minibuffer-window) (exit-minibuffer))))
  15947. (defun org-check-deadlines (ndays)
  15948. "Check if there are any deadlines due or past due.
  15949. A deadline is considered due if it happens within `org-deadline-warning-days'
  15950. days from today's date. If the deadline appears in an entry marked DONE,
  15951. it is not shown. The prefix arg NDAYS can be used to test that many
  15952. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15953. (interactive "P")
  15954. (let* ((org-warn-days
  15955. (cond
  15956. ((equal ndays '(4)) 100000)
  15957. (ndays (prefix-numeric-value ndays))
  15958. (t (abs org-deadline-warning-days))))
  15959. (case-fold-search nil)
  15960. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15961. (callback
  15962. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15963. (message "%d deadlines past-due or due within %d days"
  15964. (org-occur regexp nil callback)
  15965. org-warn-days)))
  15966. (defsubst org-re-timestamp (type)
  15967. "Return a regexp for timestamp TYPE.
  15968. Allowed values for TYPE are:
  15969. all: all timestamps
  15970. active: only active timestamps (<...>)
  15971. inactive: only inactive timestamps ([...])
  15972. scheduled: only scheduled timestamps
  15973. deadline: only deadline timestamps
  15974. closed: only closed time-stamps
  15975. When TYPE is nil, fall back on returning a regexp that matches
  15976. both scheduled and deadline timestamps."
  15977. (case type
  15978. (all org-ts-regexp-both)
  15979. (active org-ts-regexp)
  15980. (inactive org-ts-regexp-inactive)
  15981. (scheduled org-scheduled-time-regexp)
  15982. (deadline org-deadline-time-regexp)
  15983. (closed org-closed-time-regexp)
  15984. (otherwise
  15985. (concat "\\<"
  15986. (regexp-opt (list org-deadline-string org-scheduled-string))
  15987. " *<\\([^>]+\\)>"))))
  15988. (defun org-check-before-date (date)
  15989. "Check if there are deadlines or scheduled entries before DATE."
  15990. (interactive (list (org-read-date)))
  15991. (let ((case-fold-search nil)
  15992. (regexp (org-re-timestamp org-ts-type))
  15993. (callback
  15994. `(lambda ()
  15995. (and ,(if (memq org-ts-type '(active inactive all))
  15996. '(eq (org-element-type (org-element-context)) 'timestamp)
  15997. '(org-at-planning-p))
  15998. (time-less-p
  15999. (org-time-string-to-time (match-string 1))
  16000. (org-time-string-to-time date))))))
  16001. (message "%d entries before %s"
  16002. (org-occur regexp nil callback) date)))
  16003. (defun org-check-after-date (date)
  16004. "Check if there are deadlines or scheduled entries after DATE."
  16005. (interactive (list (org-read-date)))
  16006. (let ((case-fold-search nil)
  16007. (regexp (org-re-timestamp org-ts-type))
  16008. (callback
  16009. `(lambda ()
  16010. (and ,(if (memq org-ts-type '(active inactive all))
  16011. '(eq (org-element-type (org-element-context)) 'timestamp)
  16012. '(org-at-planning-p))
  16013. (not (time-less-p
  16014. (org-time-string-to-time (match-string 1))
  16015. (org-time-string-to-time date)))))))
  16016. (message "%d entries after %s"
  16017. (org-occur regexp nil callback) date)))
  16018. (defun org-check-dates-range (start-date end-date)
  16019. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16020. (interactive (list (org-read-date nil nil nil "Range starts")
  16021. (org-read-date nil nil nil "Range end")))
  16022. (let ((case-fold-search nil)
  16023. (regexp (org-re-timestamp org-ts-type))
  16024. (callback
  16025. `(lambda ()
  16026. (let ((match (match-string 1)))
  16027. (and
  16028. ,(if (memq org-ts-type '(active inactive all))
  16029. '(eq (org-element-type (org-element-context)) 'timestamp)
  16030. '(org-at-planning-p))
  16031. (not (time-less-p
  16032. (org-time-string-to-time match)
  16033. (org-time-string-to-time start-date)))
  16034. (time-less-p
  16035. (org-time-string-to-time match)
  16036. (org-time-string-to-time end-date)))))))
  16037. (message "%d entries between %s and %s"
  16038. (org-occur regexp nil callback) start-date end-date)))
  16039. (defun org-evaluate-time-range (&optional to-buffer)
  16040. "Evaluate a time range by computing the difference between start and end.
  16041. Normally the result is just printed in the echo area, but with prefix arg
  16042. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16043. If the time range is actually in a table, the result is inserted into the
  16044. next column.
  16045. For time difference computation, a year is assumed to be exactly 365
  16046. days in order to avoid rounding problems."
  16047. (interactive "P")
  16048. (or
  16049. (org-clock-update-time-maybe)
  16050. (save-excursion
  16051. (unless (org-at-date-range-p t)
  16052. (goto-char (point-at-bol))
  16053. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16054. (if (not (org-at-date-range-p t))
  16055. (user-error "Not at a time-stamp range, and none found in current line")))
  16056. (let* ((ts1 (match-string 1))
  16057. (ts2 (match-string 2))
  16058. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16059. (match-end (match-end 0))
  16060. (time1 (org-time-string-to-time ts1))
  16061. (time2 (org-time-string-to-time ts2))
  16062. (t1 (org-float-time time1))
  16063. (t2 (org-float-time time2))
  16064. (diff (abs (- t2 t1)))
  16065. (negative (< (- t2 t1) 0))
  16066. ;; (ys (floor (* 365 24 60 60)))
  16067. (ds (* 24 60 60))
  16068. (hs (* 60 60))
  16069. (fy "%dy %dd %02d:%02d")
  16070. (fy1 "%dy %dd")
  16071. (fd "%dd %02d:%02d")
  16072. (fd1 "%dd")
  16073. (fh "%02d:%02d")
  16074. y d h m align)
  16075. (if havetime
  16076. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16077. y 0
  16078. d (floor (/ diff ds)) diff (mod diff ds)
  16079. h (floor (/ diff hs)) diff (mod diff hs)
  16080. m (floor (/ diff 60)))
  16081. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16082. y 0
  16083. d (floor (+ (/ diff ds) 0.5))
  16084. h 0 m 0))
  16085. (if (not to-buffer)
  16086. (message "%s" (org-make-tdiff-string y d h m))
  16087. (if (org-at-table-p)
  16088. (progn
  16089. (goto-char match-end)
  16090. (setq align t)
  16091. (and (looking-at " *|") (goto-char (match-end 0))))
  16092. (goto-char match-end))
  16093. (if (looking-at
  16094. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16095. (replace-match ""))
  16096. (if negative (insert " -"))
  16097. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16098. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16099. (insert " " (format fh h m))))
  16100. (if align (org-table-align))
  16101. (message "Time difference inserted")))))
  16102. (defun org-make-tdiff-string (y d h m)
  16103. (let ((fmt "")
  16104. (l nil))
  16105. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16106. l (push y l)))
  16107. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16108. l (push d l)))
  16109. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16110. l (push h l)))
  16111. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16112. l (push m l)))
  16113. (apply 'format fmt (nreverse l))))
  16114. (defun org-time-string-to-time (s &optional buffer pos)
  16115. "Convert a timestamp string into internal time."
  16116. (condition-case errdata
  16117. (apply 'encode-time (org-parse-time-string s))
  16118. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16119. s (if (not (and buffer pos))
  16120. ""
  16121. (format " at %d in buffer `%s'" pos buffer))
  16122. (cdr errdata)))))
  16123. (defun org-time-string-to-seconds (s)
  16124. "Convert a timestamp string to a number of seconds."
  16125. (org-float-time (org-time-string-to-time s)))
  16126. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16127. "Convert a time stamp to an absolute day number.
  16128. If there is a specifier for a cyclic time stamp, get the closest
  16129. date to DAYNR.
  16130. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16131. The variable `date' is bound by the calendar when this is called."
  16132. (cond
  16133. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16134. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16135. daynr
  16136. (+ daynr 1000)))
  16137. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16138. (> (string-to-number (match-string 1 s)) 0))
  16139. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16140. (time-to-days (current-time))) (match-string 0 s)
  16141. prefer show-all))
  16142. (t (time-to-days
  16143. (condition-case errdata
  16144. (apply 'encode-time (org-parse-time-string s))
  16145. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16146. s (if (not (and buffer pos))
  16147. ""
  16148. (format " at %d in buffer `%s'" pos buffer))
  16149. (cdr errdata))))))))
  16150. (defun org-days-to-iso-week (days)
  16151. "Return the iso week number."
  16152. (require 'cal-iso)
  16153. (car (calendar-iso-from-absolute days)))
  16154. (defun org-small-year-to-year (year)
  16155. "Convert 2-digit years into 4-digit years.
  16156. YEAR is expanded into one of the 30 next years, if possible, or
  16157. into a past one. Any year larger than 99 is returned unchanged."
  16158. (if (>= year 100) year
  16159. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16160. (century (/ current 100))
  16161. (offset (- year (% current 100))))
  16162. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16163. ((> offset -70) (+ (* century 100) year))
  16164. (t (+ (* (1+ century) 100) year))))))
  16165. (defun org-time-from-absolute (d)
  16166. "Return the time corresponding to date D.
  16167. D may be an absolute day number, or a calendar-type list (month day year)."
  16168. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16169. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16170. (defun org-calendar-holiday ()
  16171. "List of holidays, for Diary display in Org-mode."
  16172. (require 'holidays)
  16173. (let ((hl (funcall
  16174. (if (fboundp 'calendar-check-holidays)
  16175. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16176. (if hl (mapconcat 'identity hl "; "))))
  16177. (defun org-diary-sexp-entry (sexp entry date)
  16178. "Process a SEXP diary ENTRY for DATE."
  16179. (require 'diary-lib)
  16180. (let ((result (if calendar-debug-sexp
  16181. (let ((stack-trace-on-error t))
  16182. (eval (car (read-from-string sexp))))
  16183. (condition-case nil
  16184. (eval (car (read-from-string sexp)))
  16185. (error
  16186. (beep)
  16187. (message "Bad sexp at line %d in %s: %s"
  16188. (org-current-line)
  16189. (buffer-file-name) sexp)
  16190. (sleep-for 2))))))
  16191. (cond ((stringp result) (split-string result "; "))
  16192. ((and (consp result)
  16193. (not (consp (cdr result)))
  16194. (stringp (cdr result))) (cdr result))
  16195. ((and (consp result)
  16196. (stringp (car result))) result)
  16197. (result entry))))
  16198. (defun org-diary-to-ical-string (frombuf)
  16199. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16200. This uses the icalendar.el library."
  16201. (let* ((tmpdir (if (featurep 'xemacs)
  16202. (temp-directory)
  16203. temporary-file-directory))
  16204. (tmpfile (make-temp-name
  16205. (expand-file-name "orgics" tmpdir)))
  16206. buf rtn b e)
  16207. (with-current-buffer frombuf
  16208. (icalendar-export-region (point-min) (point-max) tmpfile)
  16209. (setq buf (find-buffer-visiting tmpfile))
  16210. (set-buffer buf)
  16211. (goto-char (point-min))
  16212. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16213. (setq b (match-beginning 0)))
  16214. (goto-char (point-max))
  16215. (if (re-search-backward "^END:VEVENT" nil t)
  16216. (setq e (match-end 0)))
  16217. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16218. (kill-buffer buf)
  16219. (delete-file tmpfile)
  16220. rtn))
  16221. (defun org-closest-date (start current change prefer show-all)
  16222. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16223. When PREFER is `past', return a date that is either CURRENT or past.
  16224. When PREFER is `future', return a date that is either CURRENT or future.
  16225. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16226. ;; Make the proper lists from the dates
  16227. (catch 'exit
  16228. (let ((a1 '(("h" . hour)
  16229. ("d" . day)
  16230. ("w" . week)
  16231. ("m" . month)
  16232. ("y" . year)))
  16233. (shour (nth 2 (org-parse-time-string start)))
  16234. dn dw sday cday n1 n2 n0
  16235. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16236. (setq start (org-date-to-gregorian start)
  16237. current (org-date-to-gregorian
  16238. (if show-all
  16239. current
  16240. (time-to-days (current-time))))
  16241. sday (calendar-absolute-from-gregorian start)
  16242. cday (calendar-absolute-from-gregorian current))
  16243. (if (<= cday sday) (throw 'exit sday))
  16244. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16245. (setq dn (string-to-number (match-string 1 change))
  16246. dw (cdr (assoc (match-string 2 change) a1))))
  16247. (unless (and dn (> dn 0))
  16248. (user-error "Invalid change specifier: %s" change))
  16249. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16250. (cond
  16251. ((eq dw 'hour)
  16252. (let ((missing-hours
  16253. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16254. dn)))
  16255. (setq n1 (if (zerop missing-hours) cday
  16256. (- cday (1+ (floor (/ missing-hours 24)))))
  16257. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16258. ((eq dw 'day)
  16259. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16260. n2 (+ n1 dn)))
  16261. ((eq dw 'year)
  16262. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16263. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16264. (setq date1 (list m d y1)
  16265. n1 (calendar-absolute-from-gregorian date1)
  16266. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16267. n2 (calendar-absolute-from-gregorian date2)))
  16268. ((eq dw 'month)
  16269. ;; approx number of month between the two dates
  16270. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16271. ;; How often does dn fit in there?
  16272. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16273. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16274. m (+ m nm)
  16275. ny (floor (/ m 12))
  16276. y (+ y ny)
  16277. m (- m (* ny 12)))
  16278. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16279. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16280. (setq m2 (+ m dn) y2 y)
  16281. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16282. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16283. (while (<= n2 cday)
  16284. (setq n1 n2 m m2 y y2)
  16285. (setq m2 (+ m dn) y2 y)
  16286. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16287. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16288. ;; Make sure n1 is the earlier date
  16289. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16290. (if show-all
  16291. (cond
  16292. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16293. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16294. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16295. (cond
  16296. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16297. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16298. (t (if (= cday n1) n1 n2)))))))
  16299. (defun org-date-to-gregorian (date)
  16300. "Turn any specification of DATE into a Gregorian date for the calendar."
  16301. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16302. ((and (listp date) (= (length date) 3)) date)
  16303. ((stringp date)
  16304. (setq date (org-parse-time-string date))
  16305. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16306. ((listp date)
  16307. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16308. (defun org-parse-time-string (s &optional nodefault)
  16309. "Parse the standard Org-mode time string.
  16310. This should be a lot faster than the normal `parse-time-string'.
  16311. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16312. hour and minute fields will be nil if not given."
  16313. (cond ((string-match org-ts-regexp0 s)
  16314. (list 0
  16315. (if (or (match-beginning 8) (not nodefault))
  16316. (string-to-number (or (match-string 8 s) "0")))
  16317. (if (or (match-beginning 7) (not nodefault))
  16318. (string-to-number (or (match-string 7 s) "0")))
  16319. (string-to-number (match-string 4 s))
  16320. (string-to-number (match-string 3 s))
  16321. (string-to-number (match-string 2 s))
  16322. nil nil nil))
  16323. ((string-match "^<[^>]+>$" s)
  16324. (decode-time (seconds-to-time (org-matcher-time s))))
  16325. (t (error "Not a standard Org-mode time string: %s" s))))
  16326. (defun org-timestamp-up (&optional arg)
  16327. "Increase the date item at the cursor by one.
  16328. If the cursor is on the year, change the year. If it is on the month,
  16329. the day or the time, change that.
  16330. With prefix ARG, change by that many units."
  16331. (interactive "p")
  16332. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16333. (defun org-timestamp-down (&optional arg)
  16334. "Decrease the date item at the cursor by one.
  16335. If the cursor is on the year, change the year. If it is on the month,
  16336. the day or the time, change that.
  16337. With prefix ARG, change by that many units."
  16338. (interactive "p")
  16339. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16340. (defun org-timestamp-up-day (&optional arg)
  16341. "Increase the date in the time stamp by one day.
  16342. With prefix ARG, change that many days."
  16343. (interactive "p")
  16344. (if (and (not (org-at-timestamp-p t))
  16345. (org-at-heading-p))
  16346. (org-todo 'up)
  16347. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16348. (defun org-timestamp-down-day (&optional arg)
  16349. "Decrease the date in the time stamp by one day.
  16350. With prefix ARG, change that many days."
  16351. (interactive "p")
  16352. (if (and (not (org-at-timestamp-p t))
  16353. (org-at-heading-p))
  16354. (org-todo 'down)
  16355. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16356. (defun org-at-timestamp-p (&optional inactive-ok)
  16357. "Non-nil if point is inside a timestamp.
  16358. When optional argument INACTIVE-OK is non-nil, also consider
  16359. inactive timestamps.
  16360. When this function returns a non-nil value, match data is set
  16361. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16362. INACTIVE-OK."
  16363. (interactive)
  16364. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16365. (pos (point))
  16366. (ans (or (looking-at tsr)
  16367. (save-excursion
  16368. (skip-chars-backward "^[<\n\r\t")
  16369. (if (> (point) (point-min)) (backward-char 1))
  16370. (and (looking-at tsr)
  16371. (> (- (match-end 0) pos) -1))))))
  16372. (and ans
  16373. (boundp 'org-ts-what)
  16374. (setq org-ts-what
  16375. (cond
  16376. ((= pos (match-beginning 0)) 'bracket)
  16377. ;; Point is considered to be "on the bracket" whether
  16378. ;; it's really on it or right after it.
  16379. ((= pos (1- (match-end 0))) 'bracket)
  16380. ((= pos (match-end 0)) 'after)
  16381. ((org-pos-in-match-range pos 2) 'year)
  16382. ((org-pos-in-match-range pos 3) 'month)
  16383. ((org-pos-in-match-range pos 7) 'hour)
  16384. ((org-pos-in-match-range pos 8) 'minute)
  16385. ((or (org-pos-in-match-range pos 4)
  16386. (org-pos-in-match-range pos 5)) 'day)
  16387. ((and (> pos (or (match-end 8) (match-end 5)))
  16388. (< pos (match-end 0)))
  16389. (- pos (or (match-end 8) (match-end 5))))
  16390. (t 'day))))
  16391. ans))
  16392. (defun org-toggle-timestamp-type ()
  16393. "Toggle the type (<active> or [inactive]) of a time stamp."
  16394. (interactive)
  16395. (when (org-at-timestamp-p t)
  16396. (let ((beg (match-beginning 0)) (end (match-end 0))
  16397. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16398. (save-excursion
  16399. (goto-char beg)
  16400. (while (re-search-forward "[][<>]" end t)
  16401. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16402. t t)))
  16403. (message "Timestamp is now %sactive"
  16404. (if (equal (char-after beg) ?<) "" "in")))))
  16405. (defun org-at-clock-log-p nil
  16406. "Is the cursor on the clock log line?"
  16407. (save-excursion
  16408. (move-beginning-of-line 1)
  16409. (looking-at org-clock-line-re)))
  16410. (defvar org-clock-history) ; defined in org-clock.el
  16411. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16412. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16413. "Change the date in the time stamp at point.
  16414. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16415. `year', `minute', `second'. If WHAT is not given, the cursor position
  16416. in the timestamp determines what will be changed.
  16417. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16418. (let ((origin (point)) origin-cat
  16419. with-hm inactive
  16420. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16421. org-ts-what
  16422. extra rem
  16423. ts time time0 fixnext clrgx)
  16424. (if (not (org-at-timestamp-p t))
  16425. (user-error "Not at a timestamp"))
  16426. (if (and (not what) (eq org-ts-what 'bracket))
  16427. (org-toggle-timestamp-type)
  16428. ;; Point isn't on brackets. Remember the part of the time-stamp
  16429. ;; the point was in. Indeed, size of time-stamps may change,
  16430. ;; but point must be kept in the same category nonetheless.
  16431. (setq origin-cat org-ts-what)
  16432. (if (and (not what) (not (eq org-ts-what 'day))
  16433. org-display-custom-times
  16434. (get-text-property (point) 'display)
  16435. (not (get-text-property (1- (point)) 'display)))
  16436. (setq org-ts-what 'day))
  16437. (setq org-ts-what (or what org-ts-what)
  16438. inactive (= (char-after (match-beginning 0)) ?\[)
  16439. ts (match-string 0))
  16440. (replace-match "")
  16441. (when (string-match
  16442. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16443. ts)
  16444. (setq extra (match-string 1 ts))
  16445. (if suppress-tmp-delay
  16446. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16447. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16448. (setq with-hm t))
  16449. (setq time0 (org-parse-time-string ts))
  16450. (when (and updown
  16451. (eq org-ts-what 'minute)
  16452. (not current-prefix-arg))
  16453. ;; This looks like s-up and s-down. Change by one rounding step.
  16454. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16455. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16456. (setcar (cdr time0) (+ (nth 1 time0)
  16457. (if (> n 0) (- rem) (- dm rem))))))
  16458. (setq time
  16459. (encode-time (or (car time0) 0)
  16460. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16461. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16462. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16463. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16464. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16465. (nthcdr 6 time0)))
  16466. (when (and (member org-ts-what '(hour minute))
  16467. extra
  16468. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16469. (setq extra (org-modify-ts-extra
  16470. extra
  16471. (if (eq org-ts-what 'hour) 2 5)
  16472. n dm)))
  16473. (when (integerp org-ts-what)
  16474. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16475. (if (eq what 'calendar)
  16476. (let ((cal-date (org-get-date-from-calendar)))
  16477. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16478. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16479. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16480. (setcar time0 (or (car time0) 0))
  16481. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16482. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16483. (setq time (apply 'encode-time time0))))
  16484. ;; Insert the new time-stamp, and ensure point stays in the same
  16485. ;; category as before (i.e. not after the last position in that
  16486. ;; category).
  16487. (let ((pos (point)))
  16488. ;; Stay before inserted string. `save-excursion' is of no use.
  16489. (setq org-last-changed-timestamp
  16490. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16491. (goto-char pos))
  16492. (save-match-data
  16493. (looking-at org-ts-regexp3)
  16494. (goto-char (cond
  16495. ;; `day' category ends before `hour' if any, or at
  16496. ;; the end of the day name.
  16497. ((eq origin-cat 'day)
  16498. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16499. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16500. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16501. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16502. ;; `year' and `month' have both fixed size: point
  16503. ;; couldn't have moved into another part.
  16504. (t origin))))
  16505. ;; Update clock if on a CLOCK line.
  16506. (org-clock-update-time-maybe)
  16507. ;; Maybe adjust the closest clock in `org-clock-history'
  16508. (when org-clock-adjust-closest
  16509. (if (not (and (org-at-clock-log-p)
  16510. (< 1 (length (delq nil (mapcar 'marker-position
  16511. org-clock-history))))))
  16512. (message "No clock to adjust")
  16513. (cond ((save-excursion ; fix previous clock?
  16514. (re-search-backward org-ts-regexp0 nil t)
  16515. (org-looking-back (concat org-clock-string " \\[")))
  16516. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16517. ((save-excursion ; fix next clock?
  16518. (re-search-backward org-ts-regexp0 nil t)
  16519. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16520. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16521. (save-window-excursion
  16522. ;; Find closest clock to point, adjust the previous/next one in history
  16523. (let* ((p (save-excursion (org-back-to-heading t)))
  16524. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16525. (clfixnth
  16526. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16527. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16528. (if (not clfixpos)
  16529. (message "No clock to adjust")
  16530. (save-excursion
  16531. (org-goto-marker-or-bmk clfixpos)
  16532. (org-show-subtree)
  16533. (when (re-search-forward clrgx nil t)
  16534. (goto-char (match-beginning 1))
  16535. (let (org-clock-adjust-closest)
  16536. (org-timestamp-change n org-ts-what updown))
  16537. (message "Clock adjusted in %s for heading: %s"
  16538. (file-name-nondirectory (buffer-file-name))
  16539. (org-get-heading t t)))))))))
  16540. ;; Try to recenter the calendar window, if any.
  16541. (if (and org-calendar-follow-timestamp-change
  16542. (get-buffer-window "*Calendar*" t)
  16543. (memq org-ts-what '(day month year)))
  16544. (org-recenter-calendar (time-to-days time))))))
  16545. (defun org-modify-ts-extra (s pos n dm)
  16546. "Change the different parts of the lead-time and repeat fields in timestamp."
  16547. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16548. ng h m new rem)
  16549. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16550. (cond
  16551. ((or (org-pos-in-match-range pos 2)
  16552. (org-pos-in-match-range pos 3))
  16553. (setq m (string-to-number (match-string 3 s))
  16554. h (string-to-number (match-string 2 s)))
  16555. (if (org-pos-in-match-range pos 2)
  16556. (setq h (+ h n))
  16557. (setq n (* dm (org-no-warnings (signum n))))
  16558. (when (not (= 0 (setq rem (% m dm))))
  16559. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16560. (setq m (+ m n)))
  16561. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16562. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16563. (setq h (min 24 (max 0 h)))
  16564. (setq ng 1 new (format "-%02d:%02d" h m)))
  16565. ((org-pos-in-match-range pos 6)
  16566. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16567. ((org-pos-in-match-range pos 5)
  16568. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16569. ((org-pos-in-match-range pos 9)
  16570. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16571. ((org-pos-in-match-range pos 8)
  16572. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16573. (when ng
  16574. (setq s (concat
  16575. (substring s 0 (match-beginning ng))
  16576. new
  16577. (substring s (match-end ng))))))
  16578. s))
  16579. (defun org-recenter-calendar (date)
  16580. "If the calendar is visible, recenter it to DATE."
  16581. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16582. (when cwin
  16583. (let ((calendar-move-hook nil))
  16584. (with-selected-window cwin
  16585. (calendar-goto-date (if (listp date) date
  16586. (calendar-gregorian-from-absolute date))))))))
  16587. (defun org-goto-calendar (&optional arg)
  16588. "Go to the Emacs calendar at the current date.
  16589. If there is a time stamp in the current line, go to that date.
  16590. A prefix ARG can be used to force the current date."
  16591. (interactive "P")
  16592. (let ((tsr org-ts-regexp) diff
  16593. (calendar-move-hook nil)
  16594. (calendar-view-holidays-initially-flag nil)
  16595. (calendar-view-diary-initially-flag nil))
  16596. (if (or (org-at-timestamp-p)
  16597. (save-excursion
  16598. (beginning-of-line 1)
  16599. (looking-at (concat ".*" tsr))))
  16600. (let ((d1 (time-to-days (current-time)))
  16601. (d2 (time-to-days
  16602. (org-time-string-to-time (match-string 1)))))
  16603. (setq diff (- d2 d1))))
  16604. (calendar)
  16605. (calendar-goto-today)
  16606. (if (and diff (not arg)) (calendar-forward-day diff))))
  16607. (defun org-get-date-from-calendar ()
  16608. "Return a list (month day year) of date at point in calendar."
  16609. (with-current-buffer "*Calendar*"
  16610. (save-match-data
  16611. (calendar-cursor-to-date))))
  16612. (defun org-date-from-calendar ()
  16613. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16614. If there is already a time stamp at the cursor position, update it."
  16615. (interactive)
  16616. (if (org-at-timestamp-p t)
  16617. (org-timestamp-change 0 'calendar)
  16618. (let ((cal-date (org-get-date-from-calendar)))
  16619. (org-insert-time-stamp
  16620. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16621. (defcustom org-effort-durations
  16622. `(("min" . 1)
  16623. ("h" . 60)
  16624. ("d" . ,(* 60 8))
  16625. ("w" . ,(* 60 8 5))
  16626. ("m" . ,(* 60 8 5 4))
  16627. ("y" . ,(* 60 8 5 40)))
  16628. "Conversion factor to minutes for an effort modifier.
  16629. Each entry has the form (MODIFIER . MINUTES).
  16630. In an effort string, a number followed by MODIFIER is multiplied
  16631. by the specified number of MINUTES to obtain an effort in
  16632. minutes.
  16633. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16634. effort string \"2hours\" is equivalent to 120 minutes."
  16635. :group 'org-agenda
  16636. :version "25.1"
  16637. :package-version '(Org . "8.3")
  16638. :type '(alist :key-type (string :tag "Modifier")
  16639. :value-type (number :tag "Minutes")))
  16640. (defun org-minutes-to-clocksum-string (m)
  16641. "Format number of minutes as a clocksum string.
  16642. The format is determined by `org-time-clocksum-format',
  16643. `org-time-clocksum-use-fractional' and
  16644. `org-time-clocksum-fractional-format' and
  16645. `org-time-clocksum-use-effort-durations'."
  16646. (let ((clocksum "")
  16647. (m (round m)) ; Don't allow fractions of minutes
  16648. h d w mo y fmt n)
  16649. (setq h (if org-time-clocksum-use-effort-durations
  16650. (cdr (assoc "h" org-effort-durations)) 60)
  16651. d (if org-time-clocksum-use-effort-durations
  16652. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16653. w (if org-time-clocksum-use-effort-durations
  16654. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16655. mo (if org-time-clocksum-use-effort-durations
  16656. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16657. y (if org-time-clocksum-use-effort-durations
  16658. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16659. ;; fractional format
  16660. (if org-time-clocksum-use-fractional
  16661. (cond
  16662. ;; single format string
  16663. ((stringp org-time-clocksum-fractional-format)
  16664. (format org-time-clocksum-fractional-format (/ m (float h))))
  16665. ;; choice of fractional formats for different time units
  16666. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16667. (> (/ (truncate m) (* y d h)) 0))
  16668. (format fmt (/ m (* y d (float h)))))
  16669. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16670. (> (/ (truncate m) (* mo d h)) 0))
  16671. (format fmt (/ m (* mo d (float h)))))
  16672. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16673. (> (/ (truncate m) (* w d h)) 0))
  16674. (format fmt (/ m (* w d (float h)))))
  16675. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16676. (> (/ (truncate m) (* d h)) 0))
  16677. (format fmt (/ m (* d (float h)))))
  16678. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16679. (> (/ (truncate m) h) 0))
  16680. (format fmt (/ m (float h))))
  16681. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16682. (format fmt m))
  16683. ;; fall back to smallest time unit with a format
  16684. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16685. (format fmt (/ m (float h))))
  16686. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16687. (format fmt (/ m (* d (float h)))))
  16688. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16689. (format fmt (/ m (* w d (float h)))))
  16690. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16691. (format fmt (/ m (* mo d (float h)))))
  16692. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16693. (format fmt (/ m (* y d (float h))))))
  16694. ;; standard (non-fractional) format, with single format string
  16695. (if (stringp org-time-clocksum-format)
  16696. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16697. ;; separate formats components
  16698. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16699. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16700. (plist-get org-time-clocksum-format :require-years))
  16701. (setq clocksum (concat clocksum (format fmt n))
  16702. m (- m (* n y d h))))
  16703. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16704. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16705. (plist-get org-time-clocksum-format :require-months))
  16706. (setq clocksum (concat clocksum (format fmt n))
  16707. m (- m (* n mo d h))))
  16708. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16709. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16710. (plist-get org-time-clocksum-format :require-weeks))
  16711. (setq clocksum (concat clocksum (format fmt n))
  16712. m (- m (* n w d h))))
  16713. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16714. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16715. (plist-get org-time-clocksum-format :require-days))
  16716. (setq clocksum (concat clocksum (format fmt n))
  16717. m (- m (* n d h))))
  16718. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16719. (or (> (setq n (/ (truncate m) h)) 0)
  16720. (plist-get org-time-clocksum-format :require-hours))
  16721. (setq clocksum (concat clocksum (format fmt n))
  16722. m (- m (* n h))))
  16723. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16724. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16725. (setq clocksum (concat clocksum (format fmt m))))
  16726. ;; return formatted time duration
  16727. clocksum))))
  16728. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16729. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16730. "Org mode version 8.0")
  16731. (defun org-hours-to-clocksum-string (n)
  16732. (org-minutes-to-clocksum-string (* n 60)))
  16733. (defun org-hh:mm-string-to-minutes (s)
  16734. "Convert a string H:MM to a number of minutes.
  16735. If the string is just a number, interpret it as minutes.
  16736. In fact, the first hh:mm or number in the string will be taken,
  16737. there can be extra stuff in the string.
  16738. If no number is found, the return value is 0."
  16739. (cond
  16740. ((integerp s) s)
  16741. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16742. (+ (* (string-to-number (match-string 1 s)) 60)
  16743. (string-to-number (match-string 2 s))))
  16744. ((string-match "\\([0-9]+\\)" s)
  16745. (string-to-number (match-string 1 s)))
  16746. (t 0)))
  16747. (defcustom org-image-actual-width t
  16748. "Should we use the actual width of images when inlining them?
  16749. When set to t, always use the image width.
  16750. When set to a number, use imagemagick (when available) to set
  16751. the image's width to this value.
  16752. When set to a number in a list, try to get the width from any
  16753. #+ATTR.* keyword if it matches a width specification like
  16754. #+ATTR_HTML: :width 300px
  16755. and fall back on that number if none is found.
  16756. When set to nil, try to get the width from an #+ATTR.* keyword
  16757. and fall back on the original width if none is found.
  16758. This requires Emacs >= 24.1, build with imagemagick support."
  16759. :group 'org-appearance
  16760. :version "24.4"
  16761. :package-version '(Org . "8.0")
  16762. :type '(choice
  16763. (const :tag "Use the image width" t)
  16764. (integer :tag "Use a number of pixels")
  16765. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16766. (const :tag "Use #+ATTR* or don't resize" nil)))
  16767. (defcustom org-agenda-inhibit-startup nil
  16768. "Inhibit startup when preparing agenda buffers.
  16769. When this variable is t, the initialization of the Org agenda
  16770. buffers is inhibited: e.g. the visibility state is not set, the
  16771. tables are not re-aligned, etc."
  16772. :type 'boolean
  16773. :version "24.3"
  16774. :group 'org-agenda)
  16775. (define-obsolete-variable-alias
  16776. 'org-agenda-ignore-drawer-properties
  16777. 'org-agenda-ignore-properties "25.1")
  16778. (defcustom org-agenda-ignore-properties nil
  16779. "Avoid updating text properties when building the agenda.
  16780. Properties are used to prepare buffers for effort estimates,
  16781. appointments, statistics and subtree-local categories.
  16782. If you don't use these in the agenda, you can add them to this
  16783. list and agenda building will be a bit faster.
  16784. The value is a list, with zero or more of the symbols `effort', `appt',
  16785. `stats' or `category'."
  16786. :type '(set :greedy t
  16787. (const effort)
  16788. (const appt)
  16789. (const stats)
  16790. (const category))
  16791. :version "25.1"
  16792. :package-version '(Org . "8.3")
  16793. :group 'org-agenda)
  16794. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16795. "Convert a duration string S to minutes.
  16796. A bare number is interpreted as minutes, modifiers can be set by
  16797. customizing `org-effort-durations' (which see).
  16798. Entries containing a colon are interpreted as H:MM by
  16799. `org-hh:mm-string-to-minutes'."
  16800. (let ((result 0)
  16801. (re (concat "\\([0-9.]+\\) *\\("
  16802. (regexp-opt (mapcar 'car org-effort-durations))
  16803. "\\)")))
  16804. (while (string-match re s)
  16805. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16806. (string-to-number (match-string 1 s))))
  16807. (setq s (replace-match "" nil t s)))
  16808. (setq result (floor result))
  16809. (incf result (org-hh:mm-string-to-minutes s))
  16810. (if output-to-string (number-to-string result) result)))
  16811. ;;;; Files
  16812. (defun org-save-all-org-buffers ()
  16813. "Save all Org-mode buffers without user confirmation."
  16814. (interactive)
  16815. (message "Saving all Org-mode buffers...")
  16816. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16817. (when (featurep 'org-id) (org-id-locations-save))
  16818. (message "Saving all Org-mode buffers... done"))
  16819. (defun org-revert-all-org-buffers ()
  16820. "Revert all Org-mode buffers.
  16821. Prompt for confirmation when there are unsaved changes.
  16822. Be sure you know what you are doing before letting this function
  16823. overwrite your changes.
  16824. This function is useful in a setup where one tracks org files
  16825. with a version control system, to revert on one machine after pulling
  16826. changes from another. I believe the procedure must be like this:
  16827. 1. M-x org-save-all-org-buffers
  16828. 2. Pull changes from the other machine, resolve conflicts
  16829. 3. M-x org-revert-all-org-buffers"
  16830. (interactive)
  16831. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16832. (user-error "Abort"))
  16833. (save-excursion
  16834. (save-window-excursion
  16835. (mapc
  16836. (lambda (b)
  16837. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16838. (with-current-buffer b buffer-file-name))
  16839. (org-pop-to-buffer-same-window b)
  16840. (revert-buffer t 'no-confirm)))
  16841. (buffer-list))
  16842. (when (and (featurep 'org-id) org-id-track-globally)
  16843. (org-id-locations-load)))))
  16844. ;;;; Agenda files
  16845. ;;;###autoload
  16846. (defun org-switchb (&optional arg)
  16847. "Switch between Org buffers.
  16848. With one prefix argument, restrict available buffers to files.
  16849. With two prefix arguments, restrict available buffers to agenda files.
  16850. Defaults to `iswitchb' for buffer name completion.
  16851. Set `org-completion-use-ido' to make it use ido instead."
  16852. (interactive "P")
  16853. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16854. ((equal arg '(16)) (org-buffer-list 'agenda))
  16855. (t (org-buffer-list))))
  16856. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16857. (org-completion-use-ido org-completion-use-ido))
  16858. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16859. (setq org-completion-use-iswitchb t))
  16860. (org-pop-to-buffer-same-window
  16861. (org-icompleting-read "Org buffer: "
  16862. (mapcar 'list (mapcar 'buffer-name blist))
  16863. nil t))))
  16864. ;;; Define some older names previously used for this functionality
  16865. ;;;###autoload
  16866. (defalias 'org-ido-switchb 'org-switchb)
  16867. ;;;###autoload
  16868. (defalias 'org-iswitchb 'org-switchb)
  16869. (defun org-buffer-list (&optional predicate exclude-tmp)
  16870. "Return a list of Org buffers.
  16871. PREDICATE can be `export', `files' or `agenda'.
  16872. export restrict the list to Export buffers.
  16873. files restrict the list to buffers visiting Org files.
  16874. agenda restrict the list to buffers visiting agenda files.
  16875. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16876. (let* ((bfn nil)
  16877. (agenda-files (and (eq predicate 'agenda)
  16878. (mapcar 'file-truename (org-agenda-files t))))
  16879. (filter
  16880. (cond
  16881. ((eq predicate 'files)
  16882. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16883. ((eq predicate 'export)
  16884. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16885. ((eq predicate 'agenda)
  16886. (lambda (b)
  16887. (with-current-buffer b
  16888. (and (derived-mode-p 'org-mode)
  16889. (setq bfn (buffer-file-name b))
  16890. (member (file-truename bfn) agenda-files)))))
  16891. (t (lambda (b) (with-current-buffer b
  16892. (or (derived-mode-p 'org-mode)
  16893. (string-match "\*Org .*Export"
  16894. (buffer-name b)))))))))
  16895. (delq nil
  16896. (mapcar
  16897. (lambda(b)
  16898. (if (and (funcall filter b)
  16899. (or (not exclude-tmp)
  16900. (not (string-match "tmp" (buffer-name b)))))
  16901. b
  16902. nil))
  16903. (buffer-list)))))
  16904. (defun org-agenda-files (&optional unrestricted archives)
  16905. "Get the list of agenda files.
  16906. Optional UNRESTRICTED means return the full list even if a restriction
  16907. is currently in place.
  16908. When ARCHIVES is t, include all archive files that are really being
  16909. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16910. `org-agenda-archives-mode' is t."
  16911. (let ((files
  16912. (cond
  16913. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16914. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16915. ((listp org-agenda-files) org-agenda-files)
  16916. (t (error "Invalid value of `org-agenda-files'")))))
  16917. (setq files (apply 'append
  16918. (mapcar (lambda (f)
  16919. (if (file-directory-p f)
  16920. (directory-files
  16921. f t org-agenda-file-regexp)
  16922. (list f)))
  16923. files)))
  16924. (when org-agenda-skip-unavailable-files
  16925. (setq files (delq nil
  16926. (mapcar (function
  16927. (lambda (file)
  16928. (and (file-readable-p file) file)))
  16929. files))))
  16930. (when (or (eq archives t)
  16931. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16932. (setq files (org-add-archive-files files)))
  16933. files))
  16934. (defun org-agenda-file-p (&optional file)
  16935. "Return non-nil, if FILE is an agenda file.
  16936. If FILE is omitted, use the file associated with the current
  16937. buffer."
  16938. (let ((fname (or file (buffer-file-name))))
  16939. (and fname
  16940. (member (file-truename fname)
  16941. (mapcar #'file-truename (org-agenda-files t))))))
  16942. (defun org-edit-agenda-file-list ()
  16943. "Edit the list of agenda files.
  16944. Depending on setup, this either uses customize to edit the variable
  16945. `org-agenda-files', or it visits the file that is holding the list. In the
  16946. latter case, the buffer is set up in a way that saving it automatically kills
  16947. the buffer and restores the previous window configuration."
  16948. (interactive)
  16949. (if (stringp org-agenda-files)
  16950. (let ((cw (current-window-configuration)))
  16951. (find-file org-agenda-files)
  16952. (org-set-local 'org-window-configuration cw)
  16953. (org-add-hook 'after-save-hook
  16954. (lambda ()
  16955. (set-window-configuration
  16956. (prog1 org-window-configuration
  16957. (kill-buffer (current-buffer))))
  16958. (org-install-agenda-files-menu)
  16959. (message "New agenda file list installed"))
  16960. nil 'local)
  16961. (message "%s" (substitute-command-keys
  16962. "Edit list and finish with \\[save-buffer]")))
  16963. (customize-variable 'org-agenda-files)))
  16964. (defun org-store-new-agenda-file-list (list)
  16965. "Set new value for the agenda file list and save it correctly."
  16966. (if (stringp org-agenda-files)
  16967. (let ((fe (org-read-agenda-file-list t)) b u)
  16968. (while (setq b (find-buffer-visiting org-agenda-files))
  16969. (kill-buffer b))
  16970. (with-temp-file org-agenda-files
  16971. (insert
  16972. (mapconcat
  16973. (lambda (f) ;; Keep un-expanded entries.
  16974. (if (setq u (assoc f fe))
  16975. (cdr u)
  16976. f))
  16977. list "\n")
  16978. "\n")))
  16979. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16980. (org-insert-mode-line-in-empty-file nil))
  16981. (setq org-agenda-files list)
  16982. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16983. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16984. "Read the list of agenda files from a file.
  16985. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16986. filenames, used by `org-store-new-agenda-file-list' to write back
  16987. un-expanded file names."
  16988. (when (file-directory-p org-agenda-files)
  16989. (error "`org-agenda-files' cannot be a single directory"))
  16990. (when (stringp org-agenda-files)
  16991. (with-temp-buffer
  16992. (insert-file-contents org-agenda-files)
  16993. (mapcar
  16994. (lambda (f)
  16995. (let ((e (expand-file-name (substitute-in-file-name f)
  16996. org-directory)))
  16997. (if pair-with-expansion
  16998. (cons e f)
  16999. e)))
  17000. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17001. ;;;###autoload
  17002. (defun org-cycle-agenda-files ()
  17003. "Cycle through the files in `org-agenda-files'.
  17004. If the current buffer visits an agenda file, find the next one in the list.
  17005. If the current buffer does not, find the first agenda file."
  17006. (interactive)
  17007. (let* ((fs (or (org-agenda-files t)
  17008. (user-error "No agenda files")))
  17009. (files (copy-sequence fs))
  17010. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17011. file)
  17012. (when tcf
  17013. (while (and (setq file (pop files))
  17014. (not (equal (file-truename file) tcf)))))
  17015. (find-file (car (or files fs)))
  17016. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17017. (defun org-agenda-file-to-front (&optional to-end)
  17018. "Move/add the current file to the top of the agenda file list.
  17019. If the file is not present in the list, it is added to the front. If it is
  17020. present, it is moved there. With optional argument TO-END, add/move to the
  17021. end of the list."
  17022. (interactive "P")
  17023. (let ((org-agenda-skip-unavailable-files nil)
  17024. (file-alist (mapcar (lambda (x)
  17025. (cons (file-truename x) x))
  17026. (org-agenda-files t)))
  17027. (ctf (file-truename
  17028. (or buffer-file-name
  17029. (user-error "Please save the current buffer to a file"))))
  17030. x had)
  17031. (setq x (assoc ctf file-alist) had x)
  17032. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17033. (if to-end
  17034. (setq file-alist (append (delq x file-alist) (list x)))
  17035. (setq file-alist (cons x (delq x file-alist))))
  17036. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17037. (org-install-agenda-files-menu)
  17038. (message "File %s to %s of agenda file list"
  17039. (if had "moved" "added") (if to-end "end" "front"))))
  17040. (defun org-remove-file (&optional file)
  17041. "Remove current file from the list of files in variable `org-agenda-files'.
  17042. These are the files which are being checked for agenda entries.
  17043. Optional argument FILE means use this file instead of the current."
  17044. (interactive)
  17045. (let* ((org-agenda-skip-unavailable-files nil)
  17046. (file (or file buffer-file-name
  17047. (user-error "Current buffer does not visit a file")))
  17048. (true-file (file-truename file))
  17049. (afile (abbreviate-file-name file))
  17050. (files (delq nil (mapcar
  17051. (lambda (x)
  17052. (if (equal true-file
  17053. (file-truename x))
  17054. nil x))
  17055. (org-agenda-files t)))))
  17056. (if (not (= (length files) (length (org-agenda-files t))))
  17057. (progn
  17058. (org-store-new-agenda-file-list files)
  17059. (org-install-agenda-files-menu)
  17060. (message "Removed from Org Agenda list: %s" afile))
  17061. (message "File was not in list: %s (not removed)" afile))))
  17062. (defun org-file-menu-entry (file)
  17063. (vector file (list 'find-file file) t))
  17064. (defun org-check-agenda-file (file)
  17065. "Make sure FILE exists. If not, ask user what to do."
  17066. (when (not (file-exists-p file))
  17067. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17068. (abbreviate-file-name file))
  17069. (let ((r (downcase (read-char-exclusive))))
  17070. (cond
  17071. ((equal r ?r)
  17072. (org-remove-file file)
  17073. (throw 'nextfile t))
  17074. (t (user-error "Abort"))))))
  17075. (defun org-get-agenda-file-buffer (file)
  17076. "Get an agenda buffer visiting FILE.
  17077. If the buffer needs to be created, add it to the list of buffers
  17078. which might be released later."
  17079. (let ((buf (org-find-base-buffer-visiting file)))
  17080. (if buf
  17081. buf ; just return it
  17082. ;; Make a new buffer and remember it
  17083. (setq buf (find-file-noselect file))
  17084. (if buf (push buf org-agenda-new-buffers))
  17085. buf)))
  17086. (defun org-release-buffers (blist)
  17087. "Release all buffers in list, asking the user for confirmation when needed.
  17088. When a buffer is unmodified, it is just killed. When modified, it is saved
  17089. \(if the user agrees) and then killed."
  17090. (let (file)
  17091. (dolist (buf blist)
  17092. (setq file (buffer-file-name buf))
  17093. (when (and (buffer-modified-p buf)
  17094. file
  17095. (y-or-n-p (format "Save file %s? " file)))
  17096. (with-current-buffer buf (save-buffer)))
  17097. (kill-buffer buf))))
  17098. (defun org-agenda-prepare-buffers (files)
  17099. "Create buffers for all agenda files, protect archived trees and comments."
  17100. (interactive)
  17101. (let ((pa '(:org-archived t))
  17102. (pc '(:org-comment t))
  17103. (pall '(:org-archived t :org-comment t))
  17104. (inhibit-read-only t)
  17105. (org-inhibit-startup org-agenda-inhibit-startup)
  17106. (rea (concat ":" org-archive-tag ":"))
  17107. file re pos)
  17108. (setq org-tag-alist-for-agenda nil
  17109. org-tag-groups-alist-for-agenda nil)
  17110. (save-excursion
  17111. (save-restriction
  17112. (dolist (file files)
  17113. (catch 'nextfile
  17114. (if (bufferp file)
  17115. (set-buffer file)
  17116. (org-check-agenda-file file)
  17117. (set-buffer (org-get-agenda-file-buffer file)))
  17118. (widen)
  17119. (org-set-regexps-and-options 'tags-only)
  17120. (setq pos (point))
  17121. (or (memq 'category org-agenda-ignore-properties)
  17122. (org-refresh-category-properties))
  17123. (or (memq 'stats org-agenda-ignore-properties)
  17124. (org-refresh-stats-properties))
  17125. (or (memq 'effort org-agenda-ignore-properties)
  17126. (org-refresh-effort-properties))
  17127. (or (memq 'appt org-agenda-ignore-properties)
  17128. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17129. (setq org-todo-keywords-for-agenda
  17130. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17131. (setq org-done-keywords-for-agenda
  17132. (append org-done-keywords-for-agenda org-done-keywords))
  17133. (setq org-todo-keyword-alist-for-agenda
  17134. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17135. (setq org-tag-alist-for-agenda
  17136. (org-uniquify
  17137. (append org-tag-alist-for-agenda
  17138. org-tag-alist
  17139. org-tag-persistent-alist)))
  17140. (if org-group-tags
  17141. (setq org-tag-groups-alist-for-agenda
  17142. (org-uniquify-alist
  17143. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17144. (org-with-silent-modifications
  17145. (save-excursion
  17146. (remove-text-properties (point-min) (point-max) pall)
  17147. (when org-agenda-skip-archived-trees
  17148. (goto-char (point-min))
  17149. (while (re-search-forward rea nil t)
  17150. (if (org-at-heading-p t)
  17151. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17152. (goto-char (point-min))
  17153. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17154. (while (re-search-forward re nil t)
  17155. (when (save-match-data (org-in-commented-heading-p t))
  17156. (add-text-properties
  17157. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17158. (goto-char pos)))))
  17159. (setq org-todo-keywords-for-agenda
  17160. (org-uniquify org-todo-keywords-for-agenda))
  17161. (setq org-todo-keyword-alist-for-agenda
  17162. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17163. ;;;; CDLaTeX minor mode
  17164. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17165. "Keymap for the minor `org-cdlatex-mode'.")
  17166. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17167. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17168. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17169. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17170. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17171. (defvar org-cdlatex-texmathp-advice-is-done nil
  17172. "Flag remembering if we have applied the advice to texmathp already.")
  17173. (define-minor-mode org-cdlatex-mode
  17174. "Toggle the minor `org-cdlatex-mode'.
  17175. This mode supports entering LaTeX environment and math in LaTeX fragments
  17176. in Org-mode.
  17177. \\{org-cdlatex-mode-map}"
  17178. nil " OCDL" nil
  17179. (when org-cdlatex-mode
  17180. (require 'cdlatex)
  17181. (run-hooks 'cdlatex-mode-hook)
  17182. (cdlatex-compute-tables))
  17183. (unless org-cdlatex-texmathp-advice-is-done
  17184. (setq org-cdlatex-texmathp-advice-is-done t)
  17185. (defadvice texmathp (around org-math-always-on activate)
  17186. "Always return t in org-mode buffers.
  17187. This is because we want to insert math symbols without dollars even outside
  17188. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17189. an embedded LaTeX fragment, let texmathp do its job.
  17190. \\[org-cdlatex-mode-map]"
  17191. (interactive)
  17192. (let (p)
  17193. (cond
  17194. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17195. ((eq this-command 'cdlatex-math-symbol)
  17196. (setq ad-return-value t
  17197. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17198. (t
  17199. (let ((p (org-inside-LaTeX-fragment-p)))
  17200. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17201. (setq ad-return-value t
  17202. texmathp-why '("Org-mode embedded math" . 0))
  17203. (if p ad-do-it)))))))))
  17204. (defun turn-on-org-cdlatex ()
  17205. "Unconditionally turn on `org-cdlatex-mode'."
  17206. (org-cdlatex-mode 1))
  17207. (defun org-try-cdlatex-tab ()
  17208. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17209. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17210. - inside a LaTeX fragment, or
  17211. - after the first word in a line, where an abbreviation expansion could
  17212. insert a LaTeX environment."
  17213. (when org-cdlatex-mode
  17214. (cond
  17215. ;; Before any word on the line: No expansion possible.
  17216. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17217. ;; Just after first word on the line: Expand it. Make sure it
  17218. ;; cannot happen on headlines, though.
  17219. ((save-excursion
  17220. (skip-chars-backward "a-zA-Z0-9*")
  17221. (skip-chars-backward " \t")
  17222. (and (bolp) (not (org-at-heading-p))))
  17223. (cdlatex-tab) t)
  17224. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17225. (defun org-cdlatex-underscore-caret (&optional arg)
  17226. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17227. Revert to the normal definition outside of these fragments."
  17228. (interactive "P")
  17229. (if (org-inside-LaTeX-fragment-p)
  17230. (call-interactively 'cdlatex-sub-superscript)
  17231. (let (org-cdlatex-mode)
  17232. (call-interactively (key-binding (vector last-input-event))))))
  17233. (defun org-cdlatex-math-modify (&optional arg)
  17234. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17235. Revert to the normal definition outside of these fragments."
  17236. (interactive "P")
  17237. (if (org-inside-LaTeX-fragment-p)
  17238. (call-interactively 'cdlatex-math-modify)
  17239. (let (org-cdlatex-mode)
  17240. (call-interactively (key-binding (vector last-input-event))))))
  17241. (defun org-cdlatex-environment-indent (&optional environment item)
  17242. "Execute `cdlatex-environment' and indent the inserted environment.
  17243. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17244. The inserted environment is indented to current indentation
  17245. unless point is at the beginning of the line, in which the
  17246. environment remains unintended."
  17247. (interactive)
  17248. ;; cdlatex-environment always return nil. Therefore, capture output
  17249. ;; first and determine if an environment was selected.
  17250. (let* ((beg (point-marker))
  17251. (end (copy-marker (point) t))
  17252. (inserted (progn
  17253. (ignore-errors (cdlatex-environment environment item))
  17254. (< beg end)))
  17255. ;; Figure out how many lines to move forward after the
  17256. ;; environment has been inserted.
  17257. (lines (when inserted
  17258. (save-excursion
  17259. (- (loop while (< beg (point))
  17260. with x = 0
  17261. do (forward-line -1)
  17262. (incf x)
  17263. finally return x)
  17264. (if (progn (goto-char beg)
  17265. (and (progn (skip-chars-forward " \t") (eolp))
  17266. (progn (skip-chars-backward " \t") (bolp))))
  17267. 1 0)))))
  17268. (env (org-trim (delete-and-extract-region beg end))))
  17269. (when inserted
  17270. ;; Get indentation of next line unless at column 0.
  17271. (let ((ind (if (bolp) 0
  17272. (save-excursion
  17273. (org-return-indent)
  17274. (prog1 (org-get-indentation)
  17275. (when (progn (skip-chars-forward " \t") (eolp))
  17276. (delete-region beg (point)))))))
  17277. (bol (progn (skip-chars-backward " \t") (bolp))))
  17278. ;; Insert a newline before environment unless at column zero
  17279. ;; to "escape" the current line. Insert a newline if
  17280. ;; something is one the same line as \end{ENVIRONMENT}.
  17281. (insert
  17282. (concat (unless bol "\n") env
  17283. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17284. (unless (zerop ind)
  17285. (save-excursion
  17286. (goto-char beg)
  17287. (while (< (point) end)
  17288. (unless (eolp) (org-indent-line-to ind))
  17289. (forward-line))))
  17290. (goto-char beg)
  17291. (forward-line lines)
  17292. (org-indent-line-to ind)))
  17293. (set-marker beg nil)
  17294. (set-marker end nil)))
  17295. ;;;; LaTeX fragments
  17296. (defun org-inside-LaTeX-fragment-p ()
  17297. "Test if point is inside a LaTeX fragment.
  17298. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17299. sequence appearing also before point.
  17300. Even though the matchers for math are configurable, this function assumes
  17301. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17302. delimiters are skipped when they have been removed by customization.
  17303. The return value is nil, or a cons cell with the delimiter and the
  17304. position of this delimiter.
  17305. This function does a reasonably good job, but can locally be fooled by
  17306. for example currency specifications. For example it will assume being in
  17307. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17308. fragments that are properly closed, but during editing, we have to live
  17309. with the uncertainty caused by missing closing delimiters. This function
  17310. looks only before point, not after."
  17311. (catch 'exit
  17312. (let ((pos (point))
  17313. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17314. (lim (progn
  17315. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17316. (point)))
  17317. dd-on str (start 0) m re)
  17318. (goto-char pos)
  17319. (when dodollar
  17320. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17321. re (nth 1 (assoc "$" org-latex-regexps)))
  17322. (while (string-match re str start)
  17323. (cond
  17324. ((= (match-end 0) (length str))
  17325. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17326. ((= (match-end 0) (- (length str) 5))
  17327. (throw 'exit nil))
  17328. (t (setq start (match-end 0))))))
  17329. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17330. (goto-char pos)
  17331. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17332. (and (match-beginning 2) (throw 'exit nil))
  17333. ;; count $$
  17334. (while (re-search-backward "\\$\\$" lim t)
  17335. (setq dd-on (not dd-on)))
  17336. (goto-char pos)
  17337. (if dd-on (cons "$$" m))))))
  17338. (defun org-inside-latex-macro-p ()
  17339. "Is point inside a LaTeX macro or its arguments?"
  17340. (save-match-data
  17341. (org-in-regexp
  17342. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17343. (defvar org-latex-fragment-image-overlays nil
  17344. "List of overlays carrying the images of latex fragments.")
  17345. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17346. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17347. "Remove all overlays with LaTeX fragment images in current buffer.
  17348. When optional arguments BEG and END are non-nil, remove all
  17349. overlays between them instead. Return t when some overlays were
  17350. removed, nil otherwise."
  17351. (let (removedp)
  17352. (setq org-latex-fragment-image-overlays
  17353. (let ((beg (or beg (point-min)))
  17354. (end (or end (point-max))))
  17355. (org-remove-if
  17356. (lambda (o)
  17357. (and (>= (overlay-start o) beg)
  17358. (<= (overlay-end o) end)
  17359. (progn (delete-overlay o)
  17360. (or removedp (setq removedp t)))))
  17361. org-latex-fragment-image-overlays)))
  17362. removedp))
  17363. (define-obsolete-function-alias
  17364. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17365. (defun org-toggle-latex-fragment (&optional arg)
  17366. "Preview the LaTeX fragment at point, or all locally or globally.
  17367. If the cursor is on a LaTeX fragment, create the image and overlay
  17368. it over the source code, if there is none. Remove it otherwise.
  17369. If there is no fragment at point, display all fragments in the
  17370. current section.
  17371. With prefix ARG, preview or clear image for all fragments in the
  17372. current subtree or in the whole buffer when used before the first
  17373. headline. With a double prefix ARG \\[universal-argument] \
  17374. \\[universal-argument] preview or clear images
  17375. for all fragments in the buffer."
  17376. (interactive "P")
  17377. (unless (buffer-file-name (buffer-base-buffer))
  17378. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17379. (when (display-graphic-p)
  17380. (catch 'exit
  17381. (save-excursion
  17382. (let ((window-start (window-start)) msg)
  17383. (save-restriction
  17384. (cond
  17385. ((or (equal arg '(16))
  17386. (and (equal arg '(4))
  17387. (org-with-limited-levels (org-before-first-heading-p))))
  17388. (if (org-remove-latex-fragment-image-overlays)
  17389. (progn (message "LaTeX fragments images removed from buffer")
  17390. (throw 'exit nil))
  17391. (setq msg "Creating images for buffer...")))
  17392. ((equal arg '(4))
  17393. (org-with-limited-levels (org-back-to-heading t))
  17394. (let ((beg (point))
  17395. (end (progn (org-end-of-subtree t) (point))))
  17396. (if (org-remove-latex-fragment-image-overlays beg end)
  17397. (progn
  17398. (message "LaTeX fragment images removed from subtree")
  17399. (throw 'exit nil))
  17400. (setq msg "Creating images for subtree...")
  17401. (narrow-to-region beg end))))
  17402. ((let ((datum (org-element-context)))
  17403. (when (memq (org-element-type datum)
  17404. '(latex-environment latex-fragment))
  17405. (let* ((beg (org-element-property :begin datum))
  17406. (end (org-element-property :end datum)))
  17407. (if (org-remove-latex-fragment-image-overlays beg end)
  17408. (progn (message "LaTeX fragment image removed")
  17409. (throw 'exit nil))
  17410. (narrow-to-region beg end)
  17411. (setq msg "Creating image..."))))))
  17412. (t
  17413. (org-with-limited-levels
  17414. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17415. (outline-previous-heading)
  17416. (point)))
  17417. (end (progn (outline-next-heading) (point))))
  17418. (if (org-remove-latex-fragment-image-overlays beg end)
  17419. (progn
  17420. (message "LaTeX fragment images removed from section")
  17421. (throw 'exit nil))
  17422. (setq msg "Creating images for section...")
  17423. (narrow-to-region beg end))))))
  17424. (let ((file (buffer-file-name (buffer-base-buffer))))
  17425. (org-format-latex
  17426. (concat org-latex-preview-ltxpng-directory
  17427. (file-name-sans-extension (file-name-nondirectory file)))
  17428. ;; Emacs cannot overlay images from remote hosts.
  17429. ;; Create it in `temporary-file-directory' instead.
  17430. (if (file-remote-p file) temporary-file-directory
  17431. default-directory)
  17432. 'overlays msg 'forbuffer
  17433. org-latex-create-formula-image-program)))
  17434. ;; Work around a bug that doesn't restore window's start
  17435. ;; when widening back the buffer.
  17436. (set-window-start nil window-start)
  17437. (message (concat msg "done")))))))
  17438. (defun org-format-latex
  17439. (prefix &optional dir overlays msg forbuffer processing-type)
  17440. "Replace LaTeX fragments with links to an image, and produce images.
  17441. When optional argument OVERLAYS is non-nil, display the image on
  17442. top of the fragment instead of replacing it.
  17443. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17444. Some of the options can be changed using the variable
  17445. `org-format-latex-options', which see."
  17446. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17447. (unless (eq processing-type 'verbatim)
  17448. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17449. (cnt 0)
  17450. checkdir-flag)
  17451. (goto-char (point-min))
  17452. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17453. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17454. (overlay-recenter (point-max)))
  17455. (while (re-search-forward math-regexp nil t)
  17456. (unless (and overlays
  17457. (eq (get-char-property (point) 'org-overlay-type)
  17458. 'org-latex-overlay))
  17459. (let* ((context (org-element-context))
  17460. (type (org-element-type context)))
  17461. (when (memq type '(latex-environment latex-fragment))
  17462. (let ((block-type (eq type 'latex-environment))
  17463. (value (org-element-property :value context))
  17464. (beg (org-element-property :begin context))
  17465. (end (save-excursion
  17466. (goto-char (org-element-property :end context))
  17467. (skip-chars-backward " \r\t\n")
  17468. (point))))
  17469. (case processing-type
  17470. (mathjax
  17471. ;; Prepare for MathJax processing.
  17472. (if (eq (char-after beg) ?$)
  17473. (save-excursion
  17474. (delete-region beg end)
  17475. (insert "\\(" (substring value 1 -1) "\\)"))
  17476. (goto-char end)))
  17477. ((dvipng imagemagick)
  17478. ;; Process to an image.
  17479. (incf cnt)
  17480. (goto-char beg)
  17481. (let* ((face (face-at-point))
  17482. ;; Get the colors from the face at point.
  17483. (fg
  17484. (let ((color (plist-get org-format-latex-options
  17485. :foreground)))
  17486. (if (and forbuffer (eq color 'auto))
  17487. (face-attribute face :foreground nil 'default)
  17488. color)))
  17489. (bg
  17490. (let ((color (plist-get org-format-latex-options
  17491. :background)))
  17492. (if (and forbuffer (eq color 'auto))
  17493. (face-attribute face :background nil 'default)
  17494. color)))
  17495. (hash (sha1 (prin1-to-string
  17496. (list org-format-latex-header
  17497. org-latex-default-packages-alist
  17498. org-latex-packages-alist
  17499. org-format-latex-options
  17500. forbuffer value fg bg))))
  17501. (absprefix (expand-file-name prefix dir))
  17502. (linkfile (format "%s_%s.png" prefix hash))
  17503. (movefile (format "%s_%s.png" absprefix hash))
  17504. (sep (and block-type "\n\n"))
  17505. (link (concat sep "[[file:" linkfile "]]" sep))
  17506. (options
  17507. (org-combine-plists
  17508. org-format-latex-options
  17509. `(:foreground ,fg :background ,bg))))
  17510. (when msg (message msg cnt))
  17511. (unless checkdir-flag ; Ensure the directory exists.
  17512. (setq checkdir-flag t)
  17513. (let ((todir (file-name-directory absprefix)))
  17514. (unless (file-directory-p todir)
  17515. (make-directory todir t))))
  17516. (unless (file-exists-p movefile)
  17517. (org-create-formula-image
  17518. value movefile options forbuffer processing-type))
  17519. (if overlays
  17520. (progn
  17521. (dolist (o (overlays-in beg end))
  17522. (when (eq (overlay-get o 'org-overlay-type)
  17523. 'org-latex-overlay)
  17524. (delete-overlay o)))
  17525. (let ((ov (make-overlay beg end)))
  17526. (overlay-put ov
  17527. 'org-overlay-type
  17528. 'org-latex-overlay)
  17529. (overlay-put ov 'evaporate t)
  17530. (if (featurep 'xemacs)
  17531. (progn
  17532. (overlay-put ov 'invisible t)
  17533. (overlay-put
  17534. ov 'end-glyph
  17535. (make-glyph
  17536. (vector 'png :file movefile))))
  17537. (overlay-put
  17538. ov 'display
  17539. (list 'image
  17540. :type 'png
  17541. :file movefile
  17542. :ascent 'center)))
  17543. (push ov org-latex-fragment-image-overlays))
  17544. (goto-char end))
  17545. (delete-region beg end)
  17546. (insert
  17547. (org-add-props link
  17548. (list 'org-latex-src
  17549. (replace-regexp-in-string "\"" "" value)
  17550. 'org-latex-src-embed-type
  17551. (if block-type 'paragraph 'character)))))))
  17552. (mathml
  17553. ;; Process to MathML.
  17554. (unless (org-format-latex-mathml-available-p)
  17555. (user-error "LaTeX to MathML converter not configured"))
  17556. (incf cnt)
  17557. (when msg (message msg cnt))
  17558. (goto-char beg)
  17559. (delete-region beg end)
  17560. (insert (org-format-latex-as-mathml
  17561. value block-type prefix dir)))
  17562. (otherwise
  17563. (error "Unknown conversion type %s for LaTeX fragments"
  17564. processing-type)))))))))))
  17565. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17566. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17567. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17568. sucessful, return the portion between \"<math...> </math>\"
  17569. elements otherwise return nil. When MATHML-FILE is specified,
  17570. write the results in to that file. When invoked as an
  17571. interactive command, prompt for LATEX-FRAG, with initial value
  17572. set to the current active region and echo the results for user
  17573. inspection."
  17574. (interactive (list (let ((frag (when (org-region-active-p)
  17575. (buffer-substring-no-properties
  17576. (region-beginning) (region-end)))))
  17577. (read-string "LaTeX Fragment: " frag nil frag))))
  17578. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17579. (let* ((tmp-in-file (file-relative-name
  17580. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17581. (ignore (write-region latex-frag nil tmp-in-file))
  17582. (tmp-out-file (file-relative-name
  17583. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17584. (cmd (format-spec
  17585. org-latex-to-mathml-convert-command
  17586. `((?j . ,(and org-latex-to-mathml-jar-file
  17587. (shell-quote-argument
  17588. (expand-file-name
  17589. org-latex-to-mathml-jar-file))))
  17590. (?I . ,(shell-quote-argument tmp-in-file))
  17591. (?i . ,latex-frag)
  17592. (?o . ,(shell-quote-argument tmp-out-file)))))
  17593. mathml shell-command-output)
  17594. (when (org-called-interactively-p 'any)
  17595. (unless (org-format-latex-mathml-available-p)
  17596. (user-error "LaTeX to MathML converter not configured")))
  17597. (message "Running %s" cmd)
  17598. (setq shell-command-output (shell-command-to-string cmd))
  17599. (setq mathml
  17600. (when (file-readable-p tmp-out-file)
  17601. (with-current-buffer (find-file-noselect tmp-out-file t)
  17602. (goto-char (point-min))
  17603. (when (re-search-forward
  17604. (concat
  17605. (regexp-quote
  17606. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17607. "[^>]*?>"
  17608. "\\(.\\|\n\\)*"
  17609. "</math>")
  17610. nil t)
  17611. (prog1 (match-string 0) (kill-buffer))))))
  17612. (cond
  17613. (mathml
  17614. (setq mathml
  17615. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17616. (when mathml-file
  17617. (write-region mathml nil mathml-file))
  17618. (when (org-called-interactively-p 'any)
  17619. (message mathml)))
  17620. ((message "LaTeX to MathML conversion failed")
  17621. (message shell-command-output)))
  17622. (delete-file tmp-in-file)
  17623. (when (file-exists-p tmp-out-file)
  17624. (delete-file tmp-out-file))
  17625. mathml))
  17626. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17627. prefix &optional dir)
  17628. "Use `org-create-math-formula' but check local cache first."
  17629. (let* ((absprefix (expand-file-name prefix dir))
  17630. (print-length nil) (print-level nil)
  17631. (formula-id (concat
  17632. "formula-"
  17633. (sha1
  17634. (prin1-to-string
  17635. (list latex-frag
  17636. org-latex-to-mathml-convert-command)))))
  17637. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17638. (formula-cache-dir (file-name-directory formula-cache)))
  17639. (unless (file-directory-p formula-cache-dir)
  17640. (make-directory formula-cache-dir t))
  17641. (unless (file-exists-p formula-cache)
  17642. (org-create-math-formula latex-frag formula-cache))
  17643. (if (file-exists-p formula-cache)
  17644. ;; Successful conversion. Return the link to MathML file.
  17645. (org-add-props
  17646. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17647. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17648. 'org-latex-src-embed-type (if latex-frag-type
  17649. 'paragraph 'character)))
  17650. ;; Failed conversion. Return the LaTeX fragment verbatim
  17651. latex-frag)))
  17652. (defun org-create-formula-image (string tofile options buffer &optional type)
  17653. "Create an image from LaTeX source using dvipng or convert.
  17654. This function calls either `org-create-formula-image-with-dvipng'
  17655. or `org-create-formula-image-with-imagemagick' depending on the
  17656. value of `org-latex-create-formula-image-program' or on the value
  17657. of the optional TYPE variable.
  17658. Note: ultimately these two function should be combined as they
  17659. share a good deal of logic."
  17660. (org-check-external-command
  17661. "latex" "needed to convert LaTeX fragments to images")
  17662. (funcall
  17663. (case (or type org-latex-create-formula-image-program)
  17664. (dvipng
  17665. (org-check-external-command
  17666. "dvipng" "needed to convert LaTeX fragments to images")
  17667. #'org-create-formula-image-with-dvipng)
  17668. (imagemagick
  17669. (org-check-external-command
  17670. "convert" "you need to install imagemagick")
  17671. #'org-create-formula-image-with-imagemagick)
  17672. (t (error
  17673. "Invalid value of `org-latex-create-formula-image-program'")))
  17674. string tofile options buffer))
  17675. (declare-function org-export-get-backend "ox" (name))
  17676. (declare-function org-export--get-global-options "ox" (&optional backend))
  17677. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17678. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17679. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17680. (defun org-create-formula--latex-header ()
  17681. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17682. (let ((info (org-combine-plists (org-export--get-global-options
  17683. (org-export-get-backend 'latex))
  17684. (org-export--get-inbuffer-options
  17685. (org-export-get-backend 'latex)))))
  17686. (org-latex-guess-babel-language
  17687. (org-latex-guess-inputenc
  17688. (org-splice-latex-header
  17689. org-format-latex-header
  17690. org-latex-default-packages-alist
  17691. org-latex-packages-alist t
  17692. (plist-get info :latex-header)))
  17693. info)))
  17694. (defun org--get-display-dpi ()
  17695. "Get the DPI of the display.
  17696. Assumes that the display has the same pixel width in the
  17697. horizontal and vertical directions."
  17698. (if (display-graphic-p)
  17699. (round (/ (display-pixel-height)
  17700. (/ (display-mm-height) 25.4)))
  17701. (error "Attempt to calculate the dpi of a non-graphic display")))
  17702. ;; This function borrows from Ganesh Swami's latex2png.el
  17703. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17704. "This calls dvipng."
  17705. (require 'ox-latex)
  17706. (let* ((tmpdir (if (featurep 'xemacs)
  17707. (temp-directory)
  17708. temporary-file-directory))
  17709. (texfilebase (make-temp-name
  17710. (expand-file-name "orgtex" tmpdir)))
  17711. (texfile (concat texfilebase ".tex"))
  17712. (dvifile (concat texfilebase ".dvi"))
  17713. (pngfile (concat texfilebase ".png"))
  17714. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17715. ;; This assumes that the display has the same pixel width in
  17716. ;; the horizontal and vertical directions
  17717. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17718. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17719. "Black"))
  17720. (bg (or (plist-get options (if buffer :background :html-background))
  17721. "Transparent")))
  17722. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17723. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17724. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17725. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17726. (let ((latex-header (org-create-formula--latex-header)))
  17727. (with-temp-file texfile
  17728. (insert latex-header)
  17729. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17730. (let ((dir default-directory))
  17731. (ignore-errors
  17732. (cd tmpdir)
  17733. (call-process "latex" nil nil nil texfile))
  17734. (cd dir))
  17735. (if (not (file-exists-p dvifile))
  17736. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17737. (ignore-errors
  17738. (if (featurep 'xemacs)
  17739. (call-process "dvipng" nil nil nil
  17740. "-fg" fg "-bg" bg
  17741. "-T" "tight"
  17742. "-o" pngfile
  17743. dvifile)
  17744. (call-process "dvipng" nil nil nil
  17745. "-fg" fg "-bg" bg
  17746. "-D" dpi
  17747. ;;"-x" scale "-y" scale
  17748. "-T" "tight"
  17749. "-o" pngfile
  17750. dvifile)))
  17751. (if (not (file-exists-p pngfile))
  17752. (if org-format-latex-signal-error
  17753. (error "Failed to create png file from %s" texfile)
  17754. (message "Failed to create png file from %s" texfile)
  17755. nil)
  17756. ;; Use the requested file name and clean up
  17757. (copy-file pngfile tofile 'replace)
  17758. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17759. (if (file-exists-p (concat texfilebase e))
  17760. (delete-file (concat texfilebase e))))
  17761. pngfile))))
  17762. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17763. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17764. "This calls convert, which is included into imagemagick."
  17765. (require 'ox-latex)
  17766. (let* ((tmpdir (if (featurep 'xemacs)
  17767. (temp-directory)
  17768. temporary-file-directory))
  17769. (texfilebase (make-temp-name
  17770. (expand-file-name "orgtex" tmpdir)))
  17771. (texfile (concat texfilebase ".tex"))
  17772. (pdffile (concat texfilebase ".pdf"))
  17773. (pngfile (concat texfilebase ".png"))
  17774. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17775. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17776. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17777. "black"))
  17778. (bg (or (plist-get options (if buffer :background :html-background))
  17779. "white")))
  17780. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17781. (setq fg (org-latex-color-format fg)))
  17782. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17783. (setq bg (org-latex-color-format
  17784. (if (string= bg "Transparent") "white" bg))))
  17785. (let ((latex-header (org-create-formula--latex-header)))
  17786. (with-temp-file texfile
  17787. (insert latex-header)
  17788. (insert "\n\\begin{document}\n"
  17789. "\\definecolor{fg}{rgb}{" fg "}\n"
  17790. "\\definecolor{bg}{rgb}{" bg "}\n"
  17791. "\n\\pagecolor{bg}\n"
  17792. "\n{\\color{fg}\n"
  17793. string
  17794. "\n}\n"
  17795. "\n\\end{document}\n")))
  17796. (org-latex-compile texfile t)
  17797. (if (not (file-exists-p pdffile))
  17798. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17799. (ignore-errors
  17800. (if (featurep 'xemacs)
  17801. (call-process "convert" nil nil nil
  17802. "-density" "96"
  17803. "-trim"
  17804. "-antialias"
  17805. pdffile
  17806. "-quality" "100"
  17807. ;; "-sharpen" "0x1.0"
  17808. pngfile)
  17809. (call-process "convert" nil nil nil
  17810. "-density" dpi
  17811. "-trim"
  17812. "-antialias"
  17813. pdffile
  17814. "-quality" "100"
  17815. ;; "-sharpen" "0x1.0"
  17816. pngfile)))
  17817. (if (not (file-exists-p pngfile))
  17818. (if org-format-latex-signal-error
  17819. (error "Failed to create png file from %s" texfile)
  17820. (message "Failed to create png file from %s" texfile)
  17821. nil)
  17822. ;; Use the requested file name and clean up
  17823. (copy-file pngfile tofile 'replace)
  17824. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17825. (if (file-exists-p (concat texfilebase e))
  17826. (delete-file (concat texfilebase e))))
  17827. pngfile))))
  17828. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17829. "Fill a LaTeX header template TPL.
  17830. In the template, the following place holders will be recognized:
  17831. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17832. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17833. [PACKAGES] \\usepackage statements for PKG
  17834. [NO-PACKAGES] do not include PKG
  17835. [EXTRA] the string EXTRA
  17836. [NO-EXTRA] do not include EXTRA
  17837. For backward compatibility, if both the positive and the negative place
  17838. holder is missing, the positive one (without the \"NO-\") will be
  17839. assumed to be present at the end of the template.
  17840. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17841. EXTRA is a string.
  17842. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17843. (let (rpl (end ""))
  17844. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17845. (setq rpl (if (or (match-end 1) (not def-pkg))
  17846. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17847. tpl (replace-match rpl t t tpl))
  17848. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17849. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17850. (setq rpl (if (or (match-end 1) (not pkg))
  17851. "" (org-latex-packages-to-string pkg snippets-p t))
  17852. tpl (replace-match rpl t t tpl))
  17853. (if pkg (setq end
  17854. (concat end "\n"
  17855. (org-latex-packages-to-string pkg snippets-p)))))
  17856. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17857. (setq rpl (if (or (match-end 1) (not extra))
  17858. "" (concat extra "\n"))
  17859. tpl (replace-match rpl t t tpl))
  17860. (if (and extra (string-match "\\S-" extra))
  17861. (setq end (concat end "\n" extra))))
  17862. (if (string-match "\\S-" end)
  17863. (concat tpl "\n" end)
  17864. tpl)))
  17865. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17866. "Turn an alist of packages into a string with the \\usepackage macros."
  17867. (setq pkg (mapconcat (lambda(p)
  17868. (cond
  17869. ((stringp p) p)
  17870. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17871. (format "%% Package %s omitted" (cadr p)))
  17872. ((equal "" (car p))
  17873. (format "\\usepackage{%s}" (cadr p)))
  17874. (t
  17875. (format "\\usepackage[%s]{%s}"
  17876. (car p) (cadr p)))))
  17877. pkg
  17878. "\n"))
  17879. (if newline (concat pkg "\n") pkg))
  17880. (defun org-dvipng-color (attr)
  17881. "Return a RGB color specification for dvipng."
  17882. (apply 'format "rgb %s %s %s"
  17883. (mapcar 'org-normalize-color
  17884. (if (featurep 'xemacs)
  17885. (color-rgb-components
  17886. (face-property 'default
  17887. (cond ((eq attr :foreground) 'foreground)
  17888. ((eq attr :background) 'background))))
  17889. (color-values (face-attribute 'default attr nil))))))
  17890. (defun org-dvipng-color-format (color-name)
  17891. "Convert COLOR-NAME to a RGB color value for dvipng."
  17892. (apply 'format "rgb %s %s %s"
  17893. (mapcar 'org-normalize-color
  17894. (color-values color-name))))
  17895. (defun org-latex-color (attr)
  17896. "Return a RGB color for the LaTeX color package."
  17897. (apply 'format "%s,%s,%s"
  17898. (mapcar 'org-normalize-color
  17899. (if (featurep 'xemacs)
  17900. (color-rgb-components
  17901. (face-property 'default
  17902. (cond ((eq attr :foreground) 'foreground)
  17903. ((eq attr :background) 'background))))
  17904. (color-values (face-attribute 'default attr nil))))))
  17905. (defun org-latex-color-format (color-name)
  17906. "Convert COLOR-NAME to a RGB color value."
  17907. (apply 'format "%s,%s,%s"
  17908. (mapcar 'org-normalize-color
  17909. (color-values color-name))))
  17910. (defun org-normalize-color (value)
  17911. "Return string to be used as color value for an RGB component."
  17912. (format "%g" (/ value 65535.0)))
  17913. ;; Image display
  17914. (defvar org-inline-image-overlays nil)
  17915. (make-variable-buffer-local 'org-inline-image-overlays)
  17916. (defun org-toggle-inline-images (&optional include-linked)
  17917. "Toggle the display of inline images.
  17918. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17919. (interactive "P")
  17920. (if org-inline-image-overlays
  17921. (progn
  17922. (org-remove-inline-images)
  17923. (when (org-called-interactively-p 'interactive)
  17924. (message "Inline image display turned off")))
  17925. (org-display-inline-images include-linked)
  17926. (when (org-called-interactively-p 'interactive)
  17927. (message (if org-inline-image-overlays
  17928. (format "%d images displayed inline"
  17929. (length org-inline-image-overlays))
  17930. "No images to display inline")))))
  17931. (defun org-redisplay-inline-images ()
  17932. "Refresh the display of inline images."
  17933. (interactive)
  17934. (if (not org-inline-image-overlays)
  17935. (org-toggle-inline-images)
  17936. (org-toggle-inline-images)
  17937. (org-toggle-inline-images)))
  17938. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17939. "Display inline images.
  17940. An inline image is a link which follows either of these
  17941. conventions:
  17942. 1. Its path is a file with an extension matching return value
  17943. from `image-file-name-regexp' and it has no contents.
  17944. 2. Its description consists in a single link of the previous
  17945. type.
  17946. When optional argument INCLUDE-LINKED is non-nil, also links with
  17947. a text description part will be inlined. This can be nice for
  17948. a quick look at those images, but it does not reflect what
  17949. exported files will look like.
  17950. When optional argument REFRESH is non-nil, refresh existing
  17951. images between BEG and END. This will create new image displays
  17952. only if necessary. BEG and END default to the buffer
  17953. boundaries."
  17954. (interactive "P")
  17955. (when (display-graphic-p)
  17956. (unless refresh
  17957. (org-remove-inline-images)
  17958. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17959. (org-with-wide-buffer
  17960. (goto-char (or beg (point-min)))
  17961. (let ((case-fold-search t)
  17962. (file-extension-re (org-image-file-name-regexp)))
  17963. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17964. (let ((link (save-match-data (org-element-context))))
  17965. ;; Check if we're at an inline image.
  17966. (when (and (equal (org-element-property :type link) "file")
  17967. (or include-linked
  17968. (not (org-element-property :contents-begin link)))
  17969. (let ((parent (org-element-property :parent link)))
  17970. (or (not (eq (org-element-type parent) 'link))
  17971. (not (cdr (org-element-contents parent)))))
  17972. (org-string-match-p file-extension-re
  17973. (org-element-property :path link)))
  17974. (let ((file (expand-file-name
  17975. (org-link-unescape
  17976. (org-element-property :path link)))))
  17977. (when (file-exists-p file)
  17978. (let ((width
  17979. ;; Apply `org-image-actual-width' specifications.
  17980. (cond
  17981. ((not (image-type-available-p 'imagemagick)) nil)
  17982. ((eq org-image-actual-width t) nil)
  17983. ((listp org-image-actual-width)
  17984. (or
  17985. ;; First try to find a width among
  17986. ;; attributes associated to the paragraph
  17987. ;; containing link.
  17988. (let ((paragraph
  17989. (let ((e link))
  17990. (while (and (setq e (org-element-property
  17991. :parent e))
  17992. (not (eq (org-element-type e)
  17993. 'paragraph))))
  17994. e)))
  17995. (when paragraph
  17996. (save-excursion
  17997. (goto-char (org-element-property :begin paragraph))
  17998. (when
  17999. (re-search-forward
  18000. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18001. (org-element-property
  18002. :post-affiliated paragraph)
  18003. t)
  18004. (string-to-number (match-string 1))))))
  18005. ;; Otherwise, fall-back to provided number.
  18006. (car org-image-actual-width)))
  18007. ((numberp org-image-actual-width)
  18008. org-image-actual-width)))
  18009. (old (get-char-property-and-overlay
  18010. (org-element-property :begin link)
  18011. 'org-image-overlay)))
  18012. (if (and (car-safe old) refresh)
  18013. (image-refresh (overlay-get (cdr old) 'display))
  18014. (let ((image (create-image file
  18015. (and width 'imagemagick)
  18016. nil
  18017. :width width)))
  18018. (when image
  18019. (let* ((link
  18020. ;; If inline image is the description
  18021. ;; of another link, be sure to
  18022. ;; consider the latter as the one to
  18023. ;; apply the overlay on.
  18024. (let ((parent
  18025. (org-element-property :parent link)))
  18026. (if (eq (org-element-type parent) 'link)
  18027. parent
  18028. link)))
  18029. (ov (make-overlay
  18030. (org-element-property :begin link)
  18031. (progn
  18032. (goto-char
  18033. (org-element-property :end link))
  18034. (skip-chars-backward " \t")
  18035. (point)))))
  18036. (overlay-put ov 'display image)
  18037. (overlay-put ov 'face 'default)
  18038. (overlay-put ov 'org-image-overlay t)
  18039. (overlay-put
  18040. ov 'modification-hooks
  18041. (list 'org-display-inline-remove-overlay))
  18042. (push ov org-inline-image-overlays)))))))))))))))
  18043. (define-obsolete-function-alias
  18044. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18045. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18046. "Remove inline-display overlay if a corresponding region is modified."
  18047. (let ((inhibit-modification-hooks t))
  18048. (when (and ov after)
  18049. (delete ov org-inline-image-overlays)
  18050. (delete-overlay ov))))
  18051. (defun org-remove-inline-images ()
  18052. "Remove inline display of images."
  18053. (interactive)
  18054. (mapc 'delete-overlay org-inline-image-overlays)
  18055. (setq org-inline-image-overlays nil))
  18056. ;;;; Key bindings
  18057. ;; Outline functions from `outline-mode-prefix-map'
  18058. ;; that can be remapped in Org:
  18059. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18060. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18061. (define-key org-mode-map [remap outline-forward-same-level]
  18062. 'org-forward-heading-same-level)
  18063. (define-key org-mode-map [remap outline-backward-same-level]
  18064. 'org-backward-heading-same-level)
  18065. (define-key org-mode-map [remap show-branches]
  18066. 'org-kill-note-or-show-branches)
  18067. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18068. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18069. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18070. (define-key org-mode-map [remap outline-next-visible-heading]
  18071. 'org-next-visible-heading)
  18072. (define-key org-mode-map [remap outline-previous-visible-heading]
  18073. 'org-previous-visible-heading)
  18074. ;; Outline functions from `outline-mode-prefix-map' that can not
  18075. ;; be remapped in Org:
  18076. ;; - the column "key binding" shows whether the Outline function is still
  18077. ;; available in Org mode on the same key that it has been bound to in
  18078. ;; Outline mode:
  18079. ;; - "overridden": key used for a different functionality in Org mode
  18080. ;; - else: key still bound to the same Outline function in Org mode
  18081. ;; | Outline function | key binding | Org replacement |
  18082. ;; |------------------------------------+-------------+--------------------------|
  18083. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18084. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18085. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18086. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18087. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18088. ;; | `show-entry' | overridden | no replacement |
  18089. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18090. ;; | `show-branches' | `C-c C-k' | still same function |
  18091. ;; | `show-subtree' | overridden | visibility cycling |
  18092. ;; | `show-all' | overridden | no replacement |
  18093. ;; | `hide-subtree' | overridden | visibility cycling |
  18094. ;; | `hide-body' | overridden | no replacement |
  18095. ;; | `hide-entry' | overridden | visibility cycling |
  18096. ;; | `hide-leaves' | overridden | no replacement |
  18097. ;; | `hide-sublevels' | overridden | no replacement |
  18098. ;; | `hide-other' | overridden | no replacement |
  18099. ;; Make `C-c C-x' a prefix key
  18100. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18101. ;; TAB key with modifiers
  18102. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18103. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18104. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18105. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18106. ;; The following line is necessary under Suse GNU/Linux
  18107. (unless (featurep 'xemacs)
  18108. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18109. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18110. (define-key org-mode-map [backtab] 'org-shifttab)
  18111. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18112. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18113. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18114. ;; Cursor keys with modifiers
  18115. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18116. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18117. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18118. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18119. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18120. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18121. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18122. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18123. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18124. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18125. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18126. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18127. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18128. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18129. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18130. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18131. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18132. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18133. ;; Babel keys
  18134. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18135. (mapc (lambda (pair)
  18136. (define-key org-babel-map (car pair) (cdr pair)))
  18137. org-babel-key-bindings)
  18138. ;;; Extra keys for tty access.
  18139. ;; We only set them when really needed because otherwise the
  18140. ;; menus don't show the simple keys
  18141. (when (or org-use-extra-keys
  18142. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18143. (not window-system))
  18144. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18145. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18146. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18147. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18148. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18149. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18150. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18151. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18152. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18153. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18154. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18155. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18156. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18157. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18158. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18159. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18160. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18161. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18162. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18163. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18164. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18165. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18166. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18167. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18168. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18169. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18170. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18171. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18172. ;; All the other keys
  18173. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18174. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18175. (if (boundp 'narrow-map)
  18176. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18177. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18178. (if (boundp 'narrow-map)
  18179. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18180. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18181. (if (boundp 'narrow-map)
  18182. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18183. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18184. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18185. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18186. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18187. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18188. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18189. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18190. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18191. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18192. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18193. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18194. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18195. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18196. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18197. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18198. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18199. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18200. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18201. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18202. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18203. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18204. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18205. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18206. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18207. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18208. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18209. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18210. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18211. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18212. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18213. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18214. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18215. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18216. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18217. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18218. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18219. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18220. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18221. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18222. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18223. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18224. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18225. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18226. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18227. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18228. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18229. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18230. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18231. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18232. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18233. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18234. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18235. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18236. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18237. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18238. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18239. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18240. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18241. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18242. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18243. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18244. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18245. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18246. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18247. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18248. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18249. (org-defkey org-mode-map "\C-m" 'org-return)
  18250. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18251. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18252. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18253. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18254. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18255. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18256. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18257. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18258. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18259. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18260. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18261. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18262. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18263. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18264. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18265. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18266. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18267. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18268. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18269. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18270. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18271. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18272. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18273. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18274. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18276. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18291. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18293. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18294. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18295. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18296. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18297. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18298. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18299. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18300. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18301. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18302. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18303. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18304. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18305. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18306. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18307. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18308. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18309. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18310. (when (featurep 'xemacs)
  18311. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18312. (defconst org-speed-commands-default
  18313. '(
  18314. ("Outline Navigation")
  18315. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18316. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18317. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18318. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18319. ("F" . org-next-block)
  18320. ("B" . org-previous-block)
  18321. ("u" . (org-speed-move-safe 'outline-up-heading))
  18322. ("j" . org-goto)
  18323. ("g" . (org-refile t))
  18324. ("Outline Visibility")
  18325. ("c" . org-cycle)
  18326. ("C" . org-shifttab)
  18327. (" " . org-display-outline-path)
  18328. ("s" . org-narrow-to-subtree)
  18329. ("=" . org-columns)
  18330. ("Outline Structure Editing")
  18331. ("U" . org-metaup)
  18332. ("D" . org-metadown)
  18333. ("r" . org-metaright)
  18334. ("l" . org-metaleft)
  18335. ("R" . org-shiftmetaright)
  18336. ("L" . org-shiftmetaleft)
  18337. ("i" . (progn (forward-char 1) (call-interactively
  18338. 'org-insert-heading-respect-content)))
  18339. ("^" . org-sort)
  18340. ("w" . org-refile)
  18341. ("a" . org-archive-subtree-default-with-confirmation)
  18342. ("@" . org-mark-subtree)
  18343. ("#" . org-toggle-comment)
  18344. ("Clock Commands")
  18345. ("I" . org-clock-in)
  18346. ("O" . org-clock-out)
  18347. ("Meta Data Editing")
  18348. ("t" . org-todo)
  18349. ("," . (org-priority))
  18350. ("0" . (org-priority ?\ ))
  18351. ("1" . (org-priority ?A))
  18352. ("2" . (org-priority ?B))
  18353. ("3" . (org-priority ?C))
  18354. (":" . org-set-tags-command)
  18355. ("e" . org-set-effort)
  18356. ("E" . org-inc-effort)
  18357. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18358. (org-entry-put (point) "APPT_WARNTIME" m)))
  18359. ("Agenda Views etc")
  18360. ("v" . org-agenda)
  18361. ("/" . org-sparse-tree)
  18362. ("Misc")
  18363. ("o" . org-open-at-point)
  18364. ("?" . org-speed-command-help)
  18365. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18366. (">" . (org-agenda-remove-restriction-lock))
  18367. )
  18368. "The default speed commands.")
  18369. (defun org-print-speed-command (e)
  18370. (if (> (length (car e)) 1)
  18371. (progn
  18372. (princ "\n")
  18373. (princ (car e))
  18374. (princ "\n")
  18375. (princ (make-string (length (car e)) ?-))
  18376. (princ "\n"))
  18377. (princ (car e))
  18378. (princ " ")
  18379. (if (symbolp (cdr e))
  18380. (princ (symbol-name (cdr e)))
  18381. (prin1 (cdr e)))
  18382. (princ "\n")))
  18383. (defun org-speed-command-help ()
  18384. "Show the available speed commands."
  18385. (interactive)
  18386. (if (not org-use-speed-commands)
  18387. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18388. (with-output-to-temp-buffer "*Help*"
  18389. (princ "User-defined Speed commands\n===========================\n")
  18390. (mapc 'org-print-speed-command org-speed-commands-user)
  18391. (princ "\n")
  18392. (princ "Built-in Speed commands\n=======================\n")
  18393. (mapc 'org-print-speed-command org-speed-commands-default))
  18394. (with-current-buffer "*Help*"
  18395. (setq truncate-lines t))))
  18396. (defun org-speed-move-safe (cmd)
  18397. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18398. If not, return to the original position and throw an error."
  18399. (interactive)
  18400. (let ((pos (point)))
  18401. (call-interactively cmd)
  18402. (unless (and (bolp) (org-at-heading-p))
  18403. (goto-char pos)
  18404. (error "Boundary reached while executing %s" cmd))))
  18405. (defvar org-self-insert-command-undo-counter 0)
  18406. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18407. (defvar org-speed-command nil)
  18408. (define-obsolete-function-alias
  18409. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18410. (defun org-speed-command-activate (keys)
  18411. "Hook for activating single-letter speed commands.
  18412. `org-speed-commands-default' specifies a minimal command set.
  18413. Use `org-speed-commands-user' for further customization."
  18414. (when (or (and (bolp) (looking-at org-outline-regexp))
  18415. (and (functionp org-use-speed-commands)
  18416. (funcall org-use-speed-commands)))
  18417. (cdr (assoc keys (append org-speed-commands-user
  18418. org-speed-commands-default)))))
  18419. (define-obsolete-function-alias
  18420. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18421. (defun org-babel-speed-command-activate (keys)
  18422. "Hook for activating single-letter code block commands."
  18423. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18424. (cdr (assoc keys org-babel-key-bindings))))
  18425. (defcustom org-speed-command-hook
  18426. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18427. "Hook for activating speed commands at strategic locations.
  18428. Hook functions are called in sequence until a valid handler is
  18429. found.
  18430. Each hook takes a single argument, a user-pressed command key
  18431. which is also a `self-insert-command' from the global map.
  18432. Within the hook, examine the cursor position and the command key
  18433. and return nil or a valid handler as appropriate. Handler could
  18434. be one of an interactive command, a function, or a form.
  18435. Set `org-use-speed-commands' to non-nil value to enable this
  18436. hook. The default setting is `org-speed-command-activate'."
  18437. :group 'org-structure
  18438. :version "24.1"
  18439. :type 'hook)
  18440. (defun org-self-insert-command (N)
  18441. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18442. If the cursor is in a table looking at whitespace, the whitespace is
  18443. overwritten, and the table is not marked as requiring realignment."
  18444. (interactive "p")
  18445. (org-check-before-invisible-edit 'insert)
  18446. (cond
  18447. ((and org-use-speed-commands
  18448. (let ((kv (this-command-keys-vector)))
  18449. (setq org-speed-command
  18450. (run-hook-with-args-until-success
  18451. 'org-speed-command-hook
  18452. (make-string 1 (aref kv (1- (length kv))))))))
  18453. (cond
  18454. ((commandp org-speed-command)
  18455. (setq this-command org-speed-command)
  18456. (call-interactively org-speed-command))
  18457. ((functionp org-speed-command)
  18458. (funcall org-speed-command))
  18459. ((and org-speed-command (listp org-speed-command))
  18460. (eval org-speed-command))
  18461. (t (let (org-use-speed-commands)
  18462. (call-interactively 'org-self-insert-command)))))
  18463. ((and
  18464. (org-table-p)
  18465. (progn
  18466. ;; check if we blank the field, and if that triggers align
  18467. (and (featurep 'org-table) org-table-auto-blank-field
  18468. (memq last-command
  18469. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18470. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18471. ;; got extra space, this field does not determine column width
  18472. (let (org-table-may-need-update) (org-table-blank-field))
  18473. ;; no extra space, this field may determine column width
  18474. (org-table-blank-field)))
  18475. t)
  18476. (eq N 1)
  18477. (looking-at "[^|\n]* |"))
  18478. (let (org-table-may-need-update)
  18479. (goto-char (1- (match-end 0)))
  18480. (backward-delete-char 1)
  18481. (goto-char (match-beginning 0))
  18482. (self-insert-command N)))
  18483. (t
  18484. (setq org-table-may-need-update t)
  18485. (self-insert-command N)
  18486. (org-fix-tags-on-the-fly)
  18487. (if org-self-insert-cluster-for-undo
  18488. (if (not (eq last-command 'org-self-insert-command))
  18489. (setq org-self-insert-command-undo-counter 1)
  18490. (if (>= org-self-insert-command-undo-counter 20)
  18491. (setq org-self-insert-command-undo-counter 1)
  18492. (and (> org-self-insert-command-undo-counter 0)
  18493. buffer-undo-list (listp buffer-undo-list)
  18494. (not (cadr buffer-undo-list)) ; remove nil entry
  18495. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18496. (setq org-self-insert-command-undo-counter
  18497. (1+ org-self-insert-command-undo-counter))))))))
  18498. (defun org-check-before-invisible-edit (kind)
  18499. "Check is editing if kind KIND would be dangerous with invisible text around.
  18500. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18501. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18502. (if (and org-catch-invisible-edits
  18503. (or (not (boundp 'visible-mode)) (not visible-mode))
  18504. (or (get-char-property (point) 'invisible)
  18505. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18506. ;; OK, we need to take a closer look
  18507. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18508. (invisible-before-point (if (bobp) nil (get-char-property
  18509. (1- (point)) 'invisible)))
  18510. (border-and-ok-direction
  18511. (or
  18512. ;; Check if we are acting predictably before invisible text
  18513. (and invisible-at-point (not invisible-before-point)
  18514. (memq kind '(insert delete-backward)))
  18515. ;; Check if we are acting predictably after invisible text
  18516. ;; This works not well, and I have turned it off. It seems
  18517. ;; better to always show and stop after invisible text.
  18518. ;; (and (not invisible-at-point) invisible-before-point
  18519. ;; (memq kind '(insert delete)))
  18520. )))
  18521. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18522. (memq invisible-before-point '(outline org-hide-block t)))
  18523. (if (eq org-catch-invisible-edits 'error)
  18524. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18525. (if (and org-custom-properties-overlays
  18526. (y-or-n-p "Display invisible properties in this buffer? "))
  18527. (org-toggle-custom-properties-visibility)
  18528. ;; Make the area visible
  18529. (save-excursion
  18530. (if invisible-before-point
  18531. (goto-char (previous-single-char-property-change
  18532. (point) 'invisible)))
  18533. (show-subtree))
  18534. (cond
  18535. ((eq org-catch-invisible-edits 'show)
  18536. ;; That's it, we do the edit after showing
  18537. (message
  18538. "Unfolding invisible region around point before editing")
  18539. (sit-for 1))
  18540. ((and (eq org-catch-invisible-edits 'smart)
  18541. border-and-ok-direction)
  18542. (message "Unfolding invisible region around point before editing"))
  18543. (t
  18544. ;; Don't do the edit, make the user repeat it in full visibility
  18545. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18546. (defun org-fix-tags-on-the-fly ()
  18547. (when (and (equal (char-after (point-at-bol)) ?*)
  18548. (org-at-heading-p))
  18549. (org-align-tags-here org-tags-column)))
  18550. (defun org-delete-backward-char (N)
  18551. "Like `delete-backward-char', insert whitespace at field end in tables.
  18552. When deleting backwards, in tables this function will insert whitespace in
  18553. front of the next \"|\" separator, to keep the table aligned. The table will
  18554. still be marked for re-alignment if the field did fill the entire column,
  18555. because, in this case the deletion might narrow the column."
  18556. (interactive "p")
  18557. (save-match-data
  18558. (org-check-before-invisible-edit 'delete-backward)
  18559. (if (and (org-table-p)
  18560. (eq N 1)
  18561. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18562. (looking-at ".*?|"))
  18563. (let ((pos (point))
  18564. (noalign (looking-at "[^|\n\r]* |"))
  18565. (c org-table-may-need-update))
  18566. (backward-delete-char N)
  18567. (if (not overwrite-mode)
  18568. (progn
  18569. (skip-chars-forward "^|")
  18570. (insert " ")
  18571. (goto-char (1- pos))))
  18572. ;; noalign: if there were two spaces at the end, this field
  18573. ;; does not determine the width of the column.
  18574. (if noalign (setq org-table-may-need-update c)))
  18575. (backward-delete-char N)
  18576. (org-fix-tags-on-the-fly))))
  18577. (defun org-delete-char (N)
  18578. "Like `delete-char', but insert whitespace at field end in tables.
  18579. When deleting characters, in tables this function will insert whitespace in
  18580. front of the next \"|\" separator, to keep the table aligned. The table will
  18581. still be marked for re-alignment if the field did fill the entire column,
  18582. because, in this case the deletion might narrow the column."
  18583. (interactive "p")
  18584. (save-match-data
  18585. (org-check-before-invisible-edit 'delete)
  18586. (if (and (org-table-p)
  18587. (not (bolp))
  18588. (not (= (char-after) ?|))
  18589. (eq N 1))
  18590. (if (looking-at ".*?|")
  18591. (let ((pos (point))
  18592. (noalign (looking-at "[^|\n\r]* |"))
  18593. (c org-table-may-need-update))
  18594. (replace-match
  18595. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18596. (goto-char pos)
  18597. ;; noalign: if there were two spaces at the end, this field
  18598. ;; does not determine the width of the column.
  18599. (if noalign (setq org-table-may-need-update c)))
  18600. (delete-char N))
  18601. (delete-char N)
  18602. (org-fix-tags-on-the-fly))))
  18603. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18604. (put 'org-self-insert-command 'delete-selection
  18605. (lambda ()
  18606. (not (run-hook-with-args-until-success
  18607. 'self-insert-uses-region-functions))))
  18608. (put 'orgtbl-self-insert-command 'delete-selection
  18609. (lambda ()
  18610. (not (run-hook-with-args-until-success
  18611. 'self-insert-uses-region-functions))))
  18612. (put 'org-delete-char 'delete-selection 'supersede)
  18613. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18614. (put 'org-yank 'delete-selection 'yank)
  18615. ;; Make `flyspell-mode' delay after some commands
  18616. (put 'org-self-insert-command 'flyspell-delayed t)
  18617. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18618. (put 'org-delete-char 'flyspell-delayed t)
  18619. (put 'org-delete-backward-char 'flyspell-delayed t)
  18620. ;; Make pabbrev-mode expand after org-mode commands
  18621. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18622. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18623. (defun org-remap (map &rest commands)
  18624. "In MAP, remap the functions given in COMMANDS.
  18625. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18626. (let (new old)
  18627. (while commands
  18628. (setq old (pop commands) new (pop commands))
  18629. (if (fboundp 'command-remapping)
  18630. (org-defkey map (vector 'remap old) new)
  18631. (substitute-key-definition old new map global-map)))))
  18632. (defun org-transpose-words ()
  18633. "Transpose words for Org.
  18634. This uses the `org-mode-transpose-word-syntax-table' syntax
  18635. table, which interprets characters in `org-emphasis-alist' as
  18636. word constituents."
  18637. (interactive)
  18638. (with-syntax-table org-mode-transpose-word-syntax-table
  18639. (call-interactively 'transpose-words)))
  18640. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18641. (when (eq org-enable-table-editor 'optimized)
  18642. ;; If the user wants maximum table support, we need to hijack
  18643. ;; some standard editing functions
  18644. (org-remap org-mode-map
  18645. 'self-insert-command 'org-self-insert-command
  18646. 'delete-char 'org-delete-char
  18647. 'delete-backward-char 'org-delete-backward-char)
  18648. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18649. (defvar org-ctrl-c-ctrl-c-hook nil
  18650. "Hook for functions attaching themselves to `C-c C-c'.
  18651. This can be used to add additional functionality to the C-c C-c
  18652. key which executes context-dependent commands. This hook is run
  18653. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18654. run after the last test.
  18655. Each function will be called with no arguments. The function
  18656. must check if the context is appropriate for it to act. If yes,
  18657. it should do its thing and then return a non-nil value. If the
  18658. context is wrong, just do nothing and return nil.")
  18659. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18660. "Hook for functions attaching themselves to `C-c C-c'.
  18661. This can be used to add additional functionality to the C-c C-c
  18662. key which executes context-dependent commands. This hook is run
  18663. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18664. before the first test.
  18665. Each function will be called with no arguments. The function
  18666. must check if the context is appropriate for it to act. If yes,
  18667. it should do its thing and then return a non-nil value. If the
  18668. context is wrong, just do nothing and return nil.")
  18669. (defvar org-tab-first-hook nil
  18670. "Hook for functions to attach themselves to TAB.
  18671. See `org-ctrl-c-ctrl-c-hook' for more information.
  18672. This hook runs as the first action when TAB is pressed, even before
  18673. `org-cycle' messes around with the `outline-regexp' to cater for
  18674. inline tasks and plain list item folding.
  18675. If any function in this hook returns t, any other actions that
  18676. would have been caused by TAB (such as table field motion or visibility
  18677. cycling) will not occur.")
  18678. (defvar org-tab-after-check-for-table-hook nil
  18679. "Hook for functions to attach themselves to TAB.
  18680. See `org-ctrl-c-ctrl-c-hook' for more information.
  18681. This hook runs after it has been established that the cursor is not in a
  18682. table, but before checking if the cursor is in a headline or if global cycling
  18683. should be done.
  18684. If any function in this hook returns t, not other actions like visibility
  18685. cycling will be done.")
  18686. (defvar org-tab-after-check-for-cycling-hook nil
  18687. "Hook for functions to attach themselves to TAB.
  18688. See `org-ctrl-c-ctrl-c-hook' for more information.
  18689. This hook runs after it has been established that not table field motion and
  18690. not visibility should be done because of current context. This is probably
  18691. the place where a package like yasnippets can hook in.")
  18692. (defvar org-tab-before-tab-emulation-hook nil
  18693. "Hook for functions to attach themselves to TAB.
  18694. See `org-ctrl-c-ctrl-c-hook' for more information.
  18695. This hook runs after every other options for TAB have been exhausted, but
  18696. before indentation and \t insertion takes place.")
  18697. (defvar org-metaleft-hook nil
  18698. "Hook for functions attaching themselves to `M-left'.
  18699. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18700. (defvar org-metaright-hook nil
  18701. "Hook for functions attaching themselves to `M-right'.
  18702. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18703. (defvar org-metaup-hook nil
  18704. "Hook for functions attaching themselves to `M-up'.
  18705. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18706. (defvar org-metadown-hook nil
  18707. "Hook for functions attaching themselves to `M-down'.
  18708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18709. (defvar org-shiftmetaleft-hook nil
  18710. "Hook for functions attaching themselves to `M-S-left'.
  18711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18712. (defvar org-shiftmetaright-hook nil
  18713. "Hook for functions attaching themselves to `M-S-right'.
  18714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18715. (defvar org-shiftmetaup-hook nil
  18716. "Hook for functions attaching themselves to `M-S-up'.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-shiftmetadown-hook nil
  18719. "Hook for functions attaching themselves to `M-S-down'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-metareturn-hook nil
  18722. "Hook for functions attaching themselves to `M-RET'.
  18723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18724. (defvar org-shiftup-hook nil
  18725. "Hook for functions attaching themselves to `S-up'.
  18726. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18727. (defvar org-shiftup-final-hook nil
  18728. "Hook for functions attaching themselves to `S-up'.
  18729. This one runs after all other options except shift-select have been excluded.
  18730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18731. (defvar org-shiftdown-hook nil
  18732. "Hook for functions attaching themselves to `S-down'.
  18733. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18734. (defvar org-shiftdown-final-hook nil
  18735. "Hook for functions attaching themselves to `S-down'.
  18736. This one runs after all other options except shift-select have been excluded.
  18737. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18738. (defvar org-shiftleft-hook nil
  18739. "Hook for functions attaching themselves to `S-left'.
  18740. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18741. (defvar org-shiftleft-final-hook nil
  18742. "Hook for functions attaching themselves to `S-left'.
  18743. This one runs after all other options except shift-select have been excluded.
  18744. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18745. (defvar org-shiftright-hook nil
  18746. "Hook for functions attaching themselves to `S-right'.
  18747. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18748. (defvar org-shiftright-final-hook nil
  18749. "Hook for functions attaching themselves to `S-right'.
  18750. This one runs after all other options except shift-select have been excluded.
  18751. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18752. (defun org-modifier-cursor-error ()
  18753. "Throw an error, a modified cursor command was applied in wrong context."
  18754. (user-error "This command is active in special context like tables, headlines or items"))
  18755. (defun org-shiftselect-error ()
  18756. "Throw an error because Shift-Cursor command was applied in wrong context."
  18757. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18758. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18759. (user-error "This command works only in special context like headlines or timestamps")))
  18760. (defun org-call-for-shift-select (cmd)
  18761. (let ((this-command-keys-shift-translated t))
  18762. (call-interactively cmd)))
  18763. (defun org-shifttab (&optional arg)
  18764. "Global visibility cycling or move to previous table field.
  18765. Call `org-table-previous-field' within a table.
  18766. When ARG is nil, cycle globally through visibility states.
  18767. When ARG is a numeric prefix, show contents of this level."
  18768. (interactive "P")
  18769. (cond
  18770. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18771. ((integerp arg)
  18772. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18773. (message "Content view to level: %d" arg)
  18774. (org-content (prefix-numeric-value arg2))
  18775. (org-cycle-show-empty-lines t)
  18776. (setq org-cycle-global-status 'overview)))
  18777. (t (call-interactively 'org-global-cycle))))
  18778. (defun org-shiftmetaleft ()
  18779. "Promote subtree or delete table column.
  18780. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18781. `org-table-delete-column', depending on context. See the
  18782. individual commands for more information."
  18783. (interactive)
  18784. (cond
  18785. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18786. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18787. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18788. ((if (not (org-region-active-p)) (org-at-item-p)
  18789. (save-excursion (goto-char (region-beginning))
  18790. (org-at-item-p)))
  18791. (call-interactively 'org-outdent-item-tree))
  18792. (t (org-modifier-cursor-error))))
  18793. (defun org-shiftmetaright ()
  18794. "Demote subtree or insert table column.
  18795. Calls `org-demote-subtree', `org-indent-item-tree', or
  18796. `org-table-insert-column', depending on context. See the
  18797. individual commands for more information."
  18798. (interactive)
  18799. (cond
  18800. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18801. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18802. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18803. ((if (not (org-region-active-p)) (org-at-item-p)
  18804. (save-excursion (goto-char (region-beginning))
  18805. (org-at-item-p)))
  18806. (call-interactively 'org-indent-item-tree))
  18807. (t (org-modifier-cursor-error))))
  18808. (defun org-shiftmetaup (&optional arg)
  18809. "Drag the line at point up.
  18810. In a table, kill the current row.
  18811. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18812. but also adjust the previous clocked item in the clock history.
  18813. Everywhere else, drag the line at point up."
  18814. (interactive "P")
  18815. (cond
  18816. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18817. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18818. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18819. (call-interactively 'org-timestamp-up)))
  18820. (t (call-interactively 'org-drag-line-backward))))
  18821. (defun org-shiftmetadown (&optional arg)
  18822. "Drag the line at point down.
  18823. In a table, insert an empty row at the current line.
  18824. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18825. but also adjust the previous clocked item in the clock history.
  18826. Everywhere else, drag the line at point down."
  18827. (interactive "P")
  18828. (cond
  18829. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18830. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18831. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18832. (call-interactively 'org-timestamp-down)))
  18833. (t (call-interactively 'org-drag-line-forward))))
  18834. (defsubst org-hidden-tree-error ()
  18835. (user-error
  18836. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18837. (defun org-metaleft (&optional arg)
  18838. "Promote heading, list item at point or move table column left.
  18839. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18840. depending on context. With no specific context, calls the Emacs
  18841. default `backward-word'. See the individual commands for more
  18842. information.
  18843. This function runs the hook `org-metaleft-hook' as a first step,
  18844. and returns at first non-nil value."
  18845. (interactive "P")
  18846. (cond
  18847. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18848. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18849. ((org-with-limited-levels
  18850. (or (org-at-heading-p)
  18851. (and (org-region-active-p)
  18852. (save-excursion
  18853. (goto-char (region-beginning))
  18854. (org-at-heading-p)))))
  18855. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18856. (call-interactively 'org-do-promote))
  18857. ;; At an inline task.
  18858. ((org-at-heading-p)
  18859. (call-interactively 'org-inlinetask-promote))
  18860. ((or (org-at-item-p)
  18861. (and (org-region-active-p)
  18862. (save-excursion
  18863. (goto-char (region-beginning))
  18864. (org-at-item-p))))
  18865. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18866. (call-interactively 'org-outdent-item))
  18867. (t (call-interactively 'backward-word))))
  18868. (defun org-metaright (&optional arg)
  18869. "Demote heading, list item at point or move table column right.
  18870. In front of a drawer or a block keyword, indent it correctly.
  18871. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18872. `org-indnet-drawer' or `org-indent-block' depending on context.
  18873. With no specific context, calls the Emacs default `forward-word'.
  18874. See the individual commands for more information.
  18875. This function runs the hook `org-metaright-hook' as a first step,
  18876. and returns at first non-nil value."
  18877. (interactive "P")
  18878. (cond
  18879. ((run-hook-with-args-until-success 'org-metaright-hook))
  18880. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18881. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18882. ((org-at-block-p) (call-interactively 'org-indent-block))
  18883. ((org-with-limited-levels
  18884. (or (org-at-heading-p)
  18885. (and (org-region-active-p)
  18886. (save-excursion
  18887. (goto-char (region-beginning))
  18888. (org-at-heading-p)))))
  18889. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18890. (call-interactively 'org-do-demote))
  18891. ;; At an inline task.
  18892. ((org-at-heading-p)
  18893. (call-interactively 'org-inlinetask-demote))
  18894. ((or (org-at-item-p)
  18895. (and (org-region-active-p)
  18896. (save-excursion
  18897. (goto-char (region-beginning))
  18898. (org-at-item-p))))
  18899. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18900. (call-interactively 'org-indent-item))
  18901. (t (call-interactively 'forward-word))))
  18902. (defun org-check-for-hidden (what)
  18903. "Check if there are hidden headlines/items in the current visual line.
  18904. WHAT can be either `headlines' or `items'. If the current line is
  18905. an outline or item heading and it has a folded subtree below it,
  18906. this function returns t, nil otherwise."
  18907. (let ((re (cond
  18908. ((eq what 'headlines) org-outline-regexp-bol)
  18909. ((eq what 'items) (org-item-beginning-re))
  18910. (t (error "This should not happen"))))
  18911. beg end)
  18912. (save-excursion
  18913. (catch 'exit
  18914. (unless (org-region-active-p)
  18915. (setq beg (point-at-bol))
  18916. (beginning-of-line 2)
  18917. (while (and (not (eobp)) ;; this is like `next-line'
  18918. (get-char-property (1- (point)) 'invisible))
  18919. (beginning-of-line 2))
  18920. (setq end (point))
  18921. (goto-char beg)
  18922. (goto-char (point-at-eol))
  18923. (setq end (max end (point)))
  18924. (while (re-search-forward re end t)
  18925. (if (get-char-property (match-beginning 0) 'invisible)
  18926. (throw 'exit t))))
  18927. nil))))
  18928. (defun org-metaup (&optional arg)
  18929. "Move subtree up or move table row up.
  18930. Calls `org-move-subtree-up' or `org-table-move-row' or
  18931. `org-move-item-up', depending on context. See the individual commands
  18932. for more information."
  18933. (interactive "P")
  18934. (cond
  18935. ((run-hook-with-args-until-success 'org-metaup-hook))
  18936. ((org-region-active-p)
  18937. (let* ((a (min (region-beginning) (region-end)))
  18938. (b (1- (max (region-beginning) (region-end))))
  18939. (c (save-excursion (goto-char a)
  18940. (move-beginning-of-line 0)))
  18941. (d (save-excursion (goto-char a)
  18942. (move-end-of-line 0) (point))))
  18943. (transpose-regions a b c d)
  18944. (goto-char c)))
  18945. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18946. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18947. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18948. (t (org-drag-element-backward))))
  18949. (defun org-metadown (&optional arg)
  18950. "Move subtree down or move table row down.
  18951. Calls `org-move-subtree-down' or `org-table-move-row' or
  18952. `org-move-item-down', depending on context. See the individual
  18953. commands for more information."
  18954. (interactive "P")
  18955. (cond
  18956. ((run-hook-with-args-until-success 'org-metadown-hook))
  18957. ((org-region-active-p)
  18958. (let* ((a (min (region-beginning) (region-end)))
  18959. (b (max (region-beginning) (region-end)))
  18960. (c (save-excursion (goto-char b)
  18961. (move-beginning-of-line 1)))
  18962. (d (save-excursion (goto-char b)
  18963. (move-end-of-line 1) (1+ (point)))))
  18964. (transpose-regions a b c d)
  18965. (goto-char d)))
  18966. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18967. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18968. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18969. (t (org-drag-element-forward))))
  18970. (defun org-shiftup (&optional arg)
  18971. "Increase item in timestamp or increase priority of current headline.
  18972. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18973. depending on context. See the individual commands for more information."
  18974. (interactive "P")
  18975. (cond
  18976. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18977. ((and org-support-shift-select (org-region-active-p))
  18978. (org-call-for-shift-select 'previous-line))
  18979. ((org-at-timestamp-p t)
  18980. (call-interactively (if org-edit-timestamp-down-means-later
  18981. 'org-timestamp-down 'org-timestamp-up)))
  18982. ((and (not (eq org-support-shift-select 'always))
  18983. org-enable-priority-commands
  18984. (org-at-heading-p))
  18985. (call-interactively 'org-priority-up))
  18986. ((and (not org-support-shift-select) (org-at-item-p))
  18987. (call-interactively 'org-previous-item))
  18988. ((org-clocktable-try-shift 'up arg))
  18989. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18990. (org-support-shift-select
  18991. (org-call-for-shift-select 'previous-line))
  18992. (t (org-shiftselect-error))))
  18993. (defun org-shiftdown (&optional arg)
  18994. "Decrease item in timestamp or decrease priority of current headline.
  18995. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18996. depending on context. See the individual commands for more information."
  18997. (interactive "P")
  18998. (cond
  18999. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19000. ((and org-support-shift-select (org-region-active-p))
  19001. (org-call-for-shift-select 'next-line))
  19002. ((org-at-timestamp-p t)
  19003. (call-interactively (if org-edit-timestamp-down-means-later
  19004. 'org-timestamp-up 'org-timestamp-down)))
  19005. ((and (not (eq org-support-shift-select 'always))
  19006. org-enable-priority-commands
  19007. (org-at-heading-p))
  19008. (call-interactively 'org-priority-down))
  19009. ((and (not org-support-shift-select) (org-at-item-p))
  19010. (call-interactively 'org-next-item))
  19011. ((org-clocktable-try-shift 'down arg))
  19012. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19013. (org-support-shift-select
  19014. (org-call-for-shift-select 'next-line))
  19015. (t (org-shiftselect-error))))
  19016. (defun org-shiftright (&optional arg)
  19017. "Cycle the thing at point or in the current line, depending on context.
  19018. Depending on context, this does one of the following:
  19019. - switch a timestamp at point one day into the future
  19020. - on a headline, switch to the next TODO keyword.
  19021. - on an item, switch entire list to the next bullet type
  19022. - on a property line, switch to the next allowed value
  19023. - on a clocktable definition line, move time block into the future"
  19024. (interactive "P")
  19025. (cond
  19026. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19027. ((and org-support-shift-select (org-region-active-p))
  19028. (org-call-for-shift-select 'forward-char))
  19029. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19030. ((and (not (eq org-support-shift-select 'always))
  19031. (org-at-heading-p))
  19032. (let ((org-inhibit-logging
  19033. (not org-treat-S-cursor-todo-selection-as-state-change))
  19034. (org-inhibit-blocking
  19035. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19036. (org-call-with-arg 'org-todo 'right)))
  19037. ((or (and org-support-shift-select
  19038. (not (eq org-support-shift-select 'always))
  19039. (org-at-item-bullet-p))
  19040. (and (not org-support-shift-select) (org-at-item-p)))
  19041. (org-call-with-arg 'org-cycle-list-bullet nil))
  19042. ((and (not (eq org-support-shift-select 'always))
  19043. (org-at-property-p))
  19044. (call-interactively 'org-property-next-allowed-value))
  19045. ((org-clocktable-try-shift 'right arg))
  19046. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19047. (org-support-shift-select
  19048. (org-call-for-shift-select 'forward-char))
  19049. (t (org-shiftselect-error))))
  19050. (defun org-shiftleft (&optional arg)
  19051. "Cycle the thing at point or in the current line, depending on context.
  19052. Depending on context, this does one of the following:
  19053. - switch a timestamp at point one day into the past
  19054. - on a headline, switch to the previous TODO keyword.
  19055. - on an item, switch entire list to the previous bullet type
  19056. - on a property line, switch to the previous allowed value
  19057. - on a clocktable definition line, move time block into the past"
  19058. (interactive "P")
  19059. (cond
  19060. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19061. ((and org-support-shift-select (org-region-active-p))
  19062. (org-call-for-shift-select 'backward-char))
  19063. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19064. ((and (not (eq org-support-shift-select 'always))
  19065. (org-at-heading-p))
  19066. (let ((org-inhibit-logging
  19067. (not org-treat-S-cursor-todo-selection-as-state-change))
  19068. (org-inhibit-blocking
  19069. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19070. (org-call-with-arg 'org-todo 'left)))
  19071. ((or (and org-support-shift-select
  19072. (not (eq org-support-shift-select 'always))
  19073. (org-at-item-bullet-p))
  19074. (and (not org-support-shift-select) (org-at-item-p)))
  19075. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19076. ((and (not (eq org-support-shift-select 'always))
  19077. (org-at-property-p))
  19078. (call-interactively 'org-property-previous-allowed-value))
  19079. ((org-clocktable-try-shift 'left arg))
  19080. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19081. (org-support-shift-select
  19082. (org-call-for-shift-select 'backward-char))
  19083. (t (org-shiftselect-error))))
  19084. (defun org-shiftcontrolright ()
  19085. "Switch to next TODO set."
  19086. (interactive)
  19087. (cond
  19088. ((and org-support-shift-select (org-region-active-p))
  19089. (org-call-for-shift-select 'forward-word))
  19090. ((and (not (eq org-support-shift-select 'always))
  19091. (org-at-heading-p))
  19092. (org-call-with-arg 'org-todo 'nextset))
  19093. (org-support-shift-select
  19094. (org-call-for-shift-select 'forward-word))
  19095. (t (org-shiftselect-error))))
  19096. (defun org-shiftcontrolleft ()
  19097. "Switch to previous TODO set."
  19098. (interactive)
  19099. (cond
  19100. ((and org-support-shift-select (org-region-active-p))
  19101. (org-call-for-shift-select 'backward-word))
  19102. ((and (not (eq org-support-shift-select 'always))
  19103. (org-at-heading-p))
  19104. (org-call-with-arg 'org-todo 'previousset))
  19105. (org-support-shift-select
  19106. (org-call-for-shift-select 'backward-word))
  19107. (t (org-shiftselect-error))))
  19108. (defun org-shiftcontrolup (&optional n)
  19109. "Change timestamps synchronously up in CLOCK log lines.
  19110. Optional argument N tells to change by that many units."
  19111. (interactive "P")
  19112. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19113. (let (org-support-shift-select)
  19114. (org-clock-timestamps-up n))
  19115. (user-error "Not at a clock log")))
  19116. (defun org-shiftcontroldown (&optional n)
  19117. "Change timestamps synchronously down in CLOCK log lines.
  19118. Optional argument N tells to change by that many units."
  19119. (interactive "P")
  19120. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19121. (let (org-support-shift-select)
  19122. (org-clock-timestamps-down n))
  19123. (user-error "Not at a clock log")))
  19124. (defun org-increase-number-at-point (&optional inc)
  19125. "Increment the number at point.
  19126. With an optional prefix numeric argument INC, increment using
  19127. this numeric value."
  19128. (interactive "p")
  19129. (if (not (number-at-point))
  19130. (user-error "Not on a number")
  19131. (unless inc (setq inc 1))
  19132. (let ((pos (point))
  19133. (beg (skip-chars-backward "-+^/*0-9eE."))
  19134. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19135. (setq nap (buffer-substring-no-properties
  19136. (+ pos beg) (+ pos beg end)))
  19137. (delete-region (+ pos beg) (+ pos beg end))
  19138. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19139. (when (org-at-table-p)
  19140. (org-table-align)
  19141. (org-table-end-of-field 1))))
  19142. (defun org-decrease-number-at-point (&optional inc)
  19143. "Decrement the number at point.
  19144. With an optional prefix numeric argument INC, decrement using
  19145. this numeric value."
  19146. (interactive "p")
  19147. (org-increase-number-at-point (- (or inc 1))))
  19148. (defun org-ctrl-c-ret ()
  19149. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19150. (interactive)
  19151. (cond
  19152. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19153. (t (call-interactively 'org-insert-heading))))
  19154. (defun org-find-visible ()
  19155. (let ((s (point)))
  19156. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19157. (get-char-property s 'invisible)))
  19158. s))
  19159. (defun org-find-invisible ()
  19160. (let ((s (point)))
  19161. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19162. (not (get-char-property s 'invisible))))
  19163. s))
  19164. (defun org-copy-visible (beg end)
  19165. "Copy the visible parts of the region."
  19166. (interactive "r")
  19167. (let (snippets s)
  19168. (save-excursion
  19169. (save-restriction
  19170. (narrow-to-region beg end)
  19171. (setq s (goto-char (point-min)))
  19172. (while (not (= (point) (point-max)))
  19173. (goto-char (org-find-invisible))
  19174. (push (buffer-substring s (point)) snippets)
  19175. (setq s (goto-char (org-find-visible))))))
  19176. (kill-new (apply 'concat (nreverse snippets)))))
  19177. (defun org-copy-special ()
  19178. "Copy region in table or copy current subtree.
  19179. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19180. See the individual commands for more information."
  19181. (interactive)
  19182. (call-interactively
  19183. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19184. (defun org-cut-special ()
  19185. "Cut region in table or cut current subtree.
  19186. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19187. See the individual commands for more information."
  19188. (interactive)
  19189. (call-interactively
  19190. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19191. (defun org-paste-special (arg)
  19192. "Paste rectangular region into table, or past subtree relative to level.
  19193. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19194. See the individual commands for more information."
  19195. (interactive "P")
  19196. (if (org-at-table-p)
  19197. (org-table-paste-rectangle)
  19198. (org-paste-subtree arg)))
  19199. (defsubst org-in-fixed-width-region-p ()
  19200. "Is point in a fixed-width region?"
  19201. (save-match-data
  19202. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19203. (defun org-edit-special (&optional arg)
  19204. "Call a special editor for the element at point.
  19205. When at a table, call the formula editor with `org-table-edit-formulas'.
  19206. When in a source code block, call `org-edit-src-code'.
  19207. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19208. When in an export block, call `org-edit-export-block'.
  19209. When at an #+INCLUDE keyword, visit the included file.
  19210. When at a footnote reference, call `org-edit-footnote-reference'
  19211. On a link, call `ffap' to visit the link at point.
  19212. Otherwise, return a user error."
  19213. (interactive "P")
  19214. (let ((element (org-element-at-point)))
  19215. (assert (not buffer-read-only) nil
  19216. "Buffer is read-only: %s" (buffer-name))
  19217. (case (org-element-type element)
  19218. (src-block
  19219. (if (not arg) (org-edit-src-code)
  19220. (let* ((info (org-babel-get-src-block-info))
  19221. (lang (nth 0 info))
  19222. (params (nth 2 info))
  19223. (session (cdr (assq :session params))))
  19224. (if (not session) (org-edit-src-code)
  19225. ;; At a src-block with a session and function called with
  19226. ;; an ARG: switch to the buffer related to the inferior
  19227. ;; process.
  19228. (switch-to-buffer
  19229. (funcall (intern (concat "org-babel-prep-session:" lang))
  19230. session params))))))
  19231. (keyword
  19232. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19233. (org-open-link-from-string
  19234. (format "[[%s]]"
  19235. (expand-file-name
  19236. (let ((value (org-element-property :value element)))
  19237. (cond ((not (org-string-nw-p value))
  19238. (user-error "No file to edit"))
  19239. ((string-match "\\`\"\\(.*?\\)\"" value)
  19240. (match-string 1 value))
  19241. ((string-match "\\`[^ \t\"]\\S-*" value)
  19242. (match-string 0 value))
  19243. (t (user-error "No valid file specified")))))))
  19244. (user-error "No special environment to edit here")))
  19245. (table
  19246. (if (eq (org-element-property :type element) 'table.el)
  19247. (org-edit-table.el)
  19248. (call-interactively 'org-table-edit-formulas)))
  19249. ;; Only Org tables contain `table-row' type elements.
  19250. (table-row (call-interactively 'org-table-edit-formulas))
  19251. (example-block (org-edit-src-code))
  19252. (export-block (org-edit-export-block))
  19253. (fixed-width (org-edit-fixed-width-region))
  19254. (otherwise
  19255. ;; No notable element at point. Though, we may be at a link or
  19256. ;; a footnote reference, which are objects. Thus, scan deeper.
  19257. (let ((context (org-element-context element)))
  19258. (case (org-element-type context)
  19259. (link (call-interactively #'ffap))
  19260. (footnote-reference (org-edit-footnote-reference))
  19261. (t (user-error "No special environment to edit here"))))))))
  19262. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19263. (defun org-ctrl-c-ctrl-c (&optional arg)
  19264. "Set tags in headline, or update according to changed information at point.
  19265. This command does many different things, depending on context:
  19266. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19267. this is what we do.
  19268. - If the cursor is on a statistics cookie, update it.
  19269. - If the cursor is in a headline, prompt for tags and insert them
  19270. into the current line, aligned to `org-tags-column'. When called
  19271. with prefix arg, realign all tags in the current buffer.
  19272. - If the cursor is in one of the special #+KEYWORD lines, this
  19273. triggers scanning the buffer for these lines and updating the
  19274. information.
  19275. - If the cursor is inside a table, realign the table. This command
  19276. works even if the automatic table editor has been turned off.
  19277. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19278. the entire table.
  19279. - If the cursor is at a footnote reference or definition, jump to
  19280. the corresponding definition or references, respectively.
  19281. - If the cursor is a the beginning of a dynamic block, update it.
  19282. - If the current buffer is a capture buffer, close note and file it.
  19283. - If the cursor is on a <<<target>>>, update radio targets and
  19284. corresponding links in this buffer.
  19285. - If the cursor is on a numbered item in a plain list, renumber the
  19286. ordered list.
  19287. - If the cursor is on a checkbox, toggle it.
  19288. - If the cursor is on a code block, evaluate it. The variable
  19289. `org-confirm-babel-evaluate' can be used to control prompting
  19290. before code block evaluation, by default every code block
  19291. evaluation requires confirmation. Code block evaluation can be
  19292. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19293. (interactive "P")
  19294. (cond
  19295. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19296. org-occur-highlights)
  19297. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19298. (org-remove-occur-highlights)
  19299. (message "Temporary highlights/overlays removed from current buffer"))
  19300. ((and (local-variable-p 'org-finish-function (current-buffer))
  19301. (fboundp org-finish-function))
  19302. (funcall org-finish-function))
  19303. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19304. (t
  19305. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19306. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19307. (user-error "C-c C-c can do nothing useful at this location"))
  19308. (let* ((context (org-element-context))
  19309. (type (org-element-type context)))
  19310. (case type
  19311. ;; When at a link, act according to the parent instead.
  19312. (link (setq context (org-element-property :parent context))
  19313. (setq type (org-element-type context)))
  19314. ;; Unsupported object types: refer to the first supported
  19315. ;; element or object containing it.
  19316. ((bold code entity export-snippet inline-babel-call inline-src-block
  19317. italic latex-fragment line-break macro strike-through subscript
  19318. superscript underline verbatim)
  19319. (setq context
  19320. (org-element-lineage
  19321. context '(radio-target paragraph verse-block table-cell)))))
  19322. ;; For convenience: at the first line of a paragraph on the
  19323. ;; same line as an item, apply function on that item instead.
  19324. (when (eq type 'paragraph)
  19325. (let ((parent (org-element-property :parent context)))
  19326. (when (and (eq (org-element-type parent) 'item)
  19327. (= (line-beginning-position)
  19328. (org-element-property :begin parent)))
  19329. (setq context parent type 'item))))
  19330. ;; Act according to type of element or object at point.
  19331. (case type
  19332. (clock (org-clock-update-time-maybe))
  19333. (dynamic-block
  19334. (save-excursion
  19335. (goto-char (org-element-property :post-affiliated context))
  19336. (org-update-dblock)))
  19337. (footnote-definition
  19338. (goto-char (org-element-property :post-affiliated context))
  19339. (call-interactively 'org-footnote-action))
  19340. (footnote-reference (call-interactively 'org-footnote-action))
  19341. ((headline inlinetask)
  19342. (save-excursion (goto-char (org-element-property :begin context))
  19343. (call-interactively 'org-set-tags)))
  19344. (item
  19345. ;; At an item: a double C-u set checkbox to "[-]"
  19346. ;; unconditionally, whereas a single one will toggle its
  19347. ;; presence. Without a universal argument, if the item
  19348. ;; has a checkbox, toggle it. Otherwise repair the list.
  19349. (let* ((box (org-element-property :checkbox context))
  19350. (struct (org-element-property :structure context))
  19351. (old-struct (copy-tree struct))
  19352. (parents (org-list-parents-alist struct))
  19353. (prevs (org-list-prevs-alist struct))
  19354. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19355. (org-list-set-checkbox
  19356. (org-element-property :begin context) struct
  19357. (cond ((equal arg '(16)) "[-]")
  19358. ((and (not box) (equal arg '(4))) "[ ]")
  19359. ((or (not box) (equal arg '(4))) nil)
  19360. ((eq box 'on) "[ ]")
  19361. (t "[X]")))
  19362. ;; Mimic `org-list-write-struct' but with grabbing
  19363. ;; a return value from `org-list-struct-fix-box'.
  19364. (org-list-struct-fix-ind struct parents 2)
  19365. (org-list-struct-fix-item-end struct)
  19366. (org-list-struct-fix-bul struct prevs)
  19367. (org-list-struct-fix-ind struct parents)
  19368. (let ((block-item
  19369. (org-list-struct-fix-box struct parents prevs orderedp)))
  19370. (if (and box (equal struct old-struct))
  19371. (if (equal arg '(16))
  19372. (message "Checkboxes already reset")
  19373. (user-error "Cannot toggle this checkbox: %s"
  19374. (if (eq box 'on)
  19375. "all subitems checked"
  19376. "unchecked subitems")))
  19377. (org-list-struct-apply-struct struct old-struct)
  19378. (org-update-checkbox-count-maybe))
  19379. (when block-item
  19380. (message "Checkboxes were removed due to empty box at line %d"
  19381. (org-current-line block-item))))))
  19382. (keyword
  19383. (let ((org-inhibit-startup-visibility-stuff t)
  19384. (org-startup-align-all-tables nil))
  19385. (when (boundp 'org-table-coordinate-overlays)
  19386. (mapc 'delete-overlay org-table-coordinate-overlays)
  19387. (setq org-table-coordinate-overlays nil))
  19388. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19389. (message "Local setup has been refreshed"))
  19390. (plain-list
  19391. ;; At a plain list, with a double C-u argument, set
  19392. ;; checkboxes of each item to "[-]", whereas a single one
  19393. ;; will toggle their presence according to the state of the
  19394. ;; first item in the list. Without an argument, repair the
  19395. ;; list.
  19396. (let* ((begin (org-element-property :contents-begin context))
  19397. (beginm (move-marker (make-marker) begin))
  19398. (struct (org-element-property :structure context))
  19399. (old-struct (copy-tree struct))
  19400. (first-box (save-excursion
  19401. (goto-char begin)
  19402. (looking-at org-list-full-item-re)
  19403. (match-string-no-properties 3)))
  19404. (new-box (cond ((equal arg '(16)) "[-]")
  19405. ((equal arg '(4)) (unless first-box "[ ]"))
  19406. ((equal first-box "[X]") "[ ]")
  19407. (t "[X]"))))
  19408. (cond
  19409. (arg
  19410. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19411. (org-list-get-all-items
  19412. begin struct (org-list-prevs-alist struct))))
  19413. ((and first-box (eq (point) begin))
  19414. ;; For convenience, when point is at bol on the first
  19415. ;; item of the list and no argument is provided, simply
  19416. ;; toggle checkbox of that item, if any.
  19417. (org-list-set-checkbox begin struct new-box)))
  19418. (org-list-write-struct
  19419. struct (org-list-parents-alist struct) old-struct)
  19420. (org-update-checkbox-count-maybe)
  19421. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19422. ((property-drawer node-property)
  19423. (call-interactively 'org-property-action))
  19424. ((radio-target target)
  19425. (call-interactively 'org-update-radio-target-regexp))
  19426. (statistics-cookie
  19427. (call-interactively 'org-update-statistics-cookies))
  19428. ((table table-cell table-row)
  19429. ;; At a table, recalculate every field and align it. Also
  19430. ;; send the table if necessary. If the table has
  19431. ;; a `table.el' type, just give up. At a table row or
  19432. ;; cell, maybe recalculate line but always align table.
  19433. (if (eq (org-element-property :type context) 'table.el)
  19434. (message "Use C-c ' to edit table.el tables")
  19435. (let ((org-enable-table-editor t))
  19436. (if (or (eq type 'table)
  19437. ;; Check if point is at a TBLFM line.
  19438. (and (eq type 'table-row)
  19439. (= (point) (org-element-property :end context))))
  19440. (save-excursion
  19441. (if (org-at-TBLFM-p)
  19442. (progn (require 'org-table)
  19443. (org-table-calc-current-TBLFM))
  19444. (goto-char (org-element-property :contents-begin context))
  19445. (org-call-with-arg 'org-table-recalculate (or arg t))
  19446. (orgtbl-send-table 'maybe)))
  19447. (org-table-maybe-eval-formula)
  19448. (cond (arg (call-interactively 'org-table-recalculate))
  19449. ((org-table-maybe-recalculate-line))
  19450. (t (org-table-align)))))))
  19451. (timestamp (org-timestamp-change 0 'day))
  19452. (otherwise
  19453. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19454. (user-error
  19455. "C-c C-c can do nothing useful at this location")))))))))
  19456. (defun org-mode-restart ()
  19457. (interactive)
  19458. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19459. (funcall major-mode)
  19460. (hack-local-variables)
  19461. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19462. (org-indent-mode -1)))
  19463. (message "%s restarted" major-mode))
  19464. (defun org-kill-note-or-show-branches ()
  19465. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19466. (interactive)
  19467. (if (not org-finish-function)
  19468. (progn
  19469. (hide-subtree)
  19470. (call-interactively 'show-branches))
  19471. (let ((org-note-abort t))
  19472. (funcall org-finish-function))))
  19473. (defun org-delete-indentation (&optional ARG)
  19474. "Join current line to previous and fix whitespace at join.
  19475. If previous line is a headline add to headline title. Otherwise
  19476. the function calls `delete-indentation'.
  19477. With argument, join this line to following line."
  19478. (interactive "*P")
  19479. (if (save-excursion
  19480. (if ARG (beginning-of-line)
  19481. (forward-line -1))
  19482. (looking-at org-complex-heading-regexp))
  19483. ;; At headline.
  19484. (let ((tags-column (when (match-beginning 5)
  19485. (save-excursion (goto-char (match-beginning 5))
  19486. (current-column))))
  19487. (string (concat " " (progn (when ARG (forward-line 1))
  19488. (org-trim (delete-and-extract-region
  19489. (line-beginning-position)
  19490. (line-end-position)))))))
  19491. (unless (bobp) (delete-region (point) (1- (point))))
  19492. (goto-char (or (match-end 4)
  19493. (match-beginning 5)
  19494. (match-end 0)))
  19495. (skip-chars-backward " \t")
  19496. (save-excursion (insert string))
  19497. ;; Adjust alignment of tags.
  19498. (when tags-column
  19499. (org-align-tags-here (if org-auto-align-tags
  19500. org-tags-column
  19501. tags-column))))
  19502. (delete-indentation ARG)))
  19503. (defun org-open-line (n)
  19504. "Insert a new row in tables, call `open-line' elsewhere.
  19505. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19506. (interactive "*p")
  19507. (cond
  19508. ((not org-special-ctrl-o)
  19509. (open-line n))
  19510. ((org-at-table-p)
  19511. (org-table-insert-row))
  19512. (t
  19513. (open-line n))))
  19514. (defun org-return (&optional indent)
  19515. "Goto next table row or insert a newline.
  19516. Calls `org-table-next-row' or `newline', depending on context.
  19517. When optional INDENT argument is non-nil, call
  19518. `newline-and-indent' instead of `newline'.
  19519. When `org-return-follows-link' is non-nil and point is on
  19520. a timestamp or a link, call `org-open-at-point'. However, it
  19521. will not happen if point is in a table or on a \"dead\"
  19522. object (e.g., within a comment). In these case, you need to use
  19523. `org-open-at-point' directly."
  19524. (interactive)
  19525. (let* ((context (if org-return-follows-link (org-element-context)
  19526. (org-element-at-point)))
  19527. (type (org-element-type context)))
  19528. (cond
  19529. ;; In a table, call `org-table-next-row'.
  19530. ((or (and (eq type 'table)
  19531. (>= (point) (org-element-property :contents-begin context))
  19532. (< (point) (org-element-property :contents-end context)))
  19533. (org-element-lineage context '(table-row table-cell) t))
  19534. (org-table-justify-field-maybe)
  19535. (call-interactively #'org-table-next-row))
  19536. ;; On a link or a timestamp but not on white spaces after it,
  19537. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19538. ((and org-return-follows-link
  19539. (memq type '(link timestamp))
  19540. (< (point)
  19541. (save-excursion (goto-char (org-element-property :end context))
  19542. (skip-chars-backward " \t")
  19543. (point))))
  19544. (call-interactively #'org-open-at-point))
  19545. ;; Insert newline in heading, but preserve tags.
  19546. ((and (not (bolp))
  19547. (save-excursion (beginning-of-line)
  19548. (looking-at org-complex-heading-regexp)))
  19549. ;; At headline. Split line. However, if point is on keyword,
  19550. ;; priority cookie or tags, do not break any of them: add
  19551. ;; a newline after the headline instead.
  19552. (let ((tags-column (and (match-beginning 5)
  19553. (save-excursion (goto-char (match-beginning 5))
  19554. (current-column))))
  19555. (string
  19556. (when (and (match-end 4)
  19557. (>= (point)
  19558. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19559. (<= (point) (match-end 4)))
  19560. (delete-and-extract-region (point) (match-end 4)))))
  19561. (when (and tags-column string) ; Adjust tag alignment.
  19562. (org-align-tags-here
  19563. (if org-auto-align-tags org-tags-column tags-column)))
  19564. (end-of-line)
  19565. (org-show-entry)
  19566. (if indent (newline-and-indent) (newline))
  19567. (when string (save-excursion (insert (org-trim string))))))
  19568. ;; In a list, make sure indenting keeps trailing text within.
  19569. ((and indent
  19570. (not (eolp))
  19571. (org-element-lineage context '(item)))
  19572. (let ((trailing-data
  19573. (delete-and-extract-region (point) (line-end-position))))
  19574. (newline-and-indent)
  19575. (save-excursion (insert trailing-data))))
  19576. (t (if indent (newline-and-indent) (newline))))))
  19577. (defun org-return-indent ()
  19578. "Goto next table row or insert a newline and indent.
  19579. Calls `org-table-next-row' or `newline-and-indent', depending on
  19580. context. See the individual commands for more information."
  19581. (interactive)
  19582. (org-return t))
  19583. (defun org-ctrl-c-star ()
  19584. "Compute table, or change heading status of lines.
  19585. Calls `org-table-recalculate' or `org-toggle-heading',
  19586. depending on context."
  19587. (interactive)
  19588. (cond
  19589. ((org-at-table-p)
  19590. (call-interactively 'org-table-recalculate))
  19591. (t
  19592. ;; Convert all lines in region to list items
  19593. (call-interactively 'org-toggle-heading))))
  19594. (defun org-ctrl-c-minus ()
  19595. "Insert separator line in table or modify bullet status of line.
  19596. Also turns a plain line or a region of lines into list items.
  19597. Calls `org-table-insert-hline', `org-toggle-item', or
  19598. `org-cycle-list-bullet', depending on context."
  19599. (interactive)
  19600. (cond
  19601. ((org-at-table-p)
  19602. (call-interactively 'org-table-insert-hline))
  19603. ((org-region-active-p)
  19604. (call-interactively 'org-toggle-item))
  19605. ((org-in-item-p)
  19606. (call-interactively 'org-cycle-list-bullet))
  19607. (t
  19608. (call-interactively 'org-toggle-item))))
  19609. (defun org-toggle-item (arg)
  19610. "Convert headings or normal lines to items, items to normal lines.
  19611. If there is no active region, only the current line is considered.
  19612. If the first non blank line in the region is a headline, convert
  19613. all headlines to items, shifting text accordingly.
  19614. If it is an item, convert all items to normal lines.
  19615. If it is normal text, change region into a list of items.
  19616. With a prefix argument ARG, change the region in a single item."
  19617. (interactive "P")
  19618. (let ((shift-text
  19619. (function
  19620. ;; Shift text in current section to IND, from point to END.
  19621. ;; The function leaves point to END line.
  19622. (lambda (ind end)
  19623. (let ((min-i 1000) (end (copy-marker end)))
  19624. ;; First determine the minimum indentation (MIN-I) of
  19625. ;; the text.
  19626. (save-excursion
  19627. (catch 'exit
  19628. (while (< (point) end)
  19629. (let ((i (org-get-indentation)))
  19630. (cond
  19631. ;; Skip blank lines and inline tasks.
  19632. ((looking-at "^[ \t]*$"))
  19633. ((looking-at org-outline-regexp-bol))
  19634. ;; We can't find less than 0 indentation.
  19635. ((zerop i) (throw 'exit (setq min-i 0)))
  19636. ((< i min-i) (setq min-i i))))
  19637. (forward-line))))
  19638. ;; Then indent each line so that a line indented to
  19639. ;; MIN-I becomes indented to IND. Ignore blank lines
  19640. ;; and inline tasks in the process.
  19641. (let ((delta (- ind min-i)))
  19642. (while (< (point) end)
  19643. (unless (or (looking-at "^[ \t]*$")
  19644. (looking-at org-outline-regexp-bol))
  19645. (org-indent-line-to (+ (org-get-indentation) delta)))
  19646. (forward-line)))))))
  19647. (skip-blanks
  19648. (function
  19649. ;; Return beginning of first non-blank line, starting from
  19650. ;; line at POS.
  19651. (lambda (pos)
  19652. (save-excursion
  19653. (goto-char pos)
  19654. (skip-chars-forward " \r\t\n")
  19655. (point-at-bol)))))
  19656. beg end)
  19657. ;; Determine boundaries of changes.
  19658. (if (org-region-active-p)
  19659. (setq beg (funcall skip-blanks (region-beginning))
  19660. end (copy-marker (region-end)))
  19661. (setq beg (funcall skip-blanks (point-at-bol))
  19662. end (copy-marker (point-at-eol))))
  19663. ;; Depending on the starting line, choose an action on the text
  19664. ;; between BEG and END.
  19665. (org-with-limited-levels
  19666. (save-excursion
  19667. (goto-char beg)
  19668. (cond
  19669. ;; Case 1. Start at an item: de-itemize. Note that it only
  19670. ;; happens when a region is active: `org-ctrl-c-minus'
  19671. ;; would call `org-cycle-list-bullet' otherwise.
  19672. ((org-at-item-p)
  19673. (while (< (point) end)
  19674. (when (org-at-item-p)
  19675. (skip-chars-forward " \t")
  19676. (delete-region (point) (match-end 0)))
  19677. (forward-line)))
  19678. ;; Case 2. Start at an heading: convert to items.
  19679. ((org-at-heading-p)
  19680. (let* ((bul (org-list-bullet-string "-"))
  19681. (bul-len (length bul))
  19682. (done (org-entry-is-done-p))
  19683. (todo (org-entry-is-todo-p))
  19684. ;; Indentation of the first heading. It should be
  19685. ;; relative to the indentation of its parent, if any.
  19686. (start-ind (save-excursion
  19687. (cond
  19688. ((not org-adapt-indentation) 0)
  19689. ((not (outline-previous-heading)) 0)
  19690. (t (length (match-string 0))))))
  19691. ;; Level of first heading. Further headings will be
  19692. ;; compared to it to determine hierarchy in the list.
  19693. (ref-level (org-reduced-level (org-outline-level))))
  19694. (when (or done todo) (org-todo ""))
  19695. (while (< (point) end)
  19696. (let* ((level (org-reduced-level (org-outline-level)))
  19697. (delta (max 0 (- level ref-level))))
  19698. ;; If current headline is less indented than the first
  19699. ;; one, set it as reference, in order to preserve
  19700. ;; subtrees.
  19701. (when (< level ref-level) (setq ref-level level))
  19702. (replace-match bul t t)
  19703. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19704. (when (or done todo)
  19705. (let* ((struct (org-list-struct))
  19706. (old (copy-tree struct)))
  19707. (org-list-set-checkbox (line-beginning-position)
  19708. struct
  19709. (if done "[X]" "[ ]"))
  19710. (org-list-write-struct struct
  19711. (org-list-parents-alist struct)
  19712. old)))
  19713. ;; Ensure all text down to END (or SECTION-END) belongs
  19714. ;; to the newly created item.
  19715. (let ((section-end (save-excursion
  19716. (or (outline-next-heading) (point)))))
  19717. (forward-line)
  19718. (funcall shift-text
  19719. (+ start-ind (* (1+ delta) bul-len))
  19720. (min end section-end)))))))
  19721. ;; Case 3. Normal line with ARG: make the first line of region
  19722. ;; an item, and shift indentation of others lines to
  19723. ;; set them as item's body.
  19724. (arg (let* ((bul (org-list-bullet-string "-"))
  19725. (bul-len (length bul))
  19726. (ref-ind (org-get-indentation)))
  19727. (skip-chars-forward " \t")
  19728. (insert bul)
  19729. (forward-line)
  19730. (while (< (point) end)
  19731. ;; Ensure that lines less indented than first one
  19732. ;; still get included in item body.
  19733. (funcall shift-text
  19734. (+ ref-ind bul-len)
  19735. (min end (save-excursion (or (outline-next-heading)
  19736. (point)))))
  19737. (forward-line))))
  19738. ;; Case 4. Normal line without ARG: turn each non-item line
  19739. ;; into an item.
  19740. (t
  19741. (while (< (point) end)
  19742. (unless (or (org-at-heading-p) (org-at-item-p))
  19743. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19744. (replace-match
  19745. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19746. (forward-line))))))))
  19747. (defun org-toggle-heading (&optional nstars)
  19748. "Convert headings to normal text, or items or text to headings.
  19749. If there is no active region, only convert the current line.
  19750. With a \\[universal-argument] prefix, convert the whole list at
  19751. point into heading.
  19752. In a region:
  19753. - If the first non blank line is a headline, remove the stars
  19754. from all headlines in the region.
  19755. - If it is a normal line, turn each and every normal line (i.e.,
  19756. not an heading or an item) in the region into headings. If you
  19757. want to convert only the first line of this region, use one
  19758. universal prefix argument.
  19759. - If it is a plain list item, turn all plain list items into headings.
  19760. When converting a line into a heading, the number of stars is chosen
  19761. such that the lines become children of the current entry. However,
  19762. when a numeric prefix argument is given, its value determines the
  19763. number of stars to add."
  19764. (interactive "P")
  19765. (let ((skip-blanks
  19766. (function
  19767. ;; Return beginning of first non-blank line, starting from
  19768. ;; line at POS.
  19769. (lambda (pos)
  19770. (save-excursion
  19771. (goto-char pos)
  19772. (while (org-at-comment-p) (forward-line))
  19773. (skip-chars-forward " \r\t\n")
  19774. (point-at-bol)))))
  19775. beg end toggled)
  19776. ;; Determine boundaries of changes. If a universal prefix has
  19777. ;; been given, put the list in a region. If region ends at a bol,
  19778. ;; do not consider the last line to be in the region.
  19779. (when (and current-prefix-arg (org-at-item-p))
  19780. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19781. (org-mark-element))
  19782. (if (org-region-active-p)
  19783. (setq beg (funcall skip-blanks (region-beginning))
  19784. end (copy-marker (save-excursion
  19785. (goto-char (region-end))
  19786. (if (bolp) (point) (point-at-eol)))))
  19787. (setq beg (funcall skip-blanks (point-at-bol))
  19788. end (copy-marker (point-at-eol))))
  19789. ;; Ensure inline tasks don't count as headings.
  19790. (org-with-limited-levels
  19791. (save-excursion
  19792. (goto-char beg)
  19793. (cond
  19794. ;; Case 1. Started at an heading: de-star headings.
  19795. ((org-at-heading-p)
  19796. (while (< (point) end)
  19797. (when (org-at-heading-p t)
  19798. (looking-at org-outline-regexp) (replace-match "")
  19799. (setq toggled t))
  19800. (forward-line)))
  19801. ;; Case 2. Started at an item: change items into headlines.
  19802. ;; One star will be added by `org-list-to-subtree'.
  19803. ((org-at-item-p)
  19804. (while (< (point) end)
  19805. (when (org-at-item-p)
  19806. ;; Pay attention to cases when region ends before list.
  19807. (let* ((struct (org-list-struct))
  19808. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19809. (save-restriction
  19810. (narrow-to-region (point) list-end)
  19811. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19812. (setq toggled t))
  19813. (forward-line)))
  19814. ;; Case 3. Started at normal text: make every line an heading,
  19815. ;; skipping headlines and items.
  19816. (t (let* ((stars
  19817. (make-string
  19818. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19819. (add-stars
  19820. (cond (nstars "") ; stars from prefix only
  19821. ((equal stars "") "*") ; before first heading
  19822. (org-odd-levels-only "**") ; inside heading, odd
  19823. (t "*"))) ; inside heading, oddeven
  19824. (rpl (concat stars add-stars " "))
  19825. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19826. (while (< (point) (if (equal nstars '(4)) lend end))
  19827. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19828. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19829. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19830. (forward-line)))))))
  19831. (unless toggled (message "Cannot toggle heading from here"))))
  19832. (defun org-meta-return (&optional _arg)
  19833. "Insert a new heading or wrap a region in a table.
  19834. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19835. on context. See the individual commands for more information."
  19836. (interactive)
  19837. (org-check-before-invisible-edit 'insert)
  19838. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19839. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19840. #'org-insert-heading))))
  19841. ;;; Menu entries
  19842. (defsubst org-in-subtree-not-table-p ()
  19843. "Are we in a subtree and not in a table?"
  19844. (and (not (org-before-first-heading-p))
  19845. (not (org-at-table-p))))
  19846. ;; Define the Org-mode menus
  19847. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19848. '("Tbl"
  19849. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19850. ["Next Field" org-cycle (org-at-table-p)]
  19851. ["Previous Field" org-shifttab (org-at-table-p)]
  19852. ["Next Row" org-return (org-at-table-p)]
  19853. "--"
  19854. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19855. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19856. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19857. "--"
  19858. ("Column"
  19859. ["Move Column Left" org-metaleft (org-at-table-p)]
  19860. ["Move Column Right" org-metaright (org-at-table-p)]
  19861. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19862. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19863. ("Row"
  19864. ["Move Row Up" org-metaup (org-at-table-p)]
  19865. ["Move Row Down" org-metadown (org-at-table-p)]
  19866. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19867. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19868. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19869. "--"
  19870. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19871. ("Rectangle"
  19872. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19873. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19874. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19875. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19876. "--"
  19877. ("Calculate"
  19878. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19879. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19880. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19881. "--"
  19882. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19883. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19884. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19885. "--"
  19886. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19887. "--"
  19888. ["Sum Column/Rectangle" org-table-sum
  19889. (or (org-at-table-p) (org-region-active-p))]
  19890. ["Which Column?" org-table-current-column (org-at-table-p)])
  19891. ["Debug Formulas"
  19892. org-table-toggle-formula-debugger
  19893. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19894. ["Show Col/Row Numbers"
  19895. org-table-toggle-coordinate-overlays
  19896. :style toggle
  19897. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19898. "--"
  19899. ["Create" org-table-create (and (not (org-at-table-p))
  19900. org-enable-table-editor)]
  19901. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19902. ["Import from File" org-table-import (not (org-at-table-p))]
  19903. ["Export to File" org-table-export (org-at-table-p)]
  19904. "--"
  19905. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19906. "--"
  19907. ("Plot"
  19908. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19909. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19910. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19911. '("Org"
  19912. ("Show/Hide"
  19913. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19914. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19915. ["Sparse Tree..." org-sparse-tree t]
  19916. ["Reveal Context" org-reveal t]
  19917. ["Show All" show-all t]
  19918. "--"
  19919. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19920. "--"
  19921. ["New Heading" org-insert-heading t]
  19922. ("Navigate Headings"
  19923. ["Up" outline-up-heading t]
  19924. ["Next" outline-next-visible-heading t]
  19925. ["Previous" outline-previous-visible-heading t]
  19926. ["Next Same Level" outline-forward-same-level t]
  19927. ["Previous Same Level" outline-backward-same-level t]
  19928. "--"
  19929. ["Jump" org-goto t])
  19930. ("Edit Structure"
  19931. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19932. "--"
  19933. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19934. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19935. "--"
  19936. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19937. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19938. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19939. "--"
  19940. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19941. "--"
  19942. ["Copy visible text" org-copy-visible t]
  19943. "--"
  19944. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19945. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19946. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19947. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19948. "--"
  19949. ["Sort Region/Children" org-sort t]
  19950. "--"
  19951. ["Convert to odd levels" org-convert-to-odd-levels t]
  19952. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19953. ("Editing"
  19954. ["Emphasis..." org-emphasize t]
  19955. ["Edit Source Example" org-edit-special t]
  19956. "--"
  19957. ["Footnote new/jump" org-footnote-action t]
  19958. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19959. ("Archive"
  19960. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19961. "--"
  19962. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19963. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19964. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19965. )
  19966. "--"
  19967. ("Hyperlinks"
  19968. ["Store Link (Global)" org-store-link t]
  19969. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19970. ["Insert Link" org-insert-link t]
  19971. ["Follow Link" org-open-at-point t]
  19972. "--"
  19973. ["Next link" org-next-link t]
  19974. ["Previous link" org-previous-link t]
  19975. "--"
  19976. ["Descriptive Links"
  19977. org-toggle-link-display
  19978. :style radio
  19979. :selected org-descriptive-links
  19980. ]
  19981. ["Literal Links"
  19982. org-toggle-link-display
  19983. :style radio
  19984. :selected (not org-descriptive-links)])
  19985. "--"
  19986. ("TODO Lists"
  19987. ["TODO/DONE/-" org-todo t]
  19988. ("Select keyword"
  19989. ["Next keyword" org-shiftright (org-at-heading-p)]
  19990. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19991. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19992. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19993. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19994. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19995. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19996. "--"
  19997. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19998. :selected org-enforce-todo-dependencies :style toggle :active t]
  19999. "Settings for tree at point"
  20000. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20001. :selected (org-entry-get nil "ORDERED")
  20002. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20003. ["Do Children parallel" org-toggle-ordered-property :style radio
  20004. :selected (not (org-entry-get nil "ORDERED"))
  20005. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20006. "--"
  20007. ["Set Priority" org-priority t]
  20008. ["Priority Up" org-shiftup t]
  20009. ["Priority Down" org-shiftdown t]
  20010. "--"
  20011. ["Get news from all feeds" org-feed-update-all t]
  20012. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20013. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20014. ("TAGS and Properties"
  20015. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20016. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20017. "--"
  20018. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20019. ["Column view of properties" org-columns t]
  20020. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20021. ("Dates and Scheduling"
  20022. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20023. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20024. ("Change Date"
  20025. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20026. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20027. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20028. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20029. ["Compute Time Range" org-evaluate-time-range t]
  20030. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20031. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20032. "--"
  20033. ["Custom time format" org-toggle-time-stamp-overlays
  20034. :style radio :selected org-display-custom-times]
  20035. "--"
  20036. ["Goto Calendar" org-goto-calendar t]
  20037. ["Date from Calendar" org-date-from-calendar t]
  20038. "--"
  20039. ["Start/Restart Timer" org-timer-start t]
  20040. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20041. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20042. ["Insert Timer String" org-timer t]
  20043. ["Insert Timer Item" org-timer-item t])
  20044. ("Logging work"
  20045. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20046. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20047. ["Clock out" org-clock-out t]
  20048. ["Clock cancel" org-clock-cancel t]
  20049. "--"
  20050. ["Mark as default task" org-clock-mark-default-task t]
  20051. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20052. ["Goto running clock" org-clock-goto t]
  20053. "--"
  20054. ["Display times" org-clock-display t]
  20055. ["Create clock table" org-clock-report t]
  20056. "--"
  20057. ["Record DONE time"
  20058. (progn (setq org-log-done (not org-log-done))
  20059. (message "Switching to %s will %s record a timestamp"
  20060. (car org-done-keywords)
  20061. (if org-log-done "automatically" "not")))
  20062. :style toggle :selected org-log-done])
  20063. "--"
  20064. ["Agenda Command..." org-agenda t]
  20065. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20066. ("File List for Agenda")
  20067. ("Special views current file"
  20068. ["TODO Tree" org-show-todo-tree t]
  20069. ["Check Deadlines" org-check-deadlines t]
  20070. ["Timeline" org-timeline t]
  20071. ["Tags/Property tree" org-match-sparse-tree t])
  20072. "--"
  20073. ["Export/Publish..." org-export-dispatch t]
  20074. ("LaTeX"
  20075. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20076. :selected org-cdlatex-mode]
  20077. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20078. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20079. ["Modify math symbol" org-cdlatex-math-modify
  20080. (org-inside-LaTeX-fragment-p)]
  20081. ["Insert citation" org-reftex-citation t]
  20082. "--"
  20083. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20084. "--"
  20085. ("MobileOrg"
  20086. ["Push Files and Views" org-mobile-push t]
  20087. ["Get Captured and Flagged" org-mobile-pull t]
  20088. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20089. "--"
  20090. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20091. "--"
  20092. ("Documentation"
  20093. ["Show Version" org-version t]
  20094. ["Info Documentation" org-info t])
  20095. ("Customize"
  20096. ["Browse Org Group" org-customize t]
  20097. "--"
  20098. ["Expand This Menu" org-create-customize-menu
  20099. (fboundp 'customize-menu-create)])
  20100. ["Send bug report" org-submit-bug-report t]
  20101. "--"
  20102. ("Refresh/Reload"
  20103. ["Refresh setup current buffer" org-mode-restart t]
  20104. ["Reload Org (after update)" org-reload t]
  20105. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20106. ))
  20107. (defun org-info (&optional node)
  20108. "Read documentation for Org-mode in the info system.
  20109. With optional NODE, go directly to that node."
  20110. (interactive)
  20111. (info (format "(org)%s" (or node ""))))
  20112. ;;;###autoload
  20113. (defun org-submit-bug-report ()
  20114. "Submit a bug report on Org-mode via mail.
  20115. Don't hesitate to report any problems or inaccurate documentation.
  20116. If you don't have setup sending mail from (X)Emacs, please copy the
  20117. output buffer into your mail program, as it gives us important
  20118. information about your Org-mode version and configuration."
  20119. (interactive)
  20120. (require 'reporter)
  20121. (defvar reporter-prompt-for-summary-p)
  20122. (org-load-modules-maybe)
  20123. (org-require-autoloaded-modules)
  20124. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20125. (reporter-submit-bug-report
  20126. "emacs-orgmode@gnu.org"
  20127. (org-version nil 'full)
  20128. (let (list)
  20129. (save-window-excursion
  20130. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20131. (delete-other-windows)
  20132. (erase-buffer)
  20133. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20134. We would like to add your full Org-mode and Outline configuration to the
  20135. bug report. This greatly simplifies the work of the maintainer and
  20136. other experts on the mailing list.
  20137. HOWEVER, some variables you have customized may contain private
  20138. information. The names of customers, colleagues, or friends, might
  20139. appear in the form of file names, tags, todo states, or search strings.
  20140. If you answer yes to the prompt, you might want to check and remove
  20141. such private information before sending the email.")
  20142. (add-text-properties (point-min) (point-max) '(face org-warning))
  20143. (when (yes-or-no-p "Include your Org-mode configuration ")
  20144. (mapatoms
  20145. (lambda (v)
  20146. (and (boundp v)
  20147. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20148. (or (and (symbol-value v)
  20149. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20150. (and
  20151. (get v 'custom-type) (get v 'standard-value)
  20152. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20153. (push v list)))))
  20154. (kill-buffer (get-buffer "*Warn about privacy*"))
  20155. list))
  20156. nil nil
  20157. "Remember to cover the basics, that is, what you expected to happen and
  20158. what in fact did happen. You don't know how to make a good report? See
  20159. http://orgmode.org/manual/Feedback.html#Feedback
  20160. Your bug report will be posted to the Org-mode mailing list.
  20161. ------------------------------------------------------------------------")
  20162. (save-excursion
  20163. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20164. (replace-match "\\1Bug: \\3 [\\2]")))))
  20165. (defun org-install-agenda-files-menu ()
  20166. (let ((bl (buffer-list)))
  20167. (save-excursion
  20168. (while bl
  20169. (set-buffer (pop bl))
  20170. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20171. (when (derived-mode-p 'org-mode)
  20172. (easy-menu-change
  20173. '("Org") "File List for Agenda"
  20174. (append
  20175. (list
  20176. ["Edit File List" (org-edit-agenda-file-list) t]
  20177. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20178. ["Remove Current File from List" org-remove-file t]
  20179. ["Cycle through agenda files" org-cycle-agenda-files t]
  20180. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20181. "--")
  20182. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20183. ;;;; Documentation
  20184. (defun org-require-autoloaded-modules ()
  20185. (interactive)
  20186. (mapc 'require
  20187. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20188. org-table org-timer)))
  20189. ;;;###autoload
  20190. (defun org-reload (&optional uncompiled)
  20191. "Reload all org lisp files.
  20192. With prefix arg UNCOMPILED, load the uncompiled versions."
  20193. (interactive "P")
  20194. (require 'loadhist)
  20195. (let* ((org-dir (org-find-library-dir "org"))
  20196. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20197. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20198. (remove-re (mapconcat 'identity
  20199. (mapcar (lambda (f) (concat "^" f "$"))
  20200. (list (if (featurep 'xemacs)
  20201. "org-colview"
  20202. "org-colview-xemacs")
  20203. "org" "org-loaddefs" "org-version"))
  20204. "\\|"))
  20205. (feats (delete-dups
  20206. (mapcar 'file-name-sans-extension
  20207. (mapcar 'file-name-nondirectory
  20208. (delq nil
  20209. (mapcar 'feature-file
  20210. features))))))
  20211. (lfeat (append
  20212. (sort
  20213. (setq feats
  20214. (delq nil (mapcar
  20215. (lambda (f)
  20216. (if (and (string-match feature-re f)
  20217. (not (string-match remove-re f)))
  20218. f nil))
  20219. feats)))
  20220. 'string-lessp)
  20221. (list "org-version" "org")))
  20222. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20223. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20224. load-uncore load-misses)
  20225. (setq load-misses
  20226. (delq 't
  20227. (mapcar (lambda (f)
  20228. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20229. (and (string= org-dir contrib-dir)
  20230. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20231. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20232. (add-to-list 'load-uncore f 'append)
  20233. 't)
  20234. f))
  20235. lfeat)))
  20236. (if load-uncore
  20237. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20238. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20239. (if load-misses
  20240. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20241. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20242. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20243. ;;;###autoload
  20244. (defun org-customize ()
  20245. "Call the customize function with org as argument."
  20246. (interactive)
  20247. (org-load-modules-maybe)
  20248. (org-require-autoloaded-modules)
  20249. (customize-browse 'org))
  20250. (defun org-create-customize-menu ()
  20251. "Create a full customization menu for Org-mode, insert it into the menu."
  20252. (interactive)
  20253. (org-load-modules-maybe)
  20254. (org-require-autoloaded-modules)
  20255. (if (fboundp 'customize-menu-create)
  20256. (progn
  20257. (easy-menu-change
  20258. '("Org") "Customize"
  20259. `(["Browse Org group" org-customize t]
  20260. "--"
  20261. ,(customize-menu-create 'org)
  20262. ["Set" Custom-set t]
  20263. ["Save" Custom-save t]
  20264. ["Reset to Current" Custom-reset-current t]
  20265. ["Reset to Saved" Custom-reset-saved t]
  20266. ["Reset to Standard Settings" Custom-reset-standard t]))
  20267. (message "\"Org\"-menu now contains full customization menu"))
  20268. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20269. ;;;; Miscellaneous stuff
  20270. ;;; Generally useful functions
  20271. (defsubst org-get-at-eol (property n)
  20272. "Get text property PROPERTY at the end of line less N characters."
  20273. (get-text-property (- (point-at-eol) n) property))
  20274. (defun org-find-text-property-in-string (prop s)
  20275. "Return the first non-nil value of property PROP in string S."
  20276. (or (get-text-property 0 prop s)
  20277. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20278. prop s)))
  20279. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20280. "Display the given MESSAGE as a warning."
  20281. (if (fboundp 'display-warning)
  20282. (display-warning 'org message
  20283. (if (featurep 'xemacs) 'warning :warning))
  20284. (let ((buf (get-buffer-create "*Org warnings*")))
  20285. (with-current-buffer buf
  20286. (goto-char (point-max))
  20287. (insert "Warning (Org): " message)
  20288. (unless (bolp)
  20289. (newline)))
  20290. (display-buffer buf)
  20291. (sit-for 0))))
  20292. (defun org-eval (form)
  20293. "Eval FORM and return result."
  20294. (condition-case error
  20295. (eval form)
  20296. (error (format "%%![Error: %s]" error))))
  20297. (defun org-in-clocktable-p ()
  20298. "Check if the cursor is in a clocktable."
  20299. (let ((pos (point)) start)
  20300. (save-excursion
  20301. (end-of-line 1)
  20302. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20303. (setq start (match-beginning 0))
  20304. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20305. (>= (match-end 0) pos)
  20306. start))))
  20307. (defun org-in-verbatim-emphasis ()
  20308. (save-match-data
  20309. (and (org-in-regexp org-emph-re 2)
  20310. (>= (point) (match-beginning 3))
  20311. (<= (point) (match-end 4))
  20312. (member (match-string 3) '("=" "~")))))
  20313. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20314. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20315. (if (and marker (marker-buffer marker)
  20316. (buffer-live-p (marker-buffer marker)))
  20317. (progn
  20318. (org-pop-to-buffer-same-window (marker-buffer marker))
  20319. (if (or (> marker (point-max)) (< marker (point-min)))
  20320. (widen))
  20321. (goto-char marker)
  20322. (org-show-context 'org-goto))
  20323. (if bookmark
  20324. (bookmark-jump bookmark)
  20325. (error "Cannot find location"))))
  20326. (defun org-quote-csv-field (s)
  20327. "Quote field for inclusion in CSV material."
  20328. (if (string-match "[\",]" s)
  20329. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20330. s))
  20331. (defun org-force-self-insert (N)
  20332. "Needed to enforce self-insert under remapping."
  20333. (interactive "p")
  20334. (self-insert-command N))
  20335. (defun org-string-width (s)
  20336. "Compute width of string, ignoring invisible characters.
  20337. This ignores character with invisibility property `org-link', and also
  20338. characters with property `org-cwidth', because these will become invisible
  20339. upon the next fontification round."
  20340. (let (b l)
  20341. (when (or (eq t buffer-invisibility-spec)
  20342. (assq 'org-link buffer-invisibility-spec))
  20343. (while (setq b (text-property-any 0 (length s)
  20344. 'invisible 'org-link s))
  20345. (setq s (concat (substring s 0 b)
  20346. (substring s (or (next-single-property-change
  20347. b 'invisible s)
  20348. (length s)))))))
  20349. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20350. (setq s (concat (substring s 0 b)
  20351. (substring s (or (next-single-property-change
  20352. b 'org-cwidth s)
  20353. (length s))))))
  20354. (setq l (string-width s) b -1)
  20355. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20356. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20357. l))
  20358. (defun org-shorten-string (s maxlength)
  20359. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20360. If the string is shorter or has length MAXLENGTH, just return the
  20361. original string. If it is longer, the functions finds a space in the
  20362. string, breaks this string off at that locations and adds three dots
  20363. as ellipsis. Including the ellipsis, the string will not be longer
  20364. than MAXLENGTH. If finding a good breaking point in the string does
  20365. not work, the string is just chopped off in the middle of a word
  20366. if necessary."
  20367. (if (<= (length s) maxlength)
  20368. s
  20369. (let* ((n (max (- maxlength 4) 1))
  20370. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20371. (if (string-match re s)
  20372. (concat (match-string 1 s) "...")
  20373. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20374. (defun org-get-indentation (&optional line)
  20375. "Get the indentation of the current line, interpreting tabs.
  20376. When LINE is given, assume it represents a line and compute its indentation."
  20377. (if line
  20378. (if (string-match "^ *" (org-remove-tabs line))
  20379. (match-end 0))
  20380. (save-excursion
  20381. (beginning-of-line 1)
  20382. (skip-chars-forward " \t")
  20383. (current-column))))
  20384. (defun org-get-string-indentation (s)
  20385. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20386. (let ((n -1) (i 0) (w tab-width) c)
  20387. (catch 'exit
  20388. (while (< (setq n (1+ n)) (length s))
  20389. (setq c (aref s n))
  20390. (cond ((= c ?\ ) (setq i (1+ i)))
  20391. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20392. (t (throw 'exit t)))))
  20393. i))
  20394. (defun org-remove-tabs (s &optional width)
  20395. "Replace tabulators in S with spaces.
  20396. Assumes that s is a single line, starting in column 0."
  20397. (setq width (or width tab-width))
  20398. (while (string-match "\t" s)
  20399. (setq s (replace-match
  20400. (make-string
  20401. (- (* width (/ (+ (match-beginning 0) width) width))
  20402. (match-beginning 0)) ?\ )
  20403. t t s)))
  20404. s)
  20405. (defun org-fix-indentation (line ind)
  20406. "Fix indentation in LINE.
  20407. IND is a cons cell with target and minimum indentation.
  20408. If the current indentation in LINE is smaller than the minimum,
  20409. leave it alone. If it is larger than ind, set it to the target."
  20410. (let* ((l (org-remove-tabs line))
  20411. (i (org-get-indentation l))
  20412. (i1 (car ind)) (i2 (cdr ind)))
  20413. (if (>= i i2) (setq l (substring line i2)))
  20414. (if (> i1 0)
  20415. (concat (make-string i1 ?\ ) l)
  20416. l)))
  20417. (defun org-remove-indentation (code &optional n)
  20418. "Remove the maximum common indentation from the lines in CODE.
  20419. N may optionally be the number of spaces to remove."
  20420. (with-temp-buffer
  20421. (insert code)
  20422. (org-do-remove-indentation n)
  20423. (buffer-string)))
  20424. (defun org-do-remove-indentation (&optional n)
  20425. "Remove the maximum common indentation from the buffer."
  20426. (untabify (point-min) (point-max))
  20427. (let ((min 10000) re)
  20428. (if n
  20429. (setq min n)
  20430. (goto-char (point-min))
  20431. (while (re-search-forward "^ *[^ \n]" nil t)
  20432. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20433. (unless (or (= min 0) (= min 10000))
  20434. (setq re (format "^ \\{%d\\}" min))
  20435. (goto-char (point-min))
  20436. (while (re-search-forward re nil t)
  20437. (replace-match "")
  20438. (end-of-line 1))
  20439. min)))
  20440. (defun org-fill-template (template alist)
  20441. "Find each %key of ALIST in TEMPLATE and replace it."
  20442. (let ((case-fold-search nil))
  20443. (dolist (entry (sort (copy-sequence alist)
  20444. (lambda (a b) (< (length (car a)) (length (car b))))))
  20445. (setq template
  20446. (replace-regexp-in-string
  20447. (concat "%" (regexp-quote (car entry)))
  20448. (or (cdr entry) "") template t t)))
  20449. template))
  20450. (defun org-base-buffer (buffer)
  20451. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20452. (if (not buffer)
  20453. buffer
  20454. (or (buffer-base-buffer buffer)
  20455. buffer)))
  20456. (defun org-wrap (string &optional width lines)
  20457. "Wrap string to either a number of lines, or a width in characters.
  20458. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20459. that costs. If there is a word longer than WIDTH, the text is actually
  20460. wrapped to the length of that word.
  20461. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20462. many lines, whatever width that takes.
  20463. The return value is a list of lines, without newlines at the end."
  20464. (let* ((words (org-split-string string "[ \t\n]+"))
  20465. (maxword (apply 'max (mapcar 'org-string-width words)))
  20466. w ll)
  20467. (cond (width
  20468. (org-do-wrap words (max maxword width)))
  20469. (lines
  20470. (setq w maxword)
  20471. (setq ll (org-do-wrap words maxword))
  20472. (if (<= (length ll) lines)
  20473. ll
  20474. (setq ll words)
  20475. (while (> (length ll) lines)
  20476. (setq w (1+ w))
  20477. (setq ll (org-do-wrap words w)))
  20478. ll))
  20479. (t (error "Cannot wrap this")))))
  20480. (defun org-do-wrap (words width)
  20481. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20482. (let (lines line)
  20483. (while words
  20484. (setq line (pop words))
  20485. (while (and words (< (+ (length line) (length (car words))) width))
  20486. (setq line (concat line " " (pop words))))
  20487. (setq lines (push line lines)))
  20488. (nreverse lines)))
  20489. (defun org-split-string (string &optional separators)
  20490. "Splits STRING into substrings at SEPARATORS.
  20491. SEPARATORS is a regular expression.
  20492. No empty strings are returned if there are matches at the beginning
  20493. and end of string."
  20494. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20495. (let ((start 0) notfirst list)
  20496. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20497. (if (and notfirst
  20498. (= start (match-beginning 0))
  20499. (< start (length string)))
  20500. (1+ start) start))
  20501. (< (match-beginning 0) (length string)))
  20502. (setq notfirst t)
  20503. (or (eq (match-beginning 0) 0)
  20504. (and (eq (match-beginning 0) (match-end 0))
  20505. (eq (match-beginning 0) start))
  20506. (push (substring string start (match-beginning 0)) list))
  20507. (setq start (match-end 0)))
  20508. (or (eq start (length string))
  20509. (push (substring string start) list))
  20510. (nreverse list)))
  20511. (defun org-quote-vert (s)
  20512. "Replace \"|\" with \"\\vert\"."
  20513. (while (string-match "|" s)
  20514. (setq s (replace-match "\\vert" t t s)))
  20515. s)
  20516. (defun org-uuidgen-p (s)
  20517. "Is S an ID created by UUIDGEN?"
  20518. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20519. (defun org-in-src-block-p (&optional inside)
  20520. "Whether point is in a code source block.
  20521. When INSIDE is non-nil, don't consider we are within a src block
  20522. when point is at #+BEGIN_SRC or #+END_SRC."
  20523. (let ((case-fold-search t) ov)
  20524. (or (and (eq (get-char-property (point) 'src-block) t))
  20525. (and (not inside)
  20526. (save-match-data
  20527. (save-excursion
  20528. (beginning-of-line)
  20529. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20530. (defun org-context ()
  20531. "Return a list of contexts of the current cursor position.
  20532. If several contexts apply, all are returned.
  20533. Each context entry is a list with a symbol naming the context, and
  20534. two positions indicating start and end of the context. Possible
  20535. contexts are:
  20536. :headline anywhere in a headline
  20537. :headline-stars on the leading stars in a headline
  20538. :todo-keyword on a TODO keyword (including DONE) in a headline
  20539. :tags on the TAGS in a headline
  20540. :priority on the priority cookie in a headline
  20541. :item on the first line of a plain list item
  20542. :item-bullet on the bullet/number of a plain list item
  20543. :checkbox on the checkbox in a plain list item
  20544. :table in an org-mode table
  20545. :table-special on a special filed in a table
  20546. :table-table in a table.el table
  20547. :clocktable in a clocktable
  20548. :src-block in a source block
  20549. :link on a hyperlink
  20550. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20551. :target on a <<target>>
  20552. :radio-target on a <<<radio-target>>>
  20553. :latex-fragment on a LaTeX fragment
  20554. :latex-preview on a LaTeX fragment with overlaid preview image
  20555. This function expects the position to be visible because it uses font-lock
  20556. faces as a help to recognize the following contexts: :table-special, :link,
  20557. and :keyword."
  20558. (let* ((f (get-text-property (point) 'face))
  20559. (faces (if (listp f) f (list f)))
  20560. (case-fold-search t)
  20561. (p (point)) clist o)
  20562. ;; First the large context
  20563. (cond
  20564. ((org-at-heading-p t)
  20565. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20566. (when (progn
  20567. (beginning-of-line 1)
  20568. (looking-at org-todo-line-tags-regexp))
  20569. (push (org-point-in-group p 1 :headline-stars) clist)
  20570. (push (org-point-in-group p 2 :todo-keyword) clist)
  20571. (push (org-point-in-group p 4 :tags) clist))
  20572. (goto-char p)
  20573. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20574. (if (looking-at "\\[#[A-Z0-9]\\]")
  20575. (push (org-point-in-group p 0 :priority) clist)))
  20576. ((org-at-item-p)
  20577. (push (org-point-in-group p 2 :item-bullet) clist)
  20578. (push (list :item (point-at-bol)
  20579. (save-excursion (org-end-of-item) (point)))
  20580. clist)
  20581. (and (org-at-item-checkbox-p)
  20582. (push (org-point-in-group p 0 :checkbox) clist)))
  20583. ((org-at-table-p)
  20584. (push (list :table (org-table-begin) (org-table-end)) clist)
  20585. (if (memq 'org-formula faces)
  20586. (push (list :table-special
  20587. (previous-single-property-change p 'face)
  20588. (next-single-property-change p 'face)) clist)))
  20589. ((org-at-table-p 'any)
  20590. (push (list :table-table) clist)))
  20591. (goto-char p)
  20592. (let ((case-fold-search t))
  20593. ;; New the "medium" contexts: clocktables, source blocks
  20594. (cond ((org-in-clocktable-p)
  20595. (push (list :clocktable
  20596. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20597. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20598. (match-beginning 1))
  20599. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20600. (match-end 0))) clist))
  20601. ((org-in-src-block-p)
  20602. (push (list :src-block
  20603. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20604. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20605. (match-beginning 1))
  20606. (and (search-forward "#+END_SRC" nil t)
  20607. (match-beginning 0))) clist))))
  20608. (goto-char p)
  20609. ;; Now the small context
  20610. (cond
  20611. ((org-at-timestamp-p)
  20612. (push (org-point-in-group p 0 :timestamp) clist))
  20613. ((memq 'org-link faces)
  20614. (push (list :link
  20615. (previous-single-property-change p 'face)
  20616. (next-single-property-change p 'face)) clist))
  20617. ((memq 'org-special-keyword faces)
  20618. (push (list :keyword
  20619. (previous-single-property-change p 'face)
  20620. (next-single-property-change p 'face)) clist))
  20621. ((org-at-target-p)
  20622. (push (org-point-in-group p 0 :target) clist)
  20623. (goto-char (1- (match-beginning 0)))
  20624. (if (looking-at org-radio-target-regexp)
  20625. (push (org-point-in-group p 0 :radio-target) clist))
  20626. (goto-char p))
  20627. ((setq o (car (delq nil
  20628. (mapcar
  20629. (lambda (x)
  20630. (if (memq x org-latex-fragment-image-overlays) x))
  20631. (overlays-at (point))))))
  20632. (push (list :latex-fragment
  20633. (overlay-start o) (overlay-end o)) clist)
  20634. (push (list :latex-preview
  20635. (overlay-start o) (overlay-end o)) clist))
  20636. ((org-inside-LaTeX-fragment-p)
  20637. ;; FIXME: positions wrong.
  20638. (push (list :latex-fragment (point) (point)) clist)))
  20639. (setq clist (nreverse (delq nil clist)))
  20640. clist))
  20641. (defun org-in-regexp (re &optional nlines visually)
  20642. "Check if point is inside a match of RE.
  20643. Normally only the current line is checked, but you can include
  20644. NLINES extra lines after point into the search. If VISUALLY is
  20645. set, require that the cursor is not after the match but really
  20646. on, so that the block visually is on the match."
  20647. (catch 'exit
  20648. (let ((pos (point))
  20649. (eol (point-at-eol (+ 1 (or nlines 0))))
  20650. (inc (if visually 1 0)))
  20651. (save-excursion
  20652. (beginning-of-line (- 1 (or nlines 0)))
  20653. (while (re-search-forward re eol t)
  20654. (if (and (<= (match-beginning 0) pos)
  20655. (>= (+ inc (match-end 0)) pos))
  20656. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20657. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20658. "Org mode 8.3")
  20659. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20660. "Non-nil when point is between matches of START-RE and END-RE.
  20661. Also return a non-nil value when point is on one of the matches.
  20662. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20663. buffer positions. Default values are the positions of headlines
  20664. surrounding the point.
  20665. The functions returns a cons cell whose car (resp. cdr) is the
  20666. position before START-RE (resp. after END-RE)."
  20667. (save-match-data
  20668. (let ((pos (point))
  20669. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20670. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20671. beg end)
  20672. (save-excursion
  20673. ;; Point is on a block when on START-RE or if START-RE can be
  20674. ;; found before it...
  20675. (and (or (org-in-regexp start-re)
  20676. (re-search-backward start-re limit-up t))
  20677. (setq beg (match-beginning 0))
  20678. ;; ... and END-RE after it...
  20679. (goto-char (match-end 0))
  20680. (re-search-forward end-re limit-down t)
  20681. (> (setq end (match-end 0)) pos)
  20682. ;; ... without another START-RE in-between.
  20683. (goto-char (match-beginning 0))
  20684. (not (re-search-backward start-re (1+ beg) t))
  20685. ;; Return value.
  20686. (cons beg end))))))
  20687. (defun org-in-block-p (names)
  20688. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20689. NAMES is a list of strings containing names of blocks.
  20690. Return first block name matched, or nil. Beware that in case of
  20691. nested blocks, the returned name may not belong to the closest
  20692. block from point."
  20693. (save-match-data
  20694. (catch 'exit
  20695. (let ((case-fold-search t)
  20696. (lim-up (save-excursion (outline-previous-heading)))
  20697. (lim-down (save-excursion (outline-next-heading))))
  20698. (mapc (lambda (name)
  20699. (let ((n (regexp-quote name)))
  20700. (when (org-between-regexps-p
  20701. (concat "^[ \t]*#\\+begin_" n)
  20702. (concat "^[ \t]*#\\+end_" n)
  20703. lim-up lim-down)
  20704. (throw 'exit n))))
  20705. names))
  20706. nil)))
  20707. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20708. "Call `multi-occur' with buffers for all agenda files."
  20709. (interactive "sOrg-files matching: ")
  20710. (let* ((files (org-agenda-files))
  20711. (tnames (mapcar #'file-truename files))
  20712. (extra org-agenda-text-search-extra-files))
  20713. (when (eq (car extra) 'agenda-archives)
  20714. (setq extra (cdr extra))
  20715. (setq files (org-add-archive-files files)))
  20716. (dolist (f extra)
  20717. (unless (member (file-truename f) tnames)
  20718. (unless (member f files) (setq files (append files (list f))))
  20719. (setq tnames (append tnames (list (file-truename f))))))
  20720. (multi-occur
  20721. (mapcar (lambda (x)
  20722. (with-current-buffer
  20723. ;; FIXME: Why not just (find-file-noselect x)?
  20724. ;; Is it to avoid the "revert buffer" prompt?
  20725. (or (get-file-buffer x) (find-file-noselect x))
  20726. (widen)
  20727. (current-buffer)))
  20728. files)
  20729. regexp)))
  20730. (if (boundp 'occur-mode-find-occurrence-hook)
  20731. ;; Emacs 23
  20732. (add-hook 'occur-mode-find-occurrence-hook
  20733. (lambda ()
  20734. (when (derived-mode-p 'org-mode)
  20735. (org-reveal))))
  20736. ;; Emacs 22
  20737. (defadvice occur-mode-goto-occurrence
  20738. (after org-occur-reveal activate)
  20739. (and (derived-mode-p 'org-mode) (org-reveal)))
  20740. (defadvice occur-mode-goto-occurrence-other-window
  20741. (after org-occur-reveal activate)
  20742. (and (derived-mode-p 'org-mode) (org-reveal)))
  20743. (defadvice occur-mode-display-occurrence
  20744. (after org-occur-reveal activate)
  20745. (when (derived-mode-p 'org-mode)
  20746. (let ((pos (occur-mode-find-occurrence)))
  20747. (with-current-buffer (marker-buffer pos)
  20748. (save-excursion
  20749. (goto-char pos)
  20750. (org-reveal)))))))
  20751. (defun org-occur-link-in-agenda-files ()
  20752. "Create a link and search for it in the agendas.
  20753. The link is not stored in `org-stored-links', it is just created
  20754. for the search purpose."
  20755. (interactive)
  20756. (let ((link (condition-case nil
  20757. (org-store-link nil)
  20758. (error "Unable to create a link to here"))))
  20759. (org-occur-in-agenda-files (regexp-quote link))))
  20760. (defun org-reverse-string (string)
  20761. "Return the reverse of STRING."
  20762. (apply 'string (reverse (string-to-list string))))
  20763. ;; defsubst org-uniquify must be defined before first use
  20764. (defun org-uniquify-alist (alist)
  20765. "Merge elements of ALIST with the same key.
  20766. For example, in this alist:
  20767. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20768. => '((a 1 3) (b 2))
  20769. merge (a 1) and (a 3) into (a 1 3).
  20770. The function returns the new ALIST."
  20771. (let (rtn)
  20772. (mapc
  20773. (lambda (e)
  20774. (let (n)
  20775. (if (not (assoc (car e) rtn))
  20776. (push e rtn)
  20777. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20778. (setq rtn (assq-delete-all (car e) rtn))
  20779. (push n rtn))))
  20780. alist)
  20781. rtn))
  20782. (defun org-delete-all (elts list)
  20783. "Remove all elements in ELTS from LIST."
  20784. (while elts
  20785. (setq list (delete (pop elts) list)))
  20786. list)
  20787. (defun org-count (cl-item cl-seq)
  20788. "Count the number of occurrences of ITEM in SEQ.
  20789. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20790. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20791. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20792. (while (< cl-start cl-end)
  20793. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20794. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20795. (setq cl-start (1+ cl-start)))
  20796. cl-count))
  20797. (defun org-remove-if (predicate seq)
  20798. "Remove everything from SEQ that fulfills PREDICATE."
  20799. (let (res e)
  20800. (while seq
  20801. (setq e (pop seq))
  20802. (if (not (funcall predicate e)) (push e res)))
  20803. (nreverse res)))
  20804. (defun org-remove-if-not (predicate seq)
  20805. "Remove everything from SEQ that does not fulfill PREDICATE."
  20806. (let (res e)
  20807. (while seq
  20808. (setq e (pop seq))
  20809. (if (funcall predicate e) (push e res)))
  20810. (nreverse res)))
  20811. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20812. "Reduce two-argument FUNCTION across SEQ.
  20813. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20814. \":initial-value\" removed."
  20815. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20816. (cadr (memq :initial-value cl-keys)))
  20817. (cl-seq (pop cl-seq))
  20818. (t (funcall cl-func)))))
  20819. (while cl-seq
  20820. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20821. cl-accum))
  20822. (defun org-every (pred seq)
  20823. "Return true if PREDICATE is true of every element of SEQ.
  20824. Adapted from `every' in cl.el."
  20825. (catch 'org-every
  20826. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20827. t))
  20828. (defun org-some (pred seq)
  20829. "Return true if PREDICATE is true of any element of SEQ.
  20830. Adapted from `some' in cl.el."
  20831. (catch 'org-some
  20832. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20833. nil))
  20834. (defun org-back-over-empty-lines ()
  20835. "Move backwards over whitespace, to the beginning of the first empty line.
  20836. Returns the number of empty lines passed."
  20837. (let ((pos (point)))
  20838. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20839. (skip-chars-backward " \t\n\r")
  20840. (unless (eobp)
  20841. (forward-line -1)))
  20842. (beginning-of-line 2)
  20843. (goto-char (min (point) pos))
  20844. (count-lines (point) pos)))
  20845. (defun org-skip-whitespace ()
  20846. (skip-chars-forward " \t\n\r"))
  20847. (defun org-point-in-group (point group &optional context)
  20848. "Check if POINT is in match-group GROUP.
  20849. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20850. match. If the match group does not exist or point is not inside it,
  20851. return nil."
  20852. (and (match-beginning group)
  20853. (>= point (match-beginning group))
  20854. (<= point (match-end group))
  20855. (if context
  20856. (list context (match-beginning group) (match-end group))
  20857. t)))
  20858. (defun org-switch-to-buffer-other-window (&rest args)
  20859. "Switch to buffer in a second window on the current frame.
  20860. In particular, do not allow pop-up frames.
  20861. Returns the newly created buffer."
  20862. (org-no-popups
  20863. (apply 'switch-to-buffer-other-window args)))
  20864. (defun org-combine-plists (&rest plists)
  20865. "Create a single property list from all plists in PLISTS.
  20866. The process starts by copying the first list, and then setting properties
  20867. from the other lists. Settings in the last list are the most significant
  20868. ones and overrule settings in the other lists."
  20869. (let ((rtn (copy-sequence (pop plists)))
  20870. p v ls)
  20871. (while plists
  20872. (setq ls (pop plists))
  20873. (while ls
  20874. (setq p (pop ls) v (pop ls))
  20875. (setq rtn (plist-put rtn p v))))
  20876. rtn))
  20877. (defun org-replace-escapes (string table)
  20878. "Replace %-escapes in STRING with values in TABLE.
  20879. TABLE is an association list with keys like \"%a\" and string values.
  20880. The sequences in STRING may contain normal field width and padding information,
  20881. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20882. so values can contain further %-escapes if they are define later in TABLE."
  20883. (let ((tbl (copy-alist table))
  20884. (case-fold-search nil)
  20885. (pchg 0)
  20886. e re rpl)
  20887. (dolist (e tbl)
  20888. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20889. (when (and (cdr e) (string-match re (cdr e)))
  20890. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20891. (safe "SREF"))
  20892. (add-text-properties 0 3 (list 'sref sref) safe)
  20893. (setcdr e (replace-match safe t t (cdr e)))))
  20894. (while (string-match re string)
  20895. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20896. (cdr e)))
  20897. (setq string (replace-match rpl t t string))))
  20898. (while (setq pchg (next-property-change pchg string))
  20899. (let ((sref (get-text-property pchg 'sref string)))
  20900. (when (and sref (string-match "SREF" string pchg))
  20901. (setq string (replace-match sref t t string)))))
  20902. string))
  20903. (defun org-sublist (list start end)
  20904. "Return a section of LIST, from START to END.
  20905. Counting starts at 1."
  20906. (let (rtn (c start))
  20907. (setq list (nthcdr (1- start) list))
  20908. (while (and list (<= c end))
  20909. (push (pop list) rtn)
  20910. (setq c (1+ c)))
  20911. (nreverse rtn)))
  20912. (defun org-find-base-buffer-visiting (file)
  20913. "Like `find-buffer-visiting' but always return the base buffer and
  20914. not an indirect buffer."
  20915. (let ((buf (or (get-file-buffer file)
  20916. (find-buffer-visiting file))))
  20917. (if buf
  20918. (or (buffer-base-buffer buf) buf)
  20919. nil)))
  20920. (defun org-image-file-name-regexp (&optional extensions)
  20921. "Return regexp matching the file names of images.
  20922. If EXTENSIONS is given, only match these."
  20923. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20924. (image-file-name-regexp)
  20925. (let ((image-file-name-extensions
  20926. (or extensions
  20927. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20928. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20929. (concat "\\."
  20930. (regexp-opt (nconc (mapcar 'upcase
  20931. image-file-name-extensions)
  20932. image-file-name-extensions)
  20933. t)
  20934. "\\'"))))
  20935. (defun org-file-image-p (file &optional extensions)
  20936. "Return non-nil if FILE is an image."
  20937. (save-match-data
  20938. (string-match (org-image-file-name-regexp extensions) file)))
  20939. (defun org-get-cursor-date (&optional with-time)
  20940. "Return the date at cursor in as a time.
  20941. This works in the calendar and in the agenda, anywhere else it just
  20942. returns the current time.
  20943. If WITH-TIME is non-nil, returns the time of the event at point (in
  20944. the agenda) or the current time of the day."
  20945. (let (date day defd tp hod mod)
  20946. (when with-time
  20947. (setq tp (get-text-property (point) 'time))
  20948. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20949. (setq hod (string-to-number (match-string 1 tp))
  20950. mod (string-to-number (match-string 2 tp))))
  20951. (or tp (let ((now (decode-time)))
  20952. (setq hod (nth 2 now)
  20953. mod (nth 1 now)))))
  20954. (cond
  20955. ((eq major-mode 'calendar-mode)
  20956. (setq date (calendar-cursor-to-date)
  20957. defd (encode-time 0 (or mod 0) (or hod 0)
  20958. (nth 1 date) (nth 0 date) (nth 2 date))))
  20959. ((eq major-mode 'org-agenda-mode)
  20960. (setq day (get-text-property (point) 'day))
  20961. (if day
  20962. (setq date (calendar-gregorian-from-absolute day)
  20963. defd (encode-time 0 (or mod 0) (or hod 0)
  20964. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20965. (or defd (current-time))))
  20966. (defun org-mark-subtree (&optional up)
  20967. "Mark the current subtree.
  20968. This puts point at the start of the current subtree, and mark at
  20969. the end. If a numeric prefix UP is given, move up into the
  20970. hierarchy of headlines by UP levels before marking the subtree."
  20971. (interactive "P")
  20972. (org-with-limited-levels
  20973. (cond ((org-at-heading-p) (beginning-of-line))
  20974. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20975. (t (outline-previous-visible-heading 1))))
  20976. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20977. (if (org-called-interactively-p 'any)
  20978. (call-interactively 'org-mark-element)
  20979. (org-mark-element)))
  20980. ;;; Indentation
  20981. (defun org--get-expected-indentation (element contentsp)
  20982. "Expected indentation column for current line, according to ELEMENT.
  20983. ELEMENT is an element containing point. CONTENTSP is non-nil
  20984. when indentation is to be computed according to contents of
  20985. ELEMENT."
  20986. (let ((type (org-element-type element))
  20987. (start (org-element-property :begin element))
  20988. (post-affiliated (org-element-property :post-affiliated element)))
  20989. (org-with-wide-buffer
  20990. (cond
  20991. (contentsp
  20992. (case type
  20993. ((diary-sexp footnote-definition) 0)
  20994. ((headline inlinetask nil)
  20995. (if (not org-adapt-indentation) 0
  20996. (let ((level (org-current-level)))
  20997. (if level (1+ level) 0))))
  20998. ((item plain-list) (org-list-item-body-column post-affiliated))
  20999. (t
  21000. (goto-char start)
  21001. (org-get-indentation))))
  21002. ((memq type '(headline inlinetask nil))
  21003. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21004. (org--get-expected-indentation element t)
  21005. 0))
  21006. ((memq type '(diary-sexp footnote-definition)) 0)
  21007. ;; First paragraph of a footnote definition or an item.
  21008. ;; Indent like parent.
  21009. ((< (line-beginning-position) start)
  21010. (org--get-expected-indentation
  21011. (org-element-property :parent element) t))
  21012. ;; At first line: indent according to previous sibling, if any,
  21013. ;; ignoring footnote definitions and inline tasks, or parent's
  21014. ;; contents.
  21015. ((= (line-beginning-position) start)
  21016. (catch 'exit
  21017. (while t
  21018. (if (= (point-min) start) (throw 'exit 0)
  21019. (goto-char (1- start))
  21020. (let* ((previous (org-element-at-point))
  21021. (parent previous))
  21022. (while (and parent (<= (org-element-property :end parent) start))
  21023. (setq previous parent
  21024. parent (org-element-property :parent parent)))
  21025. (cond
  21026. ((not previous) (throw 'exit 0))
  21027. ((> (org-element-property :end previous) start)
  21028. (throw 'exit (org--get-expected-indentation previous t)))
  21029. ((memq (org-element-type previous)
  21030. '(footnote-definition inlinetask))
  21031. (setq start (org-element-property :begin previous)))
  21032. (t (goto-char (org-element-property :begin previous))
  21033. (throw 'exit
  21034. (if (bolp) (org-get-indentation)
  21035. ;; At first paragraph in an item or
  21036. ;; a footnote definition.
  21037. (org--get-expected-indentation
  21038. (org-element-property :parent previous) t))))))))))
  21039. ;; Otherwise, move to the first non-blank line above.
  21040. (t
  21041. (beginning-of-line)
  21042. (let ((pos (point)))
  21043. (skip-chars-backward " \r\t\n")
  21044. (cond
  21045. ;; Two blank lines end a footnote definition or a plain
  21046. ;; list. When we indent an empty line after them, the
  21047. ;; containing list or footnote definition is over, so it
  21048. ;; qualifies as a previous sibling. Therefore, we indent
  21049. ;; like its first line.
  21050. ((and (memq type '(footnote-definition plain-list))
  21051. (> (count-lines (point) pos) 2))
  21052. (goto-char start)
  21053. (org-get-indentation))
  21054. ;; Line above is the first one of a paragraph at the
  21055. ;; beginning of an item or a footnote definition. Indent
  21056. ;; like parent.
  21057. ((< (line-beginning-position) start)
  21058. (org--get-expected-indentation
  21059. (org-element-property :parent element) t))
  21060. ;; Line above is the beginning of an element, i.e., point
  21061. ;; was originally on the blank lines between element's start
  21062. ;; and contents.
  21063. ((= (line-beginning-position) post-affiliated)
  21064. (org--get-expected-indentation element t))
  21065. ;; POS is after contents in a greater element. Indent like
  21066. ;; the beginning of the element.
  21067. ;;
  21068. ;; As a special case, if point is at the end of a footnote
  21069. ;; definition or an item, indent like the very last element
  21070. ;; within.
  21071. ((and (not (eq type 'paragraph))
  21072. (let ((cend (org-element-property :contents-end element)))
  21073. (and cend (<= cend pos))))
  21074. (if (memq type '(footnote-definition item plain-list))
  21075. (let ((last (org-element-at-point)))
  21076. (org--get-expected-indentation
  21077. last
  21078. (memq (org-element-type last)
  21079. '(footnote-definition item plain-list))))
  21080. (goto-char start)
  21081. (org-get-indentation)))
  21082. ;; In any other case, indent like the current line.
  21083. (t (org-get-indentation)))))))))
  21084. (defun org--align-node-property ()
  21085. "Align node property at point.
  21086. Alignment is done according to `org-property-format', which see."
  21087. (when (save-excursion
  21088. (beginning-of-line)
  21089. (looking-at org-property-re))
  21090. (replace-match
  21091. (concat (match-string 4)
  21092. (org-trim
  21093. (format org-property-format (match-string 1) (match-string 3))))
  21094. t t)))
  21095. (defun org-indent-line ()
  21096. "Indent line depending on context.
  21097. Indentation is done according to the following rules:
  21098. - Footnote definitions, diary sexps, headlines and inline tasks
  21099. have to start at column 0.
  21100. - On the very first line of an element, consider, in order, the
  21101. next rules until one matches:
  21102. 1. If there's a sibling element before, ignoring footnote
  21103. definitions and inline tasks, indent like its first line.
  21104. 2. If element has a parent, indent like its contents. More
  21105. precisely, if parent is an item, indent after the
  21106. description part, if any, or the bullet (see
  21107. `org-list-description-max-indent'). Else, indent like
  21108. parent's first line.
  21109. 3. Otherwise, indent relatively to current level, if
  21110. `org-adapt-indentation' is non-nil, or to left margin.
  21111. - On a blank line at the end of an element, indent according to
  21112. the type of the element. More precisely
  21113. 1. If element is a plain list, an item, or a footnote
  21114. definition, indent like the very last element within.
  21115. 2. If element is a paragraph, indent like its last non blank
  21116. line.
  21117. 3. Otherwise, indent like its very first line.
  21118. - In the code part of a source block, use language major mode
  21119. to indent current line if `org-src-tab-acts-natively' is
  21120. non-nil. If it is nil, do nothing.
  21121. - Otherwise, indent like the first non-blank line above.
  21122. The function doesn't indent an item as it could break the whole
  21123. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21124. \\[org-shiftmetaright].
  21125. Also align node properties according to `org-property-format'."
  21126. (interactive)
  21127. (cond
  21128. (orgstruct-is-++
  21129. (let ((indent-line-function
  21130. (cadadr (assq 'indent-line-function org-fb-vars))))
  21131. (indent-according-to-mode)))
  21132. ((org-at-heading-p) 'noindent)
  21133. (t
  21134. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21135. (type (org-element-type element)))
  21136. (cond ((and (memq type '(plain-list item))
  21137. (= (line-beginning-position)
  21138. (org-element-property :post-affiliated element)))
  21139. 'noindent)
  21140. ((and (eq type 'src-block)
  21141. org-src-tab-acts-natively
  21142. (> (line-beginning-position)
  21143. (org-element-property :post-affiliated element))
  21144. (< (line-beginning-position)
  21145. (org-with-wide-buffer
  21146. (goto-char (org-element-property :end element))
  21147. (skip-chars-backward " \r\t\n")
  21148. (line-beginning-position))))
  21149. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21150. (t
  21151. (let ((column (org--get-expected-indentation element nil)))
  21152. ;; Preserve current column.
  21153. (if (<= (current-column) (current-indentation))
  21154. (org-indent-line-to column)
  21155. (save-excursion (org-indent-line-to column))))
  21156. ;; Align node property. Also preserve current column.
  21157. (when (eq type 'node-property)
  21158. (let ((column (current-column)))
  21159. (org--align-node-property)
  21160. (org-move-to-column column)))))))))
  21161. (defun org-indent-region (start end)
  21162. "Indent each non-blank line in the region.
  21163. Called from a program, START and END specify the region to
  21164. indent. The function will not indent contents of example blocks,
  21165. verse blocks and export blocks as leading white spaces are
  21166. assumed to be significant there."
  21167. (interactive "r")
  21168. (save-excursion
  21169. (goto-char start)
  21170. (skip-chars-forward " \r\t\n")
  21171. (unless (eobp) (beginning-of-line))
  21172. (let ((indent-to
  21173. (lambda (ind pos)
  21174. ;; Set IND as indentation for all lines between point and
  21175. ;; POS or END, whichever comes first. Blank lines are
  21176. ;; ignored. Leave point after POS once done.
  21177. (let ((limit (copy-marker (min end pos))))
  21178. (while (< (point) limit)
  21179. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21180. (forward-line))
  21181. (set-marker limit nil))))
  21182. (end (copy-marker end)))
  21183. (while (< (point) end)
  21184. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21185. (let* ((element (org-element-at-point))
  21186. (type (org-element-type element))
  21187. (element-end (copy-marker (org-element-property :end element)))
  21188. (ind (org--get-expected-indentation element nil)))
  21189. (cond
  21190. ((or (memq type '(paragraph table table-row))
  21191. (not (or (org-element-property :contents-begin element)
  21192. (memq type
  21193. '(example-block export-block src-block)))))
  21194. ;; Elements here are indented as a single block. Also
  21195. ;; align node properties.
  21196. (when (eq type 'node-property)
  21197. (org--align-node-property)
  21198. (beginning-of-line))
  21199. (funcall indent-to ind element-end))
  21200. (t
  21201. ;; Elements in this category consist of three parts:
  21202. ;; before the contents, the contents, and after the
  21203. ;; contents. The contents are treated specially,
  21204. ;; according to the element type, or not indented at
  21205. ;; all. Other parts are indented as a single block.
  21206. (let* ((post (copy-marker
  21207. (org-element-property :post-affiliated element)))
  21208. (cbeg
  21209. (copy-marker
  21210. (cond
  21211. ((not (org-element-property :contents-begin element))
  21212. ;; Fake contents for source blocks.
  21213. (org-with-wide-buffer
  21214. (goto-char post)
  21215. (forward-line)
  21216. (point)))
  21217. ((memq type '(footnote-definition item plain-list))
  21218. ;; Contents in these elements could start on
  21219. ;; the same line as the beginning of the
  21220. ;; element. Make sure we start indenting
  21221. ;; from the second line.
  21222. (org-with-wide-buffer
  21223. (goto-char post)
  21224. (end-of-line)
  21225. (skip-chars-forward " \r\t\n")
  21226. (if (eobp) (point) (line-beginning-position))))
  21227. (t (org-element-property :contents-begin element)))))
  21228. (cend (copy-marker
  21229. (or (org-element-property :contents-end element)
  21230. ;; Fake contents for source blocks.
  21231. (org-with-wide-buffer
  21232. (goto-char element-end)
  21233. (skip-chars-backward " \r\t\n")
  21234. (line-beginning-position)))
  21235. t)))
  21236. ;; Do not change items indentation individually as it
  21237. ;; might break the list as a whole. On the other
  21238. ;; hand, when at a plain list, indent it as a whole.
  21239. (cond ((eq type 'plain-list)
  21240. (let ((offset (- ind (org-get-indentation))))
  21241. (unless (zerop offset)
  21242. (indent-rigidly (org-element-property :begin element)
  21243. (org-element-property :end element)
  21244. offset))
  21245. (goto-char cbeg)))
  21246. ((eq type 'item) (goto-char cbeg))
  21247. (t (funcall indent-to ind cbeg)))
  21248. (when (< (point) end)
  21249. (case type
  21250. ((example-block export-block verse-block))
  21251. (src-block
  21252. ;; In a source block, indent source code
  21253. ;; according to language major mode, but only if
  21254. ;; `org-src-tab-acts-natively' is non-nil.
  21255. (when (and (< (point) end) org-src-tab-acts-natively)
  21256. (ignore-errors
  21257. (org-babel-do-in-edit-buffer
  21258. (indent-region (point-min) (point-max))))))
  21259. (t (org-indent-region (point) (min cend end))))
  21260. (goto-char (min cend end))
  21261. (when (< (point) end) (funcall indent-to ind element-end)))
  21262. (set-marker post nil)
  21263. (set-marker cbeg nil)
  21264. (set-marker cend nil))))
  21265. (set-marker element-end nil))))
  21266. (set-marker end nil))))
  21267. (defun org-indent-drawer ()
  21268. "Indent the drawer at point."
  21269. (interactive)
  21270. (unless (save-excursion
  21271. (beginning-of-line)
  21272. (org-looking-at-p org-drawer-regexp))
  21273. (user-error "Not at a drawer"))
  21274. (let ((element (org-element-at-point)))
  21275. (unless (memq (org-element-type element) '(drawer property-drawer))
  21276. (user-error "Not at a drawer"))
  21277. (org-with-wide-buffer
  21278. (org-indent-region (org-element-property :begin element)
  21279. (org-element-property :end element))))
  21280. (message "Drawer at point indented"))
  21281. (defun org-indent-block ()
  21282. "Indent the block at point."
  21283. (interactive)
  21284. (unless (save-excursion
  21285. (beginning-of-line)
  21286. (let ((case-fold-search t))
  21287. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21288. (user-error "Not at a block"))
  21289. (let ((element (org-element-at-point)))
  21290. (unless (memq (org-element-type element)
  21291. '(comment-block center-block dynamic-block example-block
  21292. export-block quote-block special-block
  21293. src-block verse-block))
  21294. (user-error "Not at a block"))
  21295. (org-with-wide-buffer
  21296. (org-indent-region (org-element-property :begin element)
  21297. (org-element-property :end element))))
  21298. (message "Block at point indented"))
  21299. ;;; Filling
  21300. ;; We use our own fill-paragraph and auto-fill functions.
  21301. ;; `org-fill-paragraph' relies on adaptive filling and context
  21302. ;; checking. Appropriate `fill-prefix' is computed with
  21303. ;; `org-adaptive-fill-function'.
  21304. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21305. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21306. ;; `org-adaptive-fill-function' value. Internally,
  21307. ;; `org-comment-line-break-function' breaks the line.
  21308. ;; `org-setup-filling' installs filling and auto-filling related
  21309. ;; variables during `org-mode' initialization.
  21310. (defvar org-element-paragraph-separate) ; org-element.el
  21311. (defun org-setup-filling ()
  21312. (require 'org-element)
  21313. ;; Prevent auto-fill from inserting unwanted new items.
  21314. (when (boundp 'fill-nobreak-predicate)
  21315. (org-set-local
  21316. 'fill-nobreak-predicate
  21317. (org-uniquify
  21318. (append fill-nobreak-predicate
  21319. '(org-fill-line-break-nobreak-p
  21320. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21321. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21322. (org-set-local 'paragraph-start paragraph-ending)
  21323. (org-set-local 'paragraph-separate paragraph-ending))
  21324. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21325. (org-set-local 'auto-fill-inhibit-regexp nil)
  21326. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21327. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21328. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21329. (defun org-fill-line-break-nobreak-p ()
  21330. "Non-nil when a new line at point would create an Org line break."
  21331. (save-excursion
  21332. (skip-chars-backward "[ \t]")
  21333. (skip-chars-backward "\\\\")
  21334. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21335. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21336. "Non-nil when a new line at point would split a timestamp."
  21337. (and (org-at-timestamp-p t)
  21338. (not (looking-at org-ts-regexp-both))))
  21339. (declare-function message-in-body-p "message" ())
  21340. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21341. (defun org-adaptive-fill-function ()
  21342. "Compute a fill prefix for the current line.
  21343. Return fill prefix, as a string, or nil if current line isn't
  21344. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21345. matches in paragraphs or comments, use it."
  21346. (catch 'exit
  21347. (when (derived-mode-p 'message-mode)
  21348. (save-excursion
  21349. (beginning-of-line)
  21350. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21351. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21352. ((looking-at message-cite-prefix-regexp)
  21353. (throw 'exit (match-string-no-properties 0)))
  21354. ((looking-at org-outline-regexp)
  21355. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21356. (org-with-wide-buffer
  21357. (unless (org-at-heading-p)
  21358. (let* ((p (line-beginning-position))
  21359. (element (save-excursion
  21360. (beginning-of-line)
  21361. (org-element-at-point)))
  21362. (type (org-element-type element))
  21363. (post-affiliated (org-element-property :post-affiliated element)))
  21364. (unless (< p post-affiliated)
  21365. (case type
  21366. (comment
  21367. (save-excursion
  21368. (beginning-of-line)
  21369. (looking-at "[ \t]*")
  21370. (concat (match-string 0) "# ")))
  21371. (footnote-definition "")
  21372. ((item plain-list)
  21373. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21374. (paragraph
  21375. ;; Fill prefix is usually the same as the current line,
  21376. ;; unless the paragraph is at the beginning of an item.
  21377. (let ((parent (org-element-property :parent element)))
  21378. (save-excursion
  21379. (beginning-of-line)
  21380. (cond ((eq (org-element-type parent) 'item)
  21381. (make-string (org-list-item-body-column
  21382. (org-element-property :begin parent))
  21383. ?\s))
  21384. ((and adaptive-fill-regexp
  21385. ;; Locally disable
  21386. ;; `adaptive-fill-function' to let
  21387. ;; `fill-context-prefix' handle
  21388. ;; `adaptive-fill-regexp' variable.
  21389. (let (adaptive-fill-function)
  21390. (fill-context-prefix
  21391. post-affiliated
  21392. (org-element-property :end element)))))
  21393. ((looking-at "[ \t]+") (match-string 0))
  21394. (t "")))))
  21395. (comment-block
  21396. ;; Only fill contents if P is within block boundaries.
  21397. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21398. (forward-line)
  21399. (point)))
  21400. (cend (save-excursion
  21401. (goto-char (org-element-property :end element))
  21402. (skip-chars-backward " \r\t\n")
  21403. (line-beginning-position))))
  21404. (when (and (>= p cbeg) (< p cend))
  21405. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21406. (match-string 0)
  21407. "")))))))))))
  21408. (declare-function message-goto-body "message" ())
  21409. (defvar message-cite-prefix-regexp) ; From message.el
  21410. (defun org-fill-paragraph (&optional justify)
  21411. "Fill element at point, when applicable.
  21412. This function only applies to comment blocks, comments, example
  21413. blocks and paragraphs. Also, as a special case, re-align table
  21414. when point is at one.
  21415. If JUSTIFY is non-nil (interactively, with prefix argument),
  21416. justify as well. If `sentence-end-double-space' is non-nil, then
  21417. period followed by one space does not end a sentence, so don't
  21418. break a line there. The variable `fill-column' controls the
  21419. width for filling.
  21420. For convenience, when point is at a plain list, an item or
  21421. a footnote definition, try to fill the first paragraph within."
  21422. (interactive)
  21423. (if (and (derived-mode-p 'message-mode)
  21424. (or (not (message-in-body-p))
  21425. (save-excursion (move-beginning-of-line 1)
  21426. (looking-at message-cite-prefix-regexp))))
  21427. ;; First ensure filling is correct in message-mode.
  21428. (let ((fill-paragraph-function
  21429. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21430. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21431. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21432. (paragraph-separate
  21433. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21434. (fill-paragraph nil))
  21435. (with-syntax-table org-mode-transpose-word-syntax-table
  21436. ;; Move to end of line in order to get the first paragraph
  21437. ;; within a plain list or a footnote definition.
  21438. (let ((element (save-excursion
  21439. (end-of-line)
  21440. (or (ignore-errors (org-element-at-point))
  21441. (user-error "An element cannot be parsed line %d"
  21442. (line-number-at-pos (point)))))))
  21443. ;; First check if point is in a blank line at the beginning of
  21444. ;; the buffer. In that case, ignore filling.
  21445. (case (org-element-type element)
  21446. ;; Use major mode filling function is src blocks.
  21447. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21448. ;; Align Org tables, leave table.el tables as-is.
  21449. (table-row (org-table-align) t)
  21450. (table
  21451. (when (eq (org-element-property :type element) 'org)
  21452. (save-excursion
  21453. (goto-char (org-element-property :post-affiliated element))
  21454. (org-table-align)))
  21455. t)
  21456. (paragraph
  21457. ;; Paragraphs may contain `line-break' type objects.
  21458. (let ((beg (max (point-min)
  21459. (org-element-property :contents-begin element)))
  21460. (end (min (point-max)
  21461. (org-element-property :contents-end element))))
  21462. ;; Do nothing if point is at an affiliated keyword.
  21463. (if (< (line-end-position) beg) t
  21464. (when (derived-mode-p 'message-mode)
  21465. ;; In `message-mode', do not fill following citation
  21466. ;; in current paragraph nor text before message body.
  21467. (let ((body-start (save-excursion (message-goto-body))))
  21468. (when body-start (setq beg (max body-start beg))))
  21469. (when (save-excursion
  21470. (re-search-forward
  21471. (concat "^" message-cite-prefix-regexp) end t))
  21472. (setq end (match-beginning 0))))
  21473. ;; Fill paragraph, taking line breaks into account.
  21474. (save-excursion
  21475. (goto-char beg)
  21476. (let ((cuts (list beg)))
  21477. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21478. (when (eq 'line-break
  21479. (org-element-type
  21480. (save-excursion (backward-char)
  21481. (org-element-context))))
  21482. (push (point) cuts)))
  21483. (dolist (c (delq end cuts))
  21484. (fill-region-as-paragraph c end justify)
  21485. (setq end c))))
  21486. t)))
  21487. ;; Contents of `comment-block' type elements should be
  21488. ;; filled as plain text, but only if point is within block
  21489. ;; markers.
  21490. (comment-block
  21491. (let* ((case-fold-search t)
  21492. (beg (save-excursion
  21493. (goto-char (org-element-property :begin element))
  21494. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21495. (forward-line)
  21496. (point)))
  21497. (end (save-excursion
  21498. (goto-char (org-element-property :end element))
  21499. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21500. (line-beginning-position))))
  21501. (if (or (< (point) beg) (> (point) end)) t
  21502. (fill-region-as-paragraph
  21503. (save-excursion (end-of-line)
  21504. (re-search-backward "^[ \t]*$" beg 'move)
  21505. (line-beginning-position))
  21506. (save-excursion (beginning-of-line)
  21507. (re-search-forward "^[ \t]*$" end 'move)
  21508. (line-beginning-position))
  21509. justify))))
  21510. ;; Fill comments.
  21511. (comment
  21512. (let ((begin (org-element-property :post-affiliated element))
  21513. (end (org-element-property :end element)))
  21514. (when (and (>= (point) begin) (<= (point) end))
  21515. (let ((begin (save-excursion
  21516. (end-of-line)
  21517. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21518. (progn (forward-line) (point))
  21519. begin)))
  21520. (end (save-excursion
  21521. (end-of-line)
  21522. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21523. (1- (line-beginning-position))
  21524. (skip-chars-backward " \r\t\n")
  21525. (line-end-position)))))
  21526. ;; Do not fill comments when at a blank line.
  21527. (when (> end begin)
  21528. (let ((fill-prefix
  21529. (save-excursion
  21530. (beginning-of-line)
  21531. (looking-at "[ \t]*#")
  21532. (let ((comment-prefix (match-string 0)))
  21533. (goto-char (match-end 0))
  21534. (if (looking-at adaptive-fill-regexp)
  21535. (concat comment-prefix (match-string 0))
  21536. (concat comment-prefix " "))))))
  21537. (save-excursion
  21538. (fill-region-as-paragraph begin end justify))))))
  21539. t))
  21540. ;; Ignore every other element.
  21541. (otherwise t))))))
  21542. (defun org-auto-fill-function ()
  21543. "Auto-fill function."
  21544. ;; Check if auto-filling is meaningful.
  21545. (let ((fc (current-fill-column)))
  21546. (when (and fc (> (current-column) fc))
  21547. (let* ((fill-prefix (org-adaptive-fill-function))
  21548. ;; Enforce empty fill prefix, if required. Otherwise, it
  21549. ;; will be computed again.
  21550. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21551. (when fill-prefix (do-auto-fill))))))
  21552. (defun org-comment-line-break-function (&optional soft)
  21553. "Break line at point and indent, continuing comment if within one.
  21554. The inserted newline is marked hard if variable
  21555. `use-hard-newlines' is true, unless optional argument SOFT is
  21556. non-nil."
  21557. (if soft (insert-and-inherit ?\n) (newline 1))
  21558. (save-excursion (forward-char -1) (delete-horizontal-space))
  21559. (delete-horizontal-space)
  21560. (indent-to-left-margin)
  21561. (insert-before-markers-and-inherit fill-prefix))
  21562. ;;; Fixed Width Areas
  21563. (defun org-toggle-fixed-width ()
  21564. "Toggle fixed-width markup.
  21565. Add or remove fixed-width markup on current line, whenever it
  21566. makes sense. Return an error otherwise.
  21567. If a region is active and if it contains only fixed-width areas
  21568. or blank lines, remove all fixed-width markup in it. If the
  21569. region contains anything else, convert all non-fixed-width lines
  21570. to fixed-width ones.
  21571. Blank lines at the end of the region are ignored unless the
  21572. region only contains such lines."
  21573. (interactive)
  21574. (if (not (org-region-active-p))
  21575. ;; No region:
  21576. ;;
  21577. ;; Remove fixed width marker only in a fixed-with element.
  21578. ;;
  21579. ;; Add fixed width maker in paragraphs, in blank lines after
  21580. ;; elements or at the beginning of a headline or an inlinetask,
  21581. ;; and before any one-line elements (e.g., a clock).
  21582. (progn
  21583. (beginning-of-line)
  21584. (let* ((element (org-element-at-point))
  21585. (type (org-element-type element)))
  21586. (cond
  21587. ((and (eq type 'fixed-width)
  21588. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21589. (replace-match
  21590. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21591. ((and (memq type '(babel-call clock comment diary-sexp headline
  21592. horizontal-rule keyword paragraph
  21593. planning))
  21594. (<= (org-element-property :post-affiliated element) (point)))
  21595. (skip-chars-forward " \t")
  21596. (insert ": "))
  21597. ((and (org-looking-at-p "[ \t]*$")
  21598. (or (eq type 'inlinetask)
  21599. (save-excursion
  21600. (skip-chars-forward " \r\t\n")
  21601. (<= (org-element-property :end element) (point)))))
  21602. (delete-region (point) (line-end-position))
  21603. (org-indent-line)
  21604. (insert ": "))
  21605. (t (user-error "Cannot insert a fixed-width line here")))))
  21606. ;; Region active.
  21607. (let* ((begin (save-excursion
  21608. (goto-char (region-beginning))
  21609. (line-beginning-position)))
  21610. (end (copy-marker
  21611. (save-excursion
  21612. (goto-char (region-end))
  21613. (unless (eolp) (beginning-of-line))
  21614. (if (save-excursion (re-search-backward "\\S-" begin t))
  21615. (progn (skip-chars-backward " \r\t\n") (point))
  21616. (point)))))
  21617. (all-fixed-width-p
  21618. (catch 'not-all-p
  21619. (save-excursion
  21620. (goto-char begin)
  21621. (skip-chars-forward " \r\t\n")
  21622. (when (eobp) (throw 'not-all-p nil))
  21623. (while (< (point) end)
  21624. (let ((element (org-element-at-point)))
  21625. (if (eq (org-element-type element) 'fixed-width)
  21626. (goto-char (org-element-property :end element))
  21627. (throw 'not-all-p nil))))
  21628. t))))
  21629. (if all-fixed-width-p
  21630. (save-excursion
  21631. (goto-char begin)
  21632. (while (< (point) end)
  21633. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21634. (replace-match
  21635. "" nil nil nil
  21636. (if (= (line-end-position) (match-end 0)) 0 1)))
  21637. (forward-line)))
  21638. (let ((min-ind (point-max)))
  21639. ;; Find minimum indentation across all lines.
  21640. (save-excursion
  21641. (goto-char begin)
  21642. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21643. (setq min-ind 0)
  21644. (catch 'zerop
  21645. (while (< (point) end)
  21646. (unless (org-looking-at-p "[ \t]*$")
  21647. (let ((ind (org-get-indentation)))
  21648. (setq min-ind (min min-ind ind))
  21649. (when (zerop ind) (throw 'zerop t))))
  21650. (forward-line)))))
  21651. ;; Loop over all lines and add fixed-width markup everywhere
  21652. ;; but in fixed-width lines.
  21653. (save-excursion
  21654. (goto-char begin)
  21655. (while (< (point) end)
  21656. (cond
  21657. ((org-at-heading-p)
  21658. (insert ": ")
  21659. (forward-line)
  21660. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21661. (insert ":")
  21662. (forward-line)))
  21663. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21664. (let* ((element (org-element-at-point))
  21665. (element-end (org-element-property :end element)))
  21666. (if (eq (org-element-type element) 'fixed-width)
  21667. (progn (goto-char element-end)
  21668. (skip-chars-backward " \r\t\n")
  21669. (forward-line))
  21670. (let ((limit (min end element-end)))
  21671. (while (< (point) limit)
  21672. (org-move-to-column min-ind t)
  21673. (insert ": ")
  21674. (forward-line))))))
  21675. (t
  21676. (org-move-to-column min-ind t)
  21677. (insert ": ")
  21678. (forward-line)))))))
  21679. (set-marker end nil))))
  21680. ;;; Comments
  21681. ;; Org comments syntax is quite complex. It requires the entire line
  21682. ;; to be just a comment. Also, even with the right syntax at the
  21683. ;; beginning of line, some some elements (i.e. verse-block or
  21684. ;; example-block) don't accept comments. Usual Emacs comment commands
  21685. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21686. ;; Org still relies on `comment-dwim', but cannot trust
  21687. ;; `comment-only-p'. So, `comment-region-function' and
  21688. ;; `uncomment-region-function' both point
  21689. ;; to`org-comment-or-uncomment-region'. Eventually,
  21690. ;; `org-insert-comment' takes care of insertion of comments at the
  21691. ;; beginning of line.
  21692. ;; `org-setup-comments-handling' install comments related variables
  21693. ;; during `org-mode' initialization.
  21694. (defun org-setup-comments-handling ()
  21695. (interactive)
  21696. (org-set-local 'comment-use-syntax nil)
  21697. (org-set-local 'comment-start "# ")
  21698. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21699. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21700. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21701. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21702. (defun org-insert-comment ()
  21703. "Insert an empty comment above current line.
  21704. If the line is empty, insert comment at its beginning. When
  21705. point is within a source block, comment according to the related
  21706. major mode."
  21707. (if (let ((element (org-element-at-point)))
  21708. (and (eq (org-element-type element) 'src-block)
  21709. (< (save-excursion
  21710. (goto-char (org-element-property :post-affiliated element))
  21711. (line-end-position))
  21712. (point))
  21713. (> (save-excursion
  21714. (goto-char (org-element-property :end element))
  21715. (skip-chars-backward " \r\t\n")
  21716. (line-beginning-position))
  21717. (point))))
  21718. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21719. (beginning-of-line)
  21720. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21721. (open-line 1))
  21722. (org-indent-line)
  21723. (insert "# ")))
  21724. (defvar comment-empty-lines) ; From newcomment.el.
  21725. (defun org-comment-or-uncomment-region (beg end &rest _)
  21726. "Comment or uncomment each non-blank line in the region.
  21727. Uncomment each non-blank line between BEG and END if it only
  21728. contains commented lines. Otherwise, comment them. If region is
  21729. strictly within a source block, use appropriate comment syntax."
  21730. (if (let ((element (org-element-at-point)))
  21731. (and (eq (org-element-type element) 'src-block)
  21732. (< (save-excursion
  21733. (goto-char (org-element-property :post-affiliated element))
  21734. (line-end-position))
  21735. beg)
  21736. (>= (save-excursion
  21737. (goto-char (org-element-property :end element))
  21738. (skip-chars-backward " \r\t\n")
  21739. (line-beginning-position))
  21740. end)))
  21741. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21742. (save-restriction
  21743. ;; Restrict region
  21744. (narrow-to-region (save-excursion (goto-char beg)
  21745. (skip-chars-forward " \r\t\n" end)
  21746. (line-beginning-position))
  21747. (save-excursion (goto-char end)
  21748. (skip-chars-backward " \r\t\n" beg)
  21749. (line-end-position)))
  21750. (let ((uncommentp
  21751. ;; UNCOMMENTP is non-nil when every non blank line between
  21752. ;; BEG and END is a comment.
  21753. (save-excursion
  21754. (goto-char (point-min))
  21755. (while (and (not (eobp))
  21756. (let ((element (org-element-at-point)))
  21757. (and (eq (org-element-type element) 'comment)
  21758. (goto-char (min (point-max)
  21759. (org-element-property
  21760. :end element)))))))
  21761. (eobp))))
  21762. (if uncommentp
  21763. ;; Only blank lines and comments in region: uncomment it.
  21764. (save-excursion
  21765. (goto-char (point-min))
  21766. (while (not (eobp))
  21767. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21768. (replace-match "" nil nil nil 1))
  21769. (forward-line)))
  21770. ;; Comment each line in region.
  21771. (let ((min-indent (point-max)))
  21772. ;; First find the minimum indentation across all lines.
  21773. (save-excursion
  21774. (goto-char (point-min))
  21775. (while (and (not (eobp)) (not (zerop min-indent)))
  21776. (unless (looking-at "[ \t]*$")
  21777. (setq min-indent (min min-indent (current-indentation))))
  21778. (forward-line)))
  21779. ;; Then loop over all lines.
  21780. (save-excursion
  21781. (goto-char (point-min))
  21782. (while (not (eobp))
  21783. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21784. ;; Don't get fooled by invisible text (e.g. link path)
  21785. ;; when moving to column MIN-INDENT.
  21786. (let ((buffer-invisibility-spec nil))
  21787. (org-move-to-column min-indent t))
  21788. (insert comment-start))
  21789. (forward-line)))))))))
  21790. (defun org-comment-dwim (arg)
  21791. "Call `comment-dwim' within a source edit buffer if needed."
  21792. (interactive "*P")
  21793. (if (org-in-src-block-p)
  21794. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21795. (call-interactively 'comment-dwim)))
  21796. ;;; Timestamps API
  21797. ;; This section contains tools to operate on timestamp objects, as
  21798. ;; returned by, e.g. `org-element-context'.
  21799. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21800. "Encode TIMESTAMP object into Emacs internal time.
  21801. Use end of date range or time range when END is non-nil."
  21802. (apply #'encode-time
  21803. (cons 0
  21804. (mapcar
  21805. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21806. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21807. '(:minute-start :hour-start :day-start :month-start
  21808. :year-start))))))
  21809. (defun org-timestamp-has-time-p (timestamp)
  21810. "Non-nil when TIMESTAMP has a time specified."
  21811. (org-element-property :hour-start timestamp))
  21812. (defun org-timestamp-format (timestamp format &optional end utc)
  21813. "Format a TIMESTAMP object into a string.
  21814. FORMAT is a format specifier to be passed to
  21815. `format-time-string'.
  21816. When optional argument END is non-nil, use end of date-range or
  21817. time-range, if possible.
  21818. When optional argument UTC is non-nil, time will be expressed as
  21819. Universal Time."
  21820. (format-time-string
  21821. format (org-timestamp--to-internal-time timestamp end) utc))
  21822. (defun org-timestamp-split-range (timestamp &optional end)
  21823. "Extract a TIMESTAMP object from a date or time range.
  21824. END, when non-nil, means extract the end of the range.
  21825. Otherwise, extract its start.
  21826. Return a new timestamp object."
  21827. (let ((type (org-element-property :type timestamp)))
  21828. (if (memq type '(active inactive diary)) timestamp
  21829. (let ((split-ts (org-element-copy timestamp)))
  21830. ;; Set new type.
  21831. (org-element-put-property
  21832. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21833. ;; Copy start properties over end properties if END is
  21834. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21835. (let ((p-alist '((:minute-start . :minute-end)
  21836. (:hour-start . :hour-end)
  21837. (:day-start . :day-end)
  21838. (:month-start . :month-end)
  21839. (:year-start . :year-end))))
  21840. (dolist (p-cell p-alist)
  21841. (org-element-put-property
  21842. split-ts
  21843. (funcall (if end #'car #'cdr) p-cell)
  21844. (org-element-property
  21845. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21846. ;; Eventually refresh `:raw-value'.
  21847. (org-element-put-property split-ts :raw-value nil)
  21848. (org-element-put-property
  21849. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21850. (defun org-timestamp-translate (timestamp &optional boundary)
  21851. "Translate TIMESTAMP object to custom format.
  21852. Format string is defined in `org-time-stamp-custom-formats',
  21853. which see.
  21854. When optional argument BOUNDARY is non-nil, it is either the
  21855. symbol `start' or `end'. In this case, only translate the
  21856. starting or ending part of TIMESTAMP if it is a date or time
  21857. range. Otherwise, translate both parts.
  21858. Return timestamp as-is if `org-display-custom-times' is nil or if
  21859. it has a `diary' type."
  21860. (let ((type (org-element-property :type timestamp)))
  21861. (if (or (not org-display-custom-times) (eq type 'diary))
  21862. (org-element-interpret-data timestamp)
  21863. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21864. org-time-stamp-custom-formats)))
  21865. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21866. (concat (org-timestamp-format timestamp fmt)
  21867. "--"
  21868. (org-timestamp-format timestamp fmt t))
  21869. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21870. ;;; Other stuff.
  21871. (defvar reftex-docstruct-symbol)
  21872. (defvar reftex-cite-format)
  21873. (defvar org--rds)
  21874. (defun org-reftex-citation ()
  21875. "Use reftex-citation to insert a citation into the buffer.
  21876. This looks for a line like
  21877. #+BIBLIOGRAPHY: foo plain option:-d
  21878. and derives from it that foo.bib is the bibliography file relevant
  21879. for this document. It then installs the necessary environment for RefTeX
  21880. to work in this buffer and calls `reftex-citation' to insert a citation
  21881. into the buffer.
  21882. Export of such citations to both LaTeX and HTML is handled by the contributed
  21883. package ox-bibtex by Taru Karttunen."
  21884. (interactive)
  21885. (let ((reftex-docstruct-symbol 'org--rds)
  21886. (reftex-cite-format "\\cite{%l}")
  21887. org--rds bib)
  21888. (save-excursion
  21889. (save-restriction
  21890. (widen)
  21891. (let ((case-fold-search t)
  21892. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21893. (if (not (save-excursion
  21894. (or (re-search-forward re nil t)
  21895. (re-search-backward re nil t))))
  21896. (user-error "No bibliography defined in file")
  21897. (setq bib (concat (match-string 1) ".bib")
  21898. org--rds (list (list 'bib bib)))))))
  21899. (call-interactively 'reftex-citation)))
  21900. ;;;; Functions extending outline functionality
  21901. (defun org-beginning-of-line (&optional arg)
  21902. "Go to the beginning of the current line. If that is invisible, continue
  21903. to a visible line beginning. This makes the function of C-a more intuitive.
  21904. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21905. first attempt, and only move to after the tags when the cursor is already
  21906. beyond the end of the headline."
  21907. (interactive "P")
  21908. (let ((pos (point))
  21909. (special (if (consp org-special-ctrl-a/e)
  21910. (car org-special-ctrl-a/e)
  21911. org-special-ctrl-a/e))
  21912. deactivate-mark refpos)
  21913. (if (org-bound-and-true-p visual-line-mode)
  21914. (beginning-of-visual-line 1)
  21915. (beginning-of-line 1))
  21916. (if (and arg (fboundp 'move-beginning-of-line))
  21917. (call-interactively 'move-beginning-of-line)
  21918. (if (bobp)
  21919. nil
  21920. (backward-char 1)
  21921. (if (org-truely-invisible-p)
  21922. (while (and (not (bobp)) (org-truely-invisible-p))
  21923. (backward-char 1)
  21924. (beginning-of-line 1))
  21925. (forward-char 1))))
  21926. (when special
  21927. (cond
  21928. ((and (looking-at org-complex-heading-regexp)
  21929. (eq (char-after (match-end 1)) ?\s))
  21930. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21931. (point-at-eol)))
  21932. (goto-char
  21933. (if (eq special t)
  21934. (cond ((> pos refpos) refpos)
  21935. ((= pos (point)) refpos)
  21936. (t (point)))
  21937. (cond ((> pos (point)) (point))
  21938. ((not (eq last-command this-command)) (point))
  21939. (t refpos)))))
  21940. ((org-at-item-p)
  21941. ;; Being at an item and not looking at an the item means point
  21942. ;; was previously moved to beginning of a visual line, which
  21943. ;; doesn't contain the item. Therefore, do nothing special,
  21944. ;; just stay here.
  21945. (when (looking-at org-list-full-item-re)
  21946. ;; Set special position at first white space character after
  21947. ;; bullet, and check-box, if any.
  21948. (let ((after-bullet
  21949. (let ((box (match-end 3)))
  21950. (if (not box) (match-end 1)
  21951. (let ((after (char-after box)))
  21952. (if (and after (= after ? )) (1+ box) box))))))
  21953. ;; Special case: Move point to special position when
  21954. ;; currently after it or at beginning of line.
  21955. (if (eq special t)
  21956. (when (or (> pos after-bullet) (= (point) pos))
  21957. (goto-char after-bullet))
  21958. ;; Reversed case: Move point to special position when
  21959. ;; point was already at beginning of line and command is
  21960. ;; repeated.
  21961. (when (and (= (point) pos) (eq last-command this-command))
  21962. (goto-char after-bullet))))))))
  21963. (org-no-warnings
  21964. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21965. (setq disable-point-adjustment
  21966. (or (not (invisible-p (point)))
  21967. (not (invisible-p (max (point-min) (1- (point))))))))
  21968. (defun org-end-of-line (&optional arg)
  21969. "Go to the end of the line.
  21970. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21971. tags on the first attempt, and only move to after the tags when
  21972. the cursor is already beyond the end of the headline."
  21973. (interactive "P")
  21974. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21975. org-special-ctrl-a/e))
  21976. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21977. 'end-of-visual-line)
  21978. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21979. (t 'end-of-line)))
  21980. deactivate-mark)
  21981. (if (or (not special) arg) (call-interactively move-fun)
  21982. (let* ((element (save-excursion (beginning-of-line)
  21983. (org-element-at-point)))
  21984. (type (org-element-type element)))
  21985. (cond
  21986. ((memq type '(headline inlinetask))
  21987. (let ((pos (point)))
  21988. (beginning-of-line 1)
  21989. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21990. (if (eq special t)
  21991. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21992. (goto-char (match-beginning 1))
  21993. (goto-char (match-end 0)))
  21994. (if (or (< pos (match-end 0))
  21995. (not (eq this-command last-command)))
  21996. (goto-char (match-end 0))
  21997. (goto-char (match-beginning 1))))
  21998. (call-interactively move-fun))))
  21999. ((outline-invisible-p (line-end-position))
  22000. ;; If element is hidden, `move-end-of-line' would put point
  22001. ;; after it. Use `end-of-line' to stay on current line.
  22002. (call-interactively 'end-of-line))
  22003. (t (call-interactively move-fun)))))
  22004. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22005. (setq disable-point-adjustment
  22006. (or (not (invisible-p (point)))
  22007. (not (invisible-p (max (point-min) (1- (point))))))))
  22008. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22009. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22010. (defun org-backward-sentence (&optional _arg)
  22011. "Go to beginning of sentence, or beginning of table field.
  22012. This will call `backward-sentence' or `org-table-beginning-of-field',
  22013. depending on context."
  22014. (interactive)
  22015. (let* ((element (org-element-at-point))
  22016. (contents-begin (org-element-property :contents-begin element))
  22017. (table (org-element-lineage element '(table) t)))
  22018. (if (and table
  22019. (> (point) contents-begin)
  22020. (<= (point) (org-element-property :contents-end table)))
  22021. (call-interactively #'org-table-beginning-of-field)
  22022. (save-restriction
  22023. (when (and contents-begin
  22024. (< (point-min) contents-begin)
  22025. (> (point) contents-begin))
  22026. (narrow-to-region contents-begin
  22027. (org-element-property :contents-end element)))
  22028. (call-interactively #'backward-sentence)))))
  22029. (defun org-forward-sentence (&optional _arg)
  22030. "Go to end of sentence, or end of table field.
  22031. This will call `forward-sentence' or `org-table-end-of-field',
  22032. depending on context."
  22033. (interactive)
  22034. (let* ((element (org-element-at-point))
  22035. (contents-end (org-element-property :contents-end element))
  22036. (table (org-element-lineage element '(table) t)))
  22037. (if (and table
  22038. (>= (point) (org-element-property :contents-begin table))
  22039. (< (point) contents-end))
  22040. (call-interactively #'org-table-end-of-field)
  22041. (save-restriction
  22042. (when (and contents-end
  22043. (> (point-max) contents-end)
  22044. ;; Skip blank lines between elements.
  22045. (< (org-element-property :end element)
  22046. (save-excursion (goto-char contents-end)
  22047. (skip-chars-forward " \r\t\n"))))
  22048. (narrow-to-region (org-element-property :contents-begin element)
  22049. contents-end))
  22050. (call-interactively #'forward-sentence)))))
  22051. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22052. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22053. (defun org-kill-line (&optional _arg)
  22054. "Kill line, to tags or end of line."
  22055. (interactive)
  22056. (cond
  22057. ((or (not org-special-ctrl-k)
  22058. (bolp)
  22059. (not (org-at-heading-p)))
  22060. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22061. org-ctrl-k-protect-subtree)
  22062. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22063. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22064. (user-error "C-k aborted as it would kill a hidden subtree")))
  22065. (call-interactively
  22066. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22067. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22068. (kill-region (point) (match-beginning 1))
  22069. (org-set-tags nil t))
  22070. (t (kill-region (point) (point-at-eol)))))
  22071. (define-key org-mode-map "\C-k" 'org-kill-line)
  22072. (defun org-yank (&optional arg)
  22073. "Yank. If the kill is a subtree, treat it specially.
  22074. This command will look at the current kill and check if is a single
  22075. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22076. cursor is at the beginning of a line or after the stars of a currently
  22077. empty headline, then the yank is handled specially. How exactly depends
  22078. on the value of the following variables, both set by default.
  22079. `org-yank-folded-subtrees'
  22080. When set, the subtree(s) will be folded after insertion, but only
  22081. if doing so would now swallow text after the yanked text.
  22082. `org-yank-adjusted-subtrees'
  22083. When set, the subtree will be promoted or demoted in order to
  22084. fit into the local outline tree structure, which means that the
  22085. level will be adjusted so that it becomes the smaller one of the
  22086. two *visible* surrounding headings.
  22087. Any prefix to this command will cause `yank' to be called directly with
  22088. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22089. will just
  22090. plainly yank the text as it is.
  22091. \[1] The test checks if the first non-white line is a heading
  22092. and if there are no other headings with fewer stars."
  22093. (interactive "P")
  22094. (org-yank-generic 'yank arg))
  22095. (defun org-yank-generic (command arg)
  22096. "Perform some yank-like command.
  22097. This function implements the behavior described in the `org-yank'
  22098. documentation. However, it has been generalized to work for any
  22099. interactive command with similar behavior."
  22100. ;; pretend to be command COMMAND
  22101. (setq this-command command)
  22102. (if arg
  22103. (call-interactively command)
  22104. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22105. (and (org-kill-is-subtree-p)
  22106. (or (bolp)
  22107. (and (looking-at "[ \t]*$")
  22108. (string-match
  22109. "\\`\\*+\\'"
  22110. (buffer-substring (point-at-bol) (point)))))))
  22111. swallowp)
  22112. (cond
  22113. ((and subtreep org-yank-folded-subtrees)
  22114. (let ((beg (point))
  22115. end)
  22116. (if (and subtreep org-yank-adjusted-subtrees)
  22117. (org-paste-subtree nil nil 'for-yank)
  22118. (call-interactively command))
  22119. (setq end (point))
  22120. (goto-char beg)
  22121. (when (and (bolp) subtreep
  22122. (not (setq swallowp
  22123. (org-yank-folding-would-swallow-text beg end))))
  22124. (org-with-limited-levels
  22125. (or (looking-at org-outline-regexp)
  22126. (re-search-forward org-outline-regexp-bol end t))
  22127. (while (and (< (point) end) (looking-at org-outline-regexp))
  22128. (hide-subtree)
  22129. (org-cycle-show-empty-lines 'folded)
  22130. (condition-case nil
  22131. (outline-forward-same-level 1)
  22132. (error (goto-char end))))))
  22133. (when swallowp
  22134. (message
  22135. "Inserted text not folded because that would swallow text"))
  22136. (goto-char end)
  22137. (skip-chars-forward " \t\n\r")
  22138. (beginning-of-line 1)
  22139. (push-mark beg 'nomsg)))
  22140. ((and subtreep org-yank-adjusted-subtrees)
  22141. (let ((beg (point-at-bol)))
  22142. (org-paste-subtree nil nil 'for-yank)
  22143. (push-mark beg 'nomsg)))
  22144. (t
  22145. (call-interactively command))))))
  22146. (defun org-yank-folding-would-swallow-text (beg end)
  22147. "Would hide-subtree at BEG swallow any text after END?"
  22148. (let (level)
  22149. (org-with-limited-levels
  22150. (save-excursion
  22151. (goto-char beg)
  22152. (when (or (looking-at org-outline-regexp)
  22153. (re-search-forward org-outline-regexp-bol end t))
  22154. (setq level (org-outline-level)))
  22155. (goto-char end)
  22156. (skip-chars-forward " \t\r\n\v\f")
  22157. (if (or (eobp)
  22158. (and (bolp) (looking-at org-outline-regexp)
  22159. (<= (org-outline-level) level)))
  22160. nil ; Nothing would be swallowed
  22161. t))))) ; something would swallow
  22162. (define-key org-mode-map "\C-y" 'org-yank)
  22163. (defun org-truely-invisible-p ()
  22164. "Check if point is at a character currently not visible.
  22165. This version does not only check the character property, but also
  22166. `visible-mode'."
  22167. ;; Early versions of noutline don't have `outline-invisible-p'.
  22168. (if (org-bound-and-true-p visible-mode)
  22169. nil
  22170. (outline-invisible-p)))
  22171. (defun org-invisible-p2 ()
  22172. "Check if point is at a character currently not visible."
  22173. (save-excursion
  22174. (if (and (eolp) (not (bobp))) (backward-char 1))
  22175. ;; Early versions of noutline don't have `outline-invisible-p'.
  22176. (outline-invisible-p)))
  22177. (defun org-back-to-heading (&optional invisible-ok)
  22178. "Call `outline-back-to-heading', but provide a better error message."
  22179. (condition-case nil
  22180. (outline-back-to-heading invisible-ok)
  22181. (error (error "Before first headline at position %d in buffer %s"
  22182. (point) (current-buffer)))))
  22183. (defun org-before-first-heading-p ()
  22184. "Before first heading?"
  22185. (save-excursion
  22186. (end-of-line)
  22187. (null (re-search-backward org-outline-regexp-bol nil t))))
  22188. (defun org-at-heading-p (&optional ignored)
  22189. (outline-on-heading-p t))
  22190. ;; Compatibility alias with Org versions < 7.8.03
  22191. (defalias 'org-on-heading-p 'org-at-heading-p)
  22192. (defun org-in-commented-heading-p (&optional no-inheritance)
  22193. "Non-nil if point is under a commented heading.
  22194. This function also checks ancestors of the current headline,
  22195. unless optional argument NO-INHERITANCE is non-nil."
  22196. (cond
  22197. ((org-before-first-heading-p) nil)
  22198. ((let ((headline (nth 4 (org-heading-components))))
  22199. (and headline
  22200. (let ((case-fold-search nil))
  22201. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22202. headline)))))
  22203. (no-inheritance nil)
  22204. (t
  22205. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22206. (defun org-at-comment-p nil
  22207. "Is cursor in a commented line?"
  22208. (save-excursion
  22209. (save-match-data
  22210. (beginning-of-line)
  22211. (looking-at "^[ \t]*# "))))
  22212. (defun org-at-drawer-p nil
  22213. "Is cursor at a drawer keyword?"
  22214. (save-excursion
  22215. (move-beginning-of-line 1)
  22216. (looking-at org-drawer-regexp)))
  22217. (defun org-at-block-p nil
  22218. "Is cursor at a block keyword?"
  22219. (save-excursion
  22220. (move-beginning-of-line 1)
  22221. (looking-at org-block-regexp)))
  22222. (defun org-point-at-end-of-empty-headline ()
  22223. "If point is at the end of an empty headline, return t, else nil.
  22224. If the heading only contains a TODO keyword, it is still still considered
  22225. empty."
  22226. (and (looking-at "[ \t]*$")
  22227. (when org-todo-line-regexp
  22228. (save-excursion
  22229. (beginning-of-line 1)
  22230. (let ((case-fold-search nil))
  22231. (looking-at org-todo-line-regexp)
  22232. (string= (match-string 3) ""))))))
  22233. (defun org-at-heading-or-item-p ()
  22234. (or (org-at-heading-p) (org-at-item-p)))
  22235. (defun org-at-target-p ()
  22236. (or (org-in-regexp org-radio-target-regexp)
  22237. (org-in-regexp org-target-regexp)))
  22238. ;; Compatibility alias with Org versions < 7.8.03
  22239. (defalias 'org-on-target-p 'org-at-target-p)
  22240. (defun org-up-heading-all (arg)
  22241. "Move to the heading line of which the present line is a subheading.
  22242. This function considers both visible and invisible heading lines.
  22243. With argument, move up ARG levels."
  22244. (if (fboundp 'outline-up-heading-all)
  22245. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22246. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22247. (defun org-up-heading-safe ()
  22248. "Move to the heading line of which the present line is a subheading.
  22249. This version will not throw an error. It will return the level of the
  22250. headline found, or nil if no higher level is found.
  22251. Also, this function will be a lot faster than `outline-up-heading',
  22252. because it relies on stars being the outline starters. This can really
  22253. make a significant difference in outlines with very many siblings."
  22254. (when (ignore-errors (org-back-to-heading t))
  22255. (let ((level-up (1- (funcall outline-level))))
  22256. (and (> level-up 0)
  22257. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22258. (funcall outline-level)))))
  22259. (defun org-first-sibling-p ()
  22260. "Is this heading the first child of its parents?"
  22261. (interactive)
  22262. (let ((re org-outline-regexp-bol)
  22263. level l)
  22264. (unless (org-at-heading-p t)
  22265. (user-error "Not at a heading"))
  22266. (setq level (funcall outline-level))
  22267. (save-excursion
  22268. (if (not (re-search-backward re nil t))
  22269. t
  22270. (setq l (funcall outline-level))
  22271. (< l level)))))
  22272. (defun org-goto-sibling (&optional previous)
  22273. "Goto the next sibling, even if it is invisible.
  22274. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22275. when a sibling was found. When none is found, return nil and don't
  22276. move point."
  22277. (let ((fun (if previous 're-search-backward 're-search-forward))
  22278. (pos (point))
  22279. (re org-outline-regexp-bol)
  22280. level l)
  22281. (when (ignore-errors (org-back-to-heading t))
  22282. (setq level (funcall outline-level))
  22283. (catch 'exit
  22284. (or previous (forward-char 1))
  22285. (while (funcall fun re nil t)
  22286. (setq l (funcall outline-level))
  22287. (when (< l level) (goto-char pos) (throw 'exit nil))
  22288. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22289. (goto-char pos)
  22290. nil))))
  22291. (defun org-show-siblings ()
  22292. "Show all siblings of the current headline."
  22293. (save-excursion
  22294. (while (org-goto-sibling) (org-flag-heading nil)))
  22295. (save-excursion
  22296. (while (org-goto-sibling 'previous)
  22297. (org-flag-heading nil))))
  22298. (defun org-goto-first-child ()
  22299. "Goto the first child, even if it is invisible.
  22300. Return t when a child was found. Otherwise don't move point and
  22301. return nil."
  22302. (let (level (pos (point)) (re org-outline-regexp-bol))
  22303. (when (ignore-errors (org-back-to-heading t))
  22304. (setq level (outline-level))
  22305. (forward-char 1)
  22306. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22307. (progn (goto-char (match-beginning 0)) t)
  22308. (goto-char pos) nil))))
  22309. (defun org-show-hidden-entry ()
  22310. "Show an entry where even the heading is hidden."
  22311. (save-excursion
  22312. (org-show-entry)))
  22313. (defun org-flag-heading (flag &optional entry)
  22314. "Flag the current heading. FLAG non-nil means make invisible.
  22315. When ENTRY is non-nil, show the entire entry."
  22316. (save-excursion
  22317. (org-back-to-heading t)
  22318. ;; Check if we should show the entire entry
  22319. (if entry
  22320. (progn
  22321. (org-show-entry)
  22322. (save-excursion
  22323. (and (outline-next-heading)
  22324. (org-flag-heading nil))))
  22325. (outline-flag-region (max (point-min) (1- (point)))
  22326. (save-excursion (outline-end-of-heading) (point))
  22327. flag))))
  22328. (defun org-get-next-sibling ()
  22329. "Move to next heading of the same level, and return point.
  22330. If there is no such heading, return nil.
  22331. This is like outline-next-sibling, but invisible headings are ok."
  22332. (let ((level (funcall outline-level)))
  22333. (outline-next-heading)
  22334. (while (and (not (eobp)) (> (funcall outline-level) level))
  22335. (outline-next-heading))
  22336. (if (or (eobp) (< (funcall outline-level) level))
  22337. nil
  22338. (point))))
  22339. (defun org-get-last-sibling ()
  22340. "Move to previous heading of the same level, and return point.
  22341. If there is no such heading, return nil."
  22342. (let ((opoint (point))
  22343. (level (funcall outline-level)))
  22344. (outline-previous-heading)
  22345. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22346. (while (and (> (funcall outline-level) level)
  22347. (not (bobp)))
  22348. (outline-previous-heading))
  22349. (if (< (funcall outline-level) level)
  22350. nil
  22351. (point)))))
  22352. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22353. "Goto to the end of a subtree."
  22354. ;; This contains an exact copy of the original function, but it uses
  22355. ;; `org-back-to-heading', to make it work also in invisible
  22356. ;; trees. And is uses an invisible-ok argument.
  22357. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22358. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22359. ;; with many children and grandchildren etc, this can be much faster
  22360. ;; than the outline version.
  22361. (org-back-to-heading invisible-ok)
  22362. (let ((first t)
  22363. (level (funcall outline-level)))
  22364. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22365. ;; A true heading (not a plain list item), in Org-mode
  22366. ;; This means we can easily find the end by looking
  22367. ;; only for the right number of stars. Using a regexp to do
  22368. ;; this is so much faster than using a Lisp loop.
  22369. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22370. (forward-char 1)
  22371. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22372. ;; something else, do it the slow way
  22373. (while (and (not (eobp))
  22374. (or first (> (funcall outline-level) level)))
  22375. (setq first nil)
  22376. (outline-next-heading)))
  22377. (unless to-heading
  22378. (if (memq (preceding-char) '(?\n ?\^M))
  22379. (progn
  22380. ;; Go to end of line before heading
  22381. (forward-char -1)
  22382. (if (memq (preceding-char) '(?\n ?\^M))
  22383. ;; leave blank line before heading
  22384. (forward-char -1))))))
  22385. (point))
  22386. (defun org-end-of-meta-data (&optional full)
  22387. "Skip planning line and properties drawer in current entry.
  22388. When optional argument FULL is non-nil, also skip empty lines,
  22389. clocking lines and regular drawers at the beginning of the
  22390. entry."
  22391. (org-back-to-heading t)
  22392. (forward-line)
  22393. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22394. (when (looking-at org-property-drawer-re)
  22395. (goto-char (match-end 0))
  22396. (forward-line))
  22397. (when (and full (not (org-at-heading-p)))
  22398. (catch 'exit
  22399. (let ((end (save-excursion (outline-next-heading) (point)))
  22400. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22401. (while (not (eobp))
  22402. (cond ((org-looking-at-p org-drawer-regexp)
  22403. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22404. (forward-line)
  22405. (throw 'exit t)))
  22406. ((org-looking-at-p re) (forward-line))
  22407. (t (throw 'exit t))))))))
  22408. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22409. "Move forward to the ARG'th subheading at same level as this one.
  22410. Stop at the first and last subheadings of a superior heading.
  22411. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22412. non-nil it will also look at invisible ones."
  22413. (interactive "p")
  22414. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22415. (if (and arg (< arg 0))
  22416. (goto-char (point-min))
  22417. (outline-next-heading))
  22418. (org-at-heading-p)
  22419. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22420. (f (if (and arg (< arg 0))
  22421. 're-search-backward
  22422. 're-search-forward))
  22423. (count (if arg (abs arg) 1))
  22424. (result (point)))
  22425. (while (and (prog1 (> count 0)
  22426. (forward-char (if (and arg (< arg 0)) -1 1)))
  22427. (funcall f org-outline-regexp-bol nil 'move))
  22428. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22429. (cond ((< l level) (setq count 0))
  22430. ((and (= l level)
  22431. (or invisible-ok
  22432. (progn
  22433. (goto-char (line-beginning-position))
  22434. (not (outline-invisible-p)))))
  22435. (setq count (1- count))
  22436. (when (eq l level)
  22437. (setq result (point)))))))
  22438. (goto-char result))
  22439. (beginning-of-line 1)))
  22440. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22441. "Move backward to the ARG'th subheading at same level as this one.
  22442. Stop at the first and last subheadings of a superior heading."
  22443. (interactive "p")
  22444. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22445. (defun org-next-visible-heading (arg)
  22446. "Move to the next visible heading.
  22447. This function wraps `outline-next-visible-heading' with
  22448. `org-with-limited-levels' in order to skip over inline tasks and
  22449. respect customization of `org-odd-levels-only'."
  22450. (interactive "p")
  22451. (org-with-limited-levels
  22452. (outline-next-visible-heading arg)))
  22453. (defun org-previous-visible-heading (arg)
  22454. "Move to the next visible heading.
  22455. This function wraps `outline-previous-visible-heading' with
  22456. `org-with-limited-levels' in order to skip over inline tasks and
  22457. respect customization of `org-odd-levels-only'."
  22458. (interactive "p")
  22459. (org-with-limited-levels
  22460. (outline-previous-visible-heading arg)))
  22461. (defun org-next-block (arg &optional backward block-regexp)
  22462. "Jump to the next block.
  22463. With a prefix argument ARG, jump forward ARG many blocks.
  22464. When BACKWARD is non-nil, jump to the previous block.
  22465. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22466. Match data is set according to this regexp when the function
  22467. returns.
  22468. Return point at beginning of the opening line of found block.
  22469. Throw an error if no block is found."
  22470. (interactive "p")
  22471. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22472. (case-fold-search t)
  22473. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22474. (count (or arg 1))
  22475. (origin (point))
  22476. last-element)
  22477. (if backward (beginning-of-line) (end-of-line))
  22478. (while (and (> count 0) (funcall search-fn re nil t))
  22479. (let ((element (save-excursion
  22480. (goto-char (match-beginning 0))
  22481. (save-match-data (org-element-at-point)))))
  22482. (when (and (memq (org-element-type element)
  22483. '(center-block comment-block dynamic-block
  22484. example-block export-block quote-block
  22485. special-block src-block verse-block))
  22486. (<= (match-beginning 0)
  22487. (org-element-property :post-affiliated element)))
  22488. (setq last-element element)
  22489. (decf count))))
  22490. (if (= count 0)
  22491. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22492. (save-match-data (org-show-context)))
  22493. (goto-char origin)
  22494. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22495. (defun org-previous-block (arg &optional block-regexp)
  22496. "Jump to the previous block.
  22497. With a prefix argument ARG, jump backward ARG many source blocks.
  22498. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22499. (interactive "p")
  22500. (org-next-block arg t block-regexp))
  22501. (defun org-forward-paragraph ()
  22502. "Move forward to beginning of next paragraph or equivalent.
  22503. The function moves point to the beginning of the next visible
  22504. structural element, which can be a paragraph, a table, a list
  22505. item, etc. It also provides some special moves for convenience:
  22506. - On an affiliated keyword, jump to the beginning of the
  22507. relative element.
  22508. - On an item or a footnote definition, move to the second
  22509. element inside, if any.
  22510. - On a table or a property drawer, jump after it.
  22511. - On a verse or source block, stop after blank lines."
  22512. (interactive)
  22513. (when (eobp) (user-error "Cannot move further down"))
  22514. (let* ((deactivate-mark nil)
  22515. (element (org-element-at-point))
  22516. (type (org-element-type element))
  22517. (post-affiliated (org-element-property :post-affiliated element))
  22518. (contents-begin (org-element-property :contents-begin element))
  22519. (contents-end (org-element-property :contents-end element))
  22520. (end (let ((end (org-element-property :end element)) (parent element))
  22521. (while (and (setq parent (org-element-property :parent parent))
  22522. (= (org-element-property :contents-end parent) end))
  22523. (setq end (org-element-property :end parent)))
  22524. end)))
  22525. (cond ((not element)
  22526. (skip-chars-forward " \r\t\n")
  22527. (or (eobp) (beginning-of-line)))
  22528. ;; On affiliated keywords, move to element's beginning.
  22529. ((< (point) post-affiliated)
  22530. (goto-char post-affiliated))
  22531. ;; At a table row, move to the end of the table. Similarly,
  22532. ;; at a node property, move to the end of the property
  22533. ;; drawer.
  22534. ((memq type '(node-property table-row))
  22535. (goto-char (org-element-property
  22536. :end (org-element-property :parent element))))
  22537. ((memq type '(property-drawer table)) (goto-char end))
  22538. ;; Consider blank lines as separators in verse and source
  22539. ;; blocks to ease editing.
  22540. ((memq type '(src-block verse-block))
  22541. (when (eq type 'src-block)
  22542. (setq contents-end
  22543. (save-excursion (goto-char end)
  22544. (skip-chars-backward " \r\t\n")
  22545. (line-beginning-position))))
  22546. (beginning-of-line)
  22547. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22548. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22549. (goto-char end)
  22550. (skip-chars-forward " \r\t\n")
  22551. (if (= (point) contents-end) (goto-char end)
  22552. (beginning-of-line))))
  22553. ;; With no contents, just skip element.
  22554. ((not contents-begin) (goto-char end))
  22555. ;; If contents are invisible, skip the element altogether.
  22556. ((outline-invisible-p (line-end-position))
  22557. (case type
  22558. (headline
  22559. (org-with-limited-levels (outline-next-visible-heading 1)))
  22560. ;; At a plain list, make sure we move to the next item
  22561. ;; instead of skipping the whole list.
  22562. (plain-list (forward-char)
  22563. (org-forward-paragraph))
  22564. (otherwise (goto-char end))))
  22565. ((>= (point) contents-end) (goto-char end))
  22566. ((>= (point) contents-begin)
  22567. ;; This can only happen on paragraphs and plain lists.
  22568. (case type
  22569. (paragraph (goto-char end))
  22570. ;; At a plain list, try to move to second element in
  22571. ;; first item, if possible.
  22572. (plain-list (end-of-line)
  22573. (org-forward-paragraph))))
  22574. ;; When contents start on the middle of a line (e.g. in
  22575. ;; items and footnote definitions), try to reach first
  22576. ;; element starting after current line.
  22577. ((> (line-end-position) contents-begin)
  22578. (end-of-line)
  22579. (org-forward-paragraph))
  22580. (t (goto-char contents-begin)))))
  22581. (defun org-backward-paragraph ()
  22582. "Move backward to start of previous paragraph or equivalent.
  22583. The function moves point to the beginning of the current
  22584. structural element, which can be a paragraph, a table, a list
  22585. item, etc., or to the beginning of the previous visible one if
  22586. point is already there. It also provides some special moves for
  22587. convenience:
  22588. - On an affiliated keyword, jump to the first one.
  22589. - On a table or a property drawer, move to its beginning.
  22590. - On a verse or source block, stop before blank lines."
  22591. (interactive)
  22592. (when (bobp) (user-error "Cannot move further up"))
  22593. (let* ((deactivate-mark nil)
  22594. (element (org-element-at-point))
  22595. (type (org-element-type element))
  22596. (contents-begin (org-element-property :contents-begin element))
  22597. (contents-end (org-element-property :contents-end element))
  22598. (post-affiliated (org-element-property :post-affiliated element))
  22599. (begin (org-element-property :begin element)))
  22600. (cond
  22601. ((not element) (goto-char (point-min)))
  22602. ((= (point) begin)
  22603. (backward-char)
  22604. (org-backward-paragraph))
  22605. ((<= (point) post-affiliated) (goto-char begin))
  22606. ((memq type '(node-property table-row))
  22607. (goto-char (org-element-property
  22608. :post-affiliated (org-element-property :parent element))))
  22609. ((memq type '(property-drawer table)) (goto-char begin))
  22610. ((memq type '(src-block verse-block))
  22611. (when (eq type 'src-block)
  22612. (setq contents-begin
  22613. (save-excursion (goto-char begin) (forward-line) (point))))
  22614. (if (= (point) contents-begin) (goto-char post-affiliated)
  22615. ;; Inside a verse block, see blank lines as paragraph
  22616. ;; separators.
  22617. (let ((origin (point)))
  22618. (skip-chars-backward " \r\t\n" contents-begin)
  22619. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22620. (skip-chars-forward " \r\t\n" origin)
  22621. (if (= (point) origin) (goto-char contents-begin)
  22622. (beginning-of-line))))))
  22623. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22624. ((eq type 'paragraph)
  22625. (goto-char contents-begin)
  22626. ;; When at first paragraph in an item or a footnote definition,
  22627. ;; move directly to beginning of line.
  22628. (let ((parent-contents
  22629. (org-element-property
  22630. :contents-begin (org-element-property :parent element))))
  22631. (when (and parent-contents (= parent-contents contents-begin))
  22632. (beginning-of-line))))
  22633. ;; At the end of a greater element, move to the beginning of the
  22634. ;; last element within.
  22635. ((>= (point) contents-end)
  22636. (goto-char (1- contents-end))
  22637. (org-backward-paragraph))
  22638. (t (goto-char (or post-affiliated begin))))
  22639. ;; Ensure we never leave point invisible.
  22640. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22641. (defun org-forward-element ()
  22642. "Move forward by one element.
  22643. Move to the next element at the same level, when possible."
  22644. (interactive)
  22645. (cond ((eobp) (user-error "Cannot move further down"))
  22646. ((org-with-limited-levels (org-at-heading-p))
  22647. (let ((origin (point)))
  22648. (goto-char (org-end-of-subtree nil t))
  22649. (unless (org-with-limited-levels (org-at-heading-p))
  22650. (goto-char origin)
  22651. (user-error "Cannot move further down"))))
  22652. (t
  22653. (let* ((elem (org-element-at-point))
  22654. (end (org-element-property :end elem))
  22655. (parent (org-element-property :parent elem)))
  22656. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22657. (goto-char (org-element-property :end parent)))
  22658. ((integer-or-marker-p end) (goto-char end))
  22659. (t (message "No element at point")))))))
  22660. (defun org-backward-element ()
  22661. "Move backward by one element.
  22662. Move to the previous element at the same level, when possible."
  22663. (interactive)
  22664. (cond ((bobp) (user-error "Cannot move further up"))
  22665. ((org-with-limited-levels (org-at-heading-p))
  22666. ;; At a headline, move to the previous one, if any, or stay
  22667. ;; here.
  22668. (let ((origin (point)))
  22669. (org-with-limited-levels (org-backward-heading-same-level 1))
  22670. ;; When current headline has no sibling above, move to its
  22671. ;; parent.
  22672. (when (= (point) origin)
  22673. (or (org-with-limited-levels (org-up-heading-safe))
  22674. (progn (goto-char origin)
  22675. (user-error "Cannot move further up"))))))
  22676. (t
  22677. (let* ((elem (org-element-at-point))
  22678. (beg (org-element-property :begin elem)))
  22679. (cond
  22680. ;; Move to beginning of current element if point isn't
  22681. ;; there already.
  22682. ((null beg) (message "No element at point"))
  22683. ((/= (point) beg) (goto-char beg))
  22684. (t (goto-char beg)
  22685. (skip-chars-backward " \r\t\n")
  22686. (unless (bobp)
  22687. (let ((prev (org-element-at-point)))
  22688. (goto-char (org-element-property :begin prev))
  22689. (while (and (setq prev (org-element-property :parent prev))
  22690. (<= (org-element-property :end prev) beg))
  22691. (goto-char (org-element-property :begin prev)))))))))))
  22692. (defun org-up-element ()
  22693. "Move to upper element."
  22694. (interactive)
  22695. (if (org-with-limited-levels (org-at-heading-p))
  22696. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22697. (let* ((elem (org-element-at-point))
  22698. (parent (org-element-property :parent elem)))
  22699. (if parent (goto-char (org-element-property :begin parent))
  22700. (if (org-with-limited-levels (org-before-first-heading-p))
  22701. (user-error "No surrounding element")
  22702. (org-with-limited-levels (org-back-to-heading)))))))
  22703. (defvar org-element-greater-elements)
  22704. (defun org-down-element ()
  22705. "Move to inner element."
  22706. (interactive)
  22707. (let ((element (org-element-at-point)))
  22708. (cond
  22709. ((memq (org-element-type element) '(plain-list table))
  22710. (goto-char (org-element-property :contents-begin element))
  22711. (forward-char))
  22712. ((memq (org-element-type element) org-element-greater-elements)
  22713. ;; If contents are hidden, first disclose them.
  22714. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22715. (goto-char (or (org-element-property :contents-begin element)
  22716. (user-error "No content for this element"))))
  22717. (t (user-error "No inner element")))))
  22718. (defun org-drag-element-backward ()
  22719. "Move backward element at point."
  22720. (interactive)
  22721. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22722. (let* ((elem (org-element-at-point))
  22723. (prev-elem
  22724. (save-excursion
  22725. (goto-char (org-element-property :begin elem))
  22726. (skip-chars-backward " \r\t\n")
  22727. (unless (bobp)
  22728. (let* ((beg (org-element-property :begin elem))
  22729. (prev (org-element-at-point))
  22730. (up prev))
  22731. (while (and (setq up (org-element-property :parent up))
  22732. (<= (org-element-property :end up) beg))
  22733. (setq prev up))
  22734. prev)))))
  22735. ;; Error out if no previous element or previous element is
  22736. ;; a parent of the current one.
  22737. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22738. (user-error "Cannot drag element backward")
  22739. (let ((pos (point)))
  22740. (org-element-swap-A-B prev-elem elem)
  22741. (goto-char (+ (org-element-property :begin prev-elem)
  22742. (- pos (org-element-property :begin elem)))))))))
  22743. (defun org-drag-element-forward ()
  22744. "Move forward element at point."
  22745. (interactive)
  22746. (let* ((pos (point))
  22747. (elem (org-element-at-point)))
  22748. (when (= (point-max) (org-element-property :end elem))
  22749. (user-error "Cannot drag element forward"))
  22750. (goto-char (org-element-property :end elem))
  22751. (let ((next-elem (org-element-at-point)))
  22752. (when (or (org-element-nested-p elem next-elem)
  22753. (and (eq (org-element-type next-elem) 'headline)
  22754. (not (eq (org-element-type elem) 'headline))))
  22755. (goto-char pos)
  22756. (user-error "Cannot drag element forward"))
  22757. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22758. ;; body's length (without final blanks) and by the length of
  22759. ;; blanks between ELEM and NEXT-ELEM.
  22760. (let ((size-next (- (save-excursion
  22761. (goto-char (org-element-property :end next-elem))
  22762. (skip-chars-backward " \r\t\n")
  22763. (forward-line)
  22764. ;; Small correction if buffer doesn't end
  22765. ;; with a newline character.
  22766. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22767. (org-element-property :begin next-elem)))
  22768. (size-blank (- (org-element-property :end elem)
  22769. (save-excursion
  22770. (goto-char (org-element-property :end elem))
  22771. (skip-chars-backward " \r\t\n")
  22772. (forward-line)
  22773. (point)))))
  22774. (org-element-swap-A-B elem next-elem)
  22775. (goto-char (+ pos size-next size-blank))))))
  22776. (defun org-drag-line-forward (arg)
  22777. "Drag the line at point ARG lines forward."
  22778. (interactive "p")
  22779. (dotimes (n (abs arg))
  22780. (let ((c (current-column)))
  22781. (if (< 0 arg)
  22782. (progn
  22783. (beginning-of-line 2)
  22784. (transpose-lines 1)
  22785. (beginning-of-line 0))
  22786. (transpose-lines 1)
  22787. (beginning-of-line -1))
  22788. (org-move-to-column c))))
  22789. (defun org-drag-line-backward (arg)
  22790. "Drag the line at point ARG lines backward."
  22791. (interactive "p")
  22792. (org-drag-line-forward (- arg)))
  22793. (defun org-mark-element ()
  22794. "Put point at beginning of this element, mark at end.
  22795. Interactively, if this command is repeated or (in Transient Mark
  22796. mode) if the mark is active, it marks the next element after the
  22797. ones already marked."
  22798. (interactive)
  22799. (let (deactivate-mark)
  22800. (if (and (org-called-interactively-p 'any)
  22801. (or (and (eq last-command this-command) (mark t))
  22802. (and transient-mark-mode mark-active)))
  22803. (set-mark
  22804. (save-excursion
  22805. (goto-char (mark))
  22806. (goto-char (org-element-property :end (org-element-at-point)))))
  22807. (let ((element (org-element-at-point)))
  22808. (end-of-line)
  22809. (push-mark (org-element-property :end element) t t)
  22810. (goto-char (org-element-property :begin element))))))
  22811. (defun org-narrow-to-element ()
  22812. "Narrow buffer to current element."
  22813. (interactive)
  22814. (let ((elem (org-element-at-point)))
  22815. (cond
  22816. ((eq (car elem) 'headline)
  22817. (narrow-to-region
  22818. (org-element-property :begin elem)
  22819. (org-element-property :end elem)))
  22820. ((memq (car elem) org-element-greater-elements)
  22821. (narrow-to-region
  22822. (org-element-property :contents-begin elem)
  22823. (org-element-property :contents-end elem)))
  22824. (t
  22825. (narrow-to-region
  22826. (org-element-property :begin elem)
  22827. (org-element-property :end elem))))))
  22828. (defun org-transpose-element ()
  22829. "Transpose current and previous elements, keeping blank lines between.
  22830. Point is moved after both elements."
  22831. (interactive)
  22832. (org-skip-whitespace)
  22833. (let ((end (org-element-property :end (org-element-at-point))))
  22834. (org-drag-element-backward)
  22835. (goto-char end)))
  22836. (defun org-unindent-buffer ()
  22837. "Un-indent the visible part of the buffer.
  22838. Relative indentation (between items, inside blocks, etc.) isn't
  22839. modified."
  22840. (interactive)
  22841. (unless (eq major-mode 'org-mode)
  22842. (user-error "Cannot un-indent a buffer not in Org mode"))
  22843. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22844. unindent-tree ; For byte-compiler.
  22845. (unindent-tree
  22846. (function
  22847. (lambda (contents)
  22848. (mapc
  22849. (lambda (element)
  22850. (if (memq (org-element-type element) '(headline section))
  22851. (funcall unindent-tree (org-element-contents element))
  22852. (save-excursion
  22853. (save-restriction
  22854. (narrow-to-region
  22855. (org-element-property :begin element)
  22856. (org-element-property :end element))
  22857. (org-do-remove-indentation)))))
  22858. (reverse contents))))))
  22859. (funcall unindent-tree (org-element-contents parse-tree))))
  22860. (defun org-show-subtree ()
  22861. "Show everything after this heading at deeper levels."
  22862. (interactive)
  22863. (outline-flag-region
  22864. (point)
  22865. (save-excursion
  22866. (org-end-of-subtree t t))
  22867. nil))
  22868. (defun org-show-entry ()
  22869. "Show the body directly following this heading.
  22870. Show the heading too, if it is currently invisible."
  22871. (interactive)
  22872. (save-excursion
  22873. (ignore-errors
  22874. (org-back-to-heading t)
  22875. (outline-flag-region
  22876. (max (point-min) (1- (point)))
  22877. (save-excursion
  22878. (if (re-search-forward
  22879. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22880. (match-beginning 1)
  22881. (point-max)))
  22882. nil)
  22883. (org-cycle-hide-drawers 'children))))
  22884. (defun org-make-options-regexp (kwds &optional extra)
  22885. "Make a regular expression for keyword lines.
  22886. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22887. when non-nil, is a regexp matching keywords names."
  22888. (concat "^[ \t]*#\\+\\("
  22889. (regexp-opt kwds)
  22890. (and extra (concat (and kwds "\\|") extra))
  22891. "\\):[ \t]*\\(.*\\)"))
  22892. ;; Make isearch reveal the necessary context
  22893. (defun org-isearch-end ()
  22894. "Reveal context after isearch exits."
  22895. (when isearch-success ; only if search was successful
  22896. (if (featurep 'xemacs)
  22897. ;; Under XEmacs, the hook is run in the correct place,
  22898. ;; we directly show the context.
  22899. (org-show-context 'isearch)
  22900. ;; In Emacs the hook runs *before* restoring the overlays.
  22901. ;; So we have to use a one-time post-command-hook to do this.
  22902. ;; (Emacs 22 has a special variable, see function `org-mode')
  22903. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22904. isearch-mode-end-hook-quit)
  22905. ;; Only when the isearch was not quitted.
  22906. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22907. 'append 'local)))))
  22908. (defun org-isearch-post-command ()
  22909. "Remove self from hook, and show context."
  22910. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22911. (org-show-context 'isearch))
  22912. ;;;; Integration with and fixes for other packages
  22913. ;;; Imenu support
  22914. (defvar org-imenu-markers nil
  22915. "All markers currently used by Imenu.")
  22916. (make-variable-buffer-local 'org-imenu-markers)
  22917. (defun org-imenu-new-marker (&optional pos)
  22918. "Return a new marker for use by Imenu, and remember the marker."
  22919. (let ((m (make-marker)))
  22920. (move-marker m (or pos (point)))
  22921. (push m org-imenu-markers)
  22922. m))
  22923. (defun org-imenu-get-tree ()
  22924. "Produce the index for Imenu."
  22925. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22926. (setq org-imenu-markers nil)
  22927. (let* ((n org-imenu-depth)
  22928. (re (concat "^" (org-get-limited-outline-regexp)))
  22929. (subs (make-vector (1+ n) nil))
  22930. (last-level 0)
  22931. m level head0 head)
  22932. (save-excursion
  22933. (save-restriction
  22934. (widen)
  22935. (goto-char (point-max))
  22936. (while (re-search-backward re nil t)
  22937. (setq level (org-reduced-level (funcall outline-level)))
  22938. (when (and (<= level n)
  22939. (looking-at org-complex-heading-regexp)
  22940. (setq head0 (org-match-string-no-properties 4)))
  22941. (setq head (org-link-display-format head0)
  22942. m (org-imenu-new-marker))
  22943. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22944. (if (>= level last-level)
  22945. (push (cons head m) (aref subs level))
  22946. (push (cons head (aref subs (1+ level))) (aref subs level))
  22947. (loop for i from (1+ level) to n do (aset subs i nil)))
  22948. (setq last-level level)))))
  22949. (aref subs 1)))
  22950. (eval-after-load "imenu"
  22951. '(progn
  22952. (add-hook 'imenu-after-jump-hook
  22953. (lambda ()
  22954. (if (derived-mode-p 'org-mode)
  22955. (org-show-context 'org-goto))))))
  22956. (defun org-link-display-format (link)
  22957. "Replace a link with its the description.
  22958. If there is no description, use the link target."
  22959. (save-match-data
  22960. (if (string-match org-bracket-link-analytic-regexp link)
  22961. (replace-match (if (match-end 5)
  22962. (match-string 5 link)
  22963. (concat (match-string 1 link)
  22964. (match-string 3 link)))
  22965. nil t link)
  22966. link)))
  22967. (defun org-toggle-link-display ()
  22968. "Toggle the literal or descriptive display of links."
  22969. (interactive)
  22970. (if org-descriptive-links
  22971. (progn (org-remove-from-invisibility-spec '(org-link))
  22972. (org-restart-font-lock)
  22973. (setq org-descriptive-links nil))
  22974. (progn (add-to-invisibility-spec '(org-link))
  22975. (org-restart-font-lock)
  22976. (setq org-descriptive-links t))))
  22977. ;; Speedbar support
  22978. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22979. "Overlay marking the agenda restriction line in speedbar.")
  22980. (overlay-put org-speedbar-restriction-lock-overlay
  22981. 'face 'org-agenda-restriction-lock)
  22982. (overlay-put org-speedbar-restriction-lock-overlay
  22983. 'help-echo "Agendas are currently limited to this item.")
  22984. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22985. (defun org-speedbar-set-agenda-restriction ()
  22986. "Restrict future agenda commands to the location at point in speedbar.
  22987. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22988. (interactive)
  22989. (require 'org-agenda)
  22990. (let (p m tp np dir txt)
  22991. (cond
  22992. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22993. 'org-imenu t))
  22994. (setq m (get-text-property p 'org-imenu-marker))
  22995. (with-current-buffer (marker-buffer m)
  22996. (goto-char m)
  22997. (org-agenda-set-restriction-lock 'subtree)))
  22998. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22999. 'speedbar-function 'speedbar-find-file))
  23000. (setq tp (previous-single-property-change
  23001. (1+ p) 'speedbar-function)
  23002. np (next-single-property-change
  23003. tp 'speedbar-function)
  23004. dir (speedbar-line-directory)
  23005. txt (buffer-substring-no-properties (or tp (point-min))
  23006. (or np (point-max))))
  23007. (with-current-buffer (find-file-noselect
  23008. (let ((default-directory dir))
  23009. (expand-file-name txt)))
  23010. (unless (derived-mode-p 'org-mode)
  23011. (user-error "Cannot restrict to non-Org-mode file"))
  23012. (org-agenda-set-restriction-lock 'file)))
  23013. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23014. (move-overlay org-speedbar-restriction-lock-overlay
  23015. (point-at-bol) (point-at-eol))
  23016. (setq current-prefix-arg nil)
  23017. (org-agenda-maybe-redo)))
  23018. (defvar speedbar-file-key-map)
  23019. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23020. (eval-after-load "speedbar"
  23021. '(progn
  23022. (speedbar-add-supported-extension ".org")
  23023. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23024. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23025. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23026. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23027. (add-hook 'speedbar-visiting-tag-hook
  23028. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23029. ;;; Fixes and Hacks for problems with other packages
  23030. (defun org--flyspell-object-check-p (element)
  23031. "Non-nil when Flyspell can check object at point.
  23032. ELEMENT is the element at point."
  23033. (let ((object (save-excursion
  23034. (when (org-looking-at-p "\\>") (backward-char))
  23035. (org-element-context element))))
  23036. (case (org-element-type object)
  23037. ;; Prevent checks in links due to keybinding conflict with
  23038. ;; Flyspell.
  23039. ((code entity export-snippet inline-babel-call
  23040. inline-src-block line-break latex-fragment link macro
  23041. statistics-cookie target timestamp verbatim)
  23042. nil)
  23043. (footnote-reference
  23044. ;; Only in inline footnotes, within the definition.
  23045. (and (eq (org-element-property :type object) 'inline)
  23046. (< (save-excursion
  23047. (goto-char (org-element-property :begin object))
  23048. (search-forward ":" nil t 2))
  23049. (point))))
  23050. (otherwise t))))
  23051. (defun org-mode-flyspell-verify ()
  23052. "Function used for `flyspell-generic-check-word-predicate'."
  23053. (if (org-at-heading-p)
  23054. ;; At a headline or an inlinetask, check title only. This is
  23055. ;; faster than relying on `org-element-at-point'.
  23056. (and (save-excursion (beginning-of-line)
  23057. (and (let ((case-fold-search t))
  23058. (not (looking-at "\\*+ END[ \t]*$")))
  23059. (looking-at org-complex-heading-regexp)))
  23060. (match-beginning 4)
  23061. (>= (point) (match-beginning 4))
  23062. (or (not (match-beginning 5))
  23063. (< (point) (match-beginning 5))))
  23064. (let* ((element (org-element-at-point))
  23065. (post-affiliated (org-element-property :post-affiliated element)))
  23066. (cond
  23067. ;; Ignore checks in all affiliated keywords but captions.
  23068. ((< (point) post-affiliated)
  23069. (and (save-excursion
  23070. (beginning-of-line)
  23071. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23072. (> (point) (match-end 0))
  23073. (org--flyspell-object-check-p element)))
  23074. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23075. ((let ((log (org-log-into-drawer)))
  23076. (and log
  23077. (let ((drawer (org-element-lineage element '(drawer))))
  23078. (and drawer
  23079. (eq (compare-strings
  23080. log nil nil
  23081. (org-element-property :drawer-name drawer) nil nil t)
  23082. t)))))
  23083. nil)
  23084. (t
  23085. (case (org-element-type element)
  23086. ((comment quote-section) t)
  23087. (comment-block
  23088. ;; Allow checks between block markers, not on them.
  23089. (and (> (line-beginning-position) post-affiliated)
  23090. (save-excursion
  23091. (end-of-line)
  23092. (skip-chars-forward " \r\t\n")
  23093. (< (point) (org-element-property :end element)))))
  23094. ;; Arbitrary list of keywords where checks are meaningful.
  23095. ;; Make sure point is on the value part of the element.
  23096. (keyword
  23097. (and (member (org-element-property :key element)
  23098. '("DESCRIPTION" "TITLE"))
  23099. (save-excursion
  23100. (search-backward ":" (line-beginning-position) t))))
  23101. ;; Check is globally allowed in paragraphs verse blocks and
  23102. ;; table rows (after affiliated keywords) but some objects
  23103. ;; must not be affected.
  23104. ((paragraph table-row verse-block)
  23105. (let ((cbeg (org-element-property :contents-begin element))
  23106. (cend (org-element-property :contents-end element)))
  23107. (and cbeg (>= (point) cbeg) (< (point) cend)
  23108. (org--flyspell-object-check-p element))))))))))
  23109. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23110. (defun org-remove-flyspell-overlays-in (beg end)
  23111. "Remove flyspell overlays in region."
  23112. (and (org-bound-and-true-p flyspell-mode)
  23113. (fboundp 'flyspell-delete-region-overlays)
  23114. (flyspell-delete-region-overlays beg end)))
  23115. (defvar flyspell-delayed-commands)
  23116. (eval-after-load "flyspell"
  23117. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23118. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23119. (eval-after-load "bookmark"
  23120. '(if (boundp 'bookmark-after-jump-hook)
  23121. ;; We can use the hook
  23122. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23123. ;; Hook not available, use advice
  23124. (defadvice bookmark-jump (after org-make-visible activate)
  23125. "Make the position visible."
  23126. (org-bookmark-jump-unhide))))
  23127. ;; Make sure saveplace shows the location if it was hidden
  23128. (eval-after-load "saveplace"
  23129. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23130. "Make the position visible."
  23131. (org-bookmark-jump-unhide)))
  23132. ;; Make sure ecb shows the location if it was hidden
  23133. (eval-after-load "ecb"
  23134. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23135. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23136. (if (derived-mode-p 'org-mode)
  23137. (org-show-context))))
  23138. (defun org-bookmark-jump-unhide ()
  23139. "Unhide the current position, to show the bookmark location."
  23140. (and (derived-mode-p 'org-mode)
  23141. (or (outline-invisible-p)
  23142. (save-excursion (goto-char (max (point-min) (1- (point))))
  23143. (outline-invisible-p)))
  23144. (org-show-context 'bookmark-jump)))
  23145. (defun org-mark-jump-unhide ()
  23146. "Make the point visible with `org-show-context' after jumping to the mark."
  23147. (when (and (derived-mode-p 'org-mode)
  23148. (outline-invisible-p))
  23149. (org-show-context 'mark-goto)))
  23150. (eval-after-load "simple"
  23151. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23152. "Make the point visible with `org-show-context'."
  23153. (org-mark-jump-unhide)))
  23154. (eval-after-load "simple"
  23155. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23156. "Make the point visible with `org-show-context'."
  23157. (org-mark-jump-unhide)))
  23158. (eval-after-load "simple"
  23159. '(defadvice pop-global-mark (after org-make-visible activate)
  23160. "Make the point visible with `org-show-context'."
  23161. (org-mark-jump-unhide)))
  23162. ;; Make session.el ignore our circular variable
  23163. (defvar session-globals-exclude)
  23164. (eval-after-load "session"
  23165. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23166. ;;;; Finish up
  23167. (provide 'org)
  23168. (run-hooks 'org-load-hook)
  23169. ;;; org.el ends here