org.el 956 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241
  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-comment-string) '(0 'org-special-keyword t))
  5626. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5628. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5629. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5630. ;; Emphasis
  5631. (if em
  5632. (if (featurep 'xemacs)
  5633. '(org-do-emphasis-faces (0 nil append))
  5634. '(org-do-emphasis-faces)))
  5635. ;; Checkboxes
  5636. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5637. 1 'org-checkbox prepend)
  5638. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5639. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5640. (0 (org-get-checkbox-statistics-face) t)))
  5641. ;; Description list items
  5642. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5643. 1 'org-list-dt prepend)
  5644. ;; ARCHIVEd headings
  5645. (list (concat
  5646. org-outline-regexp-bol
  5647. "\\(.*:" org-archive-tag ":.*\\)")
  5648. '(1 'org-archived prepend))
  5649. ;; Specials
  5650. '(org-do-latex-and-related)
  5651. '(org-fontify-entities)
  5652. '(org-raise-scripts)
  5653. ;; Code
  5654. '(org-activate-code (1 'org-code t))
  5655. ;; COMMENT
  5656. (list (format
  5657. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5658. org-todo-regexp
  5659. org-comment-string)
  5660. '(9 'org-special-keyword t))
  5661. ;; Blocks and meta lines
  5662. '(org-fontify-meta-lines-and-blocks))))
  5663. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5664. (run-hooks 'org-font-lock-set-keywords-hook)
  5665. ;; Now set the full font-lock-keywords
  5666. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5667. (org-set-local 'font-lock-defaults
  5668. '(org-font-lock-keywords t nil nil backward-paragraph))
  5669. (kill-local-variable 'font-lock-keywords) nil))
  5670. (defun org-toggle-pretty-entities ()
  5671. "Toggle the composition display of entities as UTF8 characters."
  5672. (interactive)
  5673. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5674. (org-restart-font-lock)
  5675. (if org-pretty-entities
  5676. (message "Entities are now displayed as UTF8 characters")
  5677. (save-restriction
  5678. (widen)
  5679. (org-decompose-region (point-min) (point-max))
  5680. (message "Entities are now displayed as plain text"))))
  5681. (defvar org-custom-properties-overlays nil
  5682. "List of overlays used for custom properties.")
  5683. (make-variable-buffer-local 'org-custom-properties-overlays)
  5684. (defun org-toggle-custom-properties-visibility ()
  5685. "Display or hide properties in `org-custom-properties'."
  5686. (interactive)
  5687. (if org-custom-properties-overlays
  5688. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5689. (setq org-custom-properties-overlays nil))
  5690. (when org-custom-properties
  5691. (org-with-wide-buffer
  5692. (goto-char (point-min))
  5693. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5694. (while (re-search-forward regexp nil t)
  5695. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5696. (when (and end (< (point) end))
  5697. ;; Hide first custom property in current drawer.
  5698. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5699. (overlay-put o 'invisible t)
  5700. (overlay-put o 'org-custom-property t)
  5701. (push o org-custom-properties-overlays))
  5702. ;; Hide additional custom properties in the same drawer.
  5703. (while (re-search-forward regexp end t)
  5704. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5705. (overlay-put o 'invisible t)
  5706. (overlay-put o 'org-custom-property t)
  5707. (push o org-custom-properties-overlays)))))
  5708. ;; Each entry is limited to a single property drawer.
  5709. (outline-next-heading)))))))
  5710. (defun org-fontify-entities (limit)
  5711. "Find an entity to fontify."
  5712. (let (ee)
  5713. (when org-pretty-entities
  5714. (catch 'match
  5715. ;; "\_ "-family is left out on purpose. Only the first one,
  5716. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5717. ;; confusing when adding a second white space character.
  5718. (while (re-search-forward
  5719. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5720. limit t)
  5721. (if (and (not (org-at-comment-p))
  5722. (setq ee (org-entity-get (match-string 1)))
  5723. (= (length (nth 6 ee)) 1))
  5724. (let*
  5725. ((end (if (equal (match-string 2) "{}")
  5726. (match-end 2)
  5727. (match-end 1))))
  5728. (add-text-properties
  5729. (match-beginning 0) end
  5730. (list 'font-lock-fontified t))
  5731. (compose-region (match-beginning 0) end
  5732. (nth 6 ee) nil)
  5733. (backward-char 1)
  5734. (throw 'match t))))
  5735. nil))))
  5736. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5737. "Fontify string S like in Org-mode."
  5738. (with-temp-buffer
  5739. (insert s)
  5740. (let ((org-odd-levels-only odd-levels))
  5741. (org-mode)
  5742. (font-lock-ensure)
  5743. (buffer-string))))
  5744. (defvar org-m nil)
  5745. (defvar org-l nil)
  5746. (defvar org-f nil)
  5747. (defun org-get-level-face (n)
  5748. "Get the right face for match N in font-lock matching of headlines."
  5749. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5750. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5751. (if org-cycle-level-faces
  5752. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5753. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5754. (cond
  5755. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5756. ((eq n 2) org-f)
  5757. (t (if org-level-color-stars-only nil org-f))))
  5758. (defun org-face-from-face-or-color (context inherit face-or-color)
  5759. "Create a face list that inherits INHERIT, but sets the foreground color.
  5760. When FACE-OR-COLOR is not a string, just return it."
  5761. (if (stringp face-or-color)
  5762. (list :inherit inherit
  5763. (cdr (assoc context org-faces-easy-properties))
  5764. face-or-color)
  5765. face-or-color))
  5766. (defun org-get-todo-face (kwd)
  5767. "Get the right face for a TODO keyword KWD.
  5768. If KWD is a number, get the corresponding match group."
  5769. (if (numberp kwd) (setq kwd (match-string kwd)))
  5770. (or (org-face-from-face-or-color
  5771. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5772. (and (member kwd org-done-keywords) 'org-done)
  5773. 'org-todo))
  5774. (defun org-get-priority-face (priority)
  5775. "Get the right face for PRIORITY.
  5776. PRIORITY is a character."
  5777. (or (org-face-from-face-or-color
  5778. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5779. 'org-priority))
  5780. (defun org-get-tag-face (tag)
  5781. "Get the right face for TAG.
  5782. If TAG is a number, get the corresponding match group."
  5783. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5784. (or (org-face-from-face-or-color
  5785. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5786. 'org-tag)))
  5787. (defun org-font-lock-add-priority-faces (limit)
  5788. "Add the special priority faces."
  5789. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5790. (add-text-properties
  5791. (match-beginning 1) (match-end 1)
  5792. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5793. 'font-lock-fontified t))))
  5794. (defun org-font-lock-add-tag-faces (limit)
  5795. "Add the special tag faces."
  5796. (when (and org-tag-faces org-tags-special-faces-re)
  5797. (while (re-search-forward org-tags-special-faces-re limit t)
  5798. (add-text-properties (match-beginning 1) (match-end 1)
  5799. (list 'face (org-get-tag-face 1)
  5800. 'font-lock-fontified t))
  5801. (backward-char 1))))
  5802. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5803. "Remove fontification and activation overlays from links."
  5804. (font-lock-default-unfontify-region beg end)
  5805. (let* ((buffer-undo-list t)
  5806. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5807. (inhibit-modification-hooks t)
  5808. deactivate-mark buffer-file-name buffer-file-truename)
  5809. (org-decompose-region beg end)
  5810. (remove-text-properties beg end
  5811. '(mouse-face t keymap t org-linked-text t
  5812. invisible t intangible t
  5813. org-emphasis t))
  5814. (org-remove-font-lock-display-properties beg end)))
  5815. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5816. ((raise 0.3) (height 0.7))
  5817. ((raise -0.5))
  5818. ((raise 0.5)))
  5819. "Display properties for showing superscripts and subscripts.")
  5820. (defun org-remove-font-lock-display-properties (beg end)
  5821. "Remove specific display properties that have been added by font lock.
  5822. The will remove the raise properties that are used to show superscripts
  5823. and subscripts."
  5824. (let (next prop)
  5825. (while (< beg end)
  5826. (setq next (next-single-property-change beg 'display nil end)
  5827. prop (get-text-property beg 'display))
  5828. (if (member prop org-script-display)
  5829. (put-text-property beg next 'display nil))
  5830. (setq beg next))))
  5831. (defun org-raise-scripts (limit)
  5832. "Add raise properties to sub/superscripts."
  5833. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5834. (if (re-search-forward
  5835. (if (eq org-use-sub-superscripts t)
  5836. org-match-substring-regexp
  5837. org-match-substring-with-braces-regexp)
  5838. limit t)
  5839. (let* ((pos (point)) table-p comment-p
  5840. (mpos (match-beginning 3))
  5841. (emph-p (get-text-property mpos 'org-emphasis))
  5842. (link-p (get-text-property mpos 'mouse-face))
  5843. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5844. (goto-char (point-at-bol))
  5845. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5846. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5847. (goto-char pos)
  5848. ;; Handle a_b^c
  5849. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5850. (if (or comment-p emph-p link-p keyw-p)
  5851. t
  5852. (put-text-property (match-beginning 3) (match-end 0)
  5853. 'display
  5854. (if (equal (char-after (match-beginning 2)) ?^)
  5855. (nth (if table-p 3 1) org-script-display)
  5856. (nth (if table-p 2 0) org-script-display)))
  5857. (add-text-properties (match-beginning 2) (match-end 2)
  5858. (list 'invisible t
  5859. 'org-dwidth t 'org-dwidth-n 1))
  5860. (if (and (eq (char-after (match-beginning 3)) ?{)
  5861. (eq (char-before (match-end 3)) ?}))
  5862. (progn
  5863. (add-text-properties
  5864. (match-beginning 3) (1+ (match-beginning 3))
  5865. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5866. (add-text-properties
  5867. (1- (match-end 3)) (match-end 3)
  5868. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5869. t)))))
  5870. ;;;; Visibility cycling, including org-goto and indirect buffer
  5871. ;;; Cycling
  5872. (defvar org-cycle-global-status nil)
  5873. (make-variable-buffer-local 'org-cycle-global-status)
  5874. (put 'org-cycle-global-status 'org-state t)
  5875. (defvar org-cycle-subtree-status nil)
  5876. (make-variable-buffer-local 'org-cycle-subtree-status)
  5877. (put 'org-cycle-subtree-status 'org-state t)
  5878. (defvar org-inlinetask-min-level)
  5879. (defun org-unlogged-message (&rest args)
  5880. "Display a message, but avoid logging it in the *Messages* buffer."
  5881. (let ((message-log-max nil))
  5882. (apply 'message args)))
  5883. ;;;###autoload
  5884. (defun org-cycle (&optional arg)
  5885. "TAB-action and visibility cycling for Org-mode.
  5886. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5887. is outline visibility cycling, but it also invokes other actions
  5888. in special contexts.
  5889. - When this function is called with a prefix argument, rotate the entire
  5890. buffer through 3 states (global cycling)
  5891. 1. OVERVIEW: Show only top-level headlines.
  5892. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5893. 3. SHOW ALL: Show everything.
  5894. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5895. determined by the variable `org-startup-folded', and by any VISIBILITY
  5896. properties in the buffer.
  5897. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5898. including any drawers.
  5899. - When inside a table, re-align the table and move to the next field.
  5900. - When point is at the beginning of a headline, rotate the subtree started
  5901. by this line through 3 different states (local cycling)
  5902. 1. FOLDED: Only the main headline is shown.
  5903. 2. CHILDREN: The main headline and the direct children are shown.
  5904. From this state, you can move to one of the children
  5905. and zoom in further.
  5906. 3. SUBTREE: Show the entire subtree, including body text.
  5907. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5908. - When point is at the beginning of an empty headline and the variable
  5909. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5910. of the headline by demoting and promoting it to likely levels. This
  5911. speeds up creation document structure by pressing TAB once or several
  5912. times right after creating a new headline.
  5913. - When there is a numeric prefix, go up to a heading with level ARG, do
  5914. a `show-subtree' and return to the previous cursor position. If ARG
  5915. is negative, go up that many levels.
  5916. - When point is not at the beginning of a headline, execute the global
  5917. binding for TAB, which is re-indenting the line. See the option
  5918. `org-cycle-emulate-tab' for details.
  5919. - Special case: if point is at the beginning of the buffer and there is
  5920. no headline in line 1, this function will act as if called with prefix arg
  5921. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5922. But only if also the variable `org-cycle-global-at-bob' is t."
  5923. (interactive "P")
  5924. (org-load-modules-maybe)
  5925. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5926. (and org-cycle-level-after-item/entry-creation
  5927. (or (org-cycle-level)
  5928. (org-cycle-item-indentation))))
  5929. (let* ((limit-level
  5930. (or org-cycle-max-level
  5931. (and (boundp 'org-inlinetask-min-level)
  5932. org-inlinetask-min-level
  5933. (1- org-inlinetask-min-level))))
  5934. (nstars (and limit-level
  5935. (if org-odd-levels-only
  5936. (and limit-level (1- (* limit-level 2)))
  5937. limit-level)))
  5938. (org-outline-regexp
  5939. (if (not (derived-mode-p 'org-mode))
  5940. outline-regexp
  5941. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5942. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5943. (not (looking-at org-outline-regexp))))
  5944. (org-cycle-hook
  5945. (if bob-special
  5946. (delq 'org-optimize-window-after-visibility-change
  5947. (copy-sequence org-cycle-hook))
  5948. org-cycle-hook))
  5949. (pos (point)))
  5950. (if (or bob-special (equal arg '(4)))
  5951. ;; special case: use global cycling
  5952. (setq arg t))
  5953. (cond
  5954. ((equal arg '(16))
  5955. (setq last-command 'dummy)
  5956. (org-set-startup-visibility)
  5957. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5958. ((equal arg '(64))
  5959. (show-all)
  5960. (org-unlogged-message "Entire buffer visible, including drawers"))
  5961. ;; Try cdlatex TAB completion
  5962. ((org-try-cdlatex-tab))
  5963. ;; Table: enter it or move to the next field.
  5964. ((org-at-table-p 'any)
  5965. (if (org-at-table.el-p)
  5966. (message "Use C-c ' to edit table.el tables")
  5967. (if arg (org-table-edit-field t)
  5968. (org-table-justify-field-maybe)
  5969. (call-interactively 'org-table-next-field))))
  5970. ((run-hook-with-args-until-success
  5971. 'org-tab-after-check-for-table-hook))
  5972. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5973. ((eq arg t) (org-cycle-internal-global))
  5974. ;; Drawers: delegate to `org-flag-drawer'.
  5975. ((save-excursion
  5976. (beginning-of-line 1)
  5977. (looking-at org-drawer-regexp))
  5978. (org-flag-drawer ; toggle block visibility
  5979. (not (get-char-property (match-end 0) 'invisible))))
  5980. ;; Show-subtree, ARG levels up from here.
  5981. ((integerp arg)
  5982. (save-excursion
  5983. (org-back-to-heading)
  5984. (outline-up-heading (if (< arg 0) (- arg)
  5985. (- (funcall outline-level) arg)))
  5986. (org-show-subtree)))
  5987. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5988. ((and (featurep 'org-inlinetask)
  5989. (org-inlinetask-at-task-p)
  5990. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5991. (org-inlinetask-toggle-visibility))
  5992. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5993. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5994. (save-excursion (move-beginning-of-line 1)
  5995. (looking-at org-outline-regexp)))
  5996. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5997. (org-cycle-internal-local))
  5998. ;; From there: TAB emulation and template completion.
  5999. (buffer-read-only (org-back-to-heading))
  6000. ((run-hook-with-args-until-success
  6001. 'org-tab-after-check-for-cycling-hook))
  6002. ((org-try-structure-completion))
  6003. ((run-hook-with-args-until-success
  6004. 'org-tab-before-tab-emulation-hook))
  6005. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6006. (or (not (bolp))
  6007. (not (looking-at org-outline-regexp))))
  6008. (call-interactively (global-key-binding "\t")))
  6009. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6010. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6011. (or (and (eq org-cycle-emulate-tab 'white)
  6012. (= (match-end 0) (point-at-eol)))
  6013. (and (eq org-cycle-emulate-tab 'whitestart)
  6014. (>= (match-end 0) pos))))
  6015. t
  6016. (eq org-cycle-emulate-tab t))
  6017. (call-interactively (global-key-binding "\t")))
  6018. (t (save-excursion
  6019. (org-back-to-heading)
  6020. (org-cycle)))))))
  6021. (defun org-cycle-internal-global ()
  6022. "Do the global cycling action."
  6023. ;; Hack to avoid display of messages for .org attachments in Gnus
  6024. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6025. (cond
  6026. ((and (eq last-command this-command)
  6027. (eq org-cycle-global-status 'overview))
  6028. ;; We just created the overview - now do table of contents
  6029. ;; This can be slow in very large buffers, so indicate action
  6030. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6031. (unless ga (org-unlogged-message "CONTENTS..."))
  6032. (org-content)
  6033. (unless ga (org-unlogged-message "CONTENTS...done"))
  6034. (setq org-cycle-global-status 'contents)
  6035. (run-hook-with-args 'org-cycle-hook 'contents))
  6036. ((and (eq last-command this-command)
  6037. (eq org-cycle-global-status 'contents))
  6038. ;; We just showed the table of contents - now show everything
  6039. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6040. (show-all)
  6041. (unless ga (org-unlogged-message "SHOW ALL"))
  6042. (setq org-cycle-global-status 'all)
  6043. (run-hook-with-args 'org-cycle-hook 'all))
  6044. (t
  6045. ;; Default action: go to overview
  6046. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6047. (org-overview)
  6048. (unless ga (org-unlogged-message "OVERVIEW"))
  6049. (setq org-cycle-global-status 'overview)
  6050. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6051. (defvar org-called-with-limited-levels nil
  6052. "Non-nil when `org-with-limited-levels' is currently active.")
  6053. (defun org-cycle-internal-local ()
  6054. "Do the local cycling action."
  6055. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6056. ;; First, determine end of headline (EOH), end of subtree or item
  6057. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6058. (save-excursion
  6059. (if (org-at-item-p)
  6060. (progn
  6061. (beginning-of-line)
  6062. (setq struct (org-list-struct))
  6063. (setq eoh (point-at-eol))
  6064. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6065. (setq has-children (org-list-has-child-p (point) struct)))
  6066. (org-back-to-heading)
  6067. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6068. (setq eos (save-excursion (org-end-of-subtree t t)
  6069. (when (bolp) (backward-char)) (point)))
  6070. (setq has-children
  6071. (or (save-excursion
  6072. (let ((level (funcall outline-level)))
  6073. (outline-next-heading)
  6074. (and (org-at-heading-p t)
  6075. (> (funcall outline-level) level))))
  6076. (save-excursion
  6077. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6078. ;; Determine end invisible part of buffer (EOL)
  6079. (beginning-of-line 2)
  6080. ;; XEmacs doesn't have `next-single-char-property-change'
  6081. (if (featurep 'xemacs)
  6082. (while (and (not (eobp)) ;; this is like `next-line'
  6083. (get-char-property (1- (point)) 'invisible))
  6084. (beginning-of-line 2))
  6085. (while (and (not (eobp)) ;; this is like `next-line'
  6086. (get-char-property (1- (point)) 'invisible))
  6087. (goto-char (next-single-char-property-change (point) 'invisible))
  6088. (and (eolp) (beginning-of-line 2))))
  6089. (setq eol (point)))
  6090. ;; Find out what to do next and set `this-command'
  6091. (cond
  6092. ((= eos eoh)
  6093. ;; Nothing is hidden behind this heading
  6094. (unless (org-before-first-heading-p)
  6095. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6096. (org-unlogged-message "EMPTY ENTRY")
  6097. (setq org-cycle-subtree-status nil)
  6098. (save-excursion
  6099. (goto-char eos)
  6100. (outline-next-heading)
  6101. (if (outline-invisible-p) (org-flag-heading nil))))
  6102. ((and (or (>= eol eos)
  6103. (not (string-match "\\S-" (buffer-substring eol eos))))
  6104. (or has-children
  6105. (not (setq children-skipped
  6106. org-cycle-skip-children-state-if-no-children))))
  6107. ;; Entire subtree is hidden in one line: children view
  6108. (unless (org-before-first-heading-p)
  6109. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6110. (if (org-at-item-p)
  6111. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6112. (org-show-entry)
  6113. (org-with-limited-levels (show-children))
  6114. ;; FIXME: This slows down the func way too much.
  6115. ;; How keep drawers hidden in subtree anyway?
  6116. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6117. ;; (org-cycle-hide-drawers 'subtree))
  6118. ;; Fold every list in subtree to top-level items.
  6119. (when (eq org-cycle-include-plain-lists 'integrate)
  6120. (save-excursion
  6121. (org-back-to-heading)
  6122. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6123. (beginning-of-line 1)
  6124. (let* ((struct (org-list-struct))
  6125. (prevs (org-list-prevs-alist struct))
  6126. (end (org-list-get-bottom-point struct)))
  6127. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6128. (org-list-get-all-items (point) struct prevs))
  6129. (goto-char (if (< end eos) end eos)))))))
  6130. (org-unlogged-message "CHILDREN")
  6131. (save-excursion
  6132. (goto-char eos)
  6133. (outline-next-heading)
  6134. (if (outline-invisible-p) (org-flag-heading nil)))
  6135. (setq org-cycle-subtree-status 'children)
  6136. (unless (org-before-first-heading-p)
  6137. (run-hook-with-args 'org-cycle-hook 'children)))
  6138. ((or children-skipped
  6139. (and (eq last-command this-command)
  6140. (eq org-cycle-subtree-status 'children)))
  6141. ;; We just showed the children, or no children are there,
  6142. ;; now show everything.
  6143. (unless (org-before-first-heading-p)
  6144. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6145. (outline-flag-region eoh eos nil)
  6146. (org-unlogged-message
  6147. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6148. (setq org-cycle-subtree-status 'subtree)
  6149. (unless (org-before-first-heading-p)
  6150. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6151. (t
  6152. ;; Default action: hide the subtree.
  6153. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6154. (outline-flag-region eoh eos t)
  6155. (org-unlogged-message "FOLDED")
  6156. (setq org-cycle-subtree-status 'folded)
  6157. (unless (org-before-first-heading-p)
  6158. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6159. ;;;###autoload
  6160. (defun org-global-cycle (&optional arg)
  6161. "Cycle the global visibility. For details see `org-cycle'.
  6162. With \\[universal-argument] prefix arg, switch to startup visibility.
  6163. With a numeric prefix, show all headlines up to that level."
  6164. (interactive "P")
  6165. (let ((org-cycle-include-plain-lists
  6166. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6167. (cond
  6168. ((integerp arg)
  6169. (show-all)
  6170. (hide-sublevels arg)
  6171. (setq org-cycle-global-status 'contents))
  6172. ((equal arg '(4))
  6173. (org-set-startup-visibility)
  6174. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6175. (t
  6176. (org-cycle '(4))))))
  6177. (defun org-set-startup-visibility ()
  6178. "Set the visibility required by startup options and properties."
  6179. (cond
  6180. ((eq org-startup-folded t)
  6181. (org-overview))
  6182. ((eq org-startup-folded 'content)
  6183. (org-content))
  6184. ((or (eq org-startup-folded 'showeverything)
  6185. (eq org-startup-folded nil))
  6186. (show-all)))
  6187. (unless (eq org-startup-folded 'showeverything)
  6188. (if org-hide-block-startup (org-hide-block-all))
  6189. (org-set-visibility-according-to-property 'no-cleanup)
  6190. (org-cycle-hide-archived-subtrees 'all)
  6191. (org-cycle-hide-drawers 'all)
  6192. (org-cycle-show-empty-lines t)))
  6193. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6194. "Switch subtree visibilities according to :VISIBILITY: property."
  6195. (interactive)
  6196. (let (org-show-entry-below)
  6197. (org-with-wide-buffer
  6198. (goto-char (point-min))
  6199. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6200. (if (not (org-at-property-p)) (outline-next-heading)
  6201. (let ((state (match-string 3)))
  6202. (save-excursion
  6203. (org-back-to-heading t)
  6204. (hide-subtree)
  6205. (org-reveal)
  6206. (cond
  6207. ((equal state "folded")
  6208. (hide-subtree))
  6209. ((equal state "children")
  6210. (org-show-hidden-entry)
  6211. (show-children))
  6212. ((equal state "content")
  6213. (save-excursion
  6214. (save-restriction
  6215. (org-narrow-to-subtree)
  6216. (org-content))))
  6217. ((member state '("all" "showall"))
  6218. (show-subtree)))))))
  6219. (unless no-cleanup
  6220. (org-cycle-hide-archived-subtrees 'all)
  6221. (org-cycle-hide-drawers 'all)
  6222. (org-cycle-show-empty-lines 'all)))))
  6223. ;; This function uses outline-regexp instead of the more fundamental
  6224. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6225. ;; buffers, where outline-regexp is needed.
  6226. (defun org-overview ()
  6227. "Switch to overview mode, showing only top-level headlines.
  6228. This shows all headlines with a level equal or greater than the level
  6229. of the first headline in the buffer. This is important, because if the
  6230. first headline is not level one, then (hide-sublevels 1) gives confusing
  6231. results."
  6232. (interactive)
  6233. (save-excursion
  6234. (let ((level
  6235. (save-excursion
  6236. (goto-char (point-min))
  6237. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6238. (progn
  6239. (goto-char (match-beginning 0))
  6240. (funcall outline-level))))))
  6241. (and level (hide-sublevels level)))))
  6242. (defun org-content (&optional arg)
  6243. "Show all headlines in the buffer, like a table of contents.
  6244. With numerical argument N, show content up to level N."
  6245. (interactive "P")
  6246. (org-overview)
  6247. (save-excursion
  6248. ;; Visit all headings and show their offspring
  6249. (and (integerp arg) (org-overview))
  6250. (goto-char (point-max))
  6251. (catch 'exit
  6252. (while (and (progn (condition-case nil
  6253. (outline-previous-visible-heading 1)
  6254. (error (goto-char (point-min))))
  6255. t)
  6256. (looking-at org-outline-regexp))
  6257. (if (integerp arg)
  6258. (show-children (1- arg))
  6259. (show-branches))
  6260. (if (bobp) (throw 'exit nil))))))
  6261. (defun org-optimize-window-after-visibility-change (state)
  6262. "Adjust the window after a change in outline visibility.
  6263. This function is the default value of the hook `org-cycle-hook'."
  6264. (when (get-buffer-window (current-buffer))
  6265. (cond
  6266. ((eq state 'content) nil)
  6267. ((eq state 'all) nil)
  6268. ((eq state 'folded) nil)
  6269. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6270. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6271. (defun org-remove-empty-overlays-at (pos)
  6272. "Remove outline overlays that do not contain non-white stuff."
  6273. (mapc
  6274. (lambda (o)
  6275. (and (eq 'outline (overlay-get o 'invisible))
  6276. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6277. (overlay-end o))))
  6278. (delete-overlay o)))
  6279. (overlays-at pos)))
  6280. (defun org-clean-visibility-after-subtree-move ()
  6281. "Fix visibility issues after moving a subtree."
  6282. ;; First, find a reasonable region to look at:
  6283. ;; Start two siblings above, end three below
  6284. (let* ((beg (save-excursion
  6285. (and (org-get-last-sibling)
  6286. (org-get-last-sibling))
  6287. (point)))
  6288. (end (save-excursion
  6289. (and (org-get-next-sibling)
  6290. (org-get-next-sibling)
  6291. (org-get-next-sibling))
  6292. (if (org-at-heading-p)
  6293. (point-at-eol)
  6294. (point))))
  6295. (level (looking-at "\\*+"))
  6296. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6297. (save-excursion
  6298. (save-restriction
  6299. (narrow-to-region beg end)
  6300. (when re
  6301. ;; Properly fold already folded siblings
  6302. (goto-char (point-min))
  6303. (while (re-search-forward re nil t)
  6304. (if (and (not (outline-invisible-p))
  6305. (save-excursion
  6306. (goto-char (point-at-eol)) (outline-invisible-p)))
  6307. (hide-entry))))
  6308. (org-cycle-show-empty-lines 'overview)
  6309. (org-cycle-hide-drawers 'overview)))))
  6310. (defun org-cycle-show-empty-lines (state)
  6311. "Show empty lines above all visible headlines.
  6312. The region to be covered depends on STATE when called through
  6313. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6314. entire buffer covered. Note that an empty line is only shown if there
  6315. are at least `org-cycle-separator-lines' empty lines before the headline."
  6316. (when (/= org-cycle-separator-lines 0)
  6317. (save-excursion
  6318. (let* ((n (abs org-cycle-separator-lines))
  6319. (re (cond
  6320. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6321. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6322. (t (let ((ns (number-to-string (- n 2))))
  6323. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6324. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6325. beg end)
  6326. (cond
  6327. ((memq state '(overview contents t))
  6328. (setq beg (point-min) end (point-max)))
  6329. ((memq state '(children folded))
  6330. (setq beg (point)
  6331. end (progn (org-end-of-subtree t t)
  6332. (line-beginning-position 2)))))
  6333. (when beg
  6334. (goto-char beg)
  6335. (while (re-search-forward re end t)
  6336. (unless (get-char-property (match-end 1) 'invisible)
  6337. (let ((e (match-end 1))
  6338. (b (if (>= org-cycle-separator-lines 0)
  6339. (match-beginning 1)
  6340. (save-excursion
  6341. (goto-char (match-beginning 0))
  6342. (skip-chars-backward " \t\n")
  6343. (line-end-position)))))
  6344. (outline-flag-region b e nil))))))))
  6345. ;; Never hide empty lines at the end of the file.
  6346. (save-excursion
  6347. (goto-char (point-max))
  6348. (outline-previous-heading)
  6349. (outline-end-of-heading)
  6350. (if (and (looking-at "[ \t\n]+")
  6351. (= (match-end 0) (point-max)))
  6352. (outline-flag-region (point) (match-end 0) nil))))
  6353. (defun org-show-empty-lines-in-parent ()
  6354. "Move to the parent and re-show empty lines before visible headlines."
  6355. (save-excursion
  6356. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6357. (org-cycle-show-empty-lines context))))
  6358. (defun org-files-list ()
  6359. "Return `org-agenda-files' list, plus all open org-mode files.
  6360. This is useful for operations that need to scan all of a user's
  6361. open and agenda-wise Org files."
  6362. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6363. (dolist (buf (buffer-list))
  6364. (with-current-buffer buf
  6365. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6366. (let ((file (expand-file-name (buffer-file-name))))
  6367. (unless (member file files)
  6368. (push file files))))))
  6369. files))
  6370. (defsubst org-entry-beginning-position ()
  6371. "Return the beginning position of the current entry."
  6372. (save-excursion (outline-back-to-heading t) (point)))
  6373. (defsubst org-entry-end-position ()
  6374. "Return the end position of the current entry."
  6375. (save-excursion (outline-next-heading) (point)))
  6376. (defun org-cycle-hide-drawers (state &optional exceptions)
  6377. "Re-hide all drawers after a visibility state change.
  6378. When non-nil, optional argument EXCEPTIONS is a list of strings
  6379. specifying which drawers should not be hidden."
  6380. (when (and (derived-mode-p 'org-mode)
  6381. (not (memq state '(overview folded contents))))
  6382. (save-excursion
  6383. (let* ((globalp (memq state '(contents all)))
  6384. (beg (if globalp (point-min) (point)))
  6385. (end (if globalp (point-max)
  6386. (if (eq state 'children)
  6387. (save-excursion (outline-next-heading) (point))
  6388. (org-end-of-subtree t)))))
  6389. (goto-char beg)
  6390. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6391. (unless (member-ignore-case (match-string 1) exceptions)
  6392. (let ((drawer (org-element-at-point)))
  6393. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6394. (org-flag-drawer t drawer)
  6395. ;; Make sure to skip drawer entirely or we might flag
  6396. ;; it another time when matching its ending line with
  6397. ;; `org-drawer-regexp'.
  6398. (goto-char (org-element-property :end drawer))))))))))
  6399. (defun org-flag-drawer (flag &optional element)
  6400. "When FLAG is non-nil, hide the drawer we are at.
  6401. Otherwise make it visible. When optional argument ELEMENT is
  6402. a parsed drawer, as returned by `org-element-at-point', hide or
  6403. show that drawer instead."
  6404. (when (save-excursion
  6405. (beginning-of-line)
  6406. (org-looking-at-p org-drawer-regexp))
  6407. (let ((drawer (or element (org-element-at-point))))
  6408. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6409. (let ((post (org-element-property :post-affiliated drawer)))
  6410. (save-excursion
  6411. (outline-flag-region
  6412. (progn (goto-char post) (line-end-position))
  6413. (progn (goto-char (org-element-property :end drawer))
  6414. (skip-chars-backward " \r\t\n")
  6415. (line-end-position))
  6416. flag))
  6417. ;; When the drawer is hidden away, make sure point lies in
  6418. ;; a visible part of the buffer.
  6419. (when (and flag (> (line-beginning-position) post))
  6420. (goto-char post)))))))
  6421. (defun org-subtree-end-visible-p ()
  6422. "Is the end of the current subtree visible?"
  6423. (pos-visible-in-window-p
  6424. (save-excursion (org-end-of-subtree t) (point))))
  6425. (defun org-first-headline-recenter ()
  6426. "Move cursor to the first headline and recenter the headline."
  6427. (goto-char (point-min))
  6428. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6429. (set-window-start (selected-window) (point-at-bol))))
  6430. ;;; Saving and restoring visibility
  6431. (defun org-outline-overlay-data (&optional use-markers)
  6432. "Return a list of the locations of all outline overlays.
  6433. These are overlays with the `invisible' property value `outline'.
  6434. The return value is a list of cons cells, with start and stop
  6435. positions for each overlay.
  6436. If USE-MARKERS is set, return the positions as markers."
  6437. (let (beg end)
  6438. (save-excursion
  6439. (save-restriction
  6440. (widen)
  6441. (delq nil
  6442. (mapcar (lambda (o)
  6443. (when (eq (overlay-get o 'invisible) 'outline)
  6444. (setq beg (overlay-start o)
  6445. end (overlay-end o))
  6446. (and beg end (> end beg)
  6447. (if use-markers
  6448. (cons (copy-marker beg)
  6449. (copy-marker end t))
  6450. (cons beg end)))))
  6451. (overlays-in (point-min) (point-max))))))))
  6452. (defun org-set-outline-overlay-data (data)
  6453. "Create visibility overlays for all positions in DATA.
  6454. DATA should have been made by `org-outline-overlay-data'."
  6455. (let (o)
  6456. (save-excursion
  6457. (save-restriction
  6458. (widen)
  6459. (show-all)
  6460. (mapc (lambda (c)
  6461. (outline-flag-region (car c) (cdr c) t))
  6462. data)))))
  6463. ;;; Folding of blocks
  6464. (defvar org-hide-block-overlays nil
  6465. "Overlays hiding blocks.")
  6466. (make-variable-buffer-local 'org-hide-block-overlays)
  6467. (defun org-block-map (function &optional start end)
  6468. "Call FUNCTION at the head of all source blocks in the current buffer.
  6469. Optional arguments START and END can be used to limit the range."
  6470. (let ((start (or start (point-min)))
  6471. (end (or end (point-max))))
  6472. (save-excursion
  6473. (goto-char start)
  6474. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6475. (save-excursion
  6476. (save-match-data
  6477. (goto-char (match-beginning 0))
  6478. (funcall function)))))))
  6479. (defun org-hide-block-toggle-all ()
  6480. "Toggle the visibility of all blocks in the current buffer."
  6481. (org-block-map 'org-hide-block-toggle))
  6482. (defun org-hide-block-all ()
  6483. "Fold all blocks in the current buffer."
  6484. (interactive)
  6485. (org-show-block-all)
  6486. (org-block-map 'org-hide-block-toggle-maybe))
  6487. (defun org-show-block-all ()
  6488. "Unfold all blocks in the current buffer."
  6489. (interactive)
  6490. (mapc 'delete-overlay org-hide-block-overlays)
  6491. (setq org-hide-block-overlays nil))
  6492. (defun org-hide-block-toggle-maybe ()
  6493. "Toggle visibility of block at point.
  6494. Unlike to `org-hide-block-toggle', this function does not throw
  6495. an error. Return a non-nil value when toggling is successful."
  6496. (interactive)
  6497. (ignore-errors (org-hide-block-toggle)))
  6498. (defun org-hide-block-toggle (&optional force)
  6499. "Toggle the visibility of the current block.
  6500. When optional argument FORCE is `off', make block visible. If it
  6501. is non-nil, hide it unconditionally. Throw an error when not at
  6502. a block. Return a non-nil value when toggling is successful."
  6503. (interactive)
  6504. (let ((element (org-element-at-point)))
  6505. (unless (memq (org-element-type element)
  6506. '(center-block comment-block dynamic-block example-block
  6507. export-block quote-block special-block
  6508. src-block verse-block))
  6509. (user-error "Not at a block"))
  6510. (let* ((start (save-excursion
  6511. (goto-char (org-element-property :post-affiliated element))
  6512. (line-end-position)))
  6513. (end (save-excursion
  6514. (goto-char (org-element-property :end element))
  6515. (skip-chars-backward " \r\t\n")
  6516. (line-end-position)))
  6517. (overlays (overlays-at start)))
  6518. (cond
  6519. ;; Do nothing when not before or at the block opening line or
  6520. ;; at the block closing line.
  6521. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6522. ((and (not (eq force 'off))
  6523. (not (memq t (mapcar
  6524. (lambda (o)
  6525. (eq (overlay-get o 'invisible) 'org-hide-block))
  6526. overlays))))
  6527. (let ((ov (make-overlay start end)))
  6528. (overlay-put ov 'invisible 'org-hide-block)
  6529. ;; Make the block accessible to `isearch'.
  6530. (overlay-put
  6531. ov 'isearch-open-invisible
  6532. (lambda (ov)
  6533. (when (memq ov org-hide-block-overlays)
  6534. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6535. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6536. (delete-overlay ov))))
  6537. (push ov org-hide-block-overlays)
  6538. ;; When the block is hidden away, make sure point is left in
  6539. ;; a visible part of the buffer.
  6540. (when (> (line-beginning-position) start)
  6541. (goto-char start)
  6542. (beginning-of-line))
  6543. ;; Signal successful toggling.
  6544. t))
  6545. ((or (not force) (eq force 'off))
  6546. (dolist (ov overlays t)
  6547. (when (memq ov org-hide-block-overlays)
  6548. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6549. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6550. (delete-overlay ov))))))))
  6551. ;; org-tab-after-check-for-cycling-hook
  6552. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6553. ;; Remove overlays when changing major mode
  6554. (add-hook 'org-mode-hook
  6555. (lambda () (org-add-hook 'change-major-mode-hook
  6556. 'org-show-block-all 'append 'local)))
  6557. ;;; Org-goto
  6558. (defvar org-goto-window-configuration nil)
  6559. (defvar org-goto-marker nil)
  6560. (defvar org-goto-map)
  6561. (defun org-goto-map ()
  6562. "Set the keymap `org-goto'."
  6563. (setq org-goto-map
  6564. (let ((map (make-sparse-keymap)))
  6565. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6566. mouse-drag-region universal-argument org-occur)))
  6567. (dolist (cmd cmds)
  6568. (substitute-key-definition cmd cmd map global-map)))
  6569. (suppress-keymap map)
  6570. (org-defkey map "\C-m" 'org-goto-ret)
  6571. (org-defkey map [(return)] 'org-goto-ret)
  6572. (org-defkey map [(left)] 'org-goto-left)
  6573. (org-defkey map [(right)] 'org-goto-right)
  6574. (org-defkey map [(control ?g)] 'org-goto-quit)
  6575. (org-defkey map "\C-i" 'org-cycle)
  6576. (org-defkey map [(tab)] 'org-cycle)
  6577. (org-defkey map [(down)] 'outline-next-visible-heading)
  6578. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6579. (if org-goto-auto-isearch
  6580. (if (fboundp 'define-key-after)
  6581. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6582. nil)
  6583. (org-defkey map "q" 'org-goto-quit)
  6584. (org-defkey map "n" 'outline-next-visible-heading)
  6585. (org-defkey map "p" 'outline-previous-visible-heading)
  6586. (org-defkey map "f" 'outline-forward-same-level)
  6587. (org-defkey map "b" 'outline-backward-same-level)
  6588. (org-defkey map "u" 'outline-up-heading))
  6589. (org-defkey map "/" 'org-occur)
  6590. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6591. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6592. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6593. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6594. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6595. map)))
  6596. (defconst org-goto-help
  6597. "Browse buffer copy, to find location or copy text.%s
  6598. RET=jump to location C-g=quit and return to previous location
  6599. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6600. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6601. (defun org-goto (&optional alternative-interface)
  6602. "Look up a different location in the current file, keeping current visibility.
  6603. When you want look-up or go to a different location in a
  6604. document, the fastest way is often to fold the entire buffer and
  6605. then dive into the tree. This method has the disadvantage, that
  6606. the previous location will be folded, which may not be what you
  6607. want.
  6608. This command works around this by showing a copy of the current
  6609. buffer in an indirect buffer, in overview mode. You can dive
  6610. into the tree in that copy, use org-occur and incremental search
  6611. to find a location. When pressing RET or `Q', the command
  6612. returns to the original buffer in which the visibility is still
  6613. unchanged. After RET it will also jump to the location selected
  6614. in the indirect buffer and expose the headline hierarchy above.
  6615. With a prefix argument, use the alternative interface: e.g. if
  6616. `org-goto-interface' is 'outline use 'outline-path-completion."
  6617. (interactive "P")
  6618. (org-goto-map)
  6619. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6620. (org-refile-use-outline-path t)
  6621. (org-refile-target-verify-function nil)
  6622. (interface
  6623. (if (not alternative-interface)
  6624. org-goto-interface
  6625. (if (eq org-goto-interface 'outline)
  6626. 'outline-path-completion
  6627. 'outline)))
  6628. (org-goto-start-pos (point))
  6629. (selected-point
  6630. (if (eq interface 'outline)
  6631. (car (org-get-location (current-buffer) org-goto-help))
  6632. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6633. (org-refile-check-position pa)
  6634. (nth 3 pa)))))
  6635. (if selected-point
  6636. (progn
  6637. (org-mark-ring-push org-goto-start-pos)
  6638. (goto-char selected-point)
  6639. (if (or (outline-invisible-p) (org-invisible-p2))
  6640. (org-show-context 'org-goto)))
  6641. (message "Quit"))))
  6642. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6643. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6644. (defvar org-goto-local-auto-isearch-map) ; defined below
  6645. (defun org-get-location (buf help)
  6646. "Let the user select a location in the Org-mode buffer BUF.
  6647. This function uses a recursive edit. It returns the selected position
  6648. or nil."
  6649. (org-no-popups
  6650. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6651. (isearch-hide-immediately nil)
  6652. (isearch-search-fun-function
  6653. (lambda () 'org-goto-local-search-headings))
  6654. (org-goto-selected-point org-goto-exit-command))
  6655. (save-excursion
  6656. (save-window-excursion
  6657. (delete-other-windows)
  6658. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6659. (org-pop-to-buffer-same-window
  6660. (condition-case nil
  6661. (make-indirect-buffer (current-buffer) "*org-goto*")
  6662. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6663. (with-output-to-temp-buffer "*Org Help*"
  6664. (princ (format help (if org-goto-auto-isearch
  6665. " Just type for auto-isearch."
  6666. " n/p/f/b/u to navigate, q to quit."))))
  6667. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6668. (setq buffer-read-only nil)
  6669. (let ((org-startup-truncated t)
  6670. (org-startup-folded nil)
  6671. (org-startup-align-all-tables nil))
  6672. (org-mode)
  6673. (org-overview))
  6674. (setq buffer-read-only t)
  6675. (if (and (boundp 'org-goto-start-pos)
  6676. (integer-or-marker-p org-goto-start-pos))
  6677. (progn (goto-char org-goto-start-pos)
  6678. (when (outline-invisible-p)
  6679. (org-show-set-visibility 'lineage)))
  6680. (goto-char (point-min)))
  6681. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6682. (message "Select location and press RET")
  6683. (use-local-map org-goto-map)
  6684. (recursive-edit)))
  6685. (kill-buffer "*org-goto*")
  6686. (cons org-goto-selected-point org-goto-exit-command))))
  6687. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6688. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6689. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6690. (if (fboundp 'isearch-other-control-char)
  6691. (progn
  6692. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6693. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6694. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6695. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6696. (define-key org-goto-local-auto-isearch-map [return] nil))
  6697. (defun org-goto-local-search-headings (string bound noerror)
  6698. "Search and make sure that any matches are in headlines."
  6699. (catch 'return
  6700. (while (if isearch-forward
  6701. (search-forward string bound noerror)
  6702. (search-backward string bound noerror))
  6703. (when (save-match-data
  6704. (and (save-excursion
  6705. (beginning-of-line)
  6706. (looking-at org-complex-heading-regexp))
  6707. (or (not (match-beginning 5))
  6708. (< (point) (match-beginning 5)))))
  6709. (throw 'return (point))))))
  6710. (defun org-goto-local-auto-isearch ()
  6711. "Start isearch."
  6712. (interactive)
  6713. (goto-char (point-min))
  6714. (let ((keys (this-command-keys)))
  6715. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6716. (isearch-mode t)
  6717. (isearch-process-search-char (string-to-char keys)))))
  6718. (defun org-goto-ret (&optional arg)
  6719. "Finish `org-goto' by going to the new location."
  6720. (interactive "P")
  6721. (setq org-goto-selected-point (point)
  6722. org-goto-exit-command 'return)
  6723. (throw 'exit nil))
  6724. (defun org-goto-left ()
  6725. "Finish `org-goto' by going to the new location."
  6726. (interactive)
  6727. (if (org-at-heading-p)
  6728. (progn
  6729. (beginning-of-line 1)
  6730. (setq org-goto-selected-point (point)
  6731. org-goto-exit-command 'left)
  6732. (throw 'exit nil))
  6733. (user-error "Not on a heading")))
  6734. (defun org-goto-right ()
  6735. "Finish `org-goto' by going to the new location."
  6736. (interactive)
  6737. (if (org-at-heading-p)
  6738. (progn
  6739. (setq org-goto-selected-point (point)
  6740. org-goto-exit-command 'right)
  6741. (throw 'exit nil))
  6742. (user-error "Not on a heading")))
  6743. (defun org-goto-quit ()
  6744. "Finish `org-goto' without cursor motion."
  6745. (interactive)
  6746. (setq org-goto-selected-point nil)
  6747. (setq org-goto-exit-command 'quit)
  6748. (throw 'exit nil))
  6749. ;;; Indirect buffer display of subtrees
  6750. (defvar org-indirect-dedicated-frame nil
  6751. "This is the frame being used for indirect tree display.")
  6752. (defvar org-last-indirect-buffer nil)
  6753. (defun org-tree-to-indirect-buffer (&optional arg)
  6754. "Create indirect buffer and narrow it to current subtree.
  6755. With a numerical prefix ARG, go up to this level and then take that tree.
  6756. If ARG is negative, go up that many levels.
  6757. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6758. indirect buffer previously made with this command, to avoid proliferation of
  6759. indirect buffers. However, when you call the command with a \
  6760. \\[universal-argument] prefix, or
  6761. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6762. is kept so that you can work with several indirect buffers at the same time.
  6763. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6764. \\[universal-argument] prefix also
  6765. requests that a new frame be made for the new buffer, so that the dedicated
  6766. frame is not changed."
  6767. (interactive "P")
  6768. (let ((cbuf (current-buffer))
  6769. (cwin (selected-window))
  6770. (pos (point))
  6771. beg end level heading ibuf)
  6772. (save-excursion
  6773. (org-back-to-heading t)
  6774. (when (numberp arg)
  6775. (setq level (org-outline-level))
  6776. (if (< arg 0) (setq arg (+ level arg)))
  6777. (while (> (setq level (org-outline-level)) arg)
  6778. (org-up-heading-safe)))
  6779. (setq beg (point)
  6780. heading (org-get-heading))
  6781. (org-end-of-subtree t t)
  6782. (if (org-at-heading-p) (backward-char 1))
  6783. (setq end (point)))
  6784. (if (and (buffer-live-p org-last-indirect-buffer)
  6785. (not (eq org-indirect-buffer-display 'new-frame))
  6786. (not arg))
  6787. (kill-buffer org-last-indirect-buffer))
  6788. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6789. org-last-indirect-buffer ibuf)
  6790. (cond
  6791. ((or (eq org-indirect-buffer-display 'new-frame)
  6792. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6793. (select-frame (make-frame))
  6794. (delete-other-windows)
  6795. (org-pop-to-buffer-same-window ibuf)
  6796. (org-set-frame-title heading))
  6797. ((eq org-indirect-buffer-display 'dedicated-frame)
  6798. (raise-frame
  6799. (select-frame (or (and org-indirect-dedicated-frame
  6800. (frame-live-p org-indirect-dedicated-frame)
  6801. org-indirect-dedicated-frame)
  6802. (setq org-indirect-dedicated-frame (make-frame)))))
  6803. (delete-other-windows)
  6804. (org-pop-to-buffer-same-window ibuf)
  6805. (org-set-frame-title (concat "Indirect: " heading)))
  6806. ((eq org-indirect-buffer-display 'current-window)
  6807. (org-pop-to-buffer-same-window ibuf))
  6808. ((eq org-indirect-buffer-display 'other-window)
  6809. (pop-to-buffer ibuf))
  6810. (t (error "Invalid value")))
  6811. (if (featurep 'xemacs)
  6812. (save-excursion (org-mode) (turn-on-font-lock)))
  6813. (narrow-to-region beg end)
  6814. (show-all)
  6815. (goto-char pos)
  6816. (run-hook-with-args 'org-cycle-hook 'all)
  6817. (and (window-live-p cwin) (select-window cwin))))
  6818. (defun org-get-indirect-buffer (&optional buffer heading)
  6819. (setq buffer (or buffer (current-buffer)))
  6820. (let ((n 1) (base (buffer-name buffer)) bname)
  6821. (while (buffer-live-p
  6822. (get-buffer
  6823. (setq bname
  6824. (concat base "-"
  6825. (if heading (concat heading "-" (number-to-string n))
  6826. (number-to-string n))))))
  6827. (setq n (1+ n)))
  6828. (condition-case nil
  6829. (make-indirect-buffer buffer bname 'clone)
  6830. (error (make-indirect-buffer buffer bname)))))
  6831. (defun org-set-frame-title (title)
  6832. "Set the title of the current frame to the string TITLE."
  6833. ;; FIXME: how to name a single frame in XEmacs???
  6834. (unless (featurep 'xemacs)
  6835. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6836. ;;;; Structure editing
  6837. ;;; Inserting headlines
  6838. (defun org-previous-line-empty-p (&optional next)
  6839. "Is the previous line a blank line?
  6840. When NEXT is non-nil, check the next line instead."
  6841. (save-excursion
  6842. (and (not (bobp))
  6843. (or (beginning-of-line (if next 2 0)) t)
  6844. (save-match-data
  6845. (looking-at "[ \t]*$")))))
  6846. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6847. "Insert a new heading or an item with the same depth at point.
  6848. If point is at the beginning of a heading or a list item, insert
  6849. a new heading or a new item above the current one. If point is
  6850. at the beginning of a normal line, turn the line into a heading.
  6851. If point is in the middle of a headline or a list item, split the
  6852. headline or the item and create a new headline/item with the text
  6853. in the current line after point \(see `org-M-RET-may-split-line'
  6854. on how to modify this behavior).
  6855. With one universal prefix argument, set the user option
  6856. `org-insert-heading-respect-content' to t for the duration of
  6857. the command. This modifies the behavior described above in this
  6858. ways: on list items and at the beginning of normal lines, force
  6859. the insertion of a heading after the current subtree.
  6860. With two universal prefix arguments, insert the heading at the
  6861. end of the grandparent subtree. For example, if point is within
  6862. a 2nd-level heading, then it will insert a 2nd-level heading at
  6863. the end of the 1st-level parent heading.
  6864. If point is at the beginning of a headline, insert a sibling
  6865. before the current headline. If point is not at the beginning,
  6866. split the line and create a new headline with the text in the
  6867. current line after point \(see `org-M-RET-may-split-line' on how
  6868. to modify this behavior).
  6869. If point is at the beginning of a normal line, turn this line
  6870. into a heading.
  6871. When INVISIBLE-OK is set, stop at invisible headlines when going
  6872. back. This is important for non-interactive uses of the
  6873. command.
  6874. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6875. heading, unconditionally."
  6876. (interactive "P")
  6877. (if (org-called-interactively-p 'any) (org-reveal))
  6878. (let ((itemp (and (not top-level) (org-in-item-p)))
  6879. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6880. (respect-content (or org-insert-heading-respect-content
  6881. (equal arg '(4))))
  6882. (initial-content ""))
  6883. (cond
  6884. ((or (= (buffer-size) 0)
  6885. (and (not (save-excursion
  6886. (and (ignore-errors (org-back-to-heading invisible-ok))
  6887. (org-at-heading-p))))
  6888. (or arg (not itemp))))
  6889. ;; At beginning of buffer or so high up that only a heading
  6890. ;; makes sense.
  6891. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6892. ((not respect-content)
  6893. (unless may-split (end-of-line))
  6894. (insert "\n* "))
  6895. ((re-search-forward org-outline-regexp-bol nil t)
  6896. (beginning-of-line)
  6897. (insert "* \n")
  6898. (backward-char))
  6899. (t (goto-char (point-max))
  6900. (insert "\n* ")))
  6901. (run-hooks 'org-insert-heading-hook))
  6902. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6903. (t
  6904. ;; Maybe move at the end of the subtree
  6905. (when (equal arg '(16))
  6906. (org-up-heading-safe)
  6907. (org-end-of-subtree t))
  6908. ;; Insert a heading
  6909. (save-restriction
  6910. (widen)
  6911. (let* ((level nil)
  6912. (on-heading (org-at-heading-p))
  6913. (empty-line-p (if on-heading
  6914. (org-previous-line-empty-p)
  6915. ;; We will decide later
  6916. nil))
  6917. ;; Get a level string to fall back on.
  6918. (fix-level
  6919. (if (org-before-first-heading-p) "*"
  6920. (save-excursion
  6921. (org-back-to-heading t)
  6922. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6923. (looking-at org-outline-regexp)
  6924. (make-string (1- (length (match-string 0))) ?*))))
  6925. (stars
  6926. (save-excursion
  6927. (condition-case nil
  6928. (if top-level "* "
  6929. (org-back-to-heading invisible-ok)
  6930. (when (and (not on-heading)
  6931. (featurep 'org-inlinetask)
  6932. (integerp org-inlinetask-min-level)
  6933. (>= (length (match-string 0))
  6934. org-inlinetask-min-level))
  6935. ;; Find a heading level before the inline
  6936. ;; task.
  6937. (while (and (setq level (org-up-heading-safe))
  6938. (>= level org-inlinetask-min-level)))
  6939. (if (org-at-heading-p)
  6940. (org-back-to-heading invisible-ok)
  6941. (error "This should not happen")))
  6942. (unless (and (save-excursion
  6943. (save-match-data
  6944. (org-backward-heading-same-level
  6945. 1 invisible-ok))
  6946. (= (point) (match-beginning 0)))
  6947. (not (org-previous-line-empty-p t)))
  6948. (setq empty-line-p (or empty-line-p
  6949. (org-previous-line-empty-p))))
  6950. (match-string 0))
  6951. (error (or fix-level "* ")))))
  6952. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6953. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6954. pos hide-previous previous-pos)
  6955. ;; If we insert after content, move there and clean up
  6956. ;; whitespace.
  6957. (when (and respect-content
  6958. (not (org-looking-at-p org-outline-regexp-bol)))
  6959. (if (not (org-before-first-heading-p))
  6960. (org-end-of-subtree nil t)
  6961. (re-search-forward org-outline-regexp-bol)
  6962. (beginning-of-line 0))
  6963. (skip-chars-backward " \r\t\n")
  6964. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6965. (looking-at "[ \t]+") (replace-match ""))
  6966. (unless (eobp) (forward-char 1))
  6967. (when (looking-at "^\\*")
  6968. (unless (bobp) (backward-char 1))
  6969. (insert "\n")))
  6970. ;; If we are splitting, grab the text that should be moved
  6971. ;; to the new headline.
  6972. (when may-split
  6973. (if (org-on-heading-p)
  6974. ;; This is a heading: split intelligently (keeping
  6975. ;; tags).
  6976. (let ((pos (point)))
  6977. (beginning-of-line)
  6978. (unless (looking-at org-complex-heading-regexp)
  6979. (error "This should not happen"))
  6980. (when (and (match-beginning 4)
  6981. (> pos (match-beginning 4))
  6982. (< pos (match-end 4)))
  6983. (setq initial-content (buffer-substring pos (match-end 4)))
  6984. (goto-char pos)
  6985. (delete-region (point) (match-end 4))
  6986. (if (looking-at "[ \t]*$")
  6987. (replace-match "")
  6988. (insert (make-string (length initial-content) ?\s)))
  6989. (setq initial-content (org-trim initial-content)))
  6990. (goto-char pos))
  6991. ;; A normal line.
  6992. (setq initial-content
  6993. (org-trim
  6994. (delete-and-extract-region (point) (line-end-position))))))
  6995. ;; If we are at the beginning of the line, insert before it.
  6996. ;; Otherwise, after it.
  6997. (cond
  6998. ((and (bolp) (looking-at "[ \t]*$")))
  6999. ((bolp) (save-excursion (insert "\n")))
  7000. (t (end-of-line)
  7001. (insert "\n")))
  7002. ;; Insert the new heading
  7003. (insert stars)
  7004. (just-one-space)
  7005. (insert initial-content)
  7006. (unless (and blank (org-previous-line-empty-p))
  7007. (org-N-empty-lines-before-current (if blank 1 0)))
  7008. ;; Adjust visibility, which may be messed up if we removed
  7009. ;; blank lines while previous entry was hidden.
  7010. (let ((bol (line-beginning-position)))
  7011. (dolist (o (overlays-at (1- bol)))
  7012. (when (and (eq (overlay-get o 'invisible) 'outline)
  7013. (eq (overlay-end o) bol))
  7014. (move-overlay o (overlay-start o) (1- bol)))))
  7015. (run-hooks 'org-insert-heading-hook)))))))
  7016. (defun org-N-empty-lines-before-current (N)
  7017. "Make the number of empty lines before current exactly N.
  7018. So this will delete or add empty lines."
  7019. (save-excursion
  7020. (beginning-of-line)
  7021. (let ((p (point)))
  7022. (skip-chars-backward " \r\t\n")
  7023. (unless (bolp) (forward-line))
  7024. (delete-region (point) p))
  7025. (when (> N 0) (insert (make-string N ?\n)))))
  7026. (defun org-get-heading (&optional no-tags no-todo)
  7027. "Return the heading of the current entry, without the stars.
  7028. When NO-TAGS is non-nil, don't include tags.
  7029. When NO-TODO is non-nil, don't include TODO keywords."
  7030. (save-excursion
  7031. (org-back-to-heading t)
  7032. (cond
  7033. ((and no-tags no-todo)
  7034. (looking-at org-complex-heading-regexp)
  7035. (match-string 4))
  7036. (no-tags
  7037. (looking-at (concat org-outline-regexp
  7038. "\\(.*?\\)"
  7039. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7040. (match-string 1))
  7041. (no-todo
  7042. (looking-at org-todo-line-regexp)
  7043. (match-string 3))
  7044. (t (looking-at org-heading-regexp)
  7045. (match-string 2)))))
  7046. (defvar orgstruct-mode) ; defined below
  7047. (defun org-heading-components ()
  7048. "Return the components of the current heading.
  7049. This is a list with the following elements:
  7050. - the level as an integer
  7051. - the reduced level, different if `org-odd-levels-only' is set.
  7052. - the TODO keyword, or nil
  7053. - the priority character, like ?A, or nil if no priority is given
  7054. - the headline text itself, or the tags string if no headline text
  7055. - the tags string, or nil."
  7056. (save-excursion
  7057. (org-back-to-heading t)
  7058. (if (let (case-fold-search)
  7059. (looking-at
  7060. (if orgstruct-mode
  7061. org-heading-regexp
  7062. org-complex-heading-regexp)))
  7063. (if orgstruct-mode
  7064. (list (length (match-string 1))
  7065. (org-reduced-level (length (match-string 1)))
  7066. nil
  7067. nil
  7068. (match-string 2)
  7069. nil)
  7070. (list (length (match-string 1))
  7071. (org-reduced-level (length (match-string 1)))
  7072. (org-match-string-no-properties 2)
  7073. (and (match-end 3) (aref (match-string 3) 2))
  7074. (org-match-string-no-properties 4)
  7075. (org-match-string-no-properties 5))))))
  7076. (defun org-get-entry ()
  7077. "Get the entry text, after heading, entire subtree."
  7078. (save-excursion
  7079. (org-back-to-heading t)
  7080. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7081. (defun org-insert-heading-after-current ()
  7082. "Insert a new heading with same level as current, after current subtree."
  7083. (interactive)
  7084. (org-back-to-heading)
  7085. (org-insert-heading)
  7086. (org-move-subtree-down)
  7087. (end-of-line 1))
  7088. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7089. "Insert heading with `org-insert-heading-respect-content' set to t."
  7090. (interactive)
  7091. (org-insert-heading '(4) invisible-ok))
  7092. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7093. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7094. (interactive)
  7095. (org-insert-todo-heading force-state '(4)))
  7096. (defun org-insert-todo-heading (arg &optional force-heading)
  7097. "Insert a new heading with the same level and TODO state as current heading.
  7098. If the heading has no TODO state, or if the state is DONE, use the first
  7099. state (TODO by default). Also with one prefix arg, force first state. With
  7100. two prefix args, force inserting at the end of the parent subtree."
  7101. (interactive "P")
  7102. (when (or force-heading (not (org-insert-item 'checkbox)))
  7103. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7104. force-heading))
  7105. (save-excursion
  7106. (org-back-to-heading)
  7107. (outline-previous-heading)
  7108. (looking-at org-todo-line-regexp))
  7109. (let*
  7110. ((new-mark-x
  7111. (if (or (equal arg '(4))
  7112. (not (match-beginning 2))
  7113. (member (match-string 2) org-done-keywords))
  7114. (car org-todo-keywords-1)
  7115. (match-string 2)))
  7116. (new-mark
  7117. (or
  7118. (run-hook-with-args-until-success
  7119. 'org-todo-get-default-hook new-mark-x nil)
  7120. new-mark-x)))
  7121. (beginning-of-line 1)
  7122. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7123. (if org-treat-insert-todo-heading-as-state-change
  7124. (org-todo new-mark)
  7125. (insert new-mark " "))))
  7126. (when org-provide-todo-statistics
  7127. (org-update-parent-todo-statistics))))
  7128. (defun org-insert-subheading (arg)
  7129. "Insert a new subheading and demote it.
  7130. Works for outline headings and for plain lists alike."
  7131. (interactive "P")
  7132. (org-insert-heading arg)
  7133. (cond
  7134. ((org-at-heading-p) (org-do-demote))
  7135. ((org-at-item-p) (org-indent-item))))
  7136. (defun org-insert-todo-subheading (arg)
  7137. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7138. Works for outline headings and for plain lists alike."
  7139. (interactive "P")
  7140. (org-insert-todo-heading arg)
  7141. (cond
  7142. ((org-at-heading-p) (org-do-demote))
  7143. ((org-at-item-p) (org-indent-item))))
  7144. ;;; Promotion and Demotion
  7145. (defvar org-after-demote-entry-hook nil
  7146. "Hook run after an entry has been demoted.
  7147. The cursor will be at the beginning of the entry.
  7148. When a subtree is being demoted, the hook will be called for each node.")
  7149. (defvar org-after-promote-entry-hook nil
  7150. "Hook run after an entry has been promoted.
  7151. The cursor will be at the beginning of the entry.
  7152. When a subtree is being promoted, the hook will be called for each node.")
  7153. (defun org-promote-subtree ()
  7154. "Promote the entire subtree.
  7155. See also `org-promote'."
  7156. (interactive)
  7157. (save-excursion
  7158. (org-with-limited-levels (org-map-tree 'org-promote)))
  7159. (org-fix-position-after-promote))
  7160. (defun org-demote-subtree ()
  7161. "Demote the entire subtree. See `org-demote'.
  7162. See also `org-promote'."
  7163. (interactive)
  7164. (save-excursion
  7165. (org-with-limited-levels (org-map-tree 'org-demote)))
  7166. (org-fix-position-after-promote))
  7167. (defun org-do-promote ()
  7168. "Promote the current heading higher up the tree.
  7169. If the region is active in `transient-mark-mode', promote all headings
  7170. in the region."
  7171. (interactive)
  7172. (save-excursion
  7173. (if (org-region-active-p)
  7174. (org-map-region 'org-promote (region-beginning) (region-end))
  7175. (org-promote)))
  7176. (org-fix-position-after-promote))
  7177. (defun org-do-demote ()
  7178. "Demote the current heading lower down the tree.
  7179. If the region is active in `transient-mark-mode', demote all headings
  7180. in the region."
  7181. (interactive)
  7182. (save-excursion
  7183. (if (org-region-active-p)
  7184. (org-map-region 'org-demote (region-beginning) (region-end))
  7185. (org-demote)))
  7186. (org-fix-position-after-promote))
  7187. (defun org-fix-position-after-promote ()
  7188. "Make sure that after pro/demotion cursor position is right."
  7189. (let ((pos (point)))
  7190. (when (save-excursion
  7191. (beginning-of-line 1)
  7192. (looking-at org-todo-line-regexp)
  7193. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7194. (cond ((eobp) (insert " "))
  7195. ((eolp) (insert " "))
  7196. ((equal (char-after) ?\ ) (forward-char 1))))))
  7197. (defun org-current-level ()
  7198. "Return the level of the current entry, or nil if before the first headline.
  7199. The level is the number of stars at the beginning of the
  7200. headline. Use `org-reduced-level' to remove the effect of
  7201. `org-odd-levels'. Unlike to `org-outline-level', this function
  7202. ignores inlinetasks."
  7203. (let ((level (org-with-limited-levels (org-outline-level))))
  7204. (and (> level 0) level)))
  7205. (defun org-get-previous-line-level ()
  7206. "Return the outline depth of the last headline before the current line.
  7207. Returns 0 for the first headline in the buffer, and nil if before the
  7208. first headline."
  7209. (and (org-current-level)
  7210. (or (and (/= (line-beginning-position) (point-min))
  7211. (save-excursion (beginning-of-line 0) (org-current-level)))
  7212. 0)))
  7213. (defun org-reduced-level (l)
  7214. "Compute the effective level of a heading.
  7215. This takes into account the setting of `org-odd-levels-only'."
  7216. (cond
  7217. ((zerop l) 0)
  7218. (org-odd-levels-only (1+ (floor (/ l 2))))
  7219. (t l)))
  7220. (defun org-level-increment ()
  7221. "Return the number of stars that will be added or removed at a
  7222. time to headlines when structure editing, based on the value of
  7223. `org-odd-levels-only'."
  7224. (if org-odd-levels-only 2 1))
  7225. (defun org-get-valid-level (level &optional change)
  7226. "Rectify a level change under the influence of `org-odd-levels-only'
  7227. LEVEL is a current level, CHANGE is by how much the level should be
  7228. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7229. even level numbers will become the next higher odd number."
  7230. (if org-odd-levels-only
  7231. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7232. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7233. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7234. (max 1 (+ level (or change 0)))))
  7235. (if (boundp 'define-obsolete-function-alias)
  7236. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7237. (define-obsolete-function-alias 'org-get-legal-level
  7238. 'org-get-valid-level)
  7239. (define-obsolete-function-alias 'org-get-legal-level
  7240. 'org-get-valid-level "23.1")))
  7241. (defun org-promote ()
  7242. "Promote the current heading higher up the tree."
  7243. (org-with-wide-buffer
  7244. (org-back-to-heading t)
  7245. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7246. after-change-functions))
  7247. (level (save-match-data (funcall outline-level)))
  7248. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7249. (diff (abs (- level (length up-head) -1))))
  7250. (cond
  7251. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7252. (replace-match "# " nil t))
  7253. ((= level 1)
  7254. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7255. (t (replace-match up-head nil t)))
  7256. (unless (= level 1)
  7257. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7258. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7259. (run-hooks 'org-after-promote-entry-hook))))
  7260. (defun org-demote ()
  7261. "Demote the current heading lower down the tree."
  7262. (org-with-wide-buffer
  7263. (org-back-to-heading t)
  7264. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7265. after-change-functions))
  7266. (level (save-match-data (funcall outline-level)))
  7267. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7268. (diff (abs (- level (length down-head) -1))))
  7269. (replace-match down-head nil t)
  7270. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7271. (when org-adapt-indentation (org-fixup-indentation diff))
  7272. (run-hooks 'org-after-demote-entry-hook))))
  7273. (defun org-cycle-level ()
  7274. "Cycle the level of an empty headline through possible states.
  7275. This goes first to child, then to parent, level, then up the hierarchy.
  7276. After top level, it switches back to sibling level."
  7277. (interactive)
  7278. (let ((org-adapt-indentation nil))
  7279. (when (org-point-at-end-of-empty-headline)
  7280. (setq this-command 'org-cycle-level) ; Only needed for caching
  7281. (let ((cur-level (org-current-level))
  7282. (prev-level (org-get-previous-line-level)))
  7283. (cond
  7284. ;; If first headline in file, promote to top-level.
  7285. ((= prev-level 0)
  7286. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7287. do (org-do-promote)))
  7288. ;; If same level as prev, demote one.
  7289. ((= prev-level cur-level)
  7290. (org-do-demote))
  7291. ;; If parent is top-level, promote to top level if not already.
  7292. ((= prev-level 1)
  7293. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7294. do (org-do-promote)))
  7295. ;; If top-level, return to prev-level.
  7296. ((= cur-level 1)
  7297. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7298. do (org-do-demote)))
  7299. ;; If less than prev-level, promote one.
  7300. ((< cur-level prev-level)
  7301. (org-do-promote))
  7302. ;; If deeper than prev-level, promote until higher than
  7303. ;; prev-level.
  7304. ((> cur-level prev-level)
  7305. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7306. do (org-do-promote))))
  7307. t))))
  7308. (defun org-map-tree (fun)
  7309. "Call FUN for every heading underneath the current one."
  7310. (org-back-to-heading)
  7311. (let ((level (funcall outline-level)))
  7312. (save-excursion
  7313. (funcall fun)
  7314. (while (and (progn
  7315. (outline-next-heading)
  7316. (> (funcall outline-level) level))
  7317. (not (eobp)))
  7318. (funcall fun)))))
  7319. (defun org-map-region (fun beg end)
  7320. "Call FUN for every heading between BEG and END."
  7321. (let ((org-ignore-region t))
  7322. (save-excursion
  7323. (setq end (copy-marker end))
  7324. (goto-char beg)
  7325. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7326. (< (point) end))
  7327. (funcall fun))
  7328. (while (and (progn
  7329. (outline-next-heading)
  7330. (< (point) end))
  7331. (not (eobp)))
  7332. (funcall fun)))))
  7333. (defun org-fixup-indentation (diff)
  7334. "Change the indentation in the current entry by DIFF.
  7335. DIFF is an integer. Indentation is done according to the
  7336. following rules:
  7337. - Planning information and property drawers are always indented
  7338. according to the new level of the headline;
  7339. - Footnote definitions and their contents are ignored;
  7340. - Inlinetasks' boundaries are not shifted;
  7341. - Empty lines are ignored;
  7342. - Other lines' indentation are shifted by DIFF columns, unless
  7343. it would introduce a structural change in the document, in
  7344. which case no shifting is done at all.
  7345. Assume point is at a heading or an inlinetask beginning."
  7346. (org-with-wide-buffer
  7347. (narrow-to-region (line-beginning-position)
  7348. (save-excursion
  7349. (if (org-with-limited-levels (org-at-heading-p))
  7350. (org-with-limited-levels (outline-next-heading))
  7351. (org-inlinetask-goto-end))
  7352. (point)))
  7353. (forward-line)
  7354. ;; Indent properly planning info and property drawer.
  7355. (when (org-looking-at-p org-planning-line-re)
  7356. (org-indent-line)
  7357. (forward-line))
  7358. (when (looking-at org-property-drawer-re)
  7359. (goto-char (match-end 0))
  7360. (forward-line)
  7361. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7362. (catch 'no-shift
  7363. (when (zerop diff) (throw 'no-shift nil))
  7364. ;; If DIFF is negative, first check if a shift is possible at all
  7365. ;; (e.g., it doesn't break structure). This can only happen if
  7366. ;; some contents are not properly indented.
  7367. (let ((case-fold-search t))
  7368. (when (< diff 0)
  7369. (let ((diff (- diff))
  7370. (forbidden-re (concat org-outline-regexp
  7371. "\\|"
  7372. (substring org-footnote-definition-re 1))))
  7373. (save-excursion
  7374. (while (not (eobp))
  7375. (cond
  7376. ((org-looking-at-p "[ \t]*$") (forward-line))
  7377. ((and (org-looking-at-p org-footnote-definition-re)
  7378. (let ((e (org-element-at-point)))
  7379. (and (eq (org-element-type e) 'footnote-definition)
  7380. (goto-char (org-element-property :end e))))))
  7381. ((org-looking-at-p org-outline-regexp) (forward-line))
  7382. ;; Give up if shifting would move before column 0 or
  7383. ;; if it would introduce a headline or a footnote
  7384. ;; definition.
  7385. (t
  7386. (skip-chars-forward " \t")
  7387. (let ((ind (current-column)))
  7388. (when (or (< ind diff)
  7389. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7390. (throw 'no-shift nil)))
  7391. ;; Ignore contents of example blocks and source
  7392. ;; blocks if their indentation is meant to be
  7393. ;; preserved. Jump to block's closing line.
  7394. (beginning-of-line)
  7395. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7396. (let ((e (org-element-at-point)))
  7397. (and (memq (org-element-type e)
  7398. '(example-block src-block))
  7399. (or org-src-preserve-indentation
  7400. (org-element-property :preserve-indent e))
  7401. (goto-char (org-element-property :end e))
  7402. (progn (skip-chars-backward " \r\t\n")
  7403. (beginning-of-line)
  7404. t))))
  7405. (forward-line))))))))
  7406. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7407. ;; by DIFF. Also skip contents of source or example blocks
  7408. ;; when indentation is meant to be preserved.
  7409. (while (not (eobp))
  7410. (cond
  7411. ((and (org-looking-at-p org-footnote-definition-re)
  7412. (let ((e (org-element-at-point)))
  7413. (and (eq (org-element-type e) 'footnote-definition)
  7414. (goto-char (org-element-property :end e))))))
  7415. ((org-looking-at-p org-outline-regexp) (forward-line))
  7416. ((org-looking-at-p "[ \t]*$") (forward-line))
  7417. (t
  7418. (org-indent-line-to (+ (org-get-indentation) diff))
  7419. (beginning-of-line)
  7420. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7421. (let ((e (org-element-at-point)))
  7422. (and (memq (org-element-type e)
  7423. '(example-block src-block))
  7424. (or org-src-preserve-indentation
  7425. (org-element-property :preserve-indent e))
  7426. (goto-char (org-element-property :end e))
  7427. (progn (skip-chars-backward " \r\t\n")
  7428. (beginning-of-line)
  7429. t))))
  7430. (forward-line)))))))))
  7431. (defun org-convert-to-odd-levels ()
  7432. "Convert an org-mode file with all levels allowed to one with odd levels.
  7433. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7434. level 5 etc."
  7435. (interactive)
  7436. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7437. (let ((outline-level 'org-outline-level)
  7438. (org-odd-levels-only nil) n)
  7439. (save-excursion
  7440. (goto-char (point-min))
  7441. (while (re-search-forward "^\\*\\*+ " nil t)
  7442. (setq n (- (length (match-string 0)) 2))
  7443. (while (>= (setq n (1- n)) 0)
  7444. (org-demote))
  7445. (end-of-line 1))))))
  7446. (defun org-convert-to-oddeven-levels ()
  7447. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7448. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7449. file contains a section with an even level, conversion would
  7450. destroy the structure of the file. An error is signaled in this
  7451. case."
  7452. (interactive)
  7453. (goto-char (point-min))
  7454. ;; First check if there are no even levels
  7455. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7456. (org-show-set-visibility 'canonical)
  7457. (error "Not all levels are odd in this file. Conversion not possible"))
  7458. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7459. (let ((outline-regexp org-outline-regexp)
  7460. (outline-level 'org-outline-level)
  7461. (org-odd-levels-only nil) n)
  7462. (save-excursion
  7463. (goto-char (point-min))
  7464. (while (re-search-forward "^\\*\\*+ " nil t)
  7465. (setq n (/ (1- (length (match-string 0))) 2))
  7466. (while (>= (setq n (1- n)) 0)
  7467. (org-promote))
  7468. (end-of-line 1))))))
  7469. (defun org-tr-level (n)
  7470. "Make N odd if required."
  7471. (if org-odd-levels-only (1+ (/ n 2)) n))
  7472. ;;; Vertical tree motion, cutting and pasting of subtrees
  7473. (defun org-move-subtree-up (&optional arg)
  7474. "Move the current subtree up past ARG headlines of the same level."
  7475. (interactive "p")
  7476. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7477. (defun org-move-subtree-down (&optional arg)
  7478. "Move the current subtree down past ARG headlines of the same level."
  7479. (interactive "p")
  7480. (setq arg (prefix-numeric-value arg))
  7481. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7482. 'org-get-last-sibling))
  7483. (ins-point (make-marker))
  7484. (cnt (abs arg))
  7485. (col (current-column))
  7486. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7487. ;; Select the tree
  7488. (org-back-to-heading)
  7489. (setq beg0 (point))
  7490. (save-excursion
  7491. (setq ne-beg (org-back-over-empty-lines))
  7492. (setq beg (point)))
  7493. (save-match-data
  7494. (save-excursion (outline-end-of-heading)
  7495. (setq folded (outline-invisible-p)))
  7496. (progn (org-end-of-subtree nil t)
  7497. (unless (eobp) (backward-char))))
  7498. (outline-next-heading)
  7499. (setq ne-end (org-back-over-empty-lines))
  7500. (setq end (point))
  7501. (goto-char beg0)
  7502. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7503. ;; include less whitespace
  7504. (save-excursion
  7505. (goto-char beg)
  7506. (forward-line (- ne-beg ne-end))
  7507. (setq beg (point))))
  7508. ;; Find insertion point, with error handling
  7509. (while (> cnt 0)
  7510. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7511. (progn (goto-char beg0)
  7512. (user-error "Cannot move past superior level or buffer limit")))
  7513. (setq cnt (1- cnt)))
  7514. (if (> arg 0)
  7515. ;; Moving forward - still need to move over subtree
  7516. (progn (org-end-of-subtree t t)
  7517. (save-excursion
  7518. (org-back-over-empty-lines)
  7519. (or (bolp) (newline)))))
  7520. (setq ne-ins (org-back-over-empty-lines))
  7521. (move-marker ins-point (point))
  7522. (setq txt (buffer-substring beg end))
  7523. (org-save-markers-in-region beg end)
  7524. (delete-region beg end)
  7525. (org-remove-empty-overlays-at beg)
  7526. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7527. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7528. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7529. (let ((bbb (point)))
  7530. (insert-before-markers txt)
  7531. (org-reinstall-markers-in-region bbb)
  7532. (move-marker ins-point bbb))
  7533. (or (bolp) (insert "\n"))
  7534. (setq ins-end (point))
  7535. (goto-char ins-point)
  7536. (org-skip-whitespace)
  7537. (when (and (< arg 0)
  7538. (org-first-sibling-p)
  7539. (> ne-ins ne-beg))
  7540. ;; Move whitespace back to beginning
  7541. (save-excursion
  7542. (goto-char ins-end)
  7543. (let ((kill-whole-line t))
  7544. (kill-line (- ne-ins ne-beg)) (point)))
  7545. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7546. (move-marker ins-point nil)
  7547. (if folded
  7548. (hide-subtree)
  7549. (org-show-entry)
  7550. (show-children)
  7551. (org-cycle-hide-drawers 'children))
  7552. (org-clean-visibility-after-subtree-move)
  7553. ;; move back to the initial column we were at
  7554. (move-to-column col)))
  7555. (defvar org-subtree-clip ""
  7556. "Clipboard for cut and paste of subtrees.
  7557. This is actually only a copy of the kill, because we use the normal kill
  7558. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7559. (defvar org-subtree-clip-folded nil
  7560. "Was the last copied subtree folded?
  7561. This is used to fold the tree back after pasting.")
  7562. (defun org-cut-subtree (&optional n)
  7563. "Cut the current subtree into the clipboard.
  7564. With prefix arg N, cut this many sequential subtrees.
  7565. This is a short-hand for marking the subtree and then cutting it."
  7566. (interactive "p")
  7567. (org-copy-subtree n 'cut))
  7568. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7569. "Copy the current subtree it in the clipboard.
  7570. With prefix arg N, copy this many sequential subtrees.
  7571. This is a short-hand for marking the subtree and then copying it.
  7572. If CUT is non-nil, actually cut the subtree.
  7573. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7574. of some markers in the region, even if CUT is non-nil. This is
  7575. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7576. (interactive "p")
  7577. (let (beg end folded (beg0 (point)))
  7578. (if (org-called-interactively-p 'any)
  7579. (org-back-to-heading nil) ; take what looks like a subtree
  7580. (org-back-to-heading t)) ; take what is really there
  7581. (setq beg (point))
  7582. (skip-chars-forward " \t\r\n")
  7583. (save-match-data
  7584. (if nosubtrees
  7585. (outline-next-heading)
  7586. (save-excursion (outline-end-of-heading)
  7587. (setq folded (outline-invisible-p)))
  7588. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7589. (org-end-of-subtree t t)))
  7590. ;; Include the end of an inlinetask
  7591. (when (and (featurep 'org-inlinetask)
  7592. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7593. "END[ \t]*$")))
  7594. (end-of-line))
  7595. (setq end (point))
  7596. (goto-char beg0)
  7597. (when (> end beg)
  7598. (setq org-subtree-clip-folded folded)
  7599. (when (or cut force-store-markers)
  7600. (org-save-markers-in-region beg end))
  7601. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7602. (setq org-subtree-clip (current-kill 0))
  7603. (message "%s: Subtree(s) with %d characters"
  7604. (if cut "Cut" "Copied")
  7605. (length org-subtree-clip)))))
  7606. (defun org-paste-subtree (&optional level tree for-yank remove)
  7607. "Paste the clipboard as a subtree, with modification of headline level.
  7608. The entire subtree is promoted or demoted in order to match a new headline
  7609. level.
  7610. If the cursor is at the beginning of a headline, the same level as
  7611. that headline is used to paste the tree.
  7612. If not, the new level is derived from the *visible* headings
  7613. before and after the insertion point, and taken to be the inferior headline
  7614. level of the two. So if the previous visible heading is level 3 and the
  7615. next is level 4 (or vice versa), level 4 will be used for insertion.
  7616. This makes sure that the subtree remains an independent subtree and does
  7617. not swallow low level entries.
  7618. You can also force a different level, either by using a numeric prefix
  7619. argument, or by inserting the heading marker by hand. For example, if the
  7620. cursor is after \"*****\", then the tree will be shifted to level 5.
  7621. If optional TREE is given, use this text instead of the kill ring.
  7622. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7623. move back over whitespace before inserting, and move point to the end of
  7624. the inserted text when done.
  7625. When REMOVE is non-nil, remove the subtree from the clipboard."
  7626. (interactive "P")
  7627. (setq tree (or tree (and kill-ring (current-kill 0))))
  7628. (unless (org-kill-is-subtree-p tree)
  7629. (user-error "%s"
  7630. (substitute-command-keys
  7631. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7632. (org-with-limited-levels
  7633. (let* ((visp (not (outline-invisible-p)))
  7634. (txt tree)
  7635. (^re_ "\\(\\*+\\)[ \t]*")
  7636. (old-level (if (string-match org-outline-regexp-bol txt)
  7637. (- (match-end 0) (match-beginning 0) 1)
  7638. -1))
  7639. (force-level (cond (level (prefix-numeric-value level))
  7640. ((and (looking-at "[ \t]*$")
  7641. (string-match
  7642. "^\\*+$" (buffer-substring
  7643. (point-at-bol) (point))))
  7644. (- (match-end 0) (match-beginning 0)))
  7645. ((and (bolp)
  7646. (looking-at org-outline-regexp))
  7647. (- (match-end 0) (point) 1))))
  7648. (previous-level (save-excursion
  7649. (condition-case nil
  7650. (progn
  7651. (outline-previous-visible-heading 1)
  7652. (if (looking-at ^re_)
  7653. (- (match-end 0) (match-beginning 0) 1)
  7654. 1))
  7655. (error 1))))
  7656. (next-level (save-excursion
  7657. (condition-case nil
  7658. (progn
  7659. (or (looking-at org-outline-regexp)
  7660. (outline-next-visible-heading 1))
  7661. (if (looking-at ^re_)
  7662. (- (match-end 0) (match-beginning 0) 1)
  7663. 1))
  7664. (error 1))))
  7665. (new-level (or force-level (max previous-level next-level)))
  7666. (shift (if (or (= old-level -1)
  7667. (= new-level -1)
  7668. (= old-level new-level))
  7669. 0
  7670. (- new-level old-level)))
  7671. (delta (if (> shift 0) -1 1))
  7672. (func (if (> shift 0) 'org-demote 'org-promote))
  7673. (org-odd-levels-only nil)
  7674. beg end newend)
  7675. ;; Remove the forced level indicator
  7676. (if force-level
  7677. (delete-region (point-at-bol) (point)))
  7678. ;; Paste
  7679. (beginning-of-line (if (bolp) 1 2))
  7680. (setq beg (point))
  7681. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7682. (insert-before-markers txt)
  7683. (unless (string-match "\n\\'" txt) (insert "\n"))
  7684. (setq newend (point))
  7685. (org-reinstall-markers-in-region beg)
  7686. (setq end (point))
  7687. (goto-char beg)
  7688. (skip-chars-forward " \t\n\r")
  7689. (setq beg (point))
  7690. (if (and (outline-invisible-p) visp)
  7691. (save-excursion (outline-show-heading)))
  7692. ;; Shift if necessary
  7693. (unless (= shift 0)
  7694. (save-restriction
  7695. (narrow-to-region beg end)
  7696. (while (not (= shift 0))
  7697. (org-map-region func (point-min) (point-max))
  7698. (setq shift (+ delta shift)))
  7699. (goto-char (point-min))
  7700. (setq newend (point-max))))
  7701. (when (or (org-called-interactively-p 'interactive) for-yank)
  7702. (message "Clipboard pasted as level %d subtree" new-level))
  7703. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7704. kill-ring
  7705. (eq org-subtree-clip (current-kill 0))
  7706. org-subtree-clip-folded)
  7707. ;; The tree was folded before it was killed/copied
  7708. (hide-subtree))
  7709. (and for-yank (goto-char newend))
  7710. (and remove (setq kill-ring (cdr kill-ring))))))
  7711. (defun org-kill-is-subtree-p (&optional txt)
  7712. "Check if the current kill is an outline subtree, or a set of trees.
  7713. Returns nil if kill does not start with a headline, or if the first
  7714. headline level is not the largest headline level in the tree.
  7715. So this will actually accept several entries of equal levels as well,
  7716. which is OK for `org-paste-subtree'.
  7717. If optional TXT is given, check this string instead of the current kill."
  7718. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7719. (re (org-get-limited-outline-regexp))
  7720. (^re (concat "^" re))
  7721. (start-level (and kill
  7722. (string-match
  7723. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7724. kill)
  7725. (- (match-end 2) (match-beginning 2) 1)))
  7726. (start (1+ (or (match-beginning 2) -1))))
  7727. (if (not start-level)
  7728. (progn
  7729. nil) ;; does not even start with a heading
  7730. (catch 'exit
  7731. (while (setq start (string-match ^re kill (1+ start)))
  7732. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7733. (throw 'exit nil)))
  7734. t))))
  7735. (defvar org-markers-to-move nil
  7736. "Markers that should be moved with a cut-and-paste operation.
  7737. Those markers are stored together with their positions relative to
  7738. the start of the region.")
  7739. (defun org-save-markers-in-region (beg end)
  7740. "Check markers in region.
  7741. If these markers are between BEG and END, record their position relative
  7742. to BEG, so that after moving the block of text, we can put the markers back
  7743. into place.
  7744. This function gets called just before an entry or tree gets cut from the
  7745. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7746. called immediately, to move the markers with the entries."
  7747. (setq org-markers-to-move nil)
  7748. (when (featurep 'org-clock)
  7749. (org-clock-save-markers-for-cut-and-paste beg end))
  7750. (when (featurep 'org-agenda)
  7751. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7752. (defun org-check-and-save-marker (marker beg end)
  7753. "Check if MARKER is between BEG and END.
  7754. If yes, remember the marker and the distance to BEG."
  7755. (when (and (marker-buffer marker)
  7756. (equal (marker-buffer marker) (current-buffer)))
  7757. (if (and (>= marker beg) (< marker end))
  7758. (push (cons marker (- marker beg)) org-markers-to-move))))
  7759. (defun org-reinstall-markers-in-region (beg)
  7760. "Move all remembered markers to their position relative to BEG."
  7761. (mapc (lambda (x)
  7762. (move-marker (car x) (+ beg (cdr x))))
  7763. org-markers-to-move)
  7764. (setq org-markers-to-move nil))
  7765. (defun org-narrow-to-subtree ()
  7766. "Narrow buffer to the current subtree."
  7767. (interactive)
  7768. (save-excursion
  7769. (save-match-data
  7770. (org-with-limited-levels
  7771. (narrow-to-region
  7772. (progn (org-back-to-heading t) (point))
  7773. (progn (org-end-of-subtree t t)
  7774. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7775. (point)))))))
  7776. (defun org-narrow-to-block ()
  7777. "Narrow buffer to the current block."
  7778. (interactive)
  7779. (let* ((case-fold-search t)
  7780. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7781. "^[ \t]*#\\+end_.*")))
  7782. (if blockp
  7783. (narrow-to-region (car blockp) (cdr blockp))
  7784. (user-error "Not in a block"))))
  7785. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7786. "Clone the task (subtree) at point N times.
  7787. The clones will be inserted as siblings.
  7788. In interactive use, the user will be prompted for the number of
  7789. clones to be produced. If the entry has a timestamp, the user
  7790. will also be prompted for a time shift, which may be a repeater
  7791. as used in time stamps, for example `+3d'. To disable this,
  7792. you can call the function with a universal prefix argument.
  7793. When a valid repeater is given and the entry contains any time
  7794. stamps, the clones will become a sequence in time, with time
  7795. stamps in the subtree shifted for each clone produced. If SHIFT
  7796. is nil or the empty string, time stamps will be left alone. The
  7797. ID property of the original subtree is removed.
  7798. If the original subtree did contain time stamps with a repeater,
  7799. the following will happen:
  7800. - the repeater will be removed in each clone
  7801. - an additional clone will be produced, with the current, unshifted
  7802. date(s) in the entry.
  7803. - the original entry will be placed *after* all the clones, with
  7804. repeater intact.
  7805. - the start days in the repeater in the original entry will be shifted
  7806. to past the last clone.
  7807. In this way you can spell out a number of instances of a repeating task,
  7808. and still retain the repeater to cover future instances of the task.
  7809. As described above, N+1 clones are produced when the original
  7810. subtree has a repeater. Setting N to 0, then, can be used to
  7811. remove the repeater from a subtree and create a shifted clone
  7812. with the original repeater."
  7813. (interactive "nNumber of clones to produce: ")
  7814. (let ((shift
  7815. (or shift
  7816. (if (and (not (equal current-prefix-arg '(4)))
  7817. (save-excursion
  7818. (re-search-forward org-ts-regexp-both
  7819. (save-excursion
  7820. (org-end-of-subtree t)
  7821. (point)) t)))
  7822. (read-from-minibuffer
  7823. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7824. ""))) ;; No time shift
  7825. (n-no-remove -1)
  7826. (drawer-re org-drawer-regexp)
  7827. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7828. beg end template task idprop
  7829. shift-n shift-what doshift nmin nmax)
  7830. (unless (wholenump n)
  7831. (user-error "Invalid number of replications %s" n))
  7832. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7833. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7834. shift)))
  7835. (user-error "Invalid shift specification %s" shift))
  7836. (when doshift
  7837. (setq shift-n (string-to-number (match-string 1 shift))
  7838. shift-what (cdr (assoc (match-string 2 shift)
  7839. '(("d" . day) ("w" . week)
  7840. ("m" . month) ("y" . year))))))
  7841. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7842. (setq nmin 1 nmax n)
  7843. (org-back-to-heading t)
  7844. (setq beg (point))
  7845. (setq idprop (org-entry-get nil "ID"))
  7846. (org-end-of-subtree t t)
  7847. (or (bolp) (insert "\n"))
  7848. (setq end (point))
  7849. (setq template (buffer-substring beg end))
  7850. (when (and doshift
  7851. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7852. (delete-region beg end)
  7853. (setq end beg)
  7854. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7855. (goto-char end)
  7856. (loop for n from nmin to nmax do
  7857. ;; prepare clone
  7858. (with-temp-buffer
  7859. (insert template)
  7860. (org-mode)
  7861. (goto-char (point-min))
  7862. (org-show-subtree)
  7863. (and idprop (if org-clone-delete-id
  7864. (org-entry-delete nil "ID")
  7865. (org-id-get-create t)))
  7866. (unless (= n 0)
  7867. (while (re-search-forward org-clock-re nil t)
  7868. (kill-whole-line))
  7869. (goto-char (point-min))
  7870. (while (re-search-forward drawer-re nil t)
  7871. (org-remove-empty-drawer-at (point))))
  7872. (goto-char (point-min))
  7873. (when doshift
  7874. (while (re-search-forward org-ts-regexp-both nil t)
  7875. (org-timestamp-change (* n shift-n) shift-what))
  7876. (unless (= n n-no-remove)
  7877. (goto-char (point-min))
  7878. (while (re-search-forward org-ts-regexp nil t)
  7879. (save-excursion
  7880. (goto-char (match-beginning 0))
  7881. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7882. (delete-region (match-beginning 1) (match-end 1)))))))
  7883. (setq task (buffer-string)))
  7884. (insert task))
  7885. (goto-char beg)))
  7886. ;;; Outline Sorting
  7887. (defun org-sort (with-case)
  7888. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7889. Optional argument WITH-CASE means sort case-sensitively."
  7890. (interactive "P")
  7891. (cond
  7892. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7893. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7894. (t
  7895. (org-call-with-arg 'org-sort-entries with-case))))
  7896. (defun org-sort-remove-invisible (s)
  7897. "Remove invisible links from string S."
  7898. (remove-text-properties 0 (length s) org-rm-props s)
  7899. (while (string-match org-bracket-link-regexp s)
  7900. (setq s (replace-match (if (match-end 2)
  7901. (match-string 3 s)
  7902. (match-string 1 s))
  7903. t t s)))
  7904. (let ((st (format " %s " s)))
  7905. (while (string-match org-emph-re st)
  7906. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7907. (setq s (substring st 1 -1)))
  7908. s)
  7909. (defvar org-priority-regexp) ; defined later in the file
  7910. (defvar org-after-sorting-entries-or-items-hook nil
  7911. "Hook that is run after a bunch of entries or items have been sorted.
  7912. When children are sorted, the cursor is in the parent line when this
  7913. hook gets called. When a region or a plain list is sorted, the cursor
  7914. will be in the first entry of the sorted region/list.")
  7915. (defun org-sort-entries
  7916. (&optional with-case sorting-type getkey-func compare-func property)
  7917. "Sort entries on a certain level of an outline tree.
  7918. If there is an active region, the entries in the region are sorted.
  7919. Else, if the cursor is before the first entry, sort the top-level items.
  7920. Else, the children of the entry at point are sorted.
  7921. Sorting can be alphabetically, numerically, by date/time as given by
  7922. a time stamp, by a property, by priority order, or by a custom function.
  7923. The command prompts for the sorting type unless it has been given to the
  7924. function through the SORTING-TYPE argument, which needs to be a character,
  7925. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7926. the precise meaning of each character:
  7927. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7928. c By creation time, which is assumed to be the first inactive time stamp
  7929. at the beginning of a line.
  7930. d By deadline date/time.
  7931. k By clocking time.
  7932. n Numerically, by converting the beginning of the entry/item to a number.
  7933. o By order of TODO keywords.
  7934. p By priority according to the cookie.
  7935. r By the value of a property.
  7936. s By scheduled date/time.
  7937. t By date/time, either the first active time stamp in the entry, or, if
  7938. none exist, by the first inactive one.
  7939. Capital letters will reverse the sort order.
  7940. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7941. called with point at the beginning of the record. It must return either
  7942. a string or a number that should serve as the sorting key for that record.
  7943. Comparing entries ignores case by default. However, with an optional argument
  7944. WITH-CASE, the sorting considers case as well.
  7945. Sorting is done against the visible part of the headlines, it ignores hidden
  7946. links.
  7947. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7948. (interactive "P")
  7949. (let ((case-func (if with-case 'identity 'downcase))
  7950. (cmstr
  7951. ;; The clock marker is lost when using `sort-subr', let's
  7952. ;; store the clocking string.
  7953. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7954. (save-excursion
  7955. (goto-char org-clock-marker)
  7956. (buffer-substring-no-properties (line-beginning-position)
  7957. (point)))))
  7958. start beg end stars re re2
  7959. txt what tmp)
  7960. ;; Find beginning and end of region to sort
  7961. (cond
  7962. ((org-region-active-p)
  7963. ;; we will sort the region
  7964. (setq end (region-end)
  7965. what "region")
  7966. (goto-char (region-beginning))
  7967. (if (not (org-at-heading-p)) (outline-next-heading))
  7968. (setq start (point)))
  7969. ((or (org-at-heading-p)
  7970. (ignore-errors (progn (org-back-to-heading) t)))
  7971. ;; we will sort the children of the current headline
  7972. (org-back-to-heading)
  7973. (setq start (point)
  7974. end (progn (org-end-of-subtree t t)
  7975. (or (bolp) (insert "\n"))
  7976. (when (>= (org-back-over-empty-lines) 1)
  7977. (forward-line 1))
  7978. (point))
  7979. what "children")
  7980. (goto-char start)
  7981. (show-subtree)
  7982. (outline-next-heading))
  7983. (t
  7984. ;; we will sort the top-level entries in this file
  7985. (goto-char (point-min))
  7986. (or (org-at-heading-p) (outline-next-heading))
  7987. (setq start (point))
  7988. (goto-char (point-max))
  7989. (beginning-of-line 1)
  7990. (when (looking-at ".*?\\S-")
  7991. ;; File ends in a non-white line
  7992. (end-of-line 1)
  7993. (insert "\n"))
  7994. (setq end (point-max))
  7995. (setq what "top-level")
  7996. (goto-char start)
  7997. (show-all)))
  7998. (setq beg (point))
  7999. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  8000. (looking-at "\\(\\*+\\)")
  8001. (setq stars (match-string 1)
  8002. re (concat "^" (regexp-quote stars) " +")
  8003. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8004. txt (buffer-substring beg end))
  8005. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8006. (if (and (not (equal stars "*")) (string-match re2 txt))
  8007. (user-error "Region to sort contains a level above the first entry"))
  8008. (unless sorting-type
  8009. (message
  8010. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8011. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8012. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8013. what)
  8014. (setq sorting-type (read-char-exclusive))
  8015. (unless getkey-func
  8016. (and (= (downcase sorting-type) ?f)
  8017. (setq getkey-func
  8018. (org-icompleting-read "Sort using function: "
  8019. obarray 'fboundp t nil nil))
  8020. (setq getkey-func (intern getkey-func))))
  8021. (and (= (downcase sorting-type) ?r)
  8022. (not property)
  8023. (setq property
  8024. (org-icompleting-read "Property: "
  8025. (mapcar 'list (org-buffer-property-keys t))
  8026. nil t))))
  8027. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8028. (message "Sorting entries...")
  8029. (save-restriction
  8030. (narrow-to-region start end)
  8031. (let ((dcst (downcase sorting-type))
  8032. (case-fold-search nil)
  8033. (now (current-time)))
  8034. (sort-subr
  8035. (/= dcst sorting-type)
  8036. ;; This function moves to the beginning character of the "record" to
  8037. ;; be sorted.
  8038. (lambda nil
  8039. (if (re-search-forward re nil t)
  8040. (goto-char (match-beginning 0))
  8041. (goto-char (point-max))))
  8042. ;; This function moves to the last character of the "record" being
  8043. ;; sorted.
  8044. (lambda nil
  8045. (save-match-data
  8046. (condition-case nil
  8047. (outline-forward-same-level 1)
  8048. (error
  8049. (goto-char (point-max))))))
  8050. ;; This function returns the value that gets sorted against.
  8051. (lambda nil
  8052. (cond
  8053. ((= dcst ?n)
  8054. (if (looking-at org-complex-heading-regexp)
  8055. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8056. nil))
  8057. ((= dcst ?a)
  8058. (if (looking-at org-complex-heading-regexp)
  8059. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8060. nil))
  8061. ((= dcst ?k)
  8062. (or (get-text-property (point) :org-clock-minutes) 0))
  8063. ((= dcst ?t)
  8064. (let ((end (save-excursion (outline-next-heading) (point))))
  8065. (if (or (re-search-forward org-ts-regexp end t)
  8066. (re-search-forward org-ts-regexp-both end t))
  8067. (org-time-string-to-seconds (match-string 0))
  8068. (org-float-time now))))
  8069. ((= dcst ?c)
  8070. (let ((end (save-excursion (outline-next-heading) (point))))
  8071. (if (re-search-forward
  8072. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8073. end t)
  8074. (org-time-string-to-seconds (match-string 0))
  8075. (org-float-time now))))
  8076. ((= dcst ?s)
  8077. (let ((end (save-excursion (outline-next-heading) (point))))
  8078. (if (re-search-forward org-scheduled-time-regexp end t)
  8079. (org-time-string-to-seconds (match-string 1))
  8080. (org-float-time now))))
  8081. ((= dcst ?d)
  8082. (let ((end (save-excursion (outline-next-heading) (point))))
  8083. (if (re-search-forward org-deadline-time-regexp end t)
  8084. (org-time-string-to-seconds (match-string 1))
  8085. (org-float-time now))))
  8086. ((= dcst ?p)
  8087. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8088. (string-to-char (match-string 2))
  8089. org-default-priority))
  8090. ((= dcst ?r)
  8091. (or (org-entry-get nil property) ""))
  8092. ((= dcst ?o)
  8093. (if (looking-at org-complex-heading-regexp)
  8094. (let* ((m (match-string 2))
  8095. (s (if (member m org-done-keywords) '- '+)))
  8096. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8097. ((= dcst ?f)
  8098. (if getkey-func
  8099. (progn
  8100. (setq tmp (funcall getkey-func))
  8101. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8102. tmp)
  8103. (error "Invalid key function `%s'" getkey-func)))
  8104. (t (error "Invalid sorting type `%c'" sorting-type))))
  8105. nil
  8106. (cond
  8107. ((= dcst ?a) 'string<)
  8108. ((= dcst ?f) compare-func)
  8109. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8110. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8111. ;; Reset the clock marker if needed
  8112. (when cmstr
  8113. (save-excursion
  8114. (goto-char start)
  8115. (search-forward cmstr nil t)
  8116. (move-marker org-clock-marker (point))))
  8117. (message "Sorting entries...done")))
  8118. ;;; The orgstruct minor mode
  8119. ;; Define a minor mode which can be used in other modes in order to
  8120. ;; integrate the org-mode structure editing commands.
  8121. ;; This is really a hack, because the org-mode structure commands use
  8122. ;; keys which normally belong to the major mode. Here is how it
  8123. ;; works: The minor mode defines all the keys necessary to operate the
  8124. ;; structure commands, but wraps the commands into a function which
  8125. ;; tests if the cursor is currently at a headline or a plain list
  8126. ;; item. If that is the case, the structure command is used,
  8127. ;; temporarily setting many Org-mode variables like regular
  8128. ;; expressions for filling etc. However, when any of those keys is
  8129. ;; used at a different location, function uses `key-binding' to look
  8130. ;; up if the key has an associated command in another currently active
  8131. ;; keymap (minor modes, major mode, global), and executes that
  8132. ;; command. There might be problems if any of the keys is otherwise
  8133. ;; used as a prefix key.
  8134. (defcustom orgstruct-heading-prefix-regexp ""
  8135. "Regexp that matches the custom prefix of Org headlines in
  8136. orgstruct(++)-mode."
  8137. :group 'org
  8138. :version "24.4"
  8139. :package-version '(Org . "8.3")
  8140. :type 'regexp)
  8141. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8142. (defcustom orgstruct-setup-hook nil
  8143. "Hook run after orgstruct-mode-map is filled."
  8144. :group 'org
  8145. :version "24.4"
  8146. :package-version '(Org . "8.0")
  8147. :type 'hook)
  8148. (defvar orgstruct-initialized nil)
  8149. (defvar org-local-vars nil
  8150. "List of local variables, for use by `orgstruct-mode'.")
  8151. ;;;###autoload
  8152. (define-minor-mode orgstruct-mode
  8153. "Toggle the minor mode `orgstruct-mode'.
  8154. This mode is for using Org-mode structure commands in other
  8155. modes. The following keys behave as if Org-mode were active, if
  8156. the cursor is on a headline, or on a plain list item (both as
  8157. defined by Org-mode)."
  8158. nil " OrgStruct" (make-sparse-keymap)
  8159. (funcall (if orgstruct-mode
  8160. 'add-to-invisibility-spec
  8161. 'remove-from-invisibility-spec)
  8162. '(outline . t))
  8163. (when orgstruct-mode
  8164. (org-load-modules-maybe)
  8165. (unless orgstruct-initialized
  8166. (orgstruct-setup)
  8167. (setq orgstruct-initialized t))))
  8168. ;;;###autoload
  8169. (defun turn-on-orgstruct ()
  8170. "Unconditionally turn on `orgstruct-mode'."
  8171. (orgstruct-mode 1))
  8172. (defvar org-fb-vars nil)
  8173. (make-variable-buffer-local 'org-fb-vars)
  8174. (defun orgstruct++-mode (&optional arg)
  8175. "Toggle `orgstruct-mode', the enhanced version of it.
  8176. In addition to setting orgstruct-mode, this also exports all
  8177. indentation and autofilling variables from org-mode into the
  8178. buffer. It will also recognize item context in multiline items."
  8179. (interactive "P")
  8180. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8181. (if (< arg 1)
  8182. (progn (orgstruct-mode -1)
  8183. (mapc (lambda(v)
  8184. (org-set-local (car v)
  8185. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8186. org-fb-vars))
  8187. (orgstruct-mode 1)
  8188. (setq org-fb-vars nil)
  8189. (unless org-local-vars
  8190. (setq org-local-vars (org-get-local-variables)))
  8191. (let (var val)
  8192. (mapc
  8193. (lambda (x)
  8194. (when (string-match
  8195. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8196. (symbol-name (car x)))
  8197. (setq var (car x) val (nth 1 x))
  8198. (push (list var `(quote ,(eval var))) org-fb-vars)
  8199. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8200. org-local-vars)
  8201. (org-set-local 'orgstruct-is-++ t))))
  8202. (defvar orgstruct-is-++ nil
  8203. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8204. (make-variable-buffer-local 'orgstruct-is-++)
  8205. ;;;###autoload
  8206. (defun turn-on-orgstruct++ ()
  8207. "Unconditionally turn on `orgstruct++-mode'."
  8208. (orgstruct++-mode 1))
  8209. (defun orgstruct-error ()
  8210. "Error when there is no default binding for a structure key."
  8211. (interactive)
  8212. (funcall (if (fboundp 'user-error)
  8213. 'user-error
  8214. 'error)
  8215. "This key has no function outside structure elements"))
  8216. (defun orgstruct-setup ()
  8217. "Setup orgstruct keymap."
  8218. (dolist (cell '((org-demote . t)
  8219. (org-metaleft . t)
  8220. (org-metaright . t)
  8221. (org-promote . t)
  8222. (org-shiftmetaleft . t)
  8223. (org-shiftmetaright . t)
  8224. org-backward-element
  8225. org-backward-heading-same-level
  8226. org-ctrl-c-ret
  8227. org-ctrl-c-minus
  8228. org-ctrl-c-star
  8229. org-cycle
  8230. org-forward-heading-same-level
  8231. org-insert-heading
  8232. org-insert-heading-respect-content
  8233. org-kill-note-or-show-branches
  8234. org-mark-subtree
  8235. org-meta-return
  8236. org-metadown
  8237. org-metaup
  8238. org-narrow-to-subtree
  8239. org-promote-subtree
  8240. org-reveal
  8241. org-shiftdown
  8242. org-shiftleft
  8243. org-shiftmetadown
  8244. org-shiftmetaup
  8245. org-shiftright
  8246. org-shifttab
  8247. org-shifttab
  8248. org-shiftup
  8249. org-show-subtree
  8250. org-sort
  8251. org-up-element
  8252. outline-demote
  8253. outline-next-visible-heading
  8254. outline-previous-visible-heading
  8255. outline-promote
  8256. outline-up-heading
  8257. show-children))
  8258. (let ((f (or (car-safe cell) cell))
  8259. (disable-when-heading-prefix (cdr-safe cell)))
  8260. (when (fboundp f)
  8261. (let ((new-bindings))
  8262. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8263. (where-is-internal f outline-mode-map)))
  8264. (push binding new-bindings)
  8265. ;; TODO use local-function-key-map
  8266. (dolist (rep '(("<tab>" . "TAB")
  8267. ("<return>" . "RET")
  8268. ("<escape>" . "ESC")
  8269. ("<delete>" . "DEL")))
  8270. (setq binding (read-kbd-macro
  8271. (let ((case-fold-search))
  8272. (replace-regexp-in-string
  8273. (regexp-quote (cdr rep))
  8274. (car rep)
  8275. (key-description binding)))))
  8276. (pushnew binding new-bindings :test 'equal)))
  8277. (dolist (binding new-bindings)
  8278. (let ((key (lookup-key orgstruct-mode-map binding)))
  8279. (when (or (not key) (numberp key))
  8280. (ignore-errors
  8281. (org-defkey orgstruct-mode-map
  8282. binding
  8283. (orgstruct-make-binding
  8284. f binding disable-when-heading-prefix))))))))))
  8285. (run-hooks 'orgstruct-setup-hook))
  8286. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8287. "Create a function for binding in the structure minor mode.
  8288. FUN is the command to call inside a table. KEY is the key that
  8289. should be checked in for a command to execute outside of tables.
  8290. Non-nil `disable-when-heading-prefix' means to disable the command
  8291. if `orgstruct-heading-prefix-regexp' is not empty."
  8292. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8293. (let ((nname name)
  8294. (i 0))
  8295. (while (fboundp (intern nname))
  8296. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8297. (setq name (intern nname)))
  8298. (eval
  8299. (let ((bindings '((org-heading-regexp
  8300. (concat "^"
  8301. orgstruct-heading-prefix-regexp
  8302. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8303. (org-outline-regexp
  8304. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8305. (org-outline-regexp-bol
  8306. (concat "^" org-outline-regexp))
  8307. (outline-regexp org-outline-regexp)
  8308. (outline-heading-end-regexp "\n")
  8309. (outline-level 'org-outline-level)
  8310. (outline-heading-alist))))
  8311. `(defun ,name (arg)
  8312. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8313. "Outside of structure, run the binding of `"
  8314. (key-description key) "'."
  8315. (when disable-when-heading-prefix
  8316. (concat
  8317. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8318. "back to the default binding due to limitations of Org's implementation of\n"
  8319. "`" (symbol-name fun) "'.")))
  8320. (interactive "p")
  8321. (let* ((disable
  8322. ,(and disable-when-heading-prefix
  8323. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8324. (fallback
  8325. (or disable
  8326. (not
  8327. (let* ,bindings
  8328. (org-context-p 'headline 'item
  8329. ,(when (memq fun
  8330. '(org-insert-heading
  8331. org-insert-heading-respect-content
  8332. org-meta-return))
  8333. '(when orgstruct-is-++
  8334. 'item-body))))))))
  8335. (if fallback
  8336. (let* ((orgstruct-mode)
  8337. (binding
  8338. (let ((key ,key))
  8339. (catch 'exit
  8340. (dolist
  8341. (rep
  8342. '(nil
  8343. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8344. ("<\\([^>]*\\)return>" . "\\1RET")
  8345. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8346. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8347. nil)
  8348. (when rep
  8349. (setq key (read-kbd-macro
  8350. (let ((case-fold-search))
  8351. (replace-regexp-in-string
  8352. (car rep)
  8353. (cdr rep)
  8354. (key-description key))))))
  8355. (when (key-binding key)
  8356. (throw 'exit (key-binding key))))))))
  8357. (if (keymapp binding)
  8358. (org-set-transient-map binding)
  8359. (let ((func (or binding
  8360. (unless disable
  8361. 'orgstruct-error))))
  8362. (when func
  8363. (call-interactively func)))))
  8364. (org-run-like-in-org-mode
  8365. (lambda ()
  8366. (interactive)
  8367. (let* ,bindings
  8368. (call-interactively ',fun)))))))))
  8369. name))
  8370. (defun org-contextualize-keys (alist contexts)
  8371. "Return valid elements in ALIST depending on CONTEXTS.
  8372. `org-agenda-custom-commands' or `org-capture-templates' are the
  8373. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8374. or `org-capture-templates-contexts' are the associated contexts
  8375. definitions."
  8376. (let ((contexts
  8377. ;; normalize contexts
  8378. (mapcar
  8379. (lambda(c) (cond ((listp (cadr c))
  8380. (list (car c) (car c) (cadr c)))
  8381. ((string= "" (cadr c))
  8382. (list (car c) (car c) (caddr c)))
  8383. (t c)))
  8384. contexts))
  8385. (a alist) r s)
  8386. ;; loop over all commands or templates
  8387. (dolist (c a)
  8388. (let (vrules repl)
  8389. (cond
  8390. ((not (assoc (car c) contexts))
  8391. (push c r))
  8392. ((and (assoc (car c) contexts)
  8393. (setq vrules (org-contextualize-validate-key
  8394. (car c) contexts)))
  8395. (mapc (lambda (vr)
  8396. (when (not (equal (car vr) (cadr vr)))
  8397. (setq repl vr)))
  8398. vrules)
  8399. (if (not repl) (push c r)
  8400. (push (cadr repl) s)
  8401. (push
  8402. (cons (car c)
  8403. (cdr (or (assoc (cadr repl) alist)
  8404. (error "Undefined key `%s' as contextual replacement for `%s'"
  8405. (cadr repl) (car c)))))
  8406. r))))))
  8407. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8408. (delq
  8409. nil
  8410. (delete-dups
  8411. (mapcar (lambda (x)
  8412. (let ((tpl (car x)))
  8413. (when (not (delq
  8414. nil
  8415. (mapcar (lambda (y)
  8416. (equal y tpl))
  8417. s)))
  8418. x)))
  8419. (reverse r))))))
  8420. (defun org-contextualize-validate-key (key contexts)
  8421. "Check CONTEXTS for agenda or capture KEY."
  8422. (let (rr res)
  8423. (dolist (r contexts)
  8424. (mapc
  8425. (lambda (rr)
  8426. (when
  8427. (and (equal key (car r))
  8428. (if (functionp rr) (funcall rr)
  8429. (or (and (eq (car rr) 'in-file)
  8430. (buffer-file-name)
  8431. (string-match (cdr rr) (buffer-file-name)))
  8432. (and (eq (car rr) 'in-mode)
  8433. (string-match (cdr rr) (symbol-name major-mode)))
  8434. (and (eq (car rr) 'in-buffer)
  8435. (string-match (cdr rr) (buffer-name)))
  8436. (when (and (eq (car rr) 'not-in-file)
  8437. (buffer-file-name))
  8438. (not (string-match (cdr rr) (buffer-file-name))))
  8439. (when (eq (car rr) 'not-in-mode)
  8440. (not (string-match (cdr rr) (symbol-name major-mode))))
  8441. (when (eq (car rr) 'not-in-buffer)
  8442. (not (string-match (cdr rr) (buffer-name)))))))
  8443. (push r res)))
  8444. (car (last r))))
  8445. (delete-dups (delq nil res))))
  8446. (defun org-context-p (&rest contexts)
  8447. "Check if local context is any of CONTEXTS.
  8448. Possible values in the list of contexts are `table', `headline', and `item'."
  8449. (let ((pos (point)))
  8450. (goto-char (point-at-bol))
  8451. (prog1 (or (and (memq 'table contexts)
  8452. (looking-at "[ \t]*|"))
  8453. (and (memq 'headline contexts)
  8454. (looking-at org-outline-regexp))
  8455. (and (memq 'item contexts)
  8456. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8457. (and (memq 'item-body contexts)
  8458. (org-in-item-p)))
  8459. (goto-char pos))))
  8460. (defun org-get-local-variables ()
  8461. "Return a list of all local variables in an Org mode buffer."
  8462. (let (varlist)
  8463. (with-current-buffer (get-buffer-create "*Org tmp*")
  8464. (erase-buffer)
  8465. (org-mode)
  8466. (setq varlist (buffer-local-variables)))
  8467. (kill-buffer "*Org tmp*")
  8468. (delq nil
  8469. (mapcar
  8470. (lambda (x)
  8471. (setq x
  8472. (if (symbolp x)
  8473. (list x)
  8474. (list (car x) (cdr x))))
  8475. (if (and (not (get (car x) 'org-state))
  8476. (string-match
  8477. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8478. (symbol-name (car x))))
  8479. x nil))
  8480. varlist))))
  8481. (defun org-clone-local-variables (from-buffer &optional regexp)
  8482. "Clone local variables from FROM-BUFFER.
  8483. Optional argument REGEXP selects variables to clone."
  8484. (mapc
  8485. (lambda (pair)
  8486. (and (symbolp (car pair))
  8487. (or (null regexp)
  8488. (string-match regexp (symbol-name (car pair))))
  8489. (set (make-local-variable (car pair))
  8490. (cdr pair))))
  8491. (buffer-local-variables from-buffer)))
  8492. ;;;###autoload
  8493. (defun org-run-like-in-org-mode (cmd)
  8494. "Run a command, pretending that the current buffer is in Org-mode.
  8495. This will temporarily bind local variables that are typically bound in
  8496. Org-mode to the values they have in Org-mode, and then interactively
  8497. call CMD."
  8498. (org-load-modules-maybe)
  8499. (unless org-local-vars
  8500. (setq org-local-vars (org-get-local-variables)))
  8501. (let (binds)
  8502. (dolist (var org-local-vars)
  8503. (when (or (not (boundp (car var)))
  8504. (eq (symbol-value (car var))
  8505. (default-value (car var))))
  8506. (push (list (car var) `(quote ,(cadr var))) binds)))
  8507. (eval `(let ,binds
  8508. (call-interactively (quote ,cmd))))))
  8509. (defun org-get-category (&optional pos force-refresh)
  8510. "Get the category applying to position POS."
  8511. (save-match-data
  8512. (if force-refresh (org-refresh-category-properties))
  8513. (let ((pos (or pos (point))))
  8514. (or (get-text-property pos 'org-category)
  8515. (progn (org-refresh-category-properties)
  8516. (get-text-property pos 'org-category))))))
  8517. ;;; Refresh properties
  8518. (defun org-refresh-properties (dprop tprop)
  8519. "Refresh buffer text properties.
  8520. DPROP is the drawer property and TPROP is either the
  8521. corresponding text property to set, or an alist with each element
  8522. being a text property (as a symbol) and a function to apply to
  8523. the value of the drawer property."
  8524. (let ((case-fold-search t)
  8525. (inhibit-read-only t))
  8526. (org-with-silent-modifications
  8527. (save-excursion
  8528. (save-restriction
  8529. (widen)
  8530. (goto-char (point-min))
  8531. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8532. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8533. (defun org-refresh-property (tprop p)
  8534. "Refresh the buffer text property TPROP from the drawer property P.
  8535. The refresh happens only for the current tree (not subtree)."
  8536. (unless (org-before-first-heading-p)
  8537. (save-excursion
  8538. (org-back-to-heading t)
  8539. (if (symbolp tprop)
  8540. ;; TPROP is a text property symbol
  8541. (put-text-property
  8542. (point) (or (outline-next-heading) (point-max)) tprop p)
  8543. ;; TPROP is an alist with (properties . function) elements
  8544. (dolist (al tprop)
  8545. (save-excursion
  8546. (put-text-property
  8547. (line-beginning-position) (or (outline-next-heading) (point-max))
  8548. (car al)
  8549. (funcall (cdr al) p))))))))
  8550. (defun org-refresh-category-properties ()
  8551. "Refresh category text properties in the buffer."
  8552. (let ((case-fold-search t)
  8553. (inhibit-read-only t)
  8554. (default-category
  8555. (cond ((null org-category)
  8556. (if buffer-file-name
  8557. (file-name-sans-extension
  8558. (file-name-nondirectory buffer-file-name))
  8559. "???"))
  8560. ((symbolp org-category) (symbol-name org-category))
  8561. (t org-category))))
  8562. (org-with-silent-modifications
  8563. (org-with-wide-buffer
  8564. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8565. ;; This is the default category for the buffer. If none is
  8566. ;; found, fall-back to `org-category' or buffer file name.
  8567. (put-text-property
  8568. (point-min) (point-max)
  8569. 'org-category
  8570. (catch 'buffer-category
  8571. (goto-char (point-max))
  8572. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8573. (let ((element (org-element-at-point)))
  8574. (when (eq (org-element-type element) 'keyword)
  8575. (throw 'buffer-category
  8576. (org-element-property :value element)))))
  8577. default-category))
  8578. ;; Set sub-tree specific categories.
  8579. (goto-char (point-min))
  8580. (let ((regexp (org-re-property "CATEGORY")))
  8581. (while (re-search-forward regexp nil t)
  8582. (let ((value (org-match-string-no-properties 3)))
  8583. (when (org-at-property-p)
  8584. (put-text-property
  8585. (save-excursion (org-back-to-heading t) (point))
  8586. (save-excursion (org-end-of-subtree t t) (point))
  8587. 'org-category
  8588. value)))))))))
  8589. (defun org-refresh-stats-properties ()
  8590. "Refresh stats text properties in the buffer."
  8591. (let (stats)
  8592. (org-with-silent-modifications
  8593. (save-excursion
  8594. (save-restriction
  8595. (widen)
  8596. (goto-char (point-min))
  8597. (while (re-search-forward
  8598. (concat org-outline-regexp-bol ".*"
  8599. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8600. nil t)
  8601. (setq stats (cond ((equal (match-string 3) "0") 0)
  8602. ((match-string 2)
  8603. (/ (* (string-to-number (match-string 2)) 100)
  8604. (string-to-number (match-string 3))))
  8605. (t (string-to-number (match-string 1)))))
  8606. (org-back-to-heading t)
  8607. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8608. 'org-stats stats)))))))
  8609. (defun org-refresh-effort-properties ()
  8610. "Refresh effort properties"
  8611. (org-refresh-properties
  8612. org-effort-property
  8613. '((effort . identity)
  8614. (effort-minutes . org-duration-string-to-minutes))))
  8615. ;;;; Link Stuff
  8616. ;;; Link abbreviations
  8617. (defun org-link-expand-abbrev (link)
  8618. "Apply replacements as defined in `org-link-abbrev-alist'."
  8619. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8620. (let* ((key (match-string 1 link))
  8621. (as (or (assoc key org-link-abbrev-alist-local)
  8622. (assoc key org-link-abbrev-alist)))
  8623. (tag (and (match-end 2) (match-string 3 link)))
  8624. rpl)
  8625. (if (not as)
  8626. link
  8627. (setq rpl (cdr as))
  8628. (cond
  8629. ((symbolp rpl) (funcall rpl tag))
  8630. ((string-match "%(\\([^)]+\\))" rpl)
  8631. (replace-match
  8632. (save-match-data
  8633. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8634. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8635. ((string-match "%h" rpl)
  8636. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8637. (t (concat rpl tag)))))
  8638. link))
  8639. ;;; Storing and inserting links
  8640. (defvar org-insert-link-history nil
  8641. "Minibuffer history for links inserted with `org-insert-link'.")
  8642. (defvar org-stored-links nil
  8643. "Contains the links stored with `org-store-link'.")
  8644. (defvar org-store-link-plist nil
  8645. "Plist with info about the most recently link created with `org-store-link'.")
  8646. (defvar org-link-protocols nil
  8647. "Link protocols added to Org-mode using `org-add-link-type'.")
  8648. (defvar org-store-link-functions nil
  8649. "List of functions that are called to create and store a link.
  8650. Each function will be called in turn until one returns a non-nil
  8651. value. Each function should check if it is responsible for creating
  8652. this link (for example by looking at the major mode).
  8653. If not, it must exit and return nil.
  8654. If yes, it should return a non-nil value after a calling
  8655. `org-store-link-props' with a list of properties and values.
  8656. Special properties are:
  8657. :type The link prefix, like \"http\". This must be given.
  8658. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8659. This is obligatory as well.
  8660. :description Optional default description for the second pair
  8661. of brackets in an Org-mode link. The user can still change
  8662. this when inserting this link into an Org-mode buffer.
  8663. In addition to these, any additional properties can be specified
  8664. and then used in capture templates.")
  8665. (defun org-add-link-type (type &optional follow export)
  8666. "Add TYPE to the list of `org-link-types'.
  8667. Re-compute all regular expressions depending on `org-link-types'
  8668. FOLLOW and EXPORT are two functions.
  8669. FOLLOW should take the link path as the single argument and do whatever
  8670. is necessary to follow the link, for example find a file or display
  8671. a mail message.
  8672. EXPORT should format the link path for export to one of the export formats.
  8673. It should be a function accepting three arguments:
  8674. path the path of the link, the text after the prefix (like \"http:\")
  8675. desc the description of the link, if any
  8676. format the export format, a symbol like `html' or `latex' or `ascii'.
  8677. The function may use the FORMAT information to return different values
  8678. depending on the format. The return value will be put literally into
  8679. the exported file. If the return value is nil, this means Org should
  8680. do what it normally does with links which do not have EXPORT defined.
  8681. Org mode has a built-in default for exporting links. If you are happy with
  8682. this default, there is no need to define an export function for the link
  8683. type. For a simple example of an export function, see `org-bbdb.el'."
  8684. (add-to-list 'org-link-types type t)
  8685. (org-make-link-regexps)
  8686. (org-element-update-syntax)
  8687. (if (assoc type org-link-protocols)
  8688. (setcdr (assoc type org-link-protocols) (list follow export))
  8689. (push (list type follow export) org-link-protocols)))
  8690. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8691. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8692. ;;;###autoload
  8693. (defun org-store-link (arg)
  8694. "\\<org-mode-map>Store an org-link to the current location.
  8695. This link is added to `org-stored-links' and can later be inserted
  8696. into an Org buffer with \\[org-insert-link].
  8697. For some link types, a prefix ARG is interpreted.
  8698. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8699. For file links, ARG negates `org-context-in-file-links'.
  8700. A double prefix ARG force skipping storing functions that are not
  8701. part of Org's core.
  8702. A triple prefix ARG force storing a link for each line in the
  8703. active region."
  8704. (interactive "P")
  8705. (org-load-modules-maybe)
  8706. (if (and (equal arg '(64)) (org-region-active-p))
  8707. (save-excursion
  8708. (let ((end (region-end)))
  8709. (goto-char (region-beginning))
  8710. (set-mark (point))
  8711. (while (< (point-at-eol) end)
  8712. (move-end-of-line 1) (activate-mark)
  8713. (let (current-prefix-arg)
  8714. (call-interactively 'org-store-link))
  8715. (move-beginning-of-line 2)
  8716. (set-mark (point)))))
  8717. (org-with-limited-levels
  8718. (setq org-store-link-plist nil)
  8719. (let (link cpltxt desc description search
  8720. txt custom-id agenda-link sfuns sfunsn)
  8721. (cond
  8722. ;; Store a link using an external link type
  8723. ((and (not (equal arg '(16)))
  8724. (setq sfuns
  8725. (delq
  8726. nil (mapcar (lambda (f)
  8727. (let (fs) (if (funcall f) (push f fs))))
  8728. org-store-link-functions))
  8729. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8730. (or (and (cdr sfuns)
  8731. (funcall (intern
  8732. (completing-read
  8733. "Which function for creating the link? "
  8734. sfunsn nil t (car sfunsn)))))
  8735. (funcall (caar sfuns)))
  8736. (setq link (plist-get org-store-link-plist :link)
  8737. desc (or (plist-get org-store-link-plist
  8738. :description)
  8739. link))))
  8740. ;; Store a link from a source code buffer.
  8741. ((org-src-edit-buffer-p)
  8742. (cond
  8743. ((save-excursion
  8744. (beginning-of-line)
  8745. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8746. "[ \t]*$")))
  8747. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8748. ((org-called-interactively-p 'any)
  8749. (let (label)
  8750. (while (or (not label)
  8751. (org-with-wide-buffer
  8752. (goto-char (point-min))
  8753. (re-search-forward
  8754. (regexp-quote (format org-coderef-label-format label))
  8755. nil t)))
  8756. (when label (message "Label exists already") (sit-for 2))
  8757. (setq label (read-string "Code line label: " label)))
  8758. (end-of-line)
  8759. (setq link (format org-coderef-label-format label))
  8760. (let ((gc (- 79 (length link))))
  8761. (if (< (current-column) gc) (org-move-to-column gc t)
  8762. (insert " ")))
  8763. (insert link)
  8764. (setq link (concat "(" label ")") desc nil)))
  8765. (t (setq link nil))))
  8766. ;; We are in the agenda, link to referenced location
  8767. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8768. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8769. (get-text-property (point) 'org-marker))))
  8770. (when m
  8771. (org-with-point-at m
  8772. (setq agenda-link
  8773. (if (org-called-interactively-p 'any)
  8774. (call-interactively 'org-store-link)
  8775. (org-store-link nil)))))))
  8776. ((eq major-mode 'calendar-mode)
  8777. (let ((cd (calendar-cursor-to-date)))
  8778. (setq link
  8779. (format-time-string
  8780. (car org-time-stamp-formats)
  8781. (apply 'encode-time
  8782. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8783. nil nil nil))))
  8784. (org-store-link-props :type "calendar" :date cd)))
  8785. ((eq major-mode 'help-mode)
  8786. (setq link (concat "help:" (save-excursion
  8787. (goto-char (point-min))
  8788. (looking-at "^[^ ]+")
  8789. (match-string 0))))
  8790. (org-store-link-props :type "help"))
  8791. ((eq major-mode 'w3-mode)
  8792. (setq cpltxt (if (and (buffer-name)
  8793. (not (string-match "Untitled" (buffer-name))))
  8794. (buffer-name)
  8795. (url-view-url t))
  8796. link (url-view-url t))
  8797. (org-store-link-props :type "w3" :url (url-view-url t)))
  8798. ((eq major-mode 'image-mode)
  8799. (setq cpltxt (concat "file:"
  8800. (abbreviate-file-name buffer-file-name))
  8801. link cpltxt)
  8802. (org-store-link-props :type "image" :file buffer-file-name))
  8803. ;; In dired, store a link to the file of the current line
  8804. ((derived-mode-p 'dired-mode)
  8805. (let ((file (dired-get-filename nil t)))
  8806. (setq file (if file
  8807. (abbreviate-file-name
  8808. (expand-file-name (dired-get-filename nil t)))
  8809. ;; otherwise, no file so use current directory.
  8810. default-directory))
  8811. (setq cpltxt (concat "file:" file)
  8812. link cpltxt)))
  8813. ((setq search (run-hook-with-args-until-success
  8814. 'org-create-file-search-functions))
  8815. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8816. "::" search))
  8817. (setq cpltxt (or description link)))
  8818. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8819. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8820. (cond
  8821. ;; Store a link using the target at point
  8822. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8823. (setq cpltxt
  8824. (concat "file:"
  8825. (abbreviate-file-name
  8826. (buffer-file-name (buffer-base-buffer)))
  8827. "::" (match-string 1))
  8828. link cpltxt))
  8829. ((and (featurep 'org-id)
  8830. (or (eq org-id-link-to-org-use-id t)
  8831. (and (org-called-interactively-p 'any)
  8832. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8833. (and (eq org-id-link-to-org-use-id
  8834. 'create-if-interactive-and-no-custom-id)
  8835. (not custom-id))))
  8836. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8837. ;; Store a link using the ID at point
  8838. (setq link (condition-case nil
  8839. (prog1 (org-id-store-link)
  8840. (setq desc (or (plist-get org-store-link-plist
  8841. :description)
  8842. "")))
  8843. (error
  8844. ;; Probably before first headline, link only to file
  8845. (concat "file:"
  8846. (abbreviate-file-name
  8847. (buffer-file-name (buffer-base-buffer))))))))
  8848. (t
  8849. ;; Just link to current headline
  8850. (setq cpltxt (concat "file:"
  8851. (abbreviate-file-name
  8852. (buffer-file-name (buffer-base-buffer)))))
  8853. ;; Add a context search string
  8854. (when (org-xor org-context-in-file-links arg)
  8855. (let* ((ee (org-element-at-point))
  8856. (et (org-element-type ee))
  8857. (ev (plist-get (cadr ee) :value))
  8858. (ek (plist-get (cadr ee) :key))
  8859. (eok (and (stringp ek) (string-match "name" ek))))
  8860. (setq txt (cond
  8861. ((org-at-heading-p) nil)
  8862. ((and (eq et 'keyword) eok) ev)
  8863. ((org-region-active-p)
  8864. (buffer-substring (region-beginning) (region-end)))))
  8865. (when (or (null txt) (string-match "\\S-" txt))
  8866. (setq cpltxt
  8867. (concat cpltxt "::"
  8868. (condition-case nil
  8869. (org-make-org-heading-search-string txt)
  8870. (error "")))
  8871. desc (or (and (eq et 'keyword) eok ev)
  8872. (nth 4 (ignore-errors (org-heading-components)))
  8873. "NONE")))))
  8874. (if (string-match "::\\'" cpltxt)
  8875. (setq cpltxt (substring cpltxt 0 -2)))
  8876. (setq link cpltxt))))
  8877. ((buffer-file-name (buffer-base-buffer))
  8878. ;; Just link to this file here.
  8879. (setq cpltxt (concat "file:"
  8880. (abbreviate-file-name
  8881. (buffer-file-name (buffer-base-buffer)))))
  8882. ;; Add a context string.
  8883. (when (org-xor org-context-in-file-links arg)
  8884. (setq txt (if (org-region-active-p)
  8885. (buffer-substring (region-beginning) (region-end))
  8886. (buffer-substring (point-at-bol) (point-at-eol))))
  8887. ;; Only use search option if there is some text.
  8888. (when (string-match "\\S-" txt)
  8889. (setq cpltxt
  8890. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8891. desc "NONE")))
  8892. (setq link cpltxt))
  8893. ((org-called-interactively-p 'interactive)
  8894. (user-error "No method for storing a link from this buffer"))
  8895. (t (setq link nil)))
  8896. ;; We're done setting link and desc, clean up
  8897. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8898. (setq link (or link cpltxt)
  8899. desc (or desc cpltxt))
  8900. (cond ((equal desc "NONE") (setq desc nil))
  8901. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8902. (let ((d0 (match-string 3 desc))
  8903. (p0 (match-string 5 desc)))
  8904. (setq desc
  8905. (replace-regexp-in-string
  8906. org-bracket-link-regexp
  8907. (concat (or p0 d0)
  8908. (if (equal (length (match-string 0 desc))
  8909. (length desc)) "*" "")) desc)))))
  8910. ;; Return the link
  8911. (if (not (and (or (org-called-interactively-p 'any)
  8912. executing-kbd-macro)
  8913. link))
  8914. (or agenda-link (and link (org-make-link-string link desc)))
  8915. (push (list link desc) org-stored-links)
  8916. (message "Stored: %s" (or desc link))
  8917. (when custom-id
  8918. (setq link (concat "file:" (abbreviate-file-name
  8919. (buffer-file-name)) "::#" custom-id))
  8920. (push (list link desc) org-stored-links))
  8921. (car org-stored-links))))))
  8922. (defun org-store-link-props (&rest plist)
  8923. "Store link properties, extract names and addresses."
  8924. (let (x adr)
  8925. (when (setq x (plist-get plist :from))
  8926. (setq adr (mail-extract-address-components x))
  8927. (setq plist (plist-put plist :fromname (car adr)))
  8928. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8929. (when (setq x (plist-get plist :to))
  8930. (setq adr (mail-extract-address-components x))
  8931. (setq plist (plist-put plist :toname (car adr)))
  8932. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8933. (let ((from (plist-get plist :from))
  8934. (to (plist-get plist :to)))
  8935. (when (and from to org-from-is-user-regexp)
  8936. (setq plist
  8937. (plist-put plist :fromto
  8938. (if (string-match org-from-is-user-regexp from)
  8939. (concat "to %t")
  8940. (concat "from %f"))))))
  8941. (setq org-store-link-plist plist))
  8942. (defun org-add-link-props (&rest plist)
  8943. "Add these properties to the link property list."
  8944. (let (key value)
  8945. (while plist
  8946. (setq key (pop plist) value (pop plist))
  8947. (setq org-store-link-plist
  8948. (plist-put org-store-link-plist key value)))))
  8949. (defun org-email-link-description (&optional fmt)
  8950. "Return the description part of an email link.
  8951. This takes information from `org-store-link-plist' and formats it
  8952. according to FMT (default from `org-email-link-description-format')."
  8953. (setq fmt (or fmt org-email-link-description-format))
  8954. (let* ((p org-store-link-plist)
  8955. (to (plist-get p :toaddress))
  8956. (from (plist-get p :fromaddress))
  8957. (table
  8958. (list
  8959. (cons "%c" (plist-get p :fromto))
  8960. (cons "%F" (plist-get p :from))
  8961. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8962. (cons "%T" (plist-get p :to))
  8963. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8964. (cons "%s" (plist-get p :subject))
  8965. (cons "%d" (plist-get p :date))
  8966. (cons "%m" (plist-get p :message-id)))))
  8967. (when (string-match "%c" fmt)
  8968. ;; Check if the user wrote this message
  8969. (if (and org-from-is-user-regexp from to
  8970. (save-match-data (string-match org-from-is-user-regexp from)))
  8971. (setq fmt (replace-match "to %t" t t fmt))
  8972. (setq fmt (replace-match "from %f" t t fmt))))
  8973. (org-replace-escapes fmt table)))
  8974. (defun org-make-org-heading-search-string (&optional string)
  8975. "Make search string for the current headline or STRING."
  8976. (let ((s (or string
  8977. (and (derived-mode-p 'org-mode)
  8978. (save-excursion
  8979. (org-back-to-heading t)
  8980. (org-element-property :raw-value (org-element-at-point))))))
  8981. (lines org-context-in-file-links))
  8982. (or string (setq s (concat "*" s))) ; Add * for headlines
  8983. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8984. (when (and string (integerp lines) (> lines 0))
  8985. (let ((slines (org-split-string s "\n")))
  8986. (when (< lines (length slines))
  8987. (setq s (mapconcat
  8988. 'identity
  8989. (reverse (nthcdr (- (length slines) lines)
  8990. (reverse slines))) "\n")))))
  8991. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8992. (defun org-make-link-string (link &optional description)
  8993. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8994. (unless (org-string-nw-p link) (error "Empty link"))
  8995. (let ((uri (cond ((string-match org-link-types-re link)
  8996. (concat (match-string 1 link)
  8997. (org-link-escape (substring link (match-end 1)))))
  8998. ;; For readability, url-encode internal links only
  8999. ;; when absolutely needed (i.e, when they contain
  9000. ;; square brackets). File links however, are
  9001. ;; encoded since, e.g., spaces are significant.
  9002. ((or (file-name-absolute-p link)
  9003. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9004. (org-link-escape link))
  9005. (t link)))
  9006. (description
  9007. (and (org-string-nw-p description)
  9008. ;; Remove brackets from description, as they are fatal.
  9009. (replace-regexp-in-string
  9010. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9011. (org-trim description)))))
  9012. (format "[[%s]%s]"
  9013. uri
  9014. (if description (format "[%s]" description) ""))))
  9015. (defconst org-link-escape-chars
  9016. ;;%20 %5B %5D %25
  9017. '(?\s ?\[ ?\] ?%)
  9018. "List of characters that should be escaped in a link when stored to Org.
  9019. This is the list that is used for internal purposes.")
  9020. (defconst org-link-escape-chars-browser
  9021. ;;%20 %22
  9022. '(?\s ?\")
  9023. "List of characters to be escaped before handing over to the browser.
  9024. If you consider using this constant then you probably want to use
  9025. the function `org-link-escape-browser' instead. See there why
  9026. this constant is a candidate to be removed once Org drops support
  9027. for Emacs 24.1 and 24.2.")
  9028. (defun org-link-escape (text &optional table merge)
  9029. "Return percent escaped representation of TEXT.
  9030. TEXT is a string with the text to escape.
  9031. Optional argument TABLE is a list with characters that should be
  9032. escaped. When nil, `org-link-escape-chars' is used.
  9033. If optional argument MERGE is set, merge TABLE into
  9034. `org-link-escape-chars'."
  9035. (let ((characters-to-encode
  9036. (cond ((null table) org-link-escape-chars)
  9037. (merge (append org-link-escape-chars table))
  9038. (t table))))
  9039. (mapconcat
  9040. (lambda (c)
  9041. (if (or (memq c characters-to-encode)
  9042. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9043. (mapconcat (lambda (e) (format "%%%.2X" e))
  9044. (or (encode-coding-char c 'utf-8)
  9045. (error "Unable to percent escape character: %c" c))
  9046. "")
  9047. (char-to-string c)))
  9048. text "")))
  9049. (defun org-link-escape-browser (text)
  9050. "Escape some characters before handing over to the browser.
  9051. This function is a candidate to be removed together with the
  9052. constant `org-link-escape-chars-browser' once Org drops support
  9053. for Emacs 24.1 and 24.2. All calls to this function will have to
  9054. be replaced with `url-encode-url' which is available since Emacs
  9055. 24.3.1."
  9056. ;; Example with the Org link
  9057. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9058. ;; to open the browser with +subject:"Release 8.2" filled into the
  9059. ;; query field: In this case the variable TEXT contains the
  9060. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9061. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9062. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9063. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9064. (if (fboundp 'url-encode-url)
  9065. (url-encode-url text)
  9066. (if (org-string-match-p
  9067. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9068. text)
  9069. (org-link-escape text org-link-escape-chars-browser)
  9070. text)))
  9071. (defun org-link-unescape (str)
  9072. "Unhex hexified Unicode parts in string STR.
  9073. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9074. reciprocal of `org-link-escape', which see."
  9075. (if (org-string-nw-p str)
  9076. (replace-regexp-in-string
  9077. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9078. str))
  9079. (defun org-link-unescape-compound (hex)
  9080. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9081. Note: this function also decodes single byte encodings like
  9082. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9083. (save-match-data
  9084. (let* ((bytes (cdr (split-string hex "%")))
  9085. (ret "")
  9086. (eat 0)
  9087. (sum 0))
  9088. (while bytes
  9089. (let* ((val (string-to-number (pop bytes) 16))
  9090. (shift-xor
  9091. (if (= 0 eat)
  9092. (cond
  9093. ((>= val 252) (cons 6 252))
  9094. ((>= val 248) (cons 5 248))
  9095. ((>= val 240) (cons 4 240))
  9096. ((>= val 224) (cons 3 224))
  9097. ((>= val 192) (cons 2 192))
  9098. (t (cons 0 0)))
  9099. (cons 6 128))))
  9100. (if (>= val 192) (setq eat (car shift-xor)))
  9101. (setq val (logxor val (cdr shift-xor)))
  9102. (setq sum (+ (lsh sum (car shift-xor)) val))
  9103. (if (> eat 0) (setq eat (- eat 1)))
  9104. (cond
  9105. ((= 0 eat) ;multi byte
  9106. (setq ret (concat ret (org-char-to-string sum)))
  9107. (setq sum 0))
  9108. ((not bytes) ; single byte(s)
  9109. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9110. ret)))
  9111. (defun org-link-unescape-single-byte-sequence (hex)
  9112. "Unhexify hex-encoded single byte character sequences."
  9113. (mapconcat (lambda (byte)
  9114. (char-to-string (string-to-number byte 16)))
  9115. (cdr (split-string hex "%")) ""))
  9116. (defun org-xor (a b)
  9117. "Exclusive or."
  9118. (if a (not b) b))
  9119. (defun org-fixup-message-id-for-http (s)
  9120. "Replace special characters in a message id, so it can be used in an http query."
  9121. (when (string-match "%" s)
  9122. (setq s (mapconcat (lambda (c)
  9123. (if (eq c ?%)
  9124. "%25"
  9125. (char-to-string c)))
  9126. s "")))
  9127. (while (string-match "<" s)
  9128. (setq s (replace-match "%3C" t t s)))
  9129. (while (string-match ">" s)
  9130. (setq s (replace-match "%3E" t t s)))
  9131. (while (string-match "@" s)
  9132. (setq s (replace-match "%40" t t s)))
  9133. s)
  9134. (defun org-link-prettify (link)
  9135. "Return a human-readable representation of LINK.
  9136. The car of LINK must be a raw link.
  9137. The cdr of LINK must be either a link description or nil."
  9138. (let ((desc (or (cadr link) "<no description>")))
  9139. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9140. "<" (car link) ">")))
  9141. ;;;###autoload
  9142. (defun org-insert-link-global ()
  9143. "Insert a link like Org-mode does.
  9144. This command can be called in any mode to insert a link in Org-mode syntax."
  9145. (interactive)
  9146. (org-load-modules-maybe)
  9147. (org-run-like-in-org-mode 'org-insert-link))
  9148. (defun org-insert-all-links (arg &optional pre post)
  9149. "Insert all links in `org-stored-links'.
  9150. When a universal prefix, do not delete the links from `org-stored-links'.
  9151. When `ARG' is a number, insert the last N link(s).
  9152. `PRE' and `POST' are optional arguments to define a string to
  9153. prepend or to append."
  9154. (interactive "P")
  9155. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9156. (links (copy-seq org-stored-links))
  9157. (pr (or pre "- "))
  9158. (po (or post "\n"))
  9159. (cnt 1) l)
  9160. (if (null org-stored-links)
  9161. (message "No link to insert")
  9162. (while (and (or (listp arg) (>= arg cnt))
  9163. (setq l (if (listp arg)
  9164. (pop links)
  9165. (pop org-stored-links))))
  9166. (setq cnt (1+ cnt))
  9167. (insert pr)
  9168. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9169. (insert po)))))
  9170. (defun org-insert-last-stored-link (arg)
  9171. "Insert the last link stored in `org-stored-links'."
  9172. (interactive "p")
  9173. (org-insert-all-links arg "" "\n"))
  9174. (defun org-link-fontify-links-to-this-file ()
  9175. "Fontify links to the current file in `org-stored-links'."
  9176. (let ((f (buffer-file-name)) a b)
  9177. (setq a (mapcar (lambda(l)
  9178. (let ((ll (car l)))
  9179. (when (and (string-match "^file:\\(.+\\)::" ll)
  9180. (equal f (expand-file-name (match-string 1 ll))))
  9181. ll)))
  9182. org-stored-links))
  9183. (when (featurep 'org-id)
  9184. (setq b (mapcar (lambda(l)
  9185. (let ((ll (car l)))
  9186. (when (and (string-match "^id:\\(.+\\)$" ll)
  9187. (equal f (expand-file-name
  9188. (or (org-id-find-id-file
  9189. (match-string 1 ll)) ""))))
  9190. ll)))
  9191. org-stored-links)))
  9192. (mapcar (lambda(l)
  9193. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9194. (delq nil (append a b)))))
  9195. (defvar org-link-links-in-this-file nil)
  9196. (defun org-insert-link (&optional complete-file link-location default-description)
  9197. "Insert a link. At the prompt, enter the link.
  9198. Completion can be used to insert any of the link protocol prefixes like
  9199. http or ftp in use.
  9200. The history can be used to select a link previously stored with
  9201. `org-store-link'. When the empty string is entered (i.e. if you just
  9202. press RET at the prompt), the link defaults to the most recently
  9203. stored link. As SPC triggers completion in the minibuffer, you need to
  9204. use M-SPC or C-q SPC to force the insertion of a space character.
  9205. You will also be prompted for a description, and if one is given, it will
  9206. be displayed in the buffer instead of the link.
  9207. If there is already a link at point, this command will allow you to edit link
  9208. and description parts.
  9209. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9210. be selected using completion. The path to the file will be relative to the
  9211. current directory if the file is in the current directory or a subdirectory.
  9212. Otherwise, the link will be the absolute path as completed in the minibuffer
  9213. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9214. option `org-link-file-path-type'.
  9215. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9216. the current directory or below.
  9217. With three \\[universal-argument] prefixes, negate the meaning of
  9218. `org-keep-stored-link-after-insertion'.
  9219. If `org-make-link-description-function' is non-nil, this function will be
  9220. called with the link target, and the result will be the default
  9221. link description.
  9222. If the LINK-LOCATION parameter is non-nil, this value will be
  9223. used as the link location instead of reading one interactively.
  9224. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9225. be used as the default description."
  9226. (interactive "P")
  9227. (let* ((wcf (current-window-configuration))
  9228. (origbuf (current-buffer))
  9229. (region (if (org-region-active-p)
  9230. (buffer-substring (region-beginning) (region-end))))
  9231. (remove (and region (list (region-beginning) (region-end))))
  9232. (desc region)
  9233. tmphist ; byte-compile incorrectly complains about this
  9234. (link link-location)
  9235. (abbrevs org-link-abbrev-alist-local)
  9236. entry file all-prefixes auto-desc)
  9237. (cond
  9238. (link-location) ; specified by arg, just use it.
  9239. ((org-in-regexp org-bracket-link-regexp 1)
  9240. ;; We do have a link at point, and we are going to edit it.
  9241. (setq remove (list (match-beginning 0) (match-end 0)))
  9242. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9243. (setq link (read-string "Link: "
  9244. (org-link-unescape
  9245. (org-match-string-no-properties 1)))))
  9246. ((or (org-in-regexp org-angle-link-re)
  9247. (org-in-regexp org-plain-link-re))
  9248. ;; Convert to bracket link
  9249. (setq remove (list (match-beginning 0) (match-end 0))
  9250. link (read-string "Link: "
  9251. (org-remove-angle-brackets (match-string 0)))))
  9252. ((member complete-file '((4) (16)))
  9253. ;; Completing read for file names.
  9254. (setq link (org-file-complete-link complete-file)))
  9255. (t
  9256. ;; Read link, with completion for stored links.
  9257. (org-link-fontify-links-to-this-file)
  9258. (org-switch-to-buffer-other-window "*Org Links*")
  9259. (with-current-buffer "*Org Links*"
  9260. (erase-buffer)
  9261. (insert "Insert a link.
  9262. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9263. (when org-stored-links
  9264. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9265. (insert (mapconcat 'org-link-prettify
  9266. (reverse org-stored-links) "\n")))
  9267. (goto-char (point-min)))
  9268. (let ((cw (selected-window)))
  9269. (select-window (get-buffer-window "*Org Links*" 'visible))
  9270. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9271. (unless (pos-visible-in-window-p (point-max))
  9272. (org-fit-window-to-buffer))
  9273. (and (window-live-p cw) (select-window cw)))
  9274. ;; Fake a link history, containing the stored links.
  9275. (setq tmphist (append (mapcar 'car org-stored-links)
  9276. org-insert-link-history))
  9277. (setq all-prefixes (append (mapcar 'car abbrevs)
  9278. (mapcar 'car org-link-abbrev-alist)
  9279. org-link-types))
  9280. (unwind-protect
  9281. (progn
  9282. (setq link
  9283. (org-completing-read
  9284. "Link: "
  9285. (append
  9286. (mapcar (lambda (x) (concat x ":"))
  9287. all-prefixes)
  9288. (mapcar 'car org-stored-links))
  9289. nil nil nil
  9290. 'tmphist
  9291. (caar org-stored-links)))
  9292. (if (not (string-match "\\S-" link))
  9293. (user-error "No link selected"))
  9294. (mapc (lambda(l)
  9295. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9296. org-stored-links)
  9297. (if (or (member link all-prefixes)
  9298. (and (equal ":" (substring link -1))
  9299. (member (substring link 0 -1) all-prefixes)
  9300. (setq link (substring link 0 -1))))
  9301. (setq link (with-current-buffer origbuf
  9302. (org-link-try-special-completion link)))))
  9303. (set-window-configuration wcf)
  9304. (kill-buffer "*Org Links*"))
  9305. (setq entry (assoc link org-stored-links))
  9306. (or entry (push link org-insert-link-history))
  9307. (setq desc (or desc (nth 1 entry)))))
  9308. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9309. (not org-keep-stored-link-after-insertion))
  9310. (setq org-stored-links (delq (assoc link org-stored-links)
  9311. org-stored-links)))
  9312. (if (and (string-match org-plain-link-re link)
  9313. (not (string-match org-ts-regexp link)))
  9314. ;; URL-like link, normalize the use of angular brackets.
  9315. (setq link (org-remove-angle-brackets link)))
  9316. ;; Check if we are linking to the current file with a search
  9317. ;; option If yes, simplify the link by using only the search
  9318. ;; option.
  9319. (when (and buffer-file-name
  9320. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9321. (let* ((path (match-string 1 link))
  9322. (case-fold-search nil)
  9323. (search (match-string 2 link)))
  9324. (save-match-data
  9325. (if (equal (file-truename buffer-file-name) (file-truename path))
  9326. ;; We are linking to this same file, with a search option
  9327. (setq link search)))))
  9328. ;; Check if we can/should use a relative path. If yes, simplify the link
  9329. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9330. (let* ((type (match-string 1 link))
  9331. (path (match-string 2 link))
  9332. (origpath path)
  9333. (case-fold-search nil))
  9334. (cond
  9335. ((or (eq org-link-file-path-type 'absolute)
  9336. (equal complete-file '(16)))
  9337. (setq path (abbreviate-file-name (expand-file-name path))))
  9338. ((eq org-link-file-path-type 'noabbrev)
  9339. (setq path (expand-file-name path)))
  9340. ((eq org-link-file-path-type 'relative)
  9341. (setq path (file-relative-name path)))
  9342. (t
  9343. (save-match-data
  9344. (if (string-match (concat "^" (regexp-quote
  9345. (expand-file-name
  9346. (file-name-as-directory
  9347. default-directory))))
  9348. (expand-file-name path))
  9349. ;; We are linking a file with relative path name.
  9350. (setq path (substring (expand-file-name path)
  9351. (match-end 0)))
  9352. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9353. (setq link (concat type path))
  9354. (if (equal desc origpath)
  9355. (setq desc path))))
  9356. (if org-make-link-description-function
  9357. (setq desc
  9358. (or (condition-case nil
  9359. (funcall org-make-link-description-function link desc)
  9360. (error (progn (message "Can't get link description from `%s'"
  9361. (symbol-name org-make-link-description-function))
  9362. (sit-for 2) nil)))
  9363. (read-string "Description: " default-description)))
  9364. (if default-description (setq desc default-description)
  9365. (setq desc (or (and auto-desc desc)
  9366. (read-string "Description: " desc)))))
  9367. (unless (string-match "\\S-" desc) (setq desc nil))
  9368. (if remove (apply 'delete-region remove))
  9369. (insert (org-make-link-string link desc))))
  9370. (defun org-link-try-special-completion (type)
  9371. "If there is completion support for link type TYPE, offer it."
  9372. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9373. (if (functionp fun)
  9374. (funcall fun)
  9375. (read-string "Link (no completion support): " (concat type ":")))))
  9376. (defun org-file-complete-link (&optional arg)
  9377. "Create a file link using completion."
  9378. (let ((file (org-iread-file-name "File: "))
  9379. (pwd (file-name-as-directory (expand-file-name ".")))
  9380. (pwd1 (file-name-as-directory (abbreviate-file-name
  9381. (expand-file-name ".")))))
  9382. (cond ((equal arg '(16))
  9383. (concat "file:"
  9384. (abbreviate-file-name (expand-file-name file))))
  9385. ((string-match
  9386. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9387. (concat "file:" (match-string 1 file)))
  9388. ((string-match
  9389. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9390. (expand-file-name file))
  9391. (concat "file:"
  9392. (match-string 1 (expand-file-name file))))
  9393. (t (concat "file:" file)))))
  9394. (defun org-iread-file-name (&rest args)
  9395. "Read-file-name using `ido-mode' speedup if available.
  9396. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9397. See `read-file-name' for a description of parameters."
  9398. (org-without-partial-completion
  9399. (if (and org-completion-use-ido
  9400. (fboundp 'ido-read-file-name)
  9401. (org-bound-and-true-p ido-mode)
  9402. (listp (nth 1 args)))
  9403. (let ((ido-enter-matching-directory nil))
  9404. (apply #'ido-read-file-name args))
  9405. (apply #'read-file-name args))))
  9406. (defun org-completing-read (&rest args)
  9407. "Completing-read with SPACE being a normal character."
  9408. (let ((enable-recursive-minibuffers t)
  9409. (minibuffer-local-completion-map
  9410. (copy-keymap minibuffer-local-completion-map)))
  9411. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9412. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9413. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9414. (apply 'org-icompleting-read args)))
  9415. (defun org-completing-read-no-i (&rest args)
  9416. (let (org-completion-use-ido org-completion-use-iswitchb)
  9417. (apply 'org-completing-read args)))
  9418. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9419. "Use iswitch as a completing-read replacement to choose from choices.
  9420. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9421. from."
  9422. (let* ((iswitchb-use-virtual-buffers nil)
  9423. (iswitchb-make-buflist-hook
  9424. (lambda ()
  9425. (setq iswitchb-temp-buflist choices))))
  9426. (iswitchb-read-buffer prompt)))
  9427. (defun org-icompleting-read (&rest args)
  9428. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9429. Should be called like `completing-read'."
  9430. (org-without-partial-completion
  9431. (if (not (listp (nth 1 args)))
  9432. ;; Ido only supports lists as the COLLECTION argument. Use
  9433. ;; default completion function when second argument is not
  9434. ;; a list.
  9435. (apply #'completing-read args)
  9436. (let ((ido-enter-matching-directory nil))
  9437. (apply (cond ((and org-completion-use-ido
  9438. (fboundp 'ido-completing-read)
  9439. (org-bound-and-true-p ido-mode))
  9440. #'ido-completing-read)
  9441. ((and org-completion-use-iswitchb
  9442. (org-bound-and-true-p iswitchb-mode))
  9443. #'org-iswitchb-completing-read)
  9444. (t #'completing-read))
  9445. args)))))
  9446. ;;; Opening/following a link
  9447. (defvar org-link-search-failed nil)
  9448. (defvar org-open-link-functions nil
  9449. "Hook for functions finding a plain text link.
  9450. These functions must take a single argument, the link content.
  9451. They will be called for links that look like [[link text][description]]
  9452. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9453. like a filename (e.g. \"./blue.png\").
  9454. These functions will be called *before* Org attempts to resolve the
  9455. link by doing text searches in the current buffer - so if you want a
  9456. link \"[[target]]\" to still find \"<<target>>\", your function should
  9457. handle this as a special case.
  9458. When the function does handle the link, it must return a non-nil value.
  9459. If it decides that it is not responsible for this link, it must return
  9460. nil to indicate that that Org-mode can continue with other options
  9461. like exact and fuzzy text search.")
  9462. (defun org-next-link (&optional search-backward)
  9463. "Move forward to the next link.
  9464. If the link is in hidden text, expose it."
  9465. (interactive "P")
  9466. (when (and org-link-search-failed (eq this-command last-command))
  9467. (goto-char (point-min))
  9468. (message "Link search wrapped back to beginning of buffer"))
  9469. (setq org-link-search-failed nil)
  9470. (let* ((pos (point))
  9471. (ct (org-context))
  9472. (a (assoc :link ct))
  9473. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9474. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9475. ((looking-at org-any-link-re)
  9476. ;; Don't stay stuck at link without an org-link face
  9477. (forward-char (if search-backward -1 1))))
  9478. (if (funcall srch-fun org-any-link-re nil t)
  9479. (progn
  9480. (goto-char (match-beginning 0))
  9481. (if (outline-invisible-p) (org-show-context)))
  9482. (goto-char pos)
  9483. (setq org-link-search-failed t)
  9484. (message "No further link found"))))
  9485. (defun org-previous-link ()
  9486. "Move backward to the previous link.
  9487. If the link is in hidden text, expose it."
  9488. (interactive)
  9489. (funcall 'org-next-link t))
  9490. (defun org-translate-link (s)
  9491. "Translate a link string if a translation function has been defined."
  9492. (if (and org-link-translation-function
  9493. (fboundp org-link-translation-function)
  9494. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9495. (progn
  9496. (setq s (funcall org-link-translation-function
  9497. (match-string 1 s) (match-string 2 s)))
  9498. (concat (car s) ":" (cdr s)))
  9499. s))
  9500. (defun org-translate-link-from-planner (type path)
  9501. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9502. This is still an experimental function, your mileage may vary."
  9503. (cond
  9504. ((member type '("http" "https" "news" "ftp"))
  9505. ;; standard Internet links are the same.
  9506. nil)
  9507. ((and (equal type "irc") (string-match "^//" path))
  9508. ;; Planner has two / at the beginning of an irc link, we have 1.
  9509. ;; We should have zero, actually....
  9510. (setq path (substring path 1)))
  9511. ((and (equal type "lisp") (string-match "^/" path))
  9512. ;; Planner has a slash, we do not.
  9513. (setq type "elisp" path (substring path 1)))
  9514. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9515. ;; A typical message link. Planner has the id after the final slash,
  9516. ;; we separate it with a hash mark
  9517. (setq path (concat (match-string 1 path) "#"
  9518. (org-remove-angle-brackets (match-string 2 path))))))
  9519. (cons type path))
  9520. (defun org-find-file-at-mouse (ev)
  9521. "Open file link or URL at mouse."
  9522. (interactive "e")
  9523. (mouse-set-point ev)
  9524. (org-open-at-point 'in-emacs))
  9525. (defun org-open-at-mouse (ev)
  9526. "Open file link or URL at mouse.
  9527. See the docstring of `org-open-file' for details."
  9528. (interactive "e")
  9529. (mouse-set-point ev)
  9530. (if (eq major-mode 'org-agenda-mode)
  9531. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9532. (org-open-at-point))
  9533. (defvar org-window-config-before-follow-link nil
  9534. "The window configuration before following a link.
  9535. This is saved in case the need arises to restore it.")
  9536. ;;;###autoload
  9537. (defun org-open-at-point-global ()
  9538. "Follow a link like Org-mode does.
  9539. This command can be called in any mode to follow a link that has
  9540. Org-mode syntax."
  9541. (interactive)
  9542. (org-run-like-in-org-mode 'org-open-at-point))
  9543. ;;;###autoload
  9544. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9545. "Open a link in the string S, as if it was in Org-mode."
  9546. (interactive "sLink: \nP")
  9547. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9548. (with-temp-buffer
  9549. (let ((org-inhibit-startup (not reference-buffer)))
  9550. (org-mode)
  9551. (insert s)
  9552. (goto-char (point-min))
  9553. (when reference-buffer
  9554. (setq org-link-abbrev-alist-local
  9555. (with-current-buffer reference-buffer
  9556. org-link-abbrev-alist-local)))
  9557. (org-open-at-point arg reference-buffer)))))
  9558. (defvar org-open-at-point-functions nil
  9559. "Hook that is run when following a link at point.
  9560. Functions in this hook must return t if they identify and follow
  9561. a link at point. If they don't find anything interesting at point,
  9562. they must return nil.")
  9563. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9564. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9565. (defun org-open-at-point (&optional arg reference-buffer)
  9566. "Open link, timestamp, footnote or tags at point.
  9567. When point is on a link, follow it. Normally, files will be
  9568. opened by an appropriate application. If the optional prefix
  9569. argument ARG is non-nil, Emacs will visit the file. With
  9570. a double prefix argument, try to open outside of Emacs, in the
  9571. application the system uses for this file type.
  9572. When point is on a timestamp, open the agenda at the day
  9573. specified.
  9574. When point is a footnote definition, move to the first reference
  9575. found. If it is on a reference, move to the associated
  9576. definition.
  9577. When point is on a headline, display a list of every link in the
  9578. entry, so it is possible to pick one, or all, of them. If point
  9579. is on a tag, call `org-tags-view' instead.
  9580. When optional argument REFERENCE-BUFFER is non-nil, it should
  9581. specify a buffer from where the link search should happen. This
  9582. is used internally by `org-open-link-from-string'.
  9583. On top of syntactically correct links, this function will open
  9584. the link at point in comments or comment blocks and the first
  9585. link in a property drawer line."
  9586. (interactive "P")
  9587. ;; On a code block, open block's results.
  9588. (unless (call-interactively 'org-babel-open-src-block-result)
  9589. (org-load-modules-maybe)
  9590. (setq org-window-config-before-follow-link (current-window-configuration))
  9591. (org-remove-occur-highlights nil nil t)
  9592. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9593. (let* ((context
  9594. ;; Only consider supported types, even if they are not
  9595. ;; the closest one.
  9596. (org-element-lineage
  9597. (org-element-context)
  9598. '(clock comment comment-block footnote-definition
  9599. footnote-reference headline inlinetask keyword link
  9600. node-property timestamp)
  9601. t))
  9602. (type (org-element-type context))
  9603. (value (org-element-property :value context)))
  9604. (cond
  9605. ((not context) (user-error "No link found"))
  9606. ;; Exception: open timestamps and links in properties
  9607. ;; drawers, keywords and comments.
  9608. ((memq type '(comment comment-block keyword node-property))
  9609. (cond ((org-in-regexp org-any-link-re)
  9610. (org-open-link-from-string (match-string-no-properties 0)))
  9611. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9612. (org-follow-timestamp-link))
  9613. (t (user-error "No link found"))))
  9614. ;; On a headline or an inlinetask, but not on a timestamp,
  9615. ;; a link, a footnote reference or on tags.
  9616. ((and (memq type '(headline inlinetask))
  9617. ;; Not on tags.
  9618. (progn (save-excursion (beginning-of-line)
  9619. (looking-at org-complex-heading-regexp))
  9620. (or (not (match-beginning 5))
  9621. (< (point) (match-beginning 5)))))
  9622. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9623. (links (car data))
  9624. (links-end (cdr data)))
  9625. (if links
  9626. (dolist (link (if (stringp links) (list links) links))
  9627. (search-forward link nil links-end)
  9628. (goto-char (match-beginning 0))
  9629. (org-open-at-point))
  9630. (require 'org-attach)
  9631. (org-attach-reveal 'if-exists))))
  9632. ;; On a clock line, make sure point is on the timestamp
  9633. ;; before opening it.
  9634. ((and (eq type 'clock)
  9635. value
  9636. (>= (point) (org-element-property :begin value))
  9637. (<= (point) (org-element-property :end value)))
  9638. (org-follow-timestamp-link))
  9639. ;; Do nothing on white spaces after an object, unless point
  9640. ;; is right after it.
  9641. ((> (point)
  9642. (save-excursion
  9643. (goto-char (org-element-property :end context))
  9644. (skip-chars-backward " \t")
  9645. (point)))
  9646. (user-error "No link found"))
  9647. ((eq type 'timestamp) (org-follow-timestamp-link))
  9648. ;; On tags within a headline or an inlinetask.
  9649. ((and (memq type '(headline inlinetask))
  9650. (progn (save-excursion (beginning-of-line)
  9651. (looking-at org-complex-heading-regexp))
  9652. (and (match-beginning 5)
  9653. (>= (point) (match-beginning 5)))))
  9654. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9655. ((eq type 'link)
  9656. ;; When link is located within the description of another
  9657. ;; link (e.g., an inline image), always open the parent
  9658. ;; link.
  9659. (let*((link (let ((up (org-element-property :parent context)))
  9660. (if (eq (org-element-type up) 'link) up context)))
  9661. (type (org-element-property :type link))
  9662. (path (org-link-unescape (org-element-property :path link))))
  9663. ;; Switch back to REFERENCE-BUFFER needed when called in
  9664. ;; a temporary buffer through `org-open-link-from-string'.
  9665. (with-current-buffer (or reference-buffer (current-buffer))
  9666. (cond
  9667. ((equal type "file")
  9668. (if (string-match "[*?{]" (file-name-nondirectory path))
  9669. (dired path)
  9670. ;; Look into `org-link-protocols' in order to find
  9671. ;; a DEDICATED-FUNCTION to open file. The function
  9672. ;; will be applied on raw link instead of parsed
  9673. ;; link due to the limitation in `org-add-link-type'
  9674. ;; ("open" function called with a single argument).
  9675. ;; If no such function is found, fallback to
  9676. ;; `org-open-file'.
  9677. ;;
  9678. ;; Note : "file+emacs" and "file+sys" types are
  9679. ;; hard-coded in order to escape the previous
  9680. ;; limitation.
  9681. (let* ((option (org-element-property :search-option link))
  9682. (app (org-element-property :application link))
  9683. (dedicated-function
  9684. (nth 1 (assoc app org-link-protocols))))
  9685. (if dedicated-function
  9686. (funcall dedicated-function
  9687. (concat path
  9688. (and option (concat "::" option))))
  9689. (apply #'org-open-file
  9690. path
  9691. (cond (arg)
  9692. ((equal app "emacs") 'emacs)
  9693. ((equal app "sys") 'system))
  9694. (cond ((not option) nil)
  9695. ((org-string-match-p "\\`[0-9]+\\'" option)
  9696. (list (string-to-number option)))
  9697. (t (list nil
  9698. (org-link-unescape option)))))))))
  9699. ((assoc type org-link-protocols)
  9700. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9701. ((equal type "help")
  9702. (let ((f-or-v (intern path)))
  9703. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9704. ((boundp f-or-v) (describe-variable f-or-v))
  9705. (t (error "Not a known function or variable")))))
  9706. ((member type '("http" "https" "ftp" "mailto" "news"))
  9707. (browse-url (org-link-escape-browser (concat type ":" path))))
  9708. ((equal type "doi")
  9709. (browse-url
  9710. (org-link-escape-browser (concat org-doi-server-url path))))
  9711. ((equal type "message") (browse-url (concat type ":" path)))
  9712. ((equal type "shell")
  9713. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9714. (cmd path))
  9715. (if (or (and (org-string-nw-p
  9716. org-confirm-shell-link-not-regexp)
  9717. (string-match
  9718. org-confirm-shell-link-not-regexp cmd))
  9719. (not org-confirm-shell-link-function)
  9720. (funcall org-confirm-shell-link-function
  9721. (format "Execute \"%s\" in shell? "
  9722. (org-add-props cmd nil
  9723. 'face 'org-warning))))
  9724. (progn
  9725. (message "Executing %s" cmd)
  9726. (shell-command cmd buf)
  9727. (when (featurep 'midnight)
  9728. (setq clean-buffer-list-kill-buffer-names
  9729. (cons (buffer-name buf)
  9730. clean-buffer-list-kill-buffer-names))))
  9731. (user-error "Abort"))))
  9732. ((equal type "elisp")
  9733. (let ((cmd path))
  9734. (if (or (and (org-string-nw-p
  9735. org-confirm-elisp-link-not-regexp)
  9736. (org-string-match-p
  9737. org-confirm-elisp-link-not-regexp cmd))
  9738. (not org-confirm-elisp-link-function)
  9739. (funcall org-confirm-elisp-link-function
  9740. (format "Execute \"%s\" as elisp? "
  9741. (org-add-props cmd nil
  9742. 'face 'org-warning))))
  9743. (message "%s => %s" cmd
  9744. (if (eq (string-to-char cmd) ?\()
  9745. (eval (read cmd))
  9746. (call-interactively (read cmd))))
  9747. (user-error "Abort"))))
  9748. ((equal type "id")
  9749. (require 'ord-id)
  9750. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9751. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9752. (unless (run-hook-with-args-until-success
  9753. 'org-open-link-functions path)
  9754. (if (not arg) (org-mark-ring-push)
  9755. (switch-to-buffer-other-window
  9756. (org-get-buffer-for-internal-link (current-buffer))))
  9757. (let ((destination
  9758. (org-with-wide-buffer
  9759. (if (equal type "radio")
  9760. (org-search-radio-target
  9761. (org-element-property :path link))
  9762. (org-link-search
  9763. (if (member type '("custom-id" "coderef"))
  9764. (org-element-property :raw-link link)
  9765. path)
  9766. ;; Prevent fuzzy links from matching
  9767. ;; themselves.
  9768. (and (equal type "fuzzy")
  9769. (+ 2 (org-element-property :begin link)))))
  9770. (point))))
  9771. (unless (and (<= (point-min) destination)
  9772. (>= (point-max) destination))
  9773. (widen))
  9774. (goto-char destination))))
  9775. (t (browse-url-at-point))))))
  9776. ;; On a footnote reference or at a footnote definition's label.
  9777. ((or (eq type 'footnote-reference)
  9778. (and (eq type 'footnote-definition)
  9779. (save-excursion
  9780. ;; Do not validate action when point is on the
  9781. ;; spaces right after the footnote label, in
  9782. ;; order to be on par with behaviour on links.
  9783. (skip-chars-forward " \t")
  9784. (let ((begin
  9785. (org-element-property :contents-begin context)))
  9786. (if begin (< (point) begin)
  9787. (= (org-element-property :post-affiliated context)
  9788. (line-beginning-position)))))))
  9789. (org-footnote-action))
  9790. (t (user-error "No link found")))))
  9791. (run-hook-with-args 'org-follow-link-hook)))
  9792. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9793. "Offer links in the current entry and return the selected link.
  9794. If there is only one link, return it.
  9795. If NTH is an integer, return the NTH link found.
  9796. If ZERO is a string, check also this string for a link, and if
  9797. there is one, return it."
  9798. (with-current-buffer buffer
  9799. (save-excursion
  9800. (save-restriction
  9801. (widen)
  9802. (goto-char marker)
  9803. (let ((cnt ?0)
  9804. (in-emacs (if (integerp nth) nil nth))
  9805. have-zero end links link c)
  9806. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9807. (push (match-string 0 zero) links)
  9808. (setq cnt (1- cnt) have-zero t))
  9809. (save-excursion
  9810. (org-back-to-heading t)
  9811. (setq end (save-excursion (outline-next-heading) (point)))
  9812. (while (re-search-forward org-any-link-re end t)
  9813. (push (match-string 0) links))
  9814. (setq links (org-uniquify (reverse links))))
  9815. (cond
  9816. ((null links)
  9817. (message "No links"))
  9818. ((equal (length links) 1)
  9819. (setq link (car links)))
  9820. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9821. (setq link (nth (if have-zero nth (1- nth)) links)))
  9822. (t ; we have to select a link
  9823. (save-excursion
  9824. (save-window-excursion
  9825. (delete-other-windows)
  9826. (with-output-to-temp-buffer "*Select Link*"
  9827. (mapc (lambda (l)
  9828. (if (not (string-match org-bracket-link-regexp l))
  9829. (princ (format "[%c] %s\n" (incf cnt)
  9830. (org-remove-angle-brackets l)))
  9831. (if (match-end 3)
  9832. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9833. (match-string 3 l) (match-string 1 l)))
  9834. (princ (format "[%c] %s\n" (incf cnt)
  9835. (match-string 1 l))))))
  9836. links))
  9837. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9838. (message "Select link to open, RET to open all:")
  9839. (setq c (read-char-exclusive))
  9840. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9841. (when (equal c ?q) (user-error "Abort"))
  9842. (if (equal c ?\C-m)
  9843. (setq link links)
  9844. (setq nth (- c ?0))
  9845. (if have-zero (setq nth (1+ nth)))
  9846. (unless (and (integerp nth) (>= (length links) nth))
  9847. (user-error "Invalid link selection"))
  9848. (setq link (nth (1- nth) links)))))
  9849. (cons link end))))))
  9850. ;; TODO: These functions are deprecated since `org-open-at-point'
  9851. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9852. (defun org-open-file-with-system (path)
  9853. "Open file at PATH using the system way of opening it."
  9854. (org-open-file path 'system))
  9855. (defun org-open-file-with-emacs (path)
  9856. "Open file at PATH in Emacs."
  9857. (org-open-file path 'emacs))
  9858. ;;; File search
  9859. (defvar org-create-file-search-functions nil
  9860. "List of functions to construct the right search string for a file link.
  9861. These functions are called in turn with point at the location to
  9862. which the link should point.
  9863. A function in the hook should first test if it would like to
  9864. handle this file type, for example by checking the `major-mode'
  9865. or the file extension. If it decides not to handle this file, it
  9866. should just return nil to give other functions a chance. If it
  9867. does handle the file, it must return the search string to be used
  9868. when following the link. The search string will be part of the
  9869. file link, given after a double colon, and `org-open-at-point'
  9870. will automatically search for it. If special measures must be
  9871. taken to make the search successful, another function should be
  9872. added to the companion hook `org-execute-file-search-functions',
  9873. which see.
  9874. A function in this hook may also use `setq' to set the variable
  9875. `description' to provide a suggestion for the descriptive text to
  9876. be used for this link when it gets inserted into an Org-mode
  9877. buffer with \\[org-insert-link].")
  9878. (defvar org-execute-file-search-functions nil
  9879. "List of functions to execute a file search triggered by a link.
  9880. Functions added to this hook must accept a single argument, the
  9881. search string that was part of the file link, the part after the
  9882. double colon. The function must first check if it would like to
  9883. handle this search, for example by checking the `major-mode' or
  9884. the file extension. If it decides not to handle this search, it
  9885. should just return nil to give other functions a chance. If it
  9886. does handle the search, it must return a non-nil value to keep
  9887. other functions from trying.
  9888. Each function can access the current prefix argument through the
  9889. variable `current-prefix-arg'. Note that a single prefix is used
  9890. to force opening a link in Emacs, so it may be good to only use a
  9891. numeric or double prefix to guide the search function.
  9892. In case this is needed, a function in this hook can also restore
  9893. the window configuration before `org-open-at-point' was called using:
  9894. (set-window-configuration org-window-config-before-follow-link)")
  9895. (defun org-search-radio-target (target)
  9896. "Search a radio target matching TARGET in current buffer.
  9897. White spaces are not significant."
  9898. (let ((re (format "<<<%s>>>"
  9899. (mapconcat #'regexp-quote
  9900. (org-split-string target "[ \t\n]+")
  9901. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9902. (origin (point)))
  9903. (goto-char (point-min))
  9904. (catch :radio-match
  9905. (while (re-search-forward re nil t)
  9906. (backward-char)
  9907. (let ((object (org-element-context)))
  9908. (when (eq (org-element-type object) 'radio-target)
  9909. (goto-char (org-element-property :begin object))
  9910. (org-show-context 'link-search)
  9911. (throw :radio-match nil))))
  9912. (goto-char origin)
  9913. (user-error "No match for radio target: %s" target))))
  9914. (defun org-link-search (s &optional avoid-pos stealth)
  9915. "Search for a search string S.
  9916. If S starts with \"#\", it triggers a custom ID search.
  9917. If S is enclosed within parenthesis, it initiates a coderef
  9918. search.
  9919. If S is surrounded by forward slashes, it is interpreted as
  9920. a regular expression. In Org mode files, this will create an
  9921. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9922. to list matches. If the current buffer is in `dired-mode', grep
  9923. will be used to search in all files.
  9924. When AVOID-POS is given, ignore matches near that position.
  9925. When optional argument STEALTH is non-nil, do not modify
  9926. visibility around point, thus ignoring `org-show-context-detail'
  9927. variable.
  9928. Search is case-insensitive and ignores white spaces. Return type
  9929. of matched result, with is either `dedicated' or `fuzzy'."
  9930. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9931. (let* ((case-fold-search t)
  9932. (origin (point))
  9933. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9934. (words (org-split-string s "[ \t\n]+"))
  9935. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9936. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9937. type)
  9938. (cond
  9939. ;; Check if there are any special search functions.
  9940. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9941. ((eq (string-to-char s) ?#)
  9942. ;; Look for a custom ID S if S starts with "#".
  9943. (let* ((id (substring normalized 1))
  9944. (match (org-find-property "CUSTOM_ID" id)))
  9945. (if match (progn (goto-char match) (setf type 'dedicated))
  9946. (error "No match for custom ID: %s" id))))
  9947. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9948. ;; Look for coderef targets if S is enclosed within parenthesis.
  9949. (let ((coderef (match-string-no-properties 1 normalized))
  9950. (re (substring s-single-re 1 -1)))
  9951. (goto-char (point-min))
  9952. (catch :coderef-match
  9953. (while (re-search-forward re nil t)
  9954. (let ((element (org-element-at-point)))
  9955. (when (and (memq (org-element-type element)
  9956. '(example-block src-block))
  9957. ;; Build proper regexp according to current
  9958. ;; block's label format.
  9959. (let ((label-fmt
  9960. (regexp-quote
  9961. (or (org-element-property :label-fmt element)
  9962. org-coderef-label-format))))
  9963. (save-excursion
  9964. (beginning-of-line)
  9965. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9966. (format label-fmt coderef))))))
  9967. (setq type 'dedicated)
  9968. (goto-char (match-beginning 1))
  9969. (throw :coderef-match nil))))
  9970. (goto-char origin)
  9971. (error "No match for coderef: %s" coderef))))
  9972. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9973. ;; Look for a regular expression.
  9974. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9975. (match-string 1 s)))
  9976. ;; Fuzzy links.
  9977. (t
  9978. (let* ((starred (eq (string-to-char normalized) ?*))
  9979. (headline-search (and (derived-mode-p 'org-mode)
  9980. (or org-link-search-must-match-exact-headline
  9981. starred))))
  9982. (cond
  9983. ;; Look for targets, only if not in a headline search.
  9984. ((and (not starred)
  9985. (let ((target (format "<<%s>>" s-multi-re)))
  9986. (catch :target-match
  9987. (goto-char (point-min))
  9988. (while (re-search-forward target nil t)
  9989. (backward-char)
  9990. (let ((context (org-element-context)))
  9991. (when (eq (org-element-type context) 'target)
  9992. (setq type 'dedicated)
  9993. (goto-char (org-element-property :begin context))
  9994. (throw :target-match t))))
  9995. nil))))
  9996. ;; Look for elements named after S, only if not in a headline
  9997. ;; search.
  9998. ((and (not starred)
  9999. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  10000. (catch :name-match
  10001. (goto-char (point-min))
  10002. (while (re-search-forward name nil t)
  10003. (let ((element (org-element-at-point)))
  10004. (when (equal (org-split-string
  10005. (org-element-property :name element)
  10006. "[ \t]+")
  10007. words)
  10008. (setq type 'dedicated)
  10009. (beginning-of-line)
  10010. (throw :name-match t))))
  10011. nil))))
  10012. ;; Regular text search. Prefer headlines in Org mode
  10013. ;; buffers.
  10014. ((and (derived-mode-p 'org-mode)
  10015. (let* ((wspace "[ \t]")
  10016. (wspaceopt (concat wspace "*"))
  10017. (cookie (concat "\\(?:"
  10018. wspaceopt
  10019. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10020. wspaceopt
  10021. "\\)"))
  10022. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)"))
  10023. (re (concat
  10024. org-outline-regexp-bol
  10025. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10026. "\\(?:\\[#.\\][ \t]+\\)?"
  10027. "\\(?:" org-comment-string "[ \t]+\\)?"
  10028. sep "*"
  10029. (let ((title (mapconcat #'regexp-quote
  10030. words
  10031. (concat sep "+"))))
  10032. (if starred (substring title 1) title))
  10033. sep "*"
  10034. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10035. "[ \t]*$")))
  10036. (goto-char (point-min))
  10037. (re-search-forward re nil t)))
  10038. (goto-char (match-beginning 0))
  10039. (setq type 'dedicated))
  10040. ;; Offer to create non-existent headline depending on
  10041. ;; `org-link-search-must-match-exact-headline'.
  10042. ((and (derived-mode-p 'org-mode)
  10043. (not org-link-search-inhibit-query)
  10044. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10045. (yes-or-no-p "No match - create this as a new heading? "))
  10046. (goto-char (point-max))
  10047. (unless (bolp) (newline))
  10048. (org-insert-heading nil t t)
  10049. (insert s "\n")
  10050. (beginning-of-line 0))
  10051. ;; Only headlines are looked after. No need to process
  10052. ;; further: throw an error.
  10053. ((and (derived-mode-p 'org-mode)
  10054. (or starred org-link-search-must-match-exact-headline))
  10055. (goto-char origin)
  10056. (error "No match for fuzzy expression: %s" normalized))
  10057. ;; Regular text search.
  10058. ((catch :fuzzy-match
  10059. (goto-char (point-min))
  10060. (while (re-search-forward s-multi-re nil t)
  10061. ;; Skip match if it contains AVOID-POS or it is included
  10062. ;; in a link with a description but outside the
  10063. ;; description.
  10064. (unless (or (and avoid-pos
  10065. (<= (match-beginning 0) avoid-pos)
  10066. (> (match-end 0) avoid-pos))
  10067. (and (save-match-data
  10068. (org-in-regexp org-bracket-link-regexp))
  10069. (match-beginning 3)
  10070. (or (> (match-beginning 3) (point))
  10071. (<= (match-end 3) (point)))
  10072. (org-element-lineage
  10073. (save-match-data (org-element-context))
  10074. '(link) t)))
  10075. (goto-char (match-beginning 0))
  10076. (setq type 'fuzzy)
  10077. (throw :fuzzy-match t)))
  10078. nil))
  10079. ;; All failed. Throw an error.
  10080. (t (goto-char origin)
  10081. (error "No match for fuzzy expression: %s" normalized))))))
  10082. ;; Disclose surroundings of match, if appropriate.
  10083. (when (and (derived-mode-p 'org-mode) (not stealth))
  10084. (org-show-context 'link-search))
  10085. type))
  10086. (defun org-get-buffer-for-internal-link (buffer)
  10087. "Return a buffer to be used for displaying the link target of internal links."
  10088. (cond
  10089. ((not org-display-internal-link-with-indirect-buffer)
  10090. buffer)
  10091. ((string-match "(Clone)$" (buffer-name buffer))
  10092. (message "Buffer is already a clone, not making another one")
  10093. ;; we also do not modify visibility in this case
  10094. buffer)
  10095. (t ; make a new indirect buffer for displaying the link
  10096. (let* ((bn (buffer-name buffer))
  10097. (ibn (concat bn "(Clone)"))
  10098. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10099. (with-current-buffer ib (org-overview))
  10100. ib))))
  10101. (defun org-do-occur (regexp &optional cleanup)
  10102. "Call the Emacs command `occur'.
  10103. If CLEANUP is non-nil, remove the printout of the regular expression
  10104. in the *Occur* buffer. This is useful if the regex is long and not useful
  10105. to read."
  10106. (occur regexp)
  10107. (when cleanup
  10108. (let ((cwin (selected-window)) win beg end)
  10109. (when (setq win (get-buffer-window "*Occur*"))
  10110. (select-window win))
  10111. (goto-char (point-min))
  10112. (when (re-search-forward "match[a-z]+" nil t)
  10113. (setq beg (match-end 0))
  10114. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10115. (setq end (1- (match-beginning 0)))))
  10116. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10117. (goto-char (point-min))
  10118. (select-window cwin))))
  10119. ;;; The mark ring for links jumps
  10120. (defvar org-mark-ring nil
  10121. "Mark ring for positions before jumps in Org-mode.")
  10122. (defvar org-mark-ring-last-goto nil
  10123. "Last position in the mark ring used to go back.")
  10124. ;; Fill and close the ring
  10125. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10126. (loop for i from 1 to org-mark-ring-length do
  10127. (push (make-marker) org-mark-ring))
  10128. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10129. org-mark-ring)
  10130. (defun org-mark-ring-push (&optional pos buffer)
  10131. "Put the current position or POS into the mark ring and rotate it."
  10132. (interactive)
  10133. (setq pos (or pos (point)))
  10134. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10135. (move-marker (car org-mark-ring)
  10136. (or pos (point))
  10137. (or buffer (current-buffer)))
  10138. (message "%s"
  10139. (substitute-command-keys
  10140. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10141. (defun org-mark-ring-goto (&optional n)
  10142. "Jump to the previous position in the mark ring.
  10143. With prefix arg N, jump back that many stored positions. When
  10144. called several times in succession, walk through the entire ring.
  10145. Org-mode commands jumping to a different position in the current file,
  10146. or to another Org-mode file, automatically push the old position
  10147. onto the ring."
  10148. (interactive "p")
  10149. (let (p m)
  10150. (if (eq last-command this-command)
  10151. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10152. (setq p org-mark-ring))
  10153. (setq org-mark-ring-last-goto p)
  10154. (setq m (car p))
  10155. (org-pop-to-buffer-same-window (marker-buffer m))
  10156. (goto-char m)
  10157. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10158. (defun org-remove-angle-brackets (s)
  10159. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10160. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10161. s)
  10162. (defun org-add-angle-brackets (s)
  10163. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10164. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10165. s)
  10166. (defun org-remove-double-quotes (s)
  10167. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10168. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10169. s)
  10170. ;;; Following specific links
  10171. (defun org-follow-timestamp-link ()
  10172. "Open an agenda view for the time-stamp date/range at point."
  10173. (cond
  10174. ((org-at-date-range-p t)
  10175. (let ((org-agenda-start-on-weekday)
  10176. (t1 (match-string 1))
  10177. (t2 (match-string 2)) tt1 tt2)
  10178. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10179. tt2 (time-to-days (org-time-string-to-time t2)))
  10180. (let ((org-agenda-buffer-tmp-name
  10181. (format "*Org Agenda(a:%s)"
  10182. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10183. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10184. ((org-at-timestamp-p t)
  10185. (let ((org-agenda-buffer-tmp-name
  10186. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10187. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10188. (substring (match-string 1) 0 10)))
  10189. 1)))
  10190. (t (error "This should not happen"))))
  10191. ;;; Following file links
  10192. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10193. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10194. (declare-function mailcap-mime-info
  10195. "mailcap" (string &optional request no-decode))
  10196. (defvar org-wait nil)
  10197. (defun org-open-file (path &optional in-emacs line search)
  10198. "Open the file at PATH.
  10199. First, this expands any special file name abbreviations. Then the
  10200. configuration variable `org-file-apps' is checked if it contains an
  10201. entry for this file type, and if yes, the corresponding command is launched.
  10202. If no application is found, Emacs simply visits the file.
  10203. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10204. With a double \\[universal-argument] \\[universal-argument] \
  10205. prefix arg, Org tries to avoid opening in Emacs
  10206. and to use an external application to visit the file.
  10207. Optional LINE specifies a line to go to, optional SEARCH a string
  10208. to search for. If LINE or SEARCH is given, the file will be
  10209. opened in Emacs, unless an entry from org-file-apps that makes
  10210. use of groups in a regexp matches.
  10211. If you want to change the way frames are used when following a
  10212. link, please customize `org-link-frame-setup'.
  10213. If the file does not exist, an error is thrown."
  10214. (let* ((file (if (equal path "")
  10215. buffer-file-name
  10216. (substitute-in-file-name (expand-file-name path))))
  10217. (file-apps (append org-file-apps (org-default-apps)))
  10218. (apps (org-remove-if
  10219. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10220. (apps-dlink (org-remove-if-not
  10221. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10222. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10223. (dirp (if remp nil (file-directory-p file)))
  10224. (file (if (and dirp org-open-directory-means-index-dot-org)
  10225. (concat (file-name-as-directory file) "index.org")
  10226. file))
  10227. (a-m-a-p (assq 'auto-mode apps))
  10228. (dfile (downcase file))
  10229. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10230. (link (cond ((and (eq line nil)
  10231. (eq search nil))
  10232. file)
  10233. (line
  10234. (concat file "::" (number-to-string line)))
  10235. (search
  10236. (concat file "::" search))))
  10237. (dlink (downcase link))
  10238. (old-buffer (current-buffer))
  10239. (old-pos (point))
  10240. (old-mode major-mode)
  10241. ext cmd link-match-data)
  10242. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10243. (setq ext (match-string 1 dfile))
  10244. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10245. (setq ext (match-string 1 dfile))))
  10246. (cond
  10247. ((member in-emacs '((16) system))
  10248. (setq cmd (cdr (assoc 'system apps))))
  10249. (in-emacs (setq cmd 'emacs))
  10250. (t
  10251. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10252. (and dirp (cdr (assoc 'directory apps)))
  10253. ; first, try matching against apps-dlink
  10254. ; if we get a match here, store the match data for later
  10255. (let ((match (assoc-default dlink apps-dlink
  10256. 'string-match)))
  10257. (if match
  10258. (progn (setq link-match-data (match-data))
  10259. match)
  10260. (progn (setq in-emacs (or in-emacs line search))
  10261. nil))) ; if we have no match in apps-dlink,
  10262. ; always open the file in emacs if line or search
  10263. ; is given (for backwards compatibility)
  10264. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10265. 'string-match)
  10266. (cdr (assoc ext apps))
  10267. (cdr (assoc t apps))))))
  10268. (when (eq cmd 'system)
  10269. (setq cmd (cdr (assoc 'system apps))))
  10270. (when (eq cmd 'default)
  10271. (setq cmd (cdr (assoc t apps))))
  10272. (when (eq cmd 'mailcap)
  10273. (require 'mailcap)
  10274. (mailcap-parse-mailcaps)
  10275. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10276. (command (mailcap-mime-info mime-type)))
  10277. (if (stringp command)
  10278. (setq cmd command)
  10279. (setq cmd 'emacs))))
  10280. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10281. (not (file-exists-p file))
  10282. (not org-open-non-existing-files))
  10283. (user-error "No such file: %s" file))
  10284. (cond
  10285. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10286. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10287. (while (string-match "['\"]%s['\"]" cmd)
  10288. (setq cmd (replace-match "%s" t t cmd)))
  10289. (while (string-match "%s" cmd)
  10290. (setq cmd (replace-match
  10291. (save-match-data
  10292. (shell-quote-argument
  10293. (convert-standard-filename file)))
  10294. t t cmd)))
  10295. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10296. (save-match-data
  10297. (let ((match-index 1)
  10298. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10299. (set-match-data link-match-data)
  10300. (while (<= match-index number-of-groups)
  10301. (let ((regex (concat "%" (number-to-string match-index)))
  10302. (replace-with (match-string match-index dlink)))
  10303. (while (string-match regex cmd)
  10304. (setq cmd (replace-match replace-with t t cmd))))
  10305. (setq match-index (+ match-index 1)))))
  10306. (save-window-excursion
  10307. (message "Running %s...done" cmd)
  10308. (start-process-shell-command cmd nil cmd)
  10309. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10310. ((or (stringp cmd)
  10311. (eq cmd 'emacs))
  10312. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10313. (widen)
  10314. (if line (progn (org-goto-line line)
  10315. (if (derived-mode-p 'org-mode)
  10316. (org-reveal)))
  10317. (if search (org-link-search search))))
  10318. ((consp cmd)
  10319. (let ((file (convert-standard-filename file)))
  10320. (save-match-data
  10321. (set-match-data link-match-data)
  10322. (eval cmd))))
  10323. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10324. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10325. (or (not (equal old-buffer (current-buffer)))
  10326. (not (equal old-pos (point))))
  10327. (org-mark-ring-push old-pos old-buffer))))
  10328. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10329. "This function returns non-nil if `entry' uses a regular
  10330. expression which should be matched against the whole link by
  10331. org-open-file.
  10332. It assumes that is the case when the entry uses a regular
  10333. expression which has at least one grouping construct and the
  10334. action is either a lisp form or a command string containing
  10335. '%1', i.e. using at least one subexpression match as a
  10336. parameter."
  10337. (let ((selector (car entry))
  10338. (action (cdr entry)))
  10339. (if (stringp selector)
  10340. (and (> (regexp-opt-depth selector) 0)
  10341. (or (and (stringp action)
  10342. (string-match "%[0-9]" action))
  10343. (consp action)))
  10344. nil)))
  10345. (defun org-default-apps ()
  10346. "Return the default applications for this operating system."
  10347. (cond
  10348. ((eq system-type 'darwin)
  10349. org-file-apps-defaults-macosx)
  10350. ((eq system-type 'windows-nt)
  10351. org-file-apps-defaults-windowsnt)
  10352. (t org-file-apps-defaults-gnu)))
  10353. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10354. "Convert extensions to regular expressions in the cars of LIST.
  10355. Also, weed out any non-string entries, because the return value is used
  10356. only for regexp matching.
  10357. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10358. point to the symbol `emacs', indicating that the file should
  10359. be opened in Emacs."
  10360. (append
  10361. (delq nil
  10362. (mapcar (lambda (x)
  10363. (if (not (stringp (car x)))
  10364. nil
  10365. (if (string-match "\\W" (car x))
  10366. x
  10367. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10368. list))
  10369. (if add-auto-mode
  10370. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10371. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10372. (defun org-file-remote-p (file)
  10373. "Test whether FILE specifies a location on a remote system.
  10374. Return non-nil if the location is indeed remote.
  10375. For example, the filename \"/user@host:/foo\" specifies a location
  10376. on the system \"/user@host:\"."
  10377. (cond ((fboundp 'file-remote-p)
  10378. (file-remote-p file))
  10379. ((fboundp 'tramp-handle-file-remote-p)
  10380. (tramp-handle-file-remote-p file))
  10381. ((and (boundp 'ange-ftp-name-format)
  10382. (string-match (car ange-ftp-name-format) file))
  10383. t)))
  10384. ;;;; Refiling
  10385. (defun org-get-org-file ()
  10386. "Read a filename, with default directory `org-directory'."
  10387. (let ((default (or org-default-notes-file remember-data-file)))
  10388. (read-file-name (format "File name [%s]: " default)
  10389. (file-name-as-directory org-directory)
  10390. default)))
  10391. (defun org-notes-order-reversed-p ()
  10392. "Check if the current file should receive notes in reversed order."
  10393. (cond
  10394. ((not org-reverse-note-order) nil)
  10395. ((eq t org-reverse-note-order) t)
  10396. ((not (listp org-reverse-note-order)) nil)
  10397. (t (catch 'exit
  10398. (dolist (entry org-reverse-note-order)
  10399. (if (string-match (car entry) buffer-file-name)
  10400. (throw 'exit (cdr entry))))))))
  10401. (defvar org-refile-target-table nil
  10402. "The list of refile targets, created by `org-refile'.")
  10403. (defvar org-agenda-new-buffers nil
  10404. "Buffers created to visit agenda files.")
  10405. (defvar org-refile-cache nil
  10406. "Cache for refile targets.")
  10407. (defvar org-refile-markers nil
  10408. "All the markers used for caching refile locations.")
  10409. (defun org-refile-marker (pos)
  10410. "Get a new refile marker, but only if caching is in use."
  10411. (if (not org-refile-use-cache)
  10412. pos
  10413. (let ((m (make-marker)))
  10414. (move-marker m pos)
  10415. (push m org-refile-markers)
  10416. m)))
  10417. (defun org-refile-cache-clear ()
  10418. "Clear the refile cache and disable all the markers."
  10419. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10420. (setq org-refile-markers nil)
  10421. (setq org-refile-cache nil)
  10422. (message "Refile cache has been cleared"))
  10423. (defun org-refile-cache-check-set (set)
  10424. "Check if all the markers in the cache still have live buffers."
  10425. (let (marker)
  10426. (catch 'exit
  10427. (while (and set (setq marker (nth 3 (pop set))))
  10428. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10429. (when (and marker (null (marker-buffer marker)))
  10430. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10431. (sit-for 3)
  10432. (throw 'exit nil)))
  10433. t)))
  10434. (defun org-refile-cache-put (set &rest identifiers)
  10435. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10436. (let* ((key (sha1 (prin1-to-string identifiers)))
  10437. (entry (assoc key org-refile-cache)))
  10438. (if entry
  10439. (setcdr entry set)
  10440. (push (cons key set) org-refile-cache))))
  10441. (defun org-refile-cache-get (&rest identifiers)
  10442. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10443. (cond
  10444. ((not org-refile-cache) nil)
  10445. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10446. (t
  10447. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10448. org-refile-cache))))
  10449. (and set (org-refile-cache-check-set set) set)))))
  10450. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10451. "Produce a table with refile targets."
  10452. (let ((case-fold-search nil)
  10453. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10454. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10455. targets tgs txt re files desc descre fast-path-p level pos0)
  10456. (message "Getting targets...")
  10457. (with-current-buffer (or default-buffer (current-buffer))
  10458. (dolist (entry entries)
  10459. (setq files (car entry) desc (cdr entry))
  10460. (setq fast-path-p nil)
  10461. (cond
  10462. ((null files) (setq files (list (current-buffer))))
  10463. ((eq files 'org-agenda-files)
  10464. (setq files (org-agenda-files 'unrestricted)))
  10465. ((and (symbolp files) (fboundp files))
  10466. (setq files (funcall files)))
  10467. ((and (symbolp files) (boundp files))
  10468. (setq files (symbol-value files))))
  10469. (if (stringp files) (setq files (list files)))
  10470. (cond
  10471. ((eq (car desc) :tag)
  10472. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10473. ((eq (car desc) :todo)
  10474. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10475. ((eq (car desc) :regexp)
  10476. (setq descre (cdr desc)))
  10477. ((eq (car desc) :level)
  10478. (setq descre (concat "^\\*\\{" (number-to-string
  10479. (if org-odd-levels-only
  10480. (1- (* 2 (cdr desc)))
  10481. (cdr desc)))
  10482. "\\}[ \t]")))
  10483. ((eq (car desc) :maxlevel)
  10484. (setq fast-path-p t)
  10485. (setq descre (concat "^\\*\\{1," (number-to-string
  10486. (if org-odd-levels-only
  10487. (1- (* 2 (cdr desc)))
  10488. (cdr desc)))
  10489. "\\}[ \t]")))
  10490. (t (error "Bad refiling target description %s" desc)))
  10491. (dolist (f files)
  10492. (with-current-buffer
  10493. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10494. (or
  10495. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10496. (progn
  10497. (if (bufferp f) (setq f (buffer-file-name
  10498. (buffer-base-buffer f))))
  10499. (setq f (and f (expand-file-name f)))
  10500. (if (eq org-refile-use-outline-path 'file)
  10501. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10502. (save-excursion
  10503. (save-restriction
  10504. (widen)
  10505. (goto-char (point-min))
  10506. (while (re-search-forward descre nil t)
  10507. (goto-char (setq pos0 (point-at-bol)))
  10508. (catch 'next
  10509. (when org-refile-target-verify-function
  10510. (save-match-data
  10511. (or (funcall org-refile-target-verify-function)
  10512. (throw 'next t))))
  10513. (when (and (looking-at org-complex-heading-regexp)
  10514. (not (member (match-string 4) excluded-entries))
  10515. (match-string 4))
  10516. (setq level (org-reduced-level
  10517. (- (match-end 1) (match-beginning 1)))
  10518. txt (org-link-display-format (match-string 4))
  10519. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10520. re (format org-complex-heading-regexp-format
  10521. (regexp-quote (match-string 4))))
  10522. (when org-refile-use-outline-path
  10523. (setq txt (mapconcat
  10524. 'org-protect-slash
  10525. (append
  10526. (if (eq org-refile-use-outline-path
  10527. 'file)
  10528. (list (file-name-nondirectory
  10529. (buffer-file-name
  10530. (buffer-base-buffer))))
  10531. (if (eq org-refile-use-outline-path
  10532. 'full-file-path)
  10533. (list (buffer-file-name
  10534. (buffer-base-buffer)))))
  10535. (org-get-outline-path fast-path-p
  10536. level txt)
  10537. (list txt))
  10538. "/")))
  10539. (push (list txt f re (org-refile-marker (point)))
  10540. tgs)))
  10541. (when (= (point) pos0)
  10542. ;; verification function has not moved point
  10543. (goto-char (point-at-eol))))))))
  10544. (when org-refile-use-cache
  10545. (org-refile-cache-put tgs (buffer-file-name) descre))
  10546. (setq targets (append tgs targets))))))
  10547. (message "Getting targets...done")
  10548. (nreverse targets)))
  10549. (defun org-protect-slash (s)
  10550. (while (string-match "/" s)
  10551. (setq s (replace-match "\\" t t s)))
  10552. s)
  10553. (defvar org-olpa (make-vector 20 nil))
  10554. (defun org-get-outline-path (&optional fastp level heading)
  10555. "Return the outline path to the current entry, as a list.
  10556. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10557. routine which makes outline path derivations for an entire file,
  10558. avoiding backtracing. Refile target collection makes use of that."
  10559. (if fastp
  10560. (progn
  10561. (if (> level 19)
  10562. (error "Outline path failure, more than 19 levels"))
  10563. (loop for i from level upto 19 do
  10564. (aset org-olpa i nil))
  10565. (prog1
  10566. (delq nil (append org-olpa nil))
  10567. (aset org-olpa level heading)))
  10568. (let (rtn case-fold-search)
  10569. (save-excursion
  10570. (save-restriction
  10571. (widen)
  10572. (while (org-up-heading-safe)
  10573. (when (looking-at org-complex-heading-regexp)
  10574. (push (org-trim
  10575. (replace-regexp-in-string
  10576. ;; Remove statistical/checkboxes cookies
  10577. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10578. (org-match-string-no-properties 4)))
  10579. rtn)))
  10580. rtn)))))
  10581. (defun org-format-outline-path (path &optional width prefix separator)
  10582. "Format the outline path PATH for display.
  10583. WIDTH is the maximum number of characters that is available.
  10584. PREFIX is a prefix to be included in the returned string,
  10585. such as the file name.
  10586. SEPARATOR is inserted between the different parts of the path,
  10587. the default is \"/\"."
  10588. (setq width (or width 79))
  10589. (if prefix (setq width (- width (length prefix))))
  10590. (if (not path)
  10591. (or prefix "")
  10592. (let* ((nsteps (length path))
  10593. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10594. (maxwidth (if (<= total-width width)
  10595. 10000 ;; everything fits
  10596. ;; we need to shorten the level headings
  10597. (/ (- width nsteps) nsteps)))
  10598. (org-odd-levels-only nil)
  10599. (n 0)
  10600. (total (1+ (length prefix))))
  10601. (setq maxwidth (max maxwidth 10))
  10602. (concat prefix
  10603. (if prefix (or separator "/"))
  10604. (mapconcat
  10605. (lambda (h)
  10606. (setq n (1+ n))
  10607. (if (and (= n nsteps) (< maxwidth 10000))
  10608. (setq maxwidth (- total-width total)))
  10609. (if (< (length h) maxwidth)
  10610. (progn (setq total (+ total (length h) 1)) h)
  10611. (setq h (substring h 0 (- maxwidth 2))
  10612. total (+ total maxwidth 1))
  10613. (if (string-match "[ \t]+\\'" h)
  10614. (setq h (substring h 0 (match-beginning 0))))
  10615. (setq h (concat h "..")))
  10616. (org-add-props h nil 'face
  10617. (nth (% (1- n) org-n-level-faces)
  10618. org-level-faces))
  10619. h)
  10620. path (or separator "/"))))))
  10621. (defun org-display-outline-path (&optional file current separator just-return-string)
  10622. "Display the current outline path in the echo area.
  10623. If FILE is non-nil, prepend the output with the file name.
  10624. If CURRENT is non-nil, append the current heading to the output.
  10625. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10626. the different parts of the path and defaults to \"/\".
  10627. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10628. (interactive "P")
  10629. (let* (case-fold-search
  10630. (bfn (buffer-file-name (buffer-base-buffer)))
  10631. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10632. res)
  10633. (if current (setq path (append path
  10634. (save-excursion
  10635. (org-back-to-heading t)
  10636. (if (looking-at org-complex-heading-regexp)
  10637. (list (match-string 4)))))))
  10638. (setq res
  10639. (org-format-outline-path
  10640. path
  10641. (1- (frame-width))
  10642. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10643. separator))
  10644. (if just-return-string
  10645. (org-no-properties res)
  10646. (org-unlogged-message "%s" res))))
  10647. (defvar org-refile-history nil
  10648. "History for refiling operations.")
  10649. (defvar org-after-refile-insert-hook nil
  10650. "Hook run after `org-refile' has inserted its stuff at the new location.
  10651. Note that this is still *before* the stuff will be removed from
  10652. the *old* location.")
  10653. (defvar org-capture-last-stored-marker)
  10654. (defvar org-refile-keep nil
  10655. "Non-nil means `org-refile' will copy instead of refile.")
  10656. (defun org-copy ()
  10657. "Like `org-refile', but copy."
  10658. (interactive)
  10659. (let ((org-refile-keep t))
  10660. (funcall 'org-refile nil nil nil "Copy")))
  10661. (defun org-refile (&optional arg default-buffer rfloc msg)
  10662. "Move the entry or entries at point to another heading.
  10663. The list of target headings is compiled using the information in
  10664. `org-refile-targets', which see.
  10665. At the target location, the entry is filed as a subitem of the
  10666. target heading. Depending on `org-reverse-note-order', the new
  10667. subitem will either be the first or the last subitem.
  10668. If there is an active region, all entries in that region will be
  10669. refiled. However, the region must fulfill the requirement that
  10670. the first heading sets the top-level of the moved text.
  10671. With prefix arg ARG, the command will only visit the target
  10672. location and not actually move anything.
  10673. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10674. refiling operation has put the subtree.
  10675. With a numeric prefix argument of `2', refile to the running clock.
  10676. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10677. being set to t and copy to the target location, don't move it.
  10678. Beware that keeping refiled entries may result in duplicated ID
  10679. properties.
  10680. RFLOC can be a refile location obtained in a different way.
  10681. MSG is a string to replace \"Refile\" in the default prompt with
  10682. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10683. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10684. If you are using target caching (see `org-refile-use-cache'), you
  10685. have to clear the target cache in order to find new targets.
  10686. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10687. prefix argument (`C-u C-u C-u C-c C-w')."
  10688. (interactive "P")
  10689. (if (member arg '(0 (64)))
  10690. (org-refile-cache-clear)
  10691. (let* ((actionmsg (cond (msg msg)
  10692. ((equal arg 3) "Refile (and keep)")
  10693. (t "Refile")))
  10694. (cbuf (current-buffer))
  10695. (regionp (org-region-active-p))
  10696. (region-start (and regionp (region-beginning)))
  10697. (region-end (and regionp (region-end)))
  10698. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10699. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10700. pos it nbuf file re level reversed)
  10701. (setq last-command nil)
  10702. (when regionp
  10703. (goto-char region-start)
  10704. (or (bolp) (goto-char (point-at-bol)))
  10705. (setq region-start (point))
  10706. (unless (or (org-kill-is-subtree-p
  10707. (buffer-substring region-start region-end))
  10708. (prog1 org-refile-active-region-within-subtree
  10709. (let ((s (point-at-eol)))
  10710. (org-toggle-heading)
  10711. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10712. (user-error "The region is not a (sequence of) subtree(s)")))
  10713. (if (equal arg '(16))
  10714. (org-refile-goto-last-stored)
  10715. (when (or
  10716. (and (equal arg 2)
  10717. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10718. (prog1
  10719. (setq it (list (or org-clock-heading "running clock")
  10720. (buffer-file-name
  10721. (marker-buffer org-clock-hd-marker))
  10722. ""
  10723. (marker-position org-clock-hd-marker)))
  10724. (setq arg nil)))
  10725. (setq it (or rfloc
  10726. (let (heading-text)
  10727. (save-excursion
  10728. (unless (and arg (listp arg))
  10729. (org-back-to-heading t)
  10730. (setq heading-text
  10731. (replace-regexp-in-string
  10732. org-bracket-link-regexp
  10733. "\\3"
  10734. (nth 4 (org-heading-components)))))
  10735. (org-refile-get-location
  10736. (cond ((and arg (listp arg)) "Goto")
  10737. (regionp (concat actionmsg " region to"))
  10738. (t (concat actionmsg " subtree \""
  10739. heading-text "\" to")))
  10740. default-buffer
  10741. (and (not (equal '(4) arg))
  10742. org-refile-allow-creating-parent-nodes)
  10743. arg))))))
  10744. (setq file (nth 1 it)
  10745. re (nth 2 it)
  10746. pos (nth 3 it))
  10747. (if (and (not arg)
  10748. pos
  10749. (equal (buffer-file-name) file)
  10750. (if regionp
  10751. (and (>= pos region-start)
  10752. (<= pos region-end))
  10753. (and (>= pos (point))
  10754. (< pos (save-excursion
  10755. (org-end-of-subtree t t))))))
  10756. (error "Cannot refile to position inside the tree or region"))
  10757. (setq nbuf (or (find-buffer-visiting file)
  10758. (find-file-noselect file)))
  10759. (if (and arg (not (equal arg 3)))
  10760. (progn
  10761. (org-pop-to-buffer-same-window nbuf)
  10762. (goto-char pos)
  10763. (org-show-context 'org-goto))
  10764. (if regionp
  10765. (progn
  10766. (org-kill-new (buffer-substring region-start region-end))
  10767. (org-save-markers-in-region region-start region-end))
  10768. (org-copy-subtree 1 nil t))
  10769. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10770. (find-file-noselect file)))
  10771. (setq reversed (org-notes-order-reversed-p))
  10772. (save-excursion
  10773. (save-restriction
  10774. (widen)
  10775. (if pos
  10776. (progn
  10777. (goto-char pos)
  10778. (looking-at org-outline-regexp)
  10779. (setq level (org-get-valid-level (funcall outline-level) 1))
  10780. (goto-char
  10781. (if reversed
  10782. (or (outline-next-heading) (point-max))
  10783. (or (save-excursion (org-get-next-sibling))
  10784. (org-end-of-subtree t t)
  10785. (point-max)))))
  10786. (setq level 1)
  10787. (if (not reversed)
  10788. (goto-char (point-max))
  10789. (goto-char (point-min))
  10790. (or (outline-next-heading) (goto-char (point-max)))))
  10791. (if (not (bolp)) (newline))
  10792. (org-paste-subtree level nil nil t)
  10793. (when org-log-refile
  10794. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10795. (unless (eq org-log-refile 'note)
  10796. (save-excursion (org-add-log-note))))
  10797. (and org-auto-align-tags
  10798. (let ((org-loop-over-headlines-in-active-region nil))
  10799. (org-set-tags nil t)))
  10800. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10801. :last-refile)))
  10802. (when bookmark-name
  10803. (with-demoted-errors
  10804. (bookmark-set bookmark-name))))
  10805. ;; If we are refiling for capture, make sure that the
  10806. ;; last-capture pointers point here
  10807. (when (org-bound-and-true-p org-refile-for-capture)
  10808. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10809. :last-capture-marker)))
  10810. (when bookmark-name
  10811. (with-demoted-errors
  10812. (bookmark-set bookmark-name))))
  10813. (move-marker org-capture-last-stored-marker (point)))
  10814. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10815. (run-hooks 'org-after-refile-insert-hook))))
  10816. (unless org-refile-keep
  10817. (if regionp
  10818. (delete-region (point) (+ (point) (- region-end region-start)))
  10819. (delete-region
  10820. (and (org-back-to-heading t) (point))
  10821. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10822. (when (featurep 'org-inlinetask)
  10823. (org-inlinetask-remove-END-maybe))
  10824. (setq org-markers-to-move nil)
  10825. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10826. (defun org-refile-goto-last-stored ()
  10827. "Go to the location where the last refile was stored."
  10828. (interactive)
  10829. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10830. (message "This is the location of the last refile"))
  10831. (defun org-refile--get-location (refloc tbl)
  10832. "When user refile to REFLOC, find the associated target in TBL.
  10833. Also check `org-refile-target-table'."
  10834. (car (delq
  10835. nil
  10836. (mapcar
  10837. (lambda (r) (or (assoc r tbl)
  10838. (assoc r org-refile-target-table)))
  10839. (list (replace-regexp-in-string "/$" "" refloc)
  10840. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10841. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10842. no-exclude)
  10843. "Prompt the user for a refile location, using PROMPT.
  10844. PROMPT should not be suffixed with a colon and a space, because
  10845. this function appends the default value from
  10846. `org-refile-history' automatically, if that is not empty.
  10847. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10848. this is used for the GOTO interface."
  10849. (let ((org-refile-targets org-refile-targets)
  10850. (org-refile-use-outline-path org-refile-use-outline-path)
  10851. excluded-entries)
  10852. (when (and (derived-mode-p 'org-mode)
  10853. (not org-refile-use-cache)
  10854. (not no-exclude))
  10855. (org-map-tree
  10856. (lambda()
  10857. (setq excluded-entries
  10858. (append excluded-entries (list (org-get-heading t t)))))))
  10859. (setq org-refile-target-table
  10860. (org-refile-get-targets default-buffer excluded-entries)))
  10861. (unless org-refile-target-table
  10862. (user-error "No refile targets"))
  10863. (let* ((cbuf (current-buffer))
  10864. (partial-completion-mode nil)
  10865. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10866. (cfunc (if (and org-refile-use-outline-path
  10867. org-outline-path-complete-in-steps)
  10868. 'org-olpath-completing-read
  10869. 'org-icompleting-read))
  10870. (extra (if org-refile-use-outline-path "/" ""))
  10871. (cbnex (concat (buffer-name) extra))
  10872. (filename (and cfn (expand-file-name cfn)))
  10873. (tbl (mapcar
  10874. (lambda (x)
  10875. (if (and (not (member org-refile-use-outline-path
  10876. '(file full-file-path)))
  10877. (not (equal filename (nth 1 x))))
  10878. (cons (concat (car x) extra " ("
  10879. (file-name-nondirectory (nth 1 x)) ")")
  10880. (cdr x))
  10881. (cons (concat (car x) extra) (cdr x))))
  10882. org-refile-target-table))
  10883. (completion-ignore-case t)
  10884. cdef
  10885. (prompt (concat prompt
  10886. (or (and (car org-refile-history)
  10887. (concat " (default " (car org-refile-history) ")"))
  10888. (and (assoc cbnex tbl) (setq cdef cbnex)
  10889. (concat " (default " cbnex ")"))) ": "))
  10890. pa answ parent-target child parent old-hist)
  10891. (setq old-hist org-refile-history)
  10892. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10893. nil 'org-refile-history (or cdef (car org-refile-history))))
  10894. (if (setq pa (org-refile--get-location answ tbl))
  10895. (progn
  10896. (org-refile-check-position pa)
  10897. (when (or (not org-refile-history)
  10898. (not (eq old-hist org-refile-history))
  10899. (not (equal (car pa) (car org-refile-history))))
  10900. (setq org-refile-history
  10901. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10902. org-refile-history
  10903. (cdr org-refile-history))))
  10904. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10905. (pop org-refile-history)))
  10906. pa)
  10907. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10908. (progn
  10909. (setq parent (match-string 1 answ)
  10910. child (match-string 2 answ))
  10911. (setq parent-target (org-refile--get-location parent tbl))
  10912. (when (and parent-target
  10913. (or (eq new-nodes t)
  10914. (and (eq new-nodes 'confirm)
  10915. (y-or-n-p (format "Create new node \"%s\"? "
  10916. child)))))
  10917. (org-refile-new-child parent-target child)))
  10918. (user-error "Invalid target location")))))
  10919. (declare-function org-string-nw-p "org-macs" (s))
  10920. (defun org-refile-check-position (refile-pointer)
  10921. "Check if the refile pointer matches the headline to which it points."
  10922. (let* ((file (nth 1 refile-pointer))
  10923. (re (nth 2 refile-pointer))
  10924. (pos (nth 3 refile-pointer))
  10925. buffer)
  10926. (if (and (not (markerp pos)) (not file))
  10927. (user-error "Please indicate a target file in the refile path")
  10928. (when (org-string-nw-p re)
  10929. (setq buffer (if (markerp pos)
  10930. (marker-buffer pos)
  10931. (or (find-buffer-visiting file)
  10932. (find-file-noselect file))))
  10933. (with-current-buffer buffer
  10934. (save-excursion
  10935. (save-restriction
  10936. (widen)
  10937. (goto-char pos)
  10938. (beginning-of-line 1)
  10939. (unless (org-looking-at-p re)
  10940. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10941. (defun org-refile-new-child (parent-target child)
  10942. "Use refile target PARENT-TARGET to add new CHILD below it."
  10943. (unless parent-target
  10944. (error "Cannot find parent for new node"))
  10945. (let ((file (nth 1 parent-target))
  10946. (pos (nth 3 parent-target))
  10947. level)
  10948. (with-current-buffer (or (find-buffer-visiting file)
  10949. (find-file-noselect file))
  10950. (save-excursion
  10951. (save-restriction
  10952. (widen)
  10953. (if pos
  10954. (goto-char pos)
  10955. (goto-char (point-max))
  10956. (if (not (bolp)) (newline)))
  10957. (when (looking-at org-outline-regexp)
  10958. (setq level (funcall outline-level))
  10959. (org-end-of-subtree t t))
  10960. (org-back-over-empty-lines)
  10961. (insert "\n" (make-string
  10962. (if pos (org-get-valid-level level 1) 1) ?*)
  10963. " " child "\n")
  10964. (beginning-of-line 0)
  10965. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10966. (defun org-olpath-completing-read (prompt collection &rest args)
  10967. "Read an outline path like a file name."
  10968. (let ((thetable collection)
  10969. (org-completion-use-ido nil) ; does not work with ido.
  10970. (org-completion-use-iswitchb nil)) ; or iswitchb
  10971. (apply #'org-icompleting-read
  10972. prompt
  10973. (lambda (string predicate &optional flag)
  10974. (cond
  10975. ((eq flag nil) (try-completion string thetable))
  10976. ((eq flag t)
  10977. (let ((l (length string)))
  10978. (mapcar (lambda (x)
  10979. (let ((r (substring x l))
  10980. (f (if (string-match " ([^)]*)$" x)
  10981. (match-string 0 x)
  10982. "")))
  10983. (if (string-match "/" r)
  10984. (concat string (substring r 0 (match-end 0)) f)
  10985. x)))
  10986. (all-completions string thetable predicate))))
  10987. ;; Exact match?
  10988. ((eq flag 'lambda) (assoc string thetable))))
  10989. args)))
  10990. ;;;; Dynamic blocks
  10991. (defun org-find-dblock (name)
  10992. "Find the first dynamic block with name NAME in the buffer.
  10993. If not found, stay at current position and return nil."
  10994. (let ((case-fold-search t) pos)
  10995. (save-excursion
  10996. (goto-char (point-min))
  10997. (setq pos (and (re-search-forward
  10998. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10999. (match-beginning 0))))
  11000. (if pos (goto-char pos))
  11001. pos))
  11002. (defun org-create-dblock (plist)
  11003. "Create a dynamic block section, with parameters taken from PLIST.
  11004. PLIST must contain a :name entry which is used as the name of the block."
  11005. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11006. (end-of-line 1)
  11007. (newline))
  11008. (let ((col (current-column))
  11009. (name (plist-get plist :name)))
  11010. (insert "#+BEGIN: " name)
  11011. (while plist
  11012. (if (eq (car plist) :name)
  11013. (setq plist (cddr plist))
  11014. (insert " " (prin1-to-string (pop plist)))))
  11015. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11016. (beginning-of-line -2)))
  11017. (defun org-prepare-dblock ()
  11018. "Prepare dynamic block for refresh.
  11019. This empties the block, puts the cursor at the insert position and returns
  11020. the property list including an extra property :name with the block name."
  11021. (unless (looking-at org-dblock-start-re)
  11022. (user-error "Not at a dynamic block"))
  11023. (let* ((begdel (1+ (match-end 0)))
  11024. (name (org-no-properties (match-string 1)))
  11025. (params (append (list :name name)
  11026. (read (concat "(" (match-string 3) ")")))))
  11027. (save-excursion
  11028. (beginning-of-line 1)
  11029. (skip-chars-forward " \t")
  11030. (setq params (plist-put params :indentation-column (current-column))))
  11031. (unless (re-search-forward org-dblock-end-re nil t)
  11032. (error "Dynamic block not terminated"))
  11033. (setq params
  11034. (append params
  11035. (list :content (buffer-substring
  11036. begdel (match-beginning 0)))))
  11037. (delete-region begdel (match-beginning 0))
  11038. (goto-char begdel)
  11039. (open-line 1)
  11040. params))
  11041. (defun org-map-dblocks (&optional command)
  11042. "Apply COMMAND to all dynamic blocks in the current buffer.
  11043. If COMMAND is not given, use `org-update-dblock'."
  11044. (let ((cmd (or command 'org-update-dblock)))
  11045. (save-excursion
  11046. (goto-char (point-min))
  11047. (while (re-search-forward org-dblock-start-re nil t)
  11048. (goto-char (match-beginning 0))
  11049. (save-excursion
  11050. (condition-case nil
  11051. (funcall cmd)
  11052. (error (message "Error during update of dynamic block"))))
  11053. (unless (re-search-forward org-dblock-end-re nil t)
  11054. (error "Dynamic block not terminated"))))))
  11055. (defun org-dblock-update (&optional arg)
  11056. "User command for updating dynamic blocks.
  11057. Update the dynamic block at point. With prefix ARG, update all dynamic
  11058. blocks in the buffer."
  11059. (interactive "P")
  11060. (if arg
  11061. (org-update-all-dblocks)
  11062. (or (looking-at org-dblock-start-re)
  11063. (org-beginning-of-dblock))
  11064. (org-update-dblock)))
  11065. (defun org-update-dblock ()
  11066. "Update the dynamic block at point.
  11067. This means to empty the block, parse for parameters and then call
  11068. the correct writing function."
  11069. (interactive)
  11070. (save-excursion
  11071. (let* ((win (selected-window))
  11072. (pos (point))
  11073. (line (org-current-line))
  11074. (params (org-prepare-dblock))
  11075. (name (plist-get params :name))
  11076. (indent (plist-get params :indentation-column))
  11077. (cmd (intern (concat "org-dblock-write:" name))))
  11078. (message "Updating dynamic block `%s' at line %d..." name line)
  11079. (funcall cmd params)
  11080. (message "Updating dynamic block `%s' at line %d...done" name line)
  11081. (goto-char pos)
  11082. (when (and indent (> indent 0))
  11083. (setq indent (make-string indent ?\ ))
  11084. (save-excursion
  11085. (select-window win)
  11086. (org-beginning-of-dblock)
  11087. (forward-line 1)
  11088. (while (not (looking-at org-dblock-end-re))
  11089. (insert indent)
  11090. (beginning-of-line 2))
  11091. (when (looking-at org-dblock-end-re)
  11092. (and (looking-at "[ \t]+")
  11093. (replace-match ""))
  11094. (insert indent)))))))
  11095. (defun org-beginning-of-dblock ()
  11096. "Find the beginning of the dynamic block at point.
  11097. Error if there is no such block at point."
  11098. (let ((pos (point))
  11099. beg)
  11100. (end-of-line 1)
  11101. (if (and (re-search-backward org-dblock-start-re nil t)
  11102. (setq beg (match-beginning 0))
  11103. (re-search-forward org-dblock-end-re nil t)
  11104. (> (match-end 0) pos))
  11105. (goto-char beg)
  11106. (goto-char pos)
  11107. (error "Not in a dynamic block"))))
  11108. (defun org-update-all-dblocks ()
  11109. "Update all dynamic blocks in the buffer.
  11110. This function can be used in a hook."
  11111. (interactive)
  11112. (when (derived-mode-p 'org-mode)
  11113. (org-map-dblocks 'org-update-dblock)))
  11114. ;;;; Completion
  11115. (declare-function org-export-backend-name "org-export" (cl-x))
  11116. (declare-function org-export-backend-options "org-export" (cl-x))
  11117. (defun org-get-export-keywords ()
  11118. "Return a list of all currently understood export keywords.
  11119. Export keywords include options, block names, attributes and
  11120. keywords relative to each registered export back-end."
  11121. (let (keywords)
  11122. (dolist (backend
  11123. (org-bound-and-true-p org-export-registered-backends)
  11124. (delq nil keywords))
  11125. ;; Back-end name (for keywords, like #+LATEX:)
  11126. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11127. (dolist (option-entry (org-export-backend-options backend))
  11128. ;; Back-end options.
  11129. (push (nth 1 option-entry) keywords)))))
  11130. (defconst org-options-keywords
  11131. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11132. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11133. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11134. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11135. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11136. (defcustom org-structure-template-alist
  11137. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11138. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11139. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11140. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11141. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11142. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11143. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11144. ("L" "#+LaTeX: ")
  11145. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11146. ("H" "#+HTML: ")
  11147. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11148. ("A" "#+ASCII: ")
  11149. ("i" "#+INDEX: ?")
  11150. ("I" "#+INCLUDE: %file ?"))
  11151. "Structure completion elements.
  11152. This is a list of abbreviation keys and values. The value gets inserted
  11153. if you type `<' followed by the key and then press the completion key,
  11154. usually `TAB'. %file will be replaced by a file name after prompting
  11155. for the file using completion. The cursor will be placed at the position
  11156. of the `?' in the template.
  11157. There are two templates for each key, the first uses the original Org syntax,
  11158. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11159. the default when the /org-mtags.el/ module has been loaded. See also the
  11160. variable `org-mtags-prefer-muse-templates'."
  11161. :group 'org-completion
  11162. :type '(repeat
  11163. (list
  11164. (string :tag "Key")
  11165. (string :tag "Template")))
  11166. :version "25.1"
  11167. :package-version '(Org . "8.3"))
  11168. (defun org-try-structure-completion ()
  11169. "Try to complete a structure template before point.
  11170. This looks for strings like \"<e\" on an otherwise empty line and
  11171. expands them."
  11172. (let ((l (buffer-substring (point-at-bol) (point)))
  11173. a)
  11174. (when (and (looking-at "[ \t]*$")
  11175. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11176. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11177. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11178. (match-beginning 1)) a)
  11179. t)))
  11180. (defun org-complete-expand-structure-template (start cell)
  11181. "Expand a structure template."
  11182. (let ((rpl (nth 1 cell))
  11183. (ind ""))
  11184. (delete-region start (point))
  11185. (when (string-match "\\`[ \t]*#\\+" rpl)
  11186. (cond
  11187. ((bolp))
  11188. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11189. (setq ind (buffer-substring (point-at-bol) (point))))
  11190. (t (newline))))
  11191. (setq start (point))
  11192. (if (string-match "%file" rpl)
  11193. (setq rpl (replace-match
  11194. (concat
  11195. "\""
  11196. (save-match-data
  11197. (abbreviate-file-name (read-file-name "Include file: ")))
  11198. "\"")
  11199. t t rpl)))
  11200. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11201. (concat "\n" ind)))
  11202. (insert rpl)
  11203. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11204. ;;;; TODO, DEADLINE, Comments
  11205. (defun org-toggle-comment ()
  11206. "Change the COMMENT state of an entry."
  11207. (interactive)
  11208. (save-excursion
  11209. (org-back-to-heading)
  11210. (looking-at org-complex-heading-regexp)
  11211. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11212. (skip-chars-forward " \t")
  11213. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11214. (if (org-in-commented-heading-p t)
  11215. (delete-region (point)
  11216. (progn (search-forward " " (line-end-position) 'move)
  11217. (skip-chars-forward " \t")
  11218. (point)))
  11219. (insert org-comment-string)
  11220. (unless (eolp) (insert " ")))))
  11221. (defvar org-last-todo-state-is-todo nil
  11222. "This is non-nil when the last TODO state change led to a TODO state.
  11223. If the last change removed the TODO tag or switched to DONE, then
  11224. this is nil.")
  11225. (defvar org-setting-tags nil) ; dynamically skipped
  11226. (defvar org-todo-setup-filter-hook nil
  11227. "Hook for functions that pre-filter todo specs.
  11228. Each function takes a todo spec and returns either nil or the spec
  11229. transformed into canonical form." )
  11230. (defvar org-todo-get-default-hook nil
  11231. "Hook for functions that get a default item for todo.
  11232. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11233. nil or a string to be used for the todo mark." )
  11234. (defvar org-agenda-headline-snapshot-before-repeat)
  11235. (defun org-current-effective-time ()
  11236. "Return current time adjusted for `org-extend-today-until' variable."
  11237. (let* ((ct (org-current-time))
  11238. (dct (decode-time ct))
  11239. (ct1
  11240. (cond
  11241. (org-use-last-clock-out-time-as-effective-time
  11242. (or (org-clock-get-last-clock-out-time) ct))
  11243. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11244. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11245. (t ct))))
  11246. ct1))
  11247. (defun org-todo-yesterday (&optional arg)
  11248. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11249. (interactive "P")
  11250. (if (eq major-mode 'org-agenda-mode)
  11251. (apply 'org-agenda-todo-yesterday arg)
  11252. (let* ((org-use-effective-time t)
  11253. (hour (third (decode-time
  11254. (org-current-time))))
  11255. (org-extend-today-until (1+ hour)))
  11256. (org-todo arg))))
  11257. (defvar org-block-entry-blocking ""
  11258. "First entry preventing the TODO state change.")
  11259. (defun org-cancel-repeater ()
  11260. "Cancel a repeater by setting its numeric value to zero."
  11261. (interactive)
  11262. (save-excursion
  11263. (org-back-to-heading t)
  11264. (let ((bound1 (point))
  11265. (bound0 (save-excursion (outline-next-heading) (point))))
  11266. (when (re-search-forward
  11267. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11268. org-deadline-time-regexp "\\)\\|\\("
  11269. org-ts-regexp "\\)")
  11270. bound0 t)
  11271. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11272. (replace-match "0" t nil nil 1))))))
  11273. (defun org-todo (&optional arg)
  11274. "Change the TODO state of an item.
  11275. The state of an item is given by a keyword at the start of the heading,
  11276. like
  11277. *** TODO Write paper
  11278. *** DONE Call mom
  11279. The different keywords are specified in the variable `org-todo-keywords'.
  11280. By default the available states are \"TODO\" and \"DONE\".
  11281. So for this example: when the item starts with TODO, it is changed to DONE.
  11282. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11283. DONE are present, add TODO at the beginning of the heading.
  11284. With \\[universal-argument] prefix arg, use completion to determine the new \
  11285. state.
  11286. With numeric prefix arg, switch to that state.
  11287. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11288. keywords (nextset).
  11289. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11290. With a numeric prefix arg of 0, inhibit note taking for the change.
  11291. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11292. When called through ELisp, arg is also interpreted in the following way:
  11293. 'none -> empty state
  11294. \"\"(empty string) -> switch to empty state
  11295. 'done -> switch to DONE
  11296. 'nextset -> switch to the next set of keywords
  11297. 'previousset -> switch to the previous set of keywords
  11298. \"WAITING\" -> switch to the specified keyword, but only if it
  11299. really is a member of `org-todo-keywords'."
  11300. (interactive "P")
  11301. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11302. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11303. 'region-start-level 'region))
  11304. org-loop-over-headlines-in-active-region)
  11305. (org-map-entries
  11306. `(org-todo ,arg)
  11307. org-loop-over-headlines-in-active-region
  11308. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11309. (if (equal arg '(16)) (setq arg 'nextset))
  11310. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11311. (let ((org-blocker-hook org-blocker-hook)
  11312. commentp
  11313. case-fold-search)
  11314. (when (equal arg '(64))
  11315. (setq arg nil org-blocker-hook nil))
  11316. (when (and org-blocker-hook
  11317. (or org-inhibit-blocking
  11318. (org-entry-get nil "NOBLOCKING")))
  11319. (setq org-blocker-hook nil))
  11320. (save-excursion
  11321. (catch 'exit
  11322. (org-back-to-heading t)
  11323. (when (org-in-commented-heading-p t)
  11324. (org-toggle-comment)
  11325. (setq commentp t))
  11326. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11327. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11328. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11329. (let* ((match-data (match-data))
  11330. (startpos (point-at-bol))
  11331. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11332. (org-log-done org-log-done)
  11333. (org-log-repeat org-log-repeat)
  11334. (org-todo-log-states org-todo-log-states)
  11335. (org-inhibit-logging
  11336. (if (equal arg 0)
  11337. (progn (setq arg nil) 'note) org-inhibit-logging))
  11338. (this (match-string 1))
  11339. (hl-pos (match-beginning 0))
  11340. (head (org-get-todo-sequence-head this))
  11341. (ass (assoc head org-todo-kwd-alist))
  11342. (interpret (nth 1 ass))
  11343. (done-word (nth 3 ass))
  11344. (final-done-word (nth 4 ass))
  11345. (org-last-state (or this ""))
  11346. (completion-ignore-case t)
  11347. (member (member this org-todo-keywords-1))
  11348. (tail (cdr member))
  11349. (org-state (cond
  11350. ((and org-todo-key-trigger
  11351. (or (and (equal arg '(4))
  11352. (eq org-use-fast-todo-selection 'prefix))
  11353. (and (not arg) org-use-fast-todo-selection
  11354. (not (eq org-use-fast-todo-selection
  11355. 'prefix)))))
  11356. ;; Use fast selection
  11357. (org-fast-todo-selection))
  11358. ((and (equal arg '(4))
  11359. (or (not org-use-fast-todo-selection)
  11360. (not org-todo-key-trigger)))
  11361. ;; Read a state with completion
  11362. (org-icompleting-read
  11363. "State: " (mapcar 'list org-todo-keywords-1)
  11364. nil t))
  11365. ((eq arg 'right)
  11366. (if this
  11367. (if tail (car tail) nil)
  11368. (car org-todo-keywords-1)))
  11369. ((eq arg 'left)
  11370. (if (equal member org-todo-keywords-1)
  11371. nil
  11372. (if this
  11373. (nth (- (length org-todo-keywords-1)
  11374. (length tail) 2)
  11375. org-todo-keywords-1)
  11376. (org-last org-todo-keywords-1))))
  11377. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11378. (setq arg nil))) ; hack to fall back to cycling
  11379. (arg
  11380. ;; user or caller requests a specific state
  11381. (cond
  11382. ((equal arg "") nil)
  11383. ((eq arg 'none) nil)
  11384. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11385. ((eq arg 'nextset)
  11386. (or (car (cdr (member head org-todo-heads)))
  11387. (car org-todo-heads)))
  11388. ((eq arg 'previousset)
  11389. (let ((org-todo-heads (reverse org-todo-heads)))
  11390. (or (car (cdr (member head org-todo-heads)))
  11391. (car org-todo-heads))))
  11392. ((car (member arg org-todo-keywords-1)))
  11393. ((stringp arg)
  11394. (user-error "State `%s' not valid in this file" arg))
  11395. ((nth (1- (prefix-numeric-value arg))
  11396. org-todo-keywords-1))))
  11397. ((null member) (or head (car org-todo-keywords-1)))
  11398. ((equal this final-done-word) nil) ;; -> make empty
  11399. ((null tail) nil) ;; -> first entry
  11400. ((memq interpret '(type priority))
  11401. (if (eq this-command last-command)
  11402. (car tail)
  11403. (if (> (length tail) 0)
  11404. (or done-word (car org-done-keywords))
  11405. nil)))
  11406. (t
  11407. (car tail))))
  11408. (org-state (or
  11409. (run-hook-with-args-until-success
  11410. 'org-todo-get-default-hook org-state org-last-state)
  11411. org-state))
  11412. (next (if org-state (concat " " org-state " ") " "))
  11413. (change-plist (list :type 'todo-state-change :from this :to org-state
  11414. :position startpos))
  11415. dolog now-done-p)
  11416. (when org-blocker-hook
  11417. (setq org-last-todo-state-is-todo
  11418. (not (member this org-done-keywords)))
  11419. (unless (save-excursion
  11420. (save-match-data
  11421. (org-with-wide-buffer
  11422. (run-hook-with-args-until-failure
  11423. 'org-blocker-hook change-plist))))
  11424. (if (org-called-interactively-p 'interactive)
  11425. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11426. this org-state org-block-entry-blocking)
  11427. ;; fail silently
  11428. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11429. this org-state org-block-entry-blocking)
  11430. (throw 'exit nil))))
  11431. (store-match-data match-data)
  11432. (replace-match next t t)
  11433. (cond ((equal this org-state)
  11434. (message "TODO state was already %s" (org-trim next)))
  11435. ((pos-visible-in-window-p hl-pos)
  11436. (message "TODO state changed to %s" (org-trim next))))
  11437. (unless head
  11438. (setq head (org-get-todo-sequence-head org-state)
  11439. ass (assoc head org-todo-kwd-alist)
  11440. interpret (nth 1 ass)
  11441. done-word (nth 3 ass)
  11442. final-done-word (nth 4 ass)))
  11443. (when (memq arg '(nextset previousset))
  11444. (message "Keyword-Set %d/%d: %s"
  11445. (- (length org-todo-sets) -1
  11446. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11447. (length org-todo-sets)
  11448. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11449. (setq org-last-todo-state-is-todo
  11450. (not (member org-state org-done-keywords)))
  11451. (setq now-done-p (and (member org-state org-done-keywords)
  11452. (not (member this org-done-keywords))))
  11453. (and logging (org-local-logging logging))
  11454. (when (and (or org-todo-log-states org-log-done)
  11455. (not (eq org-inhibit-logging t))
  11456. (not (memq arg '(nextset previousset))))
  11457. ;; we need to look at recording a time and note
  11458. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11459. (nth 2 (assoc this org-todo-log-states))))
  11460. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11461. (setq dolog 'time))
  11462. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11463. (and org-state
  11464. (member org-state org-not-done-keywords)
  11465. (not (member this org-not-done-keywords))))
  11466. ;; This is now a todo state and was not one before
  11467. ;; If there was a CLOSED time stamp, get rid of it.
  11468. (org-add-planning-info nil nil 'closed))
  11469. (when (and now-done-p org-log-done)
  11470. ;; It is now done, and it was not done before
  11471. (org-add-planning-info 'closed (org-current-effective-time))
  11472. (if (and (not dolog) (eq 'note org-log-done))
  11473. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11474. (when (and org-state dolog)
  11475. ;; This is a non-nil state, and we need to log it
  11476. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11477. ;; Fixup tag positioning
  11478. (org-todo-trigger-tag-changes org-state)
  11479. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11480. (when org-provide-todo-statistics
  11481. (org-update-parent-todo-statistics))
  11482. (run-hooks 'org-after-todo-state-change-hook)
  11483. (if (and arg (not (member org-state org-done-keywords)))
  11484. (setq head (org-get-todo-sequence-head org-state)))
  11485. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11486. ;; Do we need to trigger a repeat?
  11487. (when now-done-p
  11488. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11489. ;; This is for the agenda, take a snapshot of the headline.
  11490. (save-match-data
  11491. (setq org-agenda-headline-snapshot-before-repeat
  11492. (org-get-heading))))
  11493. (org-auto-repeat-maybe org-state))
  11494. ;; Fixup cursor location if close to the keyword
  11495. (if (and (outline-on-heading-p)
  11496. (not (bolp))
  11497. (save-excursion (beginning-of-line 1)
  11498. (looking-at org-todo-line-regexp))
  11499. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11500. (progn
  11501. (goto-char (or (match-end 2) (match-end 1)))
  11502. (and (looking-at " ") (just-one-space))))
  11503. (when org-trigger-hook
  11504. (save-excursion
  11505. (run-hook-with-args 'org-trigger-hook change-plist)))
  11506. (when commentp (org-toggle-comment))))))))
  11507. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11508. "Block turning an entry into a TODO, using the hierarchy.
  11509. This checks whether the current task should be blocked from state
  11510. changes. Such blocking occurs when:
  11511. 1. The task has children which are not all in a completed state.
  11512. 2. A task has a parent with the property :ORDERED:, and there
  11513. are siblings prior to the current task with incomplete
  11514. status.
  11515. 3. The parent of the task is blocked because it has siblings that should
  11516. be done first, or is child of a block grandparent TODO entry."
  11517. (if (not org-enforce-todo-dependencies)
  11518. t ; if locally turned off don't block
  11519. (catch 'dont-block
  11520. ;; If this is not a todo state change, or if this entry is already DONE,
  11521. ;; do not block
  11522. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11523. (member (plist-get change-plist :from)
  11524. (cons 'done org-done-keywords))
  11525. (member (plist-get change-plist :to)
  11526. (cons 'todo org-not-done-keywords))
  11527. (not (plist-get change-plist :to)))
  11528. (throw 'dont-block t))
  11529. ;; If this task has children, and any are undone, it's blocked
  11530. (save-excursion
  11531. (org-back-to-heading t)
  11532. (let ((this-level (funcall outline-level)))
  11533. (outline-next-heading)
  11534. (let ((child-level (funcall outline-level)))
  11535. (while (and (not (eobp))
  11536. (> child-level this-level))
  11537. ;; this todo has children, check whether they are all
  11538. ;; completed
  11539. (if (and (not (org-entry-is-done-p))
  11540. (org-entry-is-todo-p))
  11541. (progn (setq org-block-entry-blocking (org-get-heading))
  11542. (throw 'dont-block nil)))
  11543. (outline-next-heading)
  11544. (setq child-level (funcall outline-level))))))
  11545. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11546. ;; any previous siblings are undone, it's blocked
  11547. (save-excursion
  11548. (org-back-to-heading t)
  11549. (let* ((pos (point))
  11550. (parent-pos (and (org-up-heading-safe) (point))))
  11551. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11552. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11553. (forward-line 1)
  11554. (re-search-forward org-not-done-heading-regexp pos t))
  11555. (setq org-block-entry-blocking (match-string 0))
  11556. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11557. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11558. (while t
  11559. (goto-char parent-pos)
  11560. (if (not (looking-at org-not-done-heading-regexp))
  11561. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11562. (setq pos (point))
  11563. (setq parent-pos (and (org-up-heading-safe) (point)))
  11564. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11565. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11566. (forward-line 1)
  11567. (re-search-forward org-not-done-heading-regexp pos t)
  11568. (setq org-block-entry-blocking (org-get-heading)))
  11569. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11570. (defcustom org-track-ordered-property-with-tag nil
  11571. "Should the ORDERED property also be shown as a tag?
  11572. The ORDERED property decides if an entry should require subtasks to be
  11573. completed in sequence. Since a property is not very visible, setting
  11574. this option means that toggling the ORDERED property with the command
  11575. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11576. not relevant for the behavior, but it makes things more visible.
  11577. Note that toggling the tag with tags commands will not change the property
  11578. and therefore not influence behavior!
  11579. This can be t, meaning the tag ORDERED should be used, It can also be a
  11580. string to select a different tag for this task."
  11581. :group 'org-todo
  11582. :type '(choice
  11583. (const :tag "No tracking" nil)
  11584. (const :tag "Track with ORDERED tag" t)
  11585. (string :tag "Use other tag")))
  11586. (defun org-toggle-ordered-property ()
  11587. "Toggle the ORDERED property of the current entry.
  11588. For better visibility, you can track the value of this property with a tag.
  11589. See variable `org-track-ordered-property-with-tag'."
  11590. (interactive)
  11591. (let* ((t1 org-track-ordered-property-with-tag)
  11592. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11593. (save-excursion
  11594. (org-back-to-heading)
  11595. (if (org-entry-get nil "ORDERED")
  11596. (progn
  11597. (org-delete-property "ORDERED")
  11598. (and tag (org-toggle-tag tag 'off))
  11599. (message "Subtasks can be completed in arbitrary order"))
  11600. (org-entry-put nil "ORDERED" "t")
  11601. (and tag (org-toggle-tag tag 'on))
  11602. (message "Subtasks must be completed in sequence")))))
  11603. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11604. (defun org-block-todo-from-checkboxes (change-plist)
  11605. "Block turning an entry into a TODO, using checkboxes.
  11606. This checks whether the current task should be blocked from state
  11607. changes because there are unchecked boxes in this entry."
  11608. (if (not org-enforce-todo-checkbox-dependencies)
  11609. t ; if locally turned off don't block
  11610. (catch 'dont-block
  11611. ;; If this is not a todo state change, or if this entry is already DONE,
  11612. ;; do not block
  11613. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11614. (member (plist-get change-plist :from)
  11615. (cons 'done org-done-keywords))
  11616. (member (plist-get change-plist :to)
  11617. (cons 'todo org-not-done-keywords))
  11618. (not (plist-get change-plist :to)))
  11619. (throw 'dont-block t))
  11620. ;; If this task has checkboxes that are not checked, it's blocked
  11621. (save-excursion
  11622. (org-back-to-heading t)
  11623. (let ((beg (point)) end)
  11624. (outline-next-heading)
  11625. (setq end (point))
  11626. (goto-char beg)
  11627. (if (org-list-search-forward
  11628. (concat (org-item-beginning-re)
  11629. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11630. "\\[[- ]\\]")
  11631. end t)
  11632. (progn
  11633. (if (boundp 'org-blocked-by-checkboxes)
  11634. (setq org-blocked-by-checkboxes t))
  11635. (throw 'dont-block nil)))))
  11636. t))) ; do not block
  11637. (defun org-entry-blocked-p ()
  11638. "Non-nil if entry at point is blocked."
  11639. (and (not (org-entry-get nil "NOBLOCKING"))
  11640. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11641. (not (run-hook-with-args-until-failure
  11642. 'org-blocker-hook
  11643. (list :type 'todo-state-change
  11644. :position (point)
  11645. :from 'todo
  11646. :to 'done)))))
  11647. (defun org-update-statistics-cookies (all)
  11648. "Update the statistics cookie, either from TODO or from checkboxes.
  11649. This should be called with the cursor in a line with a statistics cookie."
  11650. (interactive "P")
  11651. (if all
  11652. (progn
  11653. (org-update-checkbox-count 'all)
  11654. (org-map-entries 'org-update-parent-todo-statistics))
  11655. (if (not (org-at-heading-p))
  11656. (org-update-checkbox-count)
  11657. (let ((pos (point-marker))
  11658. end l1 l2)
  11659. (ignore-errors (org-back-to-heading t))
  11660. (if (not (org-at-heading-p))
  11661. (org-update-checkbox-count)
  11662. (setq l1 (org-outline-level))
  11663. (setq end (save-excursion
  11664. (outline-next-heading)
  11665. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11666. (point)))
  11667. (if (and (save-excursion
  11668. (re-search-forward
  11669. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11670. (not (save-excursion (re-search-forward
  11671. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11672. (org-update-checkbox-count)
  11673. (if (and l2 (> l2 l1))
  11674. (progn
  11675. (goto-char end)
  11676. (org-update-parent-todo-statistics))
  11677. (goto-char pos)
  11678. (beginning-of-line 1)
  11679. (while (re-search-forward
  11680. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11681. (point-at-eol) t)
  11682. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11683. (goto-char pos)
  11684. (move-marker pos nil)))))
  11685. (defvar org-entry-property-inherited-from) ;; defined below
  11686. (defun org-update-parent-todo-statistics ()
  11687. "Update any statistics cookie in the parent of the current headline.
  11688. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11689. the entire subtree and this will travel up the hierarchy and update
  11690. statistics everywhere."
  11691. (let* ((prop (save-excursion (org-up-heading-safe)
  11692. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11693. (recursive (or (not org-hierarchical-todo-statistics)
  11694. (and prop (string-match "\\<recursive\\>" prop))))
  11695. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11696. 0))
  11697. (first t)
  11698. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11699. level ltoggle l1 new ndel
  11700. (cnt-all 0) (cnt-done 0) is-percent kwd
  11701. checkbox-beg ov ovs ove cookie-present)
  11702. (catch 'exit
  11703. (save-excursion
  11704. (beginning-of-line 1)
  11705. (setq ltoggle (funcall outline-level))
  11706. ;; Three situations are to consider:
  11707. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11708. ;; to the top-level ancestor on the headline;
  11709. ;; 2. If parent has "recursive" property, repeat up to the
  11710. ;; headline setting that property, taking inheritance into
  11711. ;; account;
  11712. ;; 3. Else, move up to direct parent and proceed only once.
  11713. (while (and (setq level (org-up-heading-safe))
  11714. (or recursive first)
  11715. (>= (point) lim))
  11716. (setq first nil cookie-present nil)
  11717. (unless (and level
  11718. (not (string-match
  11719. "\\<checkbox\\>"
  11720. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11721. "")))))
  11722. (throw 'exit nil))
  11723. (while (re-search-forward box-re (point-at-eol) t)
  11724. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11725. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11726. (save-match-data
  11727. (unless (outline-next-heading) (throw 'exit nil))
  11728. (while (and (looking-at org-complex-heading-regexp)
  11729. (> (setq l1 (length (match-string 1))) level))
  11730. (setq kwd (and (or recursive (= l1 ltoggle))
  11731. (match-string 2)))
  11732. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11733. (and (eq org-provide-todo-statistics t)
  11734. (or (member kwd org-done-keywords)))
  11735. (and (listp org-provide-todo-statistics)
  11736. (stringp (car org-provide-todo-statistics))
  11737. (or (member kwd org-provide-todo-statistics)
  11738. (member kwd org-done-keywords)))
  11739. (and (listp org-provide-todo-statistics)
  11740. (listp (car org-provide-todo-statistics))
  11741. (or (member kwd (car org-provide-todo-statistics))
  11742. (and (member kwd org-done-keywords)
  11743. (member kwd (cadr org-provide-todo-statistics))))))
  11744. (setq cnt-all (1+ cnt-all))
  11745. (if (eq org-provide-todo-statistics t)
  11746. (and kwd (setq cnt-all (1+ cnt-all)))))
  11747. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11748. (member kwd org-done-keywords))
  11749. (and (listp org-provide-todo-statistics)
  11750. (listp (car org-provide-todo-statistics))
  11751. (member kwd org-done-keywords)
  11752. (member kwd (cadr org-provide-todo-statistics)))
  11753. (and (listp org-provide-todo-statistics)
  11754. (stringp (car org-provide-todo-statistics))
  11755. (member kwd org-done-keywords)))
  11756. (setq cnt-done (1+ cnt-done)))
  11757. (outline-next-heading)))
  11758. (setq new
  11759. (if is-percent
  11760. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11761. (max 1 cnt-all)))
  11762. (format "[%d/%d]" cnt-done cnt-all))
  11763. ndel (- (match-end 0) checkbox-beg))
  11764. ;; handle overlays when updating cookie from column view
  11765. (when (setq ov (car (overlays-at checkbox-beg)))
  11766. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11767. (delete-overlay ov))
  11768. (goto-char checkbox-beg)
  11769. (insert new)
  11770. (delete-region (point) (+ (point) ndel))
  11771. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11772. (when ov (move-overlay ov ovs ove)))
  11773. (when cookie-present
  11774. (run-hook-with-args 'org-after-todo-statistics-hook
  11775. cnt-done (- cnt-all cnt-done))))))
  11776. (run-hooks 'org-todo-statistics-hook)))
  11777. (defvar org-after-todo-statistics-hook nil
  11778. "Hook that is called after a TODO statistics cookie has been updated.
  11779. Each function is called with two arguments: the number of not-done entries
  11780. and the number of done entries.
  11781. For example, the following function, when added to this hook, will switch
  11782. an entry to DONE when all children are done, and back to TODO when new
  11783. entries are set to a TODO status. Note that this hook is only called
  11784. when there is a statistics cookie in the headline!
  11785. (defun org-summary-todo (n-done n-not-done)
  11786. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11787. (let (org-log-done org-log-states) ; turn off logging
  11788. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11789. ")
  11790. (defvar org-todo-statistics-hook nil
  11791. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11792. This hook runs even if there is no statistics cookie present, in which case
  11793. `org-after-todo-statistics-hook' would not run.")
  11794. (defun org-todo-trigger-tag-changes (state)
  11795. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11796. (let ((l org-todo-state-tags-triggers)
  11797. changes)
  11798. (when (or (not state) (equal state ""))
  11799. (setq changes (append changes (cdr (assoc "" l)))))
  11800. (when (and (stringp state) (> (length state) 0))
  11801. (setq changes (append changes (cdr (assoc state l)))))
  11802. (when (member state org-not-done-keywords)
  11803. (setq changes (append changes (cdr (assoc 'todo l)))))
  11804. (when (member state org-done-keywords)
  11805. (setq changes (append changes (cdr (assoc 'done l)))))
  11806. (dolist (c changes)
  11807. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11808. (defun org-local-logging (value)
  11809. "Get logging settings from a property VALUE."
  11810. ;; Directly set the variables, they are already local.
  11811. (setq org-log-done nil
  11812. org-log-repeat nil
  11813. org-todo-log-states nil)
  11814. (dolist (w (org-split-string value))
  11815. (let (a)
  11816. (cond
  11817. ((setq a (assoc w org-startup-options))
  11818. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11819. (set (nth 1 a) (nth 2 a))))
  11820. ((setq a (org-extract-log-state-settings w))
  11821. (and (member (car a) org-todo-keywords-1)
  11822. (push a org-todo-log-states)))))))
  11823. (defun org-get-todo-sequence-head (kwd)
  11824. "Return the head of the TODO sequence to which KWD belongs.
  11825. If KWD is not set, check if there is a text property remembering the
  11826. right sequence."
  11827. (let (p)
  11828. (cond
  11829. ((not kwd)
  11830. (or (get-text-property (point-at-bol) 'org-todo-head)
  11831. (progn
  11832. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11833. nil (point-at-eol)))
  11834. (get-text-property p 'org-todo-head))))
  11835. ((not (member kwd org-todo-keywords-1))
  11836. (car org-todo-keywords-1))
  11837. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11838. (defun org-fast-todo-selection ()
  11839. "Fast TODO keyword selection with single keys.
  11840. Returns the new TODO keyword, or nil if no state change should occur."
  11841. (let* ((fulltable org-todo-key-alist)
  11842. (done-keywords org-done-keywords) ;; needed for the faces.
  11843. (maxlen (apply 'max (mapcar
  11844. (lambda (x)
  11845. (if (stringp (car x)) (string-width (car x)) 0))
  11846. fulltable)))
  11847. (expert nil)
  11848. (fwidth (+ maxlen 3 1 3))
  11849. (ncol (/ (- (window-width) 4) fwidth))
  11850. tg cnt e c tbl
  11851. groups ingroup)
  11852. (save-excursion
  11853. (save-window-excursion
  11854. (if expert
  11855. (set-buffer (get-buffer-create " *Org todo*"))
  11856. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11857. (erase-buffer)
  11858. (org-set-local 'org-done-keywords done-keywords)
  11859. (setq tbl fulltable cnt 0)
  11860. (while (setq e (pop tbl))
  11861. (cond
  11862. ((equal e '(:startgroup))
  11863. (push '() groups) (setq ingroup t)
  11864. (when (not (= cnt 0))
  11865. (setq cnt 0)
  11866. (insert "\n"))
  11867. (insert "{ "))
  11868. ((equal e '(:endgroup))
  11869. (setq ingroup nil cnt 0)
  11870. (insert "}\n"))
  11871. ((equal e '(:newline))
  11872. (when (not (= cnt 0))
  11873. (setq cnt 0)
  11874. (insert "\n")
  11875. (setq e (car tbl))
  11876. (while (equal (car tbl) '(:newline))
  11877. (insert "\n")
  11878. (setq tbl (cdr tbl)))))
  11879. (t
  11880. (setq tg (car e) c (cdr e))
  11881. (if ingroup (push tg (car groups)))
  11882. (setq tg (org-add-props tg nil 'face
  11883. (org-get-todo-face tg)))
  11884. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11885. (insert "[" c "] " tg (make-string
  11886. (- fwidth 4 (length tg)) ?\ ))
  11887. (when (= (setq cnt (1+ cnt)) ncol)
  11888. (insert "\n")
  11889. (if ingroup (insert " "))
  11890. (setq cnt 0)))))
  11891. (insert "\n")
  11892. (goto-char (point-min))
  11893. (if (not expert) (org-fit-window-to-buffer))
  11894. (message "[a-z..]:Set [SPC]:clear")
  11895. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11896. (cond
  11897. ((or (= c ?\C-g)
  11898. (and (= c ?q) (not (rassoc c fulltable))))
  11899. (setq quit-flag t))
  11900. ((= c ?\ ) nil)
  11901. ((setq e (rassoc c fulltable) tg (car e))
  11902. tg)
  11903. (t (setq quit-flag t)))))))
  11904. (defun org-entry-is-todo-p ()
  11905. (member (org-get-todo-state) org-not-done-keywords))
  11906. (defun org-entry-is-done-p ()
  11907. (member (org-get-todo-state) org-done-keywords))
  11908. (defun org-get-todo-state ()
  11909. "Return the TODO keyword of the current subtree."
  11910. (save-excursion
  11911. (org-back-to-heading t)
  11912. (and (looking-at org-todo-line-regexp)
  11913. (match-end 2)
  11914. (match-string 2))))
  11915. (defun org-at-date-range-p (&optional inactive-ok)
  11916. "Non-nil if point is inside a date range.
  11917. When optional argument INACTIVE-OK is non-nil, also consider
  11918. inactive time ranges.
  11919. When this function returns a non-nil value, match data is set
  11920. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11921. on INACTIVE-OK."
  11922. (interactive)
  11923. (save-excursion
  11924. (catch 'exit
  11925. (let ((pos (point)))
  11926. (skip-chars-backward "^[<\r\n")
  11927. (skip-chars-backward "<[")
  11928. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11929. (>= (match-end 0) pos)
  11930. (throw 'exit t))
  11931. (skip-chars-backward "^<[\r\n")
  11932. (skip-chars-backward "<[")
  11933. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11934. (>= (match-end 0) pos)
  11935. (throw 'exit t)))
  11936. nil)))
  11937. (defun org-get-repeat (&optional tagline)
  11938. "Check if there is a deadline/schedule with repeater in this entry."
  11939. (save-match-data
  11940. (save-excursion
  11941. (org-back-to-heading t)
  11942. (and (re-search-forward (if tagline
  11943. (concat tagline "\\s-*" org-repeat-re)
  11944. org-repeat-re)
  11945. (org-entry-end-position) t)
  11946. (match-string-no-properties 1)))))
  11947. (defvar org-last-changed-timestamp)
  11948. (defvar org-last-inserted-timestamp)
  11949. (defvar org-log-post-message)
  11950. (defvar org-log-note-purpose)
  11951. (defvar org-log-note-how nil)
  11952. (defvar org-log-note-extra)
  11953. (defun org-auto-repeat-maybe (done-word)
  11954. "Check if the current headline contains a repeated deadline/schedule.
  11955. If yes, set TODO state back to what it was and change the base date
  11956. of repeating deadline/scheduled time stamps to new date.
  11957. This function is run automatically after each state change to a DONE state."
  11958. ;; last-state is dynamically scoped into this function
  11959. (let* ((repeat (org-get-repeat))
  11960. (aa (assoc org-last-state org-todo-kwd-alist))
  11961. (interpret (nth 1 aa))
  11962. (head (nth 2 aa))
  11963. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11964. (msg "Entry repeats: ")
  11965. (org-log-done nil)
  11966. (org-todo-log-states nil)
  11967. re type n what ts time to-state)
  11968. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11969. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11970. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11971. org-todo-repeat-to-state))
  11972. (unless (and to-state (member to-state org-todo-keywords-1))
  11973. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11974. (org-todo to-state)
  11975. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11976. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11977. (org-time-stamp-format t t))))
  11978. (when org-log-repeat
  11979. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11980. (memq 'org-add-log-note post-command-hook))
  11981. ;; OK, we are already setup for some record
  11982. (if (eq org-log-repeat 'note)
  11983. ;; make sure we take a note, not only a time stamp
  11984. (setq org-log-note-how 'note))
  11985. ;; Set up for taking a record
  11986. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11987. org-last-state
  11988. 'findpos org-log-repeat)))
  11989. (org-back-to-heading t)
  11990. (org-add-planning-info nil nil 'closed)
  11991. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11992. org-deadline-time-regexp "\\)\\|\\("
  11993. org-ts-regexp "\\)"))
  11994. (while (re-search-forward
  11995. re (save-excursion (outline-next-heading) (point)) t)
  11996. (setq type (if (match-end 1) org-scheduled-string
  11997. (if (match-end 3) org-deadline-string "Plain:"))
  11998. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11999. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  12000. (setq n (string-to-number (match-string 2 ts))
  12001. what (match-string 3 ts))
  12002. (if (equal what "w") (setq n (* n 7) what "d"))
  12003. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12004. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12005. ;; Preparation, see if we need to modify the start date for the change
  12006. (when (match-end 1)
  12007. (setq time (save-match-data (org-time-string-to-time ts)))
  12008. (cond
  12009. ((equal (match-string 1 ts) ".")
  12010. ;; Shift starting date to today
  12011. (org-timestamp-change
  12012. (- (org-today) (time-to-days time))
  12013. 'day))
  12014. ((equal (match-string 1 ts) "+")
  12015. (let ((nshiftmax 10) (nshift 0))
  12016. (while (or (= nshift 0)
  12017. (<= (time-to-days time)
  12018. (time-to-days (current-time))))
  12019. (when (= (incf nshift) nshiftmax)
  12020. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12021. (user-error "Abort")))
  12022. (org-timestamp-change n (cdr (assoc what whata)))
  12023. (org-at-timestamp-p t)
  12024. (setq ts (match-string 1))
  12025. (setq time (save-match-data (org-time-string-to-time ts)))))
  12026. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12027. ;; rematch, so that we have everything in place for the real shift
  12028. (org-at-timestamp-p t)
  12029. (setq ts (match-string 1))
  12030. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12031. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12032. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12033. (setq org-log-post-message msg)
  12034. (message "%s" msg))))
  12035. (defun org-show-todo-tree (arg)
  12036. "Make a compact tree which shows all headlines marked with TODO.
  12037. The tree will show the lines where the regexp matches, and all higher
  12038. headlines above the match.
  12039. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12040. With a numeric prefix N, construct a sparse tree for the Nth element
  12041. of `org-todo-keywords-1'."
  12042. (interactive "P")
  12043. (let ((case-fold-search nil)
  12044. (kwd-re
  12045. (cond ((null arg) org-not-done-regexp)
  12046. ((equal arg '(4))
  12047. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12048. (mapcar 'list org-todo-keywords-1))))
  12049. (concat "\\("
  12050. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12051. "\\)\\>")))
  12052. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12053. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12054. org-todo-keywords-1)))
  12055. (t (user-error "Invalid prefix argument: %s" arg)))))
  12056. (message "%d TODO entries found"
  12057. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12058. (defun org-deadline (arg &optional time)
  12059. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12060. With one universal prefix argument, remove any deadline from the item.
  12061. With two universal prefix arguments, prompt for a warning delay.
  12062. With argument TIME, set the deadline at the corresponding date. TIME
  12063. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12064. (interactive "P")
  12065. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12066. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12067. 'region-start-level 'region))
  12068. org-loop-over-headlines-in-active-region)
  12069. (org-map-entries
  12070. `(org-deadline ',arg ,time)
  12071. org-loop-over-headlines-in-active-region
  12072. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12073. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12074. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12075. (repeater (and old-date
  12076. (string-match
  12077. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12078. old-date)
  12079. (match-string 1 old-date))))
  12080. (cond
  12081. ((equal arg '(4))
  12082. (when (and old-date org-log-redeadline)
  12083. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12084. org-log-redeadline))
  12085. (org-remove-timestamp-with-keyword org-deadline-string)
  12086. (message "Item no longer has a deadline."))
  12087. ((equal arg '(16))
  12088. (save-excursion
  12089. (org-back-to-heading t)
  12090. (if (re-search-forward
  12091. org-deadline-time-regexp
  12092. (save-excursion (outline-next-heading) (point)) t)
  12093. (let* ((rpl0 (match-string 1))
  12094. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12095. (replace-match
  12096. (concat org-deadline-string
  12097. " <" rpl
  12098. (format " -%dd"
  12099. (abs
  12100. (- (time-to-days
  12101. (save-match-data
  12102. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12103. (time-to-days old-date-time))))
  12104. ">") t t))
  12105. (user-error "No deadline information to update"))))
  12106. (t
  12107. (org-add-planning-info 'deadline time 'closed)
  12108. (when (and old-date org-log-redeadline
  12109. (not (equal old-date
  12110. (substring org-last-inserted-timestamp 1 -1))))
  12111. (org-add-log-setup 'redeadline nil old-date 'findpos
  12112. org-log-redeadline))
  12113. (when repeater
  12114. (save-excursion
  12115. (org-back-to-heading t)
  12116. (when (re-search-forward (concat org-deadline-string " "
  12117. org-last-inserted-timestamp)
  12118. (save-excursion
  12119. (outline-next-heading) (point)) t)
  12120. (goto-char (1- (match-end 0)))
  12121. (insert " " repeater)
  12122. (setq org-last-inserted-timestamp
  12123. (concat (substring org-last-inserted-timestamp 0 -1)
  12124. " " repeater
  12125. (substring org-last-inserted-timestamp -1))))))
  12126. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12127. (defun org-schedule (arg &optional time)
  12128. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12129. With one universal prefix argument, remove any scheduling date from the item.
  12130. With two universal prefix arguments, prompt for a delay cookie.
  12131. With argument TIME, scheduled at the corresponding date. TIME can
  12132. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12133. (interactive "P")
  12134. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12135. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12136. 'region-start-level 'region))
  12137. org-loop-over-headlines-in-active-region)
  12138. (org-map-entries
  12139. `(org-schedule ',arg ,time)
  12140. org-loop-over-headlines-in-active-region
  12141. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12142. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12143. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12144. (repeater (and old-date
  12145. (string-match
  12146. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12147. old-date)
  12148. (match-string 1 old-date))))
  12149. (cond
  12150. ((equal arg '(4))
  12151. (progn
  12152. (when (and old-date org-log-reschedule)
  12153. (org-add-log-setup 'delschedule nil old-date 'findpos
  12154. org-log-reschedule))
  12155. (org-remove-timestamp-with-keyword org-scheduled-string)
  12156. (message "Item is no longer scheduled.")))
  12157. ((equal arg '(16))
  12158. (save-excursion
  12159. (org-back-to-heading t)
  12160. (if (re-search-forward
  12161. org-scheduled-time-regexp
  12162. (save-excursion (outline-next-heading) (point)) t)
  12163. (let* ((rpl0 (match-string 1))
  12164. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12165. (replace-match
  12166. (concat org-scheduled-string
  12167. " <" rpl
  12168. (format " -%dd"
  12169. (abs
  12170. (- (time-to-days
  12171. (save-match-data
  12172. (org-read-date nil t nil "Delay until" old-date-time)))
  12173. (time-to-days old-date-time))))
  12174. ">") t t))
  12175. (user-error "No scheduled information to update"))))
  12176. (t
  12177. (org-add-planning-info 'scheduled time 'closed)
  12178. (when (and old-date org-log-reschedule
  12179. (not (equal old-date
  12180. (substring org-last-inserted-timestamp 1 -1))))
  12181. (org-add-log-setup 'reschedule nil old-date 'findpos
  12182. org-log-reschedule))
  12183. (when repeater
  12184. (save-excursion
  12185. (org-back-to-heading t)
  12186. (when (re-search-forward (concat org-scheduled-string " "
  12187. org-last-inserted-timestamp)
  12188. (save-excursion
  12189. (outline-next-heading) (point)) t)
  12190. (goto-char (1- (match-end 0)))
  12191. (insert " " repeater)
  12192. (setq org-last-inserted-timestamp
  12193. (concat (substring org-last-inserted-timestamp 0 -1)
  12194. " " repeater
  12195. (substring org-last-inserted-timestamp -1))))))
  12196. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12197. (defun org-get-scheduled-time (pom &optional inherit)
  12198. "Get the scheduled time as a time tuple, of a format suitable
  12199. for calling org-schedule with, or if there is no scheduling,
  12200. returns nil."
  12201. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12202. (when time
  12203. (apply 'encode-time (org-parse-time-string time)))))
  12204. (defun org-get-deadline-time (pom &optional inherit)
  12205. "Get the deadline as a time tuple, of a format suitable for
  12206. calling org-deadline with, or if there is no scheduling, returns
  12207. nil."
  12208. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12209. (when time
  12210. (apply 'encode-time (org-parse-time-string time)))))
  12211. (defun org-remove-timestamp-with-keyword (keyword)
  12212. "Remove all time stamps with KEYWORD in the current entry."
  12213. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12214. beg)
  12215. (save-excursion
  12216. (org-back-to-heading t)
  12217. (setq beg (point))
  12218. (outline-next-heading)
  12219. (while (re-search-backward re beg t)
  12220. (replace-match "")
  12221. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12222. (equal (char-before) ?\ ))
  12223. (backward-delete-char 1)
  12224. (if (string-match "^[ \t]*$" (buffer-substring
  12225. (point-at-bol) (point-at-eol)))
  12226. (delete-region (point-at-bol)
  12227. (min (point-max) (1+ (point-at-eol))))))))))
  12228. (defvar org-time-was-given) ; dynamically scoped parameter
  12229. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12230. (defun org-at-planning-p ()
  12231. "Non-nil when point is on a planning info line."
  12232. ;; This is as accurate and faster than `org-element-at-point' since
  12233. ;; planning info location is fixed in the section.
  12234. (org-with-wide-buffer
  12235. (beginning-of-line)
  12236. (and (org-looking-at-p org-planning-line-re)
  12237. (eq (point)
  12238. (ignore-errors
  12239. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12240. (org-back-to-heading t)
  12241. (org-with-limited-levels (org-back-to-heading t)))
  12242. (line-beginning-position 2))))))
  12243. (defun org-add-planning-info (what &optional time &rest remove)
  12244. "Insert new timestamp with keyword in the planning line.
  12245. WHAT indicates what kind of time stamp to add. It is a symbol
  12246. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12247. the time to use. If none is given, the user is prompted for
  12248. a date. REMOVE indicates what kind of entries to remove. An old
  12249. WHAT entry will also be removed."
  12250. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12251. (catch 'exit
  12252. (when (and (memq what '(scheduled deadline))
  12253. (or (not time)
  12254. (and (stringp time)
  12255. (string-match "^[-+]+[0-9]" time))))
  12256. ;; Try to get a default date/time from existing timestamp
  12257. (save-excursion
  12258. (org-back-to-heading t)
  12259. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12260. (when (re-search-forward (if (eq what 'scheduled)
  12261. org-scheduled-time-regexp
  12262. org-deadline-time-regexp)
  12263. end t)
  12264. (setq ts (match-string 1)
  12265. default-time (apply 'encode-time (org-parse-time-string ts))
  12266. default-input (and ts (org-get-compact-tod ts)))))))
  12267. (when what
  12268. (setq time
  12269. (if (stringp time)
  12270. ;; This is a string (relative or absolute), set
  12271. ;; proper date.
  12272. (apply #'encode-time
  12273. (org-read-date-analyze
  12274. time default-time (decode-time default-time)))
  12275. ;; If necessary, get the time from the user
  12276. (or time (org-read-date nil 'to-time nil nil
  12277. default-time default-input)))))
  12278. (org-with-wide-buffer
  12279. (org-back-to-heading t)
  12280. (forward-line)
  12281. (unless (bolp) (insert "\n"))
  12282. (cond ((org-looking-at-p org-planning-line-re)
  12283. ;; Move to current indentation.
  12284. (skip-chars-forward " \t")
  12285. ;; Check if we have to remove something.
  12286. (dolist (type (if what (cons what remove) remove))
  12287. (save-excursion
  12288. (when (re-search-forward
  12289. (concat
  12290. " *"
  12291. (case type
  12292. (closed org-closed-time-regexp)
  12293. (deadline org-deadline-time-regexp)
  12294. (scheduled org-scheduled-time-regexp)
  12295. (otherwise
  12296. (error "Invalid planning type: %s" type))))
  12297. (line-end-position) t)
  12298. (replace-match "")
  12299. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12300. (when (and (not what) (eq type 'closed))
  12301. (save-excursion
  12302. (beginning-of-line)
  12303. (when (looking-at "[ \t]*$")
  12304. (delete-region (point)
  12305. (line-beginning-position 2)))))))
  12306. ;; Remove leading white spaces.
  12307. (when (looking-at "[ \t]+") (replace-match ""))))
  12308. ((not what) (throw 'exit nil)) ; Nothing to do.
  12309. (t (insert-before-markers "\n")
  12310. (backward-char 1)
  12311. (when org-adapt-indentation
  12312. (org-indent-to-column (1+ (org-outline-level))))))
  12313. (when what
  12314. ;; Insert planning keyword.
  12315. (insert (case what
  12316. (closed org-closed-string)
  12317. (deadline org-deadline-string)
  12318. (scheduled org-scheduled-string)
  12319. (otherwise (error "Invalid planning type: %s" what)))
  12320. " ")
  12321. ;; Insert associated timestamp.
  12322. (let ((ts (org-insert-time-stamp
  12323. time
  12324. (or org-time-was-given
  12325. (and (eq what 'closed) org-log-done-with-time))
  12326. (eq what 'closed)
  12327. nil nil (list org-end-time-was-given))))
  12328. (unless (eolp) (insert " "))
  12329. ts))))))
  12330. (defvar org-log-note-marker (make-marker))
  12331. (defvar org-log-note-purpose nil)
  12332. (defvar org-log-note-state nil)
  12333. (defvar org-log-note-previous-state nil)
  12334. (defvar org-log-note-extra nil)
  12335. (defvar org-log-note-window-configuration nil)
  12336. (defvar org-log-note-return-to (make-marker))
  12337. (defvar org-log-note-effective-time nil
  12338. "Remembered current time so that dynamically scoped
  12339. `org-extend-today-until' affects tha timestamps in state change
  12340. log")
  12341. (defvar org-log-post-message nil
  12342. "Message to be displayed after a log note has been stored.
  12343. The auto-repeater uses this.")
  12344. (defun org-add-note ()
  12345. "Add a note to the current entry.
  12346. This is done in the same way as adding a state change note."
  12347. (interactive)
  12348. (org-add-log-setup 'note nil nil 'findpos nil))
  12349. (defun org-log-beginning (&optional create)
  12350. "Return expected start of log notes in current entry.
  12351. When optional argument CREATE is non-nil, the function creates
  12352. a drawer to store notes, if necessary. Returned position ignores
  12353. narrowing."
  12354. (org-with-wide-buffer
  12355. (org-end-of-meta-data)
  12356. (let ((end (if (org-at-heading-p) (point)
  12357. (save-excursion (outline-next-heading) (point))))
  12358. (drawer (org-log-into-drawer)))
  12359. (cond
  12360. (drawer
  12361. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12362. (case-fold-search t))
  12363. (catch 'exit
  12364. ;; Try to find existing drawer.
  12365. (while (re-search-forward regexp end t)
  12366. (let ((element (org-element-at-point)))
  12367. (when (eq (org-element-type element) 'drawer)
  12368. (let ((cend (org-element-property :contents-end element)))
  12369. (when (and (not org-log-states-order-reversed) cend)
  12370. (goto-char cend)))
  12371. (throw 'exit nil))))
  12372. ;; No drawer found. Create one, if permitted.
  12373. (when create
  12374. (unless (bolp) (insert "\n"))
  12375. (let ((beg (point)))
  12376. (insert ":" drawer ":\n:END:\n")
  12377. (org-indent-region beg (point)))
  12378. (end-of-line -1)))))
  12379. (org-log-state-notes-insert-after-drawers
  12380. (while (and (looking-at org-drawer-regexp)
  12381. (progn (goto-char (match-end 0))
  12382. (re-search-forward org-property-end-re end t)))
  12383. (forward-line)))))
  12384. (if (bolp) (point) (line-beginning-position 2))))
  12385. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12386. "Set up the post command hook to take a note.
  12387. If this is about to TODO state change, the new state is expected in STATE.
  12388. When FINDPOS is non-nil, find the correct position for the note in
  12389. the current entry. If not, assume that it can be inserted at point.
  12390. HOW is an indicator what kind of note should be created.
  12391. EXTRA is additional text that will be inserted into the notes buffer."
  12392. (org-with-wide-buffer
  12393. (when findpos
  12394. (goto-char (org-log-beginning t))
  12395. (unless org-log-states-order-reversed
  12396. (org-skip-over-state-notes)
  12397. (skip-chars-backward " \t\n\r")
  12398. (forward-line)))
  12399. (move-marker org-log-note-marker (point))
  12400. ;; Preserve position even if a property drawer is inserted in the
  12401. ;; process.
  12402. (set-marker-insertion-type org-log-note-marker t)
  12403. (setq org-log-note-purpose purpose
  12404. org-log-note-state state
  12405. org-log-note-previous-state prev-state
  12406. org-log-note-how how
  12407. org-log-note-extra extra
  12408. org-log-note-effective-time (org-current-effective-time))
  12409. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12410. (defun org-skip-over-state-notes ()
  12411. "Skip past the list of State notes in an entry."
  12412. (when (ignore-errors (goto-char (org-in-item-p)))
  12413. (let* ((struct (org-list-struct))
  12414. (prevs (org-list-prevs-alist struct))
  12415. (regexp
  12416. (concat "[ \t]*- +"
  12417. (replace-regexp-in-string
  12418. " +" " +"
  12419. (org-replace-escapes
  12420. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12421. `(("%d" . ,org-ts-regexp-inactive)
  12422. ("%D" . ,org-ts-regexp)
  12423. ("%s" . "\"\\S-+\"")
  12424. ("%S" . "\"\\S-+\"")
  12425. ("%t" . ,org-ts-regexp-inactive)
  12426. ("%T" . ,org-ts-regexp)
  12427. ("%u" . ".*?")
  12428. ("%U" . ".*?")))))))
  12429. (while (org-looking-at-p regexp)
  12430. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12431. (org-list-get-item-end (point) struct)))))))
  12432. (defun org-add-log-note (&optional purpose)
  12433. "Pop up a window for taking a note, and add this note later at point."
  12434. (remove-hook 'post-command-hook 'org-add-log-note)
  12435. (setq org-log-note-window-configuration (current-window-configuration))
  12436. (delete-other-windows)
  12437. (move-marker org-log-note-return-to (point))
  12438. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12439. (goto-char org-log-note-marker)
  12440. (org-switch-to-buffer-other-window "*Org Note*")
  12441. (erase-buffer)
  12442. (if (memq org-log-note-how '(time state))
  12443. (let (current-prefix-arg) (org-store-log-note))
  12444. (let ((org-inhibit-startup t)) (org-mode))
  12445. (insert (format "# Insert note for %s.
  12446. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12447. (cond
  12448. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12449. ((eq org-log-note-purpose 'done) "closed todo item")
  12450. ((eq org-log-note-purpose 'state)
  12451. (format "state change from \"%s\" to \"%s\""
  12452. (or org-log-note-previous-state "")
  12453. (or org-log-note-state "")))
  12454. ((eq org-log-note-purpose 'reschedule)
  12455. "rescheduling")
  12456. ((eq org-log-note-purpose 'delschedule)
  12457. "no longer scheduled")
  12458. ((eq org-log-note-purpose 'redeadline)
  12459. "changing deadline")
  12460. ((eq org-log-note-purpose 'deldeadline)
  12461. "removing deadline")
  12462. ((eq org-log-note-purpose 'refile)
  12463. "refiling")
  12464. ((eq org-log-note-purpose 'note)
  12465. "this entry")
  12466. (t (error "This should not happen")))))
  12467. (if org-log-note-extra (insert org-log-note-extra))
  12468. (org-set-local 'org-finish-function 'org-store-log-note)
  12469. (run-hooks 'org-log-buffer-setup-hook)))
  12470. (defvar org-note-abort nil) ; dynamically scoped
  12471. (defun org-store-log-note ()
  12472. "Finish taking a log note, and insert it to where it belongs."
  12473. (let ((txt (buffer-string)))
  12474. (kill-buffer (current-buffer))
  12475. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12476. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12477. (setq txt (replace-match "" t t txt)))
  12478. (if (string-match "\\s-+\\'" txt)
  12479. (setq txt (replace-match "" t t txt)))
  12480. (setq lines (org-split-string txt "\n"))
  12481. (when (and note (string-match "\\S-" note))
  12482. (setq note
  12483. (org-replace-escapes
  12484. note
  12485. (list (cons "%u" (user-login-name))
  12486. (cons "%U" user-full-name)
  12487. (cons "%t" (format-time-string
  12488. (org-time-stamp-format 'long 'inactive)
  12489. org-log-note-effective-time))
  12490. (cons "%T" (format-time-string
  12491. (org-time-stamp-format 'long nil)
  12492. org-log-note-effective-time))
  12493. (cons "%d" (format-time-string
  12494. (org-time-stamp-format nil 'inactive)
  12495. org-log-note-effective-time))
  12496. (cons "%D" (format-time-string
  12497. (org-time-stamp-format nil nil)
  12498. org-log-note-effective-time))
  12499. (cons "%s" (cond
  12500. ((not org-log-note-state) "")
  12501. ((org-string-match-p org-ts-regexp
  12502. org-log-note-state)
  12503. (format "\"[%s]\""
  12504. (substring org-log-note-state 1 -1)))
  12505. (t (format "\"%s\"" org-log-note-state))))
  12506. (cons "%S"
  12507. (cond
  12508. ((not org-log-note-previous-state) "")
  12509. ((org-string-match-p org-ts-regexp
  12510. org-log-note-previous-state)
  12511. (format "\"[%s]\""
  12512. (substring
  12513. org-log-note-previous-state 1 -1)))
  12514. (t (format "\"%s\""
  12515. org-log-note-previous-state)))))))
  12516. (when lines (setq note (concat note " \\\\")))
  12517. (push note lines))
  12518. (when (or current-prefix-arg org-note-abort)
  12519. (when (org-log-into-drawer)
  12520. (org-remove-empty-drawer-at org-log-note-marker))
  12521. (setq lines nil))
  12522. (when lines
  12523. (with-current-buffer (marker-buffer org-log-note-marker)
  12524. (org-with-wide-buffer
  12525. (goto-char org-log-note-marker)
  12526. (move-marker org-log-note-marker nil)
  12527. ;; Make sure point is at the beginning of an empty line.
  12528. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12529. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12530. ;; In an existing list, add a new item at the top level.
  12531. ;; Otherwise, indent line like a regular one.
  12532. (let ((itemp (org-in-item-p)))
  12533. (if itemp
  12534. (org-indent-line-to
  12535. (let ((struct (save-excursion
  12536. (goto-char itemp) (org-list-struct))))
  12537. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12538. (org-indent-line)))
  12539. (insert (org-list-bullet-string "-") (pop lines))
  12540. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12541. (dolist (line lines)
  12542. (insert "\n")
  12543. (org-indent-line-to ind)
  12544. (insert line)))
  12545. (message "Note stored")
  12546. (org-back-to-heading t)
  12547. (org-cycle-hide-drawers 'children))
  12548. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12549. ;; from within `org-add-log-note' because `buffer-undo-list'
  12550. ;; is then modified outside of `org-with-remote-undo'.
  12551. (when (eq this-command 'org-agenda-todo)
  12552. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12553. ;; Don't add undo information when called from `org-agenda-todo'
  12554. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12555. (set-window-configuration org-log-note-window-configuration)
  12556. (with-current-buffer (marker-buffer org-log-note-return-to)
  12557. (goto-char org-log-note-return-to))
  12558. (move-marker org-log-note-return-to nil)
  12559. (and org-log-post-message (message "%s" org-log-post-message))))
  12560. (defun org-remove-empty-drawer-at (pos)
  12561. "Remove an empty drawer at position POS.
  12562. POS may also be a marker."
  12563. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12564. (org-with-wide-buffer
  12565. (goto-char pos)
  12566. (let ((drawer (org-element-at-point)))
  12567. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12568. (not (org-element-property :contents-begin drawer)))
  12569. (delete-region (org-element-property :begin drawer)
  12570. (progn (goto-char (org-element-property :end drawer))
  12571. (skip-chars-backward " \r\t\n")
  12572. (forward-line)
  12573. (point))))))))
  12574. (defvar org-ts-type nil)
  12575. (defun org-sparse-tree (&optional arg type)
  12576. "Create a sparse tree, prompt for the details.
  12577. This command can create sparse trees. You first need to select the type
  12578. of match used to create the tree:
  12579. t Show all TODO entries.
  12580. T Show entries with a specific TODO keyword.
  12581. m Show entries selected by a tags/property match.
  12582. p Enter a property name and its value (both with completion on existing
  12583. names/values) and show entries with that property.
  12584. r Show entries matching a regular expression (`/' can be used as well).
  12585. b Show deadlines and scheduled items before a date.
  12586. a Show deadlines and scheduled items after a date.
  12587. d Show deadlines due within `org-deadline-warning-days'.
  12588. D Show deadlines and scheduled items between a date range."
  12589. (interactive "P")
  12590. (setq type (or type org-sparse-tree-default-date-type))
  12591. (setq org-ts-type type)
  12592. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12593. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12594. [c]ycle through date types: %s"
  12595. (case type
  12596. (all "all timestamps")
  12597. (scheduled "only scheduled")
  12598. (deadline "only deadline")
  12599. (active "only active timestamps")
  12600. (inactive "only inactive timestamps")
  12601. (closed "with a closed time-stamp")
  12602. (otherwise "scheduled/deadline")))
  12603. (let ((answer (read-char-exclusive)))
  12604. (case answer
  12605. (?c
  12606. (org-sparse-tree
  12607. arg
  12608. (cadr
  12609. (memq type '(nil all scheduled deadline active inactive closed)))))
  12610. (?d (call-interactively 'org-check-deadlines))
  12611. (?b (call-interactively 'org-check-before-date))
  12612. (?a (call-interactively 'org-check-after-date))
  12613. (?D (call-interactively 'org-check-dates-range))
  12614. (?t (call-interactively 'org-show-todo-tree))
  12615. (?T (org-show-todo-tree '(4)))
  12616. (?m (call-interactively 'org-match-sparse-tree))
  12617. ((?p ?P)
  12618. (let* ((kwd (org-icompleting-read
  12619. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12620. (value (org-icompleting-read
  12621. "Value: " (mapcar 'list (org-property-values kwd)))))
  12622. (unless (string-match "\\`{.*}\\'" value)
  12623. (setq value (concat "\"" value "\"")))
  12624. (org-match-sparse-tree arg (concat kwd "=" value))))
  12625. ((?r ?R ?/) (call-interactively 'org-occur))
  12626. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12627. (defvar org-occur-highlights nil
  12628. "List of overlays used for occur matches.")
  12629. (make-variable-buffer-local 'org-occur-highlights)
  12630. (defvar org-occur-parameters nil
  12631. "Parameters of the active org-occur calls.
  12632. This is a list, each call to org-occur pushes as cons cell,
  12633. containing the regular expression and the callback, onto the list.
  12634. The list can contain several entries if `org-occur' has been called
  12635. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12636. will only contain one set of parameters. When the highlights are
  12637. removed (for example with `C-c C-c', or with the next edit (depending
  12638. on `org-remove-highlights-with-change'), this variable is emptied
  12639. as well.")
  12640. (make-variable-buffer-local 'org-occur-parameters)
  12641. (defun org-occur (regexp &optional keep-previous callback)
  12642. "Make a compact tree which shows all matches of REGEXP.
  12643. The tree will show the lines where the regexp matches, and all higher
  12644. headlines above the match. It will also show the heading after the match,
  12645. to make sure editing the matching entry is easy.
  12646. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12647. call to `org-occur' will be kept, to allow stacking of calls to this
  12648. command.
  12649. If CALLBACK is non-nil, it is a function which is called to confirm
  12650. that the match should indeed be shown."
  12651. (interactive "sRegexp: \nP")
  12652. (when (equal regexp "")
  12653. (user-error "Regexp cannot be empty"))
  12654. (unless keep-previous
  12655. (org-remove-occur-highlights nil nil t))
  12656. (push (cons regexp callback) org-occur-parameters)
  12657. (let ((cnt 0))
  12658. (save-excursion
  12659. (goto-char (point-min))
  12660. (if (or (not keep-previous) ; do not want to keep
  12661. (not org-occur-highlights)) ; no previous matches
  12662. ;; hide everything
  12663. (org-overview))
  12664. (while (re-search-forward regexp nil t)
  12665. (when (or (not callback)
  12666. (save-match-data (funcall callback)))
  12667. (setq cnt (1+ cnt))
  12668. (when org-highlight-sparse-tree-matches
  12669. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12670. (org-show-context 'occur-tree))))
  12671. (when org-remove-highlights-with-change
  12672. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12673. nil 'local))
  12674. (unless org-sparse-tree-open-archived-trees
  12675. (org-hide-archived-subtrees (point-min) (point-max)))
  12676. (run-hooks 'org-occur-hook)
  12677. (if (org-called-interactively-p 'interactive)
  12678. (message "%d match(es) for regexp %s" cnt regexp))
  12679. cnt))
  12680. (defun org-occur-next-match (&optional n reset)
  12681. "Function for `next-error-function' to find sparse tree matches.
  12682. N is the number of matches to move, when negative move backwards.
  12683. RESET is entirely ignored - this function always goes back to the
  12684. starting point when no match is found."
  12685. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12686. (search-func (if (< n 0)
  12687. 'previous-single-char-property-change
  12688. 'next-single-char-property-change))
  12689. (n (abs n))
  12690. (pos (point))
  12691. p1)
  12692. (catch 'exit
  12693. (while (setq p1 (funcall search-func (point) 'org-type))
  12694. (when (equal p1 limit)
  12695. (goto-char pos)
  12696. (user-error "No more matches"))
  12697. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12698. (setq n (1- n))
  12699. (when (= n 0)
  12700. (goto-char p1)
  12701. (throw 'exit (point))))
  12702. (goto-char p1))
  12703. (goto-char p1)
  12704. (user-error "No more matches"))))
  12705. (defun org-show-context (&optional key)
  12706. "Make sure point and context are visible.
  12707. Optional argument KEY, when non-nil, is a symbol. See
  12708. `org-show-context-detail' for allowed values and how much is to
  12709. be shown."
  12710. (org-show-set-visibility
  12711. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12712. ((cdr (assq key org-show-context-detail)))
  12713. (t (cdr (assq 'default org-show-context-detail))))))
  12714. (defun org-show-set-visibility (detail)
  12715. "Set visibility around point according to DETAIL.
  12716. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12717. `tree', `canonical' or t. See `org-show-context-detail' for more
  12718. information."
  12719. (unless (org-before-first-heading-p)
  12720. ;; Show current heading and possibly its entry, following headline
  12721. ;; or all children.
  12722. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12723. (org-flag-heading nil)
  12724. (org-show-entry)
  12725. (org-with-limited-levels
  12726. (case detail
  12727. ((tree canonical t) (show-children))
  12728. ((nil minimal ancestors))
  12729. (t (save-excursion
  12730. (outline-next-heading)
  12731. (org-flag-heading nil))))))
  12732. ;; Show all siblings.
  12733. (when (eq detail 'lineage) (org-show-siblings))
  12734. ;; Show ancestors, possibly with their children.
  12735. (when (memq detail '(ancestors lineage tree canonical t))
  12736. (save-excursion
  12737. (while (org-up-heading-safe)
  12738. (org-flag-heading nil)
  12739. (when (memq detail '(canonical t)) (org-show-entry))
  12740. (when (memq detail '(tree canonical t)) (show-children)))))))
  12741. (defvar org-reveal-start-hook nil
  12742. "Hook run before revealing a location.")
  12743. (defun org-reveal (&optional siblings)
  12744. "Show current entry, hierarchy above it, and the following headline.
  12745. This can be used to show a consistent set of context around
  12746. locations exposed with `org-show-context'.
  12747. With optional argument SIBLINGS, on each level of the hierarchy all
  12748. siblings are shown. This repairs the tree structure to what it would
  12749. look like when opened with hierarchical calls to `org-cycle'.
  12750. With double optional argument \\[universal-argument] \\[universal-argument], \
  12751. go to the parent and show the
  12752. entire tree."
  12753. (interactive "P")
  12754. (run-hooks 'org-reveal-start-hook)
  12755. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12756. ((equal siblings '(16))
  12757. (save-excursion
  12758. (when (org-up-heading-safe)
  12759. (org-show-subtree)
  12760. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12761. (t (org-show-set-visibility 'lineage))))
  12762. (defun org-highlight-new-match (beg end)
  12763. "Highlight from BEG to END and mark the highlight is an occur headline."
  12764. (let ((ov (make-overlay beg end)))
  12765. (overlay-put ov 'face 'secondary-selection)
  12766. (overlay-put ov 'org-type 'org-occur)
  12767. (push ov org-occur-highlights)))
  12768. (defun org-remove-occur-highlights (&optional beg end noremove)
  12769. "Remove the occur highlights from the buffer.
  12770. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12771. from the `before-change-functions' in the current buffer."
  12772. (interactive)
  12773. (unless org-inhibit-highlight-removal
  12774. (mapc 'delete-overlay org-occur-highlights)
  12775. (setq org-occur-highlights nil)
  12776. (setq org-occur-parameters nil)
  12777. (unless noremove
  12778. (remove-hook 'before-change-functions
  12779. 'org-remove-occur-highlights 'local))))
  12780. ;;;; Priorities
  12781. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12782. "Regular expression matching the priority indicator.")
  12783. (defvar org-remove-priority-next-time nil)
  12784. (defun org-priority-up ()
  12785. "Increase the priority of the current item."
  12786. (interactive)
  12787. (org-priority 'up))
  12788. (defun org-priority-down ()
  12789. "Decrease the priority of the current item."
  12790. (interactive)
  12791. (org-priority 'down))
  12792. (defun org-priority (&optional action show)
  12793. "Change the priority of an item.
  12794. ACTION can be `set', `up', `down', or a character."
  12795. (interactive "P")
  12796. (if (equal action '(4))
  12797. (org-show-priority)
  12798. (unless org-enable-priority-commands
  12799. (user-error "Priority commands are disabled"))
  12800. (setq action (or action 'set))
  12801. (let (current new news have remove)
  12802. (save-excursion
  12803. (org-back-to-heading t)
  12804. (if (looking-at org-priority-regexp)
  12805. (setq current (string-to-char (match-string 2))
  12806. have t))
  12807. (cond
  12808. ((eq action 'remove)
  12809. (setq remove t new ?\ ))
  12810. ((or (eq action 'set)
  12811. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12812. (if (not (eq action 'set))
  12813. (setq new action)
  12814. (message "Priority %c-%c, SPC to remove: "
  12815. org-highest-priority org-lowest-priority)
  12816. (save-match-data
  12817. (setq new (read-char-exclusive))))
  12818. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12819. (= (upcase org-lowest-priority) org-lowest-priority))
  12820. (setq new (upcase new)))
  12821. (cond ((equal new ?\ ) (setq remove t))
  12822. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12823. (user-error "Priority must be between `%c' and `%c'"
  12824. org-highest-priority org-lowest-priority))))
  12825. ((eq action 'up)
  12826. (setq new (if have
  12827. (1- current) ; normal cycling
  12828. ;; last priority was empty
  12829. (if (eq last-command this-command)
  12830. org-lowest-priority ; wrap around empty to lowest
  12831. ;; default
  12832. (if org-priority-start-cycle-with-default
  12833. org-default-priority
  12834. (1- org-default-priority))))))
  12835. ((eq action 'down)
  12836. (setq new (if have
  12837. (1+ current) ; normal cycling
  12838. ;; last priority was empty
  12839. (if (eq last-command this-command)
  12840. org-highest-priority ; wrap around empty to highest
  12841. ;; default
  12842. (if org-priority-start-cycle-with-default
  12843. org-default-priority
  12844. (1+ org-default-priority))))))
  12845. (t (user-error "Invalid action")))
  12846. (if (or (< (upcase new) org-highest-priority)
  12847. (> (upcase new) org-lowest-priority))
  12848. (if (and (memq action '(up down))
  12849. (not have) (not (eq last-command this-command)))
  12850. ;; `new' is from default priority
  12851. (error
  12852. "The default can not be set, see `org-default-priority' why")
  12853. ;; normal cycling: `new' is beyond highest/lowest priority
  12854. ;; and is wrapped around to the empty priority
  12855. (setq remove t)))
  12856. (setq news (format "%c" new))
  12857. (if have
  12858. (if remove
  12859. (replace-match "" t t nil 1)
  12860. (replace-match news t t nil 2))
  12861. (if remove
  12862. (user-error "No priority cookie found in line")
  12863. (let ((case-fold-search nil))
  12864. (looking-at org-todo-line-regexp))
  12865. (if (match-end 2)
  12866. (progn
  12867. (goto-char (match-end 2))
  12868. (insert " [#" news "]"))
  12869. (goto-char (match-beginning 3))
  12870. (insert "[#" news "] "))))
  12871. (org-set-tags nil 'align))
  12872. (if remove
  12873. (message "Priority removed")
  12874. (message "Priority of current item set to %s" news)))))
  12875. (defun org-show-priority ()
  12876. "Show the priority of the current item.
  12877. This priority is composed of the main priority given with the [#A] cookies,
  12878. and by additional input from the age of a schedules or deadline entry."
  12879. (interactive)
  12880. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12881. (org-get-at-bol 'priority)
  12882. (save-excursion
  12883. (save-match-data
  12884. (beginning-of-line)
  12885. (and (looking-at org-heading-regexp)
  12886. (org-get-priority (match-string 0))))))))
  12887. (message "Priority is %d" (if pri pri -1000))))
  12888. (defun org-get-priority (s)
  12889. "Find priority cookie and return priority."
  12890. (save-match-data
  12891. (if (functionp org-get-priority-function)
  12892. (funcall org-get-priority-function)
  12893. (if (not (string-match org-priority-regexp s))
  12894. (* 1000 (- org-lowest-priority org-default-priority))
  12895. (* 1000 (- org-lowest-priority
  12896. (string-to-char (match-string 2 s))))))))
  12897. ;;;; Tags
  12898. (defvar org-agenda-archives-mode)
  12899. (defvar org-map-continue-from nil
  12900. "Position from where mapping should continue.
  12901. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12902. (defvar org-scanner-tags nil
  12903. "The current tag list while the tags scanner is running.")
  12904. (defvar org-trust-scanner-tags nil
  12905. "Should `org-get-tags-at' use the tags for the scanner.
  12906. This is for internal dynamical scoping only.
  12907. When this is non-nil, the function `org-get-tags-at' will return the value
  12908. of `org-scanner-tags' instead of building the list by itself. This
  12909. can lead to large speed-ups when the tags scanner is used in a file with
  12910. many entries, and when the list of tags is retrieved, for example to
  12911. obtain a list of properties. Building the tags list for each entry in such
  12912. a file becomes an N^2 operation - but with this variable set, it scales
  12913. as N.")
  12914. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12915. "Scan headline tags with inheritance and produce output ACTION.
  12916. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12917. or `agenda' to produce an entry list for an agenda view. It can also be
  12918. a Lisp form or a function that should be called at each matched headline, in
  12919. this case the return value is a list of all return values from these calls.
  12920. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12921. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12922. only lines with a not-done TODO keyword are included in the output.
  12923. This should be the same variable that was scoped into
  12924. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12925. START-LEVEL can be a string with asterisks, reducing the scope to
  12926. headlines matching this string."
  12927. (require 'org-agenda)
  12928. (let* ((re (concat "^"
  12929. (if start-level
  12930. ;; Get the correct level to match
  12931. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12932. org-outline-regexp)
  12933. " *\\(\\<\\("
  12934. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12935. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12936. (props (list 'face 'default
  12937. 'done-face 'org-agenda-done
  12938. 'undone-face 'default
  12939. 'mouse-face 'highlight
  12940. 'org-not-done-regexp org-not-done-regexp
  12941. 'org-todo-regexp org-todo-regexp
  12942. 'org-complex-heading-regexp org-complex-heading-regexp
  12943. 'help-echo
  12944. (format "mouse-2 or RET jump to org file %s"
  12945. (abbreviate-file-name
  12946. (or (buffer-file-name (buffer-base-buffer))
  12947. (buffer-name (buffer-base-buffer)))))))
  12948. (org-map-continue-from nil)
  12949. lspos tags tags-list
  12950. (tags-alist (list (cons 0 org-file-tags)))
  12951. (llast 0) rtn rtn1 level category i txt
  12952. todo marker entry priority
  12953. ts-date ts-date-type ts-date-pair)
  12954. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12955. (setq action (list 'lambda nil action)))
  12956. (save-excursion
  12957. (goto-char (point-min))
  12958. (when (eq action 'sparse-tree)
  12959. (org-overview)
  12960. (org-remove-occur-highlights))
  12961. (while (let (case-fold-search)
  12962. (re-search-forward re nil t))
  12963. (setq org-map-continue-from nil)
  12964. (catch :skip
  12965. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12966. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12967. (goto-char (setq lspos (match-beginning 0)))
  12968. (setq level (org-reduced-level (org-outline-level))
  12969. category (org-get-category))
  12970. (when (eq action 'agenda)
  12971. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12972. ts-date (car ts-date-pair)
  12973. ts-date-type (cdr ts-date-pair)))
  12974. (setq i llast llast level)
  12975. ;; remove tag lists from same and sublevels
  12976. (while (>= i level)
  12977. (when (setq entry (assoc i tags-alist))
  12978. (setq tags-alist (delete entry tags-alist)))
  12979. (setq i (1- i)))
  12980. ;; add the next tags
  12981. (when tags
  12982. (setq tags (org-split-string tags ":")
  12983. tags-alist
  12984. (cons (cons level tags) tags-alist)))
  12985. ;; compile tags for current headline
  12986. (setq tags-list
  12987. (if org-use-tag-inheritance
  12988. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12989. tags)
  12990. org-scanner-tags tags-list)
  12991. (when org-use-tag-inheritance
  12992. (setcdr (car tags-alist)
  12993. (mapcar (lambda (x)
  12994. (setq x (copy-sequence x))
  12995. (org-add-prop-inherited x))
  12996. (cdar tags-alist))))
  12997. (when (and tags org-use-tag-inheritance
  12998. (or (not (eq t org-use-tag-inheritance))
  12999. org-tags-exclude-from-inheritance))
  13000. ;; selective inheritance, remove uninherited ones
  13001. (setcdr (car tags-alist)
  13002. (org-remove-uninherited-tags (cdar tags-alist))))
  13003. (when (and
  13004. ;; eval matcher only when the todo condition is OK
  13005. (and (or (not todo-only) (member todo org-not-done-keywords))
  13006. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13007. (eval matcher)))
  13008. ;; Call the skipper, but return t if it does not skip,
  13009. ;; so that the `and' form continues evaluating
  13010. (progn
  13011. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13012. t)
  13013. ;; Check if timestamps are deselecting this entry
  13014. (or (not todo-only)
  13015. (and (member todo org-not-done-keywords)
  13016. (or (not org-agenda-tags-todo-honor-ignore-options)
  13017. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13018. ;; select this headline
  13019. (cond
  13020. ((eq action 'sparse-tree)
  13021. (and org-highlight-sparse-tree-matches
  13022. (org-get-heading) (match-end 0)
  13023. (org-highlight-new-match
  13024. (match-beginning 1) (match-end 1)))
  13025. (org-show-context 'tags-tree))
  13026. ((eq action 'agenda)
  13027. (setq txt (org-agenda-format-item
  13028. ""
  13029. (concat
  13030. (if (eq org-tags-match-list-sublevels 'indented)
  13031. (make-string (1- level) ?.) "")
  13032. (org-get-heading))
  13033. (make-string level ?\s)
  13034. category
  13035. tags-list)
  13036. priority (org-get-priority txt))
  13037. (goto-char lspos)
  13038. (setq marker (org-agenda-new-marker))
  13039. (org-add-props txt props
  13040. 'org-marker marker 'org-hd-marker marker 'org-category category
  13041. 'todo-state todo
  13042. 'ts-date ts-date
  13043. 'priority priority
  13044. 'type (concat "tagsmatch" ts-date-type))
  13045. (push txt rtn))
  13046. ((functionp action)
  13047. (setq org-map-continue-from nil)
  13048. (save-excursion
  13049. (setq rtn1 (funcall action))
  13050. (push rtn1 rtn)))
  13051. (t (user-error "Invalid action")))
  13052. ;; if we are to skip sublevels, jump to end of subtree
  13053. (unless org-tags-match-list-sublevels
  13054. (org-end-of-subtree t)
  13055. (backward-char 1))))
  13056. ;; Get the correct position from where to continue
  13057. (if org-map-continue-from
  13058. (goto-char org-map-continue-from)
  13059. (and (= (point) lspos) (end-of-line 1)))))
  13060. (when (and (eq action 'sparse-tree)
  13061. (not org-sparse-tree-open-archived-trees))
  13062. (org-hide-archived-subtrees (point-min) (point-max)))
  13063. (nreverse rtn)))
  13064. (defun org-remove-uninherited-tags (tags)
  13065. "Remove all tags that are not inherited from the list TAGS."
  13066. (cond
  13067. ((eq org-use-tag-inheritance t)
  13068. (if org-tags-exclude-from-inheritance
  13069. (org-delete-all org-tags-exclude-from-inheritance tags)
  13070. tags))
  13071. ((not org-use-tag-inheritance) nil)
  13072. ((stringp org-use-tag-inheritance)
  13073. (delq nil (mapcar
  13074. (lambda (x)
  13075. (if (and (string-match org-use-tag-inheritance x)
  13076. (not (member x org-tags-exclude-from-inheritance)))
  13077. x nil))
  13078. tags)))
  13079. ((listp org-use-tag-inheritance)
  13080. (delq nil (mapcar
  13081. (lambda (x)
  13082. (if (member x org-use-tag-inheritance) x nil))
  13083. tags)))))
  13084. (defun org-match-sparse-tree (&optional todo-only match)
  13085. "Create a sparse tree according to tags string MATCH.
  13086. MATCH can contain positive and negative selection of tags, like
  13087. \"+WORK+URGENT-WITHBOSS\".
  13088. If optional argument TODO-ONLY is non-nil, only select lines that are
  13089. also TODO lines."
  13090. (interactive "P")
  13091. (org-agenda-prepare-buffers (list (current-buffer)))
  13092. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13093. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13094. (defvar org-cached-props nil)
  13095. (defun org-cached-entry-get (pom property)
  13096. (if (or (eq t org-use-property-inheritance)
  13097. (and (stringp org-use-property-inheritance)
  13098. (let ((case-fold-search t))
  13099. (org-string-match-p org-use-property-inheritance property)))
  13100. (and (listp org-use-property-inheritance)
  13101. (member-ignore-case property org-use-property-inheritance)))
  13102. ;; Caching is not possible, check it directly.
  13103. (org-entry-get pom property 'inherit)
  13104. ;; Get all properties, so we can do complicated checks easily.
  13105. (cdr (assoc-string property
  13106. (or org-cached-props
  13107. (setq org-cached-props (org-entry-properties pom)))
  13108. t))))
  13109. (defun org-global-tags-completion-table (&optional files)
  13110. "Return the list of all tags in all agenda buffer/files.
  13111. Optional FILES argument is a list of files which can be used
  13112. instead of the agenda files."
  13113. (save-excursion
  13114. (org-uniquify
  13115. (delq nil
  13116. (apply 'append
  13117. (mapcar
  13118. (lambda (file)
  13119. (set-buffer (find-file-noselect file))
  13120. (append (org-get-buffer-tags)
  13121. (mapcar (lambda (x) (if (stringp (car-safe x))
  13122. (list (car-safe x)) nil))
  13123. org-tag-alist)))
  13124. (if (and files (car files))
  13125. files
  13126. (org-agenda-files))))))))
  13127. (defun org-make-tags-matcher (match)
  13128. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13129. The variable `todo-only' is scoped dynamically into this function.
  13130. It will be set to t if the matcher restricts matching to TODO entries,
  13131. otherwise will not be touched.
  13132. Returns a cons of the selection string MATCH and the constructed
  13133. lisp form implementing the matcher. The matcher is to be evaluated
  13134. at an Org entry, with point on the headline, and returns t if the
  13135. entry matches the selection string MATCH. The returned lisp form
  13136. references two variables with information about the entry, which
  13137. must be bound around the form's evaluation: todo, the TODO keyword
  13138. at the entry (or nil of none); and tags-list, the list of all tags
  13139. at the entry including inherited ones. Additionally, the category
  13140. of the entry (if any) must be specified as the text property
  13141. 'org-category on the headline.
  13142. See also `org-scan-tags'.
  13143. "
  13144. (declare (special todo-only))
  13145. (unless (boundp 'todo-only)
  13146. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13147. (unless match
  13148. ;; Get a new match request, with completion against the global
  13149. ;; tags table and the local tags in current buffer
  13150. (let ((org-last-tags-completion-table
  13151. (org-uniquify
  13152. (delq nil (append (org-get-buffer-tags)
  13153. (org-global-tags-completion-table))))))
  13154. (setq match (org-completing-read-no-i
  13155. "Match: " 'org-tags-completion-function nil nil nil
  13156. 'org-tags-history))))
  13157. ;; Parse the string and create a lisp form
  13158. (let ((match0 match)
  13159. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13160. minus tag mm
  13161. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13162. orterms term orlist re-p str-p level-p level-op time-p
  13163. prop-p pn pv po gv rest (start 0) (ss 0))
  13164. ;; Expand group tags
  13165. (setq match (org-tags-expand match))
  13166. ;; Check if there is a TODO part of this match, which would be the
  13167. ;; part after a "/". TO make sure that this slash is not part of
  13168. ;; a property value to be matched against, we also check that there
  13169. ;; is no " after that slash.
  13170. ;; First, find the last slash
  13171. (while (string-match "/+" match ss)
  13172. (setq start (match-beginning 0) ss (match-end 0)))
  13173. (if (and (string-match "/+" match start)
  13174. (not (save-match-data (string-match "\"" match start))))
  13175. ;; match contains also a todo-matching request
  13176. (progn
  13177. (setq tagsmatch (substring match 0 (match-beginning 0))
  13178. todomatch (substring match (match-end 0)))
  13179. (if (string-match "^!" todomatch)
  13180. (setq todo-only t todomatch (substring todomatch 1)))
  13181. (if (string-match "^\\s-*$" todomatch)
  13182. (setq todomatch nil)))
  13183. ;; only matching tags
  13184. (setq tagsmatch match todomatch nil))
  13185. ;; Make the tags matcher
  13186. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13187. (setq tagsmatcher t)
  13188. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13189. (while (setq term (pop orterms))
  13190. (while (and (equal (substring term -1) "\\") orterms)
  13191. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13192. (while (string-match re term)
  13193. (setq rest (substring term (match-end 0))
  13194. minus (and (match-end 1)
  13195. (equal (match-string 1 term) "-"))
  13196. tag (save-match-data (replace-regexp-in-string
  13197. "\\\\-" "-"
  13198. (match-string 2 term)))
  13199. re-p (equal (string-to-char tag) ?{)
  13200. level-p (match-end 4)
  13201. prop-p (match-end 5)
  13202. mm (cond
  13203. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13204. (level-p
  13205. (setq level-op (org-op-to-function (match-string 3 term)))
  13206. `(,level-op level ,(string-to-number
  13207. (match-string 4 term))))
  13208. (prop-p
  13209. (setq pn (match-string 5 term)
  13210. po (match-string 6 term)
  13211. pv (match-string 7 term)
  13212. re-p (equal (string-to-char pv) ?{)
  13213. str-p (equal (string-to-char pv) ?\")
  13214. time-p (save-match-data
  13215. (string-match "^\"[[<].*[]>]\"$" pv))
  13216. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13217. (if time-p (setq pv (org-matcher-time pv)))
  13218. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13219. (cond
  13220. ((equal pn "CATEGORY")
  13221. (setq gv '(get-text-property (point) 'org-category)))
  13222. ((equal pn "TODO")
  13223. (setq gv 'todo))
  13224. (t
  13225. (setq gv `(org-cached-entry-get nil ,pn))))
  13226. (if re-p
  13227. (if (eq po 'org<>)
  13228. `(not (string-match ,pv (or ,gv "")))
  13229. `(string-match ,pv (or ,gv "")))
  13230. (if str-p
  13231. `(,po (or ,gv "") ,pv)
  13232. `(,po (string-to-number (or ,gv ""))
  13233. ,(string-to-number pv) ))))
  13234. (t `(member ,tag tags-list)))
  13235. mm (if minus (list 'not mm) mm)
  13236. term rest)
  13237. (push mm tagsmatcher))
  13238. (push (if (> (length tagsmatcher) 1)
  13239. (cons 'and tagsmatcher)
  13240. (car tagsmatcher))
  13241. orlist)
  13242. (setq tagsmatcher nil))
  13243. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13244. (setq tagsmatcher
  13245. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13246. ;; Make the todo matcher
  13247. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13248. (setq todomatcher t)
  13249. (setq orterms (org-split-string todomatch "|") orlist nil)
  13250. (dolist (term orterms)
  13251. (while (string-match re term)
  13252. (setq minus (and (match-end 1)
  13253. (equal (match-string 1 term) "-"))
  13254. kwd (match-string 2 term)
  13255. re-p (equal (string-to-char kwd) ?{)
  13256. term (substring term (match-end 0))
  13257. mm (if re-p
  13258. `(string-match ,(substring kwd 1 -1) todo)
  13259. (list 'equal 'todo kwd))
  13260. mm (if minus (list 'not mm) mm))
  13261. (push mm todomatcher))
  13262. (push (if (> (length todomatcher) 1)
  13263. (cons 'and todomatcher)
  13264. (car todomatcher))
  13265. orlist)
  13266. (setq todomatcher nil))
  13267. (setq todomatcher (if (> (length orlist) 1)
  13268. (cons 'or orlist) (car orlist))))
  13269. ;; Return the string and lisp forms of the matcher
  13270. (setq matcher (if todomatcher
  13271. (list 'and tagsmatcher todomatcher)
  13272. tagsmatcher))
  13273. (when todo-only
  13274. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13275. matcher)))
  13276. (cons match0 matcher)))
  13277. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13278. "Expand group tags in MATCH.
  13279. This replaces every group tag in MATCH with a regexp tag search.
  13280. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13281. will be replaced like this:
  13282. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13283. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13284. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13285. Replacing by a regexp preserves the structure of the match.
  13286. E.g., this expansion
  13287. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13288. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13289. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13290. A group tag in MATCH can contain regular expressions of its own.
  13291. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13292. will be replaced like this:
  13293. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13294. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13295. assumed to be a single group tag, and the function will return
  13296. the list of tags in this group.
  13297. When DOWNCASE is non-nil, expand downcased TAGS."
  13298. (if org-group-tags
  13299. (let* ((case-fold-search t)
  13300. (stable org-mode-syntax-table)
  13301. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13302. (taggroups (if downcased
  13303. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13304. taggroups)
  13305. taggroups))
  13306. (taggroups-keys (mapcar #'car taggroups))
  13307. (return-match (if downcased (downcase match) match))
  13308. (count 0)
  13309. (work-already-expanded tags-already-expanded)
  13310. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13311. ;; @ and _ are allowed as word-components in tags.
  13312. (modify-syntax-entry ?@ "w" stable)
  13313. (modify-syntax-entry ?_ "w" stable)
  13314. ;; Temporarily replace regexp-expressions in the match-expression.
  13315. (while (string-match "{.+?}" return-match)
  13316. (incf count)
  13317. (push (match-string 0 return-match) regexps-in-match)
  13318. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13319. (while (and taggroups-keys
  13320. (with-syntax-table stable
  13321. (string-match
  13322. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13323. (regexp-opt taggroups-keys) "\\>\\)")
  13324. return-match)))
  13325. (let* ((dir (match-string 1 return-match))
  13326. (tag (match-string 2 return-match))
  13327. (tag (if downcased (downcase tag) tag)))
  13328. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13329. (member tag work-already-expanded))
  13330. (setq tags-in-group (assoc tag taggroups))
  13331. (push tag work-already-expanded)
  13332. ;; Recursively expand each tag in the group, if the tag hasn't
  13333. ;; already been expanded. Restore the match-data after all recursive calls.
  13334. (save-match-data
  13335. (let (tags-expanded)
  13336. (dolist (x (cdr tags-in-group))
  13337. (if (and (member x taggroups-keys)
  13338. (not (member x work-already-expanded)))
  13339. (setq tags-expanded
  13340. (delete-dups
  13341. (append
  13342. (org-tags-expand x t downcased
  13343. work-already-expanded)
  13344. tags-expanded)))
  13345. (setq tags-expanded
  13346. (append (list x) tags-expanded)))
  13347. (setq work-already-expanded
  13348. (delete-dups
  13349. (append tags-expanded
  13350. work-already-expanded))))
  13351. (setq tags-in-group
  13352. (delete-dups (cons (car tags-in-group)
  13353. tags-expanded)))))
  13354. ;; Filter tag-regexps from tags.
  13355. (setq regexp-in-group-escaped
  13356. (delq nil (mapcar (lambda (x)
  13357. (if (stringp x)
  13358. (and (equal "{" (substring x 0 1))
  13359. (equal "}" (substring x -1))
  13360. x)
  13361. x))
  13362. tags-in-group))
  13363. regexp-in-group
  13364. (mapcar (lambda (x)
  13365. (substring x 1 -1))
  13366. regexp-in-group-escaped)
  13367. tags-in-group
  13368. (delq nil (mapcar (lambda (x)
  13369. (if (stringp x)
  13370. (and (not (equal "{" (substring x 0 1)))
  13371. (not (equal "}" (substring x -1)))
  13372. x)
  13373. x))
  13374. tags-in-group)))
  13375. ;; If single-as-list, do no more in the while-loop.
  13376. (if (not single-as-list)
  13377. (progn
  13378. (when regexp-in-group
  13379. (setq regexp-in-group
  13380. (concat "\\|"
  13381. (mapconcat 'identity regexp-in-group
  13382. "\\|"))))
  13383. (setq tags-in-group
  13384. (concat dir
  13385. "{\\<"
  13386. (regexp-opt tags-in-group)
  13387. "\\>"
  13388. regexp-in-group
  13389. "}"))
  13390. (when (stringp tags-in-group)
  13391. (org-add-props tags-in-group '(grouptag t)))
  13392. (setq return-match
  13393. (replace-match tags-in-group t t return-match)))
  13394. (setq tags-in-group
  13395. (append regexp-in-group-escaped tags-in-group))))
  13396. (setq taggroups-keys (delete tag taggroups-keys))))
  13397. ;; Add the regular expressions back into the match-expression again.
  13398. (while regexps-in-match
  13399. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13400. (pop regexps-in-match)
  13401. return-match t t))
  13402. (decf count))
  13403. (if single-as-list
  13404. (if tags-in-group tags-in-group (list return-match))
  13405. return-match))
  13406. (if single-as-list
  13407. (list (if downcased (downcase match) match))
  13408. match)))
  13409. (defun org-op-to-function (op &optional stringp)
  13410. "Turn an operator into the appropriate function."
  13411. (setq op
  13412. (cond
  13413. ((equal op "<" ) '(< string< org-time<))
  13414. ((equal op ">" ) '(> org-string> org-time>))
  13415. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13416. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13417. ((member op '("=" "==")) '(= string= org-time=))
  13418. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13419. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13420. (defun org<> (a b) (not (= a b)))
  13421. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13422. (defun org-string>= (a b) (not (string< a b)))
  13423. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13424. (defun org-string<> (a b) (not (string= 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) (>= a b)))
  13430. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13431. (defun org-2ft (s)
  13432. "Convert S to a floating point time.
  13433. If S is already a number, just return it. If it is a string, parse
  13434. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13435. (cond
  13436. ((numberp s) s)
  13437. ((stringp s)
  13438. (condition-case nil
  13439. (float-time (apply 'encode-time (org-parse-time-string s)))
  13440. (error 0.)))
  13441. (t 0.)))
  13442. (defun org-time-today ()
  13443. "Time in seconds today at 0:00.
  13444. Returns the float number of seconds since the beginning of the
  13445. epoch to the beginning of today (00:00)."
  13446. (float-time (apply 'encode-time
  13447. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13448. (defun org-matcher-time (s)
  13449. "Interpret a time comparison value."
  13450. (save-match-data
  13451. (cond
  13452. ((string= s "<now>") (float-time))
  13453. ((string= s "<today>") (org-time-today))
  13454. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13455. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13456. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13457. (+ (org-time-today)
  13458. (* (string-to-number (match-string 1 s))
  13459. (cdr (assoc (match-string 2 s)
  13460. '(("d" . 86400.0) ("w" . 604800.0)
  13461. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13462. (t (org-2ft s)))))
  13463. (defun org-match-any-p (re list)
  13464. "Does re match any element of list?"
  13465. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13466. (delq nil list))
  13467. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13468. (defvar org-tags-overlay (make-overlay 1 1))
  13469. (org-detach-overlay org-tags-overlay)
  13470. (defun org-get-local-tags-at (&optional pos)
  13471. "Get a list of tags defined in the current headline."
  13472. (org-get-tags-at pos 'local))
  13473. (defun org-get-local-tags ()
  13474. "Get a list of tags defined in the current headline."
  13475. (org-get-tags-at nil 'local))
  13476. (defun org-get-tags-at (&optional pos local)
  13477. "Get a list of all headline tags applicable at POS.
  13478. POS defaults to point. If tags are inherited, the list contains
  13479. the targets in the same sequence as the headlines appear, i.e.
  13480. the tags of the current headline come last.
  13481. When LOCAL is non-nil, only return tags from the current headline,
  13482. ignore inherited ones."
  13483. (interactive)
  13484. (if (and org-trust-scanner-tags
  13485. (or (not pos) (equal pos (point)))
  13486. (not local))
  13487. org-scanner-tags
  13488. (let (tags ltags lastpos parent)
  13489. (save-excursion
  13490. (save-restriction
  13491. (widen)
  13492. (goto-char (or pos (point)))
  13493. (save-match-data
  13494. (catch 'done
  13495. (condition-case nil
  13496. (progn
  13497. (org-back-to-heading t)
  13498. (while (not (equal lastpos (point)))
  13499. (setq lastpos (point))
  13500. (when (looking-at
  13501. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13502. (setq ltags (org-split-string
  13503. (org-match-string-no-properties 1) ":"))
  13504. (when parent
  13505. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13506. (setq tags (append
  13507. (if parent
  13508. (org-remove-uninherited-tags ltags)
  13509. ltags)
  13510. tags)))
  13511. (or org-use-tag-inheritance (throw 'done t))
  13512. (if local (throw 'done t))
  13513. (or (org-up-heading-safe) (error nil))
  13514. (setq parent t)))
  13515. (error nil)))))
  13516. (if local
  13517. tags
  13518. (reverse (delete-dups
  13519. (reverse (append
  13520. (org-remove-uninherited-tags
  13521. org-file-tags)
  13522. tags)))))))))
  13523. (defun org-add-prop-inherited (s)
  13524. (add-text-properties 0 (length s) '(inherited t) s)
  13525. s)
  13526. (defun org-toggle-tag (tag &optional onoff)
  13527. "Toggle the tag TAG for the current line.
  13528. If ONOFF is `on' or `off', don't toggle but set to this state."
  13529. (let (res current)
  13530. (save-excursion
  13531. (org-back-to-heading t)
  13532. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13533. (point-at-eol) t)
  13534. (progn
  13535. (setq current (match-string 1))
  13536. (replace-match ""))
  13537. (setq current ""))
  13538. (setq current (nreverse (org-split-string current ":")))
  13539. (cond
  13540. ((eq onoff 'on)
  13541. (setq res t)
  13542. (or (member tag current) (push tag current)))
  13543. ((eq onoff 'off)
  13544. (or (not (member tag current)) (setq current (delete tag current))))
  13545. (t (if (member tag current)
  13546. (setq current (delete tag current))
  13547. (setq res t)
  13548. (push tag current))))
  13549. (end-of-line 1)
  13550. (if current
  13551. (progn
  13552. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13553. (org-set-tags nil t))
  13554. (delete-horizontal-space))
  13555. (run-hooks 'org-after-tags-change-hook))
  13556. res))
  13557. (defun org-align-tags-here (to-col)
  13558. ;; Assumes that this is a headline
  13559. "Align tags on the current headline to TO-COL."
  13560. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13561. (beginning-of-line 1)
  13562. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13563. (< pos (match-beginning 2)))
  13564. (progn
  13565. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13566. (goto-char (match-beginning 1))
  13567. (insert " ")
  13568. (delete-region (point) (1+ (match-beginning 2)))
  13569. (setq ncol (max (current-column)
  13570. (1+ col)
  13571. (if (> to-col 0)
  13572. to-col
  13573. (- (abs to-col) tags-l))))
  13574. (setq p (point))
  13575. (insert (make-string (- ncol (current-column)) ?\ ))
  13576. (setq ncol (current-column))
  13577. (when indent-tabs-mode (tabify p (point-at-eol)))
  13578. (org-move-to-column (min ncol col)))
  13579. (goto-char pos))))
  13580. (defun org-set-tags-command (&optional arg just-align)
  13581. "Call the set-tags command for the current entry."
  13582. (interactive "P")
  13583. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13584. (org-set-tags arg just-align)
  13585. (save-excursion
  13586. (unless (and (org-region-active-p)
  13587. org-loop-over-headlines-in-active-region)
  13588. (org-back-to-heading t))
  13589. (org-set-tags arg just-align))))
  13590. (defun org-set-tags-to (data)
  13591. "Set the tags of the current entry to DATA, replacing the current tags.
  13592. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13593. If DATA is nil or the empty string, any tags will be removed."
  13594. (interactive "sTags: ")
  13595. (setq data
  13596. (cond
  13597. ((eq data nil) "")
  13598. ((equal data "") "")
  13599. ((stringp data)
  13600. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13601. ":"))
  13602. ((listp data)
  13603. (concat ":" (mapconcat 'identity data ":") ":"))))
  13604. (when data
  13605. (save-excursion
  13606. (org-back-to-heading t)
  13607. (when (looking-at org-complex-heading-regexp)
  13608. (if (match-end 5)
  13609. (progn
  13610. (goto-char (match-beginning 5))
  13611. (insert data)
  13612. (delete-region (point) (point-at-eol))
  13613. (org-set-tags nil 'align))
  13614. (goto-char (point-at-eol))
  13615. (insert " " data)
  13616. (org-set-tags nil 'align)))
  13617. (beginning-of-line 1)
  13618. (if (looking-at ".*?\\([ \t]+\\)$")
  13619. (delete-region (match-beginning 1) (match-end 1))))))
  13620. (defun org-align-all-tags ()
  13621. "Align the tags i all headings."
  13622. (interactive)
  13623. (save-excursion
  13624. (or (ignore-errors (org-back-to-heading t))
  13625. (outline-next-heading))
  13626. (if (org-at-heading-p)
  13627. (org-set-tags t)
  13628. (message "No headings"))))
  13629. (defvar org-indent-indentation-per-level)
  13630. (defun org-set-tags (&optional arg just-align)
  13631. "Set the tags for the current headline.
  13632. With prefix ARG, realign all tags in headings in the current buffer.
  13633. When JUST-ALIGN is non-nil, only align tags."
  13634. (interactive "P")
  13635. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13636. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13637. 'region-start-level
  13638. 'region))
  13639. org-loop-over-headlines-in-active-region)
  13640. (org-map-entries
  13641. ;; We don't use ARG and JUST-ALIGN here because these args
  13642. ;; are not useful when looping over headlines.
  13643. #'org-set-tags
  13644. org-loop-over-headlines-in-active-region
  13645. cl
  13646. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13647. (let ((org-setting-tags t))
  13648. (if arg
  13649. (save-excursion
  13650. (goto-char (point-min))
  13651. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13652. (while (re-search-forward org-outline-regexp-bol nil t)
  13653. (org-set-tags nil t)
  13654. (end-of-line)))
  13655. (message "All tags realigned to column %d" org-tags-column))
  13656. (let* ((current (org-get-tags-string))
  13657. (col (current-column))
  13658. (tags
  13659. (if just-align current
  13660. ;; Get a new set of tags from the user.
  13661. (save-excursion
  13662. (let* ((table
  13663. (setq
  13664. org-last-tags-completion-table
  13665. (append
  13666. org-tag-persistent-alist
  13667. (or org-tag-alist (org-get-buffer-tags))
  13668. (and
  13669. org-complete-tags-always-offer-all-agenda-tags
  13670. (org-global-tags-completion-table
  13671. (org-agenda-files))))))
  13672. (current-tags (org-split-string current ":"))
  13673. (inherited-tags
  13674. (nreverse (nthcdr (length current-tags)
  13675. (nreverse (org-get-tags-at))))))
  13676. (replace-regexp-in-string
  13677. "\\([-+&]+\\|,\\)"
  13678. ":"
  13679. (if (or (eq t org-use-fast-tag-selection)
  13680. (and org-use-fast-tag-selection
  13681. (delq nil (mapcar #'cdr table))))
  13682. (org-fast-tag-selection
  13683. current-tags inherited-tags table
  13684. (and org-fast-tag-selection-include-todo
  13685. org-todo-key-alist))
  13686. (let ((org-add-colon-after-tag-completion
  13687. (< 1 (length table))))
  13688. (org-trim
  13689. (completing-read
  13690. "Tags: "
  13691. #'org-tags-completion-function
  13692. nil nil current 'org-tags-history))))))))))
  13693. (when org-tags-sort-function
  13694. (setq tags
  13695. (mapconcat
  13696. #'identity
  13697. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13698. org-tags-sort-function)
  13699. ":")))
  13700. (if (not (org-string-nw-p tags)) (setq tags "")
  13701. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13702. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13703. ;; Insert new tags at the correct column
  13704. (beginning-of-line)
  13705. (let ((level (if (looking-at org-outline-regexp)
  13706. (- (match-end 0) (point) 1)
  13707. 1)))
  13708. (cond
  13709. ((and (equal current "") (equal tags "")))
  13710. ((re-search-forward
  13711. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13712. (line-end-position)
  13713. t)
  13714. (if (equal tags "") (replace-match "" t t)
  13715. (goto-char (match-beginning 0))
  13716. (let* ((c0 (current-column))
  13717. ;; Compute offset for the case of org-indent-mode
  13718. ;; active.
  13719. (di (if (org-bound-and-true-p org-indent-mode)
  13720. (* (1- org-indent-indentation-per-level)
  13721. (1- level))
  13722. 0))
  13723. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13724. (tc (+ org-tags-column
  13725. (if (> org-tags-column 0) (- di) di)))
  13726. (c1 (max (1+ c0)
  13727. (if (> tc 0) tc
  13728. (- (- tc) (string-width tags)))))
  13729. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13730. (replace-match rpl t t)
  13731. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13732. (tabify p0 (point))))))
  13733. (t (error "Tags alignment failed"))))
  13734. (org-move-to-column col))
  13735. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13736. (defun org-change-tag-in-region (beg end tag off)
  13737. "Add or remove TAG for each entry in the region.
  13738. This works in the agenda, and also in an org-mode buffer."
  13739. (interactive
  13740. (list (region-beginning) (region-end)
  13741. (let ((org-last-tags-completion-table
  13742. (if (derived-mode-p 'org-mode)
  13743. (org-uniquify
  13744. (delq nil (append (org-get-buffer-tags)
  13745. (org-global-tags-completion-table))))
  13746. (org-global-tags-completion-table))))
  13747. (org-icompleting-read
  13748. "Tag: " 'org-tags-completion-function nil nil nil
  13749. 'org-tags-history))
  13750. (progn
  13751. (message "[s]et or [r]emove? ")
  13752. (equal (read-char-exclusive) ?r))))
  13753. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13754. (let ((agendap (equal major-mode 'org-agenda-mode))
  13755. l1 l2 m buf pos newhead (cnt 0))
  13756. (goto-char end)
  13757. (setq l2 (1- (org-current-line)))
  13758. (goto-char beg)
  13759. (setq l1 (org-current-line))
  13760. (loop for l from l1 to l2 do
  13761. (org-goto-line l)
  13762. (setq m (get-text-property (point) 'org-hd-marker))
  13763. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13764. (and agendap m))
  13765. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13766. pos (if agendap m (point)))
  13767. (with-current-buffer buf
  13768. (save-excursion
  13769. (save-restriction
  13770. (goto-char pos)
  13771. (setq cnt (1+ cnt))
  13772. (org-toggle-tag tag (if off 'off 'on))
  13773. (setq newhead (org-get-heading)))))
  13774. (and agendap (org-agenda-change-all-lines newhead m))))
  13775. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13776. (defun org-tags-completion-function (string predicate &optional flag)
  13777. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13778. (confirm (lambda (x) (stringp (car x)))))
  13779. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13780. (setq s1 (match-string 1 string)
  13781. s2 (match-string 2 string))
  13782. (setq s1 "" s2 string))
  13783. (cond
  13784. ((eq flag nil)
  13785. ;; try completion
  13786. (setq rtn (try-completion s2 ctable confirm))
  13787. (if (stringp rtn)
  13788. (setq rtn
  13789. (concat s1 s2 (substring rtn (length s2))
  13790. (if (and org-add-colon-after-tag-completion
  13791. (assoc rtn ctable))
  13792. ":" ""))))
  13793. rtn)
  13794. ((eq flag t)
  13795. ;; all-completions
  13796. (all-completions s2 ctable confirm))
  13797. ((eq flag 'lambda)
  13798. ;; exact match?
  13799. (assoc s2 ctable)))))
  13800. (defun org-fast-tag-insert (kwd tags face &optional end)
  13801. "Insert KDW, and the TAGS, the latter with face FACE.
  13802. Also insert END."
  13803. (insert (format "%-12s" (concat kwd ":"))
  13804. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13805. (or end "")))
  13806. (defun org-fast-tag-show-exit (flag)
  13807. (save-excursion
  13808. (org-goto-line 3)
  13809. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13810. (replace-match ""))
  13811. (when flag
  13812. (end-of-line 1)
  13813. (org-move-to-column (- (window-width) 19) t)
  13814. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13815. (defun org-set-current-tags-overlay (current prefix)
  13816. "Add an overlay to CURRENT tag with PREFIX."
  13817. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13818. (if (featurep 'xemacs)
  13819. (org-overlay-display org-tags-overlay (concat prefix s)
  13820. 'secondary-selection)
  13821. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13822. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13823. (defvar org-last-tag-selection-key nil)
  13824. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13825. "Fast tag selection with single keys.
  13826. CURRENT is the current list of tags in the headline, INHERITED is the
  13827. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13828. possibly with grouping information. TODO-TABLE is a similar table with
  13829. TODO keywords, should these have keys assigned to them.
  13830. If the keys are nil, a-z are automatically assigned.
  13831. Returns the new tags string, or nil to not change the current settings."
  13832. (let* ((fulltable (append table todo-table))
  13833. (maxlen (apply 'max (mapcar
  13834. (lambda (x)
  13835. (if (stringp (car x)) (string-width (car x)) 0))
  13836. fulltable)))
  13837. (buf (current-buffer))
  13838. (expert (eq org-fast-tag-selection-single-key 'expert))
  13839. (buffer-tags nil)
  13840. (fwidth (+ maxlen 3 1 3))
  13841. (ncol (/ (- (window-width) 4) fwidth))
  13842. (i-face 'org-done)
  13843. (c-face 'org-todo)
  13844. tg cnt e c char c1 c2 ntable tbl rtn
  13845. ov-start ov-end ov-prefix
  13846. (exit-after-next org-fast-tag-selection-single-key)
  13847. (done-keywords org-done-keywords)
  13848. groups ingroup intaggroup)
  13849. (save-excursion
  13850. (beginning-of-line 1)
  13851. (if (looking-at
  13852. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13853. (setq ov-start (match-beginning 1)
  13854. ov-end (match-end 1)
  13855. ov-prefix "")
  13856. (setq ov-start (1- (point-at-eol))
  13857. ov-end (1+ ov-start))
  13858. (skip-chars-forward "^\n\r")
  13859. (setq ov-prefix
  13860. (concat
  13861. (buffer-substring (1- (point)) (point))
  13862. (if (> (current-column) org-tags-column)
  13863. " "
  13864. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13865. (move-overlay org-tags-overlay ov-start ov-end)
  13866. (save-window-excursion
  13867. (if expert
  13868. (set-buffer (get-buffer-create " *Org tags*"))
  13869. (delete-other-windows)
  13870. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13871. (org-switch-to-buffer-other-window " *Org tags*"))
  13872. (erase-buffer)
  13873. (org-set-local 'org-done-keywords done-keywords)
  13874. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13875. (org-fast-tag-insert "Current" current c-face "\n\n")
  13876. (org-fast-tag-show-exit exit-after-next)
  13877. (org-set-current-tags-overlay current ov-prefix)
  13878. (setq tbl fulltable char ?a cnt 0)
  13879. (while (setq e (pop tbl))
  13880. (cond
  13881. ((eq (car e) :startgroup)
  13882. (push '() groups) (setq ingroup t)
  13883. (unless (zerop cnt)
  13884. (setq cnt 0)
  13885. (insert "\n"))
  13886. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13887. ((eq (car e) :endgroup)
  13888. (setq ingroup nil cnt 0)
  13889. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13890. ((eq (car e) :startgrouptag)
  13891. (setq intaggroup t)
  13892. (unless (zerop cnt)
  13893. (setq cnt 0)
  13894. (insert "\n"))
  13895. (insert "[ "))
  13896. ((eq (car e) :endgrouptag)
  13897. (setq intaggroup nil cnt 0)
  13898. (insert "]\n"))
  13899. ((equal e '(:newline))
  13900. (unless (zerop cnt)
  13901. (setq cnt 0)
  13902. (insert "\n")
  13903. (setq e (car tbl))
  13904. (while (equal (car tbl) '(:newline))
  13905. (insert "\n")
  13906. (setq tbl (cdr tbl)))))
  13907. ((equal e '(:grouptags)) (insert " : "))
  13908. (t
  13909. (setq tg (copy-sequence (car e)) c2 nil)
  13910. (if (cdr e)
  13911. (setq c (cdr e))
  13912. ;; automatically assign a character.
  13913. (setq c1 (string-to-char
  13914. (downcase (substring
  13915. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13916. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13917. (while (or (rassoc char ntable) (rassoc char table))
  13918. (setq char (1+ char)))
  13919. (setq c2 c1))
  13920. (setq c (or c2 char)))
  13921. (when ingroup (push tg (car groups)))
  13922. (setq tg (org-add-props tg nil 'face
  13923. (cond
  13924. ((not (assoc tg table))
  13925. (org-get-todo-face tg))
  13926. ((member tg current) c-face)
  13927. ((member tg inherited) i-face))))
  13928. (when (equal (caar tbl) :grouptags)
  13929. (org-add-props tg nil 'face 'org-tag-group))
  13930. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13931. (insert "[" c "] " tg (make-string
  13932. (- fwidth 4 (length tg)) ?\ ))
  13933. (push (cons tg c) ntable)
  13934. (when (= (incf cnt) ncol)
  13935. (insert "\n")
  13936. (when (or ingroup intaggroup) (insert " "))
  13937. (setq cnt 0)))))
  13938. (setq ntable (nreverse ntable))
  13939. (insert "\n")
  13940. (goto-char (point-min))
  13941. (unless expert (org-fit-window-to-buffer))
  13942. (setq rtn
  13943. (catch 'exit
  13944. (while t
  13945. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13946. (if (not groups) "no " "")
  13947. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13948. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13949. (setq org-last-tag-selection-key c)
  13950. (cond
  13951. ((= c ?\r) (throw 'exit t))
  13952. ((= c ?!)
  13953. (setq groups (not groups))
  13954. (goto-char (point-min))
  13955. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13956. ((= c ?\C-c)
  13957. (if (not expert)
  13958. (org-fast-tag-show-exit
  13959. (setq exit-after-next (not exit-after-next)))
  13960. (setq expert nil)
  13961. (delete-other-windows)
  13962. (set-window-buffer (split-window-vertically) " *Org tags*")
  13963. (org-switch-to-buffer-other-window " *Org tags*")
  13964. (org-fit-window-to-buffer)))
  13965. ((or (= c ?\C-g)
  13966. (and (= c ?q) (not (rassoc c ntable))))
  13967. (org-detach-overlay org-tags-overlay)
  13968. (setq quit-flag t))
  13969. ((= c ?\ )
  13970. (setq current nil)
  13971. (when exit-after-next (setq exit-after-next 'now)))
  13972. ((= c ?\t)
  13973. (condition-case nil
  13974. (setq tg (org-icompleting-read
  13975. "Tag: "
  13976. (or buffer-tags
  13977. (with-current-buffer buf
  13978. (setq buffer-tags
  13979. (org-get-buffer-tags))))))
  13980. (quit (setq tg "")))
  13981. (when (string-match "\\S-" tg)
  13982. (add-to-list 'buffer-tags (list tg))
  13983. (if (member tg current)
  13984. (setq current (delete tg current))
  13985. (push tg current)))
  13986. (when exit-after-next (setq exit-after-next 'now)))
  13987. ((setq e (rassoc c todo-table) tg (car e))
  13988. (with-current-buffer buf
  13989. (save-excursion (org-todo tg)))
  13990. (when exit-after-next (setq exit-after-next 'now)))
  13991. ((setq e (rassoc c ntable) tg (car e))
  13992. (if (member tg current)
  13993. (setq current (delete tg current))
  13994. (loop for g in groups do
  13995. (when (member tg g)
  13996. (dolist (x g) (setq current (delete x current)))))
  13997. (push tg current))
  13998. (when exit-after-next (setq exit-after-next 'now))))
  13999. ;; Create a sorted list
  14000. (setq current
  14001. (sort current
  14002. (lambda (a b)
  14003. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14004. (when (eq exit-after-next 'now) (throw 'exit t))
  14005. (goto-char (point-min))
  14006. (beginning-of-line 2)
  14007. (delete-region (point) (point-at-eol))
  14008. (org-fast-tag-insert "Current" current c-face)
  14009. (org-set-current-tags-overlay current ov-prefix)
  14010. (while (re-search-forward
  14011. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14012. (setq tg (match-string 1))
  14013. (add-text-properties
  14014. (match-beginning 1) (match-end 1)
  14015. (list 'face
  14016. (cond
  14017. ((member tg current) c-face)
  14018. ((member tg inherited) i-face)
  14019. (t (get-text-property (match-beginning 1) 'face))))))
  14020. (goto-char (point-min)))))
  14021. (org-detach-overlay org-tags-overlay)
  14022. (if rtn
  14023. (mapconcat 'identity current ":")
  14024. nil))))
  14025. (defun org-get-tags-string ()
  14026. "Get the TAGS string in the current headline."
  14027. (unless (org-at-heading-p t)
  14028. (user-error "Not on a heading"))
  14029. (save-excursion
  14030. (beginning-of-line 1)
  14031. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14032. (org-match-string-no-properties 1)
  14033. "")))
  14034. (defun org-get-tags ()
  14035. "Get the list of tags specified in the current headline."
  14036. (org-split-string (org-get-tags-string) ":"))
  14037. (defun org-get-buffer-tags ()
  14038. "Get a table of all tags used in the buffer, for completion."
  14039. (org-with-wide-buffer
  14040. (goto-char (point-min))
  14041. (let ((tag-re (concat org-outline-regexp-bol
  14042. "\\(?:.*?[ \t]\\)?"
  14043. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14044. tags)
  14045. (while (re-search-forward tag-re nil t)
  14046. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14047. (push tag tags)))
  14048. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14049. ;;;; The mapping API
  14050. (defun org-map-entries (func &optional match scope &rest skip)
  14051. "Call FUNC at each headline selected by MATCH in SCOPE.
  14052. FUNC is a function or a lisp form. The function will be called without
  14053. arguments, with the cursor positioned at the beginning of the headline.
  14054. The return values of all calls to the function will be collected and
  14055. returned as a list.
  14056. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14057. does not need to preserve point. After evaluation, the cursor will be
  14058. moved to the end of the line (presumably of the headline of the
  14059. processed entry) and search continues from there. Under some
  14060. circumstances, this may not produce the wanted results. For example,
  14061. if you have removed (e.g. archived) the current (sub)tree it could
  14062. mean that the next entry will be skipped entirely. In such cases, you
  14063. can specify the position from where search should continue by making
  14064. FUNC set the variable `org-map-continue-from' to the desired buffer
  14065. position.
  14066. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14067. Only headlines that are matched by this query will be considered during
  14068. the iteration. When MATCH is nil or t, all headlines will be
  14069. visited by the iteration.
  14070. SCOPE determines the scope of this command. It can be any of:
  14071. nil The current buffer, respecting the restriction if any
  14072. tree The subtree started with the entry at point
  14073. region The entries within the active region, if any
  14074. region-start-level
  14075. The entries within the active region, but only those at
  14076. the same level than the first one.
  14077. file The current buffer, without restriction
  14078. file-with-archives
  14079. The current buffer, and any archives associated with it
  14080. agenda All agenda files
  14081. agenda-with-archives
  14082. All agenda files with any archive files associated with them
  14083. \(file1 file2 ...)
  14084. If this is a list, all files in the list will be scanned
  14085. The remaining args are treated as settings for the skipping facilities of
  14086. the scanner. The following items can be given here:
  14087. archive skip trees with the archive tag
  14088. comment skip trees with the COMMENT keyword
  14089. function or Emacs Lisp form:
  14090. will be used as value for `org-agenda-skip-function', so
  14091. whenever the function returns a position, FUNC will not be
  14092. called for that entry and search will continue from the
  14093. position returned
  14094. If your function needs to retrieve the tags including inherited tags
  14095. at the *current* entry, you can use the value of the variable
  14096. `org-scanner-tags' which will be much faster than getting the value
  14097. with `org-get-tags-at'. If your function gets properties with
  14098. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14099. to t around the call to `org-entry-properties' to get the same speedup.
  14100. Note that if your function moves around to retrieve tags and properties at
  14101. a *different* entry, you cannot use these techniques."
  14102. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14103. (not (org-region-active-p)))
  14104. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14105. (org-agenda-skip-archived-trees (memq 'archive skip))
  14106. (org-agenda-skip-comment-trees (memq 'comment skip))
  14107. (org-agenda-skip-function
  14108. (car (org-delete-all '(comment archive) skip)))
  14109. (org-tags-match-list-sublevels t)
  14110. (start-level (eq scope 'region-start-level))
  14111. matcher file res
  14112. org-todo-keywords-for-agenda
  14113. org-done-keywords-for-agenda
  14114. org-todo-keyword-alist-for-agenda
  14115. org-tag-alist-for-agenda
  14116. todo-only)
  14117. (cond
  14118. ((eq match t) (setq matcher t))
  14119. ((eq match nil) (setq matcher t))
  14120. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14121. (save-excursion
  14122. (save-restriction
  14123. (cond ((eq scope 'tree)
  14124. (org-back-to-heading t)
  14125. (org-narrow-to-subtree)
  14126. (setq scope nil))
  14127. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14128. (org-region-active-p))
  14129. ;; If needed, set start-level to a string like "2"
  14130. (when start-level
  14131. (save-excursion
  14132. (goto-char (region-beginning))
  14133. (unless (org-at-heading-p) (outline-next-heading))
  14134. (setq start-level (org-current-level))))
  14135. (narrow-to-region (region-beginning)
  14136. (save-excursion
  14137. (goto-char (region-end))
  14138. (unless (and (bolp) (org-at-heading-p))
  14139. (outline-next-heading))
  14140. (point)))
  14141. (setq scope nil)))
  14142. (if (not scope)
  14143. (progn
  14144. (org-agenda-prepare-buffers
  14145. (and buffer-file-name (list buffer-file-name)))
  14146. (setq res (org-scan-tags func matcher todo-only start-level)))
  14147. ;; Get the right scope
  14148. (cond
  14149. ((and scope (listp scope) (symbolp (car scope)))
  14150. (setq scope (eval scope)))
  14151. ((eq scope 'agenda)
  14152. (setq scope (org-agenda-files t)))
  14153. ((eq scope 'agenda-with-archives)
  14154. (setq scope (org-agenda-files t))
  14155. (setq scope (org-add-archive-files scope)))
  14156. ((eq scope 'file)
  14157. (setq scope (and buffer-file-name (list buffer-file-name))))
  14158. ((eq scope 'file-with-archives)
  14159. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14160. (org-agenda-prepare-buffers scope)
  14161. (dolist (file scope)
  14162. (with-current-buffer (org-find-base-buffer-visiting file)
  14163. (save-excursion
  14164. (save-restriction
  14165. (widen)
  14166. (goto-char (point-min))
  14167. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14168. res)))
  14169. ;;; Properties API
  14170. (defconst org-special-properties
  14171. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14172. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14173. "The special properties valid in Org mode.
  14174. These are properties that are not defined in the property drawer,
  14175. but in some other way.")
  14176. (defconst org-default-properties
  14177. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14178. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14179. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14180. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14181. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14182. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14183. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14184. "Some properties that are used by Org mode for various purposes.
  14185. Being in this list makes sure that they are offered for completion.")
  14186. (defun org--update-property-plist (key val props)
  14187. "Associate KEY to VAL in alist PROPS.
  14188. Modifications are made by side-effect. Return new alist."
  14189. (let* ((appending (string= (substring key -1) "+"))
  14190. (key (if appending (substring key 0 -1) key))
  14191. (old (assoc-string key props t)))
  14192. (if (not old) (cons (cons key val) props)
  14193. (setcdr old (if appending (concat (cdr old) " " val) val))
  14194. props)))
  14195. (defun org-get-property-block (&optional beg force)
  14196. "Return the (beg . end) range of the body of the property drawer.
  14197. BEG is the beginning of the current subtree, or of the part
  14198. before the first headline. If it is not given, it will be found.
  14199. If the drawer does not exist, create it if FORCE is non-nil, or
  14200. return nil."
  14201. (org-with-wide-buffer
  14202. (when beg (goto-char beg))
  14203. (unless (org-before-first-heading-p)
  14204. (let ((beg (cond (beg)
  14205. ((or (not (featurep 'org-inlinetask))
  14206. (org-inlinetask-in-task-p))
  14207. (org-back-to-heading t))
  14208. (t (org-with-limited-levels (org-back-to-heading t))))))
  14209. (forward-line)
  14210. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14211. (cond ((looking-at org-property-drawer-re)
  14212. (forward-line)
  14213. (cons (point) (progn (goto-char (match-end 0))
  14214. (line-beginning-position))))
  14215. (force
  14216. (goto-char beg)
  14217. (org-insert-property-drawer)
  14218. (let ((pos (save-excursion (search-forward ":END:")
  14219. (line-beginning-position))))
  14220. (cons pos pos))))))))
  14221. (defun org-at-property-p ()
  14222. "Non-nil when point is inside a property drawer.
  14223. See `org-property-re' for match data, if applicable."
  14224. (save-excursion
  14225. (beginning-of-line)
  14226. (and (looking-at org-property-re)
  14227. (let ((property-drawer (save-match-data (org-get-property-block))))
  14228. (and property-drawer
  14229. (>= (point) (car property-drawer))
  14230. (< (point) (cdr property-drawer)))))))
  14231. (defun org-property-action ()
  14232. "Do an action on properties."
  14233. (interactive)
  14234. (unless (org-at-property-p) (user-error "Not at a property"))
  14235. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14236. (let ((c (read-char-exclusive)))
  14237. (case c
  14238. (?s (call-interactively #'org-set-property))
  14239. (?d (call-interactively #'org-delete-property))
  14240. (?D (call-interactively #'org-delete-property-globally))
  14241. (?c (call-interactively #'org-compute-property-at-point))
  14242. (otherwise (user-error "No such property action %c" c)))))
  14243. (defun org-inc-effort ()
  14244. "Increment the value of the effort property in the current entry."
  14245. (interactive)
  14246. (org-set-effort nil t))
  14247. (defvar org-clock-effort) ; Defined in org-clock.el.
  14248. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14249. (defun org-set-effort (&optional value increment)
  14250. "Set the effort property of the current entry.
  14251. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14252. 10th allowed value.
  14253. When INCREMENT is non-nil, set the property to the next allowed value."
  14254. (interactive "P")
  14255. (if (equal value 0) (setq value 10))
  14256. (let* ((completion-ignore-case t)
  14257. (prop org-effort-property)
  14258. (cur (org-entry-get nil prop))
  14259. (allowed (org-property-get-allowed-values nil prop 'table))
  14260. (existing (mapcar 'list (org-property-values prop)))
  14261. (heading (nth 4 (org-heading-components)))
  14262. rpl
  14263. (val (cond
  14264. ((stringp value) value)
  14265. ((and allowed (integerp value))
  14266. (or (car (nth (1- value) allowed))
  14267. (car (org-last allowed))))
  14268. ((and allowed increment)
  14269. (or (caadr (member (list cur) allowed))
  14270. (user-error "Allowed effort values are not set")))
  14271. (allowed
  14272. (message "Select 1-9,0, [RET%s]: %s"
  14273. (if cur (concat "=" cur) "")
  14274. (mapconcat 'car allowed " "))
  14275. (setq rpl (read-char-exclusive))
  14276. (if (equal rpl ?\r)
  14277. cur
  14278. (setq rpl (- rpl ?0))
  14279. (if (equal rpl 0) (setq rpl 10))
  14280. (if (and (> rpl 0) (<= rpl (length allowed)))
  14281. (car (nth (1- rpl) allowed))
  14282. (org-completing-read "Effort: " allowed nil))))
  14283. (t
  14284. (let (org-completion-use-ido org-completion-use-iswitchb)
  14285. (org-completing-read
  14286. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14287. (concat "[" cur "]") "")
  14288. ": ")
  14289. existing nil nil "" nil cur))))))
  14290. (unless (equal (org-entry-get nil prop) val)
  14291. (org-entry-put nil prop val))
  14292. (org-refresh-property
  14293. '((effort . identity)
  14294. (effort-minutes . org-duration-string-to-minutes))
  14295. val)
  14296. (when (string= heading org-clock-current-task)
  14297. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14298. (org-clock-update-mode-line))
  14299. (message "%s is now %s" prop val)))
  14300. (defun org-entry-properties (&optional pom which)
  14301. "Get all properties of the current entry.
  14302. When POM is a buffer position, get all properties from the entry
  14303. there instead.
  14304. This includes the TODO keyword, the tags, time strings for
  14305. deadline, scheduled, and clocking, and any additional properties
  14306. defined in the entry.
  14307. If WHICH is nil or `all', get all properties. If WHICH is
  14308. `special' or `standard', only get that subclass. If WHICH is
  14309. a string, only get that property.
  14310. Return value is an alist. Keys are properties, as upcased
  14311. strings."
  14312. (org-with-point-at pom
  14313. (when (and (derived-mode-p 'org-mode)
  14314. (ignore-errors (org-back-to-heading t)))
  14315. (catch 'exit
  14316. (let* ((beg (point))
  14317. (specific (and (stringp which) (upcase which)))
  14318. (which (cond ((not specific) which)
  14319. ((member specific org-special-properties) 'special)
  14320. (t 'standard)))
  14321. props)
  14322. ;; Get the special properties, like TODO and TAGS.
  14323. (when (memq which '(nil all special))
  14324. (when (or (not specific) (string= specific "CLOCKSUM"))
  14325. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14326. (when clocksum
  14327. (push (cons "CLOCKSUM"
  14328. (org-columns-number-to-string
  14329. (/ (float clocksum) 60.) 'add_times))
  14330. props)))
  14331. (when specific (throw 'exit props)))
  14332. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14333. (let ((clocksumt (get-text-property (point)
  14334. :org-clock-minutes-today)))
  14335. (when clocksumt
  14336. (push (cons "CLOCKSUM_T"
  14337. (org-columns-number-to-string
  14338. (/ (float clocksumt) 60.) 'add_times))
  14339. props)))
  14340. (when specific (throw 'exit props)))
  14341. (when (or (not specific) (string= specific "ITEM"))
  14342. (when (looking-at org-complex-heading-regexp)
  14343. (push (cons "ITEM"
  14344. (concat
  14345. (org-match-string-no-properties 1)
  14346. (let ((title (org-match-string-no-properties 4)))
  14347. (when (org-string-nw-p title)
  14348. (concat " " (org-remove-tabs title))))))
  14349. props))
  14350. (when specific (throw 'exit props)))
  14351. (when (or (not specific) (string= specific "TODO"))
  14352. (let ((case-fold-search nil))
  14353. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14354. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14355. (when specific (throw 'exit props)))
  14356. (when (or (not specific) (string= specific "PRIORITY"))
  14357. (when (looking-at org-priority-regexp)
  14358. (push (cons "PRIORITY" (org-match-string-no-properties 2))
  14359. props))
  14360. (when specific (throw 'exit props)))
  14361. (when (or (not specific) (string= specific "FILE"))
  14362. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14363. props)
  14364. (when specific (throw 'exit props)))
  14365. (when (or (not specific) (string= specific "TAGS"))
  14366. (let ((value (org-string-nw-p (org-get-tags-string))))
  14367. (when value (push (cons "TAGS" value) props)))
  14368. (when specific (throw 'exit props)))
  14369. (when (or (not specific) (string= specific "ALLTAGS"))
  14370. (let ((value (org-get-tags-at)))
  14371. (when value
  14372. (push (cons "ALLTAGS"
  14373. (format ":%s:" (mapconcat #'identity value ":")))
  14374. props)))
  14375. (when specific (throw 'exit props)))
  14376. (when (or (not specific) (string= specific "BLOCKED"))
  14377. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14378. (when specific (throw 'exit props)))
  14379. (when (or (not specific)
  14380. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14381. (forward-line)
  14382. (when (org-looking-at-p org-planning-line-re)
  14383. (end-of-line)
  14384. (let ((bol (line-beginning-position))
  14385. ;; Backward compatibility: time keywords used to
  14386. ;; be configurable (before 8.3). Make sure we
  14387. ;; get the correct keyword.
  14388. (key-assoc `(("CLOSED" . ,org-closed-string)
  14389. ("DEADLINE" . ,org-deadline-string)
  14390. ("SCHEDULED" . ,org-scheduled-string))))
  14391. (dolist (pair (if specific (list (assoc specific key-assoc))
  14392. key-assoc))
  14393. (save-excursion
  14394. (when (search-backward (cdr pair) bol t)
  14395. (goto-char (match-end 0))
  14396. (skip-chars-forward " \t")
  14397. (and (looking-at org-ts-regexp-both)
  14398. (push (cons (car pair)
  14399. (org-match-string-no-properties 0))
  14400. props)))))))
  14401. (when specific (throw 'exit props)))
  14402. (when (or (not specific)
  14403. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14404. (let ((find-ts
  14405. (lambda (end ts)
  14406. (let ((regexp (cond
  14407. ((string= specific "TIMESTAMP")
  14408. org-ts-regexp)
  14409. ((string= specific "TIMESTAMP_IA")
  14410. org-ts-regexp-inactive)
  14411. ((assoc "TIMESTAMP_IA" ts)
  14412. org-ts-regexp)
  14413. ((assoc "TIMESTAMP" ts)
  14414. org-ts-regexp-inactive)
  14415. (t org-ts-regexp-both))))
  14416. (catch 'next
  14417. (while (re-search-forward regexp end t)
  14418. (backward-char)
  14419. (let ((object (org-element-context)))
  14420. ;; Accept to match timestamps in node
  14421. ;; properties, too.
  14422. (when (memq (org-element-type object)
  14423. '(node-property timestamp))
  14424. (let ((type
  14425. (org-element-property :type object)))
  14426. (cond
  14427. ((and (memq type '(active active-range))
  14428. (not (equal specific "TIMESTAMP_IA")))
  14429. (unless (assoc "TIMESTAMP" ts)
  14430. (push (cons "TIMESTAMP"
  14431. (org-element-property
  14432. :raw-value object))
  14433. ts)
  14434. (when specific (throw 'exit ts))))
  14435. ((and (memq type '(inactive inactive-range))
  14436. (not (string= specific "TIMESTAMP")))
  14437. (unless (assoc "TIMESTAMP_IA" ts)
  14438. (push (cons "TIMESTAMP_IA"
  14439. (org-element-property
  14440. :raw-value object))
  14441. ts)
  14442. (when specific (throw 'exit ts))))))
  14443. ;; Both timestamp types are found,
  14444. ;; move to next part.
  14445. (when (= (length ts) 2) (throw 'next ts)))))
  14446. ts)))))
  14447. (goto-char beg)
  14448. ;; First look for timestamps within headline.
  14449. (let ((ts (funcall find-ts (line-end-position) nil)))
  14450. (if (= (length ts) 2) (setq props (nconc ts props))
  14451. (forward-line)
  14452. ;; Then find timestamps in the section, skipping
  14453. ;; planning line.
  14454. (when (org-looking-at-p org-planning-line-re)
  14455. (forward-line))
  14456. (let ((end (save-excursion (outline-next-heading))))
  14457. (setq props (nconc (funcall find-ts end ts) props))))))))
  14458. ;; Get the standard properties, like :PROP:.
  14459. (when (memq which '(nil all standard))
  14460. ;; If we are looking after a specific property, delegate
  14461. ;; to `org-entry-get', which is faster. However, make an
  14462. ;; exception for "CATEGORY", since it can be also set
  14463. ;; through keywords (i.e. #+CATEGORY).
  14464. (if (and specific (not (equal specific "CATEGORY")))
  14465. (let ((value (org-entry-get beg specific nil t)))
  14466. (throw 'exit (and value (list (cons specific value)))))
  14467. (let ((range (org-get-property-block beg)))
  14468. (when range
  14469. (let ((end (cdr range)) seen-base)
  14470. (goto-char (car range))
  14471. ;; Unlike to `org--update-property-plist', we
  14472. ;; handle the case where base values is found
  14473. ;; after its extension. We also forbid standard
  14474. ;; properties to be named as special properties.
  14475. (while (re-search-forward org-property-re end t)
  14476. (let* ((key (upcase (org-match-string-no-properties 2)))
  14477. (extendp (org-string-match-p "\\+\\'" key))
  14478. (key-base (if extendp (substring key 0 -1) key))
  14479. (value (org-match-string-no-properties 3)))
  14480. (cond
  14481. ((member-ignore-case key-base org-special-properties))
  14482. (extendp
  14483. (setq props
  14484. (org--update-property-plist key value props)))
  14485. ((member key seen-base))
  14486. (t (push key seen-base)
  14487. (let ((p (assoc-string key props t)))
  14488. (if p (setcdr p (concat value " " (cdr p)))
  14489. (push (cons key value) props))))))))))))
  14490. (unless (assoc "CATEGORY" props)
  14491. (push (cons "CATEGORY" (org-get-category beg)) props)
  14492. (when (string= specific "CATEGORY") (throw 'exit props)))
  14493. ;; Return value.
  14494. props)))))
  14495. (defun org-property--local-values (property literal-nil)
  14496. "Return value for PROPERTY in current entry.
  14497. Value is a list whose care is the base value for PROPERTY and cdr
  14498. a list of accumulated values. Return nil if neither is found in
  14499. the entry. Also return nil when PROPERTY is set to \"nil\",
  14500. unless LITERAL-NIL is non-nil."
  14501. (let ((range (org-get-property-block)))
  14502. (when range
  14503. (goto-char (car range))
  14504. (let* ((case-fold-search t)
  14505. (end (cdr range))
  14506. (value
  14507. ;; Base value.
  14508. (save-excursion
  14509. (let ((v (and (re-search-forward
  14510. (org-re-property property nil t) end t)
  14511. (org-match-string-no-properties 3))))
  14512. (list (if literal-nil v (org-not-nil v)))))))
  14513. ;; Find additional values.
  14514. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14515. (while (re-search-forward property+ end t)
  14516. (push (org-match-string-no-properties 3) value)))
  14517. ;; Return final values.
  14518. (and (not (equal value '(nil))) (nreverse value))))))
  14519. (defun org-entry-get (pom property &optional inherit literal-nil)
  14520. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14521. If INHERIT is non-nil and the entry does not have the property,
  14522. then also check higher levels of the hierarchy. If INHERIT is
  14523. the symbol `selective', use inheritance only if the setting in
  14524. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14525. If the property is present but empty, the return value is the
  14526. empty string. If the property is not present at all, nil is
  14527. returned. In any other case, return the value as a string.
  14528. Search is case-insensitive.
  14529. If LITERAL-NIL is set, return the string value \"nil\" as
  14530. a string, do not interpret it as the list atom nil. This is used
  14531. for inheritance when a \"nil\" value can supersede a non-nil
  14532. value higher up the hierarchy."
  14533. (org-with-point-at pom
  14534. (cond
  14535. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14536. ;; We need a special property. Use `org-entry-properties' to
  14537. ;; retrieve it, but specify the wanted property.
  14538. (cdr (assoc-string property (org-entry-properties nil property))))
  14539. ((and inherit
  14540. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14541. (org-entry-get-with-inheritance property literal-nil))
  14542. (t
  14543. (let* ((local (org-property--local-values property literal-nil))
  14544. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14545. (if literal-nil value (org-not-nil value)))))))
  14546. (defun org-property-or-variable-value (var &optional inherit)
  14547. "Check if there is a property fixing the value of VAR.
  14548. If yes, return this value. If not, return the current value of the variable."
  14549. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14550. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14551. (read prop)
  14552. (symbol-value var))))
  14553. (defun org-entry-delete (pom property)
  14554. "Delete PROPERTY from entry at point-or-marker POM.
  14555. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14556. non-nil when a property was removed."
  14557. (unless (member property org-special-properties)
  14558. (org-with-point-at pom
  14559. (let ((range (org-get-property-block)))
  14560. (when range
  14561. (let* ((begin (car range))
  14562. (origin (cdr range))
  14563. (end (copy-marker origin))
  14564. (re (org-re-property
  14565. (concat (regexp-quote property) "\\+?") t t)))
  14566. (goto-char begin)
  14567. (while (re-search-forward re end t)
  14568. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14569. ;; If drawer is empty, remove it altogether.
  14570. (when (= begin end)
  14571. (delete-region (line-beginning-position 0)
  14572. (line-beginning-position 2)))
  14573. ;; Return non-nil if some property was removed.
  14574. (prog1 (/= end origin) (set-marker end nil))))))))
  14575. ;; Multi-values properties are properties that contain multiple values
  14576. ;; These values are assumed to be single words, separated by whitespace.
  14577. (defun org-entry-add-to-multivalued-property (pom property value)
  14578. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14579. (let* ((old (org-entry-get pom property))
  14580. (values (and old (org-split-string old "[ \t]"))))
  14581. (setq value (org-entry-protect-space value))
  14582. (unless (member value values)
  14583. (setq values (append values (list value)))
  14584. (org-entry-put pom property
  14585. (mapconcat 'identity values " ")))))
  14586. (defun org-entry-remove-from-multivalued-property (pom property value)
  14587. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14588. (let* ((old (org-entry-get pom property))
  14589. (values (and old (org-split-string old "[ \t]"))))
  14590. (setq value (org-entry-protect-space value))
  14591. (when (member value values)
  14592. (setq values (delete value values))
  14593. (org-entry-put pom property
  14594. (mapconcat 'identity values " ")))))
  14595. (defun org-entry-member-in-multivalued-property (pom property value)
  14596. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14597. (let* ((old (org-entry-get pom property))
  14598. (values (and old (org-split-string old "[ \t]"))))
  14599. (setq value (org-entry-protect-space value))
  14600. (member value values)))
  14601. (defun org-entry-get-multivalued-property (pom property)
  14602. "Return a list of values in a multivalued property."
  14603. (let* ((value (org-entry-get pom property))
  14604. (values (and value (org-split-string value "[ \t]"))))
  14605. (mapcar 'org-entry-restore-space values)))
  14606. (defun org-entry-put-multivalued-property (pom property &rest values)
  14607. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14608. VALUES should be a list of strings. Spaces will be protected."
  14609. (org-entry-put pom property
  14610. (mapconcat 'org-entry-protect-space values " "))
  14611. (let* ((value (org-entry-get pom property))
  14612. (values (and value (org-split-string value "[ \t]"))))
  14613. (mapcar 'org-entry-restore-space values)))
  14614. (defun org-entry-protect-space (s)
  14615. "Protect spaces and newline in string S."
  14616. (while (string-match " " s)
  14617. (setq s (replace-match "%20" t t s)))
  14618. (while (string-match "\n" s)
  14619. (setq s (replace-match "%0A" t t s)))
  14620. s)
  14621. (defun org-entry-restore-space (s)
  14622. "Restore spaces and newline in string S."
  14623. (while (string-match "%20" s)
  14624. (setq s (replace-match " " t t s)))
  14625. (while (string-match "%0A" s)
  14626. (setq s (replace-match "\n" t t s)))
  14627. s)
  14628. (defvar org-entry-property-inherited-from (make-marker)
  14629. "Marker pointing to the entry from where a property was inherited.
  14630. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14631. location of the entry where the inheritance search matched. If there was
  14632. no match, the marker will point nowhere.
  14633. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14634. is set.")
  14635. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14636. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14637. The search will stop at the first ancestor which has the property defined.
  14638. If the value found is \"nil\", return nil to show that the property
  14639. should be considered as undefined (this is the meaning of nil here).
  14640. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14641. (move-marker org-entry-property-inherited-from nil)
  14642. (org-with-wide-buffer
  14643. (let (value)
  14644. (catch 'exit
  14645. (while t
  14646. (let ((v (org-property--local-values property literal-nil)))
  14647. (when v
  14648. (setq value
  14649. (concat (mapconcat #'identity (delq nil v) " ")
  14650. (and value " ")
  14651. value)))
  14652. (cond
  14653. ((car v)
  14654. (org-back-to-heading t)
  14655. (move-marker org-entry-property-inherited-from (point))
  14656. (throw 'exit nil))
  14657. ((org-up-heading-safe))
  14658. (t
  14659. (let ((global
  14660. (cdr (or (assoc-string property org-file-properties t)
  14661. (assoc-string property org-global-properties t)
  14662. (assoc-string property org-global-properties-fixed t)))))
  14663. (cond ((not global))
  14664. (value (setq value (concat global " " value)))
  14665. (t (setq value global))))
  14666. (throw 'exit nil))))))
  14667. (if literal-nil value (org-not-nil value)))))
  14668. (defvar org-property-changed-functions nil
  14669. "Hook called when the value of a property has changed.
  14670. Each hook function should accept two arguments, the name of the property
  14671. and the new value.")
  14672. (defun org-entry-put (pom property value)
  14673. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14674. If the value is nil, it is converted to the empty string. If
  14675. it is not a string, an error is raised.
  14676. PROPERTY can be any regular property (see
  14677. `org-special-properties'). It can also be \"TODO\",
  14678. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14679. For the last two properties, VALUE may have any of the special
  14680. values \"earlier\" and \"later\". The function then increases or
  14681. decreases scheduled or deadline date by one day."
  14682. (cond ((null value) (setq value ""))
  14683. ((not (stringp value)) (error "Properties values should be strings")))
  14684. (org-with-point-at pom
  14685. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14686. (org-back-to-heading t)
  14687. (org-with-limited-levels (org-back-to-heading t)))
  14688. (let ((beg (point)))
  14689. (cond
  14690. ((equal property "TODO")
  14691. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14692. ((not (member value org-todo-keywords-1))
  14693. (user-error "\"%s\" is not a valid TODO state" value)))
  14694. (org-todo value)
  14695. (org-set-tags nil 'align))
  14696. ((equal property "PRIORITY")
  14697. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14698. (org-set-tags nil 'align))
  14699. ((equal property "SCHEDULED")
  14700. (forward-line)
  14701. (if (and (org-looking-at-p org-planning-line-re)
  14702. (re-search-forward
  14703. org-scheduled-time-regexp (line-end-position) t))
  14704. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14705. ((string= value "later") (org-timestamp-change 1 'day))
  14706. ((string= value "") (org-schedule '(4)))
  14707. (t (org-schedule nil value)))
  14708. (if (member value '("earlier" "later" ""))
  14709. (call-interactively #'org-schedule)
  14710. (org-schedule nil value))))
  14711. ((equal property "DEADLINE")
  14712. (forward-line)
  14713. (if (and (org-looking-at-p org-planning-line-re)
  14714. (re-search-forward
  14715. org-deadline-time-regexp (line-end-position) t))
  14716. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14717. ((string= value "later") (org-timestamp-change 1 'day))
  14718. ((string= value "") (org-deadline '(4)))
  14719. (t (org-deadline nil value)))
  14720. (if (member value '("earlier" "later" ""))
  14721. (call-interactively #'org-deadline)
  14722. (org-deadline nil value))))
  14723. ((member property org-special-properties)
  14724. (error "The %s property cannot be set with `org-entry-put'" property))
  14725. (t
  14726. (let* ((range (org-get-property-block beg 'force))
  14727. (end (cdr range))
  14728. (case-fold-search t))
  14729. (goto-char (car range))
  14730. (if (re-search-forward (org-re-property property nil t) end t)
  14731. (progn (delete-region (match-beginning 0) (match-end 0))
  14732. (goto-char (match-beginning 0)))
  14733. (goto-char end)
  14734. (insert "\n")
  14735. (backward-char))
  14736. (insert ":" property ":")
  14737. (when value (insert " " value))
  14738. (org-indent-line)))))
  14739. (run-hook-with-args 'org-property-changed-functions property value)))
  14740. (defun org-buffer-property-keys (&optional specials defaults columns)
  14741. "Get all property keys in the current buffer.
  14742. When SPECIALS is non-nil, also list the special properties that
  14743. reflect things like tags and TODO state.
  14744. When DEFAULTS is non-nil, also include properties that has
  14745. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14746. DESCRIPTION, LOCATION, and LOGGING and others.
  14747. When COLUMNS in non-nil, also include property names given in
  14748. COLUMN formats in the current buffer."
  14749. (let ((case-fold-search t)
  14750. (props (append
  14751. (and specials org-special-properties)
  14752. (and defaults (cons org-effort-property org-default-properties))
  14753. nil)))
  14754. (org-with-wide-buffer
  14755. (goto-char (point-min))
  14756. (while (re-search-forward org-property-start-re nil t)
  14757. (let ((range (org-get-property-block)))
  14758. (catch 'skip
  14759. (unless range
  14760. (when (and (not (org-before-first-heading-p))
  14761. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14762. (line-beginning-position))))
  14763. (org-get-property-block nil t))
  14764. (throw 'skip nil))
  14765. (goto-char (car range))
  14766. (let ((begin (car range))
  14767. (end (cdr range)))
  14768. ;; Make sure that found property block is not located
  14769. ;; before current point, as it would generate an infloop.
  14770. ;; It can happen, for example, in the following
  14771. ;; situation:
  14772. ;;
  14773. ;; * Headline
  14774. ;; :PROPERTIES:
  14775. ;; ...
  14776. ;; :END:
  14777. ;; *************** Inlinetask
  14778. ;; #+BEGIN_EXAMPLE
  14779. ;; :PROPERTIES:
  14780. ;; #+END_EXAMPLE
  14781. ;;
  14782. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14783. (while (< (point) end)
  14784. (let ((p (progn (looking-at org-property-re)
  14785. (org-match-string-no-properties 2))))
  14786. ;; Only add true property name, not extension symbol.
  14787. (add-to-list 'props
  14788. (if (not (org-string-match-p "\\+\\'" p)) p
  14789. (substring p 0 -1))))
  14790. (forward-line))))
  14791. (outline-next-heading)))
  14792. (when columns
  14793. (goto-char (point-min))
  14794. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14795. (let ((element (org-element-at-point)))
  14796. (when (memq (org-element-type element) '(keyword node-property))
  14797. (let ((value (org-element-property :value element))
  14798. (start 0))
  14799. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14800. (setq start (match-end 0))
  14801. (let ((p (org-match-string-no-properties 1 value)))
  14802. (unless (member-ignore-case p org-special-properties)
  14803. (add-to-list 'props p))))))))))
  14804. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14805. (defun org-property-values (key)
  14806. "List all non-nil values of property KEY in current buffer."
  14807. (org-with-wide-buffer
  14808. (goto-char (point-min))
  14809. (let ((case-fold-search t)
  14810. (re (org-re-property key))
  14811. values)
  14812. (while (re-search-forward re nil t)
  14813. (add-to-list 'values (org-entry-get (point) key)))
  14814. values)))
  14815. (defun org-insert-property-drawer ()
  14816. "Insert a property drawer into the current entry."
  14817. (org-with-wide-buffer
  14818. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14819. (org-back-to-heading t)
  14820. (org-with-limited-levels (org-back-to-heading t)))
  14821. (forward-line)
  14822. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14823. (unless (org-looking-at-p org-property-drawer-re)
  14824. (let ((inhibit-read-only t))
  14825. (unless (bolp) (insert "\n"))
  14826. (let ((begin (point)))
  14827. (insert ":PROPERTIES:\n:END:\n")
  14828. (org-indent-region begin (point)))))))
  14829. (defun org-insert-drawer (&optional arg drawer)
  14830. "Insert a drawer at point.
  14831. When optional argument ARG is non-nil, insert a property drawer.
  14832. Optional argument DRAWER, when non-nil, is a string representing
  14833. drawer's name. Otherwise, the user is prompted for a name.
  14834. If a region is active, insert the drawer around that region
  14835. instead.
  14836. Point is left between drawer's boundaries."
  14837. (interactive "P")
  14838. (let* ((drawer (if arg "PROPERTIES"
  14839. (or drawer (read-from-minibuffer "Drawer: ")))))
  14840. (cond
  14841. ;; With C-u, fall back on `org-insert-property-drawer'
  14842. (arg (org-insert-property-drawer))
  14843. ;; Check validity of suggested drawer's name.
  14844. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14845. (user-error "Invalid drawer name"))
  14846. ;; With an active region, insert a drawer at point.
  14847. ((not (org-region-active-p))
  14848. (progn
  14849. (unless (bolp) (insert "\n"))
  14850. (insert (format ":%s:\n\n:END:\n" drawer))
  14851. (forward-line -2)))
  14852. ;; Otherwise, insert the drawer at point
  14853. (t
  14854. (let ((rbeg (region-beginning))
  14855. (rend (copy-marker (region-end))))
  14856. (unwind-protect
  14857. (progn
  14858. (goto-char rbeg)
  14859. (beginning-of-line)
  14860. (when (save-excursion
  14861. (re-search-forward org-outline-regexp-bol rend t))
  14862. (user-error "Drawers cannot contain headlines"))
  14863. ;; Position point at the beginning of the first
  14864. ;; non-blank line in region. Insert drawer's opening
  14865. ;; there, then indent it.
  14866. (org-skip-whitespace)
  14867. (beginning-of-line)
  14868. (insert ":" drawer ":\n")
  14869. (forward-line -1)
  14870. (indent-for-tab-command)
  14871. ;; Move point to the beginning of the first blank line
  14872. ;; after the last non-blank line in region. Insert
  14873. ;; drawer's closing, then indent it.
  14874. (goto-char rend)
  14875. (skip-chars-backward " \r\t\n")
  14876. (insert "\n:END:")
  14877. (deactivate-mark t)
  14878. (indent-for-tab-command)
  14879. (unless (eolp) (insert "\n")))
  14880. ;; Clear marker, whatever the outcome of insertion is.
  14881. (set-marker rend nil)))))))
  14882. (defvar org-property-set-functions-alist nil
  14883. "Property set function alist.
  14884. Each entry should have the following format:
  14885. (PROPERTY . READ-FUNCTION)
  14886. The read function will be called with the same argument as
  14887. `org-completing-read'.")
  14888. (defun org-set-property-function (property)
  14889. "Get the function that should be used to set PROPERTY.
  14890. This is computed according to `org-property-set-functions-alist'."
  14891. (or (cdr (assoc property org-property-set-functions-alist))
  14892. 'org-completing-read))
  14893. (defun org-read-property-value (property)
  14894. "Read PROPERTY value from user."
  14895. (let* ((completion-ignore-case t)
  14896. (allowed (org-property-get-allowed-values nil property 'table))
  14897. (cur (org-entry-get nil property))
  14898. (prompt (concat property " value"
  14899. (if (and cur (string-match "\\S-" cur))
  14900. (concat " [" cur "]") "") ": "))
  14901. (set-function (org-set-property-function property))
  14902. (val (if allowed
  14903. (funcall set-function prompt allowed nil
  14904. (not (get-text-property 0 'org-unrestricted
  14905. (caar allowed))))
  14906. (let (org-completion-use-ido org-completion-use-iswitchb)
  14907. (funcall set-function prompt
  14908. (mapcar 'list (org-property-values property))
  14909. nil nil "" nil cur)))))
  14910. (org-trim val)))
  14911. (defvar org-last-set-property nil)
  14912. (defvar org-last-set-property-value nil)
  14913. (defun org-read-property-name ()
  14914. "Read a property name."
  14915. (let ((completion-ignore-case t)
  14916. (default-prop (or (and (org-at-property-p)
  14917. (org-match-string-no-properties 2))
  14918. org-last-set-property)))
  14919. (org-completing-read
  14920. (concat "Property"
  14921. (if default-prop (concat " [" default-prop "]") "")
  14922. ": ")
  14923. (mapcar #'list (org-buffer-property-keys nil t t))
  14924. nil nil nil nil default-prop)))
  14925. (defun org-set-property-and-value (use-last)
  14926. "Allow to set [PROPERTY]: [value] direction from prompt.
  14927. When use-default, don't even ask, just use the last
  14928. \"[PROPERTY]: [value]\" string from the history."
  14929. (interactive "P")
  14930. (let* ((completion-ignore-case t)
  14931. (pv (or (and use-last org-last-set-property-value)
  14932. (org-completing-read
  14933. "Enter a \"[Property]: [value]\" pair: "
  14934. nil nil nil nil nil
  14935. org-last-set-property-value)))
  14936. prop val)
  14937. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14938. (setq prop (match-string 1 pv)
  14939. val (match-string 2 pv))
  14940. (org-set-property prop val))))
  14941. (defun org-set-property (property value)
  14942. "In the current entry, set PROPERTY to VALUE.
  14943. When called interactively, this will prompt for a property name, offering
  14944. completion on existing and default properties. And then it will prompt
  14945. for a value, offering completion either on allowed values (via an inherited
  14946. xxx_ALL property) or on existing values in other instances of this property
  14947. in the current file."
  14948. (interactive (list nil nil))
  14949. (let* ((property (or property (org-read-property-name)))
  14950. (value (or value (org-read-property-value property)))
  14951. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14952. (setq org-last-set-property property)
  14953. (setq org-last-set-property-value (concat property ": " value))
  14954. ;; Possibly postprocess the inserted value:
  14955. (when fn (setq value (funcall fn value)))
  14956. (unless (equal (org-entry-get nil property) value)
  14957. (org-entry-put nil property value))))
  14958. (defun org-find-property (property &optional value)
  14959. "Find first entry in buffer that sets PROPERTY.
  14960. When optional argument VALUE is non-nil, only consider an entry
  14961. if it contains PROPERTY set to this value. If PROPERTY should be
  14962. explicitly set to nil, use string \"nil\" for VALUE.
  14963. Return position where the entry begins, or nil if there is no
  14964. such entry. If narrowing is in effect, only search the visible
  14965. part of the buffer."
  14966. (save-excursion
  14967. (goto-char (point-min))
  14968. (let ((case-fold-search t)
  14969. (re (org-re-property property nil (not value) value)))
  14970. (catch 'exit
  14971. (while (re-search-forward re nil t)
  14972. (when (if value (org-at-property-p)
  14973. (org-entry-get (point) property nil t))
  14974. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14975. (defun org-delete-property (property)
  14976. "In the current entry, delete PROPERTY."
  14977. (interactive
  14978. (let* ((completion-ignore-case t)
  14979. (cat (org-entry-get (point) "CATEGORY"))
  14980. (props0 (org-entry-properties nil 'standard))
  14981. (props (if cat props0
  14982. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14983. (prop (if (< 1 (length props))
  14984. (org-icompleting-read "Property: " props nil t)
  14985. (caar props))))
  14986. (list prop)))
  14987. (if (not property)
  14988. (message "No property to delete in this entry")
  14989. (org-entry-delete nil property)
  14990. (message "Property \"%s\" deleted" property)))
  14991. (defun org-delete-property-globally (property)
  14992. "Remove PROPERTY globally, from all entries.
  14993. This function ignores narrowing, if any."
  14994. (interactive
  14995. (let* ((completion-ignore-case t)
  14996. (prop (org-icompleting-read
  14997. "Globally remove property: "
  14998. (mapcar #'list (org-buffer-property-keys)))))
  14999. (list prop)))
  15000. (org-with-wide-buffer
  15001. (goto-char (point-min))
  15002. (let ((count 0)
  15003. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15004. (while (re-search-forward re nil t)
  15005. (when (org-entry-delete (point) property) (incf count)))
  15006. (message "Property \"%s\" removed from %d entries" property count))))
  15007. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15008. (defun org-compute-property-at-point ()
  15009. "Compute the property at point.
  15010. This looks for an enclosing column format, extracts the operator and
  15011. then applies it to the property in the column format's scope."
  15012. (interactive)
  15013. (unless (org-at-property-p)
  15014. (user-error "Not at a property"))
  15015. (let ((prop (org-match-string-no-properties 2)))
  15016. (org-columns-get-format-and-top-level)
  15017. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15018. (user-error "No operator defined for property %s" prop))
  15019. (org-columns-compute prop)))
  15020. (defvar org-property-allowed-value-functions nil
  15021. "Hook for functions supplying allowed values for a specific property.
  15022. The functions must take a single argument, the name of the property, and
  15023. return a flat list of allowed values. If \":ETC\" is one of
  15024. the values, this means that these values are intended as defaults for
  15025. completion, but that other values should be allowed too.
  15026. The functions must return nil if they are not responsible for this
  15027. property.")
  15028. (defun org-property-get-allowed-values (pom property &optional table)
  15029. "Get allowed values for the property PROPERTY.
  15030. When TABLE is non-nil, return an alist that can directly be used for
  15031. completion."
  15032. (let (vals)
  15033. (cond
  15034. ((equal property "TODO")
  15035. (setq vals (org-with-point-at pom
  15036. (append org-todo-keywords-1 '("")))))
  15037. ((equal property "PRIORITY")
  15038. (let ((n org-lowest-priority))
  15039. (while (>= n org-highest-priority)
  15040. (push (char-to-string n) vals)
  15041. (setq n (1- n)))))
  15042. ((equal property "CATEGORY"))
  15043. ((member property org-special-properties))
  15044. ((setq vals (run-hook-with-args-until-success
  15045. 'org-property-allowed-value-functions property)))
  15046. (t
  15047. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15048. (when (and vals (string-match "\\S-" vals))
  15049. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15050. (setq vals (mapcar (lambda (x)
  15051. (cond ((stringp x) x)
  15052. ((numberp x) (number-to-string x))
  15053. ((symbolp x) (symbol-name x))
  15054. (t "???")))
  15055. vals)))))
  15056. (when (member ":ETC" vals)
  15057. (setq vals (remove ":ETC" vals))
  15058. (org-add-props (car vals) '(org-unrestricted t)))
  15059. (if table (mapcar 'list vals) vals)))
  15060. (defun org-property-previous-allowed-value (&optional previous)
  15061. "Switch to the next allowed value for this property."
  15062. (interactive)
  15063. (org-property-next-allowed-value t))
  15064. (defun org-property-next-allowed-value (&optional previous)
  15065. "Switch to the next allowed value for this property."
  15066. (interactive)
  15067. (unless (org-at-property-p)
  15068. (user-error "Not at a property"))
  15069. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15070. (key (match-string 2))
  15071. (value (match-string 3))
  15072. (allowed (or (org-property-get-allowed-values (point) key)
  15073. (and (member value '("[ ]" "[-]" "[X]"))
  15074. '("[ ]" "[X]"))))
  15075. (heading (save-match-data (nth 4 (org-heading-components))))
  15076. nval)
  15077. (unless allowed
  15078. (user-error "Allowed values for this property have not been defined"))
  15079. (if previous (setq allowed (reverse allowed)))
  15080. (if (member value allowed)
  15081. (setq nval (car (cdr (member value allowed)))))
  15082. (setq nval (or nval (car allowed)))
  15083. (if (equal nval value)
  15084. (user-error "Only one allowed value for this property"))
  15085. (org-at-property-p)
  15086. (replace-match (concat " :" key ": " nval) t t)
  15087. (org-indent-line)
  15088. (beginning-of-line 1)
  15089. (skip-chars-forward " \t")
  15090. (when (equal prop org-effort-property)
  15091. (org-refresh-property
  15092. '((effort . identity)
  15093. (effort-minutes . org-duration-string-to-minutes))
  15094. nval)
  15095. (when (string= org-clock-current-task heading)
  15096. (setq org-clock-effort nval)
  15097. (org-clock-update-mode-line)))
  15098. (run-hook-with-args 'org-property-changed-functions key nval)))
  15099. (defun org-find-olp (path &optional this-buffer)
  15100. "Return a marker pointing to the entry at outline path OLP.
  15101. If anything goes wrong, throw an error.
  15102. You can wrap this call to catch the error like this:
  15103. (condition-case msg
  15104. (org-mobile-locate-entry (match-string 4))
  15105. (error (nth 1 msg)))
  15106. The return value will then be either a string with the error message,
  15107. or a marker if everything is OK.
  15108. If THIS-BUFFER is set, the outline path does not contain a file,
  15109. only headings."
  15110. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15111. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15112. (level 1)
  15113. (lmin 1)
  15114. (lmax 1)
  15115. limit re end found pos heading cnt flevel)
  15116. (unless buffer (error "File not found :%s" file))
  15117. (with-current-buffer buffer
  15118. (save-excursion
  15119. (save-restriction
  15120. (widen)
  15121. (setq limit (point-max))
  15122. (goto-char (point-min))
  15123. (dolist (heading path)
  15124. (setq re (format org-complex-heading-regexp-format
  15125. (regexp-quote heading)))
  15126. (setq cnt 0 pos (point))
  15127. (while (re-search-forward re end t)
  15128. (setq level (- (match-end 1) (match-beginning 1)))
  15129. (if (and (>= level lmin) (<= level lmax))
  15130. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15131. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15132. lmax heading))
  15133. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15134. lmax heading))
  15135. (goto-char found)
  15136. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15137. (setq end (save-excursion (org-end-of-subtree t t))))
  15138. (when (org-at-heading-p)
  15139. (point-marker)))))))
  15140. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15141. "Find node HEADING in BUFFER.
  15142. Return a marker to the heading if it was found, or nil if not.
  15143. If POS-ONLY is set, return just the position instead of a marker.
  15144. The heading text must match exact, but it may have a TODO keyword,
  15145. a priority cookie and tags in the standard locations."
  15146. (with-current-buffer (or buffer (current-buffer))
  15147. (save-excursion
  15148. (save-restriction
  15149. (widen)
  15150. (goto-char (point-min))
  15151. (let (case-fold-search)
  15152. (if (re-search-forward
  15153. (format org-complex-heading-regexp-format
  15154. (regexp-quote heading)) nil t)
  15155. (if pos-only
  15156. (match-beginning 0)
  15157. (move-marker (make-marker) (match-beginning 0)))))))))
  15158. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15159. "Find Org node headline HEADING in all .org files in directory DIR.
  15160. When the target headline is found, return a marker to this location."
  15161. (let ((files (directory-files (or dir default-directory)
  15162. t "\\`[^.#].*\\.org\\'"))
  15163. visiting m buffer)
  15164. (catch 'found
  15165. (dolist (file files)
  15166. (message "trying %s" file)
  15167. (setq visiting (org-find-base-buffer-visiting file))
  15168. (setq buffer (or visiting (find-file-noselect file)))
  15169. (setq m (org-find-exact-headline-in-buffer
  15170. heading buffer))
  15171. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15172. (and m (throw 'found m))))))
  15173. (defun org-find-entry-with-id (ident)
  15174. "Locate the entry that contains the ID property with exact value IDENT.
  15175. IDENT can be a string, a symbol or a number, this function will search for
  15176. the string representation of it.
  15177. Return the position where this entry starts, or nil if there is no such entry."
  15178. (interactive "sID: ")
  15179. (let ((id (cond
  15180. ((stringp ident) ident)
  15181. ((symbolp ident) (symbol-name ident))
  15182. ((numberp ident) (number-to-string ident))
  15183. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15184. (org-with-wide-buffer (org-find-property "ID" id))))
  15185. ;;;; Timestamps
  15186. (defvar org-last-changed-timestamp nil)
  15187. (defvar org-last-inserted-timestamp nil
  15188. "The last time stamp inserted with `org-insert-time-stamp'.")
  15189. (defvar org-ts-what) ; dynamically scoped parameter
  15190. (defun org-time-stamp (arg &optional inactive)
  15191. "Prompt for a date/time and insert a time stamp.
  15192. If the user specifies a time like HH:MM or if this command is
  15193. called with at least one prefix argument, the time stamp contains
  15194. the date and the time. Otherwise, only the date is included.
  15195. All parts of a date not specified by the user are filled in from
  15196. the timestamp at point, if any, or the current date/time
  15197. otherwise.
  15198. If there is already a timestamp at the cursor, it is replaced.
  15199. With two universal prefix arguments, insert an active timestamp
  15200. with the current time without prompting the user.
  15201. When called from lisp, the timestamp is inactive if INACTIVE is
  15202. non-nil."
  15203. (interactive "P")
  15204. (let* ((ts (cond
  15205. ((org-at-date-range-p t)
  15206. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15207. ((org-at-timestamp-p t) (match-string 0))))
  15208. ;; Default time is either the timestamp at point or today.
  15209. ;; When entering a range, only the range start is considered.
  15210. (default-time (if (not ts) (current-time)
  15211. (apply #'encode-time (org-parse-time-string ts))))
  15212. (default-input (and ts (org-get-compact-tod ts)))
  15213. (repeater (and ts
  15214. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15215. (match-string 0 ts)))
  15216. org-time-was-given
  15217. org-end-time-was-given
  15218. (time
  15219. (and (if (equal arg '(16)) (current-time)
  15220. ;; Preserve `this-command' and `last-command'.
  15221. (let ((this-command this-command)
  15222. (last-command last-command))
  15223. (org-read-date
  15224. arg 'totime nil nil default-time default-input
  15225. inactive))))))
  15226. (cond
  15227. ((and ts
  15228. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15229. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15230. (insert "--")
  15231. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15232. (ts
  15233. ;; Make sure we're on a timestamp. When in the middle of a date
  15234. ;; range, move arbitrarily to range end.
  15235. (unless (org-at-timestamp-p t)
  15236. (skip-chars-forward "-")
  15237. (org-at-timestamp-p t))
  15238. (replace-match "")
  15239. (setq org-last-changed-timestamp
  15240. (org-insert-time-stamp
  15241. time (or org-time-was-given arg)
  15242. inactive nil nil (list org-end-time-was-given)))
  15243. (when repeater
  15244. (backward-char)
  15245. (insert " " repeater)
  15246. (setq org-last-changed-timestamp
  15247. (concat (substring org-last-inserted-timestamp 0 -1)
  15248. " " repeater ">")))
  15249. (message "Timestamp updated"))
  15250. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15251. (t (org-insert-time-stamp
  15252. time (or org-time-was-given arg) inactive nil nil
  15253. (list org-end-time-was-given))))))
  15254. ;; FIXME: can we use this for something else, like computing time differences?
  15255. (defun org-get-compact-tod (s)
  15256. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15257. (let* ((t1 (match-string 1 s))
  15258. (h1 (string-to-number (match-string 2 s)))
  15259. (m1 (string-to-number (match-string 3 s)))
  15260. (t2 (and (match-end 4) (match-string 5 s)))
  15261. (h2 (and t2 (string-to-number (match-string 6 s))))
  15262. (m2 (and t2 (string-to-number (match-string 7 s))))
  15263. dh dm)
  15264. (if (not t2)
  15265. t1
  15266. (setq dh (- h2 h1) dm (- m2 m1))
  15267. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15268. (concat t1 "+" (number-to-string dh)
  15269. (and (/= 0 dm) (format ":%02d" dm)))))))
  15270. (defun org-time-stamp-inactive (&optional arg)
  15271. "Insert an inactive time stamp.
  15272. An inactive time stamp is enclosed in square brackets instead of angle
  15273. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15274. does not link to the calendar and cannot be changed with the S-cursor keys.
  15275. So these are more for recording a certain time/date."
  15276. (interactive "P")
  15277. (org-time-stamp arg 'inactive))
  15278. (defvar org-date-ovl (make-overlay 1 1))
  15279. (overlay-put org-date-ovl 'face 'org-date-selected)
  15280. (org-detach-overlay org-date-ovl)
  15281. (defvar org-ans1) ; dynamically scoped parameter
  15282. (defvar org-ans2) ; dynamically scoped parameter
  15283. (defvar org-plain-time-of-day-regexp) ; defined below
  15284. (defvar org-overriding-default-time nil) ; dynamically scoped
  15285. (defvar org-read-date-overlay nil)
  15286. (defvar org-dcst nil) ; dynamically scoped
  15287. (defvar org-read-date-history nil)
  15288. (defvar org-read-date-final-answer nil)
  15289. (defvar org-read-date-analyze-futurep nil)
  15290. (defvar org-read-date-analyze-forced-year nil)
  15291. (defvar org-read-date-inactive)
  15292. (defvar org-read-date-minibuffer-local-map
  15293. (let* ((map (make-sparse-keymap)))
  15294. (set-keymap-parent map minibuffer-local-map)
  15295. (org-defkey map (kbd ".")
  15296. (lambda () (interactive)
  15297. ;; Are we at the beginning of the prompt?
  15298. (if (looking-back "^[^:]+: ")
  15299. (org-eval-in-calendar '(calendar-goto-today))
  15300. (insert "."))))
  15301. (org-defkey map (kbd "C-.")
  15302. (lambda () (interactive)
  15303. (org-eval-in-calendar '(calendar-goto-today))))
  15304. (org-defkey map [(meta shift left)]
  15305. (lambda () (interactive)
  15306. (org-eval-in-calendar '(calendar-backward-month 1))))
  15307. (org-defkey map [(meta shift right)]
  15308. (lambda () (interactive)
  15309. (org-eval-in-calendar '(calendar-forward-month 1))))
  15310. (org-defkey map [(meta shift up)]
  15311. (lambda () (interactive)
  15312. (org-eval-in-calendar '(calendar-backward-year 1))))
  15313. (org-defkey map [(meta shift down)]
  15314. (lambda () (interactive)
  15315. (org-eval-in-calendar '(calendar-forward-year 1))))
  15316. (org-defkey map [?\e (shift left)]
  15317. (lambda () (interactive)
  15318. (org-eval-in-calendar '(calendar-backward-month 1))))
  15319. (org-defkey map [?\e (shift right)]
  15320. (lambda () (interactive)
  15321. (org-eval-in-calendar '(calendar-forward-month 1))))
  15322. (org-defkey map [?\e (shift up)]
  15323. (lambda () (interactive)
  15324. (org-eval-in-calendar '(calendar-backward-year 1))))
  15325. (org-defkey map [?\e (shift down)]
  15326. (lambda () (interactive)
  15327. (org-eval-in-calendar '(calendar-forward-year 1))))
  15328. (org-defkey map [(shift up)]
  15329. (lambda () (interactive)
  15330. (org-eval-in-calendar '(calendar-backward-week 1))))
  15331. (org-defkey map [(shift down)]
  15332. (lambda () (interactive)
  15333. (org-eval-in-calendar '(calendar-forward-week 1))))
  15334. (org-defkey map [(shift left)]
  15335. (lambda () (interactive)
  15336. (org-eval-in-calendar '(calendar-backward-day 1))))
  15337. (org-defkey map [(shift right)]
  15338. (lambda () (interactive)
  15339. (org-eval-in-calendar '(calendar-forward-day 1))))
  15340. (org-defkey map "!"
  15341. (lambda () (interactive)
  15342. (org-eval-in-calendar '(diary-view-entries))
  15343. (message "")))
  15344. (org-defkey map ">"
  15345. (lambda () (interactive)
  15346. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15347. (org-defkey map "<"
  15348. (lambda () (interactive)
  15349. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15350. (org-defkey map "\C-v"
  15351. (lambda () (interactive)
  15352. (org-eval-in-calendar
  15353. '(calendar-scroll-left-three-months 1))))
  15354. (org-defkey map "\M-v"
  15355. (lambda () (interactive)
  15356. (org-eval-in-calendar
  15357. '(calendar-scroll-right-three-months 1))))
  15358. map)
  15359. "Keymap for minibuffer commands when using `org-read-date'.")
  15360. (defvar org-def)
  15361. (defvar org-defdecode)
  15362. (defvar org-with-time)
  15363. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15364. default-time default-input inactive)
  15365. "Read a date, possibly a time, and make things smooth for the user.
  15366. The prompt will suggest to enter an ISO date, but you can also enter anything
  15367. which will at least partially be understood by `parse-time-string'.
  15368. Unrecognized parts of the date will default to the current day, month, year,
  15369. hour and minute. If this command is called to replace a timestamp at point,
  15370. or to enter the second timestamp of a range, the default time is taken
  15371. from the existing stamp. Furthermore, the command prefers the future,
  15372. so if you are giving a date where the year is not given, and the day-month
  15373. combination is already past in the current year, it will assume you
  15374. mean next year. For details, see the manual. A few examples:
  15375. 3-2-5 --> 2003-02-05
  15376. feb 15 --> currentyear-02-15
  15377. 2/15 --> currentyear-02-15
  15378. sep 12 9 --> 2009-09-12
  15379. 12:45 --> today 12:45
  15380. 22 sept 0:34 --> currentyear-09-22 0:34
  15381. 12 --> currentyear-currentmonth-12
  15382. Fri --> nearest Friday after today
  15383. -Tue --> last Tuesday
  15384. etc.
  15385. Furthermore you can specify a relative date by giving, as the *first* thing
  15386. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15387. change in days weeks, months, years.
  15388. With a single plus or minus, the date is relative to today. With a double
  15389. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15390. +4d --> four days from today
  15391. +4 --> same as above
  15392. +2w --> two weeks from today
  15393. ++5 --> five days from default date
  15394. The function understands only English month and weekday abbreviations.
  15395. While prompting, a calendar is popped up - you can also select the
  15396. date with the mouse (button 1). The calendar shows a period of three
  15397. months. To scroll it to other months, use the keys `>' and `<'.
  15398. If you don't like the calendar, turn it off with
  15399. \(setq org-read-date-popup-calendar nil)
  15400. With optional argument TO-TIME, the date will immediately be converted
  15401. to an internal time.
  15402. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15403. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15404. still enter a time, and this function will inform the calling routine
  15405. about this change. The calling routine may then choose to change the
  15406. format used to insert the time stamp into the buffer to include the time.
  15407. With optional argument FROM-STRING, read from this string instead from
  15408. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15409. the time/date that is used for everything that is not specified by the
  15410. user."
  15411. (require 'parse-time)
  15412. (let* ((org-time-stamp-rounding-minutes
  15413. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15414. (org-dcst org-display-custom-times)
  15415. (ct (org-current-time))
  15416. (org-def (or org-overriding-default-time default-time ct))
  15417. (org-defdecode (decode-time org-def))
  15418. (dummy (progn
  15419. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15420. (setcar (nthcdr 2 org-defdecode) -1)
  15421. (setcar (nthcdr 1 org-defdecode) 59)
  15422. (setq org-def (apply 'encode-time org-defdecode)
  15423. org-defdecode (decode-time org-def)))))
  15424. (cur-frame (selected-frame))
  15425. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15426. (calendar-frame-setup nil)
  15427. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15428. (calendar-move-hook nil)
  15429. (calendar-view-diary-initially-flag nil)
  15430. (calendar-view-holidays-initially-flag nil)
  15431. (timestr (format-time-string
  15432. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15433. (prompt (concat (if prompt (concat prompt " ") "")
  15434. (format "Date+time [%s]: " timestr)))
  15435. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15436. (cond
  15437. (from-string (setq ans from-string))
  15438. (org-read-date-popup-calendar
  15439. (save-excursion
  15440. (save-window-excursion
  15441. (calendar)
  15442. (when (eq calendar-setup 'calendar-only)
  15443. (setq cal-frame
  15444. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15445. (select-frame cal-frame))
  15446. (org-eval-in-calendar '(setq cursor-type nil) t)
  15447. (unwind-protect
  15448. (progn
  15449. (calendar-forward-day (- (time-to-days org-def)
  15450. (calendar-absolute-from-gregorian
  15451. (calendar-current-date))))
  15452. (org-eval-in-calendar nil t)
  15453. (let* ((old-map (current-local-map))
  15454. (map (copy-keymap calendar-mode-map))
  15455. (minibuffer-local-map
  15456. (copy-keymap org-read-date-minibuffer-local-map)))
  15457. (org-defkey map (kbd "RET") 'org-calendar-select)
  15458. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15459. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15460. (unwind-protect
  15461. (progn
  15462. (use-local-map map)
  15463. (setq org-read-date-inactive inactive)
  15464. (add-hook 'post-command-hook 'org-read-date-display)
  15465. (setq org-ans0 (read-string prompt default-input
  15466. 'org-read-date-history nil))
  15467. ;; org-ans0: from prompt
  15468. ;; org-ans1: from mouse click
  15469. ;; org-ans2: from calendar motion
  15470. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15471. (remove-hook 'post-command-hook 'org-read-date-display)
  15472. (use-local-map old-map)
  15473. (when org-read-date-overlay
  15474. (delete-overlay org-read-date-overlay)
  15475. (setq org-read-date-overlay nil)))))
  15476. (bury-buffer "*Calendar*")
  15477. (when cal-frame
  15478. (delete-frame cal-frame)
  15479. (select-frame-set-input-focus cur-frame))))))
  15480. (t ; Naked prompt only
  15481. (unwind-protect
  15482. (setq ans (read-string prompt default-input
  15483. 'org-read-date-history timestr))
  15484. (when org-read-date-overlay
  15485. (delete-overlay org-read-date-overlay)
  15486. (setq org-read-date-overlay nil)))))
  15487. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15488. (when org-read-date-analyze-forced-year
  15489. (message "Year was forced into %s"
  15490. (if org-read-date-force-compatible-dates
  15491. "compatible range (1970-2037)"
  15492. "range representable on this machine"))
  15493. (ding))
  15494. ;; One round trip to get rid of 34th of August and stuff like that....
  15495. (setq final (decode-time (apply 'encode-time final)))
  15496. (setq org-read-date-final-answer ans)
  15497. (if to-time
  15498. (apply 'encode-time final)
  15499. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15500. (format "%04d-%02d-%02d %02d:%02d"
  15501. (nth 5 final) (nth 4 final) (nth 3 final)
  15502. (nth 2 final) (nth 1 final))
  15503. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15504. (defun org-read-date-display ()
  15505. "Display the current date prompt interpretation in the minibuffer."
  15506. (when org-read-date-display-live
  15507. (when org-read-date-overlay
  15508. (delete-overlay org-read-date-overlay))
  15509. (when (minibufferp (current-buffer))
  15510. (save-excursion
  15511. (end-of-line 1)
  15512. (while (not (equal (buffer-substring
  15513. (max (point-min) (- (point) 4)) (point))
  15514. " "))
  15515. (insert " ")))
  15516. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15517. " " (or org-ans1 org-ans2)))
  15518. (org-end-time-was-given nil)
  15519. (f (org-read-date-analyze ans org-def org-defdecode))
  15520. (fmts (if org-dcst
  15521. org-time-stamp-custom-formats
  15522. org-time-stamp-formats))
  15523. (fmt (if (or org-with-time
  15524. (and (boundp 'org-time-was-given) org-time-was-given))
  15525. (cdr fmts)
  15526. (car fmts)))
  15527. (txt (format-time-string fmt (apply 'encode-time f)))
  15528. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15529. (txt (concat "=> " txt)))
  15530. (when (and org-end-time-was-given
  15531. (string-match org-plain-time-of-day-regexp txt))
  15532. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15533. org-end-time-was-given
  15534. (substring txt (match-end 0)))))
  15535. (when org-read-date-analyze-futurep
  15536. (setq txt (concat txt " (=>F)")))
  15537. (setq org-read-date-overlay
  15538. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15539. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15540. (defun org-read-date-analyze (ans org-def org-defdecode)
  15541. "Analyze the combined answer of the date prompt."
  15542. ;; FIXME: cleanup and comment
  15543. ;; Pass `current-time' result to `decode-time' (instead of calling
  15544. ;; without arguments) so that only `current-time' has to be
  15545. ;; overriden in tests.
  15546. (let ((nowdecode (decode-time (current-time)))
  15547. delta deltan deltaw deltadef year month day
  15548. hour minute second wday pm h2 m2 tl wday1
  15549. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15550. (setq org-read-date-analyze-futurep nil
  15551. org-read-date-analyze-forced-year nil)
  15552. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15553. (setq ans "+0"))
  15554. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15555. (setq ans (replace-match "" t t ans)
  15556. deltan (car delta)
  15557. deltaw (nth 1 delta)
  15558. deltadef (nth 2 delta)))
  15559. ;; Check if there is an iso week date in there. If yes, store the
  15560. ;; info and postpone interpreting it until the rest of the parsing
  15561. ;; is done.
  15562. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15563. (setq iso-year (if (match-end 1)
  15564. (org-small-year-to-year
  15565. (string-to-number (match-string 1 ans))))
  15566. iso-weekday (if (match-end 3)
  15567. (string-to-number (match-string 3 ans)))
  15568. iso-week (string-to-number (match-string 2 ans)))
  15569. (setq ans (replace-match "" t t ans)))
  15570. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15571. (when (string-match
  15572. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15573. (setq year (if (match-end 2)
  15574. (string-to-number (match-string 2 ans))
  15575. (progn (setq kill-year t)
  15576. (string-to-number (format-time-string "%Y"))))
  15577. month (string-to-number (match-string 3 ans))
  15578. day (string-to-number (match-string 4 ans)))
  15579. (if (< year 100) (setq year (+ 2000 year)))
  15580. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15581. t nil ans)))
  15582. ;; Help matching dotted european dates
  15583. (when (string-match
  15584. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15585. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15586. (setq kill-year t)
  15587. (string-to-number (format-time-string "%Y")))
  15588. day (string-to-number (match-string 1 ans))
  15589. month (string-to-number (match-string 2 ans))
  15590. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15591. t nil ans)))
  15592. ;; Help matching american dates, like 5/30 or 5/30/7
  15593. (when (string-match
  15594. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15595. (setq year (if (match-end 4)
  15596. (string-to-number (match-string 4 ans))
  15597. (progn (setq kill-year t)
  15598. (string-to-number (format-time-string "%Y"))))
  15599. month (string-to-number (match-string 1 ans))
  15600. day (string-to-number (match-string 2 ans)))
  15601. (if (< year 100) (setq year (+ 2000 year)))
  15602. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15603. t nil ans)))
  15604. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15605. ;; If there is a time with am/pm, and *no* time without it, we convert
  15606. ;; so that matching will be successful.
  15607. (loop for i from 1 to 2 do ; twice, for end time as well
  15608. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15609. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15610. (setq hour (string-to-number (match-string 1 ans))
  15611. minute (if (match-end 3)
  15612. (string-to-number (match-string 3 ans))
  15613. 0)
  15614. pm (equal ?p
  15615. (string-to-char (downcase (match-string 4 ans)))))
  15616. (if (and (= hour 12) (not pm))
  15617. (setq hour 0)
  15618. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15619. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15620. t t ans))))
  15621. ;; Check if a time range is given as a duration
  15622. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15623. (setq hour (string-to-number (match-string 1 ans))
  15624. h2 (+ hour (string-to-number (match-string 3 ans)))
  15625. minute (string-to-number (match-string 2 ans))
  15626. m2 (+ minute (if (match-end 5) (string-to-number
  15627. (match-string 5 ans))0)))
  15628. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15629. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15630. t t ans)))
  15631. ;; Check if there is a time range
  15632. (when (boundp 'org-end-time-was-given)
  15633. (setq org-time-was-given nil)
  15634. (when (and (string-match org-plain-time-of-day-regexp ans)
  15635. (match-end 8))
  15636. (setq org-end-time-was-given (match-string 8 ans))
  15637. (setq ans (concat (substring ans 0 (match-beginning 7))
  15638. (substring ans (match-end 7))))))
  15639. (setq tl (parse-time-string ans)
  15640. day (or (nth 3 tl) (nth 3 org-defdecode))
  15641. month
  15642. (cond ((nth 4 tl))
  15643. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15644. ;; Day was specified. Make sure DAY+MONTH
  15645. ;; combination happens in the future.
  15646. ((nth 3 tl)
  15647. (setq futurep t)
  15648. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15649. (nth 4 nowdecode)))
  15650. (t (nth 4 org-defdecode)))
  15651. year
  15652. (cond ((and (not kill-year) (nth 5 tl)))
  15653. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15654. ;; Month was guessed in the future and is at least
  15655. ;; equal to NOWDECODE's. Fix year accordingly.
  15656. (futurep
  15657. (if (or (> month (nth 4 nowdecode))
  15658. (>= day (nth 3 nowdecode)))
  15659. (nth 5 nowdecode)
  15660. (1+ (nth 5 nowdecode))))
  15661. ;; Month was specified. Make sure MONTH+YEAR
  15662. ;; combination happens in the future.
  15663. ((nth 4 tl)
  15664. (setq futurep t)
  15665. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15666. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15667. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15668. (t (nth 5 nowdecode))))
  15669. (t (nth 5 org-defdecode)))
  15670. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15671. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15672. second (or (nth 0 tl) 0)
  15673. wday (nth 6 tl))
  15674. (when (and (eq org-read-date-prefer-future 'time)
  15675. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15676. (equal day (nth 3 nowdecode))
  15677. (equal month (nth 4 nowdecode))
  15678. (equal year (nth 5 nowdecode))
  15679. (nth 2 tl)
  15680. (or (< (nth 2 tl) (nth 2 nowdecode))
  15681. (and (= (nth 2 tl) (nth 2 nowdecode))
  15682. (nth 1 tl)
  15683. (< (nth 1 tl) (nth 1 nowdecode)))))
  15684. (setq day (1+ day)
  15685. futurep t))
  15686. ;; Special date definitions below
  15687. (cond
  15688. (iso-week
  15689. ;; There was an iso week
  15690. (require 'cal-iso)
  15691. (setq futurep nil)
  15692. (setq year (or iso-year year)
  15693. day (or iso-weekday wday 1)
  15694. wday nil ; to make sure that the trigger below does not match
  15695. iso-date (calendar-gregorian-from-absolute
  15696. (calendar-iso-to-absolute
  15697. (list iso-week day year))))
  15698. ; FIXME: Should we also push ISO weeks into the future?
  15699. ; (when (and org-read-date-prefer-future
  15700. ; (not iso-year)
  15701. ; (< (calendar-absolute-from-gregorian iso-date)
  15702. ; (time-to-days (current-time))))
  15703. ; (setq year (1+ year)
  15704. ; iso-date (calendar-gregorian-from-absolute
  15705. ; (calendar-iso-to-absolute
  15706. ; (list iso-week day year)))))
  15707. (setq month (car iso-date)
  15708. year (nth 2 iso-date)
  15709. day (nth 1 iso-date)))
  15710. (deltan
  15711. (setq futurep nil)
  15712. (unless deltadef
  15713. ;; Pass `current-time' result to `decode-time' (instead of
  15714. ;; calling without arguments) so that only `current-time' has
  15715. ;; to be overriden in tests.
  15716. (let ((now (decode-time (current-time))))
  15717. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15718. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15719. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15720. ((equal deltaw "m") (setq month (+ month deltan)))
  15721. ((equal deltaw "y") (setq year (+ year deltan)))))
  15722. ((and wday (not (nth 3 tl)))
  15723. ;; Weekday was given, but no day, so pick that day in the week
  15724. ;; on or after the derived date.
  15725. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15726. (unless (equal wday wday1)
  15727. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15728. (if (and (boundp 'org-time-was-given)
  15729. (nth 2 tl))
  15730. (setq org-time-was-given t))
  15731. (if (< year 100) (setq year (+ 2000 year)))
  15732. ;; Check of the date is representable
  15733. (if org-read-date-force-compatible-dates
  15734. (progn
  15735. (if (< year 1970)
  15736. (setq year 1970 org-read-date-analyze-forced-year t))
  15737. (if (> year 2037)
  15738. (setq year 2037 org-read-date-analyze-forced-year t)))
  15739. (condition-case nil
  15740. (ignore (encode-time second minute hour day month year))
  15741. (error
  15742. (setq year (nth 5 org-defdecode))
  15743. (setq org-read-date-analyze-forced-year t))))
  15744. (setq org-read-date-analyze-futurep futurep)
  15745. (list second minute hour day month year)))
  15746. (defvar parse-time-weekdays)
  15747. (defun org-read-date-get-relative (s today default)
  15748. "Check string S for special relative date string.
  15749. TODAY and DEFAULT are internal times, for today and for a default.
  15750. Return shift list (N what def-flag)
  15751. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15752. N is the number of WHATs to shift.
  15753. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15754. the DEFAULT date rather than TODAY."
  15755. (require 'parse-time)
  15756. (when (and
  15757. (string-match
  15758. (concat
  15759. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15760. "\\([0-9]+\\)?"
  15761. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15762. "\\([ \t]\\|$\\)") s)
  15763. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15764. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15765. (string-to-char (substring (match-string 1 s) -1))
  15766. ?+))
  15767. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15768. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15769. (what (if (match-end 3) (match-string 3 s) "d"))
  15770. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15771. (date (if rel default today))
  15772. (wday (nth 6 (decode-time date)))
  15773. delta)
  15774. (if wday1
  15775. (progn
  15776. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15777. (if (= delta 0) (setq delta 7))
  15778. (if (= dir ?-)
  15779. (progn
  15780. (setq delta (- delta 7))
  15781. (if (= delta 0) (setq delta -7))))
  15782. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15783. (list delta "d" rel))
  15784. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15785. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15786. "Turn a user-specified date into the internal representation.
  15787. The internal representation needed by the calendar is (month day year).
  15788. This is a wrapper to handle the brain-dead convention in calendar that
  15789. user function argument order change dependent on argument order."
  15790. (if (boundp 'calendar-date-style)
  15791. (cond
  15792. ((eq calendar-date-style 'american)
  15793. (list arg1 arg2 arg3))
  15794. ((eq calendar-date-style 'european)
  15795. (list arg2 arg1 arg3))
  15796. ((eq calendar-date-style 'iso)
  15797. (list arg2 arg3 arg1)))
  15798. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15799. (if (org-bound-and-true-p european-calendar-style)
  15800. (list arg2 arg1 arg3)
  15801. (list arg1 arg2 arg3)))))
  15802. (defun org-eval-in-calendar (form &optional keepdate)
  15803. "Eval FORM in the calendar window and return to current window.
  15804. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15805. otherwise stick to the current value of `org-ans2'."
  15806. (let ((sf (selected-frame))
  15807. (sw (selected-window)))
  15808. (select-window (get-buffer-window "*Calendar*" t))
  15809. (eval form)
  15810. (when (and (not keepdate) (calendar-cursor-to-date))
  15811. (let* ((date (calendar-cursor-to-date))
  15812. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15813. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15814. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15815. (select-window sw)
  15816. (org-select-frame-set-input-focus sf)))
  15817. (defun org-calendar-select ()
  15818. "Return to `org-read-date' with the date currently selected.
  15819. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15820. (interactive)
  15821. (when (calendar-cursor-to-date)
  15822. (let* ((date (calendar-cursor-to-date))
  15823. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15824. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15825. (if (active-minibuffer-window) (exit-minibuffer))))
  15826. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15827. "Insert a date stamp for the date given by the internal TIME.
  15828. See `format-time-string' for the format of TIME.
  15829. WITH-HM means use the stamp format that includes the time of the day.
  15830. INACTIVE means use square brackets instead of angular ones, so that the
  15831. stamp will not contribute to the agenda.
  15832. PRE and POST are optional strings to be inserted before and after the
  15833. stamp.
  15834. The command returns the inserted time stamp."
  15835. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15836. stamp)
  15837. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15838. (insert-before-markers (or pre ""))
  15839. (when (listp extra)
  15840. (setq extra (car extra))
  15841. (if (and (stringp extra)
  15842. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15843. (setq extra (format "-%02d:%02d"
  15844. (string-to-number (match-string 1 extra))
  15845. (string-to-number (match-string 2 extra))))
  15846. (setq extra nil)))
  15847. (when extra
  15848. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15849. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15850. (insert-before-markers (or post ""))
  15851. (setq org-last-inserted-timestamp stamp)))
  15852. (defun org-toggle-time-stamp-overlays ()
  15853. "Toggle the use of custom time stamp formats."
  15854. (interactive)
  15855. (setq org-display-custom-times (not org-display-custom-times))
  15856. (unless org-display-custom-times
  15857. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15858. (while (setq p (next-single-property-change p 'display))
  15859. (if (and (get-text-property p 'display)
  15860. (eq (get-text-property p 'face) 'org-date))
  15861. (remove-text-properties
  15862. p (setq p (next-single-property-change p 'display))
  15863. '(display t))))
  15864. (set-buffer-modified-p bmp)))
  15865. (if (featurep 'xemacs)
  15866. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15867. (org-restart-font-lock)
  15868. (setq org-table-may-need-update t)
  15869. (if org-display-custom-times
  15870. (message "Time stamps are overlaid with custom format")
  15871. (message "Time stamp overlays removed")))
  15872. (defun org-display-custom-time (beg end)
  15873. "Overlay modified time stamp format over timestamp between BEG and END."
  15874. (let* ((ts (buffer-substring beg end))
  15875. t1 w1 with-hm tf time str w2 (off 0))
  15876. (save-match-data
  15877. (setq t1 (org-parse-time-string ts t))
  15878. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15879. (setq off (- (match-end 0) (match-beginning 0)))))
  15880. (setq end (- end off))
  15881. (setq w1 (- end beg)
  15882. with-hm (and (nth 1 t1) (nth 2 t1))
  15883. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15884. time (org-fix-decoded-time t1)
  15885. str (org-add-props
  15886. (format-time-string
  15887. (substring tf 1 -1) (apply 'encode-time time))
  15888. nil 'mouse-face 'highlight)
  15889. w2 (length str))
  15890. (if (not (= w2 w1))
  15891. (add-text-properties (1+ beg) (+ 2 beg)
  15892. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15893. (if (featurep 'xemacs)
  15894. (progn
  15895. (put-text-property beg end 'invisible t)
  15896. (put-text-property beg end 'end-glyph (make-glyph str)))
  15897. (put-text-property beg end 'display str))))
  15898. (defun org-fix-decoded-time (time)
  15899. "Set 0 instead of nil for the first 6 elements of time.
  15900. Don't touch the rest."
  15901. (let ((n 0))
  15902. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15903. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15904. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15905. "Difference between TIMESTAMP-STRING and now in days.
  15906. If SECONDS is non-nil, return the difference in seconds."
  15907. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15908. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15909. (funcall fdiff (current-time)))))
  15910. (defun org-deadline-close (timestamp-string &optional ndays)
  15911. "Is the time in TIMESTAMP-STRING close to the current date?"
  15912. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15913. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15914. (not (org-entry-is-done-p))))
  15915. (defun org-get-wdays (ts &optional delay zero-delay)
  15916. "Get the deadline lead time appropriate for timestring TS.
  15917. When DELAY is non-nil, get the delay time for scheduled items
  15918. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15919. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15920. don't try to find the delay cookie in the scheduled timestamp."
  15921. (let ((tv (if delay org-scheduled-delay-days
  15922. org-deadline-warning-days)))
  15923. (cond
  15924. ((or (and delay (< tv 0))
  15925. (and delay zero-delay (<= tv 0))
  15926. (and (not delay) (<= tv 0)))
  15927. ;; Enforce this value no matter what
  15928. (- tv))
  15929. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15930. ;; lead time is specified.
  15931. (floor (* (string-to-number (match-string 1 ts))
  15932. (cdr (assoc (match-string 2 ts)
  15933. '(("d" . 1) ("w" . 7)
  15934. ("m" . 30.4) ("y" . 365.25)
  15935. ("h" . 0.041667)))))))
  15936. ;; go for the default.
  15937. (t tv))))
  15938. (defun org-calendar-select-mouse (ev)
  15939. "Return to `org-read-date' with the date currently selected.
  15940. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15941. (interactive "e")
  15942. (mouse-set-point ev)
  15943. (when (calendar-cursor-to-date)
  15944. (let* ((date (calendar-cursor-to-date))
  15945. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15946. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15947. (if (active-minibuffer-window) (exit-minibuffer))))
  15948. (defun org-check-deadlines (ndays)
  15949. "Check if there are any deadlines due or past due.
  15950. A deadline is considered due if it happens within `org-deadline-warning-days'
  15951. days from today's date. If the deadline appears in an entry marked DONE,
  15952. it is not shown. The prefix arg NDAYS can be used to test that many
  15953. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15954. (interactive "P")
  15955. (let* ((org-warn-days
  15956. (cond
  15957. ((equal ndays '(4)) 100000)
  15958. (ndays (prefix-numeric-value ndays))
  15959. (t (abs org-deadline-warning-days))))
  15960. (case-fold-search nil)
  15961. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15962. (callback
  15963. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15964. (message "%d deadlines past-due or due within %d days"
  15965. (org-occur regexp nil callback)
  15966. org-warn-days)))
  15967. (defsubst org-re-timestamp (type)
  15968. "Return a regexp for timestamp TYPE.
  15969. Allowed values for TYPE are:
  15970. all: all timestamps
  15971. active: only active timestamps (<...>)
  15972. inactive: only inactive timestamps ([...])
  15973. scheduled: only scheduled timestamps
  15974. deadline: only deadline timestamps
  15975. closed: only closed time-stamps
  15976. When TYPE is nil, fall back on returning a regexp that matches
  15977. both scheduled and deadline timestamps."
  15978. (case type
  15979. (all org-ts-regexp-both)
  15980. (active org-ts-regexp)
  15981. (inactive org-ts-regexp-inactive)
  15982. (scheduled org-scheduled-time-regexp)
  15983. (deadline org-deadline-time-regexp)
  15984. (closed org-closed-time-regexp)
  15985. (otherwise
  15986. (concat "\\<"
  15987. (regexp-opt (list org-deadline-string org-scheduled-string))
  15988. " *<\\([^>]+\\)>"))))
  15989. (defun org-check-before-date (date)
  15990. "Check if there are deadlines or scheduled entries before DATE."
  15991. (interactive (list (org-read-date)))
  15992. (let ((case-fold-search nil)
  15993. (regexp (org-re-timestamp org-ts-type))
  15994. (callback
  15995. `(lambda ()
  15996. (and ,(if (memq org-ts-type '(active inactive all))
  15997. '(eq (org-element-type (org-element-context)) 'timestamp)
  15998. '(org-at-planning-p))
  15999. (time-less-p
  16000. (org-time-string-to-time (match-string 1))
  16001. (org-time-string-to-time date))))))
  16002. (message "%d entries before %s"
  16003. (org-occur regexp nil callback) date)))
  16004. (defun org-check-after-date (date)
  16005. "Check if there are deadlines or scheduled entries after DATE."
  16006. (interactive (list (org-read-date)))
  16007. (let ((case-fold-search nil)
  16008. (regexp (org-re-timestamp org-ts-type))
  16009. (callback
  16010. `(lambda ()
  16011. (and ,(if (memq org-ts-type '(active inactive all))
  16012. '(eq (org-element-type (org-element-context)) 'timestamp)
  16013. '(org-at-planning-p))
  16014. (not (time-less-p
  16015. (org-time-string-to-time (match-string 1))
  16016. (org-time-string-to-time date)))))))
  16017. (message "%d entries after %s"
  16018. (org-occur regexp nil callback) date)))
  16019. (defun org-check-dates-range (start-date end-date)
  16020. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16021. (interactive (list (org-read-date nil nil nil "Range starts")
  16022. (org-read-date nil nil nil "Range end")))
  16023. (let ((case-fold-search nil)
  16024. (regexp (org-re-timestamp org-ts-type))
  16025. (callback
  16026. `(lambda ()
  16027. (let ((match (match-string 1)))
  16028. (and
  16029. ,(if (memq org-ts-type '(active inactive all))
  16030. '(eq (org-element-type (org-element-context)) 'timestamp)
  16031. '(org-at-planning-p))
  16032. (not (time-less-p
  16033. (org-time-string-to-time match)
  16034. (org-time-string-to-time start-date)))
  16035. (time-less-p
  16036. (org-time-string-to-time match)
  16037. (org-time-string-to-time end-date)))))))
  16038. (message "%d entries between %s and %s"
  16039. (org-occur regexp nil callback) start-date end-date)))
  16040. (defun org-evaluate-time-range (&optional to-buffer)
  16041. "Evaluate a time range by computing the difference between start and end.
  16042. Normally the result is just printed in the echo area, but with prefix arg
  16043. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16044. If the time range is actually in a table, the result is inserted into the
  16045. next column.
  16046. For time difference computation, a year is assumed to be exactly 365
  16047. days in order to avoid rounding problems."
  16048. (interactive "P")
  16049. (or
  16050. (org-clock-update-time-maybe)
  16051. (save-excursion
  16052. (unless (org-at-date-range-p t)
  16053. (goto-char (point-at-bol))
  16054. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16055. (if (not (org-at-date-range-p t))
  16056. (user-error "Not at a time-stamp range, and none found in current line")))
  16057. (let* ((ts1 (match-string 1))
  16058. (ts2 (match-string 2))
  16059. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16060. (match-end (match-end 0))
  16061. (time1 (org-time-string-to-time ts1))
  16062. (time2 (org-time-string-to-time ts2))
  16063. (t1 (org-float-time time1))
  16064. (t2 (org-float-time time2))
  16065. (diff (abs (- t2 t1)))
  16066. (negative (< (- t2 t1) 0))
  16067. ;; (ys (floor (* 365 24 60 60)))
  16068. (ds (* 24 60 60))
  16069. (hs (* 60 60))
  16070. (fy "%dy %dd %02d:%02d")
  16071. (fy1 "%dy %dd")
  16072. (fd "%dd %02d:%02d")
  16073. (fd1 "%dd")
  16074. (fh "%02d:%02d")
  16075. y d h m align)
  16076. (if havetime
  16077. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16078. y 0
  16079. d (floor (/ diff ds)) diff (mod diff ds)
  16080. h (floor (/ diff hs)) diff (mod diff hs)
  16081. m (floor (/ diff 60)))
  16082. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16083. y 0
  16084. d (floor (+ (/ diff ds) 0.5))
  16085. h 0 m 0))
  16086. (if (not to-buffer)
  16087. (message "%s" (org-make-tdiff-string y d h m))
  16088. (if (org-at-table-p)
  16089. (progn
  16090. (goto-char match-end)
  16091. (setq align t)
  16092. (and (looking-at " *|") (goto-char (match-end 0))))
  16093. (goto-char match-end))
  16094. (if (looking-at
  16095. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16096. (replace-match ""))
  16097. (if negative (insert " -"))
  16098. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16099. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16100. (insert " " (format fh h m))))
  16101. (if align (org-table-align))
  16102. (message "Time difference inserted")))))
  16103. (defun org-make-tdiff-string (y d h m)
  16104. (let ((fmt "")
  16105. (l nil))
  16106. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16107. l (push y l)))
  16108. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16109. l (push d l)))
  16110. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16111. l (push h l)))
  16112. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16113. l (push m l)))
  16114. (apply 'format fmt (nreverse l))))
  16115. (defun org-time-string-to-time (s &optional buffer pos)
  16116. "Convert a timestamp string into internal time."
  16117. (condition-case errdata
  16118. (apply 'encode-time (org-parse-time-string s))
  16119. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16120. s (if (not (and buffer pos))
  16121. ""
  16122. (format " at %d in buffer `%s'" pos buffer))
  16123. (cdr errdata)))))
  16124. (defun org-time-string-to-seconds (s)
  16125. "Convert a timestamp string to a number of seconds."
  16126. (org-float-time (org-time-string-to-time s)))
  16127. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16128. "Convert a time stamp to an absolute day number.
  16129. If there is a specifier for a cyclic time stamp, get the closest
  16130. date to DAYNR.
  16131. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16132. The variable `date' is bound by the calendar when this is called."
  16133. (cond
  16134. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16135. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16136. daynr
  16137. (+ daynr 1000)))
  16138. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16139. (> (string-to-number (match-string 1 s)) 0))
  16140. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16141. (time-to-days (current-time))) (match-string 0 s)
  16142. prefer show-all))
  16143. (t (time-to-days
  16144. (condition-case errdata
  16145. (apply 'encode-time (org-parse-time-string s))
  16146. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16147. s (if (not (and buffer pos))
  16148. ""
  16149. (format " at %d in buffer `%s'" pos buffer))
  16150. (cdr errdata))))))))
  16151. (defun org-days-to-iso-week (days)
  16152. "Return the iso week number."
  16153. (require 'cal-iso)
  16154. (car (calendar-iso-from-absolute days)))
  16155. (defun org-small-year-to-year (year)
  16156. "Convert 2-digit years into 4-digit years.
  16157. YEAR is expanded into one of the 30 next years, if possible, or
  16158. into a past one. Any year larger than 99 is returned unchanged."
  16159. (if (>= year 100) year
  16160. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16161. (century (/ current 100))
  16162. (offset (- year (% current 100))))
  16163. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16164. ((> offset -70) (+ (* century 100) year))
  16165. (t (+ (* (1+ century) 100) year))))))
  16166. (defun org-time-from-absolute (d)
  16167. "Return the time corresponding to date D.
  16168. D may be an absolute day number, or a calendar-type list (month day year)."
  16169. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16170. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16171. (defun org-calendar-holiday ()
  16172. "List of holidays, for Diary display in Org-mode."
  16173. (require 'holidays)
  16174. (let ((hl (funcall
  16175. (if (fboundp 'calendar-check-holidays)
  16176. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16177. (if hl (mapconcat 'identity hl "; "))))
  16178. (defun org-diary-sexp-entry (sexp entry date)
  16179. "Process a SEXP diary ENTRY for DATE."
  16180. (require 'diary-lib)
  16181. (let ((result (if calendar-debug-sexp
  16182. (let ((stack-trace-on-error t))
  16183. (eval (car (read-from-string sexp))))
  16184. (condition-case nil
  16185. (eval (car (read-from-string sexp)))
  16186. (error
  16187. (beep)
  16188. (message "Bad sexp at line %d in %s: %s"
  16189. (org-current-line)
  16190. (buffer-file-name) sexp)
  16191. (sleep-for 2))))))
  16192. (cond ((stringp result) (split-string result "; "))
  16193. ((and (consp result)
  16194. (not (consp (cdr result)))
  16195. (stringp (cdr result))) (cdr result))
  16196. ((and (consp result)
  16197. (stringp (car result))) result)
  16198. (result entry))))
  16199. (defun org-diary-to-ical-string (frombuf)
  16200. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16201. This uses the icalendar.el library."
  16202. (let* ((tmpdir (if (featurep 'xemacs)
  16203. (temp-directory)
  16204. temporary-file-directory))
  16205. (tmpfile (make-temp-name
  16206. (expand-file-name "orgics" tmpdir)))
  16207. buf rtn b e)
  16208. (with-current-buffer frombuf
  16209. (icalendar-export-region (point-min) (point-max) tmpfile)
  16210. (setq buf (find-buffer-visiting tmpfile))
  16211. (set-buffer buf)
  16212. (goto-char (point-min))
  16213. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16214. (setq b (match-beginning 0)))
  16215. (goto-char (point-max))
  16216. (if (re-search-backward "^END:VEVENT" nil t)
  16217. (setq e (match-end 0)))
  16218. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16219. (kill-buffer buf)
  16220. (delete-file tmpfile)
  16221. rtn))
  16222. (defun org-closest-date (start current change prefer show-all)
  16223. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16224. When PREFER is `past', return a date that is either CURRENT or past.
  16225. When PREFER is `future', return a date that is either CURRENT or future.
  16226. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16227. ;; Make the proper lists from the dates
  16228. (catch 'exit
  16229. (let ((a1 '(("h" . hour)
  16230. ("d" . day)
  16231. ("w" . week)
  16232. ("m" . month)
  16233. ("y" . year)))
  16234. (shour (nth 2 (org-parse-time-string start)))
  16235. dn dw sday cday n1 n2 n0
  16236. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16237. (setq start (org-date-to-gregorian start)
  16238. current (org-date-to-gregorian
  16239. (if show-all
  16240. current
  16241. (time-to-days (current-time))))
  16242. sday (calendar-absolute-from-gregorian start)
  16243. cday (calendar-absolute-from-gregorian current))
  16244. (if (<= cday sday) (throw 'exit sday))
  16245. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16246. (setq dn (string-to-number (match-string 1 change))
  16247. dw (cdr (assoc (match-string 2 change) a1))))
  16248. (unless (and dn (> dn 0))
  16249. (user-error "Invalid change specifier: %s" change))
  16250. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16251. (cond
  16252. ((eq dw 'hour)
  16253. (let ((missing-hours
  16254. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16255. dn)))
  16256. (setq n1 (if (zerop missing-hours) cday
  16257. (- cday (1+ (floor (/ missing-hours 24)))))
  16258. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16259. ((eq dw 'day)
  16260. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16261. n2 (+ n1 dn)))
  16262. ((eq dw 'year)
  16263. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16264. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16265. (setq date1 (list m d y1)
  16266. n1 (calendar-absolute-from-gregorian date1)
  16267. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16268. n2 (calendar-absolute-from-gregorian date2)))
  16269. ((eq dw 'month)
  16270. ;; approx number of month between the two dates
  16271. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16272. ;; How often does dn fit in there?
  16273. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16274. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16275. m (+ m nm)
  16276. ny (floor (/ m 12))
  16277. y (+ y ny)
  16278. m (- m (* ny 12)))
  16279. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16280. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16281. (setq m2 (+ m dn) y2 y)
  16282. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16283. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16284. (while (<= n2 cday)
  16285. (setq n1 n2 m m2 y y2)
  16286. (setq m2 (+ m dn) y2 y)
  16287. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16288. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16289. ;; Make sure n1 is the earlier date
  16290. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16291. (if show-all
  16292. (cond
  16293. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16294. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16295. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16296. (cond
  16297. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16298. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16299. (t (if (= cday n1) n1 n2)))))))
  16300. (defun org-date-to-gregorian (date)
  16301. "Turn any specification of DATE into a Gregorian date for the calendar."
  16302. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16303. ((and (listp date) (= (length date) 3)) date)
  16304. ((stringp date)
  16305. (setq date (org-parse-time-string date))
  16306. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16307. ((listp date)
  16308. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16309. (defun org-parse-time-string (s &optional nodefault)
  16310. "Parse the standard Org-mode time string.
  16311. This should be a lot faster than the normal `parse-time-string'.
  16312. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16313. hour and minute fields will be nil if not given."
  16314. (cond ((string-match org-ts-regexp0 s)
  16315. (list 0
  16316. (if (or (match-beginning 8) (not nodefault))
  16317. (string-to-number (or (match-string 8 s) "0")))
  16318. (if (or (match-beginning 7) (not nodefault))
  16319. (string-to-number (or (match-string 7 s) "0")))
  16320. (string-to-number (match-string 4 s))
  16321. (string-to-number (match-string 3 s))
  16322. (string-to-number (match-string 2 s))
  16323. nil nil nil))
  16324. ((string-match "^<[^>]+>$" s)
  16325. (decode-time (seconds-to-time (org-matcher-time s))))
  16326. (t (error "Not a standard Org-mode time string: %s" s))))
  16327. (defun org-timestamp-up (&optional arg)
  16328. "Increase the date item at the cursor by one.
  16329. If the cursor is on the year, change the year. If it is on the month,
  16330. the day or the time, change that.
  16331. With prefix ARG, change by that many units."
  16332. (interactive "p")
  16333. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16334. (defun org-timestamp-down (&optional arg)
  16335. "Decrease the date item at the cursor by one.
  16336. If the cursor is on the year, change the year. If it is on the month,
  16337. the day or the time, change that.
  16338. With prefix ARG, change by that many units."
  16339. (interactive "p")
  16340. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16341. (defun org-timestamp-up-day (&optional arg)
  16342. "Increase the date in the time stamp by one day.
  16343. With prefix ARG, change that many days."
  16344. (interactive "p")
  16345. (if (and (not (org-at-timestamp-p t))
  16346. (org-at-heading-p))
  16347. (org-todo 'up)
  16348. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16349. (defun org-timestamp-down-day (&optional arg)
  16350. "Decrease the date in the time stamp by one day.
  16351. With prefix ARG, change that many days."
  16352. (interactive "p")
  16353. (if (and (not (org-at-timestamp-p t))
  16354. (org-at-heading-p))
  16355. (org-todo 'down)
  16356. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16357. (defun org-at-timestamp-p (&optional inactive-ok)
  16358. "Non-nil if point is inside a timestamp.
  16359. When optional argument INACTIVE-OK is non-nil, also consider
  16360. inactive timestamps.
  16361. When this function returns a non-nil value, match data is set
  16362. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16363. INACTIVE-OK."
  16364. (interactive)
  16365. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16366. (pos (point))
  16367. (ans (or (looking-at tsr)
  16368. (save-excursion
  16369. (skip-chars-backward "^[<\n\r\t")
  16370. (if (> (point) (point-min)) (backward-char 1))
  16371. (and (looking-at tsr)
  16372. (> (- (match-end 0) pos) -1))))))
  16373. (and ans
  16374. (boundp 'org-ts-what)
  16375. (setq org-ts-what
  16376. (cond
  16377. ((= pos (match-beginning 0)) 'bracket)
  16378. ;; Point is considered to be "on the bracket" whether
  16379. ;; it's really on it or right after it.
  16380. ((= pos (1- (match-end 0))) 'bracket)
  16381. ((= pos (match-end 0)) 'after)
  16382. ((org-pos-in-match-range pos 2) 'year)
  16383. ((org-pos-in-match-range pos 3) 'month)
  16384. ((org-pos-in-match-range pos 7) 'hour)
  16385. ((org-pos-in-match-range pos 8) 'minute)
  16386. ((or (org-pos-in-match-range pos 4)
  16387. (org-pos-in-match-range pos 5)) 'day)
  16388. ((and (> pos (or (match-end 8) (match-end 5)))
  16389. (< pos (match-end 0)))
  16390. (- pos (or (match-end 8) (match-end 5))))
  16391. (t 'day))))
  16392. ans))
  16393. (defun org-toggle-timestamp-type ()
  16394. "Toggle the type (<active> or [inactive]) of a time stamp."
  16395. (interactive)
  16396. (when (org-at-timestamp-p t)
  16397. (let ((beg (match-beginning 0)) (end (match-end 0))
  16398. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16399. (save-excursion
  16400. (goto-char beg)
  16401. (while (re-search-forward "[][<>]" end t)
  16402. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16403. t t)))
  16404. (message "Timestamp is now %sactive"
  16405. (if (equal (char-after beg) ?<) "" "in")))))
  16406. (defun org-at-clock-log-p nil
  16407. "Is the cursor on the clock log line?"
  16408. (save-excursion
  16409. (move-beginning-of-line 1)
  16410. (looking-at org-clock-line-re)))
  16411. (defvar org-clock-history) ; defined in org-clock.el
  16412. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16413. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16414. "Change the date in the time stamp at point.
  16415. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16416. `year', `minute', `second'. If WHAT is not given, the cursor position
  16417. in the timestamp determines what will be changed.
  16418. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16419. (let ((origin (point)) origin-cat
  16420. with-hm inactive
  16421. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16422. org-ts-what
  16423. extra rem
  16424. ts time time0 fixnext clrgx)
  16425. (if (not (org-at-timestamp-p t))
  16426. (user-error "Not at a timestamp"))
  16427. (if (and (not what) (eq org-ts-what 'bracket))
  16428. (org-toggle-timestamp-type)
  16429. ;; Point isn't on brackets. Remember the part of the time-stamp
  16430. ;; the point was in. Indeed, size of time-stamps may change,
  16431. ;; but point must be kept in the same category nonetheless.
  16432. (setq origin-cat org-ts-what)
  16433. (if (and (not what) (not (eq org-ts-what 'day))
  16434. org-display-custom-times
  16435. (get-text-property (point) 'display)
  16436. (not (get-text-property (1- (point)) 'display)))
  16437. (setq org-ts-what 'day))
  16438. (setq org-ts-what (or what org-ts-what)
  16439. inactive (= (char-after (match-beginning 0)) ?\[)
  16440. ts (match-string 0))
  16441. (replace-match "")
  16442. (when (string-match
  16443. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16444. ts)
  16445. (setq extra (match-string 1 ts))
  16446. (if suppress-tmp-delay
  16447. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16448. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16449. (setq with-hm t))
  16450. (setq time0 (org-parse-time-string ts))
  16451. (when (and updown
  16452. (eq org-ts-what 'minute)
  16453. (not current-prefix-arg))
  16454. ;; This looks like s-up and s-down. Change by one rounding step.
  16455. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16456. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16457. (setcar (cdr time0) (+ (nth 1 time0)
  16458. (if (> n 0) (- rem) (- dm rem))))))
  16459. (setq time
  16460. (encode-time (or (car time0) 0)
  16461. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16462. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16463. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16464. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16465. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16466. (nthcdr 6 time0)))
  16467. (when (and (member org-ts-what '(hour minute))
  16468. extra
  16469. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16470. (setq extra (org-modify-ts-extra
  16471. extra
  16472. (if (eq org-ts-what 'hour) 2 5)
  16473. n dm)))
  16474. (when (integerp org-ts-what)
  16475. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16476. (if (eq what 'calendar)
  16477. (let ((cal-date (org-get-date-from-calendar)))
  16478. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16479. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16480. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16481. (setcar time0 (or (car time0) 0))
  16482. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16483. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16484. (setq time (apply 'encode-time time0))))
  16485. ;; Insert the new time-stamp, and ensure point stays in the same
  16486. ;; category as before (i.e. not after the last position in that
  16487. ;; category).
  16488. (let ((pos (point)))
  16489. ;; Stay before inserted string. `save-excursion' is of no use.
  16490. (setq org-last-changed-timestamp
  16491. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16492. (goto-char pos))
  16493. (save-match-data
  16494. (looking-at org-ts-regexp3)
  16495. (goto-char (cond
  16496. ;; `day' category ends before `hour' if any, or at
  16497. ;; the end of the day name.
  16498. ((eq origin-cat 'day)
  16499. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16500. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16501. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16502. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16503. ;; `year' and `month' have both fixed size: point
  16504. ;; couldn't have moved into another part.
  16505. (t origin))))
  16506. ;; Update clock if on a CLOCK line.
  16507. (org-clock-update-time-maybe)
  16508. ;; Maybe adjust the closest clock in `org-clock-history'
  16509. (when org-clock-adjust-closest
  16510. (if (not (and (org-at-clock-log-p)
  16511. (< 1 (length (delq nil (mapcar 'marker-position
  16512. org-clock-history))))))
  16513. (message "No clock to adjust")
  16514. (cond ((save-excursion ; fix previous clock?
  16515. (re-search-backward org-ts-regexp0 nil t)
  16516. (org-looking-back (concat org-clock-string " \\[")))
  16517. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16518. ((save-excursion ; fix next clock?
  16519. (re-search-backward org-ts-regexp0 nil t)
  16520. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16521. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16522. (save-window-excursion
  16523. ;; Find closest clock to point, adjust the previous/next one in history
  16524. (let* ((p (save-excursion (org-back-to-heading t)))
  16525. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16526. (clfixnth
  16527. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16528. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16529. (if (not clfixpos)
  16530. (message "No clock to adjust")
  16531. (save-excursion
  16532. (org-goto-marker-or-bmk clfixpos)
  16533. (org-show-subtree)
  16534. (when (re-search-forward clrgx nil t)
  16535. (goto-char (match-beginning 1))
  16536. (let (org-clock-adjust-closest)
  16537. (org-timestamp-change n org-ts-what updown))
  16538. (message "Clock adjusted in %s for heading: %s"
  16539. (file-name-nondirectory (buffer-file-name))
  16540. (org-get-heading t t)))))))))
  16541. ;; Try to recenter the calendar window, if any.
  16542. (if (and org-calendar-follow-timestamp-change
  16543. (get-buffer-window "*Calendar*" t)
  16544. (memq org-ts-what '(day month year)))
  16545. (org-recenter-calendar (time-to-days time))))))
  16546. (defun org-modify-ts-extra (s pos n dm)
  16547. "Change the different parts of the lead-time and repeat fields in timestamp."
  16548. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16549. ng h m new rem)
  16550. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16551. (cond
  16552. ((or (org-pos-in-match-range pos 2)
  16553. (org-pos-in-match-range pos 3))
  16554. (setq m (string-to-number (match-string 3 s))
  16555. h (string-to-number (match-string 2 s)))
  16556. (if (org-pos-in-match-range pos 2)
  16557. (setq h (+ h n))
  16558. (setq n (* dm (org-no-warnings (signum n))))
  16559. (when (not (= 0 (setq rem (% m dm))))
  16560. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16561. (setq m (+ m n)))
  16562. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16563. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16564. (setq h (min 24 (max 0 h)))
  16565. (setq ng 1 new (format "-%02d:%02d" h m)))
  16566. ((org-pos-in-match-range pos 6)
  16567. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16568. ((org-pos-in-match-range pos 5)
  16569. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16570. ((org-pos-in-match-range pos 9)
  16571. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16572. ((org-pos-in-match-range pos 8)
  16573. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16574. (when ng
  16575. (setq s (concat
  16576. (substring s 0 (match-beginning ng))
  16577. new
  16578. (substring s (match-end ng))))))
  16579. s))
  16580. (defun org-recenter-calendar (date)
  16581. "If the calendar is visible, recenter it to DATE."
  16582. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16583. (when cwin
  16584. (let ((calendar-move-hook nil))
  16585. (with-selected-window cwin
  16586. (calendar-goto-date (if (listp date) date
  16587. (calendar-gregorian-from-absolute date))))))))
  16588. (defun org-goto-calendar (&optional arg)
  16589. "Go to the Emacs calendar at the current date.
  16590. If there is a time stamp in the current line, go to that date.
  16591. A prefix ARG can be used to force the current date."
  16592. (interactive "P")
  16593. (let ((tsr org-ts-regexp) diff
  16594. (calendar-move-hook nil)
  16595. (calendar-view-holidays-initially-flag nil)
  16596. (calendar-view-diary-initially-flag nil))
  16597. (if (or (org-at-timestamp-p)
  16598. (save-excursion
  16599. (beginning-of-line 1)
  16600. (looking-at (concat ".*" tsr))))
  16601. (let ((d1 (time-to-days (current-time)))
  16602. (d2 (time-to-days
  16603. (org-time-string-to-time (match-string 1)))))
  16604. (setq diff (- d2 d1))))
  16605. (calendar)
  16606. (calendar-goto-today)
  16607. (if (and diff (not arg)) (calendar-forward-day diff))))
  16608. (defun org-get-date-from-calendar ()
  16609. "Return a list (month day year) of date at point in calendar."
  16610. (with-current-buffer "*Calendar*"
  16611. (save-match-data
  16612. (calendar-cursor-to-date))))
  16613. (defun org-date-from-calendar ()
  16614. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16615. If there is already a time stamp at the cursor position, update it."
  16616. (interactive)
  16617. (if (org-at-timestamp-p t)
  16618. (org-timestamp-change 0 'calendar)
  16619. (let ((cal-date (org-get-date-from-calendar)))
  16620. (org-insert-time-stamp
  16621. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16622. (defcustom org-effort-durations
  16623. `(("min" . 1)
  16624. ("h" . 60)
  16625. ("d" . ,(* 60 8))
  16626. ("w" . ,(* 60 8 5))
  16627. ("m" . ,(* 60 8 5 4))
  16628. ("y" . ,(* 60 8 5 40)))
  16629. "Conversion factor to minutes for an effort modifier.
  16630. Each entry has the form (MODIFIER . MINUTES).
  16631. In an effort string, a number followed by MODIFIER is multiplied
  16632. by the specified number of MINUTES to obtain an effort in
  16633. minutes.
  16634. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16635. effort string \"2hours\" is equivalent to 120 minutes."
  16636. :group 'org-agenda
  16637. :version "25.1"
  16638. :package-version '(Org . "8.3")
  16639. :type '(alist :key-type (string :tag "Modifier")
  16640. :value-type (number :tag "Minutes")))
  16641. (defun org-minutes-to-clocksum-string (m)
  16642. "Format number of minutes as a clocksum string.
  16643. The format is determined by `org-time-clocksum-format',
  16644. `org-time-clocksum-use-fractional' and
  16645. `org-time-clocksum-fractional-format' and
  16646. `org-time-clocksum-use-effort-durations'."
  16647. (let ((clocksum "")
  16648. (m (round m)) ; Don't allow fractions of minutes
  16649. h d w mo y fmt n)
  16650. (setq h (if org-time-clocksum-use-effort-durations
  16651. (cdr (assoc "h" org-effort-durations)) 60)
  16652. d (if org-time-clocksum-use-effort-durations
  16653. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16654. w (if org-time-clocksum-use-effort-durations
  16655. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16656. mo (if org-time-clocksum-use-effort-durations
  16657. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16658. y (if org-time-clocksum-use-effort-durations
  16659. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16660. ;; fractional format
  16661. (if org-time-clocksum-use-fractional
  16662. (cond
  16663. ;; single format string
  16664. ((stringp org-time-clocksum-fractional-format)
  16665. (format org-time-clocksum-fractional-format (/ m (float h))))
  16666. ;; choice of fractional formats for different time units
  16667. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16668. (> (/ (truncate m) (* y d h)) 0))
  16669. (format fmt (/ m (* y d (float h)))))
  16670. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16671. (> (/ (truncate m) (* mo d h)) 0))
  16672. (format fmt (/ m (* mo d (float h)))))
  16673. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16674. (> (/ (truncate m) (* w d h)) 0))
  16675. (format fmt (/ m (* w d (float h)))))
  16676. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16677. (> (/ (truncate m) (* d h)) 0))
  16678. (format fmt (/ m (* d (float h)))))
  16679. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16680. (> (/ (truncate m) h) 0))
  16681. (format fmt (/ m (float h))))
  16682. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16683. (format fmt m))
  16684. ;; fall back to smallest time unit with a format
  16685. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16686. (format fmt (/ m (float h))))
  16687. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16688. (format fmt (/ m (* d (float h)))))
  16689. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16690. (format fmt (/ m (* w d (float h)))))
  16691. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16692. (format fmt (/ m (* mo d (float h)))))
  16693. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16694. (format fmt (/ m (* y d (float h))))))
  16695. ;; standard (non-fractional) format, with single format string
  16696. (if (stringp org-time-clocksum-format)
  16697. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16698. ;; separate formats components
  16699. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16700. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16701. (plist-get org-time-clocksum-format :require-years))
  16702. (setq clocksum (concat clocksum (format fmt n))
  16703. m (- m (* n y d h))))
  16704. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16705. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16706. (plist-get org-time-clocksum-format :require-months))
  16707. (setq clocksum (concat clocksum (format fmt n))
  16708. m (- m (* n mo d h))))
  16709. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16710. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16711. (plist-get org-time-clocksum-format :require-weeks))
  16712. (setq clocksum (concat clocksum (format fmt n))
  16713. m (- m (* n w d h))))
  16714. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16715. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16716. (plist-get org-time-clocksum-format :require-days))
  16717. (setq clocksum (concat clocksum (format fmt n))
  16718. m (- m (* n d h))))
  16719. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16720. (or (> (setq n (/ (truncate m) h)) 0)
  16721. (plist-get org-time-clocksum-format :require-hours))
  16722. (setq clocksum (concat clocksum (format fmt n))
  16723. m (- m (* n h))))
  16724. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16725. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16726. (setq clocksum (concat clocksum (format fmt m))))
  16727. ;; return formatted time duration
  16728. clocksum))))
  16729. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16730. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16731. "Org mode version 8.0")
  16732. (defun org-hours-to-clocksum-string (n)
  16733. (org-minutes-to-clocksum-string (* n 60)))
  16734. (defun org-hh:mm-string-to-minutes (s)
  16735. "Convert a string H:MM to a number of minutes.
  16736. If the string is just a number, interpret it as minutes.
  16737. In fact, the first hh:mm or number in the string will be taken,
  16738. there can be extra stuff in the string.
  16739. If no number is found, the return value is 0."
  16740. (cond
  16741. ((integerp s) s)
  16742. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16743. (+ (* (string-to-number (match-string 1 s)) 60)
  16744. (string-to-number (match-string 2 s))))
  16745. ((string-match "\\([0-9]+\\)" s)
  16746. (string-to-number (match-string 1 s)))
  16747. (t 0)))
  16748. (defcustom org-image-actual-width t
  16749. "Should we use the actual width of images when inlining them?
  16750. When set to t, always use the image width.
  16751. When set to a number, use imagemagick (when available) to set
  16752. the image's width to this value.
  16753. When set to a number in a list, try to get the width from any
  16754. #+ATTR.* keyword if it matches a width specification like
  16755. #+ATTR_HTML: :width 300px
  16756. and fall back on that number if none is found.
  16757. When set to nil, try to get the width from an #+ATTR.* keyword
  16758. and fall back on the original width if none is found.
  16759. This requires Emacs >= 24.1, build with imagemagick support."
  16760. :group 'org-appearance
  16761. :version "24.4"
  16762. :package-version '(Org . "8.0")
  16763. :type '(choice
  16764. (const :tag "Use the image width" t)
  16765. (integer :tag "Use a number of pixels")
  16766. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16767. (const :tag "Use #+ATTR* or don't resize" nil)))
  16768. (defcustom org-agenda-inhibit-startup nil
  16769. "Inhibit startup when preparing agenda buffers.
  16770. When this variable is t, the initialization of the Org agenda
  16771. buffers is inhibited: e.g. the visibility state is not set, the
  16772. tables are not re-aligned, etc."
  16773. :type 'boolean
  16774. :version "24.3"
  16775. :group 'org-agenda)
  16776. (define-obsolete-variable-alias
  16777. 'org-agenda-ignore-drawer-properties
  16778. 'org-agenda-ignore-properties "25.1")
  16779. (defcustom org-agenda-ignore-properties nil
  16780. "Avoid updating text properties when building the agenda.
  16781. Properties are used to prepare buffers for effort estimates,
  16782. appointments, statistics and subtree-local categories.
  16783. If you don't use these in the agenda, you can add them to this
  16784. list and agenda building will be a bit faster.
  16785. The value is a list, with zero or more of the symbols `effort', `appt',
  16786. `stats' or `category'."
  16787. :type '(set :greedy t
  16788. (const effort)
  16789. (const appt)
  16790. (const stats)
  16791. (const category))
  16792. :version "25.1"
  16793. :package-version '(Org . "8.3")
  16794. :group 'org-agenda)
  16795. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16796. "Convert a duration string S to minutes.
  16797. A bare number is interpreted as minutes, modifiers can be set by
  16798. customizing `org-effort-durations' (which see).
  16799. Entries containing a colon are interpreted as H:MM by
  16800. `org-hh:mm-string-to-minutes'."
  16801. (let ((result 0)
  16802. (re (concat "\\([0-9.]+\\) *\\("
  16803. (regexp-opt (mapcar 'car org-effort-durations))
  16804. "\\)")))
  16805. (while (string-match re s)
  16806. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16807. (string-to-number (match-string 1 s))))
  16808. (setq s (replace-match "" nil t s)))
  16809. (setq result (floor result))
  16810. (incf result (org-hh:mm-string-to-minutes s))
  16811. (if output-to-string (number-to-string result) result)))
  16812. ;;;; Files
  16813. (defun org-save-all-org-buffers ()
  16814. "Save all Org-mode buffers without user confirmation."
  16815. (interactive)
  16816. (message "Saving all Org-mode buffers...")
  16817. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16818. (when (featurep 'org-id) (org-id-locations-save))
  16819. (message "Saving all Org-mode buffers... done"))
  16820. (defun org-revert-all-org-buffers ()
  16821. "Revert all Org-mode buffers.
  16822. Prompt for confirmation when there are unsaved changes.
  16823. Be sure you know what you are doing before letting this function
  16824. overwrite your changes.
  16825. This function is useful in a setup where one tracks org files
  16826. with a version control system, to revert on one machine after pulling
  16827. changes from another. I believe the procedure must be like this:
  16828. 1. M-x org-save-all-org-buffers
  16829. 2. Pull changes from the other machine, resolve conflicts
  16830. 3. M-x org-revert-all-org-buffers"
  16831. (interactive)
  16832. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16833. (user-error "Abort"))
  16834. (save-excursion
  16835. (save-window-excursion
  16836. (mapc
  16837. (lambda (b)
  16838. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16839. (with-current-buffer b buffer-file-name))
  16840. (org-pop-to-buffer-same-window b)
  16841. (revert-buffer t 'no-confirm)))
  16842. (buffer-list))
  16843. (when (and (featurep 'org-id) org-id-track-globally)
  16844. (org-id-locations-load)))))
  16845. ;;;; Agenda files
  16846. ;;;###autoload
  16847. (defun org-switchb (&optional arg)
  16848. "Switch between Org buffers.
  16849. With one prefix argument, restrict available buffers to files.
  16850. With two prefix arguments, restrict available buffers to agenda files.
  16851. Defaults to `iswitchb' for buffer name completion.
  16852. Set `org-completion-use-ido' to make it use ido instead."
  16853. (interactive "P")
  16854. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16855. ((equal arg '(16)) (org-buffer-list 'agenda))
  16856. (t (org-buffer-list))))
  16857. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16858. (org-completion-use-ido org-completion-use-ido))
  16859. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16860. (setq org-completion-use-iswitchb t))
  16861. (org-pop-to-buffer-same-window
  16862. (org-icompleting-read "Org buffer: "
  16863. (mapcar 'list (mapcar 'buffer-name blist))
  16864. nil t))))
  16865. ;;; Define some older names previously used for this functionality
  16866. ;;;###autoload
  16867. (defalias 'org-ido-switchb 'org-switchb)
  16868. ;;;###autoload
  16869. (defalias 'org-iswitchb 'org-switchb)
  16870. (defun org-buffer-list (&optional predicate exclude-tmp)
  16871. "Return a list of Org buffers.
  16872. PREDICATE can be `export', `files' or `agenda'.
  16873. export restrict the list to Export buffers.
  16874. files restrict the list to buffers visiting Org files.
  16875. agenda restrict the list to buffers visiting agenda files.
  16876. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16877. (let* ((bfn nil)
  16878. (agenda-files (and (eq predicate 'agenda)
  16879. (mapcar 'file-truename (org-agenda-files t))))
  16880. (filter
  16881. (cond
  16882. ((eq predicate 'files)
  16883. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16884. ((eq predicate 'export)
  16885. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16886. ((eq predicate 'agenda)
  16887. (lambda (b)
  16888. (with-current-buffer b
  16889. (and (derived-mode-p 'org-mode)
  16890. (setq bfn (buffer-file-name b))
  16891. (member (file-truename bfn) agenda-files)))))
  16892. (t (lambda (b) (with-current-buffer b
  16893. (or (derived-mode-p 'org-mode)
  16894. (string-match "\*Org .*Export"
  16895. (buffer-name b)))))))))
  16896. (delq nil
  16897. (mapcar
  16898. (lambda(b)
  16899. (if (and (funcall filter b)
  16900. (or (not exclude-tmp)
  16901. (not (string-match "tmp" (buffer-name b)))))
  16902. b
  16903. nil))
  16904. (buffer-list)))))
  16905. (defun org-agenda-files (&optional unrestricted archives)
  16906. "Get the list of agenda files.
  16907. Optional UNRESTRICTED means return the full list even if a restriction
  16908. is currently in place.
  16909. When ARCHIVES is t, include all archive files that are really being
  16910. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16911. `org-agenda-archives-mode' is t."
  16912. (let ((files
  16913. (cond
  16914. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16915. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16916. ((listp org-agenda-files) org-agenda-files)
  16917. (t (error "Invalid value of `org-agenda-files'")))))
  16918. (setq files (apply 'append
  16919. (mapcar (lambda (f)
  16920. (if (file-directory-p f)
  16921. (directory-files
  16922. f t org-agenda-file-regexp)
  16923. (list f)))
  16924. files)))
  16925. (when org-agenda-skip-unavailable-files
  16926. (setq files (delq nil
  16927. (mapcar (function
  16928. (lambda (file)
  16929. (and (file-readable-p file) file)))
  16930. files))))
  16931. (when (or (eq archives t)
  16932. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16933. (setq files (org-add-archive-files files)))
  16934. files))
  16935. (defun org-agenda-file-p (&optional file)
  16936. "Return non-nil, if FILE is an agenda file.
  16937. If FILE is omitted, use the file associated with the current
  16938. buffer."
  16939. (let ((fname (or file (buffer-file-name))))
  16940. (and fname
  16941. (member (file-truename fname)
  16942. (mapcar #'file-truename (org-agenda-files t))))))
  16943. (defun org-edit-agenda-file-list ()
  16944. "Edit the list of agenda files.
  16945. Depending on setup, this either uses customize to edit the variable
  16946. `org-agenda-files', or it visits the file that is holding the list. In the
  16947. latter case, the buffer is set up in a way that saving it automatically kills
  16948. the buffer and restores the previous window configuration."
  16949. (interactive)
  16950. (if (stringp org-agenda-files)
  16951. (let ((cw (current-window-configuration)))
  16952. (find-file org-agenda-files)
  16953. (org-set-local 'org-window-configuration cw)
  16954. (org-add-hook 'after-save-hook
  16955. (lambda ()
  16956. (set-window-configuration
  16957. (prog1 org-window-configuration
  16958. (kill-buffer (current-buffer))))
  16959. (org-install-agenda-files-menu)
  16960. (message "New agenda file list installed"))
  16961. nil 'local)
  16962. (message "%s" (substitute-command-keys
  16963. "Edit list and finish with \\[save-buffer]")))
  16964. (customize-variable 'org-agenda-files)))
  16965. (defun org-store-new-agenda-file-list (list)
  16966. "Set new value for the agenda file list and save it correctly."
  16967. (if (stringp org-agenda-files)
  16968. (let ((fe (org-read-agenda-file-list t)) b u)
  16969. (while (setq b (find-buffer-visiting org-agenda-files))
  16970. (kill-buffer b))
  16971. (with-temp-file org-agenda-files
  16972. (insert
  16973. (mapconcat
  16974. (lambda (f) ;; Keep un-expanded entries.
  16975. (if (setq u (assoc f fe))
  16976. (cdr u)
  16977. f))
  16978. list "\n")
  16979. "\n")))
  16980. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16981. (org-insert-mode-line-in-empty-file nil))
  16982. (setq org-agenda-files list)
  16983. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16984. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16985. "Read the list of agenda files from a file.
  16986. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16987. filenames, used by `org-store-new-agenda-file-list' to write back
  16988. un-expanded file names."
  16989. (when (file-directory-p org-agenda-files)
  16990. (error "`org-agenda-files' cannot be a single directory"))
  16991. (when (stringp org-agenda-files)
  16992. (with-temp-buffer
  16993. (insert-file-contents org-agenda-files)
  16994. (mapcar
  16995. (lambda (f)
  16996. (let ((e (expand-file-name (substitute-in-file-name f)
  16997. org-directory)))
  16998. (if pair-with-expansion
  16999. (cons e f)
  17000. e)))
  17001. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17002. ;;;###autoload
  17003. (defun org-cycle-agenda-files ()
  17004. "Cycle through the files in `org-agenda-files'.
  17005. If the current buffer visits an agenda file, find the next one in the list.
  17006. If the current buffer does not, find the first agenda file."
  17007. (interactive)
  17008. (let* ((fs (or (org-agenda-files t)
  17009. (user-error "No agenda files")))
  17010. (files (copy-sequence fs))
  17011. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17012. file)
  17013. (when tcf
  17014. (while (and (setq file (pop files))
  17015. (not (equal (file-truename file) tcf)))))
  17016. (find-file (car (or files fs)))
  17017. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17018. (defun org-agenda-file-to-front (&optional to-end)
  17019. "Move/add the current file to the top of the agenda file list.
  17020. If the file is not present in the list, it is added to the front. If it is
  17021. present, it is moved there. With optional argument TO-END, add/move to the
  17022. end of the list."
  17023. (interactive "P")
  17024. (let ((org-agenda-skip-unavailable-files nil)
  17025. (file-alist (mapcar (lambda (x)
  17026. (cons (file-truename x) x))
  17027. (org-agenda-files t)))
  17028. (ctf (file-truename
  17029. (or buffer-file-name
  17030. (user-error "Please save the current buffer to a file"))))
  17031. x had)
  17032. (setq x (assoc ctf file-alist) had x)
  17033. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17034. (if to-end
  17035. (setq file-alist (append (delq x file-alist) (list x)))
  17036. (setq file-alist (cons x (delq x file-alist))))
  17037. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17038. (org-install-agenda-files-menu)
  17039. (message "File %s to %s of agenda file list"
  17040. (if had "moved" "added") (if to-end "end" "front"))))
  17041. (defun org-remove-file (&optional file)
  17042. "Remove current file from the list of files in variable `org-agenda-files'.
  17043. These are the files which are being checked for agenda entries.
  17044. Optional argument FILE means use this file instead of the current."
  17045. (interactive)
  17046. (let* ((org-agenda-skip-unavailable-files nil)
  17047. (file (or file buffer-file-name
  17048. (user-error "Current buffer does not visit a file")))
  17049. (true-file (file-truename file))
  17050. (afile (abbreviate-file-name file))
  17051. (files (delq nil (mapcar
  17052. (lambda (x)
  17053. (if (equal true-file
  17054. (file-truename x))
  17055. nil x))
  17056. (org-agenda-files t)))))
  17057. (if (not (= (length files) (length (org-agenda-files t))))
  17058. (progn
  17059. (org-store-new-agenda-file-list files)
  17060. (org-install-agenda-files-menu)
  17061. (message "Removed from Org Agenda list: %s" afile))
  17062. (message "File was not in list: %s (not removed)" afile))))
  17063. (defun org-file-menu-entry (file)
  17064. (vector file (list 'find-file file) t))
  17065. (defun org-check-agenda-file (file)
  17066. "Make sure FILE exists. If not, ask user what to do."
  17067. (when (not (file-exists-p file))
  17068. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17069. (abbreviate-file-name file))
  17070. (let ((r (downcase (read-char-exclusive))))
  17071. (cond
  17072. ((equal r ?r)
  17073. (org-remove-file file)
  17074. (throw 'nextfile t))
  17075. (t (user-error "Abort"))))))
  17076. (defun org-get-agenda-file-buffer (file)
  17077. "Get an agenda buffer visiting FILE.
  17078. If the buffer needs to be created, add it to the list of buffers
  17079. which might be released later."
  17080. (let ((buf (org-find-base-buffer-visiting file)))
  17081. (if buf
  17082. buf ; just return it
  17083. ;; Make a new buffer and remember it
  17084. (setq buf (find-file-noselect file))
  17085. (if buf (push buf org-agenda-new-buffers))
  17086. buf)))
  17087. (defun org-release-buffers (blist)
  17088. "Release all buffers in list, asking the user for confirmation when needed.
  17089. When a buffer is unmodified, it is just killed. When modified, it is saved
  17090. \(if the user agrees) and then killed."
  17091. (let (file)
  17092. (dolist (buf blist)
  17093. (setq file (buffer-file-name buf))
  17094. (when (and (buffer-modified-p buf)
  17095. file
  17096. (y-or-n-p (format "Save file %s? " file)))
  17097. (with-current-buffer buf (save-buffer)))
  17098. (kill-buffer buf))))
  17099. (defun org-agenda-prepare-buffers (files)
  17100. "Create buffers for all agenda files, protect archived trees and comments."
  17101. (interactive)
  17102. (let ((pa '(:org-archived t))
  17103. (pc '(:org-comment t))
  17104. (pall '(:org-archived t :org-comment t))
  17105. (inhibit-read-only t)
  17106. (org-inhibit-startup org-agenda-inhibit-startup)
  17107. (rea (concat ":" org-archive-tag ":"))
  17108. file re pos)
  17109. (setq org-tag-alist-for-agenda nil
  17110. org-tag-groups-alist-for-agenda nil)
  17111. (save-excursion
  17112. (save-restriction
  17113. (dolist (file files)
  17114. (catch 'nextfile
  17115. (if (bufferp file)
  17116. (set-buffer file)
  17117. (org-check-agenda-file file)
  17118. (set-buffer (org-get-agenda-file-buffer file)))
  17119. (widen)
  17120. (org-set-regexps-and-options 'tags-only)
  17121. (setq pos (point))
  17122. (or (memq 'category org-agenda-ignore-properties)
  17123. (org-refresh-category-properties))
  17124. (or (memq 'stats org-agenda-ignore-properties)
  17125. (org-refresh-stats-properties))
  17126. (or (memq 'effort org-agenda-ignore-properties)
  17127. (org-refresh-effort-properties))
  17128. (or (memq 'appt org-agenda-ignore-properties)
  17129. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17130. (setq org-todo-keywords-for-agenda
  17131. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17132. (setq org-done-keywords-for-agenda
  17133. (append org-done-keywords-for-agenda org-done-keywords))
  17134. (setq org-todo-keyword-alist-for-agenda
  17135. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17136. (setq org-tag-alist-for-agenda
  17137. (org-uniquify
  17138. (append org-tag-alist-for-agenda
  17139. org-tag-alist
  17140. org-tag-persistent-alist)))
  17141. (if org-group-tags
  17142. (setq org-tag-groups-alist-for-agenda
  17143. (org-uniquify-alist
  17144. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17145. (org-with-silent-modifications
  17146. (save-excursion
  17147. (remove-text-properties (point-min) (point-max) pall)
  17148. (when org-agenda-skip-archived-trees
  17149. (goto-char (point-min))
  17150. (while (re-search-forward rea nil t)
  17151. (if (org-at-heading-p t)
  17152. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17153. (goto-char (point-min))
  17154. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17155. (while (re-search-forward re nil t)
  17156. (when (save-match-data (org-in-commented-heading-p t))
  17157. (add-text-properties
  17158. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17159. (goto-char pos)))))
  17160. (setq org-todo-keywords-for-agenda
  17161. (org-uniquify org-todo-keywords-for-agenda))
  17162. (setq org-todo-keyword-alist-for-agenda
  17163. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17164. ;;;; CDLaTeX minor mode
  17165. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17166. "Keymap for the minor `org-cdlatex-mode'.")
  17167. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17168. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17169. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17170. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17171. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17172. (defvar org-cdlatex-texmathp-advice-is-done nil
  17173. "Flag remembering if we have applied the advice to texmathp already.")
  17174. (define-minor-mode org-cdlatex-mode
  17175. "Toggle the minor `org-cdlatex-mode'.
  17176. This mode supports entering LaTeX environment and math in LaTeX fragments
  17177. in Org-mode.
  17178. \\{org-cdlatex-mode-map}"
  17179. nil " OCDL" nil
  17180. (when org-cdlatex-mode
  17181. (require 'cdlatex)
  17182. (run-hooks 'cdlatex-mode-hook)
  17183. (cdlatex-compute-tables))
  17184. (unless org-cdlatex-texmathp-advice-is-done
  17185. (setq org-cdlatex-texmathp-advice-is-done t)
  17186. (defadvice texmathp (around org-math-always-on activate)
  17187. "Always return t in org-mode buffers.
  17188. This is because we want to insert math symbols without dollars even outside
  17189. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17190. an embedded LaTeX fragment, let texmathp do its job.
  17191. \\[org-cdlatex-mode-map]"
  17192. (interactive)
  17193. (let (p)
  17194. (cond
  17195. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17196. ((eq this-command 'cdlatex-math-symbol)
  17197. (setq ad-return-value t
  17198. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17199. (t
  17200. (let ((p (org-inside-LaTeX-fragment-p)))
  17201. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17202. (setq ad-return-value t
  17203. texmathp-why '("Org-mode embedded math" . 0))
  17204. (if p ad-do-it)))))))))
  17205. (defun turn-on-org-cdlatex ()
  17206. "Unconditionally turn on `org-cdlatex-mode'."
  17207. (org-cdlatex-mode 1))
  17208. (defun org-try-cdlatex-tab ()
  17209. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17210. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17211. - inside a LaTeX fragment, or
  17212. - after the first word in a line, where an abbreviation expansion could
  17213. insert a LaTeX environment."
  17214. (when org-cdlatex-mode
  17215. (cond
  17216. ;; Before any word on the line: No expansion possible.
  17217. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17218. ;; Just after first word on the line: Expand it. Make sure it
  17219. ;; cannot happen on headlines, though.
  17220. ((save-excursion
  17221. (skip-chars-backward "a-zA-Z0-9*")
  17222. (skip-chars-backward " \t")
  17223. (and (bolp) (not (org-at-heading-p))))
  17224. (cdlatex-tab) t)
  17225. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17226. (defun org-cdlatex-underscore-caret (&optional arg)
  17227. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17228. Revert to the normal definition outside of these fragments."
  17229. (interactive "P")
  17230. (if (org-inside-LaTeX-fragment-p)
  17231. (call-interactively 'cdlatex-sub-superscript)
  17232. (let (org-cdlatex-mode)
  17233. (call-interactively (key-binding (vector last-input-event))))))
  17234. (defun org-cdlatex-math-modify (&optional arg)
  17235. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17236. Revert to the normal definition outside of these fragments."
  17237. (interactive "P")
  17238. (if (org-inside-LaTeX-fragment-p)
  17239. (call-interactively 'cdlatex-math-modify)
  17240. (let (org-cdlatex-mode)
  17241. (call-interactively (key-binding (vector last-input-event))))))
  17242. (defun org-cdlatex-environment-indent (&optional environment item)
  17243. "Execute `cdlatex-environment' and indent the inserted environment.
  17244. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17245. The inserted environment is indented to current indentation
  17246. unless point is at the beginning of the line, in which the
  17247. environment remains unintended."
  17248. (interactive)
  17249. ;; cdlatex-environment always return nil. Therefore, capture output
  17250. ;; first and determine if an environment was selected.
  17251. (let* ((beg (point-marker))
  17252. (end (copy-marker (point) t))
  17253. (inserted (progn
  17254. (ignore-errors (cdlatex-environment environment item))
  17255. (< beg end)))
  17256. ;; Figure out how many lines to move forward after the
  17257. ;; environment has been inserted.
  17258. (lines (when inserted
  17259. (save-excursion
  17260. (- (loop while (< beg (point))
  17261. with x = 0
  17262. do (forward-line -1)
  17263. (incf x)
  17264. finally return x)
  17265. (if (progn (goto-char beg)
  17266. (and (progn (skip-chars-forward " \t") (eolp))
  17267. (progn (skip-chars-backward " \t") (bolp))))
  17268. 1 0)))))
  17269. (env (org-trim (delete-and-extract-region beg end))))
  17270. (when inserted
  17271. ;; Get indentation of next line unless at column 0.
  17272. (let ((ind (if (bolp) 0
  17273. (save-excursion
  17274. (org-return-indent)
  17275. (prog1 (org-get-indentation)
  17276. (when (progn (skip-chars-forward " \t") (eolp))
  17277. (delete-region beg (point)))))))
  17278. (bol (progn (skip-chars-backward " \t") (bolp))))
  17279. ;; Insert a newline before environment unless at column zero
  17280. ;; to "escape" the current line. Insert a newline if
  17281. ;; something is one the same line as \end{ENVIRONMENT}.
  17282. (insert
  17283. (concat (unless bol "\n") env
  17284. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17285. (unless (zerop ind)
  17286. (save-excursion
  17287. (goto-char beg)
  17288. (while (< (point) end)
  17289. (unless (eolp) (org-indent-line-to ind))
  17290. (forward-line))))
  17291. (goto-char beg)
  17292. (forward-line lines)
  17293. (org-indent-line-to ind)))
  17294. (set-marker beg nil)
  17295. (set-marker end nil)))
  17296. ;;;; LaTeX fragments
  17297. (defun org-inside-LaTeX-fragment-p ()
  17298. "Test if point is inside a LaTeX fragment.
  17299. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17300. sequence appearing also before point.
  17301. Even though the matchers for math are configurable, this function assumes
  17302. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17303. delimiters are skipped when they have been removed by customization.
  17304. The return value is nil, or a cons cell with the delimiter and the
  17305. position of this delimiter.
  17306. This function does a reasonably good job, but can locally be fooled by
  17307. for example currency specifications. For example it will assume being in
  17308. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17309. fragments that are properly closed, but during editing, we have to live
  17310. with the uncertainty caused by missing closing delimiters. This function
  17311. looks only before point, not after."
  17312. (catch 'exit
  17313. (let ((pos (point))
  17314. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17315. (lim (progn
  17316. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17317. (point)))
  17318. dd-on str (start 0) m re)
  17319. (goto-char pos)
  17320. (when dodollar
  17321. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17322. re (nth 1 (assoc "$" org-latex-regexps)))
  17323. (while (string-match re str start)
  17324. (cond
  17325. ((= (match-end 0) (length str))
  17326. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17327. ((= (match-end 0) (- (length str) 5))
  17328. (throw 'exit nil))
  17329. (t (setq start (match-end 0))))))
  17330. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17331. (goto-char pos)
  17332. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17333. (and (match-beginning 2) (throw 'exit nil))
  17334. ;; count $$
  17335. (while (re-search-backward "\\$\\$" lim t)
  17336. (setq dd-on (not dd-on)))
  17337. (goto-char pos)
  17338. (if dd-on (cons "$$" m))))))
  17339. (defun org-inside-latex-macro-p ()
  17340. "Is point inside a LaTeX macro or its arguments?"
  17341. (save-match-data
  17342. (org-in-regexp
  17343. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17344. (defvar org-latex-fragment-image-overlays nil
  17345. "List of overlays carrying the images of latex fragments.")
  17346. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17347. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17348. "Remove all overlays with LaTeX fragment images in current buffer.
  17349. When optional arguments BEG and END are non-nil, remove all
  17350. overlays between them instead. Return t when some overlays were
  17351. removed, nil otherwise."
  17352. (let (removedp)
  17353. (setq org-latex-fragment-image-overlays
  17354. (let ((beg (or beg (point-min)))
  17355. (end (or end (point-max))))
  17356. (org-remove-if
  17357. (lambda (o)
  17358. (and (>= (overlay-start o) beg)
  17359. (<= (overlay-end o) end)
  17360. (progn (delete-overlay o)
  17361. (or removedp (setq removedp t)))))
  17362. org-latex-fragment-image-overlays)))
  17363. removedp))
  17364. (define-obsolete-function-alias
  17365. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17366. (defun org-toggle-latex-fragment (&optional arg)
  17367. "Preview the LaTeX fragment at point, or all locally or globally.
  17368. If the cursor is on a LaTeX fragment, create the image and overlay
  17369. it over the source code, if there is none. Remove it otherwise.
  17370. If there is no fragment at point, display all fragments in the
  17371. current section.
  17372. With prefix ARG, preview or clear image for all fragments in the
  17373. current subtree or in the whole buffer when used before the first
  17374. headline. With a double prefix ARG \\[universal-argument] \
  17375. \\[universal-argument] preview or clear images
  17376. for all fragments in the buffer."
  17377. (interactive "P")
  17378. (unless (buffer-file-name (buffer-base-buffer))
  17379. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17380. (when (display-graphic-p)
  17381. (catch 'exit
  17382. (save-excursion
  17383. (let ((window-start (window-start)) msg)
  17384. (save-restriction
  17385. (cond
  17386. ((or (equal arg '(16))
  17387. (and (equal arg '(4))
  17388. (org-with-limited-levels (org-before-first-heading-p))))
  17389. (if (org-remove-latex-fragment-image-overlays)
  17390. (progn (message "LaTeX fragments images removed from buffer")
  17391. (throw 'exit nil))
  17392. (setq msg "Creating images for buffer...")))
  17393. ((equal arg '(4))
  17394. (org-with-limited-levels (org-back-to-heading t))
  17395. (let ((beg (point))
  17396. (end (progn (org-end-of-subtree t) (point))))
  17397. (if (org-remove-latex-fragment-image-overlays beg end)
  17398. (progn
  17399. (message "LaTeX fragment images removed from subtree")
  17400. (throw 'exit nil))
  17401. (setq msg "Creating images for subtree...")
  17402. (narrow-to-region beg end))))
  17403. ((let ((datum (org-element-context)))
  17404. (when (memq (org-element-type datum)
  17405. '(latex-environment latex-fragment))
  17406. (let* ((beg (org-element-property :begin datum))
  17407. (end (org-element-property :end datum)))
  17408. (if (org-remove-latex-fragment-image-overlays beg end)
  17409. (progn (message "LaTeX fragment image removed")
  17410. (throw 'exit nil))
  17411. (narrow-to-region beg end)
  17412. (setq msg "Creating image..."))))))
  17413. (t
  17414. (org-with-limited-levels
  17415. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17416. (outline-previous-heading)
  17417. (point)))
  17418. (end (progn (outline-next-heading) (point))))
  17419. (if (org-remove-latex-fragment-image-overlays beg end)
  17420. (progn
  17421. (message "LaTeX fragment images removed from section")
  17422. (throw 'exit nil))
  17423. (setq msg "Creating images for section...")
  17424. (narrow-to-region beg end))))))
  17425. (let ((file (buffer-file-name (buffer-base-buffer))))
  17426. (org-format-latex
  17427. (concat org-latex-preview-ltxpng-directory
  17428. (file-name-sans-extension (file-name-nondirectory file)))
  17429. ;; Emacs cannot overlay images from remote hosts.
  17430. ;; Create it in `temporary-file-directory' instead.
  17431. (if (file-remote-p file) temporary-file-directory
  17432. default-directory)
  17433. 'overlays msg 'forbuffer
  17434. org-latex-create-formula-image-program)))
  17435. ;; Work around a bug that doesn't restore window's start
  17436. ;; when widening back the buffer.
  17437. (set-window-start nil window-start)
  17438. (message (concat msg "done")))))))
  17439. (defun org-format-latex
  17440. (prefix &optional dir overlays msg forbuffer processing-type)
  17441. "Replace LaTeX fragments with links to an image, and produce images.
  17442. When optional argument OVERLAYS is non-nil, display the image on
  17443. top of the fragment instead of replacing it.
  17444. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17445. Some of the options can be changed using the variable
  17446. `org-format-latex-options', which see."
  17447. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17448. (unless (eq processing-type 'verbatim)
  17449. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17450. (cnt 0)
  17451. checkdir-flag)
  17452. (goto-char (point-min))
  17453. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17454. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17455. (overlay-recenter (point-max)))
  17456. (while (re-search-forward math-regexp nil t)
  17457. (unless (and overlays
  17458. (eq (get-char-property (point) 'org-overlay-type)
  17459. 'org-latex-overlay))
  17460. (let* ((context (org-element-context))
  17461. (type (org-element-type context)))
  17462. (when (memq type '(latex-environment latex-fragment))
  17463. (let ((block-type (eq type 'latex-environment))
  17464. (value (org-element-property :value context))
  17465. (beg (org-element-property :begin context))
  17466. (end (save-excursion
  17467. (goto-char (org-element-property :end context))
  17468. (skip-chars-backward " \r\t\n")
  17469. (point))))
  17470. (case processing-type
  17471. (mathjax
  17472. ;; Prepare for MathJax processing.
  17473. (if (eq (char-after beg) ?$)
  17474. (save-excursion
  17475. (delete-region beg end)
  17476. (insert "\\(" (substring value 1 -1) "\\)"))
  17477. (goto-char end)))
  17478. ((dvipng imagemagick)
  17479. ;; Process to an image.
  17480. (incf cnt)
  17481. (goto-char beg)
  17482. (let* ((face (face-at-point))
  17483. ;; Get the colors from the face at point.
  17484. (fg
  17485. (let ((color (plist-get org-format-latex-options
  17486. :foreground)))
  17487. (if (and forbuffer (eq color 'auto))
  17488. (face-attribute face :foreground nil 'default)
  17489. color)))
  17490. (bg
  17491. (let ((color (plist-get org-format-latex-options
  17492. :background)))
  17493. (if (and forbuffer (eq color 'auto))
  17494. (face-attribute face :background nil 'default)
  17495. color)))
  17496. (hash (sha1 (prin1-to-string
  17497. (list org-format-latex-header
  17498. org-latex-default-packages-alist
  17499. org-latex-packages-alist
  17500. org-format-latex-options
  17501. forbuffer value fg bg))))
  17502. (absprefix (expand-file-name prefix dir))
  17503. (linkfile (format "%s_%s.png" prefix hash))
  17504. (movefile (format "%s_%s.png" absprefix hash))
  17505. (sep (and block-type "\n\n"))
  17506. (link (concat sep "[[file:" linkfile "]]" sep))
  17507. (options
  17508. (org-combine-plists
  17509. org-format-latex-options
  17510. `(:foreground ,fg :background ,bg))))
  17511. (when msg (message msg cnt))
  17512. (unless checkdir-flag ; Ensure the directory exists.
  17513. (setq checkdir-flag t)
  17514. (let ((todir (file-name-directory absprefix)))
  17515. (unless (file-directory-p todir)
  17516. (make-directory todir t))))
  17517. (unless (file-exists-p movefile)
  17518. (org-create-formula-image
  17519. value movefile options forbuffer processing-type))
  17520. (if overlays
  17521. (progn
  17522. (dolist (o (overlays-in beg end))
  17523. (when (eq (overlay-get o 'org-overlay-type)
  17524. 'org-latex-overlay)
  17525. (delete-overlay o)))
  17526. (let ((ov (make-overlay beg end)))
  17527. (overlay-put ov
  17528. 'org-overlay-type
  17529. 'org-latex-overlay)
  17530. (overlay-put ov 'evaporate t)
  17531. (if (featurep 'xemacs)
  17532. (progn
  17533. (overlay-put ov 'invisible t)
  17534. (overlay-put
  17535. ov 'end-glyph
  17536. (make-glyph
  17537. (vector 'png :file movefile))))
  17538. (overlay-put
  17539. ov 'display
  17540. (list 'image
  17541. :type 'png
  17542. :file movefile
  17543. :ascent 'center)))
  17544. (push ov org-latex-fragment-image-overlays))
  17545. (goto-char end))
  17546. (delete-region beg end)
  17547. (insert
  17548. (org-add-props link
  17549. (list 'org-latex-src
  17550. (replace-regexp-in-string "\"" "" value)
  17551. 'org-latex-src-embed-type
  17552. (if block-type 'paragraph 'character)))))))
  17553. (mathml
  17554. ;; Process to MathML.
  17555. (unless (org-format-latex-mathml-available-p)
  17556. (user-error "LaTeX to MathML converter not configured"))
  17557. (incf cnt)
  17558. (when msg (message msg cnt))
  17559. (goto-char beg)
  17560. (delete-region beg end)
  17561. (insert (org-format-latex-as-mathml
  17562. value block-type prefix dir)))
  17563. (otherwise
  17564. (error "Unknown conversion type %s for LaTeX fragments"
  17565. processing-type)))))))))))
  17566. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17567. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17568. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17569. sucessful, return the portion between \"<math...> </math>\"
  17570. elements otherwise return nil. When MATHML-FILE is specified,
  17571. write the results in to that file. When invoked as an
  17572. interactive command, prompt for LATEX-FRAG, with initial value
  17573. set to the current active region and echo the results for user
  17574. inspection."
  17575. (interactive (list (let ((frag (when (org-region-active-p)
  17576. (buffer-substring-no-properties
  17577. (region-beginning) (region-end)))))
  17578. (read-string "LaTeX Fragment: " frag nil frag))))
  17579. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17580. (let* ((tmp-in-file (file-relative-name
  17581. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17582. (ignore (write-region latex-frag nil tmp-in-file))
  17583. (tmp-out-file (file-relative-name
  17584. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17585. (cmd (format-spec
  17586. org-latex-to-mathml-convert-command
  17587. `((?j . ,(and org-latex-to-mathml-jar-file
  17588. (shell-quote-argument
  17589. (expand-file-name
  17590. org-latex-to-mathml-jar-file))))
  17591. (?I . ,(shell-quote-argument tmp-in-file))
  17592. (?i . ,latex-frag)
  17593. (?o . ,(shell-quote-argument tmp-out-file)))))
  17594. mathml shell-command-output)
  17595. (when (org-called-interactively-p 'any)
  17596. (unless (org-format-latex-mathml-available-p)
  17597. (user-error "LaTeX to MathML converter not configured")))
  17598. (message "Running %s" cmd)
  17599. (setq shell-command-output (shell-command-to-string cmd))
  17600. (setq mathml
  17601. (when (file-readable-p tmp-out-file)
  17602. (with-current-buffer (find-file-noselect tmp-out-file t)
  17603. (goto-char (point-min))
  17604. (when (re-search-forward
  17605. (concat
  17606. (regexp-quote
  17607. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17608. "[^>]*?>"
  17609. "\\(.\\|\n\\)*"
  17610. "</math>")
  17611. nil t)
  17612. (prog1 (match-string 0) (kill-buffer))))))
  17613. (cond
  17614. (mathml
  17615. (setq mathml
  17616. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17617. (when mathml-file
  17618. (write-region mathml nil mathml-file))
  17619. (when (org-called-interactively-p 'any)
  17620. (message mathml)))
  17621. ((message "LaTeX to MathML conversion failed")
  17622. (message shell-command-output)))
  17623. (delete-file tmp-in-file)
  17624. (when (file-exists-p tmp-out-file)
  17625. (delete-file tmp-out-file))
  17626. mathml))
  17627. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17628. prefix &optional dir)
  17629. "Use `org-create-math-formula' but check local cache first."
  17630. (let* ((absprefix (expand-file-name prefix dir))
  17631. (print-length nil) (print-level nil)
  17632. (formula-id (concat
  17633. "formula-"
  17634. (sha1
  17635. (prin1-to-string
  17636. (list latex-frag
  17637. org-latex-to-mathml-convert-command)))))
  17638. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17639. (formula-cache-dir (file-name-directory formula-cache)))
  17640. (unless (file-directory-p formula-cache-dir)
  17641. (make-directory formula-cache-dir t))
  17642. (unless (file-exists-p formula-cache)
  17643. (org-create-math-formula latex-frag formula-cache))
  17644. (if (file-exists-p formula-cache)
  17645. ;; Successful conversion. Return the link to MathML file.
  17646. (org-add-props
  17647. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17648. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17649. 'org-latex-src-embed-type (if latex-frag-type
  17650. 'paragraph 'character)))
  17651. ;; Failed conversion. Return the LaTeX fragment verbatim
  17652. latex-frag)))
  17653. (defun org-create-formula-image (string tofile options buffer &optional type)
  17654. "Create an image from LaTeX source using dvipng or convert.
  17655. This function calls either `org-create-formula-image-with-dvipng'
  17656. or `org-create-formula-image-with-imagemagick' depending on the
  17657. value of `org-latex-create-formula-image-program' or on the value
  17658. of the optional TYPE variable.
  17659. Note: ultimately these two function should be combined as they
  17660. share a good deal of logic."
  17661. (org-check-external-command
  17662. "latex" "needed to convert LaTeX fragments to images")
  17663. (funcall
  17664. (case (or type org-latex-create-formula-image-program)
  17665. (dvipng
  17666. (org-check-external-command
  17667. "dvipng" "needed to convert LaTeX fragments to images")
  17668. #'org-create-formula-image-with-dvipng)
  17669. (imagemagick
  17670. (org-check-external-command
  17671. "convert" "you need to install imagemagick")
  17672. #'org-create-formula-image-with-imagemagick)
  17673. (t (error
  17674. "Invalid value of `org-latex-create-formula-image-program'")))
  17675. string tofile options buffer))
  17676. (declare-function org-export-get-backend "ox" (name))
  17677. (declare-function org-export--get-global-options "ox" (&optional backend))
  17678. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17679. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17680. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17681. (defun org-create-formula--latex-header ()
  17682. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17683. (let ((info (org-combine-plists (org-export--get-global-options
  17684. (org-export-get-backend 'latex))
  17685. (org-export--get-inbuffer-options
  17686. (org-export-get-backend 'latex)))))
  17687. (org-latex-guess-babel-language
  17688. (org-latex-guess-inputenc
  17689. (org-splice-latex-header
  17690. org-format-latex-header
  17691. org-latex-default-packages-alist
  17692. org-latex-packages-alist t
  17693. (plist-get info :latex-header)))
  17694. info)))
  17695. (defun org--get-display-dpi ()
  17696. "Get the DPI of the display.
  17697. Assumes that the display has the same pixel width in the
  17698. horizontal and vertical directions."
  17699. (if (display-graphic-p)
  17700. (round (/ (display-pixel-height)
  17701. (/ (display-mm-height) 25.4)))
  17702. (error "Attempt to calculate the dpi of a non-graphic display")))
  17703. ;; This function borrows from Ganesh Swami's latex2png.el
  17704. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17705. "This calls dvipng."
  17706. (require 'ox-latex)
  17707. (let* ((tmpdir (if (featurep 'xemacs)
  17708. (temp-directory)
  17709. temporary-file-directory))
  17710. (texfilebase (make-temp-name
  17711. (expand-file-name "orgtex" tmpdir)))
  17712. (texfile (concat texfilebase ".tex"))
  17713. (dvifile (concat texfilebase ".dvi"))
  17714. (pngfile (concat texfilebase ".png"))
  17715. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17716. ;; This assumes that the display has the same pixel width in
  17717. ;; the horizontal and vertical directions
  17718. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17719. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17720. "Black"))
  17721. (bg (or (plist-get options (if buffer :background :html-background))
  17722. "Transparent")))
  17723. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17724. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17725. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17726. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17727. (let ((latex-header (org-create-formula--latex-header)))
  17728. (with-temp-file texfile
  17729. (insert latex-header)
  17730. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17731. (let ((dir default-directory))
  17732. (ignore-errors
  17733. (cd tmpdir)
  17734. (call-process "latex" nil nil nil texfile))
  17735. (cd dir))
  17736. (if (not (file-exists-p dvifile))
  17737. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17738. (ignore-errors
  17739. (if (featurep 'xemacs)
  17740. (call-process "dvipng" nil nil nil
  17741. "-fg" fg "-bg" bg
  17742. "-T" "tight"
  17743. "-o" pngfile
  17744. dvifile)
  17745. (call-process "dvipng" nil nil nil
  17746. "-fg" fg "-bg" bg
  17747. "-D" dpi
  17748. ;;"-x" scale "-y" scale
  17749. "-T" "tight"
  17750. "-o" pngfile
  17751. dvifile)))
  17752. (if (not (file-exists-p pngfile))
  17753. (if org-format-latex-signal-error
  17754. (error "Failed to create png file from %s" texfile)
  17755. (message "Failed to create png file from %s" texfile)
  17756. nil)
  17757. ;; Use the requested file name and clean up
  17758. (copy-file pngfile tofile 'replace)
  17759. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17760. (if (file-exists-p (concat texfilebase e))
  17761. (delete-file (concat texfilebase e))))
  17762. pngfile))))
  17763. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17764. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17765. "This calls convert, which is included into imagemagick."
  17766. (require 'ox-latex)
  17767. (let* ((tmpdir (if (featurep 'xemacs)
  17768. (temp-directory)
  17769. temporary-file-directory))
  17770. (texfilebase (make-temp-name
  17771. (expand-file-name "orgtex" tmpdir)))
  17772. (texfile (concat texfilebase ".tex"))
  17773. (pdffile (concat texfilebase ".pdf"))
  17774. (pngfile (concat texfilebase ".png"))
  17775. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17776. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17777. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17778. "black"))
  17779. (bg (or (plist-get options (if buffer :background :html-background))
  17780. "white")))
  17781. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17782. (setq fg (org-latex-color-format fg)))
  17783. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17784. (setq bg (org-latex-color-format
  17785. (if (string= bg "Transparent") "white" bg))))
  17786. (let ((latex-header (org-create-formula--latex-header)))
  17787. (with-temp-file texfile
  17788. (insert latex-header)
  17789. (insert "\n\\begin{document}\n"
  17790. "\\definecolor{fg}{rgb}{" fg "}\n"
  17791. "\\definecolor{bg}{rgb}{" bg "}\n"
  17792. "\n\\pagecolor{bg}\n"
  17793. "\n{\\color{fg}\n"
  17794. string
  17795. "\n}\n"
  17796. "\n\\end{document}\n")))
  17797. (org-latex-compile texfile t)
  17798. (if (not (file-exists-p pdffile))
  17799. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17800. (ignore-errors
  17801. (if (featurep 'xemacs)
  17802. (call-process "convert" nil nil nil
  17803. "-density" "96"
  17804. "-trim"
  17805. "-antialias"
  17806. pdffile
  17807. "-quality" "100"
  17808. ;; "-sharpen" "0x1.0"
  17809. pngfile)
  17810. (call-process "convert" nil nil nil
  17811. "-density" dpi
  17812. "-trim"
  17813. "-antialias"
  17814. pdffile
  17815. "-quality" "100"
  17816. ;; "-sharpen" "0x1.0"
  17817. pngfile)))
  17818. (if (not (file-exists-p pngfile))
  17819. (if org-format-latex-signal-error
  17820. (error "Failed to create png file from %s" texfile)
  17821. (message "Failed to create png file from %s" texfile)
  17822. nil)
  17823. ;; Use the requested file name and clean up
  17824. (copy-file pngfile tofile 'replace)
  17825. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17826. (if (file-exists-p (concat texfilebase e))
  17827. (delete-file (concat texfilebase e))))
  17828. pngfile))))
  17829. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17830. "Fill a LaTeX header template TPL.
  17831. In the template, the following place holders will be recognized:
  17832. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17833. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17834. [PACKAGES] \\usepackage statements for PKG
  17835. [NO-PACKAGES] do not include PKG
  17836. [EXTRA] the string EXTRA
  17837. [NO-EXTRA] do not include EXTRA
  17838. For backward compatibility, if both the positive and the negative place
  17839. holder is missing, the positive one (without the \"NO-\") will be
  17840. assumed to be present at the end of the template.
  17841. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17842. EXTRA is a string.
  17843. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17844. (let (rpl (end ""))
  17845. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17846. (setq rpl (if (or (match-end 1) (not def-pkg))
  17847. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17848. tpl (replace-match rpl t t tpl))
  17849. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17850. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17851. (setq rpl (if (or (match-end 1) (not pkg))
  17852. "" (org-latex-packages-to-string pkg snippets-p t))
  17853. tpl (replace-match rpl t t tpl))
  17854. (if pkg (setq end
  17855. (concat end "\n"
  17856. (org-latex-packages-to-string pkg snippets-p)))))
  17857. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17858. (setq rpl (if (or (match-end 1) (not extra))
  17859. "" (concat extra "\n"))
  17860. tpl (replace-match rpl t t tpl))
  17861. (if (and extra (string-match "\\S-" extra))
  17862. (setq end (concat end "\n" extra))))
  17863. (if (string-match "\\S-" end)
  17864. (concat tpl "\n" end)
  17865. tpl)))
  17866. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17867. "Turn an alist of packages into a string with the \\usepackage macros."
  17868. (setq pkg (mapconcat (lambda(p)
  17869. (cond
  17870. ((stringp p) p)
  17871. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17872. (format "%% Package %s omitted" (cadr p)))
  17873. ((equal "" (car p))
  17874. (format "\\usepackage{%s}" (cadr p)))
  17875. (t
  17876. (format "\\usepackage[%s]{%s}"
  17877. (car p) (cadr p)))))
  17878. pkg
  17879. "\n"))
  17880. (if newline (concat pkg "\n") pkg))
  17881. (defun org-dvipng-color (attr)
  17882. "Return a RGB color specification for dvipng."
  17883. (apply 'format "rgb %s %s %s"
  17884. (mapcar 'org-normalize-color
  17885. (if (featurep 'xemacs)
  17886. (color-rgb-components
  17887. (face-property 'default
  17888. (cond ((eq attr :foreground) 'foreground)
  17889. ((eq attr :background) 'background))))
  17890. (color-values (face-attribute 'default attr nil))))))
  17891. (defun org-dvipng-color-format (color-name)
  17892. "Convert COLOR-NAME to a RGB color value for dvipng."
  17893. (apply 'format "rgb %s %s %s"
  17894. (mapcar 'org-normalize-color
  17895. (color-values color-name))))
  17896. (defun org-latex-color (attr)
  17897. "Return a RGB color for the LaTeX color package."
  17898. (apply 'format "%s,%s,%s"
  17899. (mapcar 'org-normalize-color
  17900. (if (featurep 'xemacs)
  17901. (color-rgb-components
  17902. (face-property 'default
  17903. (cond ((eq attr :foreground) 'foreground)
  17904. ((eq attr :background) 'background))))
  17905. (color-values (face-attribute 'default attr nil))))))
  17906. (defun org-latex-color-format (color-name)
  17907. "Convert COLOR-NAME to a RGB color value."
  17908. (apply 'format "%s,%s,%s"
  17909. (mapcar 'org-normalize-color
  17910. (color-values color-name))))
  17911. (defun org-normalize-color (value)
  17912. "Return string to be used as color value for an RGB component."
  17913. (format "%g" (/ value 65535.0)))
  17914. ;; Image display
  17915. (defvar org-inline-image-overlays nil)
  17916. (make-variable-buffer-local 'org-inline-image-overlays)
  17917. (defun org-toggle-inline-images (&optional include-linked)
  17918. "Toggle the display of inline images.
  17919. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17920. (interactive "P")
  17921. (if org-inline-image-overlays
  17922. (progn
  17923. (org-remove-inline-images)
  17924. (when (org-called-interactively-p 'interactive)
  17925. (message "Inline image display turned off")))
  17926. (org-display-inline-images include-linked)
  17927. (when (org-called-interactively-p 'interactive)
  17928. (message (if org-inline-image-overlays
  17929. (format "%d images displayed inline"
  17930. (length org-inline-image-overlays))
  17931. "No images to display inline")))))
  17932. (defun org-redisplay-inline-images ()
  17933. "Refresh the display of inline images."
  17934. (interactive)
  17935. (if (not org-inline-image-overlays)
  17936. (org-toggle-inline-images)
  17937. (org-toggle-inline-images)
  17938. (org-toggle-inline-images)))
  17939. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17940. "Display inline images.
  17941. An inline image is a link which follows either of these
  17942. conventions:
  17943. 1. Its path is a file with an extension matching return value
  17944. from `image-file-name-regexp' and it has no contents.
  17945. 2. Its description consists in a single link of the previous
  17946. type.
  17947. When optional argument INCLUDE-LINKED is non-nil, also links with
  17948. a text description part will be inlined. This can be nice for
  17949. a quick look at those images, but it does not reflect what
  17950. exported files will look like.
  17951. When optional argument REFRESH is non-nil, refresh existing
  17952. images between BEG and END. This will create new image displays
  17953. only if necessary. BEG and END default to the buffer
  17954. boundaries."
  17955. (interactive "P")
  17956. (when (display-graphic-p)
  17957. (unless refresh
  17958. (org-remove-inline-images)
  17959. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17960. (org-with-wide-buffer
  17961. (goto-char (or beg (point-min)))
  17962. (let ((case-fold-search t)
  17963. (file-extension-re (org-image-file-name-regexp)))
  17964. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17965. (let ((link (save-match-data (org-element-context))))
  17966. ;; Check if we're at an inline image.
  17967. (when (and (equal (org-element-property :type link) "file")
  17968. (or include-linked
  17969. (not (org-element-property :contents-begin link)))
  17970. (let ((parent (org-element-property :parent link)))
  17971. (or (not (eq (org-element-type parent) 'link))
  17972. (not (cdr (org-element-contents parent)))))
  17973. (org-string-match-p file-extension-re
  17974. (org-element-property :path link)))
  17975. (let ((file (expand-file-name
  17976. (org-link-unescape
  17977. (org-element-property :path link)))))
  17978. (when (file-exists-p file)
  17979. (let ((width
  17980. ;; Apply `org-image-actual-width' specifications.
  17981. (cond
  17982. ((not (image-type-available-p 'imagemagick)) nil)
  17983. ((eq org-image-actual-width t) nil)
  17984. ((listp org-image-actual-width)
  17985. (or
  17986. ;; First try to find a width among
  17987. ;; attributes associated to the paragraph
  17988. ;; containing link.
  17989. (let ((paragraph
  17990. (let ((e link))
  17991. (while (and (setq e (org-element-property
  17992. :parent e))
  17993. (not (eq (org-element-type e)
  17994. 'paragraph))))
  17995. e)))
  17996. (when paragraph
  17997. (save-excursion
  17998. (goto-char (org-element-property :begin paragraph))
  17999. (when
  18000. (re-search-forward
  18001. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18002. (org-element-property
  18003. :post-affiliated paragraph)
  18004. t)
  18005. (string-to-number (match-string 1))))))
  18006. ;; Otherwise, fall-back to provided number.
  18007. (car org-image-actual-width)))
  18008. ((numberp org-image-actual-width)
  18009. org-image-actual-width)))
  18010. (old (get-char-property-and-overlay
  18011. (org-element-property :begin link)
  18012. 'org-image-overlay)))
  18013. (if (and (car-safe old) refresh)
  18014. (image-refresh (overlay-get (cdr old) 'display))
  18015. (let ((image (create-image file
  18016. (and width 'imagemagick)
  18017. nil
  18018. :width width)))
  18019. (when image
  18020. (let* ((link
  18021. ;; If inline image is the description
  18022. ;; of another link, be sure to
  18023. ;; consider the latter as the one to
  18024. ;; apply the overlay on.
  18025. (let ((parent
  18026. (org-element-property :parent link)))
  18027. (if (eq (org-element-type parent) 'link)
  18028. parent
  18029. link)))
  18030. (ov (make-overlay
  18031. (org-element-property :begin link)
  18032. (progn
  18033. (goto-char
  18034. (org-element-property :end link))
  18035. (skip-chars-backward " \t")
  18036. (point)))))
  18037. (overlay-put ov 'display image)
  18038. (overlay-put ov 'face 'default)
  18039. (overlay-put ov 'org-image-overlay t)
  18040. (overlay-put
  18041. ov 'modification-hooks
  18042. (list 'org-display-inline-remove-overlay))
  18043. (push ov org-inline-image-overlays)))))))))))))))
  18044. (define-obsolete-function-alias
  18045. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18046. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18047. "Remove inline-display overlay if a corresponding region is modified."
  18048. (let ((inhibit-modification-hooks t))
  18049. (when (and ov after)
  18050. (delete ov org-inline-image-overlays)
  18051. (delete-overlay ov))))
  18052. (defun org-remove-inline-images ()
  18053. "Remove inline display of images."
  18054. (interactive)
  18055. (mapc 'delete-overlay org-inline-image-overlays)
  18056. (setq org-inline-image-overlays nil))
  18057. ;;;; Key bindings
  18058. ;; Outline functions from `outline-mode-prefix-map'
  18059. ;; that can be remapped in Org:
  18060. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18061. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18062. (define-key org-mode-map [remap outline-forward-same-level]
  18063. 'org-forward-heading-same-level)
  18064. (define-key org-mode-map [remap outline-backward-same-level]
  18065. 'org-backward-heading-same-level)
  18066. (define-key org-mode-map [remap show-branches]
  18067. 'org-kill-note-or-show-branches)
  18068. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18069. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18070. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18071. (define-key org-mode-map [remap outline-next-visible-heading]
  18072. 'org-next-visible-heading)
  18073. (define-key org-mode-map [remap outline-previous-visible-heading]
  18074. 'org-previous-visible-heading)
  18075. ;; Outline functions from `outline-mode-prefix-map' that can not
  18076. ;; be remapped in Org:
  18077. ;; - the column "key binding" shows whether the Outline function is still
  18078. ;; available in Org mode on the same key that it has been bound to in
  18079. ;; Outline mode:
  18080. ;; - "overridden": key used for a different functionality in Org mode
  18081. ;; - else: key still bound to the same Outline function in Org mode
  18082. ;; | Outline function | key binding | Org replacement |
  18083. ;; |------------------------------------+-------------+--------------------------|
  18084. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18085. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18086. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18087. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18088. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18089. ;; | `show-entry' | overridden | no replacement |
  18090. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18091. ;; | `show-branches' | `C-c C-k' | still same function |
  18092. ;; | `show-subtree' | overridden | visibility cycling |
  18093. ;; | `show-all' | overridden | no replacement |
  18094. ;; | `hide-subtree' | overridden | visibility cycling |
  18095. ;; | `hide-body' | overridden | no replacement |
  18096. ;; | `hide-entry' | overridden | visibility cycling |
  18097. ;; | `hide-leaves' | overridden | no replacement |
  18098. ;; | `hide-sublevels' | overridden | no replacement |
  18099. ;; | `hide-other' | overridden | no replacement |
  18100. ;; Make `C-c C-x' a prefix key
  18101. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18102. ;; TAB key with modifiers
  18103. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18104. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18105. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18106. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18107. ;; The following line is necessary under Suse GNU/Linux
  18108. (unless (featurep 'xemacs)
  18109. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18110. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18111. (define-key org-mode-map [backtab] 'org-shifttab)
  18112. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18113. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18114. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18115. ;; Cursor keys with modifiers
  18116. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18117. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18118. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18119. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18120. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18121. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18122. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18123. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18124. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18125. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18126. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18127. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18128. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18129. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18130. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18131. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18132. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18133. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18134. ;; Babel keys
  18135. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18136. (mapc (lambda (pair)
  18137. (define-key org-babel-map (car pair) (cdr pair)))
  18138. org-babel-key-bindings)
  18139. ;;; Extra keys for tty access.
  18140. ;; We only set them when really needed because otherwise the
  18141. ;; menus don't show the simple keys
  18142. (when (or org-use-extra-keys
  18143. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18144. (not window-system))
  18145. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18146. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18147. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18148. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18149. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18150. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18151. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18152. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18153. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18154. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18155. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18156. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18157. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18158. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18159. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18160. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18161. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18162. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18163. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18164. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18165. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18166. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18167. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18168. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18169. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18170. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18171. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18172. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18173. ;; All the other keys
  18174. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18175. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18176. (if (boundp 'narrow-map)
  18177. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18178. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18179. (if (boundp 'narrow-map)
  18180. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18181. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18182. (if (boundp 'narrow-map)
  18183. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18184. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18185. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18186. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18187. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18188. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18189. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18190. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18191. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18192. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18193. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18194. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18195. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18196. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18197. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18198. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18199. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18200. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18201. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18202. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18203. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18204. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18205. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18206. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18207. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18208. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18209. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18210. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18211. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18212. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18213. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18214. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18215. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18216. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18217. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18218. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18219. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18220. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18221. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18222. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18223. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18224. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18225. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18226. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18227. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18228. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18229. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18230. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18231. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18232. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18233. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18234. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18235. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18236. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18237. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18238. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18239. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18240. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18241. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18242. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18243. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18244. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18245. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18246. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18247. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18248. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18249. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18250. (org-defkey org-mode-map "\C-m" 'org-return)
  18251. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18252. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18253. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18254. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18255. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18256. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18257. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18258. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18259. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18260. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18261. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18262. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18263. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18264. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18265. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18266. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18267. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18268. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18269. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18270. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18271. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18272. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18273. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18274. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18275. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18277. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18292. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18293. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18294. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18295. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18296. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18297. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18298. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18299. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18300. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18301. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18302. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18303. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18304. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18305. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18306. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18307. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18308. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18309. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18310. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18311. (when (featurep 'xemacs)
  18312. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18313. (defconst org-speed-commands-default
  18314. '(
  18315. ("Outline Navigation")
  18316. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18317. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18318. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18319. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18320. ("F" . org-next-block)
  18321. ("B" . org-previous-block)
  18322. ("u" . (org-speed-move-safe 'outline-up-heading))
  18323. ("j" . org-goto)
  18324. ("g" . (org-refile t))
  18325. ("Outline Visibility")
  18326. ("c" . org-cycle)
  18327. ("C" . org-shifttab)
  18328. (" " . org-display-outline-path)
  18329. ("s" . org-narrow-to-subtree)
  18330. ("=" . org-columns)
  18331. ("Outline Structure Editing")
  18332. ("U" . org-metaup)
  18333. ("D" . org-metadown)
  18334. ("r" . org-metaright)
  18335. ("l" . org-metaleft)
  18336. ("R" . org-shiftmetaright)
  18337. ("L" . org-shiftmetaleft)
  18338. ("i" . (progn (forward-char 1) (call-interactively
  18339. 'org-insert-heading-respect-content)))
  18340. ("^" . org-sort)
  18341. ("w" . org-refile)
  18342. ("a" . org-archive-subtree-default-with-confirmation)
  18343. ("@" . org-mark-subtree)
  18344. ("#" . org-toggle-comment)
  18345. ("Clock Commands")
  18346. ("I" . org-clock-in)
  18347. ("O" . org-clock-out)
  18348. ("Meta Data Editing")
  18349. ("t" . org-todo)
  18350. ("," . (org-priority))
  18351. ("0" . (org-priority ?\ ))
  18352. ("1" . (org-priority ?A))
  18353. ("2" . (org-priority ?B))
  18354. ("3" . (org-priority ?C))
  18355. (":" . org-set-tags-command)
  18356. ("e" . org-set-effort)
  18357. ("E" . org-inc-effort)
  18358. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18359. (org-entry-put (point) "APPT_WARNTIME" m)))
  18360. ("Agenda Views etc")
  18361. ("v" . org-agenda)
  18362. ("/" . org-sparse-tree)
  18363. ("Misc")
  18364. ("o" . org-open-at-point)
  18365. ("?" . org-speed-command-help)
  18366. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18367. (">" . (org-agenda-remove-restriction-lock))
  18368. )
  18369. "The default speed commands.")
  18370. (defun org-print-speed-command (e)
  18371. (if (> (length (car e)) 1)
  18372. (progn
  18373. (princ "\n")
  18374. (princ (car e))
  18375. (princ "\n")
  18376. (princ (make-string (length (car e)) ?-))
  18377. (princ "\n"))
  18378. (princ (car e))
  18379. (princ " ")
  18380. (if (symbolp (cdr e))
  18381. (princ (symbol-name (cdr e)))
  18382. (prin1 (cdr e)))
  18383. (princ "\n")))
  18384. (defun org-speed-command-help ()
  18385. "Show the available speed commands."
  18386. (interactive)
  18387. (if (not org-use-speed-commands)
  18388. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18389. (with-output-to-temp-buffer "*Help*"
  18390. (princ "User-defined Speed commands\n===========================\n")
  18391. (mapc 'org-print-speed-command org-speed-commands-user)
  18392. (princ "\n")
  18393. (princ "Built-in Speed commands\n=======================\n")
  18394. (mapc 'org-print-speed-command org-speed-commands-default))
  18395. (with-current-buffer "*Help*"
  18396. (setq truncate-lines t))))
  18397. (defun org-speed-move-safe (cmd)
  18398. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18399. If not, return to the original position and throw an error."
  18400. (interactive)
  18401. (let ((pos (point)))
  18402. (call-interactively cmd)
  18403. (unless (and (bolp) (org-at-heading-p))
  18404. (goto-char pos)
  18405. (error "Boundary reached while executing %s" cmd))))
  18406. (defvar org-self-insert-command-undo-counter 0)
  18407. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18408. (defvar org-speed-command nil)
  18409. (define-obsolete-function-alias
  18410. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18411. (defun org-speed-command-activate (keys)
  18412. "Hook for activating single-letter speed commands.
  18413. `org-speed-commands-default' specifies a minimal command set.
  18414. Use `org-speed-commands-user' for further customization."
  18415. (when (or (and (bolp) (looking-at org-outline-regexp))
  18416. (and (functionp org-use-speed-commands)
  18417. (funcall org-use-speed-commands)))
  18418. (cdr (assoc keys (append org-speed-commands-user
  18419. org-speed-commands-default)))))
  18420. (define-obsolete-function-alias
  18421. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18422. (defun org-babel-speed-command-activate (keys)
  18423. "Hook for activating single-letter code block commands."
  18424. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18425. (cdr (assoc keys org-babel-key-bindings))))
  18426. (defcustom org-speed-command-hook
  18427. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18428. "Hook for activating speed commands at strategic locations.
  18429. Hook functions are called in sequence until a valid handler is
  18430. found.
  18431. Each hook takes a single argument, a user-pressed command key
  18432. which is also a `self-insert-command' from the global map.
  18433. Within the hook, examine the cursor position and the command key
  18434. and return nil or a valid handler as appropriate. Handler could
  18435. be one of an interactive command, a function, or a form.
  18436. Set `org-use-speed-commands' to non-nil value to enable this
  18437. hook. The default setting is `org-speed-command-activate'."
  18438. :group 'org-structure
  18439. :version "24.1"
  18440. :type 'hook)
  18441. (defun org-self-insert-command (N)
  18442. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18443. If the cursor is in a table looking at whitespace, the whitespace is
  18444. overwritten, and the table is not marked as requiring realignment."
  18445. (interactive "p")
  18446. (org-check-before-invisible-edit 'insert)
  18447. (cond
  18448. ((and org-use-speed-commands
  18449. (let ((kv (this-command-keys-vector)))
  18450. (setq org-speed-command
  18451. (run-hook-with-args-until-success
  18452. 'org-speed-command-hook
  18453. (make-string 1 (aref kv (1- (length kv))))))))
  18454. (cond
  18455. ((commandp org-speed-command)
  18456. (setq this-command org-speed-command)
  18457. (call-interactively org-speed-command))
  18458. ((functionp org-speed-command)
  18459. (funcall org-speed-command))
  18460. ((and org-speed-command (listp org-speed-command))
  18461. (eval org-speed-command))
  18462. (t (let (org-use-speed-commands)
  18463. (call-interactively 'org-self-insert-command)))))
  18464. ((and
  18465. (org-table-p)
  18466. (progn
  18467. ;; check if we blank the field, and if that triggers align
  18468. (and (featurep 'org-table) org-table-auto-blank-field
  18469. (memq last-command
  18470. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18471. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18472. ;; got extra space, this field does not determine column width
  18473. (let (org-table-may-need-update) (org-table-blank-field))
  18474. ;; no extra space, this field may determine column width
  18475. (org-table-blank-field)))
  18476. t)
  18477. (eq N 1)
  18478. (looking-at "[^|\n]* |"))
  18479. (let (org-table-may-need-update)
  18480. (goto-char (1- (match-end 0)))
  18481. (backward-delete-char 1)
  18482. (goto-char (match-beginning 0))
  18483. (self-insert-command N)))
  18484. (t
  18485. (setq org-table-may-need-update t)
  18486. (self-insert-command N)
  18487. (org-fix-tags-on-the-fly)
  18488. (if org-self-insert-cluster-for-undo
  18489. (if (not (eq last-command 'org-self-insert-command))
  18490. (setq org-self-insert-command-undo-counter 1)
  18491. (if (>= org-self-insert-command-undo-counter 20)
  18492. (setq org-self-insert-command-undo-counter 1)
  18493. (and (> org-self-insert-command-undo-counter 0)
  18494. buffer-undo-list (listp buffer-undo-list)
  18495. (not (cadr buffer-undo-list)) ; remove nil entry
  18496. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18497. (setq org-self-insert-command-undo-counter
  18498. (1+ org-self-insert-command-undo-counter))))))))
  18499. (defun org-check-before-invisible-edit (kind)
  18500. "Check is editing if kind KIND would be dangerous with invisible text around.
  18501. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18502. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18503. (if (and org-catch-invisible-edits
  18504. (or (not (boundp 'visible-mode)) (not visible-mode))
  18505. (or (get-char-property (point) 'invisible)
  18506. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18507. ;; OK, we need to take a closer look
  18508. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18509. (invisible-before-point (if (bobp) nil (get-char-property
  18510. (1- (point)) 'invisible)))
  18511. (border-and-ok-direction
  18512. (or
  18513. ;; Check if we are acting predictably before invisible text
  18514. (and invisible-at-point (not invisible-before-point)
  18515. (memq kind '(insert delete-backward)))
  18516. ;; Check if we are acting predictably after invisible text
  18517. ;; This works not well, and I have turned it off. It seems
  18518. ;; better to always show and stop after invisible text.
  18519. ;; (and (not invisible-at-point) invisible-before-point
  18520. ;; (memq kind '(insert delete)))
  18521. )))
  18522. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18523. (memq invisible-before-point '(outline org-hide-block t)))
  18524. (if (eq org-catch-invisible-edits 'error)
  18525. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18526. (if (and org-custom-properties-overlays
  18527. (y-or-n-p "Display invisible properties in this buffer? "))
  18528. (org-toggle-custom-properties-visibility)
  18529. ;; Make the area visible
  18530. (save-excursion
  18531. (if invisible-before-point
  18532. (goto-char (previous-single-char-property-change
  18533. (point) 'invisible)))
  18534. (show-subtree))
  18535. (cond
  18536. ((eq org-catch-invisible-edits 'show)
  18537. ;; That's it, we do the edit after showing
  18538. (message
  18539. "Unfolding invisible region around point before editing")
  18540. (sit-for 1))
  18541. ((and (eq org-catch-invisible-edits 'smart)
  18542. border-and-ok-direction)
  18543. (message "Unfolding invisible region around point before editing"))
  18544. (t
  18545. ;; Don't do the edit, make the user repeat it in full visibility
  18546. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18547. (defun org-fix-tags-on-the-fly ()
  18548. (when (and (equal (char-after (point-at-bol)) ?*)
  18549. (org-at-heading-p))
  18550. (org-align-tags-here org-tags-column)))
  18551. (defun org-delete-backward-char (N)
  18552. "Like `delete-backward-char', insert whitespace at field end in tables.
  18553. When deleting backwards, in tables this function will insert whitespace in
  18554. front of the next \"|\" separator, to keep the table aligned. The table will
  18555. still be marked for re-alignment if the field did fill the entire column,
  18556. because, in this case the deletion might narrow the column."
  18557. (interactive "p")
  18558. (save-match-data
  18559. (org-check-before-invisible-edit 'delete-backward)
  18560. (if (and (org-table-p)
  18561. (eq N 1)
  18562. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18563. (looking-at ".*?|"))
  18564. (let ((pos (point))
  18565. (noalign (looking-at "[^|\n\r]* |"))
  18566. (c org-table-may-need-update))
  18567. (backward-delete-char N)
  18568. (if (not overwrite-mode)
  18569. (progn
  18570. (skip-chars-forward "^|")
  18571. (insert " ")
  18572. (goto-char (1- pos))))
  18573. ;; noalign: if there were two spaces at the end, this field
  18574. ;; does not determine the width of the column.
  18575. (if noalign (setq org-table-may-need-update c)))
  18576. (backward-delete-char N)
  18577. (org-fix-tags-on-the-fly))))
  18578. (defun org-delete-char (N)
  18579. "Like `delete-char', but insert whitespace at field end in tables.
  18580. When deleting characters, in tables this function will insert whitespace in
  18581. front of the next \"|\" separator, to keep the table aligned. The table will
  18582. still be marked for re-alignment if the field did fill the entire column,
  18583. because, in this case the deletion might narrow the column."
  18584. (interactive "p")
  18585. (save-match-data
  18586. (org-check-before-invisible-edit 'delete)
  18587. (if (and (org-table-p)
  18588. (not (bolp))
  18589. (not (= (char-after) ?|))
  18590. (eq N 1))
  18591. (if (looking-at ".*?|")
  18592. (let ((pos (point))
  18593. (noalign (looking-at "[^|\n\r]* |"))
  18594. (c org-table-may-need-update))
  18595. (replace-match
  18596. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18597. (goto-char pos)
  18598. ;; noalign: if there were two spaces at the end, this field
  18599. ;; does not determine the width of the column.
  18600. (if noalign (setq org-table-may-need-update c)))
  18601. (delete-char N))
  18602. (delete-char N)
  18603. (org-fix-tags-on-the-fly))))
  18604. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18605. (put 'org-self-insert-command 'delete-selection
  18606. (lambda ()
  18607. (not (run-hook-with-args-until-success
  18608. 'self-insert-uses-region-functions))))
  18609. (put 'orgtbl-self-insert-command 'delete-selection
  18610. (lambda ()
  18611. (not (run-hook-with-args-until-success
  18612. 'self-insert-uses-region-functions))))
  18613. (put 'org-delete-char 'delete-selection 'supersede)
  18614. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18615. (put 'org-yank 'delete-selection 'yank)
  18616. ;; Make `flyspell-mode' delay after some commands
  18617. (put 'org-self-insert-command 'flyspell-delayed t)
  18618. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18619. (put 'org-delete-char 'flyspell-delayed t)
  18620. (put 'org-delete-backward-char 'flyspell-delayed t)
  18621. ;; Make pabbrev-mode expand after org-mode commands
  18622. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18623. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18624. (defun org-remap (map &rest commands)
  18625. "In MAP, remap the functions given in COMMANDS.
  18626. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18627. (let (new old)
  18628. (while commands
  18629. (setq old (pop commands) new (pop commands))
  18630. (if (fboundp 'command-remapping)
  18631. (org-defkey map (vector 'remap old) new)
  18632. (substitute-key-definition old new map global-map)))))
  18633. (defun org-transpose-words ()
  18634. "Transpose words for Org.
  18635. This uses the `org-mode-transpose-word-syntax-table' syntax
  18636. table, which interprets characters in `org-emphasis-alist' as
  18637. word constituents."
  18638. (interactive)
  18639. (with-syntax-table org-mode-transpose-word-syntax-table
  18640. (call-interactively 'transpose-words)))
  18641. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18642. (when (eq org-enable-table-editor 'optimized)
  18643. ;; If the user wants maximum table support, we need to hijack
  18644. ;; some standard editing functions
  18645. (org-remap org-mode-map
  18646. 'self-insert-command 'org-self-insert-command
  18647. 'delete-char 'org-delete-char
  18648. 'delete-backward-char 'org-delete-backward-char)
  18649. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18650. (defvar org-ctrl-c-ctrl-c-hook nil
  18651. "Hook for functions attaching themselves to `C-c C-c'.
  18652. This can be used to add additional functionality to the C-c C-c
  18653. key which executes context-dependent commands. This hook is run
  18654. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18655. run after the last test.
  18656. Each function will be called with no arguments. The function
  18657. must check if the context is appropriate for it to act. If yes,
  18658. it should do its thing and then return a non-nil value. If the
  18659. context is wrong, just do nothing and return nil.")
  18660. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18661. "Hook for functions attaching themselves to `C-c C-c'.
  18662. This can be used to add additional functionality to the C-c C-c
  18663. key which executes context-dependent commands. This hook is run
  18664. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18665. before the first test.
  18666. Each function will be called with no arguments. The function
  18667. must check if the context is appropriate for it to act. If yes,
  18668. it should do its thing and then return a non-nil value. If the
  18669. context is wrong, just do nothing and return nil.")
  18670. (defvar org-tab-first-hook nil
  18671. "Hook for functions to attach themselves to TAB.
  18672. See `org-ctrl-c-ctrl-c-hook' for more information.
  18673. This hook runs as the first action when TAB is pressed, even before
  18674. `org-cycle' messes around with the `outline-regexp' to cater for
  18675. inline tasks and plain list item folding.
  18676. If any function in this hook returns t, any other actions that
  18677. would have been caused by TAB (such as table field motion or visibility
  18678. cycling) will not occur.")
  18679. (defvar org-tab-after-check-for-table-hook nil
  18680. "Hook for functions to attach themselves to TAB.
  18681. See `org-ctrl-c-ctrl-c-hook' for more information.
  18682. This hook runs after it has been established that the cursor is not in a
  18683. table, but before checking if the cursor is in a headline or if global cycling
  18684. should be done.
  18685. If any function in this hook returns t, not other actions like visibility
  18686. cycling will be done.")
  18687. (defvar org-tab-after-check-for-cycling-hook nil
  18688. "Hook for functions to attach themselves to TAB.
  18689. See `org-ctrl-c-ctrl-c-hook' for more information.
  18690. This hook runs after it has been established that not table field motion and
  18691. not visibility should be done because of current context. This is probably
  18692. the place where a package like yasnippets can hook in.")
  18693. (defvar org-tab-before-tab-emulation-hook nil
  18694. "Hook for functions to attach themselves to TAB.
  18695. See `org-ctrl-c-ctrl-c-hook' for more information.
  18696. This hook runs after every other options for TAB have been exhausted, but
  18697. before indentation and \t insertion takes place.")
  18698. (defvar org-metaleft-hook nil
  18699. "Hook for functions attaching themselves to `M-left'.
  18700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18701. (defvar org-metaright-hook nil
  18702. "Hook for functions attaching themselves to `M-right'.
  18703. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18704. (defvar org-metaup-hook nil
  18705. "Hook for functions attaching themselves to `M-up'.
  18706. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18707. (defvar org-metadown-hook nil
  18708. "Hook for functions attaching themselves to `M-down'.
  18709. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18710. (defvar org-shiftmetaleft-hook nil
  18711. "Hook for functions attaching themselves to `M-S-left'.
  18712. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18713. (defvar org-shiftmetaright-hook nil
  18714. "Hook for functions attaching themselves to `M-S-right'.
  18715. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18716. (defvar org-shiftmetaup-hook nil
  18717. "Hook for functions attaching themselves to `M-S-up'.
  18718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18719. (defvar org-shiftmetadown-hook nil
  18720. "Hook for functions attaching themselves to `M-S-down'.
  18721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18722. (defvar org-metareturn-hook nil
  18723. "Hook for functions attaching themselves to `M-RET'.
  18724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18725. (defvar org-shiftup-hook nil
  18726. "Hook for functions attaching themselves to `S-up'.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-shiftup-final-hook nil
  18729. "Hook for functions attaching themselves to `S-up'.
  18730. This one runs after all other options except shift-select have been excluded.
  18731. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18732. (defvar org-shiftdown-hook nil
  18733. "Hook for functions attaching themselves to `S-down'.
  18734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18735. (defvar org-shiftdown-final-hook nil
  18736. "Hook for functions attaching themselves to `S-down'.
  18737. This one runs after all other options except shift-select have been excluded.
  18738. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18739. (defvar org-shiftleft-hook nil
  18740. "Hook for functions attaching themselves to `S-left'.
  18741. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18742. (defvar org-shiftleft-final-hook nil
  18743. "Hook for functions attaching themselves to `S-left'.
  18744. This one runs after all other options except shift-select have been excluded.
  18745. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18746. (defvar org-shiftright-hook nil
  18747. "Hook for functions attaching themselves to `S-right'.
  18748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18749. (defvar org-shiftright-final-hook nil
  18750. "Hook for functions attaching themselves to `S-right'.
  18751. This one runs after all other options except shift-select have been excluded.
  18752. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18753. (defun org-modifier-cursor-error ()
  18754. "Throw an error, a modified cursor command was applied in wrong context."
  18755. (user-error "This command is active in special context like tables, headlines or items"))
  18756. (defun org-shiftselect-error ()
  18757. "Throw an error because Shift-Cursor command was applied in wrong context."
  18758. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18759. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18760. (user-error "This command works only in special context like headlines or timestamps")))
  18761. (defun org-call-for-shift-select (cmd)
  18762. (let ((this-command-keys-shift-translated t))
  18763. (call-interactively cmd)))
  18764. (defun org-shifttab (&optional arg)
  18765. "Global visibility cycling or move to previous table field.
  18766. Call `org-table-previous-field' within a table.
  18767. When ARG is nil, cycle globally through visibility states.
  18768. When ARG is a numeric prefix, show contents of this level."
  18769. (interactive "P")
  18770. (cond
  18771. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18772. ((integerp arg)
  18773. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18774. (message "Content view to level: %d" arg)
  18775. (org-content (prefix-numeric-value arg2))
  18776. (org-cycle-show-empty-lines t)
  18777. (setq org-cycle-global-status 'overview)))
  18778. (t (call-interactively 'org-global-cycle))))
  18779. (defun org-shiftmetaleft ()
  18780. "Promote subtree or delete table column.
  18781. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18782. `org-table-delete-column', depending on context. See the
  18783. individual commands for more information."
  18784. (interactive)
  18785. (cond
  18786. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18787. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18788. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18789. ((if (not (org-region-active-p)) (org-at-item-p)
  18790. (save-excursion (goto-char (region-beginning))
  18791. (org-at-item-p)))
  18792. (call-interactively 'org-outdent-item-tree))
  18793. (t (org-modifier-cursor-error))))
  18794. (defun org-shiftmetaright ()
  18795. "Demote subtree or insert table column.
  18796. Calls `org-demote-subtree', `org-indent-item-tree', or
  18797. `org-table-insert-column', depending on context. See the
  18798. individual commands for more information."
  18799. (interactive)
  18800. (cond
  18801. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18802. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18803. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18804. ((if (not (org-region-active-p)) (org-at-item-p)
  18805. (save-excursion (goto-char (region-beginning))
  18806. (org-at-item-p)))
  18807. (call-interactively 'org-indent-item-tree))
  18808. (t (org-modifier-cursor-error))))
  18809. (defun org-shiftmetaup (&optional arg)
  18810. "Drag the line at point up.
  18811. In a table, kill the current row.
  18812. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18813. but also adjust the previous clocked item in the clock history.
  18814. Everywhere else, drag the line at point up."
  18815. (interactive "P")
  18816. (cond
  18817. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18818. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18819. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18820. (call-interactively 'org-timestamp-up)))
  18821. (t (call-interactively 'org-drag-line-backward))))
  18822. (defun org-shiftmetadown (&optional arg)
  18823. "Drag the line at point down.
  18824. In a table, insert an empty row at the current line.
  18825. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18826. but also adjust the previous clocked item in the clock history.
  18827. Everywhere else, drag the line at point down."
  18828. (interactive "P")
  18829. (cond
  18830. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18831. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18832. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18833. (call-interactively 'org-timestamp-down)))
  18834. (t (call-interactively 'org-drag-line-forward))))
  18835. (defsubst org-hidden-tree-error ()
  18836. (user-error
  18837. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18838. (defun org-metaleft (&optional arg)
  18839. "Promote heading, list item at point or move table column left.
  18840. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18841. depending on context. With no specific context, calls the Emacs
  18842. default `backward-word'. See the individual commands for more
  18843. information.
  18844. This function runs the hook `org-metaleft-hook' as a first step,
  18845. and returns at first non-nil value."
  18846. (interactive "P")
  18847. (cond
  18848. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18849. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18850. ((org-with-limited-levels
  18851. (or (org-at-heading-p)
  18852. (and (org-region-active-p)
  18853. (save-excursion
  18854. (goto-char (region-beginning))
  18855. (org-at-heading-p)))))
  18856. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18857. (call-interactively 'org-do-promote))
  18858. ;; At an inline task.
  18859. ((org-at-heading-p)
  18860. (call-interactively 'org-inlinetask-promote))
  18861. ((or (org-at-item-p)
  18862. (and (org-region-active-p)
  18863. (save-excursion
  18864. (goto-char (region-beginning))
  18865. (org-at-item-p))))
  18866. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18867. (call-interactively 'org-outdent-item))
  18868. (t (call-interactively 'backward-word))))
  18869. (defun org-metaright (&optional arg)
  18870. "Demote heading, list item at point or move table column right.
  18871. In front of a drawer or a block keyword, indent it correctly.
  18872. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18873. `org-indnet-drawer' or `org-indent-block' depending on context.
  18874. With no specific context, calls the Emacs default `forward-word'.
  18875. See the individual commands for more information.
  18876. This function runs the hook `org-metaright-hook' as a first step,
  18877. and returns at first non-nil value."
  18878. (interactive "P")
  18879. (cond
  18880. ((run-hook-with-args-until-success 'org-metaright-hook))
  18881. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18882. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18883. ((org-at-block-p) (call-interactively 'org-indent-block))
  18884. ((org-with-limited-levels
  18885. (or (org-at-heading-p)
  18886. (and (org-region-active-p)
  18887. (save-excursion
  18888. (goto-char (region-beginning))
  18889. (org-at-heading-p)))))
  18890. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18891. (call-interactively 'org-do-demote))
  18892. ;; At an inline task.
  18893. ((org-at-heading-p)
  18894. (call-interactively 'org-inlinetask-demote))
  18895. ((or (org-at-item-p)
  18896. (and (org-region-active-p)
  18897. (save-excursion
  18898. (goto-char (region-beginning))
  18899. (org-at-item-p))))
  18900. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18901. (call-interactively 'org-indent-item))
  18902. (t (call-interactively 'forward-word))))
  18903. (defun org-check-for-hidden (what)
  18904. "Check if there are hidden headlines/items in the current visual line.
  18905. WHAT can be either `headlines' or `items'. If the current line is
  18906. an outline or item heading and it has a folded subtree below it,
  18907. this function returns t, nil otherwise."
  18908. (let ((re (cond
  18909. ((eq what 'headlines) org-outline-regexp-bol)
  18910. ((eq what 'items) (org-item-beginning-re))
  18911. (t (error "This should not happen"))))
  18912. beg end)
  18913. (save-excursion
  18914. (catch 'exit
  18915. (unless (org-region-active-p)
  18916. (setq beg (point-at-bol))
  18917. (beginning-of-line 2)
  18918. (while (and (not (eobp)) ;; this is like `next-line'
  18919. (get-char-property (1- (point)) 'invisible))
  18920. (beginning-of-line 2))
  18921. (setq end (point))
  18922. (goto-char beg)
  18923. (goto-char (point-at-eol))
  18924. (setq end (max end (point)))
  18925. (while (re-search-forward re end t)
  18926. (if (get-char-property (match-beginning 0) 'invisible)
  18927. (throw 'exit t))))
  18928. nil))))
  18929. (defun org-metaup (&optional arg)
  18930. "Move subtree up or move table row up.
  18931. Calls `org-move-subtree-up' or `org-table-move-row' or
  18932. `org-move-item-up', depending on context. See the individual commands
  18933. for more information."
  18934. (interactive "P")
  18935. (cond
  18936. ((run-hook-with-args-until-success 'org-metaup-hook))
  18937. ((org-region-active-p)
  18938. (let* ((a (min (region-beginning) (region-end)))
  18939. (b (1- (max (region-beginning) (region-end))))
  18940. (c (save-excursion (goto-char a)
  18941. (move-beginning-of-line 0)))
  18942. (d (save-excursion (goto-char a)
  18943. (move-end-of-line 0) (point))))
  18944. (transpose-regions a b c d)
  18945. (goto-char c)))
  18946. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18947. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18948. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18949. (t (org-drag-element-backward))))
  18950. (defun org-metadown (&optional arg)
  18951. "Move subtree down or move table row down.
  18952. Calls `org-move-subtree-down' or `org-table-move-row' or
  18953. `org-move-item-down', depending on context. See the individual
  18954. commands for more information."
  18955. (interactive "P")
  18956. (cond
  18957. ((run-hook-with-args-until-success 'org-metadown-hook))
  18958. ((org-region-active-p)
  18959. (let* ((a (min (region-beginning) (region-end)))
  18960. (b (max (region-beginning) (region-end)))
  18961. (c (save-excursion (goto-char b)
  18962. (move-beginning-of-line 1)))
  18963. (d (save-excursion (goto-char b)
  18964. (move-end-of-line 1) (1+ (point)))))
  18965. (transpose-regions a b c d)
  18966. (goto-char d)))
  18967. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18968. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18969. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18970. (t (org-drag-element-forward))))
  18971. (defun org-shiftup (&optional arg)
  18972. "Increase item in timestamp or increase priority of current headline.
  18973. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18974. depending on context. See the individual commands for more information."
  18975. (interactive "P")
  18976. (cond
  18977. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18978. ((and org-support-shift-select (org-region-active-p))
  18979. (org-call-for-shift-select 'previous-line))
  18980. ((org-at-timestamp-p t)
  18981. (call-interactively (if org-edit-timestamp-down-means-later
  18982. 'org-timestamp-down 'org-timestamp-up)))
  18983. ((and (not (eq org-support-shift-select 'always))
  18984. org-enable-priority-commands
  18985. (org-at-heading-p))
  18986. (call-interactively 'org-priority-up))
  18987. ((and (not org-support-shift-select) (org-at-item-p))
  18988. (call-interactively 'org-previous-item))
  18989. ((org-clocktable-try-shift 'up arg))
  18990. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18991. (org-support-shift-select
  18992. (org-call-for-shift-select 'previous-line))
  18993. (t (org-shiftselect-error))))
  18994. (defun org-shiftdown (&optional arg)
  18995. "Decrease item in timestamp or decrease priority of current headline.
  18996. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18997. depending on context. See the individual commands for more information."
  18998. (interactive "P")
  18999. (cond
  19000. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19001. ((and org-support-shift-select (org-region-active-p))
  19002. (org-call-for-shift-select 'next-line))
  19003. ((org-at-timestamp-p t)
  19004. (call-interactively (if org-edit-timestamp-down-means-later
  19005. 'org-timestamp-up 'org-timestamp-down)))
  19006. ((and (not (eq org-support-shift-select 'always))
  19007. org-enable-priority-commands
  19008. (org-at-heading-p))
  19009. (call-interactively 'org-priority-down))
  19010. ((and (not org-support-shift-select) (org-at-item-p))
  19011. (call-interactively 'org-next-item))
  19012. ((org-clocktable-try-shift 'down arg))
  19013. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19014. (org-support-shift-select
  19015. (org-call-for-shift-select 'next-line))
  19016. (t (org-shiftselect-error))))
  19017. (defun org-shiftright (&optional arg)
  19018. "Cycle the thing at point or in the current line, depending on context.
  19019. Depending on context, this does one of the following:
  19020. - switch a timestamp at point one day into the future
  19021. - on a headline, switch to the next TODO keyword.
  19022. - on an item, switch entire list to the next bullet type
  19023. - on a property line, switch to the next allowed value
  19024. - on a clocktable definition line, move time block into the future"
  19025. (interactive "P")
  19026. (cond
  19027. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19028. ((and org-support-shift-select (org-region-active-p))
  19029. (org-call-for-shift-select 'forward-char))
  19030. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19031. ((and (not (eq org-support-shift-select 'always))
  19032. (org-at-heading-p))
  19033. (let ((org-inhibit-logging
  19034. (not org-treat-S-cursor-todo-selection-as-state-change))
  19035. (org-inhibit-blocking
  19036. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19037. (org-call-with-arg 'org-todo 'right)))
  19038. ((or (and org-support-shift-select
  19039. (not (eq org-support-shift-select 'always))
  19040. (org-at-item-bullet-p))
  19041. (and (not org-support-shift-select) (org-at-item-p)))
  19042. (org-call-with-arg 'org-cycle-list-bullet nil))
  19043. ((and (not (eq org-support-shift-select 'always))
  19044. (org-at-property-p))
  19045. (call-interactively 'org-property-next-allowed-value))
  19046. ((org-clocktable-try-shift 'right arg))
  19047. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19048. (org-support-shift-select
  19049. (org-call-for-shift-select 'forward-char))
  19050. (t (org-shiftselect-error))))
  19051. (defun org-shiftleft (&optional arg)
  19052. "Cycle the thing at point or in the current line, depending on context.
  19053. Depending on context, this does one of the following:
  19054. - switch a timestamp at point one day into the past
  19055. - on a headline, switch to the previous TODO keyword.
  19056. - on an item, switch entire list to the previous bullet type
  19057. - on a property line, switch to the previous allowed value
  19058. - on a clocktable definition line, move time block into the past"
  19059. (interactive "P")
  19060. (cond
  19061. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19062. ((and org-support-shift-select (org-region-active-p))
  19063. (org-call-for-shift-select 'backward-char))
  19064. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19065. ((and (not (eq org-support-shift-select 'always))
  19066. (org-at-heading-p))
  19067. (let ((org-inhibit-logging
  19068. (not org-treat-S-cursor-todo-selection-as-state-change))
  19069. (org-inhibit-blocking
  19070. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19071. (org-call-with-arg 'org-todo 'left)))
  19072. ((or (and org-support-shift-select
  19073. (not (eq org-support-shift-select 'always))
  19074. (org-at-item-bullet-p))
  19075. (and (not org-support-shift-select) (org-at-item-p)))
  19076. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19077. ((and (not (eq org-support-shift-select 'always))
  19078. (org-at-property-p))
  19079. (call-interactively 'org-property-previous-allowed-value))
  19080. ((org-clocktable-try-shift 'left arg))
  19081. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19082. (org-support-shift-select
  19083. (org-call-for-shift-select 'backward-char))
  19084. (t (org-shiftselect-error))))
  19085. (defun org-shiftcontrolright ()
  19086. "Switch to next TODO set."
  19087. (interactive)
  19088. (cond
  19089. ((and org-support-shift-select (org-region-active-p))
  19090. (org-call-for-shift-select 'forward-word))
  19091. ((and (not (eq org-support-shift-select 'always))
  19092. (org-at-heading-p))
  19093. (org-call-with-arg 'org-todo 'nextset))
  19094. (org-support-shift-select
  19095. (org-call-for-shift-select 'forward-word))
  19096. (t (org-shiftselect-error))))
  19097. (defun org-shiftcontrolleft ()
  19098. "Switch to previous TODO set."
  19099. (interactive)
  19100. (cond
  19101. ((and org-support-shift-select (org-region-active-p))
  19102. (org-call-for-shift-select 'backward-word))
  19103. ((and (not (eq org-support-shift-select 'always))
  19104. (org-at-heading-p))
  19105. (org-call-with-arg 'org-todo 'previousset))
  19106. (org-support-shift-select
  19107. (org-call-for-shift-select 'backward-word))
  19108. (t (org-shiftselect-error))))
  19109. (defun org-shiftcontrolup (&optional n)
  19110. "Change timestamps synchronously up in CLOCK log lines.
  19111. Optional argument N tells to change by that many units."
  19112. (interactive "P")
  19113. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19114. (let (org-support-shift-select)
  19115. (org-clock-timestamps-up n))
  19116. (user-error "Not at a clock log")))
  19117. (defun org-shiftcontroldown (&optional n)
  19118. "Change timestamps synchronously down in CLOCK log lines.
  19119. Optional argument N tells to change by that many units."
  19120. (interactive "P")
  19121. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19122. (let (org-support-shift-select)
  19123. (org-clock-timestamps-down n))
  19124. (user-error "Not at a clock log")))
  19125. (defun org-increase-number-at-point (&optional inc)
  19126. "Increment the number at point.
  19127. With an optional prefix numeric argument INC, increment using
  19128. this numeric value."
  19129. (interactive "p")
  19130. (if (not (number-at-point))
  19131. (user-error "Not on a number")
  19132. (unless inc (setq inc 1))
  19133. (let ((pos (point))
  19134. (beg (skip-chars-backward "-+^/*0-9eE."))
  19135. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19136. (setq nap (buffer-substring-no-properties
  19137. (+ pos beg) (+ pos beg end)))
  19138. (delete-region (+ pos beg) (+ pos beg end))
  19139. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19140. (when (org-at-table-p)
  19141. (org-table-align)
  19142. (org-table-end-of-field 1))))
  19143. (defun org-decrease-number-at-point (&optional inc)
  19144. "Decrement the number at point.
  19145. With an optional prefix numeric argument INC, decrement using
  19146. this numeric value."
  19147. (interactive "p")
  19148. (org-increase-number-at-point (- (or inc 1))))
  19149. (defun org-ctrl-c-ret ()
  19150. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19151. (interactive)
  19152. (cond
  19153. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19154. (t (call-interactively 'org-insert-heading))))
  19155. (defun org-find-visible ()
  19156. (let ((s (point)))
  19157. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19158. (get-char-property s 'invisible)))
  19159. s))
  19160. (defun org-find-invisible ()
  19161. (let ((s (point)))
  19162. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19163. (not (get-char-property s 'invisible))))
  19164. s))
  19165. (defun org-copy-visible (beg end)
  19166. "Copy the visible parts of the region."
  19167. (interactive "r")
  19168. (let (snippets s)
  19169. (save-excursion
  19170. (save-restriction
  19171. (narrow-to-region beg end)
  19172. (setq s (goto-char (point-min)))
  19173. (while (not (= (point) (point-max)))
  19174. (goto-char (org-find-invisible))
  19175. (push (buffer-substring s (point)) snippets)
  19176. (setq s (goto-char (org-find-visible))))))
  19177. (kill-new (apply 'concat (nreverse snippets)))))
  19178. (defun org-copy-special ()
  19179. "Copy region in table or copy current subtree.
  19180. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19181. See the individual commands for more information."
  19182. (interactive)
  19183. (call-interactively
  19184. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19185. (defun org-cut-special ()
  19186. "Cut region in table or cut current subtree.
  19187. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19188. See the individual commands for more information."
  19189. (interactive)
  19190. (call-interactively
  19191. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19192. (defun org-paste-special (arg)
  19193. "Paste rectangular region into table, or past subtree relative to level.
  19194. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19195. See the individual commands for more information."
  19196. (interactive "P")
  19197. (if (org-at-table-p)
  19198. (org-table-paste-rectangle)
  19199. (org-paste-subtree arg)))
  19200. (defsubst org-in-fixed-width-region-p ()
  19201. "Is point in a fixed-width region?"
  19202. (save-match-data
  19203. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19204. (defun org-edit-special (&optional arg)
  19205. "Call a special editor for the element at point.
  19206. When at a table, call the formula editor with `org-table-edit-formulas'.
  19207. When in a source code block, call `org-edit-src-code'.
  19208. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19209. When in an export block, call `org-edit-export-block'.
  19210. When at an #+INCLUDE keyword, visit the included file.
  19211. When at a footnote reference, call `org-edit-footnote-reference'
  19212. On a link, call `ffap' to visit the link at point.
  19213. Otherwise, return a user error."
  19214. (interactive "P")
  19215. (let ((element (org-element-at-point)))
  19216. (assert (not buffer-read-only) nil
  19217. "Buffer is read-only: %s" (buffer-name))
  19218. (case (org-element-type element)
  19219. (src-block
  19220. (if (not arg) (org-edit-src-code)
  19221. (let* ((info (org-babel-get-src-block-info))
  19222. (lang (nth 0 info))
  19223. (params (nth 2 info))
  19224. (session (cdr (assq :session params))))
  19225. (if (not session) (org-edit-src-code)
  19226. ;; At a src-block with a session and function called with
  19227. ;; an ARG: switch to the buffer related to the inferior
  19228. ;; process.
  19229. (switch-to-buffer
  19230. (funcall (intern (concat "org-babel-prep-session:" lang))
  19231. session params))))))
  19232. (keyword
  19233. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19234. (org-open-link-from-string
  19235. (format "[[%s]]"
  19236. (expand-file-name
  19237. (let ((value (org-element-property :value element)))
  19238. (cond ((not (org-string-nw-p value))
  19239. (user-error "No file to edit"))
  19240. ((string-match "\\`\"\\(.*?\\)\"" value)
  19241. (match-string 1 value))
  19242. ((string-match "\\`[^ \t\"]\\S-*" value)
  19243. (match-string 0 value))
  19244. (t (user-error "No valid file specified")))))))
  19245. (user-error "No special environment to edit here")))
  19246. (table
  19247. (if (eq (org-element-property :type element) 'table.el)
  19248. (org-edit-table.el)
  19249. (call-interactively 'org-table-edit-formulas)))
  19250. ;; Only Org tables contain `table-row' type elements.
  19251. (table-row (call-interactively 'org-table-edit-formulas))
  19252. (example-block (org-edit-src-code))
  19253. (export-block (org-edit-export-block))
  19254. (fixed-width (org-edit-fixed-width-region))
  19255. (otherwise
  19256. ;; No notable element at point. Though, we may be at a link or
  19257. ;; a footnote reference, which are objects. Thus, scan deeper.
  19258. (let ((context (org-element-context element)))
  19259. (case (org-element-type context)
  19260. (link (call-interactively #'ffap))
  19261. (footnote-reference (org-edit-footnote-reference))
  19262. (t (user-error "No special environment to edit here"))))))))
  19263. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19264. (defun org-ctrl-c-ctrl-c (&optional arg)
  19265. "Set tags in headline, or update according to changed information at point.
  19266. This command does many different things, depending on context:
  19267. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19268. this is what we do.
  19269. - If the cursor is on a statistics cookie, update it.
  19270. - If the cursor is in a headline, prompt for tags and insert them
  19271. into the current line, aligned to `org-tags-column'. When called
  19272. with prefix arg, realign all tags in the current buffer.
  19273. - If the cursor is in one of the special #+KEYWORD lines, this
  19274. triggers scanning the buffer for these lines and updating the
  19275. information.
  19276. - If the cursor is inside a table, realign the table. This command
  19277. works even if the automatic table editor has been turned off.
  19278. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19279. the entire table.
  19280. - If the cursor is at a footnote reference or definition, jump to
  19281. the corresponding definition or references, respectively.
  19282. - If the cursor is a the beginning of a dynamic block, update it.
  19283. - If the current buffer is a capture buffer, close note and file it.
  19284. - If the cursor is on a <<<target>>>, update radio targets and
  19285. corresponding links in this buffer.
  19286. - If the cursor is on a numbered item in a plain list, renumber the
  19287. ordered list.
  19288. - If the cursor is on a checkbox, toggle it.
  19289. - If the cursor is on a code block, evaluate it. The variable
  19290. `org-confirm-babel-evaluate' can be used to control prompting
  19291. before code block evaluation, by default every code block
  19292. evaluation requires confirmation. Code block evaluation can be
  19293. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19294. (interactive "P")
  19295. (cond
  19296. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19297. org-occur-highlights)
  19298. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19299. (org-remove-occur-highlights)
  19300. (message "Temporary highlights/overlays removed from current buffer"))
  19301. ((and (local-variable-p 'org-finish-function (current-buffer))
  19302. (fboundp org-finish-function))
  19303. (funcall org-finish-function))
  19304. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19305. (t
  19306. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19307. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19308. (user-error "C-c C-c can do nothing useful at this location"))
  19309. (let* ((context (org-element-context))
  19310. (type (org-element-type context)))
  19311. (case type
  19312. ;; When at a link, act according to the parent instead.
  19313. (link (setq context (org-element-property :parent context))
  19314. (setq type (org-element-type context)))
  19315. ;; Unsupported object types: refer to the first supported
  19316. ;; element or object containing it.
  19317. ((bold code entity export-snippet inline-babel-call inline-src-block
  19318. italic latex-fragment line-break macro strike-through subscript
  19319. superscript underline verbatim)
  19320. (setq context
  19321. (org-element-lineage
  19322. context '(radio-target paragraph verse-block table-cell)))))
  19323. ;; For convenience: at the first line of a paragraph on the
  19324. ;; same line as an item, apply function on that item instead.
  19325. (when (eq type 'paragraph)
  19326. (let ((parent (org-element-property :parent context)))
  19327. (when (and (eq (org-element-type parent) 'item)
  19328. (= (line-beginning-position)
  19329. (org-element-property :begin parent)))
  19330. (setq context parent type 'item))))
  19331. ;; Act according to type of element or object at point.
  19332. (case type
  19333. (clock (org-clock-update-time-maybe))
  19334. (dynamic-block
  19335. (save-excursion
  19336. (goto-char (org-element-property :post-affiliated context))
  19337. (org-update-dblock)))
  19338. (footnote-definition
  19339. (goto-char (org-element-property :post-affiliated context))
  19340. (call-interactively 'org-footnote-action))
  19341. (footnote-reference (call-interactively 'org-footnote-action))
  19342. ((headline inlinetask)
  19343. (save-excursion (goto-char (org-element-property :begin context))
  19344. (call-interactively 'org-set-tags)))
  19345. (item
  19346. ;; At an item: a double C-u set checkbox to "[-]"
  19347. ;; unconditionally, whereas a single one will toggle its
  19348. ;; presence. Without a universal argument, if the item
  19349. ;; has a checkbox, toggle it. Otherwise repair the list.
  19350. (let* ((box (org-element-property :checkbox context))
  19351. (struct (org-element-property :structure context))
  19352. (old-struct (copy-tree struct))
  19353. (parents (org-list-parents-alist struct))
  19354. (prevs (org-list-prevs-alist struct))
  19355. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19356. (org-list-set-checkbox
  19357. (org-element-property :begin context) struct
  19358. (cond ((equal arg '(16)) "[-]")
  19359. ((and (not box) (equal arg '(4))) "[ ]")
  19360. ((or (not box) (equal arg '(4))) nil)
  19361. ((eq box 'on) "[ ]")
  19362. (t "[X]")))
  19363. ;; Mimic `org-list-write-struct' but with grabbing
  19364. ;; a return value from `org-list-struct-fix-box'.
  19365. (org-list-struct-fix-ind struct parents 2)
  19366. (org-list-struct-fix-item-end struct)
  19367. (org-list-struct-fix-bul struct prevs)
  19368. (org-list-struct-fix-ind struct parents)
  19369. (let ((block-item
  19370. (org-list-struct-fix-box struct parents prevs orderedp)))
  19371. (if (and box (equal struct old-struct))
  19372. (if (equal arg '(16))
  19373. (message "Checkboxes already reset")
  19374. (user-error "Cannot toggle this checkbox: %s"
  19375. (if (eq box 'on)
  19376. "all subitems checked"
  19377. "unchecked subitems")))
  19378. (org-list-struct-apply-struct struct old-struct)
  19379. (org-update-checkbox-count-maybe))
  19380. (when block-item
  19381. (message "Checkboxes were removed due to empty box at line %d"
  19382. (org-current-line block-item))))))
  19383. (keyword
  19384. (let ((org-inhibit-startup-visibility-stuff t)
  19385. (org-startup-align-all-tables nil))
  19386. (when (boundp 'org-table-coordinate-overlays)
  19387. (mapc 'delete-overlay org-table-coordinate-overlays)
  19388. (setq org-table-coordinate-overlays nil))
  19389. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19390. (message "Local setup has been refreshed"))
  19391. (plain-list
  19392. ;; At a plain list, with a double C-u argument, set
  19393. ;; checkboxes of each item to "[-]", whereas a single one
  19394. ;; will toggle their presence according to the state of the
  19395. ;; first item in the list. Without an argument, repair the
  19396. ;; list.
  19397. (let* ((begin (org-element-property :contents-begin context))
  19398. (beginm (move-marker (make-marker) begin))
  19399. (struct (org-element-property :structure context))
  19400. (old-struct (copy-tree struct))
  19401. (first-box (save-excursion
  19402. (goto-char begin)
  19403. (looking-at org-list-full-item-re)
  19404. (match-string-no-properties 3)))
  19405. (new-box (cond ((equal arg '(16)) "[-]")
  19406. ((equal arg '(4)) (unless first-box "[ ]"))
  19407. ((equal first-box "[X]") "[ ]")
  19408. (t "[X]"))))
  19409. (cond
  19410. (arg
  19411. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19412. (org-list-get-all-items
  19413. begin struct (org-list-prevs-alist struct))))
  19414. ((and first-box (eq (point) begin))
  19415. ;; For convenience, when point is at bol on the first
  19416. ;; item of the list and no argument is provided, simply
  19417. ;; toggle checkbox of that item, if any.
  19418. (org-list-set-checkbox begin struct new-box)))
  19419. (org-list-write-struct
  19420. struct (org-list-parents-alist struct) old-struct)
  19421. (org-update-checkbox-count-maybe)
  19422. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19423. ((property-drawer node-property)
  19424. (call-interactively 'org-property-action))
  19425. ((radio-target target)
  19426. (call-interactively 'org-update-radio-target-regexp))
  19427. (statistics-cookie
  19428. (call-interactively 'org-update-statistics-cookies))
  19429. ((table table-cell table-row)
  19430. ;; At a table, recalculate every field and align it. Also
  19431. ;; send the table if necessary. If the table has
  19432. ;; a `table.el' type, just give up. At a table row or
  19433. ;; cell, maybe recalculate line but always align table.
  19434. (if (eq (org-element-property :type context) 'table.el)
  19435. (message "Use C-c ' to edit table.el tables")
  19436. (let ((org-enable-table-editor t))
  19437. (if (or (eq type 'table)
  19438. ;; Check if point is at a TBLFM line.
  19439. (and (eq type 'table-row)
  19440. (= (point) (org-element-property :end context))))
  19441. (save-excursion
  19442. (if (org-at-TBLFM-p)
  19443. (progn (require 'org-table)
  19444. (org-table-calc-current-TBLFM))
  19445. (goto-char (org-element-property :contents-begin context))
  19446. (org-call-with-arg 'org-table-recalculate (or arg t))
  19447. (orgtbl-send-table 'maybe)))
  19448. (org-table-maybe-eval-formula)
  19449. (cond (arg (call-interactively 'org-table-recalculate))
  19450. ((org-table-maybe-recalculate-line))
  19451. (t (org-table-align)))))))
  19452. (timestamp (org-timestamp-change 0 'day))
  19453. (otherwise
  19454. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19455. (user-error
  19456. "C-c C-c can do nothing useful at this location")))))))))
  19457. (defun org-mode-restart ()
  19458. (interactive)
  19459. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19460. (funcall major-mode)
  19461. (hack-local-variables)
  19462. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19463. (org-indent-mode -1)))
  19464. (message "%s restarted" major-mode))
  19465. (defun org-kill-note-or-show-branches ()
  19466. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19467. (interactive)
  19468. (if (not org-finish-function)
  19469. (progn
  19470. (hide-subtree)
  19471. (call-interactively 'show-branches))
  19472. (let ((org-note-abort t))
  19473. (funcall org-finish-function))))
  19474. (defun org-delete-indentation (&optional ARG)
  19475. "Join current line to previous and fix whitespace at join.
  19476. If previous line is a headline add to headline title. Otherwise
  19477. the function calls `delete-indentation'.
  19478. With argument, join this line to following line."
  19479. (interactive "*P")
  19480. (if (save-excursion
  19481. (if ARG (beginning-of-line)
  19482. (forward-line -1))
  19483. (looking-at org-complex-heading-regexp))
  19484. ;; At headline.
  19485. (let ((tags-column (when (match-beginning 5)
  19486. (save-excursion (goto-char (match-beginning 5))
  19487. (current-column))))
  19488. (string (concat " " (progn (when ARG (forward-line 1))
  19489. (org-trim (delete-and-extract-region
  19490. (line-beginning-position)
  19491. (line-end-position)))))))
  19492. (unless (bobp) (delete-region (point) (1- (point))))
  19493. (goto-char (or (match-end 4)
  19494. (match-beginning 5)
  19495. (match-end 0)))
  19496. (skip-chars-backward " \t")
  19497. (save-excursion (insert string))
  19498. ;; Adjust alignment of tags.
  19499. (when tags-column
  19500. (org-align-tags-here (if org-auto-align-tags
  19501. org-tags-column
  19502. tags-column))))
  19503. (delete-indentation ARG)))
  19504. (defun org-open-line (n)
  19505. "Insert a new row in tables, call `open-line' elsewhere.
  19506. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19507. (interactive "*p")
  19508. (cond
  19509. ((not org-special-ctrl-o)
  19510. (open-line n))
  19511. ((org-at-table-p)
  19512. (org-table-insert-row))
  19513. (t
  19514. (open-line n))))
  19515. (defun org-return (&optional indent)
  19516. "Goto next table row or insert a newline.
  19517. Calls `org-table-next-row' or `newline', depending on context.
  19518. When optional INDENT argument is non-nil, call
  19519. `newline-and-indent' instead of `newline'.
  19520. When `org-return-follows-link' is non-nil and point is on
  19521. a timestamp or a link, call `org-open-at-point'. However, it
  19522. will not happen if point is in a table or on a \"dead\"
  19523. object (e.g., within a comment). In these case, you need to use
  19524. `org-open-at-point' directly."
  19525. (interactive)
  19526. (let* ((context (if org-return-follows-link (org-element-context)
  19527. (org-element-at-point)))
  19528. (type (org-element-type context)))
  19529. (cond
  19530. ;; In a table, call `org-table-next-row'.
  19531. ((or (and (eq type 'table)
  19532. (>= (point) (org-element-property :contents-begin context))
  19533. (< (point) (org-element-property :contents-end context)))
  19534. (org-element-lineage context '(table-row table-cell) t))
  19535. (org-table-justify-field-maybe)
  19536. (call-interactively #'org-table-next-row))
  19537. ;; On a link or a timestamp but not on white spaces after it,
  19538. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19539. ((and org-return-follows-link
  19540. (memq type '(link timestamp))
  19541. (< (point)
  19542. (save-excursion (goto-char (org-element-property :end context))
  19543. (skip-chars-backward " \t")
  19544. (point))))
  19545. (call-interactively #'org-open-at-point))
  19546. ;; Insert newline in heading, but preserve tags.
  19547. ((and (not (bolp))
  19548. (save-excursion (beginning-of-line)
  19549. (looking-at org-complex-heading-regexp)))
  19550. ;; At headline. Split line. However, if point is on keyword,
  19551. ;; priority cookie or tags, do not break any of them: add
  19552. ;; a newline after the headline instead.
  19553. (let ((tags-column (and (match-beginning 5)
  19554. (save-excursion (goto-char (match-beginning 5))
  19555. (current-column))))
  19556. (string
  19557. (when (and (match-end 4)
  19558. (>= (point)
  19559. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19560. (<= (point) (match-end 4)))
  19561. (delete-and-extract-region (point) (match-end 4)))))
  19562. (when (and tags-column string) ; Adjust tag alignment.
  19563. (org-align-tags-here
  19564. (if org-auto-align-tags org-tags-column tags-column)))
  19565. (end-of-line)
  19566. (org-show-entry)
  19567. (if indent (newline-and-indent) (newline))
  19568. (when string (save-excursion (insert (org-trim string))))))
  19569. ;; In a list, make sure indenting keeps trailing text within.
  19570. ((and indent
  19571. (not (eolp))
  19572. (org-element-lineage context '(item)))
  19573. (let ((trailing-data
  19574. (delete-and-extract-region (point) (line-end-position))))
  19575. (newline-and-indent)
  19576. (save-excursion (insert trailing-data))))
  19577. (t (if indent (newline-and-indent) (newline))))))
  19578. (defun org-return-indent ()
  19579. "Goto next table row or insert a newline and indent.
  19580. Calls `org-table-next-row' or `newline-and-indent', depending on
  19581. context. See the individual commands for more information."
  19582. (interactive)
  19583. (org-return t))
  19584. (defun org-ctrl-c-star ()
  19585. "Compute table, or change heading status of lines.
  19586. Calls `org-table-recalculate' or `org-toggle-heading',
  19587. depending on context."
  19588. (interactive)
  19589. (cond
  19590. ((org-at-table-p)
  19591. (call-interactively 'org-table-recalculate))
  19592. (t
  19593. ;; Convert all lines in region to list items
  19594. (call-interactively 'org-toggle-heading))))
  19595. (defun org-ctrl-c-minus ()
  19596. "Insert separator line in table or modify bullet status of line.
  19597. Also turns a plain line or a region of lines into list items.
  19598. Calls `org-table-insert-hline', `org-toggle-item', or
  19599. `org-cycle-list-bullet', depending on context."
  19600. (interactive)
  19601. (cond
  19602. ((org-at-table-p)
  19603. (call-interactively 'org-table-insert-hline))
  19604. ((org-region-active-p)
  19605. (call-interactively 'org-toggle-item))
  19606. ((org-in-item-p)
  19607. (call-interactively 'org-cycle-list-bullet))
  19608. (t
  19609. (call-interactively 'org-toggle-item))))
  19610. (defun org-toggle-item (arg)
  19611. "Convert headings or normal lines to items, items to normal lines.
  19612. If there is no active region, only the current line is considered.
  19613. If the first non blank line in the region is a headline, convert
  19614. all headlines to items, shifting text accordingly.
  19615. If it is an item, convert all items to normal lines.
  19616. If it is normal text, change region into a list of items.
  19617. With a prefix argument ARG, change the region in a single item."
  19618. (interactive "P")
  19619. (let ((shift-text
  19620. (function
  19621. ;; Shift text in current section to IND, from point to END.
  19622. ;; The function leaves point to END line.
  19623. (lambda (ind end)
  19624. (let ((min-i 1000) (end (copy-marker end)))
  19625. ;; First determine the minimum indentation (MIN-I) of
  19626. ;; the text.
  19627. (save-excursion
  19628. (catch 'exit
  19629. (while (< (point) end)
  19630. (let ((i (org-get-indentation)))
  19631. (cond
  19632. ;; Skip blank lines and inline tasks.
  19633. ((looking-at "^[ \t]*$"))
  19634. ((looking-at org-outline-regexp-bol))
  19635. ;; We can't find less than 0 indentation.
  19636. ((zerop i) (throw 'exit (setq min-i 0)))
  19637. ((< i min-i) (setq min-i i))))
  19638. (forward-line))))
  19639. ;; Then indent each line so that a line indented to
  19640. ;; MIN-I becomes indented to IND. Ignore blank lines
  19641. ;; and inline tasks in the process.
  19642. (let ((delta (- ind min-i)))
  19643. (while (< (point) end)
  19644. (unless (or (looking-at "^[ \t]*$")
  19645. (looking-at org-outline-regexp-bol))
  19646. (org-indent-line-to (+ (org-get-indentation) delta)))
  19647. (forward-line)))))))
  19648. (skip-blanks
  19649. (function
  19650. ;; Return beginning of first non-blank line, starting from
  19651. ;; line at POS.
  19652. (lambda (pos)
  19653. (save-excursion
  19654. (goto-char pos)
  19655. (skip-chars-forward " \r\t\n")
  19656. (point-at-bol)))))
  19657. beg end)
  19658. ;; Determine boundaries of changes.
  19659. (if (org-region-active-p)
  19660. (setq beg (funcall skip-blanks (region-beginning))
  19661. end (copy-marker (region-end)))
  19662. (setq beg (funcall skip-blanks (point-at-bol))
  19663. end (copy-marker (point-at-eol))))
  19664. ;; Depending on the starting line, choose an action on the text
  19665. ;; between BEG and END.
  19666. (org-with-limited-levels
  19667. (save-excursion
  19668. (goto-char beg)
  19669. (cond
  19670. ;; Case 1. Start at an item: de-itemize. Note that it only
  19671. ;; happens when a region is active: `org-ctrl-c-minus'
  19672. ;; would call `org-cycle-list-bullet' otherwise.
  19673. ((org-at-item-p)
  19674. (while (< (point) end)
  19675. (when (org-at-item-p)
  19676. (skip-chars-forward " \t")
  19677. (delete-region (point) (match-end 0)))
  19678. (forward-line)))
  19679. ;; Case 2. Start at an heading: convert to items.
  19680. ((org-at-heading-p)
  19681. (let* ((bul (org-list-bullet-string "-"))
  19682. (bul-len (length bul))
  19683. (done (org-entry-is-done-p))
  19684. (todo (org-entry-is-todo-p))
  19685. ;; Indentation of the first heading. It should be
  19686. ;; relative to the indentation of its parent, if any.
  19687. (start-ind (save-excursion
  19688. (cond
  19689. ((not org-adapt-indentation) 0)
  19690. ((not (outline-previous-heading)) 0)
  19691. (t (length (match-string 0))))))
  19692. ;; Level of first heading. Further headings will be
  19693. ;; compared to it to determine hierarchy in the list.
  19694. (ref-level (org-reduced-level (org-outline-level))))
  19695. (when (or done todo) (org-todo ""))
  19696. (while (< (point) end)
  19697. (let* ((level (org-reduced-level (org-outline-level)))
  19698. (delta (max 0 (- level ref-level))))
  19699. ;; If current headline is less indented than the first
  19700. ;; one, set it as reference, in order to preserve
  19701. ;; subtrees.
  19702. (when (< level ref-level) (setq ref-level level))
  19703. (replace-match bul t t)
  19704. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19705. (when (or done todo)
  19706. (let* ((struct (org-list-struct))
  19707. (old (copy-tree struct)))
  19708. (org-list-set-checkbox (line-beginning-position)
  19709. struct
  19710. (if done "[X]" "[ ]"))
  19711. (org-list-write-struct struct
  19712. (org-list-parents-alist struct)
  19713. old)))
  19714. ;; Ensure all text down to END (or SECTION-END) belongs
  19715. ;; to the newly created item.
  19716. (let ((section-end (save-excursion
  19717. (or (outline-next-heading) (point)))))
  19718. (forward-line)
  19719. (funcall shift-text
  19720. (+ start-ind (* (1+ delta) bul-len))
  19721. (min end section-end)))))))
  19722. ;; Case 3. Normal line with ARG: make the first line of region
  19723. ;; an item, and shift indentation of others lines to
  19724. ;; set them as item's body.
  19725. (arg (let* ((bul (org-list-bullet-string "-"))
  19726. (bul-len (length bul))
  19727. (ref-ind (org-get-indentation)))
  19728. (skip-chars-forward " \t")
  19729. (insert bul)
  19730. (forward-line)
  19731. (while (< (point) end)
  19732. ;; Ensure that lines less indented than first one
  19733. ;; still get included in item body.
  19734. (funcall shift-text
  19735. (+ ref-ind bul-len)
  19736. (min end (save-excursion (or (outline-next-heading)
  19737. (point)))))
  19738. (forward-line))))
  19739. ;; Case 4. Normal line without ARG: turn each non-item line
  19740. ;; into an item.
  19741. (t
  19742. (while (< (point) end)
  19743. (unless (or (org-at-heading-p) (org-at-item-p))
  19744. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19745. (replace-match
  19746. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19747. (forward-line))))))))
  19748. (defun org-toggle-heading (&optional nstars)
  19749. "Convert headings to normal text, or items or text to headings.
  19750. If there is no active region, only convert the current line.
  19751. With a \\[universal-argument] prefix, convert the whole list at
  19752. point into heading.
  19753. In a region:
  19754. - If the first non blank line is a headline, remove the stars
  19755. from all headlines in the region.
  19756. - If it is a normal line, turn each and every normal line (i.e.,
  19757. not an heading or an item) in the region into headings. If you
  19758. want to convert only the first line of this region, use one
  19759. universal prefix argument.
  19760. - If it is a plain list item, turn all plain list items into headings.
  19761. When converting a line into a heading, the number of stars is chosen
  19762. such that the lines become children of the current entry. However,
  19763. when a numeric prefix argument is given, its value determines the
  19764. number of stars to add."
  19765. (interactive "P")
  19766. (let ((skip-blanks
  19767. (function
  19768. ;; Return beginning of first non-blank line, starting from
  19769. ;; line at POS.
  19770. (lambda (pos)
  19771. (save-excursion
  19772. (goto-char pos)
  19773. (while (org-at-comment-p) (forward-line))
  19774. (skip-chars-forward " \r\t\n")
  19775. (point-at-bol)))))
  19776. beg end toggled)
  19777. ;; Determine boundaries of changes. If a universal prefix has
  19778. ;; been given, put the list in a region. If region ends at a bol,
  19779. ;; do not consider the last line to be in the region.
  19780. (when (and current-prefix-arg (org-at-item-p))
  19781. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19782. (org-mark-element))
  19783. (if (org-region-active-p)
  19784. (setq beg (funcall skip-blanks (region-beginning))
  19785. end (copy-marker (save-excursion
  19786. (goto-char (region-end))
  19787. (if (bolp) (point) (point-at-eol)))))
  19788. (setq beg (funcall skip-blanks (point-at-bol))
  19789. end (copy-marker (point-at-eol))))
  19790. ;; Ensure inline tasks don't count as headings.
  19791. (org-with-limited-levels
  19792. (save-excursion
  19793. (goto-char beg)
  19794. (cond
  19795. ;; Case 1. Started at an heading: de-star headings.
  19796. ((org-at-heading-p)
  19797. (while (< (point) end)
  19798. (when (org-at-heading-p t)
  19799. (looking-at org-outline-regexp) (replace-match "")
  19800. (setq toggled t))
  19801. (forward-line)))
  19802. ;; Case 2. Started at an item: change items into headlines.
  19803. ;; One star will be added by `org-list-to-subtree'.
  19804. ((org-at-item-p)
  19805. (while (< (point) end)
  19806. (when (org-at-item-p)
  19807. ;; Pay attention to cases when region ends before list.
  19808. (let* ((struct (org-list-struct))
  19809. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19810. (save-restriction
  19811. (narrow-to-region (point) list-end)
  19812. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19813. (setq toggled t))
  19814. (forward-line)))
  19815. ;; Case 3. Started at normal text: make every line an heading,
  19816. ;; skipping headlines and items.
  19817. (t (let* ((stars
  19818. (make-string
  19819. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19820. (add-stars
  19821. (cond (nstars "") ; stars from prefix only
  19822. ((equal stars "") "*") ; before first heading
  19823. (org-odd-levels-only "**") ; inside heading, odd
  19824. (t "*"))) ; inside heading, oddeven
  19825. (rpl (concat stars add-stars " "))
  19826. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19827. (while (< (point) (if (equal nstars '(4)) lend end))
  19828. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19829. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19830. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19831. (forward-line)))))))
  19832. (unless toggled (message "Cannot toggle heading from here"))))
  19833. (defun org-meta-return (&optional _arg)
  19834. "Insert a new heading or wrap a region in a table.
  19835. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19836. on context. See the individual commands for more information."
  19837. (interactive)
  19838. (org-check-before-invisible-edit 'insert)
  19839. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19840. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19841. #'org-insert-heading))))
  19842. ;;; Menu entries
  19843. (defsubst org-in-subtree-not-table-p ()
  19844. "Are we in a subtree and not in a table?"
  19845. (and (not (org-before-first-heading-p))
  19846. (not (org-at-table-p))))
  19847. ;; Define the Org-mode menus
  19848. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19849. '("Tbl"
  19850. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19851. ["Next Field" org-cycle (org-at-table-p)]
  19852. ["Previous Field" org-shifttab (org-at-table-p)]
  19853. ["Next Row" org-return (org-at-table-p)]
  19854. "--"
  19855. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19856. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19857. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19858. "--"
  19859. ("Column"
  19860. ["Move Column Left" org-metaleft (org-at-table-p)]
  19861. ["Move Column Right" org-metaright (org-at-table-p)]
  19862. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19863. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19864. ("Row"
  19865. ["Move Row Up" org-metaup (org-at-table-p)]
  19866. ["Move Row Down" org-metadown (org-at-table-p)]
  19867. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19868. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19869. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19870. "--"
  19871. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19872. ("Rectangle"
  19873. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19874. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19875. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19876. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19877. "--"
  19878. ("Calculate"
  19879. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19880. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19881. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19882. "--"
  19883. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19884. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19885. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19886. "--"
  19887. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19888. "--"
  19889. ["Sum Column/Rectangle" org-table-sum
  19890. (or (org-at-table-p) (org-region-active-p))]
  19891. ["Which Column?" org-table-current-column (org-at-table-p)])
  19892. ["Debug Formulas"
  19893. org-table-toggle-formula-debugger
  19894. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19895. ["Show Col/Row Numbers"
  19896. org-table-toggle-coordinate-overlays
  19897. :style toggle
  19898. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19899. "--"
  19900. ["Create" org-table-create (and (not (org-at-table-p))
  19901. org-enable-table-editor)]
  19902. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19903. ["Import from File" org-table-import (not (org-at-table-p))]
  19904. ["Export to File" org-table-export (org-at-table-p)]
  19905. "--"
  19906. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19907. "--"
  19908. ("Plot"
  19909. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19910. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19911. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19912. '("Org"
  19913. ("Show/Hide"
  19914. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19915. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19916. ["Sparse Tree..." org-sparse-tree t]
  19917. ["Reveal Context" org-reveal t]
  19918. ["Show All" show-all t]
  19919. "--"
  19920. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19921. "--"
  19922. ["New Heading" org-insert-heading t]
  19923. ("Navigate Headings"
  19924. ["Up" outline-up-heading t]
  19925. ["Next" outline-next-visible-heading t]
  19926. ["Previous" outline-previous-visible-heading t]
  19927. ["Next Same Level" outline-forward-same-level t]
  19928. ["Previous Same Level" outline-backward-same-level t]
  19929. "--"
  19930. ["Jump" org-goto t])
  19931. ("Edit Structure"
  19932. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19933. "--"
  19934. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19935. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19936. "--"
  19937. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19938. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19939. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19940. "--"
  19941. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19942. "--"
  19943. ["Copy visible text" org-copy-visible t]
  19944. "--"
  19945. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19946. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19947. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19948. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19949. "--"
  19950. ["Sort Region/Children" org-sort t]
  19951. "--"
  19952. ["Convert to odd levels" org-convert-to-odd-levels t]
  19953. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19954. ("Editing"
  19955. ["Emphasis..." org-emphasize t]
  19956. ["Edit Source Example" org-edit-special t]
  19957. "--"
  19958. ["Footnote new/jump" org-footnote-action t]
  19959. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19960. ("Archive"
  19961. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19962. "--"
  19963. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19964. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19965. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19966. )
  19967. "--"
  19968. ("Hyperlinks"
  19969. ["Store Link (Global)" org-store-link t]
  19970. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19971. ["Insert Link" org-insert-link t]
  19972. ["Follow Link" org-open-at-point t]
  19973. "--"
  19974. ["Next link" org-next-link t]
  19975. ["Previous link" org-previous-link t]
  19976. "--"
  19977. ["Descriptive Links"
  19978. org-toggle-link-display
  19979. :style radio
  19980. :selected org-descriptive-links
  19981. ]
  19982. ["Literal Links"
  19983. org-toggle-link-display
  19984. :style radio
  19985. :selected (not org-descriptive-links)])
  19986. "--"
  19987. ("TODO Lists"
  19988. ["TODO/DONE/-" org-todo t]
  19989. ("Select keyword"
  19990. ["Next keyword" org-shiftright (org-at-heading-p)]
  19991. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19992. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19993. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19994. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19995. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19996. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19997. "--"
  19998. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19999. :selected org-enforce-todo-dependencies :style toggle :active t]
  20000. "Settings for tree at point"
  20001. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20002. :selected (org-entry-get nil "ORDERED")
  20003. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20004. ["Do Children parallel" org-toggle-ordered-property :style radio
  20005. :selected (not (org-entry-get nil "ORDERED"))
  20006. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20007. "--"
  20008. ["Set Priority" org-priority t]
  20009. ["Priority Up" org-shiftup t]
  20010. ["Priority Down" org-shiftdown t]
  20011. "--"
  20012. ["Get news from all feeds" org-feed-update-all t]
  20013. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20014. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20015. ("TAGS and Properties"
  20016. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20017. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20018. "--"
  20019. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20020. ["Column view of properties" org-columns t]
  20021. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20022. ("Dates and Scheduling"
  20023. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20024. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20025. ("Change Date"
  20026. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20027. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20028. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20029. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20030. ["Compute Time Range" org-evaluate-time-range t]
  20031. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20032. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20033. "--"
  20034. ["Custom time format" org-toggle-time-stamp-overlays
  20035. :style radio :selected org-display-custom-times]
  20036. "--"
  20037. ["Goto Calendar" org-goto-calendar t]
  20038. ["Date from Calendar" org-date-from-calendar t]
  20039. "--"
  20040. ["Start/Restart Timer" org-timer-start t]
  20041. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20042. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20043. ["Insert Timer String" org-timer t]
  20044. ["Insert Timer Item" org-timer-item t])
  20045. ("Logging work"
  20046. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20047. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20048. ["Clock out" org-clock-out t]
  20049. ["Clock cancel" org-clock-cancel t]
  20050. "--"
  20051. ["Mark as default task" org-clock-mark-default-task t]
  20052. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20053. ["Goto running clock" org-clock-goto t]
  20054. "--"
  20055. ["Display times" org-clock-display t]
  20056. ["Create clock table" org-clock-report t]
  20057. "--"
  20058. ["Record DONE time"
  20059. (progn (setq org-log-done (not org-log-done))
  20060. (message "Switching to %s will %s record a timestamp"
  20061. (car org-done-keywords)
  20062. (if org-log-done "automatically" "not")))
  20063. :style toggle :selected org-log-done])
  20064. "--"
  20065. ["Agenda Command..." org-agenda t]
  20066. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20067. ("File List for Agenda")
  20068. ("Special views current file"
  20069. ["TODO Tree" org-show-todo-tree t]
  20070. ["Check Deadlines" org-check-deadlines t]
  20071. ["Timeline" org-timeline t]
  20072. ["Tags/Property tree" org-match-sparse-tree t])
  20073. "--"
  20074. ["Export/Publish..." org-export-dispatch t]
  20075. ("LaTeX"
  20076. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20077. :selected org-cdlatex-mode]
  20078. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20079. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20080. ["Modify math symbol" org-cdlatex-math-modify
  20081. (org-inside-LaTeX-fragment-p)]
  20082. ["Insert citation" org-reftex-citation t]
  20083. "--"
  20084. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20085. "--"
  20086. ("MobileOrg"
  20087. ["Push Files and Views" org-mobile-push t]
  20088. ["Get Captured and Flagged" org-mobile-pull t]
  20089. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20090. "--"
  20091. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20092. "--"
  20093. ("Documentation"
  20094. ["Show Version" org-version t]
  20095. ["Info Documentation" org-info t])
  20096. ("Customize"
  20097. ["Browse Org Group" org-customize t]
  20098. "--"
  20099. ["Expand This Menu" org-create-customize-menu
  20100. (fboundp 'customize-menu-create)])
  20101. ["Send bug report" org-submit-bug-report t]
  20102. "--"
  20103. ("Refresh/Reload"
  20104. ["Refresh setup current buffer" org-mode-restart t]
  20105. ["Reload Org (after update)" org-reload t]
  20106. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20107. ))
  20108. (defun org-info (&optional node)
  20109. "Read documentation for Org-mode in the info system.
  20110. With optional NODE, go directly to that node."
  20111. (interactive)
  20112. (info (format "(org)%s" (or node ""))))
  20113. ;;;###autoload
  20114. (defun org-submit-bug-report ()
  20115. "Submit a bug report on Org-mode via mail.
  20116. Don't hesitate to report any problems or inaccurate documentation.
  20117. If you don't have setup sending mail from (X)Emacs, please copy the
  20118. output buffer into your mail program, as it gives us important
  20119. information about your Org-mode version and configuration."
  20120. (interactive)
  20121. (require 'reporter)
  20122. (defvar reporter-prompt-for-summary-p)
  20123. (org-load-modules-maybe)
  20124. (org-require-autoloaded-modules)
  20125. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20126. (reporter-submit-bug-report
  20127. "emacs-orgmode@gnu.org"
  20128. (org-version nil 'full)
  20129. (let (list)
  20130. (save-window-excursion
  20131. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20132. (delete-other-windows)
  20133. (erase-buffer)
  20134. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20135. We would like to add your full Org-mode and Outline configuration to the
  20136. bug report. This greatly simplifies the work of the maintainer and
  20137. other experts on the mailing list.
  20138. HOWEVER, some variables you have customized may contain private
  20139. information. The names of customers, colleagues, or friends, might
  20140. appear in the form of file names, tags, todo states, or search strings.
  20141. If you answer yes to the prompt, you might want to check and remove
  20142. such private information before sending the email.")
  20143. (add-text-properties (point-min) (point-max) '(face org-warning))
  20144. (when (yes-or-no-p "Include your Org-mode configuration ")
  20145. (mapatoms
  20146. (lambda (v)
  20147. (and (boundp v)
  20148. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20149. (or (and (symbol-value v)
  20150. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20151. (and
  20152. (get v 'custom-type) (get v 'standard-value)
  20153. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20154. (push v list)))))
  20155. (kill-buffer (get-buffer "*Warn about privacy*"))
  20156. list))
  20157. nil nil
  20158. "Remember to cover the basics, that is, what you expected to happen and
  20159. what in fact did happen. You don't know how to make a good report? See
  20160. http://orgmode.org/manual/Feedback.html#Feedback
  20161. Your bug report will be posted to the Org-mode mailing list.
  20162. ------------------------------------------------------------------------")
  20163. (save-excursion
  20164. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20165. (replace-match "\\1Bug: \\3 [\\2]")))))
  20166. (defun org-install-agenda-files-menu ()
  20167. (let ((bl (buffer-list)))
  20168. (save-excursion
  20169. (while bl
  20170. (set-buffer (pop bl))
  20171. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20172. (when (derived-mode-p 'org-mode)
  20173. (easy-menu-change
  20174. '("Org") "File List for Agenda"
  20175. (append
  20176. (list
  20177. ["Edit File List" (org-edit-agenda-file-list) t]
  20178. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20179. ["Remove Current File from List" org-remove-file t]
  20180. ["Cycle through agenda files" org-cycle-agenda-files t]
  20181. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20182. "--")
  20183. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20184. ;;;; Documentation
  20185. (defun org-require-autoloaded-modules ()
  20186. (interactive)
  20187. (mapc 'require
  20188. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20189. org-table org-timer)))
  20190. ;;;###autoload
  20191. (defun org-reload (&optional uncompiled)
  20192. "Reload all org lisp files.
  20193. With prefix arg UNCOMPILED, load the uncompiled versions."
  20194. (interactive "P")
  20195. (require 'loadhist)
  20196. (let* ((org-dir (org-find-library-dir "org"))
  20197. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20198. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20199. (remove-re (mapconcat 'identity
  20200. (mapcar (lambda (f) (concat "^" f "$"))
  20201. (list (if (featurep 'xemacs)
  20202. "org-colview"
  20203. "org-colview-xemacs")
  20204. "org" "org-loaddefs" "org-version"))
  20205. "\\|"))
  20206. (feats (delete-dups
  20207. (mapcar 'file-name-sans-extension
  20208. (mapcar 'file-name-nondirectory
  20209. (delq nil
  20210. (mapcar 'feature-file
  20211. features))))))
  20212. (lfeat (append
  20213. (sort
  20214. (setq feats
  20215. (delq nil (mapcar
  20216. (lambda (f)
  20217. (if (and (string-match feature-re f)
  20218. (not (string-match remove-re f)))
  20219. f nil))
  20220. feats)))
  20221. 'string-lessp)
  20222. (list "org-version" "org")))
  20223. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20224. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20225. load-uncore load-misses)
  20226. (setq load-misses
  20227. (delq 't
  20228. (mapcar (lambda (f)
  20229. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20230. (and (string= org-dir contrib-dir)
  20231. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20232. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20233. (add-to-list 'load-uncore f 'append)
  20234. 't)
  20235. f))
  20236. lfeat)))
  20237. (if load-uncore
  20238. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20239. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20240. (if load-misses
  20241. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20242. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20243. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20244. ;;;###autoload
  20245. (defun org-customize ()
  20246. "Call the customize function with org as argument."
  20247. (interactive)
  20248. (org-load-modules-maybe)
  20249. (org-require-autoloaded-modules)
  20250. (customize-browse 'org))
  20251. (defun org-create-customize-menu ()
  20252. "Create a full customization menu for Org-mode, insert it into the menu."
  20253. (interactive)
  20254. (org-load-modules-maybe)
  20255. (org-require-autoloaded-modules)
  20256. (if (fboundp 'customize-menu-create)
  20257. (progn
  20258. (easy-menu-change
  20259. '("Org") "Customize"
  20260. `(["Browse Org group" org-customize t]
  20261. "--"
  20262. ,(customize-menu-create 'org)
  20263. ["Set" Custom-set t]
  20264. ["Save" Custom-save t]
  20265. ["Reset to Current" Custom-reset-current t]
  20266. ["Reset to Saved" Custom-reset-saved t]
  20267. ["Reset to Standard Settings" Custom-reset-standard t]))
  20268. (message "\"Org\"-menu now contains full customization menu"))
  20269. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20270. ;;;; Miscellaneous stuff
  20271. ;;; Generally useful functions
  20272. (defsubst org-get-at-eol (property n)
  20273. "Get text property PROPERTY at the end of line less N characters."
  20274. (get-text-property (- (point-at-eol) n) property))
  20275. (defun org-find-text-property-in-string (prop s)
  20276. "Return the first non-nil value of property PROP in string S."
  20277. (or (get-text-property 0 prop s)
  20278. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20279. prop s)))
  20280. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20281. "Display the given MESSAGE as a warning."
  20282. (if (fboundp 'display-warning)
  20283. (display-warning 'org message
  20284. (if (featurep 'xemacs) 'warning :warning))
  20285. (let ((buf (get-buffer-create "*Org warnings*")))
  20286. (with-current-buffer buf
  20287. (goto-char (point-max))
  20288. (insert "Warning (Org): " message)
  20289. (unless (bolp)
  20290. (newline)))
  20291. (display-buffer buf)
  20292. (sit-for 0))))
  20293. (defun org-eval (form)
  20294. "Eval FORM and return result."
  20295. (condition-case error
  20296. (eval form)
  20297. (error (format "%%![Error: %s]" error))))
  20298. (defun org-in-clocktable-p ()
  20299. "Check if the cursor is in a clocktable."
  20300. (let ((pos (point)) start)
  20301. (save-excursion
  20302. (end-of-line 1)
  20303. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20304. (setq start (match-beginning 0))
  20305. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20306. (>= (match-end 0) pos)
  20307. start))))
  20308. (defun org-in-verbatim-emphasis ()
  20309. (save-match-data
  20310. (and (org-in-regexp org-emph-re 2)
  20311. (>= (point) (match-beginning 3))
  20312. (<= (point) (match-end 4))
  20313. (member (match-string 3) '("=" "~")))))
  20314. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20315. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20316. (if (and marker (marker-buffer marker)
  20317. (buffer-live-p (marker-buffer marker)))
  20318. (progn
  20319. (org-pop-to-buffer-same-window (marker-buffer marker))
  20320. (if (or (> marker (point-max)) (< marker (point-min)))
  20321. (widen))
  20322. (goto-char marker)
  20323. (org-show-context 'org-goto))
  20324. (if bookmark
  20325. (bookmark-jump bookmark)
  20326. (error "Cannot find location"))))
  20327. (defun org-quote-csv-field (s)
  20328. "Quote field for inclusion in CSV material."
  20329. (if (string-match "[\",]" s)
  20330. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20331. s))
  20332. (defun org-force-self-insert (N)
  20333. "Needed to enforce self-insert under remapping."
  20334. (interactive "p")
  20335. (self-insert-command N))
  20336. (defun org-string-width (s)
  20337. "Compute width of string, ignoring invisible characters.
  20338. This ignores character with invisibility property `org-link', and also
  20339. characters with property `org-cwidth', because these will become invisible
  20340. upon the next fontification round."
  20341. (let (b l)
  20342. (when (or (eq t buffer-invisibility-spec)
  20343. (assq 'org-link buffer-invisibility-spec))
  20344. (while (setq b (text-property-any 0 (length s)
  20345. 'invisible 'org-link s))
  20346. (setq s (concat (substring s 0 b)
  20347. (substring s (or (next-single-property-change
  20348. b 'invisible s)
  20349. (length s)))))))
  20350. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20351. (setq s (concat (substring s 0 b)
  20352. (substring s (or (next-single-property-change
  20353. b 'org-cwidth s)
  20354. (length s))))))
  20355. (setq l (string-width s) b -1)
  20356. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20357. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20358. l))
  20359. (defun org-shorten-string (s maxlength)
  20360. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20361. If the string is shorter or has length MAXLENGTH, just return the
  20362. original string. If it is longer, the functions finds a space in the
  20363. string, breaks this string off at that locations and adds three dots
  20364. as ellipsis. Including the ellipsis, the string will not be longer
  20365. than MAXLENGTH. If finding a good breaking point in the string does
  20366. not work, the string is just chopped off in the middle of a word
  20367. if necessary."
  20368. (if (<= (length s) maxlength)
  20369. s
  20370. (let* ((n (max (- maxlength 4) 1))
  20371. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20372. (if (string-match re s)
  20373. (concat (match-string 1 s) "...")
  20374. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20375. (defun org-get-indentation (&optional line)
  20376. "Get the indentation of the current line, interpreting tabs.
  20377. When LINE is given, assume it represents a line and compute its indentation."
  20378. (if line
  20379. (if (string-match "^ *" (org-remove-tabs line))
  20380. (match-end 0))
  20381. (save-excursion
  20382. (beginning-of-line 1)
  20383. (skip-chars-forward " \t")
  20384. (current-column))))
  20385. (defun org-get-string-indentation (s)
  20386. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20387. (let ((n -1) (i 0) (w tab-width) c)
  20388. (catch 'exit
  20389. (while (< (setq n (1+ n)) (length s))
  20390. (setq c (aref s n))
  20391. (cond ((= c ?\ ) (setq i (1+ i)))
  20392. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20393. (t (throw 'exit t)))))
  20394. i))
  20395. (defun org-remove-tabs (s &optional width)
  20396. "Replace tabulators in S with spaces.
  20397. Assumes that s is a single line, starting in column 0."
  20398. (setq width (or width tab-width))
  20399. (while (string-match "\t" s)
  20400. (setq s (replace-match
  20401. (make-string
  20402. (- (* width (/ (+ (match-beginning 0) width) width))
  20403. (match-beginning 0)) ?\ )
  20404. t t s)))
  20405. s)
  20406. (defun org-fix-indentation (line ind)
  20407. "Fix indentation in LINE.
  20408. IND is a cons cell with target and minimum indentation.
  20409. If the current indentation in LINE is smaller than the minimum,
  20410. leave it alone. If it is larger than ind, set it to the target."
  20411. (let* ((l (org-remove-tabs line))
  20412. (i (org-get-indentation l))
  20413. (i1 (car ind)) (i2 (cdr ind)))
  20414. (if (>= i i2) (setq l (substring line i2)))
  20415. (if (> i1 0)
  20416. (concat (make-string i1 ?\ ) l)
  20417. l)))
  20418. (defun org-remove-indentation (code &optional n)
  20419. "Remove the maximum common indentation from the lines in CODE.
  20420. N may optionally be the number of spaces to remove."
  20421. (with-temp-buffer
  20422. (insert code)
  20423. (org-do-remove-indentation n)
  20424. (buffer-string)))
  20425. (defun org-do-remove-indentation (&optional n)
  20426. "Remove the maximum common indentation from the buffer."
  20427. (untabify (point-min) (point-max))
  20428. (let ((min 10000) re)
  20429. (if n
  20430. (setq min n)
  20431. (goto-char (point-min))
  20432. (while (re-search-forward "^ *[^ \n]" nil t)
  20433. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20434. (unless (or (= min 0) (= min 10000))
  20435. (setq re (format "^ \\{%d\\}" min))
  20436. (goto-char (point-min))
  20437. (while (re-search-forward re nil t)
  20438. (replace-match "")
  20439. (end-of-line 1))
  20440. min)))
  20441. (defun org-fill-template (template alist)
  20442. "Find each %key of ALIST in TEMPLATE and replace it."
  20443. (let ((case-fold-search nil))
  20444. (dolist (entry (sort (copy-sequence alist)
  20445. (lambda (a b) (< (length (car a)) (length (car b))))))
  20446. (setq template
  20447. (replace-regexp-in-string
  20448. (concat "%" (regexp-quote (car entry)))
  20449. (or (cdr entry) "") template t t)))
  20450. template))
  20451. (defun org-base-buffer (buffer)
  20452. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20453. (if (not buffer)
  20454. buffer
  20455. (or (buffer-base-buffer buffer)
  20456. buffer)))
  20457. (defun org-wrap (string &optional width lines)
  20458. "Wrap string to either a number of lines, or a width in characters.
  20459. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20460. that costs. If there is a word longer than WIDTH, the text is actually
  20461. wrapped to the length of that word.
  20462. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20463. many lines, whatever width that takes.
  20464. The return value is a list of lines, without newlines at the end."
  20465. (let* ((words (org-split-string string "[ \t\n]+"))
  20466. (maxword (apply 'max (mapcar 'org-string-width words)))
  20467. w ll)
  20468. (cond (width
  20469. (org-do-wrap words (max maxword width)))
  20470. (lines
  20471. (setq w maxword)
  20472. (setq ll (org-do-wrap words maxword))
  20473. (if (<= (length ll) lines)
  20474. ll
  20475. (setq ll words)
  20476. (while (> (length ll) lines)
  20477. (setq w (1+ w))
  20478. (setq ll (org-do-wrap words w)))
  20479. ll))
  20480. (t (error "Cannot wrap this")))))
  20481. (defun org-do-wrap (words width)
  20482. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20483. (let (lines line)
  20484. (while words
  20485. (setq line (pop words))
  20486. (while (and words (< (+ (length line) (length (car words))) width))
  20487. (setq line (concat line " " (pop words))))
  20488. (setq lines (push line lines)))
  20489. (nreverse lines)))
  20490. (defun org-split-string (string &optional separators)
  20491. "Splits STRING into substrings at SEPARATORS.
  20492. SEPARATORS is a regular expression.
  20493. No empty strings are returned if there are matches at the beginning
  20494. and end of string."
  20495. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20496. (let ((start 0) notfirst list)
  20497. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20498. (if (and notfirst
  20499. (= start (match-beginning 0))
  20500. (< start (length string)))
  20501. (1+ start) start))
  20502. (< (match-beginning 0) (length string)))
  20503. (setq notfirst t)
  20504. (or (eq (match-beginning 0) 0)
  20505. (and (eq (match-beginning 0) (match-end 0))
  20506. (eq (match-beginning 0) start))
  20507. (push (substring string start (match-beginning 0)) list))
  20508. (setq start (match-end 0)))
  20509. (or (eq start (length string))
  20510. (push (substring string start) list))
  20511. (nreverse list)))
  20512. (defun org-quote-vert (s)
  20513. "Replace \"|\" with \"\\vert\"."
  20514. (while (string-match "|" s)
  20515. (setq s (replace-match "\\vert" t t s)))
  20516. s)
  20517. (defun org-uuidgen-p (s)
  20518. "Is S an ID created by UUIDGEN?"
  20519. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20520. (defun org-in-src-block-p (&optional inside)
  20521. "Whether point is in a code source block.
  20522. When INSIDE is non-nil, don't consider we are within a src block
  20523. when point is at #+BEGIN_SRC or #+END_SRC."
  20524. (let ((case-fold-search t) ov)
  20525. (or (and (eq (get-char-property (point) 'src-block) t))
  20526. (and (not inside)
  20527. (save-match-data
  20528. (save-excursion
  20529. (beginning-of-line)
  20530. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20531. (defun org-context ()
  20532. "Return a list of contexts of the current cursor position.
  20533. If several contexts apply, all are returned.
  20534. Each context entry is a list with a symbol naming the context, and
  20535. two positions indicating start and end of the context. Possible
  20536. contexts are:
  20537. :headline anywhere in a headline
  20538. :headline-stars on the leading stars in a headline
  20539. :todo-keyword on a TODO keyword (including DONE) in a headline
  20540. :tags on the TAGS in a headline
  20541. :priority on the priority cookie in a headline
  20542. :item on the first line of a plain list item
  20543. :item-bullet on the bullet/number of a plain list item
  20544. :checkbox on the checkbox in a plain list item
  20545. :table in an org-mode table
  20546. :table-special on a special filed in a table
  20547. :table-table in a table.el table
  20548. :clocktable in a clocktable
  20549. :src-block in a source block
  20550. :link on a hyperlink
  20551. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20552. :target on a <<target>>
  20553. :radio-target on a <<<radio-target>>>
  20554. :latex-fragment on a LaTeX fragment
  20555. :latex-preview on a LaTeX fragment with overlaid preview image
  20556. This function expects the position to be visible because it uses font-lock
  20557. faces as a help to recognize the following contexts: :table-special, :link,
  20558. and :keyword."
  20559. (let* ((f (get-text-property (point) 'face))
  20560. (faces (if (listp f) f (list f)))
  20561. (case-fold-search t)
  20562. (p (point)) clist o)
  20563. ;; First the large context
  20564. (cond
  20565. ((org-at-heading-p t)
  20566. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20567. (when (progn
  20568. (beginning-of-line 1)
  20569. (looking-at org-todo-line-tags-regexp))
  20570. (push (org-point-in-group p 1 :headline-stars) clist)
  20571. (push (org-point-in-group p 2 :todo-keyword) clist)
  20572. (push (org-point-in-group p 4 :tags) clist))
  20573. (goto-char p)
  20574. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20575. (if (looking-at "\\[#[A-Z0-9]\\]")
  20576. (push (org-point-in-group p 0 :priority) clist)))
  20577. ((org-at-item-p)
  20578. (push (org-point-in-group p 2 :item-bullet) clist)
  20579. (push (list :item (point-at-bol)
  20580. (save-excursion (org-end-of-item) (point)))
  20581. clist)
  20582. (and (org-at-item-checkbox-p)
  20583. (push (org-point-in-group p 0 :checkbox) clist)))
  20584. ((org-at-table-p)
  20585. (push (list :table (org-table-begin) (org-table-end)) clist)
  20586. (if (memq 'org-formula faces)
  20587. (push (list :table-special
  20588. (previous-single-property-change p 'face)
  20589. (next-single-property-change p 'face)) clist)))
  20590. ((org-at-table-p 'any)
  20591. (push (list :table-table) clist)))
  20592. (goto-char p)
  20593. (let ((case-fold-search t))
  20594. ;; New the "medium" contexts: clocktables, source blocks
  20595. (cond ((org-in-clocktable-p)
  20596. (push (list :clocktable
  20597. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20598. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20599. (match-beginning 1))
  20600. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20601. (match-end 0))) clist))
  20602. ((org-in-src-block-p)
  20603. (push (list :src-block
  20604. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20605. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20606. (match-beginning 1))
  20607. (and (search-forward "#+END_SRC" nil t)
  20608. (match-beginning 0))) clist))))
  20609. (goto-char p)
  20610. ;; Now the small context
  20611. (cond
  20612. ((org-at-timestamp-p)
  20613. (push (org-point-in-group p 0 :timestamp) clist))
  20614. ((memq 'org-link faces)
  20615. (push (list :link
  20616. (previous-single-property-change p 'face)
  20617. (next-single-property-change p 'face)) clist))
  20618. ((memq 'org-special-keyword faces)
  20619. (push (list :keyword
  20620. (previous-single-property-change p 'face)
  20621. (next-single-property-change p 'face)) clist))
  20622. ((org-at-target-p)
  20623. (push (org-point-in-group p 0 :target) clist)
  20624. (goto-char (1- (match-beginning 0)))
  20625. (if (looking-at org-radio-target-regexp)
  20626. (push (org-point-in-group p 0 :radio-target) clist))
  20627. (goto-char p))
  20628. ((setq o (car (delq nil
  20629. (mapcar
  20630. (lambda (x)
  20631. (if (memq x org-latex-fragment-image-overlays) x))
  20632. (overlays-at (point))))))
  20633. (push (list :latex-fragment
  20634. (overlay-start o) (overlay-end o)) clist)
  20635. (push (list :latex-preview
  20636. (overlay-start o) (overlay-end o)) clist))
  20637. ((org-inside-LaTeX-fragment-p)
  20638. ;; FIXME: positions wrong.
  20639. (push (list :latex-fragment (point) (point)) clist)))
  20640. (setq clist (nreverse (delq nil clist)))
  20641. clist))
  20642. (defun org-in-regexp (re &optional nlines visually)
  20643. "Check if point is inside a match of RE.
  20644. Normally only the current line is checked, but you can include
  20645. NLINES extra lines after point into the search. If VISUALLY is
  20646. set, require that the cursor is not after the match but really
  20647. on, so that the block visually is on the match."
  20648. (catch 'exit
  20649. (let ((pos (point))
  20650. (eol (point-at-eol (+ 1 (or nlines 0))))
  20651. (inc (if visually 1 0)))
  20652. (save-excursion
  20653. (beginning-of-line (- 1 (or nlines 0)))
  20654. (while (re-search-forward re eol t)
  20655. (if (and (<= (match-beginning 0) pos)
  20656. (>= (+ inc (match-end 0)) pos))
  20657. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20658. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20659. "Org mode 8.3")
  20660. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20661. "Non-nil when point is between matches of START-RE and END-RE.
  20662. Also return a non-nil value when point is on one of the matches.
  20663. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20664. buffer positions. Default values are the positions of headlines
  20665. surrounding the point.
  20666. The functions returns a cons cell whose car (resp. cdr) is the
  20667. position before START-RE (resp. after END-RE)."
  20668. (save-match-data
  20669. (let ((pos (point))
  20670. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20671. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20672. beg end)
  20673. (save-excursion
  20674. ;; Point is on a block when on START-RE or if START-RE can be
  20675. ;; found before it...
  20676. (and (or (org-in-regexp start-re)
  20677. (re-search-backward start-re limit-up t))
  20678. (setq beg (match-beginning 0))
  20679. ;; ... and END-RE after it...
  20680. (goto-char (match-end 0))
  20681. (re-search-forward end-re limit-down t)
  20682. (> (setq end (match-end 0)) pos)
  20683. ;; ... without another START-RE in-between.
  20684. (goto-char (match-beginning 0))
  20685. (not (re-search-backward start-re (1+ beg) t))
  20686. ;; Return value.
  20687. (cons beg end))))))
  20688. (defun org-in-block-p (names)
  20689. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20690. NAMES is a list of strings containing names of blocks.
  20691. Return first block name matched, or nil. Beware that in case of
  20692. nested blocks, the returned name may not belong to the closest
  20693. block from point."
  20694. (save-match-data
  20695. (catch 'exit
  20696. (let ((case-fold-search t)
  20697. (lim-up (save-excursion (outline-previous-heading)))
  20698. (lim-down (save-excursion (outline-next-heading))))
  20699. (mapc (lambda (name)
  20700. (let ((n (regexp-quote name)))
  20701. (when (org-between-regexps-p
  20702. (concat "^[ \t]*#\\+begin_" n)
  20703. (concat "^[ \t]*#\\+end_" n)
  20704. lim-up lim-down)
  20705. (throw 'exit n))))
  20706. names))
  20707. nil)))
  20708. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20709. "Call `multi-occur' with buffers for all agenda files."
  20710. (interactive "sOrg-files matching: ")
  20711. (let* ((files (org-agenda-files))
  20712. (tnames (mapcar #'file-truename files))
  20713. (extra org-agenda-text-search-extra-files))
  20714. (when (eq (car extra) 'agenda-archives)
  20715. (setq extra (cdr extra))
  20716. (setq files (org-add-archive-files files)))
  20717. (dolist (f extra)
  20718. (unless (member (file-truename f) tnames)
  20719. (unless (member f files) (setq files (append files (list f))))
  20720. (setq tnames (append tnames (list (file-truename f))))))
  20721. (multi-occur
  20722. (mapcar (lambda (x)
  20723. (with-current-buffer
  20724. ;; FIXME: Why not just (find-file-noselect x)?
  20725. ;; Is it to avoid the "revert buffer" prompt?
  20726. (or (get-file-buffer x) (find-file-noselect x))
  20727. (widen)
  20728. (current-buffer)))
  20729. files)
  20730. regexp)))
  20731. (if (boundp 'occur-mode-find-occurrence-hook)
  20732. ;; Emacs 23
  20733. (add-hook 'occur-mode-find-occurrence-hook
  20734. (lambda ()
  20735. (when (derived-mode-p 'org-mode)
  20736. (org-reveal))))
  20737. ;; Emacs 22
  20738. (defadvice occur-mode-goto-occurrence
  20739. (after org-occur-reveal activate)
  20740. (and (derived-mode-p 'org-mode) (org-reveal)))
  20741. (defadvice occur-mode-goto-occurrence-other-window
  20742. (after org-occur-reveal activate)
  20743. (and (derived-mode-p 'org-mode) (org-reveal)))
  20744. (defadvice occur-mode-display-occurrence
  20745. (after org-occur-reveal activate)
  20746. (when (derived-mode-p 'org-mode)
  20747. (let ((pos (occur-mode-find-occurrence)))
  20748. (with-current-buffer (marker-buffer pos)
  20749. (save-excursion
  20750. (goto-char pos)
  20751. (org-reveal)))))))
  20752. (defun org-occur-link-in-agenda-files ()
  20753. "Create a link and search for it in the agendas.
  20754. The link is not stored in `org-stored-links', it is just created
  20755. for the search purpose."
  20756. (interactive)
  20757. (let ((link (condition-case nil
  20758. (org-store-link nil)
  20759. (error "Unable to create a link to here"))))
  20760. (org-occur-in-agenda-files (regexp-quote link))))
  20761. (defun org-reverse-string (string)
  20762. "Return the reverse of STRING."
  20763. (apply 'string (reverse (string-to-list string))))
  20764. ;; defsubst org-uniquify must be defined before first use
  20765. (defun org-uniquify-alist (alist)
  20766. "Merge elements of ALIST with the same key.
  20767. For example, in this alist:
  20768. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20769. => '((a 1 3) (b 2))
  20770. merge (a 1) and (a 3) into (a 1 3).
  20771. The function returns the new ALIST."
  20772. (let (rtn)
  20773. (mapc
  20774. (lambda (e)
  20775. (let (n)
  20776. (if (not (assoc (car e) rtn))
  20777. (push e rtn)
  20778. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20779. (setq rtn (assq-delete-all (car e) rtn))
  20780. (push n rtn))))
  20781. alist)
  20782. rtn))
  20783. (defun org-delete-all (elts list)
  20784. "Remove all elements in ELTS from LIST."
  20785. (while elts
  20786. (setq list (delete (pop elts) list)))
  20787. list)
  20788. (defun org-count (cl-item cl-seq)
  20789. "Count the number of occurrences of ITEM in SEQ.
  20790. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20791. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20792. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20793. (while (< cl-start cl-end)
  20794. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20795. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20796. (setq cl-start (1+ cl-start)))
  20797. cl-count))
  20798. (defun org-remove-if (predicate seq)
  20799. "Remove everything from SEQ that fulfills PREDICATE."
  20800. (let (res e)
  20801. (while seq
  20802. (setq e (pop seq))
  20803. (if (not (funcall predicate e)) (push e res)))
  20804. (nreverse res)))
  20805. (defun org-remove-if-not (predicate seq)
  20806. "Remove everything from SEQ that does not fulfill PREDICATE."
  20807. (let (res e)
  20808. (while seq
  20809. (setq e (pop seq))
  20810. (if (funcall predicate e) (push e res)))
  20811. (nreverse res)))
  20812. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20813. "Reduce two-argument FUNCTION across SEQ.
  20814. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20815. \":initial-value\" removed."
  20816. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20817. (cadr (memq :initial-value cl-keys)))
  20818. (cl-seq (pop cl-seq))
  20819. (t (funcall cl-func)))))
  20820. (while cl-seq
  20821. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20822. cl-accum))
  20823. (defun org-every (pred seq)
  20824. "Return true if PREDICATE is true of every element of SEQ.
  20825. Adapted from `every' in cl.el."
  20826. (catch 'org-every
  20827. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20828. t))
  20829. (defun org-some (pred seq)
  20830. "Return true if PREDICATE is true of any element of SEQ.
  20831. Adapted from `some' in cl.el."
  20832. (catch 'org-some
  20833. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20834. nil))
  20835. (defun org-back-over-empty-lines ()
  20836. "Move backwards over whitespace, to the beginning of the first empty line.
  20837. Returns the number of empty lines passed."
  20838. (let ((pos (point)))
  20839. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20840. (skip-chars-backward " \t\n\r")
  20841. (unless (eobp)
  20842. (forward-line -1)))
  20843. (beginning-of-line 2)
  20844. (goto-char (min (point) pos))
  20845. (count-lines (point) pos)))
  20846. (defun org-skip-whitespace ()
  20847. (skip-chars-forward " \t\n\r"))
  20848. (defun org-point-in-group (point group &optional context)
  20849. "Check if POINT is in match-group GROUP.
  20850. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20851. match. If the match group does not exist or point is not inside it,
  20852. return nil."
  20853. (and (match-beginning group)
  20854. (>= point (match-beginning group))
  20855. (<= point (match-end group))
  20856. (if context
  20857. (list context (match-beginning group) (match-end group))
  20858. t)))
  20859. (defun org-switch-to-buffer-other-window (&rest args)
  20860. "Switch to buffer in a second window on the current frame.
  20861. In particular, do not allow pop-up frames.
  20862. Returns the newly created buffer."
  20863. (org-no-popups
  20864. (apply 'switch-to-buffer-other-window args)))
  20865. (defun org-combine-plists (&rest plists)
  20866. "Create a single property list from all plists in PLISTS.
  20867. The process starts by copying the first list, and then setting properties
  20868. from the other lists. Settings in the last list are the most significant
  20869. ones and overrule settings in the other lists."
  20870. (let ((rtn (copy-sequence (pop plists)))
  20871. p v ls)
  20872. (while plists
  20873. (setq ls (pop plists))
  20874. (while ls
  20875. (setq p (pop ls) v (pop ls))
  20876. (setq rtn (plist-put rtn p v))))
  20877. rtn))
  20878. (defun org-replace-escapes (string table)
  20879. "Replace %-escapes in STRING with values in TABLE.
  20880. TABLE is an association list with keys like \"%a\" and string values.
  20881. The sequences in STRING may contain normal field width and padding information,
  20882. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20883. so values can contain further %-escapes if they are define later in TABLE."
  20884. (let ((tbl (copy-alist table))
  20885. (case-fold-search nil)
  20886. (pchg 0)
  20887. e re rpl)
  20888. (dolist (e tbl)
  20889. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20890. (when (and (cdr e) (string-match re (cdr e)))
  20891. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20892. (safe "SREF"))
  20893. (add-text-properties 0 3 (list 'sref sref) safe)
  20894. (setcdr e (replace-match safe t t (cdr e)))))
  20895. (while (string-match re string)
  20896. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20897. (cdr e)))
  20898. (setq string (replace-match rpl t t string))))
  20899. (while (setq pchg (next-property-change pchg string))
  20900. (let ((sref (get-text-property pchg 'sref string)))
  20901. (when (and sref (string-match "SREF" string pchg))
  20902. (setq string (replace-match sref t t string)))))
  20903. string))
  20904. (defun org-sublist (list start end)
  20905. "Return a section of LIST, from START to END.
  20906. Counting starts at 1."
  20907. (let (rtn (c start))
  20908. (setq list (nthcdr (1- start) list))
  20909. (while (and list (<= c end))
  20910. (push (pop list) rtn)
  20911. (setq c (1+ c)))
  20912. (nreverse rtn)))
  20913. (defun org-find-base-buffer-visiting (file)
  20914. "Like `find-buffer-visiting' but always return the base buffer and
  20915. not an indirect buffer."
  20916. (let ((buf (or (get-file-buffer file)
  20917. (find-buffer-visiting file))))
  20918. (if buf
  20919. (or (buffer-base-buffer buf) buf)
  20920. nil)))
  20921. (defun org-image-file-name-regexp (&optional extensions)
  20922. "Return regexp matching the file names of images.
  20923. If EXTENSIONS is given, only match these."
  20924. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20925. (image-file-name-regexp)
  20926. (let ((image-file-name-extensions
  20927. (or extensions
  20928. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20929. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20930. (concat "\\."
  20931. (regexp-opt (nconc (mapcar 'upcase
  20932. image-file-name-extensions)
  20933. image-file-name-extensions)
  20934. t)
  20935. "\\'"))))
  20936. (defun org-file-image-p (file &optional extensions)
  20937. "Return non-nil if FILE is an image."
  20938. (save-match-data
  20939. (string-match (org-image-file-name-regexp extensions) file)))
  20940. (defun org-get-cursor-date (&optional with-time)
  20941. "Return the date at cursor in as a time.
  20942. This works in the calendar and in the agenda, anywhere else it just
  20943. returns the current time.
  20944. If WITH-TIME is non-nil, returns the time of the event at point (in
  20945. the agenda) or the current time of the day."
  20946. (let (date day defd tp hod mod)
  20947. (when with-time
  20948. (setq tp (get-text-property (point) 'time))
  20949. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20950. (setq hod (string-to-number (match-string 1 tp))
  20951. mod (string-to-number (match-string 2 tp))))
  20952. (or tp (let ((now (decode-time)))
  20953. (setq hod (nth 2 now)
  20954. mod (nth 1 now)))))
  20955. (cond
  20956. ((eq major-mode 'calendar-mode)
  20957. (setq date (calendar-cursor-to-date)
  20958. defd (encode-time 0 (or mod 0) (or hod 0)
  20959. (nth 1 date) (nth 0 date) (nth 2 date))))
  20960. ((eq major-mode 'org-agenda-mode)
  20961. (setq day (get-text-property (point) 'day))
  20962. (if day
  20963. (setq date (calendar-gregorian-from-absolute day)
  20964. defd (encode-time 0 (or mod 0) (or hod 0)
  20965. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20966. (or defd (current-time))))
  20967. (defun org-mark-subtree (&optional up)
  20968. "Mark the current subtree.
  20969. This puts point at the start of the current subtree, and mark at
  20970. the end. If a numeric prefix UP is given, move up into the
  20971. hierarchy of headlines by UP levels before marking the subtree."
  20972. (interactive "P")
  20973. (org-with-limited-levels
  20974. (cond ((org-at-heading-p) (beginning-of-line))
  20975. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20976. (t (outline-previous-visible-heading 1))))
  20977. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20978. (if (org-called-interactively-p 'any)
  20979. (call-interactively 'org-mark-element)
  20980. (org-mark-element)))
  20981. ;;; Indentation
  20982. (defun org--get-expected-indentation (element contentsp)
  20983. "Expected indentation column for current line, according to ELEMENT.
  20984. ELEMENT is an element containing point. CONTENTSP is non-nil
  20985. when indentation is to be computed according to contents of
  20986. ELEMENT."
  20987. (let ((type (org-element-type element))
  20988. (start (org-element-property :begin element))
  20989. (post-affiliated (org-element-property :post-affiliated element)))
  20990. (org-with-wide-buffer
  20991. (cond
  20992. (contentsp
  20993. (case type
  20994. ((diary-sexp footnote-definition) 0)
  20995. ((headline inlinetask nil)
  20996. (if (not org-adapt-indentation) 0
  20997. (let ((level (org-current-level)))
  20998. (if level (1+ level) 0))))
  20999. ((item plain-list) (org-list-item-body-column post-affiliated))
  21000. (t
  21001. (goto-char start)
  21002. (org-get-indentation))))
  21003. ((memq type '(headline inlinetask nil))
  21004. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21005. (org--get-expected-indentation element t)
  21006. 0))
  21007. ((memq type '(diary-sexp footnote-definition)) 0)
  21008. ;; First paragraph of a footnote definition or an item.
  21009. ;; Indent like parent.
  21010. ((< (line-beginning-position) start)
  21011. (org--get-expected-indentation
  21012. (org-element-property :parent element) t))
  21013. ;; At first line: indent according to previous sibling, if any,
  21014. ;; ignoring footnote definitions and inline tasks, or parent's
  21015. ;; contents.
  21016. ((= (line-beginning-position) start)
  21017. (catch 'exit
  21018. (while t
  21019. (if (= (point-min) start) (throw 'exit 0)
  21020. (goto-char (1- start))
  21021. (let* ((previous (org-element-at-point))
  21022. (parent previous))
  21023. (while (and parent (<= (org-element-property :end parent) start))
  21024. (setq previous parent
  21025. parent (org-element-property :parent parent)))
  21026. (cond
  21027. ((not previous) (throw 'exit 0))
  21028. ((> (org-element-property :end previous) start)
  21029. (throw 'exit (org--get-expected-indentation previous t)))
  21030. ((memq (org-element-type previous)
  21031. '(footnote-definition inlinetask))
  21032. (setq start (org-element-property :begin previous)))
  21033. (t (goto-char (org-element-property :begin previous))
  21034. (throw 'exit
  21035. (if (bolp) (org-get-indentation)
  21036. ;; At first paragraph in an item or
  21037. ;; a footnote definition.
  21038. (org--get-expected-indentation
  21039. (org-element-property :parent previous) t))))))))))
  21040. ;; Otherwise, move to the first non-blank line above.
  21041. (t
  21042. (beginning-of-line)
  21043. (let ((pos (point)))
  21044. (skip-chars-backward " \r\t\n")
  21045. (cond
  21046. ;; Two blank lines end a footnote definition or a plain
  21047. ;; list. When we indent an empty line after them, the
  21048. ;; containing list or footnote definition is over, so it
  21049. ;; qualifies as a previous sibling. Therefore, we indent
  21050. ;; like its first line.
  21051. ((and (memq type '(footnote-definition plain-list))
  21052. (> (count-lines (point) pos) 2))
  21053. (goto-char start)
  21054. (org-get-indentation))
  21055. ;; Line above is the first one of a paragraph at the
  21056. ;; beginning of an item or a footnote definition. Indent
  21057. ;; like parent.
  21058. ((< (line-beginning-position) start)
  21059. (org--get-expected-indentation
  21060. (org-element-property :parent element) t))
  21061. ;; Line above is the beginning of an element, i.e., point
  21062. ;; was originally on the blank lines between element's start
  21063. ;; and contents.
  21064. ((= (line-beginning-position) post-affiliated)
  21065. (org--get-expected-indentation element t))
  21066. ;; POS is after contents in a greater element. Indent like
  21067. ;; the beginning of the element.
  21068. ;;
  21069. ;; As a special case, if point is at the end of a footnote
  21070. ;; definition or an item, indent like the very last element
  21071. ;; within.
  21072. ((and (not (eq type 'paragraph))
  21073. (let ((cend (org-element-property :contents-end element)))
  21074. (and cend (<= cend pos))))
  21075. (if (memq type '(footnote-definition item plain-list))
  21076. (let ((last (org-element-at-point)))
  21077. (org--get-expected-indentation
  21078. last
  21079. (memq (org-element-type last)
  21080. '(footnote-definition item plain-list))))
  21081. (goto-char start)
  21082. (org-get-indentation)))
  21083. ;; In any other case, indent like the current line.
  21084. (t (org-get-indentation)))))))))
  21085. (defun org--align-node-property ()
  21086. "Align node property at point.
  21087. Alignment is done according to `org-property-format', which see."
  21088. (when (save-excursion
  21089. (beginning-of-line)
  21090. (looking-at org-property-re))
  21091. (replace-match
  21092. (concat (match-string 4)
  21093. (org-trim
  21094. (format org-property-format (match-string 1) (match-string 3))))
  21095. t t)))
  21096. (defun org-indent-line ()
  21097. "Indent line depending on context.
  21098. Indentation is done according to the following rules:
  21099. - Footnote definitions, diary sexps, headlines and inline tasks
  21100. have to start at column 0.
  21101. - On the very first line of an element, consider, in order, the
  21102. next rules until one matches:
  21103. 1. If there's a sibling element before, ignoring footnote
  21104. definitions and inline tasks, indent like its first line.
  21105. 2. If element has a parent, indent like its contents. More
  21106. precisely, if parent is an item, indent after the
  21107. description part, if any, or the bullet (see
  21108. `org-list-description-max-indent'). Else, indent like
  21109. parent's first line.
  21110. 3. Otherwise, indent relatively to current level, if
  21111. `org-adapt-indentation' is non-nil, or to left margin.
  21112. - On a blank line at the end of an element, indent according to
  21113. the type of the element. More precisely
  21114. 1. If element is a plain list, an item, or a footnote
  21115. definition, indent like the very last element within.
  21116. 2. If element is a paragraph, indent like its last non blank
  21117. line.
  21118. 3. Otherwise, indent like its very first line.
  21119. - In the code part of a source block, use language major mode
  21120. to indent current line if `org-src-tab-acts-natively' is
  21121. non-nil. If it is nil, do nothing.
  21122. - Otherwise, indent like the first non-blank line above.
  21123. The function doesn't indent an item as it could break the whole
  21124. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21125. \\[org-shiftmetaright].
  21126. Also align node properties according to `org-property-format'."
  21127. (interactive)
  21128. (cond
  21129. (orgstruct-is-++
  21130. (let ((indent-line-function
  21131. (cadadr (assq 'indent-line-function org-fb-vars))))
  21132. (indent-according-to-mode)))
  21133. ((org-at-heading-p) 'noindent)
  21134. (t
  21135. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21136. (type (org-element-type element)))
  21137. (cond ((and (memq type '(plain-list item))
  21138. (= (line-beginning-position)
  21139. (org-element-property :post-affiliated element)))
  21140. 'noindent)
  21141. ((and (eq type 'src-block)
  21142. org-src-tab-acts-natively
  21143. (> (line-beginning-position)
  21144. (org-element-property :post-affiliated element))
  21145. (< (line-beginning-position)
  21146. (org-with-wide-buffer
  21147. (goto-char (org-element-property :end element))
  21148. (skip-chars-backward " \r\t\n")
  21149. (line-beginning-position))))
  21150. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21151. (t
  21152. (let ((column (org--get-expected-indentation element nil)))
  21153. ;; Preserve current column.
  21154. (if (<= (current-column) (current-indentation))
  21155. (org-indent-line-to column)
  21156. (save-excursion (org-indent-line-to column))))
  21157. ;; Align node property. Also preserve current column.
  21158. (when (eq type 'node-property)
  21159. (let ((column (current-column)))
  21160. (org--align-node-property)
  21161. (org-move-to-column column)))))))))
  21162. (defun org-indent-region (start end)
  21163. "Indent each non-blank line in the region.
  21164. Called from a program, START and END specify the region to
  21165. indent. The function will not indent contents of example blocks,
  21166. verse blocks and export blocks as leading white spaces are
  21167. assumed to be significant there."
  21168. (interactive "r")
  21169. (save-excursion
  21170. (goto-char start)
  21171. (skip-chars-forward " \r\t\n")
  21172. (unless (eobp) (beginning-of-line))
  21173. (let ((indent-to
  21174. (lambda (ind pos)
  21175. ;; Set IND as indentation for all lines between point and
  21176. ;; POS or END, whichever comes first. Blank lines are
  21177. ;; ignored. Leave point after POS once done.
  21178. (let ((limit (copy-marker (min end pos))))
  21179. (while (< (point) limit)
  21180. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21181. (forward-line))
  21182. (set-marker limit nil))))
  21183. (end (copy-marker end)))
  21184. (while (< (point) end)
  21185. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21186. (let* ((element (org-element-at-point))
  21187. (type (org-element-type element))
  21188. (element-end (copy-marker (org-element-property :end element)))
  21189. (ind (org--get-expected-indentation element nil)))
  21190. (cond
  21191. ((or (memq type '(paragraph table table-row))
  21192. (not (or (org-element-property :contents-begin element)
  21193. (memq type
  21194. '(example-block export-block src-block)))))
  21195. ;; Elements here are indented as a single block. Also
  21196. ;; align node properties.
  21197. (when (eq type 'node-property)
  21198. (org--align-node-property)
  21199. (beginning-of-line))
  21200. (funcall indent-to ind element-end))
  21201. (t
  21202. ;; Elements in this category consist of three parts:
  21203. ;; before the contents, the contents, and after the
  21204. ;; contents. The contents are treated specially,
  21205. ;; according to the element type, or not indented at
  21206. ;; all. Other parts are indented as a single block.
  21207. (let* ((post (copy-marker
  21208. (org-element-property :post-affiliated element)))
  21209. (cbeg
  21210. (copy-marker
  21211. (cond
  21212. ((not (org-element-property :contents-begin element))
  21213. ;; Fake contents for source blocks.
  21214. (org-with-wide-buffer
  21215. (goto-char post)
  21216. (forward-line)
  21217. (point)))
  21218. ((memq type '(footnote-definition item plain-list))
  21219. ;; Contents in these elements could start on
  21220. ;; the same line as the beginning of the
  21221. ;; element. Make sure we start indenting
  21222. ;; from the second line.
  21223. (org-with-wide-buffer
  21224. (goto-char post)
  21225. (end-of-line)
  21226. (skip-chars-forward " \r\t\n")
  21227. (if (eobp) (point) (line-beginning-position))))
  21228. (t (org-element-property :contents-begin element)))))
  21229. (cend (copy-marker
  21230. (or (org-element-property :contents-end element)
  21231. ;; Fake contents for source blocks.
  21232. (org-with-wide-buffer
  21233. (goto-char element-end)
  21234. (skip-chars-backward " \r\t\n")
  21235. (line-beginning-position)))
  21236. t)))
  21237. ;; Do not change items indentation individually as it
  21238. ;; might break the list as a whole. On the other
  21239. ;; hand, when at a plain list, indent it as a whole.
  21240. (cond ((eq type 'plain-list)
  21241. (let ((offset (- ind (org-get-indentation))))
  21242. (unless (zerop offset)
  21243. (indent-rigidly (org-element-property :begin element)
  21244. (org-element-property :end element)
  21245. offset))
  21246. (goto-char cbeg)))
  21247. ((eq type 'item) (goto-char cbeg))
  21248. (t (funcall indent-to ind cbeg)))
  21249. (when (< (point) end)
  21250. (case type
  21251. ((example-block export-block verse-block))
  21252. (src-block
  21253. ;; In a source block, indent source code
  21254. ;; according to language major mode, but only if
  21255. ;; `org-src-tab-acts-natively' is non-nil.
  21256. (when (and (< (point) end) org-src-tab-acts-natively)
  21257. (ignore-errors
  21258. (org-babel-do-in-edit-buffer
  21259. (indent-region (point-min) (point-max))))))
  21260. (t (org-indent-region (point) (min cend end))))
  21261. (goto-char (min cend end))
  21262. (when (< (point) end) (funcall indent-to ind element-end)))
  21263. (set-marker post nil)
  21264. (set-marker cbeg nil)
  21265. (set-marker cend nil))))
  21266. (set-marker element-end nil))))
  21267. (set-marker end nil))))
  21268. (defun org-indent-drawer ()
  21269. "Indent the drawer at point."
  21270. (interactive)
  21271. (unless (save-excursion
  21272. (beginning-of-line)
  21273. (org-looking-at-p org-drawer-regexp))
  21274. (user-error "Not at a drawer"))
  21275. (let ((element (org-element-at-point)))
  21276. (unless (memq (org-element-type element) '(drawer property-drawer))
  21277. (user-error "Not at a drawer"))
  21278. (org-with-wide-buffer
  21279. (org-indent-region (org-element-property :begin element)
  21280. (org-element-property :end element))))
  21281. (message "Drawer at point indented"))
  21282. (defun org-indent-block ()
  21283. "Indent the block at point."
  21284. (interactive)
  21285. (unless (save-excursion
  21286. (beginning-of-line)
  21287. (let ((case-fold-search t))
  21288. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21289. (user-error "Not at a block"))
  21290. (let ((element (org-element-at-point)))
  21291. (unless (memq (org-element-type element)
  21292. '(comment-block center-block dynamic-block example-block
  21293. export-block quote-block special-block
  21294. src-block verse-block))
  21295. (user-error "Not at a block"))
  21296. (org-with-wide-buffer
  21297. (org-indent-region (org-element-property :begin element)
  21298. (org-element-property :end element))))
  21299. (message "Block at point indented"))
  21300. ;;; Filling
  21301. ;; We use our own fill-paragraph and auto-fill functions.
  21302. ;; `org-fill-paragraph' relies on adaptive filling and context
  21303. ;; checking. Appropriate `fill-prefix' is computed with
  21304. ;; `org-adaptive-fill-function'.
  21305. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21306. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21307. ;; `org-adaptive-fill-function' value. Internally,
  21308. ;; `org-comment-line-break-function' breaks the line.
  21309. ;; `org-setup-filling' installs filling and auto-filling related
  21310. ;; variables during `org-mode' initialization.
  21311. (defvar org-element-paragraph-separate) ; org-element.el
  21312. (defun org-setup-filling ()
  21313. (require 'org-element)
  21314. ;; Prevent auto-fill from inserting unwanted new items.
  21315. (when (boundp 'fill-nobreak-predicate)
  21316. (org-set-local
  21317. 'fill-nobreak-predicate
  21318. (org-uniquify
  21319. (append fill-nobreak-predicate
  21320. '(org-fill-line-break-nobreak-p
  21321. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21322. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21323. (org-set-local 'paragraph-start paragraph-ending)
  21324. (org-set-local 'paragraph-separate paragraph-ending))
  21325. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21326. (org-set-local 'auto-fill-inhibit-regexp nil)
  21327. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21328. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21329. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21330. (defun org-fill-line-break-nobreak-p ()
  21331. "Non-nil when a new line at point would create an Org line break."
  21332. (save-excursion
  21333. (skip-chars-backward "[ \t]")
  21334. (skip-chars-backward "\\\\")
  21335. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21336. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21337. "Non-nil when a new line at point would split a timestamp."
  21338. (and (org-at-timestamp-p t)
  21339. (not (looking-at org-ts-regexp-both))))
  21340. (declare-function message-in-body-p "message" ())
  21341. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21342. (defun org-adaptive-fill-function ()
  21343. "Compute a fill prefix for the current line.
  21344. Return fill prefix, as a string, or nil if current line isn't
  21345. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21346. matches in paragraphs or comments, use it."
  21347. (catch 'exit
  21348. (when (derived-mode-p 'message-mode)
  21349. (save-excursion
  21350. (beginning-of-line)
  21351. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21352. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21353. ((looking-at message-cite-prefix-regexp)
  21354. (throw 'exit (match-string-no-properties 0)))
  21355. ((looking-at org-outline-regexp)
  21356. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21357. (org-with-wide-buffer
  21358. (unless (org-at-heading-p)
  21359. (let* ((p (line-beginning-position))
  21360. (element (save-excursion
  21361. (beginning-of-line)
  21362. (org-element-at-point)))
  21363. (type (org-element-type element))
  21364. (post-affiliated (org-element-property :post-affiliated element)))
  21365. (unless (< p post-affiliated)
  21366. (case type
  21367. (comment
  21368. (save-excursion
  21369. (beginning-of-line)
  21370. (looking-at "[ \t]*")
  21371. (concat (match-string 0) "# ")))
  21372. (footnote-definition "")
  21373. ((item plain-list)
  21374. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21375. (paragraph
  21376. ;; Fill prefix is usually the same as the current line,
  21377. ;; unless the paragraph is at the beginning of an item.
  21378. (let ((parent (org-element-property :parent element)))
  21379. (save-excursion
  21380. (beginning-of-line)
  21381. (cond ((eq (org-element-type parent) 'item)
  21382. (make-string (org-list-item-body-column
  21383. (org-element-property :begin parent))
  21384. ?\s))
  21385. ((and adaptive-fill-regexp
  21386. ;; Locally disable
  21387. ;; `adaptive-fill-function' to let
  21388. ;; `fill-context-prefix' handle
  21389. ;; `adaptive-fill-regexp' variable.
  21390. (let (adaptive-fill-function)
  21391. (fill-context-prefix
  21392. post-affiliated
  21393. (org-element-property :end element)))))
  21394. ((looking-at "[ \t]+") (match-string 0))
  21395. (t "")))))
  21396. (comment-block
  21397. ;; Only fill contents if P is within block boundaries.
  21398. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21399. (forward-line)
  21400. (point)))
  21401. (cend (save-excursion
  21402. (goto-char (org-element-property :end element))
  21403. (skip-chars-backward " \r\t\n")
  21404. (line-beginning-position))))
  21405. (when (and (>= p cbeg) (< p cend))
  21406. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21407. (match-string 0)
  21408. "")))))))))))
  21409. (declare-function message-goto-body "message" ())
  21410. (defvar message-cite-prefix-regexp) ; From message.el
  21411. (defun org-fill-paragraph (&optional justify)
  21412. "Fill element at point, when applicable.
  21413. This function only applies to comment blocks, comments, example
  21414. blocks and paragraphs. Also, as a special case, re-align table
  21415. when point is at one.
  21416. If JUSTIFY is non-nil (interactively, with prefix argument),
  21417. justify as well. If `sentence-end-double-space' is non-nil, then
  21418. period followed by one space does not end a sentence, so don't
  21419. break a line there. The variable `fill-column' controls the
  21420. width for filling.
  21421. For convenience, when point is at a plain list, an item or
  21422. a footnote definition, try to fill the first paragraph within."
  21423. (interactive)
  21424. (if (and (derived-mode-p 'message-mode)
  21425. (or (not (message-in-body-p))
  21426. (save-excursion (move-beginning-of-line 1)
  21427. (looking-at message-cite-prefix-regexp))))
  21428. ;; First ensure filling is correct in message-mode.
  21429. (let ((fill-paragraph-function
  21430. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21431. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21432. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21433. (paragraph-separate
  21434. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21435. (fill-paragraph nil))
  21436. (with-syntax-table org-mode-transpose-word-syntax-table
  21437. ;; Move to end of line in order to get the first paragraph
  21438. ;; within a plain list or a footnote definition.
  21439. (let ((element (save-excursion
  21440. (end-of-line)
  21441. (or (ignore-errors (org-element-at-point))
  21442. (user-error "An element cannot be parsed line %d"
  21443. (line-number-at-pos (point)))))))
  21444. ;; First check if point is in a blank line at the beginning of
  21445. ;; the buffer. In that case, ignore filling.
  21446. (case (org-element-type element)
  21447. ;; Use major mode filling function is src blocks.
  21448. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21449. ;; Align Org tables, leave table.el tables as-is.
  21450. (table-row (org-table-align) t)
  21451. (table
  21452. (when (eq (org-element-property :type element) 'org)
  21453. (save-excursion
  21454. (goto-char (org-element-property :post-affiliated element))
  21455. (org-table-align)))
  21456. t)
  21457. (paragraph
  21458. ;; Paragraphs may contain `line-break' type objects.
  21459. (let ((beg (max (point-min)
  21460. (org-element-property :contents-begin element)))
  21461. (end (min (point-max)
  21462. (org-element-property :contents-end element))))
  21463. ;; Do nothing if point is at an affiliated keyword.
  21464. (if (< (line-end-position) beg) t
  21465. (when (derived-mode-p 'message-mode)
  21466. ;; In `message-mode', do not fill following citation
  21467. ;; in current paragraph nor text before message body.
  21468. (let ((body-start (save-excursion (message-goto-body))))
  21469. (when body-start (setq beg (max body-start beg))))
  21470. (when (save-excursion
  21471. (re-search-forward
  21472. (concat "^" message-cite-prefix-regexp) end t))
  21473. (setq end (match-beginning 0))))
  21474. ;; Fill paragraph, taking line breaks into account.
  21475. (save-excursion
  21476. (goto-char beg)
  21477. (let ((cuts (list beg)))
  21478. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21479. (when (eq 'line-break
  21480. (org-element-type
  21481. (save-excursion (backward-char)
  21482. (org-element-context))))
  21483. (push (point) cuts)))
  21484. (dolist (c (delq end cuts))
  21485. (fill-region-as-paragraph c end justify)
  21486. (setq end c))))
  21487. t)))
  21488. ;; Contents of `comment-block' type elements should be
  21489. ;; filled as plain text, but only if point is within block
  21490. ;; markers.
  21491. (comment-block
  21492. (let* ((case-fold-search t)
  21493. (beg (save-excursion
  21494. (goto-char (org-element-property :begin element))
  21495. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21496. (forward-line)
  21497. (point)))
  21498. (end (save-excursion
  21499. (goto-char (org-element-property :end element))
  21500. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21501. (line-beginning-position))))
  21502. (if (or (< (point) beg) (> (point) end)) t
  21503. (fill-region-as-paragraph
  21504. (save-excursion (end-of-line)
  21505. (re-search-backward "^[ \t]*$" beg 'move)
  21506. (line-beginning-position))
  21507. (save-excursion (beginning-of-line)
  21508. (re-search-forward "^[ \t]*$" end 'move)
  21509. (line-beginning-position))
  21510. justify))))
  21511. ;; Fill comments.
  21512. (comment
  21513. (let ((begin (org-element-property :post-affiliated element))
  21514. (end (org-element-property :end element)))
  21515. (when (and (>= (point) begin) (<= (point) end))
  21516. (let ((begin (save-excursion
  21517. (end-of-line)
  21518. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21519. (progn (forward-line) (point))
  21520. begin)))
  21521. (end (save-excursion
  21522. (end-of-line)
  21523. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21524. (1- (line-beginning-position))
  21525. (skip-chars-backward " \r\t\n")
  21526. (line-end-position)))))
  21527. ;; Do not fill comments when at a blank line.
  21528. (when (> end begin)
  21529. (let ((fill-prefix
  21530. (save-excursion
  21531. (beginning-of-line)
  21532. (looking-at "[ \t]*#")
  21533. (let ((comment-prefix (match-string 0)))
  21534. (goto-char (match-end 0))
  21535. (if (looking-at adaptive-fill-regexp)
  21536. (concat comment-prefix (match-string 0))
  21537. (concat comment-prefix " "))))))
  21538. (save-excursion
  21539. (fill-region-as-paragraph begin end justify))))))
  21540. t))
  21541. ;; Ignore every other element.
  21542. (otherwise t))))))
  21543. (defun org-auto-fill-function ()
  21544. "Auto-fill function."
  21545. ;; Check if auto-filling is meaningful.
  21546. (let ((fc (current-fill-column)))
  21547. (when (and fc (> (current-column) fc))
  21548. (let* ((fill-prefix (org-adaptive-fill-function))
  21549. ;; Enforce empty fill prefix, if required. Otherwise, it
  21550. ;; will be computed again.
  21551. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21552. (when fill-prefix (do-auto-fill))))))
  21553. (defun org-comment-line-break-function (&optional soft)
  21554. "Break line at point and indent, continuing comment if within one.
  21555. The inserted newline is marked hard if variable
  21556. `use-hard-newlines' is true, unless optional argument SOFT is
  21557. non-nil."
  21558. (if soft (insert-and-inherit ?\n) (newline 1))
  21559. (save-excursion (forward-char -1) (delete-horizontal-space))
  21560. (delete-horizontal-space)
  21561. (indent-to-left-margin)
  21562. (insert-before-markers-and-inherit fill-prefix))
  21563. ;;; Fixed Width Areas
  21564. (defun org-toggle-fixed-width ()
  21565. "Toggle fixed-width markup.
  21566. Add or remove fixed-width markup on current line, whenever it
  21567. makes sense. Return an error otherwise.
  21568. If a region is active and if it contains only fixed-width areas
  21569. or blank lines, remove all fixed-width markup in it. If the
  21570. region contains anything else, convert all non-fixed-width lines
  21571. to fixed-width ones.
  21572. Blank lines at the end of the region are ignored unless the
  21573. region only contains such lines."
  21574. (interactive)
  21575. (if (not (org-region-active-p))
  21576. ;; No region:
  21577. ;;
  21578. ;; Remove fixed width marker only in a fixed-with element.
  21579. ;;
  21580. ;; Add fixed width maker in paragraphs, in blank lines after
  21581. ;; elements or at the beginning of a headline or an inlinetask,
  21582. ;; and before any one-line elements (e.g., a clock).
  21583. (progn
  21584. (beginning-of-line)
  21585. (let* ((element (org-element-at-point))
  21586. (type (org-element-type element)))
  21587. (cond
  21588. ((and (eq type 'fixed-width)
  21589. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21590. (replace-match
  21591. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21592. ((and (memq type '(babel-call clock comment diary-sexp headline
  21593. horizontal-rule keyword paragraph
  21594. planning))
  21595. (<= (org-element-property :post-affiliated element) (point)))
  21596. (skip-chars-forward " \t")
  21597. (insert ": "))
  21598. ((and (org-looking-at-p "[ \t]*$")
  21599. (or (eq type 'inlinetask)
  21600. (save-excursion
  21601. (skip-chars-forward " \r\t\n")
  21602. (<= (org-element-property :end element) (point)))))
  21603. (delete-region (point) (line-end-position))
  21604. (org-indent-line)
  21605. (insert ": "))
  21606. (t (user-error "Cannot insert a fixed-width line here")))))
  21607. ;; Region active.
  21608. (let* ((begin (save-excursion
  21609. (goto-char (region-beginning))
  21610. (line-beginning-position)))
  21611. (end (copy-marker
  21612. (save-excursion
  21613. (goto-char (region-end))
  21614. (unless (eolp) (beginning-of-line))
  21615. (if (save-excursion (re-search-backward "\\S-" begin t))
  21616. (progn (skip-chars-backward " \r\t\n") (point))
  21617. (point)))))
  21618. (all-fixed-width-p
  21619. (catch 'not-all-p
  21620. (save-excursion
  21621. (goto-char begin)
  21622. (skip-chars-forward " \r\t\n")
  21623. (when (eobp) (throw 'not-all-p nil))
  21624. (while (< (point) end)
  21625. (let ((element (org-element-at-point)))
  21626. (if (eq (org-element-type element) 'fixed-width)
  21627. (goto-char (org-element-property :end element))
  21628. (throw 'not-all-p nil))))
  21629. t))))
  21630. (if all-fixed-width-p
  21631. (save-excursion
  21632. (goto-char begin)
  21633. (while (< (point) end)
  21634. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21635. (replace-match
  21636. "" nil nil nil
  21637. (if (= (line-end-position) (match-end 0)) 0 1)))
  21638. (forward-line)))
  21639. (let ((min-ind (point-max)))
  21640. ;; Find minimum indentation across all lines.
  21641. (save-excursion
  21642. (goto-char begin)
  21643. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21644. (setq min-ind 0)
  21645. (catch 'zerop
  21646. (while (< (point) end)
  21647. (unless (org-looking-at-p "[ \t]*$")
  21648. (let ((ind (org-get-indentation)))
  21649. (setq min-ind (min min-ind ind))
  21650. (when (zerop ind) (throw 'zerop t))))
  21651. (forward-line)))))
  21652. ;; Loop over all lines and add fixed-width markup everywhere
  21653. ;; but in fixed-width lines.
  21654. (save-excursion
  21655. (goto-char begin)
  21656. (while (< (point) end)
  21657. (cond
  21658. ((org-at-heading-p)
  21659. (insert ": ")
  21660. (forward-line)
  21661. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21662. (insert ":")
  21663. (forward-line)))
  21664. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21665. (let* ((element (org-element-at-point))
  21666. (element-end (org-element-property :end element)))
  21667. (if (eq (org-element-type element) 'fixed-width)
  21668. (progn (goto-char element-end)
  21669. (skip-chars-backward " \r\t\n")
  21670. (forward-line))
  21671. (let ((limit (min end element-end)))
  21672. (while (< (point) limit)
  21673. (org-move-to-column min-ind t)
  21674. (insert ": ")
  21675. (forward-line))))))
  21676. (t
  21677. (org-move-to-column min-ind t)
  21678. (insert ": ")
  21679. (forward-line)))))))
  21680. (set-marker end nil))))
  21681. ;;; Comments
  21682. ;; Org comments syntax is quite complex. It requires the entire line
  21683. ;; to be just a comment. Also, even with the right syntax at the
  21684. ;; beginning of line, some some elements (i.e. verse-block or
  21685. ;; example-block) don't accept comments. Usual Emacs comment commands
  21686. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21687. ;; Org still relies on `comment-dwim', but cannot trust
  21688. ;; `comment-only-p'. So, `comment-region-function' and
  21689. ;; `uncomment-region-function' both point
  21690. ;; to`org-comment-or-uncomment-region'. Eventually,
  21691. ;; `org-insert-comment' takes care of insertion of comments at the
  21692. ;; beginning of line.
  21693. ;; `org-setup-comments-handling' install comments related variables
  21694. ;; during `org-mode' initialization.
  21695. (defun org-setup-comments-handling ()
  21696. (interactive)
  21697. (org-set-local 'comment-use-syntax nil)
  21698. (org-set-local 'comment-start "# ")
  21699. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21700. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21701. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21702. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21703. (defun org-insert-comment ()
  21704. "Insert an empty comment above current line.
  21705. If the line is empty, insert comment at its beginning. When
  21706. point is within a source block, comment according to the related
  21707. major mode."
  21708. (if (let ((element (org-element-at-point)))
  21709. (and (eq (org-element-type element) 'src-block)
  21710. (< (save-excursion
  21711. (goto-char (org-element-property :post-affiliated element))
  21712. (line-end-position))
  21713. (point))
  21714. (> (save-excursion
  21715. (goto-char (org-element-property :end element))
  21716. (skip-chars-backward " \r\t\n")
  21717. (line-beginning-position))
  21718. (point))))
  21719. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21720. (beginning-of-line)
  21721. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21722. (open-line 1))
  21723. (org-indent-line)
  21724. (insert "# ")))
  21725. (defvar comment-empty-lines) ; From newcomment.el.
  21726. (defun org-comment-or-uncomment-region (beg end &rest _)
  21727. "Comment or uncomment each non-blank line in the region.
  21728. Uncomment each non-blank line between BEG and END if it only
  21729. contains commented lines. Otherwise, comment them. If region is
  21730. strictly within a source block, use appropriate comment syntax."
  21731. (if (let ((element (org-element-at-point)))
  21732. (and (eq (org-element-type element) 'src-block)
  21733. (< (save-excursion
  21734. (goto-char (org-element-property :post-affiliated element))
  21735. (line-end-position))
  21736. beg)
  21737. (>= (save-excursion
  21738. (goto-char (org-element-property :end element))
  21739. (skip-chars-backward " \r\t\n")
  21740. (line-beginning-position))
  21741. end)))
  21742. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21743. (save-restriction
  21744. ;; Restrict region
  21745. (narrow-to-region (save-excursion (goto-char beg)
  21746. (skip-chars-forward " \r\t\n" end)
  21747. (line-beginning-position))
  21748. (save-excursion (goto-char end)
  21749. (skip-chars-backward " \r\t\n" beg)
  21750. (line-end-position)))
  21751. (let ((uncommentp
  21752. ;; UNCOMMENTP is non-nil when every non blank line between
  21753. ;; BEG and END is a comment.
  21754. (save-excursion
  21755. (goto-char (point-min))
  21756. (while (and (not (eobp))
  21757. (let ((element (org-element-at-point)))
  21758. (and (eq (org-element-type element) 'comment)
  21759. (goto-char (min (point-max)
  21760. (org-element-property
  21761. :end element)))))))
  21762. (eobp))))
  21763. (if uncommentp
  21764. ;; Only blank lines and comments in region: uncomment it.
  21765. (save-excursion
  21766. (goto-char (point-min))
  21767. (while (not (eobp))
  21768. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21769. (replace-match "" nil nil nil 1))
  21770. (forward-line)))
  21771. ;; Comment each line in region.
  21772. (let ((min-indent (point-max)))
  21773. ;; First find the minimum indentation across all lines.
  21774. (save-excursion
  21775. (goto-char (point-min))
  21776. (while (and (not (eobp)) (not (zerop min-indent)))
  21777. (unless (looking-at "[ \t]*$")
  21778. (setq min-indent (min min-indent (current-indentation))))
  21779. (forward-line)))
  21780. ;; Then loop over all lines.
  21781. (save-excursion
  21782. (goto-char (point-min))
  21783. (while (not (eobp))
  21784. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21785. ;; Don't get fooled by invisible text (e.g. link path)
  21786. ;; when moving to column MIN-INDENT.
  21787. (let ((buffer-invisibility-spec nil))
  21788. (org-move-to-column min-indent t))
  21789. (insert comment-start))
  21790. (forward-line)))))))))
  21791. (defun org-comment-dwim (arg)
  21792. "Call `comment-dwim' within a source edit buffer if needed."
  21793. (interactive "*P")
  21794. (if (org-in-src-block-p)
  21795. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21796. (call-interactively 'comment-dwim)))
  21797. ;;; Timestamps API
  21798. ;; This section contains tools to operate on timestamp objects, as
  21799. ;; returned by, e.g. `org-element-context'.
  21800. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21801. "Encode TIMESTAMP object into Emacs internal time.
  21802. Use end of date range or time range when END is non-nil."
  21803. (apply #'encode-time
  21804. (cons 0
  21805. (mapcar
  21806. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21807. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21808. '(:minute-start :hour-start :day-start :month-start
  21809. :year-start))))))
  21810. (defun org-timestamp-has-time-p (timestamp)
  21811. "Non-nil when TIMESTAMP has a time specified."
  21812. (org-element-property :hour-start timestamp))
  21813. (defun org-timestamp-format (timestamp format &optional end utc)
  21814. "Format a TIMESTAMP object into a string.
  21815. FORMAT is a format specifier to be passed to
  21816. `format-time-string'.
  21817. When optional argument END is non-nil, use end of date-range or
  21818. time-range, if possible.
  21819. When optional argument UTC is non-nil, time will be expressed as
  21820. Universal Time."
  21821. (format-time-string
  21822. format (org-timestamp--to-internal-time timestamp end) utc))
  21823. (defun org-timestamp-split-range (timestamp &optional end)
  21824. "Extract a TIMESTAMP object from a date or time range.
  21825. END, when non-nil, means extract the end of the range.
  21826. Otherwise, extract its start.
  21827. Return a new timestamp object."
  21828. (let ((type (org-element-property :type timestamp)))
  21829. (if (memq type '(active inactive diary)) timestamp
  21830. (let ((split-ts (org-element-copy timestamp)))
  21831. ;; Set new type.
  21832. (org-element-put-property
  21833. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21834. ;; Copy start properties over end properties if END is
  21835. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21836. (let ((p-alist '((:minute-start . :minute-end)
  21837. (:hour-start . :hour-end)
  21838. (:day-start . :day-end)
  21839. (:month-start . :month-end)
  21840. (:year-start . :year-end))))
  21841. (dolist (p-cell p-alist)
  21842. (org-element-put-property
  21843. split-ts
  21844. (funcall (if end #'car #'cdr) p-cell)
  21845. (org-element-property
  21846. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21847. ;; Eventually refresh `:raw-value'.
  21848. (org-element-put-property split-ts :raw-value nil)
  21849. (org-element-put-property
  21850. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21851. (defun org-timestamp-translate (timestamp &optional boundary)
  21852. "Translate TIMESTAMP object to custom format.
  21853. Format string is defined in `org-time-stamp-custom-formats',
  21854. which see.
  21855. When optional argument BOUNDARY is non-nil, it is either the
  21856. symbol `start' or `end'. In this case, only translate the
  21857. starting or ending part of TIMESTAMP if it is a date or time
  21858. range. Otherwise, translate both parts.
  21859. Return timestamp as-is if `org-display-custom-times' is nil or if
  21860. it has a `diary' type."
  21861. (let ((type (org-element-property :type timestamp)))
  21862. (if (or (not org-display-custom-times) (eq type 'diary))
  21863. (org-element-interpret-data timestamp)
  21864. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21865. org-time-stamp-custom-formats)))
  21866. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21867. (concat (org-timestamp-format timestamp fmt)
  21868. "--"
  21869. (org-timestamp-format timestamp fmt t))
  21870. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21871. ;;; Other stuff.
  21872. (defvar reftex-docstruct-symbol)
  21873. (defvar reftex-cite-format)
  21874. (defvar org--rds)
  21875. (defun org-reftex-citation ()
  21876. "Use reftex-citation to insert a citation into the buffer.
  21877. This looks for a line like
  21878. #+BIBLIOGRAPHY: foo plain option:-d
  21879. and derives from it that foo.bib is the bibliography file relevant
  21880. for this document. It then installs the necessary environment for RefTeX
  21881. to work in this buffer and calls `reftex-citation' to insert a citation
  21882. into the buffer.
  21883. Export of such citations to both LaTeX and HTML is handled by the contributed
  21884. package ox-bibtex by Taru Karttunen."
  21885. (interactive)
  21886. (let ((reftex-docstruct-symbol 'org--rds)
  21887. (reftex-cite-format "\\cite{%l}")
  21888. org--rds bib)
  21889. (save-excursion
  21890. (save-restriction
  21891. (widen)
  21892. (let ((case-fold-search t)
  21893. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21894. (if (not (save-excursion
  21895. (or (re-search-forward re nil t)
  21896. (re-search-backward re nil t))))
  21897. (user-error "No bibliography defined in file")
  21898. (setq bib (concat (match-string 1) ".bib")
  21899. org--rds (list (list 'bib bib)))))))
  21900. (call-interactively 'reftex-citation)))
  21901. ;;;; Functions extending outline functionality
  21902. (defun org-beginning-of-line (&optional arg)
  21903. "Go to the beginning of the current line. If that is invisible, continue
  21904. to a visible line beginning. This makes the function of C-a more intuitive.
  21905. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21906. first attempt, and only move to after the tags when the cursor is already
  21907. beyond the end of the headline."
  21908. (interactive "P")
  21909. (let ((pos (point))
  21910. (special (if (consp org-special-ctrl-a/e)
  21911. (car org-special-ctrl-a/e)
  21912. org-special-ctrl-a/e))
  21913. deactivate-mark refpos)
  21914. (if (org-bound-and-true-p visual-line-mode)
  21915. (beginning-of-visual-line 1)
  21916. (beginning-of-line 1))
  21917. (if (and arg (fboundp 'move-beginning-of-line))
  21918. (call-interactively 'move-beginning-of-line)
  21919. (if (bobp)
  21920. nil
  21921. (backward-char 1)
  21922. (if (org-truely-invisible-p)
  21923. (while (and (not (bobp)) (org-truely-invisible-p))
  21924. (backward-char 1)
  21925. (beginning-of-line 1))
  21926. (forward-char 1))))
  21927. (when special
  21928. (cond
  21929. ((and (looking-at org-complex-heading-regexp)
  21930. (eq (char-after (match-end 1)) ?\s))
  21931. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21932. (point-at-eol)))
  21933. (goto-char
  21934. (if (eq special t)
  21935. (cond ((> pos refpos) refpos)
  21936. ((= pos (point)) refpos)
  21937. (t (point)))
  21938. (cond ((> pos (point)) (point))
  21939. ((not (eq last-command this-command)) (point))
  21940. (t refpos)))))
  21941. ((org-at-item-p)
  21942. ;; Being at an item and not looking at an the item means point
  21943. ;; was previously moved to beginning of a visual line, which
  21944. ;; doesn't contain the item. Therefore, do nothing special,
  21945. ;; just stay here.
  21946. (when (looking-at org-list-full-item-re)
  21947. ;; Set special position at first white space character after
  21948. ;; bullet, and check-box, if any.
  21949. (let ((after-bullet
  21950. (let ((box (match-end 3)))
  21951. (if (not box) (match-end 1)
  21952. (let ((after (char-after box)))
  21953. (if (and after (= after ? )) (1+ box) box))))))
  21954. ;; Special case: Move point to special position when
  21955. ;; currently after it or at beginning of line.
  21956. (if (eq special t)
  21957. (when (or (> pos after-bullet) (= (point) pos))
  21958. (goto-char after-bullet))
  21959. ;; Reversed case: Move point to special position when
  21960. ;; point was already at beginning of line and command is
  21961. ;; repeated.
  21962. (when (and (= (point) pos) (eq last-command this-command))
  21963. (goto-char after-bullet))))))))
  21964. (org-no-warnings
  21965. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21966. (setq disable-point-adjustment
  21967. (or (not (invisible-p (point)))
  21968. (not (invisible-p (max (point-min) (1- (point))))))))
  21969. (defun org-end-of-line (&optional arg)
  21970. "Go to the end of the line.
  21971. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21972. tags on the first attempt, and only move to after the tags when
  21973. the cursor is already beyond the end of the headline."
  21974. (interactive "P")
  21975. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21976. org-special-ctrl-a/e))
  21977. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21978. 'end-of-visual-line)
  21979. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21980. (t 'end-of-line)))
  21981. deactivate-mark)
  21982. (if (or (not special) arg) (call-interactively move-fun)
  21983. (let* ((element (save-excursion (beginning-of-line)
  21984. (org-element-at-point)))
  21985. (type (org-element-type element)))
  21986. (cond
  21987. ((memq type '(headline inlinetask))
  21988. (let ((pos (point)))
  21989. (beginning-of-line 1)
  21990. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21991. (if (eq special t)
  21992. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21993. (goto-char (match-beginning 1))
  21994. (goto-char (match-end 0)))
  21995. (if (or (< pos (match-end 0))
  21996. (not (eq this-command last-command)))
  21997. (goto-char (match-end 0))
  21998. (goto-char (match-beginning 1))))
  21999. (call-interactively move-fun))))
  22000. ((outline-invisible-p (line-end-position))
  22001. ;; If element is hidden, `move-end-of-line' would put point
  22002. ;; after it. Use `end-of-line' to stay on current line.
  22003. (call-interactively 'end-of-line))
  22004. (t (call-interactively move-fun)))))
  22005. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22006. (setq disable-point-adjustment
  22007. (or (not (invisible-p (point)))
  22008. (not (invisible-p (max (point-min) (1- (point))))))))
  22009. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22010. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22011. (defun org-backward-sentence (&optional _arg)
  22012. "Go to beginning of sentence, or beginning of table field.
  22013. This will call `backward-sentence' or `org-table-beginning-of-field',
  22014. depending on context."
  22015. (interactive)
  22016. (let* ((element (org-element-at-point))
  22017. (contents-begin (org-element-property :contents-begin element))
  22018. (table (org-element-lineage element '(table) t)))
  22019. (if (and table
  22020. (> (point) contents-begin)
  22021. (<= (point) (org-element-property :contents-end table)))
  22022. (call-interactively #'org-table-beginning-of-field)
  22023. (save-restriction
  22024. (when (and contents-begin
  22025. (< (point-min) contents-begin)
  22026. (> (point) contents-begin))
  22027. (narrow-to-region contents-begin
  22028. (org-element-property :contents-end element)))
  22029. (call-interactively #'backward-sentence)))))
  22030. (defun org-forward-sentence (&optional _arg)
  22031. "Go to end of sentence, or end of table field.
  22032. This will call `forward-sentence' or `org-table-end-of-field',
  22033. depending on context."
  22034. (interactive)
  22035. (let* ((element (org-element-at-point))
  22036. (contents-end (org-element-property :contents-end element))
  22037. (table (org-element-lineage element '(table) t)))
  22038. (if (and table
  22039. (>= (point) (org-element-property :contents-begin table))
  22040. (< (point) contents-end))
  22041. (call-interactively #'org-table-end-of-field)
  22042. (save-restriction
  22043. (when (and contents-end
  22044. (> (point-max) contents-end)
  22045. ;; Skip blank lines between elements.
  22046. (< (org-element-property :end element)
  22047. (save-excursion (goto-char contents-end)
  22048. (skip-chars-forward " \r\t\n"))))
  22049. (narrow-to-region (org-element-property :contents-begin element)
  22050. contents-end))
  22051. (call-interactively #'forward-sentence)))))
  22052. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22053. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22054. (defun org-kill-line (&optional _arg)
  22055. "Kill line, to tags or end of line."
  22056. (interactive)
  22057. (cond
  22058. ((or (not org-special-ctrl-k)
  22059. (bolp)
  22060. (not (org-at-heading-p)))
  22061. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22062. org-ctrl-k-protect-subtree)
  22063. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22064. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22065. (user-error "C-k aborted as it would kill a hidden subtree")))
  22066. (call-interactively
  22067. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22068. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22069. (kill-region (point) (match-beginning 1))
  22070. (org-set-tags nil t))
  22071. (t (kill-region (point) (point-at-eol)))))
  22072. (define-key org-mode-map "\C-k" 'org-kill-line)
  22073. (defun org-yank (&optional arg)
  22074. "Yank. If the kill is a subtree, treat it specially.
  22075. This command will look at the current kill and check if is a single
  22076. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22077. cursor is at the beginning of a line or after the stars of a currently
  22078. empty headline, then the yank is handled specially. How exactly depends
  22079. on the value of the following variables, both set by default.
  22080. `org-yank-folded-subtrees'
  22081. When set, the subtree(s) will be folded after insertion, but only
  22082. if doing so would now swallow text after the yanked text.
  22083. `org-yank-adjusted-subtrees'
  22084. When set, the subtree will be promoted or demoted in order to
  22085. fit into the local outline tree structure, which means that the
  22086. level will be adjusted so that it becomes the smaller one of the
  22087. two *visible* surrounding headings.
  22088. Any prefix to this command will cause `yank' to be called directly with
  22089. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22090. will just
  22091. plainly yank the text as it is.
  22092. \[1] The test checks if the first non-white line is a heading
  22093. and if there are no other headings with fewer stars."
  22094. (interactive "P")
  22095. (org-yank-generic 'yank arg))
  22096. (defun org-yank-generic (command arg)
  22097. "Perform some yank-like command.
  22098. This function implements the behavior described in the `org-yank'
  22099. documentation. However, it has been generalized to work for any
  22100. interactive command with similar behavior."
  22101. ;; pretend to be command COMMAND
  22102. (setq this-command command)
  22103. (if arg
  22104. (call-interactively command)
  22105. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22106. (and (org-kill-is-subtree-p)
  22107. (or (bolp)
  22108. (and (looking-at "[ \t]*$")
  22109. (string-match
  22110. "\\`\\*+\\'"
  22111. (buffer-substring (point-at-bol) (point)))))))
  22112. swallowp)
  22113. (cond
  22114. ((and subtreep org-yank-folded-subtrees)
  22115. (let ((beg (point))
  22116. end)
  22117. (if (and subtreep org-yank-adjusted-subtrees)
  22118. (org-paste-subtree nil nil 'for-yank)
  22119. (call-interactively command))
  22120. (setq end (point))
  22121. (goto-char beg)
  22122. (when (and (bolp) subtreep
  22123. (not (setq swallowp
  22124. (org-yank-folding-would-swallow-text beg end))))
  22125. (org-with-limited-levels
  22126. (or (looking-at org-outline-regexp)
  22127. (re-search-forward org-outline-regexp-bol end t))
  22128. (while (and (< (point) end) (looking-at org-outline-regexp))
  22129. (hide-subtree)
  22130. (org-cycle-show-empty-lines 'folded)
  22131. (condition-case nil
  22132. (outline-forward-same-level 1)
  22133. (error (goto-char end))))))
  22134. (when swallowp
  22135. (message
  22136. "Inserted text not folded because that would swallow text"))
  22137. (goto-char end)
  22138. (skip-chars-forward " \t\n\r")
  22139. (beginning-of-line 1)
  22140. (push-mark beg 'nomsg)))
  22141. ((and subtreep org-yank-adjusted-subtrees)
  22142. (let ((beg (point-at-bol)))
  22143. (org-paste-subtree nil nil 'for-yank)
  22144. (push-mark beg 'nomsg)))
  22145. (t
  22146. (call-interactively command))))))
  22147. (defun org-yank-folding-would-swallow-text (beg end)
  22148. "Would hide-subtree at BEG swallow any text after END?"
  22149. (let (level)
  22150. (org-with-limited-levels
  22151. (save-excursion
  22152. (goto-char beg)
  22153. (when (or (looking-at org-outline-regexp)
  22154. (re-search-forward org-outline-regexp-bol end t))
  22155. (setq level (org-outline-level)))
  22156. (goto-char end)
  22157. (skip-chars-forward " \t\r\n\v\f")
  22158. (if (or (eobp)
  22159. (and (bolp) (looking-at org-outline-regexp)
  22160. (<= (org-outline-level) level)))
  22161. nil ; Nothing would be swallowed
  22162. t))))) ; something would swallow
  22163. (define-key org-mode-map "\C-y" 'org-yank)
  22164. (defun org-truely-invisible-p ()
  22165. "Check if point is at a character currently not visible.
  22166. This version does not only check the character property, but also
  22167. `visible-mode'."
  22168. ;; Early versions of noutline don't have `outline-invisible-p'.
  22169. (if (org-bound-and-true-p visible-mode)
  22170. nil
  22171. (outline-invisible-p)))
  22172. (defun org-invisible-p2 ()
  22173. "Check if point is at a character currently not visible."
  22174. (save-excursion
  22175. (if (and (eolp) (not (bobp))) (backward-char 1))
  22176. ;; Early versions of noutline don't have `outline-invisible-p'.
  22177. (outline-invisible-p)))
  22178. (defun org-back-to-heading (&optional invisible-ok)
  22179. "Call `outline-back-to-heading', but provide a better error message."
  22180. (condition-case nil
  22181. (outline-back-to-heading invisible-ok)
  22182. (error (error "Before first headline at position %d in buffer %s"
  22183. (point) (current-buffer)))))
  22184. (defun org-before-first-heading-p ()
  22185. "Before first heading?"
  22186. (save-excursion
  22187. (end-of-line)
  22188. (null (re-search-backward org-outline-regexp-bol nil t))))
  22189. (defun org-at-heading-p (&optional ignored)
  22190. (outline-on-heading-p t))
  22191. ;; Compatibility alias with Org versions < 7.8.03
  22192. (defalias 'org-on-heading-p 'org-at-heading-p)
  22193. (defun org-in-commented-heading-p (&optional no-inheritance)
  22194. "Non-nil if point is under a commented heading.
  22195. This function also checks ancestors of the current headline,
  22196. unless optional argument NO-INHERITANCE is non-nil."
  22197. (cond
  22198. ((org-before-first-heading-p) nil)
  22199. ((let ((headline (nth 4 (org-heading-components))))
  22200. (and headline
  22201. (let ((case-fold-search nil))
  22202. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22203. headline)))))
  22204. (no-inheritance nil)
  22205. (t
  22206. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22207. (defun org-at-comment-p nil
  22208. "Is cursor in a commented line?"
  22209. (save-excursion
  22210. (save-match-data
  22211. (beginning-of-line)
  22212. (looking-at "^[ \t]*# "))))
  22213. (defun org-at-drawer-p nil
  22214. "Is cursor at a drawer keyword?"
  22215. (save-excursion
  22216. (move-beginning-of-line 1)
  22217. (looking-at org-drawer-regexp)))
  22218. (defun org-at-block-p nil
  22219. "Is cursor at a block keyword?"
  22220. (save-excursion
  22221. (move-beginning-of-line 1)
  22222. (looking-at org-block-regexp)))
  22223. (defun org-point-at-end-of-empty-headline ()
  22224. "If point is at the end of an empty headline, return t, else nil.
  22225. If the heading only contains a TODO keyword, it is still still considered
  22226. empty."
  22227. (and (looking-at "[ \t]*$")
  22228. (when org-todo-line-regexp
  22229. (save-excursion
  22230. (beginning-of-line 1)
  22231. (let ((case-fold-search nil))
  22232. (looking-at org-todo-line-regexp)
  22233. (string= (match-string 3) ""))))))
  22234. (defun org-at-heading-or-item-p ()
  22235. (or (org-at-heading-p) (org-at-item-p)))
  22236. (defun org-at-target-p ()
  22237. (or (org-in-regexp org-radio-target-regexp)
  22238. (org-in-regexp org-target-regexp)))
  22239. ;; Compatibility alias with Org versions < 7.8.03
  22240. (defalias 'org-on-target-p 'org-at-target-p)
  22241. (defun org-up-heading-all (arg)
  22242. "Move to the heading line of which the present line is a subheading.
  22243. This function considers both visible and invisible heading lines.
  22244. With argument, move up ARG levels."
  22245. (if (fboundp 'outline-up-heading-all)
  22246. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22247. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22248. (defun org-up-heading-safe ()
  22249. "Move to the heading line of which the present line is a subheading.
  22250. This version will not throw an error. It will return the level of the
  22251. headline found, or nil if no higher level is found.
  22252. Also, this function will be a lot faster than `outline-up-heading',
  22253. because it relies on stars being the outline starters. This can really
  22254. make a significant difference in outlines with very many siblings."
  22255. (when (ignore-errors (org-back-to-heading t))
  22256. (let ((level-up (1- (funcall outline-level))))
  22257. (and (> level-up 0)
  22258. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22259. (funcall outline-level)))))
  22260. (defun org-first-sibling-p ()
  22261. "Is this heading the first child of its parents?"
  22262. (interactive)
  22263. (let ((re org-outline-regexp-bol)
  22264. level l)
  22265. (unless (org-at-heading-p t)
  22266. (user-error "Not at a heading"))
  22267. (setq level (funcall outline-level))
  22268. (save-excursion
  22269. (if (not (re-search-backward re nil t))
  22270. t
  22271. (setq l (funcall outline-level))
  22272. (< l level)))))
  22273. (defun org-goto-sibling (&optional previous)
  22274. "Goto the next sibling, even if it is invisible.
  22275. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22276. when a sibling was found. When none is found, return nil and don't
  22277. move point."
  22278. (let ((fun (if previous 're-search-backward 're-search-forward))
  22279. (pos (point))
  22280. (re org-outline-regexp-bol)
  22281. level l)
  22282. (when (ignore-errors (org-back-to-heading t))
  22283. (setq level (funcall outline-level))
  22284. (catch 'exit
  22285. (or previous (forward-char 1))
  22286. (while (funcall fun re nil t)
  22287. (setq l (funcall outline-level))
  22288. (when (< l level) (goto-char pos) (throw 'exit nil))
  22289. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22290. (goto-char pos)
  22291. nil))))
  22292. (defun org-show-siblings ()
  22293. "Show all siblings of the current headline."
  22294. (save-excursion
  22295. (while (org-goto-sibling) (org-flag-heading nil)))
  22296. (save-excursion
  22297. (while (org-goto-sibling 'previous)
  22298. (org-flag-heading nil))))
  22299. (defun org-goto-first-child ()
  22300. "Goto the first child, even if it is invisible.
  22301. Return t when a child was found. Otherwise don't move point and
  22302. return nil."
  22303. (let (level (pos (point)) (re org-outline-regexp-bol))
  22304. (when (ignore-errors (org-back-to-heading t))
  22305. (setq level (outline-level))
  22306. (forward-char 1)
  22307. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22308. (progn (goto-char (match-beginning 0)) t)
  22309. (goto-char pos) nil))))
  22310. (defun org-show-hidden-entry ()
  22311. "Show an entry where even the heading is hidden."
  22312. (save-excursion
  22313. (org-show-entry)))
  22314. (defun org-flag-heading (flag &optional entry)
  22315. "Flag the current heading. FLAG non-nil means make invisible.
  22316. When ENTRY is non-nil, show the entire entry."
  22317. (save-excursion
  22318. (org-back-to-heading t)
  22319. ;; Check if we should show the entire entry
  22320. (if entry
  22321. (progn
  22322. (org-show-entry)
  22323. (save-excursion
  22324. (and (outline-next-heading)
  22325. (org-flag-heading nil))))
  22326. (outline-flag-region (max (point-min) (1- (point)))
  22327. (save-excursion (outline-end-of-heading) (point))
  22328. flag))))
  22329. (defun org-get-next-sibling ()
  22330. "Move to next heading of the same level, and return point.
  22331. If there is no such heading, return nil.
  22332. This is like outline-next-sibling, but invisible headings are ok."
  22333. (let ((level (funcall outline-level)))
  22334. (outline-next-heading)
  22335. (while (and (not (eobp)) (> (funcall outline-level) level))
  22336. (outline-next-heading))
  22337. (if (or (eobp) (< (funcall outline-level) level))
  22338. nil
  22339. (point))))
  22340. (defun org-get-last-sibling ()
  22341. "Move to previous heading of the same level, and return point.
  22342. If there is no such heading, return nil."
  22343. (let ((opoint (point))
  22344. (level (funcall outline-level)))
  22345. (outline-previous-heading)
  22346. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22347. (while (and (> (funcall outline-level) level)
  22348. (not (bobp)))
  22349. (outline-previous-heading))
  22350. (if (< (funcall outline-level) level)
  22351. nil
  22352. (point)))))
  22353. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22354. "Goto to the end of a subtree."
  22355. ;; This contains an exact copy of the original function, but it uses
  22356. ;; `org-back-to-heading', to make it work also in invisible
  22357. ;; trees. And is uses an invisible-ok argument.
  22358. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22359. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22360. ;; with many children and grandchildren etc, this can be much faster
  22361. ;; than the outline version.
  22362. (org-back-to-heading invisible-ok)
  22363. (let ((first t)
  22364. (level (funcall outline-level)))
  22365. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22366. ;; A true heading (not a plain list item), in Org-mode
  22367. ;; This means we can easily find the end by looking
  22368. ;; only for the right number of stars. Using a regexp to do
  22369. ;; this is so much faster than using a Lisp loop.
  22370. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22371. (forward-char 1)
  22372. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22373. ;; something else, do it the slow way
  22374. (while (and (not (eobp))
  22375. (or first (> (funcall outline-level) level)))
  22376. (setq first nil)
  22377. (outline-next-heading)))
  22378. (unless to-heading
  22379. (if (memq (preceding-char) '(?\n ?\^M))
  22380. (progn
  22381. ;; Go to end of line before heading
  22382. (forward-char -1)
  22383. (if (memq (preceding-char) '(?\n ?\^M))
  22384. ;; leave blank line before heading
  22385. (forward-char -1))))))
  22386. (point))
  22387. (defun org-end-of-meta-data (&optional full)
  22388. "Skip planning line and properties drawer in current entry.
  22389. When optional argument FULL is non-nil, also skip empty lines,
  22390. clocking lines and regular drawers at the beginning of the
  22391. entry."
  22392. (org-back-to-heading t)
  22393. (forward-line)
  22394. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22395. (when (looking-at org-property-drawer-re)
  22396. (goto-char (match-end 0))
  22397. (forward-line))
  22398. (when (and full (not (org-at-heading-p)))
  22399. (catch 'exit
  22400. (let ((end (save-excursion (outline-next-heading) (point)))
  22401. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22402. (while (not (eobp))
  22403. (cond ((org-looking-at-p org-drawer-regexp)
  22404. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22405. (forward-line)
  22406. (throw 'exit t)))
  22407. ((org-looking-at-p re) (forward-line))
  22408. (t (throw 'exit t))))))))
  22409. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22410. "Move forward to the ARG'th subheading at same level as this one.
  22411. Stop at the first and last subheadings of a superior heading.
  22412. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22413. non-nil it will also look at invisible ones."
  22414. (interactive "p")
  22415. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22416. (if (and arg (< arg 0))
  22417. (goto-char (point-min))
  22418. (outline-next-heading))
  22419. (org-at-heading-p)
  22420. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22421. (f (if (and arg (< arg 0))
  22422. 're-search-backward
  22423. 're-search-forward))
  22424. (count (if arg (abs arg) 1))
  22425. (result (point)))
  22426. (while (and (prog1 (> count 0)
  22427. (forward-char (if (and arg (< arg 0)) -1 1)))
  22428. (funcall f org-outline-regexp-bol nil 'move))
  22429. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22430. (cond ((< l level) (setq count 0))
  22431. ((and (= l level)
  22432. (or invisible-ok
  22433. (progn
  22434. (goto-char (line-beginning-position))
  22435. (not (outline-invisible-p)))))
  22436. (setq count (1- count))
  22437. (when (eq l level)
  22438. (setq result (point)))))))
  22439. (goto-char result))
  22440. (beginning-of-line 1)))
  22441. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22442. "Move backward to the ARG'th subheading at same level as this one.
  22443. Stop at the first and last subheadings of a superior heading."
  22444. (interactive "p")
  22445. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22446. (defun org-next-visible-heading (arg)
  22447. "Move to the next visible heading.
  22448. This function wraps `outline-next-visible-heading' with
  22449. `org-with-limited-levels' in order to skip over inline tasks and
  22450. respect customization of `org-odd-levels-only'."
  22451. (interactive "p")
  22452. (org-with-limited-levels
  22453. (outline-next-visible-heading arg)))
  22454. (defun org-previous-visible-heading (arg)
  22455. "Move to the next visible heading.
  22456. This function wraps `outline-previous-visible-heading' with
  22457. `org-with-limited-levels' in order to skip over inline tasks and
  22458. respect customization of `org-odd-levels-only'."
  22459. (interactive "p")
  22460. (org-with-limited-levels
  22461. (outline-previous-visible-heading arg)))
  22462. (defun org-next-block (arg &optional backward block-regexp)
  22463. "Jump to the next block.
  22464. With a prefix argument ARG, jump forward ARG many blocks.
  22465. When BACKWARD is non-nil, jump to the previous block.
  22466. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22467. Match data is set according to this regexp when the function
  22468. returns.
  22469. Return point at beginning of the opening line of found block.
  22470. Throw an error if no block is found."
  22471. (interactive "p")
  22472. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22473. (case-fold-search t)
  22474. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22475. (count (or arg 1))
  22476. (origin (point))
  22477. last-element)
  22478. (if backward (beginning-of-line) (end-of-line))
  22479. (while (and (> count 0) (funcall search-fn re nil t))
  22480. (let ((element (save-excursion
  22481. (goto-char (match-beginning 0))
  22482. (save-match-data (org-element-at-point)))))
  22483. (when (and (memq (org-element-type element)
  22484. '(center-block comment-block dynamic-block
  22485. example-block export-block quote-block
  22486. special-block src-block verse-block))
  22487. (<= (match-beginning 0)
  22488. (org-element-property :post-affiliated element)))
  22489. (setq last-element element)
  22490. (decf count))))
  22491. (if (= count 0)
  22492. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22493. (save-match-data (org-show-context)))
  22494. (goto-char origin)
  22495. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22496. (defun org-previous-block (arg &optional block-regexp)
  22497. "Jump to the previous block.
  22498. With a prefix argument ARG, jump backward ARG many source blocks.
  22499. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22500. (interactive "p")
  22501. (org-next-block arg t block-regexp))
  22502. (defun org-forward-paragraph ()
  22503. "Move forward to beginning of next paragraph or equivalent.
  22504. The function moves point to the beginning of the next visible
  22505. structural element, which can be a paragraph, a table, a list
  22506. item, etc. It also provides some special moves for convenience:
  22507. - On an affiliated keyword, jump to the beginning of the
  22508. relative element.
  22509. - On an item or a footnote definition, move to the second
  22510. element inside, if any.
  22511. - On a table or a property drawer, jump after it.
  22512. - On a verse or source block, stop after blank lines."
  22513. (interactive)
  22514. (when (eobp) (user-error "Cannot move further down"))
  22515. (let* ((deactivate-mark nil)
  22516. (element (org-element-at-point))
  22517. (type (org-element-type element))
  22518. (post-affiliated (org-element-property :post-affiliated element))
  22519. (contents-begin (org-element-property :contents-begin element))
  22520. (contents-end (org-element-property :contents-end element))
  22521. (end (let ((end (org-element-property :end element)) (parent element))
  22522. (while (and (setq parent (org-element-property :parent parent))
  22523. (= (org-element-property :contents-end parent) end))
  22524. (setq end (org-element-property :end parent)))
  22525. end)))
  22526. (cond ((not element)
  22527. (skip-chars-forward " \r\t\n")
  22528. (or (eobp) (beginning-of-line)))
  22529. ;; On affiliated keywords, move to element's beginning.
  22530. ((< (point) post-affiliated)
  22531. (goto-char post-affiliated))
  22532. ;; At a table row, move to the end of the table. Similarly,
  22533. ;; at a node property, move to the end of the property
  22534. ;; drawer.
  22535. ((memq type '(node-property table-row))
  22536. (goto-char (org-element-property
  22537. :end (org-element-property :parent element))))
  22538. ((memq type '(property-drawer table)) (goto-char end))
  22539. ;; Consider blank lines as separators in verse and source
  22540. ;; blocks to ease editing.
  22541. ((memq type '(src-block verse-block))
  22542. (when (eq type 'src-block)
  22543. (setq contents-end
  22544. (save-excursion (goto-char end)
  22545. (skip-chars-backward " \r\t\n")
  22546. (line-beginning-position))))
  22547. (beginning-of-line)
  22548. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22549. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22550. (goto-char end)
  22551. (skip-chars-forward " \r\t\n")
  22552. (if (= (point) contents-end) (goto-char end)
  22553. (beginning-of-line))))
  22554. ;; With no contents, just skip element.
  22555. ((not contents-begin) (goto-char end))
  22556. ;; If contents are invisible, skip the element altogether.
  22557. ((outline-invisible-p (line-end-position))
  22558. (case type
  22559. (headline
  22560. (org-with-limited-levels (outline-next-visible-heading 1)))
  22561. ;; At a plain list, make sure we move to the next item
  22562. ;; instead of skipping the whole list.
  22563. (plain-list (forward-char)
  22564. (org-forward-paragraph))
  22565. (otherwise (goto-char end))))
  22566. ((>= (point) contents-end) (goto-char end))
  22567. ((>= (point) contents-begin)
  22568. ;; This can only happen on paragraphs and plain lists.
  22569. (case type
  22570. (paragraph (goto-char end))
  22571. ;; At a plain list, try to move to second element in
  22572. ;; first item, if possible.
  22573. (plain-list (end-of-line)
  22574. (org-forward-paragraph))))
  22575. ;; When contents start on the middle of a line (e.g. in
  22576. ;; items and footnote definitions), try to reach first
  22577. ;; element starting after current line.
  22578. ((> (line-end-position) contents-begin)
  22579. (end-of-line)
  22580. (org-forward-paragraph))
  22581. (t (goto-char contents-begin)))))
  22582. (defun org-backward-paragraph ()
  22583. "Move backward to start of previous paragraph or equivalent.
  22584. The function moves point to the beginning of the current
  22585. structural element, which can be a paragraph, a table, a list
  22586. item, etc., or to the beginning of the previous visible one if
  22587. point is already there. It also provides some special moves for
  22588. convenience:
  22589. - On an affiliated keyword, jump to the first one.
  22590. - On a table or a property drawer, move to its beginning.
  22591. - On a verse or source block, stop before blank lines."
  22592. (interactive)
  22593. (when (bobp) (user-error "Cannot move further up"))
  22594. (let* ((deactivate-mark nil)
  22595. (element (org-element-at-point))
  22596. (type (org-element-type element))
  22597. (contents-begin (org-element-property :contents-begin element))
  22598. (contents-end (org-element-property :contents-end element))
  22599. (post-affiliated (org-element-property :post-affiliated element))
  22600. (begin (org-element-property :begin element)))
  22601. (cond
  22602. ((not element) (goto-char (point-min)))
  22603. ((= (point) begin)
  22604. (backward-char)
  22605. (org-backward-paragraph))
  22606. ((<= (point) post-affiliated) (goto-char begin))
  22607. ((memq type '(node-property table-row))
  22608. (goto-char (org-element-property
  22609. :post-affiliated (org-element-property :parent element))))
  22610. ((memq type '(property-drawer table)) (goto-char begin))
  22611. ((memq type '(src-block verse-block))
  22612. (when (eq type 'src-block)
  22613. (setq contents-begin
  22614. (save-excursion (goto-char begin) (forward-line) (point))))
  22615. (if (= (point) contents-begin) (goto-char post-affiliated)
  22616. ;; Inside a verse block, see blank lines as paragraph
  22617. ;; separators.
  22618. (let ((origin (point)))
  22619. (skip-chars-backward " \r\t\n" contents-begin)
  22620. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22621. (skip-chars-forward " \r\t\n" origin)
  22622. (if (= (point) origin) (goto-char contents-begin)
  22623. (beginning-of-line))))))
  22624. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22625. ((eq type 'paragraph)
  22626. (goto-char contents-begin)
  22627. ;; When at first paragraph in an item or a footnote definition,
  22628. ;; move directly to beginning of line.
  22629. (let ((parent-contents
  22630. (org-element-property
  22631. :contents-begin (org-element-property :parent element))))
  22632. (when (and parent-contents (= parent-contents contents-begin))
  22633. (beginning-of-line))))
  22634. ;; At the end of a greater element, move to the beginning of the
  22635. ;; last element within.
  22636. ((>= (point) contents-end)
  22637. (goto-char (1- contents-end))
  22638. (org-backward-paragraph))
  22639. (t (goto-char (or post-affiliated begin))))
  22640. ;; Ensure we never leave point invisible.
  22641. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22642. (defun org-forward-element ()
  22643. "Move forward by one element.
  22644. Move to the next element at the same level, when possible."
  22645. (interactive)
  22646. (cond ((eobp) (user-error "Cannot move further down"))
  22647. ((org-with-limited-levels (org-at-heading-p))
  22648. (let ((origin (point)))
  22649. (goto-char (org-end-of-subtree nil t))
  22650. (unless (org-with-limited-levels (org-at-heading-p))
  22651. (goto-char origin)
  22652. (user-error "Cannot move further down"))))
  22653. (t
  22654. (let* ((elem (org-element-at-point))
  22655. (end (org-element-property :end elem))
  22656. (parent (org-element-property :parent elem)))
  22657. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22658. (goto-char (org-element-property :end parent)))
  22659. ((integer-or-marker-p end) (goto-char end))
  22660. (t (message "No element at point")))))))
  22661. (defun org-backward-element ()
  22662. "Move backward by one element.
  22663. Move to the previous element at the same level, when possible."
  22664. (interactive)
  22665. (cond ((bobp) (user-error "Cannot move further up"))
  22666. ((org-with-limited-levels (org-at-heading-p))
  22667. ;; At a headline, move to the previous one, if any, or stay
  22668. ;; here.
  22669. (let ((origin (point)))
  22670. (org-with-limited-levels (org-backward-heading-same-level 1))
  22671. ;; When current headline has no sibling above, move to its
  22672. ;; parent.
  22673. (when (= (point) origin)
  22674. (or (org-with-limited-levels (org-up-heading-safe))
  22675. (progn (goto-char origin)
  22676. (user-error "Cannot move further up"))))))
  22677. (t
  22678. (let* ((elem (org-element-at-point))
  22679. (beg (org-element-property :begin elem)))
  22680. (cond
  22681. ;; Move to beginning of current element if point isn't
  22682. ;; there already.
  22683. ((null beg) (message "No element at point"))
  22684. ((/= (point) beg) (goto-char beg))
  22685. (t (goto-char beg)
  22686. (skip-chars-backward " \r\t\n")
  22687. (unless (bobp)
  22688. (let ((prev (org-element-at-point)))
  22689. (goto-char (org-element-property :begin prev))
  22690. (while (and (setq prev (org-element-property :parent prev))
  22691. (<= (org-element-property :end prev) beg))
  22692. (goto-char (org-element-property :begin prev)))))))))))
  22693. (defun org-up-element ()
  22694. "Move to upper element."
  22695. (interactive)
  22696. (if (org-with-limited-levels (org-at-heading-p))
  22697. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22698. (let* ((elem (org-element-at-point))
  22699. (parent (org-element-property :parent elem)))
  22700. (if parent (goto-char (org-element-property :begin parent))
  22701. (if (org-with-limited-levels (org-before-first-heading-p))
  22702. (user-error "No surrounding element")
  22703. (org-with-limited-levels (org-back-to-heading)))))))
  22704. (defvar org-element-greater-elements)
  22705. (defun org-down-element ()
  22706. "Move to inner element."
  22707. (interactive)
  22708. (let ((element (org-element-at-point)))
  22709. (cond
  22710. ((memq (org-element-type element) '(plain-list table))
  22711. (goto-char (org-element-property :contents-begin element))
  22712. (forward-char))
  22713. ((memq (org-element-type element) org-element-greater-elements)
  22714. ;; If contents are hidden, first disclose them.
  22715. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22716. (goto-char (or (org-element-property :contents-begin element)
  22717. (user-error "No content for this element"))))
  22718. (t (user-error "No inner element")))))
  22719. (defun org-drag-element-backward ()
  22720. "Move backward element at point."
  22721. (interactive)
  22722. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22723. (let* ((elem (org-element-at-point))
  22724. (prev-elem
  22725. (save-excursion
  22726. (goto-char (org-element-property :begin elem))
  22727. (skip-chars-backward " \r\t\n")
  22728. (unless (bobp)
  22729. (let* ((beg (org-element-property :begin elem))
  22730. (prev (org-element-at-point))
  22731. (up prev))
  22732. (while (and (setq up (org-element-property :parent up))
  22733. (<= (org-element-property :end up) beg))
  22734. (setq prev up))
  22735. prev)))))
  22736. ;; Error out if no previous element or previous element is
  22737. ;; a parent of the current one.
  22738. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22739. (user-error "Cannot drag element backward")
  22740. (let ((pos (point)))
  22741. (org-element-swap-A-B prev-elem elem)
  22742. (goto-char (+ (org-element-property :begin prev-elem)
  22743. (- pos (org-element-property :begin elem)))))))))
  22744. (defun org-drag-element-forward ()
  22745. "Move forward element at point."
  22746. (interactive)
  22747. (let* ((pos (point))
  22748. (elem (org-element-at-point)))
  22749. (when (= (point-max) (org-element-property :end elem))
  22750. (user-error "Cannot drag element forward"))
  22751. (goto-char (org-element-property :end elem))
  22752. (let ((next-elem (org-element-at-point)))
  22753. (when (or (org-element-nested-p elem next-elem)
  22754. (and (eq (org-element-type next-elem) 'headline)
  22755. (not (eq (org-element-type elem) 'headline))))
  22756. (goto-char pos)
  22757. (user-error "Cannot drag element forward"))
  22758. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22759. ;; body's length (without final blanks) and by the length of
  22760. ;; blanks between ELEM and NEXT-ELEM.
  22761. (let ((size-next (- (save-excursion
  22762. (goto-char (org-element-property :end next-elem))
  22763. (skip-chars-backward " \r\t\n")
  22764. (forward-line)
  22765. ;; Small correction if buffer doesn't end
  22766. ;; with a newline character.
  22767. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22768. (org-element-property :begin next-elem)))
  22769. (size-blank (- (org-element-property :end elem)
  22770. (save-excursion
  22771. (goto-char (org-element-property :end elem))
  22772. (skip-chars-backward " \r\t\n")
  22773. (forward-line)
  22774. (point)))))
  22775. (org-element-swap-A-B elem next-elem)
  22776. (goto-char (+ pos size-next size-blank))))))
  22777. (defun org-drag-line-forward (arg)
  22778. "Drag the line at point ARG lines forward."
  22779. (interactive "p")
  22780. (dotimes (n (abs arg))
  22781. (let ((c (current-column)))
  22782. (if (< 0 arg)
  22783. (progn
  22784. (beginning-of-line 2)
  22785. (transpose-lines 1)
  22786. (beginning-of-line 0))
  22787. (transpose-lines 1)
  22788. (beginning-of-line -1))
  22789. (org-move-to-column c))))
  22790. (defun org-drag-line-backward (arg)
  22791. "Drag the line at point ARG lines backward."
  22792. (interactive "p")
  22793. (org-drag-line-forward (- arg)))
  22794. (defun org-mark-element ()
  22795. "Put point at beginning of this element, mark at end.
  22796. Interactively, if this command is repeated or (in Transient Mark
  22797. mode) if the mark is active, it marks the next element after the
  22798. ones already marked."
  22799. (interactive)
  22800. (let (deactivate-mark)
  22801. (if (and (org-called-interactively-p 'any)
  22802. (or (and (eq last-command this-command) (mark t))
  22803. (and transient-mark-mode mark-active)))
  22804. (set-mark
  22805. (save-excursion
  22806. (goto-char (mark))
  22807. (goto-char (org-element-property :end (org-element-at-point)))))
  22808. (let ((element (org-element-at-point)))
  22809. (end-of-line)
  22810. (push-mark (org-element-property :end element) t t)
  22811. (goto-char (org-element-property :begin element))))))
  22812. (defun org-narrow-to-element ()
  22813. "Narrow buffer to current element."
  22814. (interactive)
  22815. (let ((elem (org-element-at-point)))
  22816. (cond
  22817. ((eq (car elem) 'headline)
  22818. (narrow-to-region
  22819. (org-element-property :begin elem)
  22820. (org-element-property :end elem)))
  22821. ((memq (car elem) org-element-greater-elements)
  22822. (narrow-to-region
  22823. (org-element-property :contents-begin elem)
  22824. (org-element-property :contents-end elem)))
  22825. (t
  22826. (narrow-to-region
  22827. (org-element-property :begin elem)
  22828. (org-element-property :end elem))))))
  22829. (defun org-transpose-element ()
  22830. "Transpose current and previous elements, keeping blank lines between.
  22831. Point is moved after both elements."
  22832. (interactive)
  22833. (org-skip-whitespace)
  22834. (let ((end (org-element-property :end (org-element-at-point))))
  22835. (org-drag-element-backward)
  22836. (goto-char end)))
  22837. (defun org-unindent-buffer ()
  22838. "Un-indent the visible part of the buffer.
  22839. Relative indentation (between items, inside blocks, etc.) isn't
  22840. modified."
  22841. (interactive)
  22842. (unless (eq major-mode 'org-mode)
  22843. (user-error "Cannot un-indent a buffer not in Org mode"))
  22844. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22845. unindent-tree ; For byte-compiler.
  22846. (unindent-tree
  22847. (function
  22848. (lambda (contents)
  22849. (mapc
  22850. (lambda (element)
  22851. (if (memq (org-element-type element) '(headline section))
  22852. (funcall unindent-tree (org-element-contents element))
  22853. (save-excursion
  22854. (save-restriction
  22855. (narrow-to-region
  22856. (org-element-property :begin element)
  22857. (org-element-property :end element))
  22858. (org-do-remove-indentation)))))
  22859. (reverse contents))))))
  22860. (funcall unindent-tree (org-element-contents parse-tree))))
  22861. (defun org-show-subtree ()
  22862. "Show everything after this heading at deeper levels."
  22863. (interactive)
  22864. (outline-flag-region
  22865. (point)
  22866. (save-excursion
  22867. (org-end-of-subtree t t))
  22868. nil))
  22869. (defun org-show-entry ()
  22870. "Show the body directly following this heading.
  22871. Show the heading too, if it is currently invisible."
  22872. (interactive)
  22873. (save-excursion
  22874. (ignore-errors
  22875. (org-back-to-heading t)
  22876. (outline-flag-region
  22877. (max (point-min) (1- (point)))
  22878. (save-excursion
  22879. (if (re-search-forward
  22880. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22881. (match-beginning 1)
  22882. (point-max)))
  22883. nil)
  22884. (org-cycle-hide-drawers 'children))))
  22885. (defun org-make-options-regexp (kwds &optional extra)
  22886. "Make a regular expression for keyword lines.
  22887. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22888. when non-nil, is a regexp matching keywords names."
  22889. (concat "^[ \t]*#\\+\\("
  22890. (regexp-opt kwds)
  22891. (and extra (concat (and kwds "\\|") extra))
  22892. "\\):[ \t]*\\(.*\\)"))
  22893. ;; Make isearch reveal the necessary context
  22894. (defun org-isearch-end ()
  22895. "Reveal context after isearch exits."
  22896. (when isearch-success ; only if search was successful
  22897. (if (featurep 'xemacs)
  22898. ;; Under XEmacs, the hook is run in the correct place,
  22899. ;; we directly show the context.
  22900. (org-show-context 'isearch)
  22901. ;; In Emacs the hook runs *before* restoring the overlays.
  22902. ;; So we have to use a one-time post-command-hook to do this.
  22903. ;; (Emacs 22 has a special variable, see function `org-mode')
  22904. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22905. isearch-mode-end-hook-quit)
  22906. ;; Only when the isearch was not quitted.
  22907. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22908. 'append 'local)))))
  22909. (defun org-isearch-post-command ()
  22910. "Remove self from hook, and show context."
  22911. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22912. (org-show-context 'isearch))
  22913. ;;;; Integration with and fixes for other packages
  22914. ;;; Imenu support
  22915. (defvar org-imenu-markers nil
  22916. "All markers currently used by Imenu.")
  22917. (make-variable-buffer-local 'org-imenu-markers)
  22918. (defun org-imenu-new-marker (&optional pos)
  22919. "Return a new marker for use by Imenu, and remember the marker."
  22920. (let ((m (make-marker)))
  22921. (move-marker m (or pos (point)))
  22922. (push m org-imenu-markers)
  22923. m))
  22924. (defun org-imenu-get-tree ()
  22925. "Produce the index for Imenu."
  22926. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22927. (setq org-imenu-markers nil)
  22928. (let* ((n org-imenu-depth)
  22929. (re (concat "^" (org-get-limited-outline-regexp)))
  22930. (subs (make-vector (1+ n) nil))
  22931. (last-level 0)
  22932. m level head0 head)
  22933. (save-excursion
  22934. (save-restriction
  22935. (widen)
  22936. (goto-char (point-max))
  22937. (while (re-search-backward re nil t)
  22938. (setq level (org-reduced-level (funcall outline-level)))
  22939. (when (and (<= level n)
  22940. (looking-at org-complex-heading-regexp)
  22941. (setq head0 (org-match-string-no-properties 4)))
  22942. (setq head (org-link-display-format head0)
  22943. m (org-imenu-new-marker))
  22944. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22945. (if (>= level last-level)
  22946. (push (cons head m) (aref subs level))
  22947. (push (cons head (aref subs (1+ level))) (aref subs level))
  22948. (loop for i from (1+ level) to n do (aset subs i nil)))
  22949. (setq last-level level)))))
  22950. (aref subs 1)))
  22951. (eval-after-load "imenu"
  22952. '(progn
  22953. (add-hook 'imenu-after-jump-hook
  22954. (lambda ()
  22955. (if (derived-mode-p 'org-mode)
  22956. (org-show-context 'org-goto))))))
  22957. (defun org-link-display-format (link)
  22958. "Replace a link with its the description.
  22959. If there is no description, use the link target."
  22960. (save-match-data
  22961. (if (string-match org-bracket-link-analytic-regexp link)
  22962. (replace-match (if (match-end 5)
  22963. (match-string 5 link)
  22964. (concat (match-string 1 link)
  22965. (match-string 3 link)))
  22966. nil t link)
  22967. link)))
  22968. (defun org-toggle-link-display ()
  22969. "Toggle the literal or descriptive display of links."
  22970. (interactive)
  22971. (if org-descriptive-links
  22972. (progn (org-remove-from-invisibility-spec '(org-link))
  22973. (org-restart-font-lock)
  22974. (setq org-descriptive-links nil))
  22975. (progn (add-to-invisibility-spec '(org-link))
  22976. (org-restart-font-lock)
  22977. (setq org-descriptive-links t))))
  22978. ;; Speedbar support
  22979. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22980. "Overlay marking the agenda restriction line in speedbar.")
  22981. (overlay-put org-speedbar-restriction-lock-overlay
  22982. 'face 'org-agenda-restriction-lock)
  22983. (overlay-put org-speedbar-restriction-lock-overlay
  22984. 'help-echo "Agendas are currently limited to this item.")
  22985. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22986. (defun org-speedbar-set-agenda-restriction ()
  22987. "Restrict future agenda commands to the location at point in speedbar.
  22988. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22989. (interactive)
  22990. (require 'org-agenda)
  22991. (let (p m tp np dir txt)
  22992. (cond
  22993. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22994. 'org-imenu t))
  22995. (setq m (get-text-property p 'org-imenu-marker))
  22996. (with-current-buffer (marker-buffer m)
  22997. (goto-char m)
  22998. (org-agenda-set-restriction-lock 'subtree)))
  22999. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23000. 'speedbar-function 'speedbar-find-file))
  23001. (setq tp (previous-single-property-change
  23002. (1+ p) 'speedbar-function)
  23003. np (next-single-property-change
  23004. tp 'speedbar-function)
  23005. dir (speedbar-line-directory)
  23006. txt (buffer-substring-no-properties (or tp (point-min))
  23007. (or np (point-max))))
  23008. (with-current-buffer (find-file-noselect
  23009. (let ((default-directory dir))
  23010. (expand-file-name txt)))
  23011. (unless (derived-mode-p 'org-mode)
  23012. (user-error "Cannot restrict to non-Org-mode file"))
  23013. (org-agenda-set-restriction-lock 'file)))
  23014. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23015. (move-overlay org-speedbar-restriction-lock-overlay
  23016. (point-at-bol) (point-at-eol))
  23017. (setq current-prefix-arg nil)
  23018. (org-agenda-maybe-redo)))
  23019. (defvar speedbar-file-key-map)
  23020. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23021. (eval-after-load "speedbar"
  23022. '(progn
  23023. (speedbar-add-supported-extension ".org")
  23024. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23025. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23026. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23027. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23028. (add-hook 'speedbar-visiting-tag-hook
  23029. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23030. ;;; Fixes and Hacks for problems with other packages
  23031. (defun org--flyspell-object-check-p (element)
  23032. "Non-nil when Flyspell can check object at point.
  23033. ELEMENT is the element at point."
  23034. (let ((object (save-excursion
  23035. (when (org-looking-at-p "\\>") (backward-char))
  23036. (org-element-context element))))
  23037. (case (org-element-type object)
  23038. ;; Prevent checks in links due to keybinding conflict with
  23039. ;; Flyspell.
  23040. ((code entity export-snippet inline-babel-call
  23041. inline-src-block line-break latex-fragment link macro
  23042. statistics-cookie target timestamp verbatim)
  23043. nil)
  23044. (footnote-reference
  23045. ;; Only in inline footnotes, within the definition.
  23046. (and (eq (org-element-property :type object) 'inline)
  23047. (< (save-excursion
  23048. (goto-char (org-element-property :begin object))
  23049. (search-forward ":" nil t 2))
  23050. (point))))
  23051. (otherwise t))))
  23052. (defun org-mode-flyspell-verify ()
  23053. "Function used for `flyspell-generic-check-word-predicate'."
  23054. (if (org-at-heading-p)
  23055. ;; At a headline or an inlinetask, check title only. This is
  23056. ;; faster than relying on `org-element-at-point'.
  23057. (and (save-excursion (beginning-of-line)
  23058. (and (let ((case-fold-search t))
  23059. (not (looking-at "\\*+ END[ \t]*$")))
  23060. (looking-at org-complex-heading-regexp)))
  23061. (match-beginning 4)
  23062. (>= (point) (match-beginning 4))
  23063. (or (not (match-beginning 5))
  23064. (< (point) (match-beginning 5))))
  23065. (let* ((element (org-element-at-point))
  23066. (post-affiliated (org-element-property :post-affiliated element)))
  23067. (cond
  23068. ;; Ignore checks in all affiliated keywords but captions.
  23069. ((< (point) post-affiliated)
  23070. (and (save-excursion
  23071. (beginning-of-line)
  23072. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23073. (> (point) (match-end 0))
  23074. (org--flyspell-object-check-p element)))
  23075. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23076. ((let ((log (org-log-into-drawer)))
  23077. (and log
  23078. (let ((drawer (org-element-lineage element '(drawer))))
  23079. (and drawer
  23080. (eq (compare-strings
  23081. log nil nil
  23082. (org-element-property :drawer-name drawer) nil nil t)
  23083. t)))))
  23084. nil)
  23085. (t
  23086. (case (org-element-type element)
  23087. ((comment quote-section) t)
  23088. (comment-block
  23089. ;; Allow checks between block markers, not on them.
  23090. (and (> (line-beginning-position) post-affiliated)
  23091. (save-excursion
  23092. (end-of-line)
  23093. (skip-chars-forward " \r\t\n")
  23094. (< (point) (org-element-property :end element)))))
  23095. ;; Arbitrary list of keywords where checks are meaningful.
  23096. ;; Make sure point is on the value part of the element.
  23097. (keyword
  23098. (and (member (org-element-property :key element)
  23099. '("DESCRIPTION" "TITLE"))
  23100. (save-excursion
  23101. (search-backward ":" (line-beginning-position) t))))
  23102. ;; Check is globally allowed in paragraphs verse blocks and
  23103. ;; table rows (after affiliated keywords) but some objects
  23104. ;; must not be affected.
  23105. ((paragraph table-row verse-block)
  23106. (let ((cbeg (org-element-property :contents-begin element))
  23107. (cend (org-element-property :contents-end element)))
  23108. (and cbeg (>= (point) cbeg) (< (point) cend)
  23109. (org--flyspell-object-check-p element))))))))))
  23110. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23111. (defun org-remove-flyspell-overlays-in (beg end)
  23112. "Remove flyspell overlays in region."
  23113. (and (org-bound-and-true-p flyspell-mode)
  23114. (fboundp 'flyspell-delete-region-overlays)
  23115. (flyspell-delete-region-overlays beg end)))
  23116. (defvar flyspell-delayed-commands)
  23117. (eval-after-load "flyspell"
  23118. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23119. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23120. (eval-after-load "bookmark"
  23121. '(if (boundp 'bookmark-after-jump-hook)
  23122. ;; We can use the hook
  23123. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23124. ;; Hook not available, use advice
  23125. (defadvice bookmark-jump (after org-make-visible activate)
  23126. "Make the position visible."
  23127. (org-bookmark-jump-unhide))))
  23128. ;; Make sure saveplace shows the location if it was hidden
  23129. (eval-after-load "saveplace"
  23130. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23131. "Make the position visible."
  23132. (org-bookmark-jump-unhide)))
  23133. ;; Make sure ecb shows the location if it was hidden
  23134. (eval-after-load "ecb"
  23135. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23136. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23137. (if (derived-mode-p 'org-mode)
  23138. (org-show-context))))
  23139. (defun org-bookmark-jump-unhide ()
  23140. "Unhide the current position, to show the bookmark location."
  23141. (and (derived-mode-p 'org-mode)
  23142. (or (outline-invisible-p)
  23143. (save-excursion (goto-char (max (point-min) (1- (point))))
  23144. (outline-invisible-p)))
  23145. (org-show-context 'bookmark-jump)))
  23146. (defun org-mark-jump-unhide ()
  23147. "Make the point visible with `org-show-context' after jumping to the mark."
  23148. (when (and (derived-mode-p 'org-mode)
  23149. (outline-invisible-p))
  23150. (org-show-context 'mark-goto)))
  23151. (eval-after-load "simple"
  23152. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23153. "Make the point visible with `org-show-context'."
  23154. (org-mark-jump-unhide)))
  23155. (eval-after-load "simple"
  23156. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23157. "Make the point visible with `org-show-context'."
  23158. (org-mark-jump-unhide)))
  23159. (eval-after-load "simple"
  23160. '(defadvice pop-global-mark (after org-make-visible activate)
  23161. "Make the point visible with `org-show-context'."
  23162. (org-mark-jump-unhide)))
  23163. ;; Make session.el ignore our circular variable
  23164. (defvar session-globals-exclude)
  23165. (eval-after-load "session"
  23166. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23167. ;;;; Finish up
  23168. (provide 'org)
  23169. (run-hooks 'org-load-hook)
  23170. ;;; org.el ends here