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: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  85. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  86. ;;
  87. ;; In Org buffers, the value of `outline-regexp' is that of
  88. ;; `org-outline-regexp'. The only function still directly relying on
  89. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  90. ;; job when `orgstruct-mode' is active.
  91. (defvar org-outline-regexp "\\*+ "
  92. "Regexp to match Org headlines.")
  93. (defvar org-outline-regexp-bol "^\\*+ "
  94. "Regexp to match Org headlines.
  95. This is similar to `org-outline-regexp' but additionally makes
  96. sure that we are at the beginning of the line.")
  97. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  98. "Matches a headline, putting stars and text into groups.
  99. Stars are put in group 1 and the trimmed body in group 2.")
  100. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  101. (unless (boundp 'calendar-view-holidays-initially-flag)
  102. (org-defvaralias 'calendar-view-holidays-initially-flag
  103. 'view-calendar-holidays-initially))
  104. (unless (boundp 'calendar-view-diary-initially-flag)
  105. (org-defvaralias 'calendar-view-diary-initially-flag
  106. 'view-diary-entries-initially))
  107. (unless (boundp 'diary-fancy-buffer)
  108. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  109. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  112. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  113. (declare-function org-agenda-redo "org-agenda" (&optional all))
  114. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  115. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  116. (declare-function org-beamer-mode "ox-beamer" ())
  117. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  118. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  119. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  120. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  121. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  122. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  123. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  124. (declare-function org-clock-update-time-maybe "org-clock" ())
  125. (declare-function org-clocktable-shift "org-clock" (dir n))
  126. (declare-function org-element-at-point "org-element" ())
  127. (declare-function org-element-cache-refresh "org-element" (pos))
  128. (declare-function org-element-cache-reset "org-element" (&optional all))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-context "org-element" (&optional element))
  131. (declare-function org-element-copy "org-element" (datum))
  132. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  133. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element" (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element-type "org-element" (element))
  140. (declare-function org-element-update-syntax "org-element" ())
  141. (declare-function org-id-find-id-file "org-id" (id))
  142. (declare-function org-id-get-create "org-id" (&optional force))
  143. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  144. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  145. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  146. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  147. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  148. (declare-function org-table-align "org-table" ())
  149. (declare-function org-table-begin "org-table" (&optional table-type))
  150. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  151. (declare-function org-table-blank-field "org-table" ())
  152. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  153. (declare-function org-table-edit-field "org-table" (arg))
  154. (declare-function org-table-end "org-table" (&optional table-type))
  155. (declare-function org-table-end-of-field "org-table" (&optional n))
  156. (declare-function org-table-insert-row "org-table" (&optional arg))
  157. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  158. (declare-function org-table-maybe-eval-formula "org-table" ())
  159. (declare-function org-table-maybe-recalculate-line "org-table" ())
  160. (declare-function org-table-next-row "org-table" ())
  161. (declare-function org-table-paste-rectangle "org-table" ())
  162. (declare-function org-table-wrap-region "org-table" (arg))
  163. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  164. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  165. (declare-function orgtbl-mode "org-table" (&optional arg))
  166. (defsubst org-uniquify (list)
  167. "Non-destructively remove duplicate elements from LIST."
  168. (let ((res (copy-sequence list))) (delete-dups res)))
  169. (defsubst org-get-at-bol (property)
  170. "Get text property PROPERTY at the beginning of line."
  171. (get-text-property (point-at-bol) property))
  172. (defsubst org-trim (s)
  173. "Remove whitespace at the beginning and the end of string S."
  174. (replace-regexp-in-string
  175. "\\`[ \t\n\r]+" ""
  176. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  177. ;; load languages based on value of `org-babel-load-languages'
  178. (defvar org-babel-load-languages)
  179. ;;;###autoload
  180. (defun org-babel-do-load-languages (sym value)
  181. "Load the languages defined in `org-babel-load-languages'."
  182. (set-default sym value)
  183. (mapc (lambda (pair)
  184. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  185. (if active
  186. (progn
  187. (require (intern (concat "ob-" lang))))
  188. (progn
  189. (funcall 'fmakunbound
  190. (intern (concat "org-babel-execute:" lang)))
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-expand-body:" lang)))))))
  193. org-babel-load-languages))
  194. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  195. ;;;###autoload
  196. (defun org-babel-load-file (file &optional compile)
  197. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  198. This function exports the source code using `org-babel-tangle'
  199. and then loads the resulting file using `load-file'. With prefix
  200. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  201. file to byte-code before it is loaded."
  202. (interactive "fFile to load: \nP")
  203. (let* ((age (lambda (file)
  204. (float-time
  205. (time-subtract (current-time)
  206. (nth 5 (or (file-attributes (file-truename file))
  207. (file-attributes file)))))))
  208. (base-name (file-name-sans-extension file))
  209. (exported-file (concat base-name ".el")))
  210. ;; tangle if the org-mode file is newer than the elisp file
  211. (unless (and (file-exists-p exported-file)
  212. (> (funcall age file) (funcall age exported-file)))
  213. ;; Tangle-file traversal returns reversed list of tangled files
  214. ;; and we want to evaluate the first target.
  215. (setq exported-file
  216. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "ebnf2ps" ebnf2ps))
  283. :value-type (boolean :tag "Activate" :value t)))
  284. ;;;; Customization variables
  285. (defcustom org-clone-delete-id nil
  286. "Remove ID property of clones of a subtree.
  287. When non-nil, clones of a subtree don't inherit the ID property.
  288. Otherwise they inherit the ID property with a new unique
  289. identifier."
  290. :type 'boolean
  291. :version "24.1"
  292. :group 'org-id)
  293. ;;; Version
  294. (org-check-version)
  295. ;;;###autoload
  296. (defun org-version (&optional here full message)
  297. "Show the org-mode version.
  298. Interactively, or when MESSAGE is non-nil, show it in echo area.
  299. With prefix argument, or when HERE is non-nil, insert it at point.
  300. In non-interactive uses, a reduced version string is output unless
  301. FULL is given."
  302. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  303. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  304. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  305. (load-suffixes (list ".el"))
  306. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  307. (org-trash (or
  308. (and (fboundp 'org-release) (fboundp 'org-git-version))
  309. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  310. (load-suffixes save-load-suffixes)
  311. (org-version (org-release))
  312. (git-version (org-git-version))
  313. (version (format "Org-mode version %s (%s @ %s)"
  314. org-version
  315. git-version
  316. (if org-install-dir
  317. (if (string= org-dir org-install-dir)
  318. org-install-dir
  319. (concat "mixed installation! " org-install-dir " and " org-dir))
  320. "org-loaddefs.el can not be found!")))
  321. (version1 (if full version org-version)))
  322. (when here (insert version1))
  323. (when message (message "%s" version1))
  324. version1))
  325. (defconst org-version (org-version))
  326. ;;; Syntax Constants
  327. ;;;; Block
  328. (defconst org-block-regexp
  329. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  330. "Regular expression for hiding blocks.")
  331. (defconst org-dblock-start-re
  332. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  333. "Matches the start line of a dynamic block, with parameters.")
  334. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  335. "Matches the end of a dynamic block.")
  336. ;;;; Clock and Planning
  337. (defconst org-clock-string "CLOCK:"
  338. "String used as prefix for timestamps clocking work hours on an item.")
  339. (defvar org-closed-string "CLOSED:"
  340. "String used as the prefix for timestamps logging closing a TODO entry.")
  341. (defvar org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].")
  346. (defvar org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].")
  351. (defconst org-ds-keyword-length
  352. (+ 2
  353. (apply #'max
  354. (mapcar #'length
  355. (list org-deadline-string org-scheduled-string
  356. org-clock-string org-closed-string))))
  357. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  358. (defconst org-planning-line-re
  359. (concat "^[ \t]*"
  360. (regexp-opt
  361. (list org-closed-string org-deadline-string org-scheduled-string)
  362. t))
  363. "Matches a line with planning info.
  364. Matched keyword is in group 1.")
  365. (defconst org-clock-line-re
  366. (concat "^[ \t]*" org-clock-string)
  367. "Matches a line with clock info.")
  368. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  369. "Matches the DEADLINE keyword.")
  370. (defconst org-deadline-time-regexp
  371. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  372. "Matches the DEADLINE keyword together with a time stamp.")
  373. (defconst org-deadline-time-hour-regexp
  374. (concat "\\<" org-deadline-string
  375. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  376. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  377. (defconst org-deadline-line-regexp
  378. (concat "\\<\\(" org-deadline-string "\\).*")
  379. "Matches the DEADLINE keyword and the rest of the line.")
  380. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  381. "Matches the SCHEDULED keyword.")
  382. (defconst org-scheduled-time-regexp
  383. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  384. "Matches the SCHEDULED keyword together with a time stamp.")
  385. (defconst org-scheduled-time-hour-regexp
  386. (concat "\\<" org-scheduled-string
  387. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  388. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  389. (defconst org-closed-time-regexp
  390. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  391. "Matches the CLOSED keyword together with a time stamp.")
  392. (defconst org-keyword-time-regexp
  393. (concat "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string
  396. org-clock-string)
  397. t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 4 keywords, together with the time stamp.")
  400. (defconst org-keyword-time-not-clock-regexp
  401. (concat
  402. "\\<"
  403. (regexp-opt
  404. (list org-scheduled-string org-deadline-string org-closed-string) t)
  405. " *[[<]\\([^]>]+\\)[]>]")
  406. "Matches any of the 3 keywords, together with the time stamp.")
  407. (defconst org-maybe-keyword-time-regexp
  408. (concat "\\(\\<"
  409. (regexp-opt
  410. (list org-scheduled-string org-deadline-string org-closed-string
  411. org-clock-string)
  412. t)
  413. "\\)?"
  414. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  415. "\\|"
  416. "<%%([^\r\n>]*>\\)")
  417. "Matches a timestamp, possibly preceded by a keyword.")
  418. (defconst org-all-time-keywords
  419. (mapcar (lambda (w) (substring w 0 -1))
  420. (list org-scheduled-string org-deadline-string
  421. org-clock-string org-closed-string))
  422. "List of time keywords.")
  423. ;;;; Drawer
  424. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  425. "Matches first or last line of a hidden block.
  426. Group 1 contains drawer's name or \"END\".")
  427. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  428. "Regular expression matching the first line of a property drawer.")
  429. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  430. "Regular expression matching the last line of a property drawer.")
  431. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  432. "Regular expression matching the first line of a clock drawer.")
  433. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  434. "Regular expression matching the last line of a clock drawer.")
  435. (defconst org-property-drawer-re
  436. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  437. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  438. "[ \t]*:END:[ \t]*$")
  439. "Matches an entire property drawer.")
  440. (defconst org-clock-drawer-re
  441. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  442. org-clock-drawer-end-re "\\)\n?")
  443. "Matches an entire clock drawer.")
  444. ;;;; Headline
  445. (defconst org-heading-keyword-regexp-format
  446. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  447. "Printf format for a regexp matching a headline with some keyword.
  448. This regexp will match the headline of any node which has the
  449. exact keyword that is put into the format. The keyword isn't in
  450. any group by default, but the stars and the body are.")
  451. (defconst org-heading-keyword-maybe-regexp-format
  452. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  453. "Printf format for a regexp matching a headline, possibly with some keyword.
  454. This regexp can match any headline with the specified keyword, or
  455. without a keyword. The keyword isn't in any group by default,
  456. but the stars and the body are.")
  457. (defconst org-archive-tag "ARCHIVE"
  458. "The tag that marks a subtree as archived.
  459. An archived subtree does not open during visibility cycling, and does
  460. not contribute to the agenda listings.")
  461. (defconst org-comment-string "COMMENT"
  462. "Entries starting with this keyword will never be exported.
  463. An entry can be toggled between COMMENT and normal with
  464. \\[org-toggle-comment].")
  465. ;;;; LaTeX Environments and Fragments
  466. (defconst org-latex-regexps
  467. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  468. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  469. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  470. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  471. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  472. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  473. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  474. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  475. "Regular expressions for matching embedded LaTeX.")
  476. ;;;; Node Property
  477. (defconst org-effort-property "Effort"
  478. "The property that is being used to keep track of effort estimates.
  479. Effort estimates given in this property need to have the format H:MM.")
  480. ;;;; Table
  481. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  482. "Detect an org-type or table-type table.")
  483. (defconst org-table-line-regexp "^[ \t]*|"
  484. "Detect an org-type table line.")
  485. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  486. "Detect an org-type table line.")
  487. (defconst org-table-hline-regexp "^[ \t]*|-"
  488. "Detect an org-type table hline.")
  489. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  490. "Detect a table-type table hline.")
  491. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  492. "Detect the first line outside a table when searching from within it.
  493. This works for both table types.")
  494. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  495. "Detect a #+TBLFM line.")
  496. ;;;; Timestamp
  497. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  498. "Regular expression for fast time stamp matching.")
  499. (defconst org-ts-regexp-inactive
  500. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  501. "Regular expression for fast inactive time stamp matching.")
  502. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  503. "Regular expression for fast time stamp matching.")
  504. (defconst org-ts-regexp0
  505. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  506. "Regular expression matching time strings for analysis.
  507. This one does not require the space after the date, so it can be used
  508. on a string that terminates immediately after the date.")
  509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  510. "Regular expression matching time strings for analysis.")
  511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  512. "Regular expression matching time stamps, with groups.")
  513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  514. "Regular expression matching time stamps (also [..]), with groups.")
  515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  516. "Regular expression matching a time stamp range.")
  517. (defconst org-tr-regexp-both
  518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  519. "Regular expression matching a time stamp range.")
  520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  521. org-ts-regexp "\\)?")
  522. "Regular expression matching a time stamp or time stamp range.")
  523. (defconst org-tsr-regexp-both
  524. (concat org-ts-regexp-both "\\(--?-?"
  525. org-ts-regexp-both "\\)?")
  526. "Regular expression matching a time stamp or time stamp range.
  527. The time stamps may be either active or inactive.")
  528. (defconst org-repeat-re
  529. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  530. "Regular expression for specifying repeated events.
  531. After a match, group 1 contains the repeat expression.")
  532. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  533. "Formats for `format-time-string' which are used for time stamps.")
  534. ;;; The custom variables
  535. (defgroup org nil
  536. "Outline-based notes management and organizer."
  537. :tag "Org"
  538. :group 'outlines
  539. :group 'calendar)
  540. (defcustom org-mode-hook nil
  541. "Mode hook for Org-mode, run after the mode was turned on."
  542. :group 'org
  543. :type 'hook)
  544. (defcustom org-load-hook nil
  545. "Hook that is run after org.el has been loaded."
  546. :group 'org
  547. :type 'hook)
  548. (defcustom org-log-buffer-setup-hook nil
  549. "Hook that is run after an Org log buffer is created."
  550. :group 'org
  551. :version "24.1"
  552. :type 'hook)
  553. (defvar org-modules) ; defined below
  554. (defvar org-modules-loaded nil
  555. "Have the modules been loaded already?")
  556. (defun org-load-modules-maybe (&optional force)
  557. "Load all extensions listed in `org-modules'."
  558. (when (or force (not org-modules-loaded))
  559. (mapc (lambda (ext)
  560. (condition-case nil (require ext)
  561. (error (message "Problems while trying to load feature `%s'" ext))))
  562. org-modules)
  563. (setq org-modules-loaded t)))
  564. (defun org-set-modules (var value)
  565. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  566. (set var value)
  567. (when (featurep 'org)
  568. (org-load-modules-maybe 'force)
  569. (org-element-cache-reset 'all)))
  570. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  571. "Modules that should always be loaded together with org.el.
  572. If a description starts with <C>, the file is not part of Emacs
  573. and loading it will require that you have downloaded and properly
  574. installed the Org mode distribution.
  575. You can also use this system to load external packages (i.e. neither Org
  576. core modules, nor modules from the CONTRIB directory). Just add symbols
  577. to the end of the list. If the package is called org-xyz.el, then you need
  578. to add the symbol `xyz', and the package must have a call to:
  579. \(provide 'org-xyz)
  580. For export specific modules, see also `org-export-backends'."
  581. :group 'org
  582. :set 'org-set-modules
  583. :version "24.4"
  584. :package-version '(Org . "8.0")
  585. :type
  586. '(set :greedy t
  587. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  588. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  589. (const :tag " crypt: Encryption of subtrees" org-crypt)
  590. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  591. (const :tag " docview: Links to doc-view buffers" org-docview)
  592. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  593. (const :tag " habit: Track your consistency with habits" org-habit)
  594. (const :tag " id: Global IDs for identifying entries" org-id)
  595. (const :tag " info: Links to Info nodes" org-info)
  596. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  597. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  598. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  599. (const :tag " mouse: Additional mouse support" org-mouse)
  600. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  601. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  602. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  603. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  604. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  605. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  606. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  607. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  608. (const :tag "C collector: Collect properties into tables" org-collector)
  609. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  610. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  611. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  612. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  613. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  614. (const :tag "C eval: Include command output as text" org-eval)
  615. (const :tag "C eww: Store link to url of eww" org-eww)
  616. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  617. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  618. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  619. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  620. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  621. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  622. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  623. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  624. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  625. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  626. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  627. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  628. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  629. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  630. (const :tag "C registry: A registry for Org-mode links" org-registry)
  631. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  632. (const :tag "C secretary: Team management with org-mode" org-secretary)
  633. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  634. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  635. (const :tag "C track: Keep up with Org-mode development" org-track)
  636. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  637. (const :tag "C vm: Links to VM folders/messages" org-vm)
  638. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  639. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  640. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  641. (defvar org-export-registered-backends) ; From ox.el.
  642. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  643. (declare-function org-export-backend-name "ox" (backend))
  644. (defcustom org-export-backends '(ascii html icalendar latex)
  645. "List of export back-ends that should be always available.
  646. If a description starts with <C>, the file is not part of Emacs
  647. and loading it will require that you have downloaded and properly
  648. installed the Org mode distribution.
  649. Unlike to `org-modules', libraries in this list will not be
  650. loaded along with Org, but only once the export framework is
  651. needed.
  652. This variable needs to be set before org.el is loaded. If you
  653. need to make a change while Emacs is running, use the customize
  654. interface or run the following code, where VAL stands for the new
  655. value of the variable, after updating it:
  656. \(progn
  657. \(setq org-export-registered-backends
  658. \(org-remove-if-not
  659. \(lambda (backend)
  660. \(let ((name (org-export-backend-name backend)))
  661. \(or (memq name val)
  662. \(catch 'parentp
  663. \(dolist (b val)
  664. \(and (org-export-derived-backend-p b name)
  665. \(throw 'parentp t)))))))
  666. org-export-registered-backends))
  667. \(let ((new-list (mapcar #'org-export-backend-name
  668. org-export-registered-backends)))
  669. \(dolist (backend val)
  670. \(cond
  671. \((not (load (format \"ox-%s\" backend) t t))
  672. \(message \"Problems while trying to load export back-end `%s'\"
  673. backend))
  674. \((not (memq backend new-list)) (push backend new-list))))
  675. \(set-default 'org-export-backends new-list)))
  676. Adding a back-end to this list will also pull the back-end it
  677. depends on, if any."
  678. :group 'org
  679. :group 'org-export
  680. :version "24.4"
  681. :package-version '(Org . "8.0")
  682. :initialize 'custom-initialize-set
  683. :set (lambda (var val)
  684. (if (not (featurep 'ox)) (set-default var val)
  685. ;; Any back-end not required anymore (not present in VAL and not
  686. ;; a parent of any back-end in the new value) is removed from the
  687. ;; list of registered back-ends.
  688. (setq org-export-registered-backends
  689. (org-remove-if-not
  690. (lambda (backend)
  691. (let ((name (org-export-backend-name backend)))
  692. (or (memq name val)
  693. (catch 'parentp
  694. (dolist (b val)
  695. (and (org-export-derived-backend-p b name)
  696. (throw 'parentp t)))))))
  697. org-export-registered-backends))
  698. ;; Now build NEW-LIST of both new back-ends and required
  699. ;; parents.
  700. (let ((new-list (mapcar #'org-export-backend-name
  701. org-export-registered-backends)))
  702. (dolist (backend val)
  703. (cond
  704. ((not (load (format "ox-%s" backend) t t))
  705. (message "Problems while trying to load export back-end `%s'"
  706. backend))
  707. ((not (memq backend new-list)) (push backend new-list))))
  708. ;; Set VAR to that list with fixed dependencies.
  709. (set-default var new-list))))
  710. :type '(set :greedy t
  711. (const :tag " ascii Export buffer to ASCII format" ascii)
  712. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  713. (const :tag " html Export buffer to HTML format" html)
  714. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  715. (const :tag " latex Export buffer to LaTeX format" latex)
  716. (const :tag " man Export buffer to MAN format" man)
  717. (const :tag " md Export buffer to Markdown format" md)
  718. (const :tag " odt Export buffer to ODT format" odt)
  719. (const :tag " org Export buffer to Org format" org)
  720. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  721. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  722. (const :tag "C deck Export buffer to deck.js presentations" deck)
  723. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  724. (const :tag "C groff Export buffer to Groff format" groff)
  725. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  726. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  727. (const :tag "C s5 Export buffer to s5 presentations" s5)
  728. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  729. (eval-after-load 'ox
  730. '(mapc
  731. (lambda (backend)
  732. (condition-case nil (require (intern (format "ox-%s" backend)))
  733. (error (message "Problems while trying to load export back-end `%s'"
  734. backend))))
  735. org-export-backends))
  736. (defcustom org-support-shift-select nil
  737. "Non-nil means make shift-cursor commands select text when possible.
  738. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  739. start selecting a region, or enlarge regions started in this way.
  740. In Org-mode, in special contexts, these same keys are used for
  741. other purposes, important enough to compete with shift selection.
  742. Org tries to balance these needs by supporting `shift-select-mode'
  743. outside these special contexts, under control of this variable.
  744. The default of this variable is nil, to avoid confusing behavior. Shifted
  745. cursor keys will then execute Org commands in the following contexts:
  746. - on a headline, changing TODO state (left/right) and priority (up/down)
  747. - on a time stamp, changing the time
  748. - in a plain list item, changing the bullet type
  749. - in a property definition line, switching between allowed values
  750. - in the BEGIN line of a clock table (changing the time block).
  751. Outside these contexts, the commands will throw an error.
  752. When this variable is t and the cursor is not in a special
  753. context, Org-mode will support shift-selection for making and
  754. enlarging regions. To make this more effective, the bullet
  755. cycling will no longer happen anywhere in an item line, but only
  756. if the cursor is exactly on the bullet.
  757. If you set this variable to the symbol `always', then the keys
  758. will not be special in headlines, property lines, and item lines,
  759. to make shift selection work there as well. If this is what you
  760. want, you can use the following alternative commands: `C-c C-t'
  761. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  762. can be used to switch TODO sets, `C-c -' to cycle item bullet
  763. types, and properties can be edited by hand or in column view.
  764. However, when the cursor is on a timestamp, shift-cursor commands
  765. will still edit the time stamp - this is just too good to give up.
  766. XEmacs user should have this variable set to nil, because
  767. `shift-select-mode' is in Emacs 23 or later only."
  768. :group 'org
  769. :type '(choice
  770. (const :tag "Never" nil)
  771. (const :tag "When outside special context" t)
  772. (const :tag "Everywhere except timestamps" always)))
  773. (defcustom org-loop-over-headlines-in-active-region nil
  774. "Shall some commands act upon headlines in the active region?
  775. When set to t, some commands will be performed in all headlines
  776. within the active region.
  777. When set to `start-level', some commands will be performed in all
  778. headlines within the active region, provided that these headlines
  779. are of the same level than the first one.
  780. When set to a string, those commands will be performed on the
  781. matching headlines within the active region. Such string must be
  782. a tags/property/todo match as it is used in the agenda tags view.
  783. The list of commands is: `org-schedule', `org-deadline',
  784. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  785. `org-archive-to-archive-sibling'. The archiving commands skip
  786. already archived entries."
  787. :type '(choice (const :tag "Don't loop" nil)
  788. (const :tag "All headlines in active region" t)
  789. (const :tag "In active region, headlines at the same level than the first one" start-level)
  790. (string :tag "Tags/Property/Todo matcher"))
  791. :version "24.1"
  792. :group 'org-todo
  793. :group 'org-archive)
  794. (defgroup org-startup nil
  795. "Options concerning startup of Org-mode."
  796. :tag "Org Startup"
  797. :group 'org)
  798. (defcustom org-startup-folded t
  799. "Non-nil means entering Org-mode will switch to OVERVIEW.
  800. This can also be configured on a per-file basis by adding one of
  801. the following lines anywhere in the buffer:
  802. #+STARTUP: fold (or `overview', this is equivalent)
  803. #+STARTUP: nofold (or `showall', this is equivalent)
  804. #+STARTUP: content
  805. #+STARTUP: showeverything
  806. By default, this option is ignored when Org opens agenda files
  807. for the first time. If you want the agenda to honor the startup
  808. option, set `org-agenda-inhibit-startup' to nil."
  809. :group 'org-startup
  810. :type '(choice
  811. (const :tag "nofold: show all" nil)
  812. (const :tag "fold: overview" t)
  813. (const :tag "content: all headlines" content)
  814. (const :tag "show everything, even drawers" showeverything)))
  815. (defcustom org-startup-truncated t
  816. "Non-nil means entering Org-mode will set `truncate-lines'.
  817. This is useful since some lines containing links can be very long and
  818. uninteresting. Also tables look terrible when wrapped."
  819. :group 'org-startup
  820. :type 'boolean)
  821. (defcustom org-startup-indented nil
  822. "Non-nil means turn on `org-indent-mode' on startup.
  823. This can also be configured on a per-file basis by adding one of
  824. the following lines anywhere in the buffer:
  825. #+STARTUP: indent
  826. #+STARTUP: noindent"
  827. :group 'org-structure
  828. :type '(choice
  829. (const :tag "Not" nil)
  830. (const :tag "Globally (slow on startup in large files)" t)))
  831. (defcustom org-use-sub-superscripts t
  832. "Non-nil means interpret \"_\" and \"^\" for display.
  833. If you want to control how Org exports those characters, see
  834. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  835. used to be an alias for `org-export-with-sub-superscripts' in
  836. Org <8.0, it is not anymore.
  837. When this option is turned on, you can use TeX-like syntax for
  838. sub- and superscripts within the buffer. Several characters after
  839. \"_\" or \"^\" will be considered as a single item - so grouping
  840. with {} is normally not needed. For example, the following things
  841. will be parsed as single sub- or superscripts:
  842. 10^24 or 10^tau several digits will be considered 1 item.
  843. 10^-12 or 10^-tau a leading sign with digits or a word
  844. x^2-y^3 will be read as x^2 - y^3, because items are
  845. terminated by almost any nonword/nondigit char.
  846. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  847. Still, ambiguity is possible. So when in doubt, use {} to enclose
  848. the sub/superscript. If you set this variable to the symbol `{}',
  849. the braces are *required* in order to trigger interpretations as
  850. sub/superscript. This can be helpful in documents that need \"_\"
  851. frequently in plain text."
  852. :group 'org-startup
  853. :version "24.4"
  854. :package-version '(Org . "8.0")
  855. :type '(choice
  856. (const :tag "Always interpret" t)
  857. (const :tag "Only with braces" {})
  858. (const :tag "Never interpret" nil)))
  859. (defcustom org-startup-with-beamer-mode nil
  860. "Non-nil means turn on `org-beamer-mode' on startup.
  861. This can also be configured on a per-file basis by adding one of
  862. the following lines anywhere in the buffer:
  863. #+STARTUP: beamer"
  864. :group 'org-startup
  865. :version "24.1"
  866. :type 'boolean)
  867. (defcustom org-startup-align-all-tables nil
  868. "Non-nil means align all tables when visiting a file.
  869. This is useful when the column width in tables is forced with <N> cookies
  870. in table fields. Such tables will look correct only after the first re-align.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: align
  874. #+STARTUP: noalign"
  875. :group 'org-startup
  876. :type 'boolean)
  877. (defcustom org-startup-with-inline-images nil
  878. "Non-nil means show inline images when loading a new Org file.
  879. This can also be configured on a per-file basis by adding one of
  880. the following lines anywhere in the buffer:
  881. #+STARTUP: inlineimages
  882. #+STARTUP: noinlineimages"
  883. :group 'org-startup
  884. :version "24.1"
  885. :type 'boolean)
  886. (defcustom org-startup-with-latex-preview nil
  887. "Non-nil means preview LaTeX fragments when loading a new Org file.
  888. This can also be configured on a per-file basis by adding one of
  889. the following lines anywhere in the buffer:
  890. #+STARTUP: latexpreview
  891. #+STARTUP: nolatexpreview"
  892. :group 'org-startup
  893. :version "24.4"
  894. :package-version '(Org . "8.0")
  895. :type 'boolean)
  896. (defcustom org-insert-mode-line-in-empty-file nil
  897. "Non-nil means insert the first line setting Org-mode in empty files.
  898. When the function `org-mode' is called interactively in an empty file, this
  899. normally means that the file name does not automatically trigger Org-mode.
  900. To ensure that the file will always be in Org-mode in the future, a
  901. line enforcing Org-mode will be inserted into the buffer, if this option
  902. has been set."
  903. :group 'org-startup
  904. :type 'boolean)
  905. (defcustom org-replace-disputed-keys nil
  906. "Non-nil means use alternative key bindings for some keys.
  907. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  908. These keys are also used by other packages like shift-selection-mode'
  909. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  910. If you want to use Org-mode together with one of these other modes,
  911. or more generally if you would like to move some Org-mode commands to
  912. other keys, set this variable and configure the keys with the variable
  913. `org-disputed-keys'.
  914. This option is only relevant at load-time of Org-mode, and must be set
  915. *before* org.el is loaded. Changing it requires a restart of Emacs to
  916. become effective."
  917. :group 'org-startup
  918. :type 'boolean)
  919. (defcustom org-use-extra-keys nil
  920. "Non-nil means use extra key sequence definitions for certain commands.
  921. This happens automatically if you run XEmacs or if `window-system'
  922. is nil. This variable lets you do the same manually. You must
  923. set it before loading org.
  924. Example: on Carbon Emacs 22 running graphically, with an external
  925. keyboard on a Powerbook, the default way of setting M-left might
  926. not work for either Alt or ESC. Setting this variable will make
  927. it work for ESC."
  928. :group 'org-startup
  929. :type 'boolean)
  930. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  931. (defcustom org-disputed-keys
  932. '(([(shift up)] . [(meta p)])
  933. ([(shift down)] . [(meta n)])
  934. ([(shift left)] . [(meta -)])
  935. ([(shift right)] . [(meta +)])
  936. ([(control shift right)] . [(meta shift +)])
  937. ([(control shift left)] . [(meta shift -)]))
  938. "Keys for which Org-mode and other modes compete.
  939. This is an alist, cars are the default keys, second element specifies
  940. the alternative to use when `org-replace-disputed-keys' is t.
  941. Keys can be specified in any syntax supported by `define-key'.
  942. The value of this option takes effect only at Org-mode's startup,
  943. therefore you'll have to restart Emacs to apply it after changing."
  944. :group 'org-startup
  945. :type 'alist)
  946. (defun org-key (key)
  947. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  948. Or return the original if not disputed.
  949. Also apply the translations defined in `org-xemacs-key-equivalents'."
  950. (when org-replace-disputed-keys
  951. (let* ((nkey (key-description key))
  952. (x (org-find-if (lambda (x)
  953. (equal (key-description (car x)) nkey))
  954. org-disputed-keys)))
  955. (setq key (if x (cdr x) key))))
  956. (when (featurep 'xemacs)
  957. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  958. key)
  959. (defun org-find-if (predicate seq)
  960. (catch 'exit
  961. (while seq
  962. (if (funcall predicate (car seq))
  963. (throw 'exit (car seq))
  964. (pop seq)))))
  965. (defun org-defkey (keymap key def)
  966. "Define a key, possibly translated, as returned by `org-key'."
  967. (define-key keymap (org-key key) def))
  968. (defcustom org-ellipsis nil
  969. "The ellipsis to use in the Org-mode outline.
  970. When nil, just use the standard three dots.
  971. When a string, use that string instead.
  972. When a face, use the standard 3 dots, but with the specified face.
  973. The change affects only Org-mode (which will then use its own display table).
  974. Changing this requires executing \\[org-mode] in a buffer to become
  975. effective."
  976. :group 'org-startup
  977. :type '(choice (const :tag "Default" nil)
  978. (face :tag "Face" :value org-warning)
  979. (string :tag "String" :value "...#")))
  980. (defvar org-display-table nil
  981. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  982. (defgroup org-keywords nil
  983. "Keywords in Org-mode."
  984. :tag "Org Keywords"
  985. :group 'org)
  986. (defcustom org-closed-keep-when-no-todo nil
  987. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  988. :group 'org-todo
  989. :group 'org-keywords
  990. :version "24.4"
  991. :package-version '(Org . "8.0")
  992. :type 'boolean)
  993. (defgroup org-structure nil
  994. "Options concerning the general structure of Org-mode files."
  995. :tag "Org Structure"
  996. :group 'org)
  997. (defgroup org-reveal-location nil
  998. "Options about how to make context of a location visible."
  999. :tag "Org Reveal Location"
  1000. :group 'org-structure)
  1001. (defcustom org-show-context-detail '((isearch . lineage)
  1002. (bookmark-jump . lineage)
  1003. (default . ancestors))
  1004. "Alist between context and visibility span when revealing a location.
  1005. \\<org-mode-map>Some actions may move point into invisible
  1006. locations. As a consequence, Org always expose a neighborhood
  1007. around point. How much is shown depends on the initial action,
  1008. or context. Valid contexts are
  1009. agenda when exposing an entry from the agenda
  1010. org-goto when using the command `org-goto' (\\[org-goto])
  1011. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1012. tags-tree when constructing a sparse tree based on tags matches
  1013. link-search when exposing search matches associated with a link
  1014. mark-goto when exposing the jump goal of a mark
  1015. bookmark-jump when exposing a bookmark location
  1016. isearch when exiting from an incremental search
  1017. default default for all contexts not set explicitly
  1018. Allowed visibility spans are
  1019. minimal show current headline; if point is not on headline,
  1020. also show entry
  1021. local show current headline, entry and next headline
  1022. ancestors show current headline and its direct ancestors; if
  1023. point is not on headline, also show entry
  1024. lineage show current headline, its direct ancestors and all
  1025. their children; if point is not on headline, also show
  1026. entry and first child
  1027. tree show current headline, its direct ancestors and all
  1028. their children; if point is not on headline, also show
  1029. entry and all children
  1030. canonical show current headline, its direct ancestors along with
  1031. their entries and children; if point is not located on
  1032. the headline, also show current entry and all children
  1033. As special cases, a nil or t value means show all contexts in
  1034. `minimal' or `canonical' view, respectively.
  1035. Some views can make displayed information very compact, but also
  1036. make it harder to edit the location of the match. In such
  1037. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1038. more context."
  1039. :group 'org-reveal-location
  1040. :version "25.1"
  1041. :package-version '(Org . "8.3")
  1042. :type '(choice
  1043. (const :tag "Canonical" t)
  1044. (const :tag "Minimal" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const agenda)
  1049. (const org-goto)
  1050. (const occur-tree)
  1051. (const tags-tree)
  1052. (const link-search)
  1053. (const mark-goto)
  1054. (const bookmark-jump)
  1055. (const isearch)
  1056. (const default))
  1057. (choice :tag "Detail level"
  1058. (const minimal)
  1059. (const local)
  1060. (const ancestors)
  1061. (const lineage)
  1062. (const tree)
  1063. (const canonical))))))
  1064. (defcustom org-indirect-buffer-display 'other-window
  1065. "How should indirect tree buffers be displayed?
  1066. This applies to indirect buffers created with the commands
  1067. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1068. Valid values are:
  1069. current-window Display in the current window
  1070. other-window Just display in another window.
  1071. dedicated-frame Create one new frame, and re-use it each time.
  1072. new-frame Make a new frame each time. Note that in this case
  1073. previously-made indirect buffers are kept, and you need to
  1074. kill these buffers yourself."
  1075. :group 'org-structure
  1076. :group 'org-agenda-windows
  1077. :type '(choice
  1078. (const :tag "In current window" current-window)
  1079. (const :tag "In current frame, other window" other-window)
  1080. (const :tag "Each time a new frame" new-frame)
  1081. (const :tag "One dedicated frame" dedicated-frame)))
  1082. (defcustom org-use-speed-commands nil
  1083. "Non-nil means activate single letter commands at beginning of a headline.
  1084. This may also be a function to test for appropriate locations where speed
  1085. commands should be active.
  1086. For example, to activate speed commands when the point is on any
  1087. star at the beginning of the headline, you can do this:
  1088. (setq org-use-speed-commands
  1089. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1090. :group 'org-structure
  1091. :type '(choice
  1092. (const :tag "Never" nil)
  1093. (const :tag "At beginning of headline stars" t)
  1094. (function)))
  1095. (defcustom org-speed-commands-user nil
  1096. "Alist of additional speed commands.
  1097. This list will be checked before `org-speed-commands-default'
  1098. when the variable `org-use-speed-commands' is non-nil
  1099. and when the cursor is at the beginning of a headline.
  1100. The car if each entry is a string with a single letter, which must
  1101. be assigned to `self-insert-command' in the global map.
  1102. The cdr is either a command to be called interactively, a function
  1103. to be called, or a form to be evaluated.
  1104. An entry that is just a list with a single string will be interpreted
  1105. as a descriptive headline that will be added when listing the speed
  1106. commands in the Help buffer using the `?' speed command."
  1107. :group 'org-structure
  1108. :type '(repeat :value ("k" . ignore)
  1109. (choice :value ("k" . ignore)
  1110. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1111. (cons :tag "Letter and Command"
  1112. (string :tag "Command letter")
  1113. (choice
  1114. (function)
  1115. (sexp))))))
  1116. (defcustom org-bookmark-names-plist
  1117. '(:last-capture "org-capture-last-stored"
  1118. :last-refile "org-refile-last-stored"
  1119. :last-capture-marker "org-capture-last-stored-marker")
  1120. "Names for bookmarks automatically set by some Org commands.
  1121. This can provide strings as names for a number of bookmarks Org sets
  1122. automatically. The following keys are currently implemented:
  1123. :last-capture
  1124. :last-capture-marker
  1125. :last-refile
  1126. When a key does not show up in the property list, the corresponding bookmark
  1127. is not set."
  1128. :group 'org-structure
  1129. :type 'plist)
  1130. (defgroup org-cycle nil
  1131. "Options concerning visibility cycling in Org-mode."
  1132. :tag "Org Cycle"
  1133. :group 'org-structure)
  1134. (defcustom org-cycle-skip-children-state-if-no-children t
  1135. "Non-nil means skip CHILDREN state in entries that don't have any."
  1136. :group 'org-cycle
  1137. :type 'boolean)
  1138. (defcustom org-cycle-max-level nil
  1139. "Maximum level which should still be subject to visibility cycling.
  1140. Levels higher than this will, for cycling, be treated as text, not a headline.
  1141. When `org-odd-levels-only' is set, a value of N in this variable actually
  1142. means 2N-1 stars as the limiting headline.
  1143. When nil, cycle all levels.
  1144. Note that the limiting level of cycling is also influenced by
  1145. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1146. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1147. than its value."
  1148. :group 'org-cycle
  1149. :type '(choice
  1150. (const :tag "No limit" nil)
  1151. (integer :tag "Maximum level")))
  1152. (defcustom org-hide-block-startup nil
  1153. "Non-nil means entering Org-mode will fold all blocks.
  1154. This can also be set in on a per-file basis with
  1155. #+STARTUP: hideblocks
  1156. #+STARTUP: showblocks"
  1157. :group 'org-startup
  1158. :group 'org-cycle
  1159. :type 'boolean)
  1160. (defcustom org-cycle-global-at-bob nil
  1161. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1162. This makes it possible to do global cycling without having to use S-TAB or
  1163. \\[universal-argument] TAB. For this special case to work, the first line
  1164. of the buffer must not be a headline -- it may be empty or some other text.
  1165. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1166. sure the cursor stays at the beginning of the buffer. When this option is
  1167. nil, don't do anything special at the beginning of the buffer."
  1168. :group 'org-cycle
  1169. :type 'boolean)
  1170. (defcustom org-cycle-level-after-item/entry-creation t
  1171. "Non-nil means cycle entry level or item indentation in new empty entries.
  1172. When the cursor is at the end of an empty headline, i.e., with only stars
  1173. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1174. and then all possible ancestor states, before returning to the original state.
  1175. This makes data entry extremely fast: M-RET to create a new headline,
  1176. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1177. When the cursor is at the end of an empty plain list item, one TAB will
  1178. make it a subitem, two or more tabs will back up to make this an item
  1179. higher up in the item hierarchy."
  1180. :group 'org-cycle
  1181. :type 'boolean)
  1182. (defcustom org-cycle-emulate-tab t
  1183. "Where should `org-cycle' emulate TAB.
  1184. nil Never
  1185. white Only in completely white lines
  1186. whitestart Only at the beginning of lines, before the first non-white char
  1187. t Everywhere except in headlines
  1188. exc-hl-bol Everywhere except at the start of a headline
  1189. If TAB is used in a place where it does not emulate TAB, the current subtree
  1190. visibility is cycled."
  1191. :group 'org-cycle
  1192. :type '(choice (const :tag "Never" nil)
  1193. (const :tag "Only in completely white lines" white)
  1194. (const :tag "Before first char in a line" whitestart)
  1195. (const :tag "Everywhere except in headlines" t)
  1196. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1197. (defcustom org-cycle-separator-lines 2
  1198. "Number of empty lines needed to keep an empty line between collapsed trees.
  1199. If you leave an empty line between the end of a subtree and the following
  1200. headline, this empty line is hidden when the subtree is folded.
  1201. Org-mode will leave (exactly) one empty line visible if the number of
  1202. empty lines is equal or larger to the number given in this variable.
  1203. So the default 2 means at least 2 empty lines after the end of a subtree
  1204. are needed to produce free space between a collapsed subtree and the
  1205. following headline.
  1206. If the number is negative, and the number of empty lines is at least -N,
  1207. all empty lines are shown.
  1208. Special case: when 0, never leave empty lines in collapsed view."
  1209. :group 'org-cycle
  1210. :type 'integer)
  1211. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1212. (defcustom org-pre-cycle-hook nil
  1213. "Hook that is run before visibility cycling is happening.
  1214. The function(s) in this hook must accept a single argument which indicates
  1215. the new state that will be set right after running this hook. The
  1216. argument is a symbol. Before a global state change, it can have the values
  1217. `overview', `content', or `all'. Before a local state change, it can have
  1218. the values `folded', `children', or `subtree'."
  1219. :group 'org-cycle
  1220. :type 'hook)
  1221. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1222. org-cycle-hide-drawers
  1223. org-cycle-show-empty-lines
  1224. org-optimize-window-after-visibility-change)
  1225. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1226. The function(s) in this hook must accept a single argument which indicates
  1227. the new state that was set by the most recent `org-cycle' command. The
  1228. argument is a symbol. After a global state change, it can have the values
  1229. `overview', `contents', or `all'. After a local state change, it can have
  1230. the values `folded', `children', or `subtree'."
  1231. :group 'org-cycle
  1232. :type 'hook
  1233. :version "25.1"
  1234. :package-version '(Org . "8.3"))
  1235. (defgroup org-edit-structure nil
  1236. "Options concerning structure editing in Org-mode."
  1237. :tag "Org Edit Structure"
  1238. :group 'org-structure)
  1239. (defcustom org-odd-levels-only nil
  1240. "Non-nil means skip even levels and only use odd levels for the outline.
  1241. This has the effect that two stars are being added/taken away in
  1242. promotion/demotion commands. It also influences how levels are
  1243. handled by the exporters.
  1244. Changing it requires restart of `font-lock-mode' to become effective
  1245. for fontification also in regions already fontified.
  1246. You may also set this on a per-file basis by adding one of the following
  1247. lines to the buffer:
  1248. #+STARTUP: odd
  1249. #+STARTUP: oddeven"
  1250. :group 'org-edit-structure
  1251. :group 'org-appearance
  1252. :type 'boolean)
  1253. (defcustom org-adapt-indentation t
  1254. "Non-nil means adapt indentation to outline node level.
  1255. When this variable is set, Org assumes that you write outlines by
  1256. indenting text in each node to align with the headline (after the
  1257. stars). The following issues are influenced by this variable:
  1258. - The indentation is increased by one space in a demotion
  1259. command, and decreased by one in a promotion command. However,
  1260. in the latter case, if shifting some line in the entry body
  1261. would alter document structure (e.g., insert a new headline),
  1262. indentation is not changed at all.
  1263. - Property drawers and planning information is inserted indented
  1264. when this variable is set. When nil, they will not be indented.
  1265. - TAB indents a line relative to current level. The lines below
  1266. a headline will be indented when this variable is set.
  1267. Note that this is all about true indentation, by adding and
  1268. removing space characters. See also `org-indent.el' which does
  1269. level-dependent indentation in a virtual way, i.e. at display
  1270. time in Emacs."
  1271. :group 'org-edit-structure
  1272. :type 'boolean)
  1273. (defcustom org-special-ctrl-a/e nil
  1274. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1275. When t, `C-a' will bring back the cursor to the beginning of the
  1276. headline text, i.e. after the stars and after a possible TODO
  1277. keyword. In an item, this will be the position after bullet and
  1278. check-box, if any. When the cursor is already at that position,
  1279. another `C-a' will bring it to the beginning of the line.
  1280. `C-e' will jump to the end of the headline, ignoring the presence
  1281. of tags in the headline. A second `C-e' will then jump to the
  1282. true end of the line, after any tags. This also means that, when
  1283. this variable is non-nil, `C-e' also will never jump beyond the
  1284. end of the heading of a folded section, i.e. not after the
  1285. ellipses.
  1286. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1287. normally, going to the true line boundary first. Only a directly
  1288. following, identical keypress will bring the cursor to the
  1289. special positions.
  1290. This may also be a cons cell where the behavior for `C-a' and
  1291. `C-e' is set separately."
  1292. :group 'org-edit-structure
  1293. :type '(choice
  1294. (const :tag "off" nil)
  1295. (const :tag "on: after stars/bullet and before tags first" t)
  1296. (const :tag "reversed: true line boundary first" reversed)
  1297. (cons :tag "Set C-a and C-e separately"
  1298. (choice :tag "Special C-a"
  1299. (const :tag "off" nil)
  1300. (const :tag "on: after stars/bullet first" t)
  1301. (const :tag "reversed: before stars/bullet first" reversed))
  1302. (choice :tag "Special C-e"
  1303. (const :tag "off" nil)
  1304. (const :tag "on: before tags first" t)
  1305. (const :tag "reversed: after tags first" reversed)))))
  1306. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1307. (defcustom org-special-ctrl-k nil
  1308. "Non-nil means `C-k' will behave specially in headlines.
  1309. When nil, `C-k' will call the default `kill-line' command.
  1310. When t, the following will happen while the cursor is in the headline:
  1311. - When the cursor is at the beginning of a headline, kill the entire
  1312. line and possible the folded subtree below the line.
  1313. - When in the middle of the headline text, kill the headline up to the tags.
  1314. - When after the headline text, kill the tags."
  1315. :group 'org-edit-structure
  1316. :type 'boolean)
  1317. (defcustom org-ctrl-k-protect-subtree nil
  1318. "Non-nil means, do not delete a hidden subtree with C-k.
  1319. When set to the symbol `error', simply throw an error when C-k is
  1320. used to kill (part-of) a headline that has hidden text behind it.
  1321. Any other non-nil value will result in a query to the user, if it is
  1322. OK to kill that hidden subtree. When nil, kill without remorse."
  1323. :group 'org-edit-structure
  1324. :version "24.1"
  1325. :type '(choice
  1326. (const :tag "Do not protect hidden subtrees" nil)
  1327. (const :tag "Protect hidden subtrees with a security query" t)
  1328. (const :tag "Never kill a hidden subtree with C-k" error)))
  1329. (defcustom org-special-ctrl-o t
  1330. "Non-nil means, make `C-o' insert a row in tables."
  1331. :group 'org-edit-structure
  1332. :type 'boolean)
  1333. (defcustom org-catch-invisible-edits nil
  1334. "Check if in invisible region before inserting or deleting a character.
  1335. Valid values are:
  1336. nil Do not check, so just do invisible edits.
  1337. error Throw an error and do nothing.
  1338. show Make point visible, and do the requested edit.
  1339. show-and-error Make point visible, then throw an error and abort the edit.
  1340. smart Make point visible, and do insertion/deletion if it is
  1341. adjacent to visible text and the change feels predictable.
  1342. Never delete a previously invisible character or add in the
  1343. middle or right after an invisible region. Basically, this
  1344. allows insertion and backward-delete right before ellipses.
  1345. FIXME: maybe in this case we should not even show?"
  1346. :group 'org-edit-structure
  1347. :version "24.1"
  1348. :type '(choice
  1349. (const :tag "Do not check" nil)
  1350. (const :tag "Throw error when trying to edit" error)
  1351. (const :tag "Unhide, but do not do the edit" show-and-error)
  1352. (const :tag "Show invisible part and do the edit" show)
  1353. (const :tag "Be smart and do the right thing" smart)))
  1354. (defcustom org-yank-folded-subtrees t
  1355. "Non-nil means when yanking subtrees, fold them.
  1356. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1357. it starts with a heading and all other headings in it are either children
  1358. or siblings, then fold all the subtrees. However, do this only if no
  1359. text after the yank would be swallowed into a folded tree by this action."
  1360. :group 'org-edit-structure
  1361. :type 'boolean)
  1362. (defcustom org-yank-adjusted-subtrees nil
  1363. "Non-nil means when yanking subtrees, adjust the level.
  1364. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1365. this function for details."
  1366. :group 'org-edit-structure
  1367. :type 'boolean)
  1368. (defcustom org-M-RET-may-split-line '((default . t))
  1369. "Non-nil means M-RET will split the line at the cursor position.
  1370. When nil, it will go to the end of the line before making a
  1371. new line.
  1372. You may also set this option in a different way for different
  1373. contexts. Valid contexts are:
  1374. headline when creating a new headline
  1375. item when creating a new item
  1376. table in a table field
  1377. default the value to be used for all contexts not explicitly
  1378. customized"
  1379. :group 'org-structure
  1380. :group 'org-table
  1381. :type '(choice
  1382. (const :tag "Always" t)
  1383. (const :tag "Never" nil)
  1384. (repeat :greedy t :tag "Individual contexts"
  1385. (cons
  1386. (choice :tag "Context"
  1387. (const headline)
  1388. (const item)
  1389. (const table)
  1390. (const default))
  1391. (boolean)))))
  1392. (defcustom org-insert-heading-respect-content nil
  1393. "Non-nil means insert new headings after the current subtree.
  1394. When nil, the new heading is created directly after the current line.
  1395. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1396. this variable on for the duration of the command."
  1397. :group 'org-structure
  1398. :type 'boolean)
  1399. (defcustom org-blank-before-new-entry '((heading . auto)
  1400. (plain-list-item . auto))
  1401. "Should `org-insert-heading' leave a blank line before new heading/item?
  1402. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1403. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1404. which case Org will look at the surrounding headings/items and try to
  1405. make an intelligent decision whether to insert a blank line or not.
  1406. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1407. the setting here is ignored and no empty line is inserted to avoid breaking
  1408. the list structure."
  1409. :group 'org-edit-structure
  1410. :type '(list
  1411. (cons (const heading)
  1412. (choice (const :tag "Never" nil)
  1413. (const :tag "Always" t)
  1414. (const :tag "Auto" auto)))
  1415. (cons (const plain-list-item)
  1416. (choice (const :tag "Never" nil)
  1417. (const :tag "Always" t)
  1418. (const :tag "Auto" auto)))))
  1419. (defcustom org-insert-heading-hook nil
  1420. "Hook being run after inserting a new heading."
  1421. :group 'org-edit-structure
  1422. :type 'hook)
  1423. (defcustom org-enable-fixed-width-editor t
  1424. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1425. This currently only means they are never auto-wrapped.
  1426. When nil, such lines will be treated like ordinary lines."
  1427. :group 'org-edit-structure
  1428. :type 'boolean)
  1429. (defcustom org-goto-auto-isearch t
  1430. "Non-nil means typing characters in `org-goto' starts incremental search.
  1431. When nil, you can use these keybindings to navigate the buffer:
  1432. q Quit the org-goto interface
  1433. n Go to the next visible heading
  1434. p Go to the previous visible heading
  1435. f Go one heading forward on same level
  1436. b Go one heading backward on same level
  1437. u Go one heading up"
  1438. :group 'org-edit-structure
  1439. :type 'boolean)
  1440. (defgroup org-sparse-trees nil
  1441. "Options concerning sparse trees in Org-mode."
  1442. :tag "Org Sparse Trees"
  1443. :group 'org-structure)
  1444. (defcustom org-highlight-sparse-tree-matches t
  1445. "Non-nil means highlight all matches that define a sparse tree.
  1446. The highlights will automatically disappear the next time the buffer is
  1447. changed by an edit command."
  1448. :group 'org-sparse-trees
  1449. :type 'boolean)
  1450. (defcustom org-remove-highlights-with-change t
  1451. "Non-nil means any change to the buffer will remove temporary highlights.
  1452. Such highlights are created by `org-occur' and `org-clock-display'.
  1453. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1454. The highlights created by `org-toggle-latex-fragment' always need
  1455. `C-c C-x C-l' to be removed."
  1456. :group 'org-sparse-trees
  1457. :group 'org-time
  1458. :type 'boolean)
  1459. (defcustom org-occur-hook '(org-first-headline-recenter)
  1460. "Hook that is run after `org-occur' has constructed a sparse tree.
  1461. This can be used to recenter the window to show as much of the structure
  1462. as possible."
  1463. :group 'org-sparse-trees
  1464. :type 'hook)
  1465. (defgroup org-imenu-and-speedbar nil
  1466. "Options concerning imenu and speedbar in Org-mode."
  1467. :tag "Org Imenu and Speedbar"
  1468. :group 'org-structure)
  1469. (defcustom org-imenu-depth 2
  1470. "The maximum level for Imenu access to Org-mode headlines.
  1471. This also applied for speedbar access."
  1472. :group 'org-imenu-and-speedbar
  1473. :type 'integer)
  1474. (defgroup org-table nil
  1475. "Options concerning tables in Org-mode."
  1476. :tag "Org Table"
  1477. :group 'org)
  1478. (defcustom org-enable-table-editor 'optimized
  1479. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1480. When nil, such lines will be treated like ordinary lines.
  1481. When equal to the symbol `optimized', the table editor will be optimized to
  1482. do the following:
  1483. - Automatic overwrite mode in front of whitespace in table fields.
  1484. This makes the structure of the table stay in tact as long as the edited
  1485. field does not exceed the column width.
  1486. - Minimize the number of realigns. Normally, the table is aligned each time
  1487. TAB or RET are pressed to move to another field. With optimization this
  1488. happens only if changes to a field might have changed the column width.
  1489. Optimization requires replacing the functions `self-insert-command',
  1490. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1491. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1492. very good at guessing when a re-align will be necessary, but you can always
  1493. force one with \\[org-ctrl-c-ctrl-c].
  1494. If you would like to use the optimized version in Org-mode, but the
  1495. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1496. This variable can be used to turn on and off the table editor during a session,
  1497. but in order to toggle optimization, a restart is required.
  1498. See also the variable `org-table-auto-blank-field'."
  1499. :group 'org-table
  1500. :type '(choice
  1501. (const :tag "off" nil)
  1502. (const :tag "on" t)
  1503. (const :tag "on, optimized" optimized)))
  1504. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1505. (version<= emacs-version "24.1"))
  1506. "Non-nil means cluster self-insert commands for undo when possible.
  1507. If this is set, then, like in the Emacs command loop, 20 consecutive
  1508. characters will be undone together.
  1509. This is configurable, because there is some impact on typing performance."
  1510. :group 'org-table
  1511. :type 'boolean)
  1512. (defcustom org-table-tab-recognizes-table.el t
  1513. "Non-nil means TAB will automatically notice a table.el table.
  1514. When it sees such a table, it moves point into it and - if necessary -
  1515. calls `table-recognize-table'."
  1516. :group 'org-table-editing
  1517. :type 'boolean)
  1518. (defgroup org-link nil
  1519. "Options concerning links in Org-mode."
  1520. :tag "Org Link"
  1521. :group 'org)
  1522. (defvar org-link-abbrev-alist-local nil
  1523. "Buffer-local version of `org-link-abbrev-alist', which see.
  1524. The value of this is taken from the #+LINK lines.")
  1525. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1526. (defcustom org-link-abbrev-alist nil
  1527. "Alist of link abbreviations.
  1528. The car of each element is a string, to be replaced at the start of a link.
  1529. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1530. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1531. [[linkkey:tag][description]]
  1532. The 'linkkey' must be a word word, starting with a letter, followed
  1533. by letters, numbers, '-' or '_'.
  1534. If REPLACE is a string, the tag will simply be appended to create the link.
  1535. If the string contains \"%s\", the tag will be inserted there. If the string
  1536. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1537. at that point (see the function `url-hexify-string'). If the string contains
  1538. the specifier \"%(my-function)\", then the custom function `my-function' will
  1539. be invoked: this function takes the tag as its only argument and must return
  1540. a string.
  1541. REPLACE may also be a function that will be called with the tag as the
  1542. only argument to create the link, which should be returned as a string.
  1543. See the manual for examples."
  1544. :group 'org-link
  1545. :type '(repeat
  1546. (cons
  1547. (string :tag "Protocol")
  1548. (choice
  1549. (string :tag "Format")
  1550. (function)))))
  1551. (defcustom org-descriptive-links t
  1552. "Non-nil means Org will display descriptive links.
  1553. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1554. \"Org Website\", hiding the link itself and just displaying its
  1555. description. When set to nil, Org will display the full links
  1556. literally.
  1557. You can interactively set the value of this variable by calling
  1558. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1559. :group 'org-link
  1560. :type 'boolean)
  1561. (defcustom org-link-file-path-type 'adaptive
  1562. "How the path name in file links should be stored.
  1563. Valid values are:
  1564. relative Relative to the current directory, i.e. the directory of the file
  1565. into which the link is being inserted.
  1566. absolute Absolute path, if possible with ~ for home directory.
  1567. noabbrev Absolute path, no abbreviation of home directory.
  1568. adaptive Use relative path for files in the current directory and sub-
  1569. directories of it. For other files, use an absolute path."
  1570. :group 'org-link
  1571. :type '(choice
  1572. (const relative)
  1573. (const absolute)
  1574. (const noabbrev)
  1575. (const adaptive)))
  1576. (defvaralias 'org-activate-links 'org-highlight-links)
  1577. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1578. "Types of links that should be highlighted in Org-mode files.
  1579. This is a list of symbols, each one of them leading to the
  1580. highlighting of a certain link type.
  1581. You can still open links that are not highlighted.
  1582. In principle, it does not hurt to turn on highlighting for all
  1583. link types. There may be a small gain when turning off unused
  1584. link types. The types are:
  1585. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1586. angle Links in angular brackets that may contain whitespace like
  1587. <bbdb:Carsten Dominik>.
  1588. plain Plain links in normal text, no whitespace, like http://google.com.
  1589. radio Text that is matched by a radio target, see manual for details.
  1590. tag Tag settings in a headline (link to tag search).
  1591. date Time stamps (link to calendar).
  1592. footnote Footnote labels.
  1593. If you set this variable during an Emacs session, use `org-mode-restart'
  1594. in the Org buffer so that the change takes effect."
  1595. :group 'org-link
  1596. :group 'org-appearance
  1597. :type '(set :greedy t
  1598. (const :tag "Double bracket links" bracket)
  1599. (const :tag "Angular bracket links" angle)
  1600. (const :tag "Plain text links" plain)
  1601. (const :tag "Radio target matches" radio)
  1602. (const :tag "Tags" tag)
  1603. (const :tag "Timestamps" date)
  1604. (const :tag "Footnotes" footnote)))
  1605. (defcustom org-make-link-description-function nil
  1606. "Function to use for generating link descriptions from links.
  1607. When nil, the link location will be used. This function must take
  1608. two parameters: the first one is the link, the second one is the
  1609. description generated by `org-insert-link'. The function should
  1610. return the description to use."
  1611. :group 'org-link
  1612. :type '(choice (const nil) (function)))
  1613. (defgroup org-link-store nil
  1614. "Options concerning storing links in Org-mode."
  1615. :tag "Org Store Link"
  1616. :group 'org-link)
  1617. (defcustom org-url-hexify-p t
  1618. "When non-nil, hexify URL when creating a link."
  1619. :type 'boolean
  1620. :version "24.3"
  1621. :group 'org-link-store)
  1622. (defcustom org-email-link-description-format "Email %c: %.30s"
  1623. "Format of the description part of a link to an email or usenet message.
  1624. The following %-escapes will be replaced by corresponding information:
  1625. %F full \"From\" field
  1626. %f name, taken from \"From\" field, address if no name
  1627. %T full \"To\" field
  1628. %t first name in \"To\" field, address if no name
  1629. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1630. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1631. %s subject
  1632. %d date
  1633. %m message-id.
  1634. You may use normal field width specification between the % and the letter.
  1635. This is for example useful to limit the length of the subject.
  1636. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1637. :group 'org-link-store
  1638. :type 'string)
  1639. (defcustom org-from-is-user-regexp
  1640. (let (r1 r2)
  1641. (when (and user-mail-address (not (string= user-mail-address "")))
  1642. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1643. (when (and user-full-name (not (string= user-full-name "")))
  1644. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1645. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1646. "Regexp matched against the \"From:\" header of an email or usenet message.
  1647. It should match if the message is from the user him/herself."
  1648. :group 'org-link-store
  1649. :type 'regexp)
  1650. (defcustom org-context-in-file-links t
  1651. "Non-nil means file links from `org-store-link' contain context.
  1652. A search string will be added to the file name with :: as separator and
  1653. used to find the context when the link is activated by the command
  1654. `org-open-at-point'. When this option is t, the entire active region
  1655. will be placed in the search string of the file link. If set to a
  1656. positive integer, only the first n lines of context will be stored.
  1657. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1658. negates this setting for the duration of the command."
  1659. :group 'org-link-store
  1660. :type '(choice boolean integer))
  1661. (defcustom org-keep-stored-link-after-insertion nil
  1662. "Non-nil means keep link in list for entire session.
  1663. The command `org-store-link' adds a link pointing to the current
  1664. location to an internal list. These links accumulate during a session.
  1665. The command `org-insert-link' can be used to insert links into any
  1666. Org-mode file (offering completion for all stored links). When this
  1667. option is nil, every link which has been inserted once using \\[org-insert-link]
  1668. will be removed from the list, to make completing the unused links
  1669. more efficient."
  1670. :group 'org-link-store
  1671. :type 'boolean)
  1672. (defgroup org-link-follow nil
  1673. "Options concerning following links in Org-mode."
  1674. :tag "Org Follow Link"
  1675. :group 'org-link)
  1676. (defcustom org-link-translation-function nil
  1677. "Function to translate links with different syntax to Org syntax.
  1678. This can be used to translate links created for example by the Planner
  1679. or emacs-wiki packages to Org syntax.
  1680. The function must accept two parameters, a TYPE containing the link
  1681. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1682. which is everything after the link protocol. It should return a cons
  1683. with possibly modified values of type and path.
  1684. Org contains a function for this, so if you set this variable to
  1685. `org-translate-link-from-planner', you should be able follow many
  1686. links created by planner."
  1687. :group 'org-link-follow
  1688. :type '(choice (const nil) (function)))
  1689. (defcustom org-follow-link-hook nil
  1690. "Hook that is run after a link has been followed."
  1691. :group 'org-link-follow
  1692. :type 'hook)
  1693. (defcustom org-tab-follows-link nil
  1694. "Non-nil means on links TAB will follow the link.
  1695. Needs to be set before org.el is loaded.
  1696. This really should not be used, it does not make sense, and the
  1697. implementation is bad."
  1698. :group 'org-link-follow
  1699. :type 'boolean)
  1700. (defcustom org-return-follows-link nil
  1701. "Non-nil means on links RET will follow the link.
  1702. In tables, the special behavior of RET has precedence."
  1703. :group 'org-link-follow
  1704. :type 'boolean)
  1705. (defcustom org-mouse-1-follows-link
  1706. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1707. "Non-nil means mouse-1 on a link will follow the link.
  1708. A longer mouse click will still set point. Does not work on XEmacs.
  1709. Needs to be set before org.el is loaded."
  1710. :group 'org-link-follow
  1711. :version "24.4"
  1712. :package-version '(Org . "8.3")
  1713. :type '(choice
  1714. (const :tag "A double click follows the link" double)
  1715. (const :tag "Unconditionally follow the link with mouse-1" t)
  1716. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1717. (defcustom org-mark-ring-length 4
  1718. "Number of different positions to be recorded in the ring.
  1719. Changing this requires a restart of Emacs to work correctly."
  1720. :group 'org-link-follow
  1721. :type 'integer)
  1722. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1723. "Non-nil means internal links in Org files must exactly match a headline.
  1724. When nil, the link search tries to match a phrase with all words
  1725. in the search text."
  1726. :group 'org-link-follow
  1727. :version "24.1"
  1728. :type '(choice
  1729. (const :tag "Use fuzzy text search" nil)
  1730. (const :tag "Match only exact headline" t)
  1731. (const :tag "Match exact headline or query to create it"
  1732. query-to-create)))
  1733. (defcustom org-link-frame-setup
  1734. '((vm . vm-visit-folder-other-frame)
  1735. (vm-imap . vm-visit-imap-folder-other-frame)
  1736. (gnus . org-gnus-no-new-news)
  1737. (file . find-file-other-window)
  1738. (wl . wl-other-frame))
  1739. "Setup the frame configuration for following links.
  1740. When following a link with Emacs, it may often be useful to display
  1741. this link in another window or frame. This variable can be used to
  1742. set this up for the different types of links.
  1743. For VM, use any of
  1744. `vm-visit-folder'
  1745. `vm-visit-folder-other-window'
  1746. `vm-visit-folder-other-frame'
  1747. For Gnus, use any of
  1748. `gnus'
  1749. `gnus-other-frame'
  1750. `org-gnus-no-new-news'
  1751. For FILE, use any of
  1752. `find-file'
  1753. `find-file-other-window'
  1754. `find-file-other-frame'
  1755. For Wanderlust use any of
  1756. `wl'
  1757. `wl-other-frame'
  1758. For the calendar, use the variable `calendar-setup'.
  1759. For BBDB, it is currently only possible to display the matches in
  1760. another window."
  1761. :group 'org-link-follow
  1762. :type '(list
  1763. (cons (const vm)
  1764. (choice
  1765. (const vm-visit-folder)
  1766. (const vm-visit-folder-other-window)
  1767. (const vm-visit-folder-other-frame)))
  1768. (cons (const vm-imap)
  1769. (choice
  1770. (const vm-visit-imap-folder)
  1771. (const vm-visit-imap-folder-other-window)
  1772. (const vm-visit-imap-folder-other-frame)))
  1773. (cons (const gnus)
  1774. (choice
  1775. (const gnus)
  1776. (const gnus-other-frame)
  1777. (const org-gnus-no-new-news)))
  1778. (cons (const file)
  1779. (choice
  1780. (const find-file)
  1781. (const find-file-other-window)
  1782. (const find-file-other-frame)))
  1783. (cons (const wl)
  1784. (choice
  1785. (const wl)
  1786. (const wl-other-frame)))))
  1787. (defcustom org-display-internal-link-with-indirect-buffer nil
  1788. "Non-nil means use indirect buffer to display infile links.
  1789. Activating internal links (from one location in a file to another location
  1790. in the same file) normally just jumps to the location. When the link is
  1791. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1792. is displayed in
  1793. another window. When this option is set, the other window actually displays
  1794. an indirect buffer clone of the current buffer, to avoid any visibility
  1795. changes to the current buffer."
  1796. :group 'org-link-follow
  1797. :type 'boolean)
  1798. (defcustom org-open-non-existing-files nil
  1799. "Non-nil means `org-open-file' will open non-existing files.
  1800. When nil, an error will be generated.
  1801. This variable applies only to external applications because they
  1802. might choke on non-existing files. If the link is to a file that
  1803. will be opened in Emacs, the variable is ignored."
  1804. :group 'org-link-follow
  1805. :type 'boolean)
  1806. (defcustom org-open-directory-means-index-dot-org nil
  1807. "Non-nil means a link to a directory really means to index.org.
  1808. When nil, following a directory link will run dired or open a finder/explorer
  1809. window on that directory."
  1810. :group 'org-link-follow
  1811. :type 'boolean)
  1812. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1813. "Non-nil means ask for confirmation before executing shell links.
  1814. Shell links can be dangerous: just think about a link
  1815. [[shell:rm -rf ~/*][Google Search]]
  1816. This link would show up in your Org-mode document as \"Google Search\",
  1817. but really it would remove your entire home directory.
  1818. Therefore we advise against setting this variable to nil.
  1819. Just change it to `y-or-n-p' if you want to confirm with a
  1820. single keystroke rather than having to type \"yes\"."
  1821. :group 'org-link-follow
  1822. :type '(choice
  1823. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1824. (const :tag "with y-or-n (faster)" y-or-n-p)
  1825. (const :tag "no confirmation (dangerous)" nil)))
  1826. (put 'org-confirm-shell-link-function
  1827. 'safe-local-variable
  1828. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1829. (defcustom org-confirm-shell-link-not-regexp ""
  1830. "A regexp to skip confirmation for shell links."
  1831. :group 'org-link-follow
  1832. :version "24.1"
  1833. :type 'regexp)
  1834. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1835. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1836. Elisp links can be dangerous: just think about a link
  1837. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1838. This link would show up in your Org-mode document as \"Google Search\",
  1839. but really it would remove your entire home directory.
  1840. Therefore we advise against setting this variable to nil.
  1841. Just change it to `y-or-n-p' if you want to confirm with a
  1842. single keystroke rather than having to type \"yes\"."
  1843. :group 'org-link-follow
  1844. :type '(choice
  1845. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1846. (const :tag "with y-or-n (faster)" y-or-n-p)
  1847. (const :tag "no confirmation (dangerous)" nil)))
  1848. (put 'org-confirm-shell-link-function
  1849. 'safe-local-variable
  1850. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1851. (defcustom org-confirm-elisp-link-not-regexp ""
  1852. "A regexp to skip confirmation for Elisp links."
  1853. :group 'org-link-follow
  1854. :version "24.1"
  1855. :type 'regexp)
  1856. (defconst org-file-apps-defaults-gnu
  1857. '((remote . emacs)
  1858. (system . mailcap)
  1859. (t . mailcap))
  1860. "Default file applications on a UNIX or GNU/Linux system.
  1861. See `org-file-apps'.")
  1862. (defconst org-file-apps-defaults-macosx
  1863. '((remote . emacs)
  1864. (t . "open %s")
  1865. (system . "open %s")
  1866. ("ps.gz" . "gv %s")
  1867. ("eps.gz" . "gv %s")
  1868. ("dvi" . "xdvi %s")
  1869. ("fig" . "xfig %s"))
  1870. "Default file applications on a MacOS X system.
  1871. The system \"open\" is known as a default, but we use X11 applications
  1872. for some files for which the OS does not have a good default.
  1873. See `org-file-apps'.")
  1874. (defconst org-file-apps-defaults-windowsnt
  1875. (list
  1876. '(remote . emacs)
  1877. (cons t
  1878. (list (if (featurep 'xemacs)
  1879. 'mswindows-shell-execute
  1880. 'w32-shell-execute)
  1881. "open" 'file))
  1882. (cons 'system
  1883. (list (if (featurep 'xemacs)
  1884. 'mswindows-shell-execute
  1885. 'w32-shell-execute)
  1886. "open" 'file)))
  1887. "Default file applications on a Windows NT system.
  1888. The system \"open\" is used for most files.
  1889. See `org-file-apps'.")
  1890. (defcustom org-file-apps
  1891. '((auto-mode . emacs)
  1892. ("\\.mm\\'" . default)
  1893. ("\\.x?html?\\'" . default)
  1894. ("\\.pdf\\'" . default))
  1895. "External applications for opening `file:path' items in a document.
  1896. Org-mode uses system defaults for different file types, but
  1897. you can use this variable to set the application for a given file
  1898. extension. The entries in this list are cons cells where the car identifies
  1899. files and the cdr the corresponding command. Possible values for the
  1900. file identifier are
  1901. \"string\" A string as a file identifier can be interpreted in different
  1902. ways, depending on its contents:
  1903. - Alphanumeric characters only:
  1904. Match links with this file extension.
  1905. Example: (\"pdf\" . \"evince %s\")
  1906. to open PDFs with evince.
  1907. - Regular expression: Match links where the
  1908. filename matches the regexp. If you want to
  1909. use groups here, use shy groups.
  1910. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1911. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1912. to open *.html and *.xhtml with firefox.
  1913. - Regular expression which contains (non-shy) groups:
  1914. Match links where the whole link, including \"::\", and
  1915. anything after that, matches the regexp.
  1916. In a custom command string, %1, %2, etc. are replaced with
  1917. the parts of the link that were matched by the groups.
  1918. For backwards compatibility, if a command string is given
  1919. that does not use any of the group matches, this case is
  1920. handled identically to the second one (i.e. match against
  1921. file name only).
  1922. In a custom lisp form, you can access the group matches with
  1923. (match-string n link).
  1924. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1925. to open [[file:document.pdf::5]] with evince at page 5.
  1926. `directory' Matches a directory
  1927. `remote' Matches a remote file, accessible through tramp or efs.
  1928. Remote files most likely should be visited through Emacs
  1929. because external applications cannot handle such paths.
  1930. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1931. so all files Emacs knows how to handle. Using this with
  1932. command `emacs' will open most files in Emacs. Beware that this
  1933. will also open html files inside Emacs, unless you add
  1934. (\"html\" . default) to the list as well.
  1935. t Default for files not matched by any of the other options.
  1936. `system' The system command to open files, like `open' on Windows
  1937. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1938. that will be selected if you call `C-c C-o' with a double
  1939. \\[universal-argument] \\[universal-argument] prefix.
  1940. Possible values for the command are:
  1941. `emacs' The file will be visited by the current Emacs process.
  1942. `default' Use the default application for this file type, which is the
  1943. association for t in the list, most likely in the system-specific
  1944. part.
  1945. This can be used to overrule an unwanted setting in the
  1946. system-specific variable.
  1947. `system' Use the system command for opening files, like \"open\".
  1948. This command is specified by the entry whose car is `system'.
  1949. Most likely, the system-specific version of this variable
  1950. does define this command, but you can overrule/replace it
  1951. here.
  1952. string A command to be executed by a shell; %s will be replaced
  1953. by the path to the file.
  1954. sexp A Lisp form which will be evaluated. The file path will
  1955. be available in the Lisp variable `file'.
  1956. For more examples, see the system specific constants
  1957. `org-file-apps-defaults-macosx'
  1958. `org-file-apps-defaults-windowsnt'
  1959. `org-file-apps-defaults-gnu'."
  1960. :group 'org-link-follow
  1961. :type '(repeat
  1962. (cons (choice :value ""
  1963. (string :tag "Extension")
  1964. (const :tag "System command to open files" system)
  1965. (const :tag "Default for unrecognized files" t)
  1966. (const :tag "Remote file" remote)
  1967. (const :tag "Links to a directory" directory)
  1968. (const :tag "Any files that have Emacs modes"
  1969. auto-mode))
  1970. (choice :value ""
  1971. (const :tag "Visit with Emacs" emacs)
  1972. (const :tag "Use default" default)
  1973. (const :tag "Use the system command" system)
  1974. (string :tag "Command")
  1975. (sexp :tag "Lisp form")))))
  1976. (defcustom org-doi-server-url "http://dx.doi.org/"
  1977. "The URL of the DOI server."
  1978. :type 'string
  1979. :version "24.3"
  1980. :group 'org-link-follow)
  1981. (defgroup org-refile nil
  1982. "Options concerning refiling entries in Org-mode."
  1983. :tag "Org Refile"
  1984. :group 'org)
  1985. (defcustom org-directory "~/org"
  1986. "Directory with org files.
  1987. This is just a default location to look for Org files. There is no need
  1988. at all to put your files into this directory. It is only used in the
  1989. following situations:
  1990. 1. When a capture template specifies a target file that is not an
  1991. absolute path. The path will then be interpreted relative to
  1992. `org-directory'
  1993. 2. When a capture note is filed away in an interactive way (when exiting the
  1994. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1995. with `org-directory' as the default path."
  1996. :group 'org-refile
  1997. :group 'org-capture
  1998. :type 'directory)
  1999. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2000. "Default target for storing notes.
  2001. Used as a fall back file for org-capture.el, for templates that
  2002. do not specify a target file."
  2003. :group 'org-refile
  2004. :group 'org-capture
  2005. :type 'file)
  2006. (defcustom org-goto-interface 'outline
  2007. "The default interface to be used for `org-goto'.
  2008. Allowed values are:
  2009. outline The interface shows an outline of the relevant file
  2010. and the correct heading is found by moving through
  2011. the outline or by searching with incremental search.
  2012. outline-path-completion Headlines in the current buffer are offered via
  2013. completion. This is the interface also used by
  2014. the refile command."
  2015. :group 'org-refile
  2016. :type '(choice
  2017. (const :tag "Outline" outline)
  2018. (const :tag "Outline-path-completion" outline-path-completion)))
  2019. (defcustom org-goto-max-level 5
  2020. "Maximum target level when running `org-goto' with refile interface."
  2021. :group 'org-refile
  2022. :type 'integer)
  2023. (defcustom org-reverse-note-order nil
  2024. "Non-nil means store new notes at the beginning of a file or entry.
  2025. When nil, new notes will be filed to the end of a file or entry.
  2026. This can also be a list with cons cells of regular expressions that
  2027. are matched against file names, and values."
  2028. :group 'org-capture
  2029. :group 'org-refile
  2030. :type '(choice
  2031. (const :tag "Reverse always" t)
  2032. (const :tag "Reverse never" nil)
  2033. (repeat :tag "By file name regexp"
  2034. (cons regexp boolean))))
  2035. (defcustom org-log-refile nil
  2036. "Information to record when a task is refiled.
  2037. Possible values are:
  2038. nil Don't add anything
  2039. time Add a time stamp to the task
  2040. note Prompt for a note and add it with template `org-log-note-headings'
  2041. This option can also be set with on a per-file-basis with
  2042. #+STARTUP: nologrefile
  2043. #+STARTUP: logrefile
  2044. #+STARTUP: lognoterefile
  2045. You can have local logging settings for a subtree by setting the LOGGING
  2046. property to one or more of these keywords.
  2047. When bulk-refiling from the agenda, the value `note' is forbidden and
  2048. will temporarily be changed to `time'."
  2049. :group 'org-refile
  2050. :group 'org-progress
  2051. :version "24.1"
  2052. :type '(choice
  2053. (const :tag "No logging" nil)
  2054. (const :tag "Record timestamp" time)
  2055. (const :tag "Record timestamp with note." note)))
  2056. (defcustom org-refile-targets nil
  2057. "Targets for refiling entries with \\[org-refile].
  2058. This is a list of cons cells. Each cell contains:
  2059. - a specification of the files to be considered, either a list of files,
  2060. or a symbol whose function or variable value will be used to retrieve
  2061. a file name or a list of file names. If you use `org-agenda-files' for
  2062. that, all agenda files will be scanned for targets. Nil means consider
  2063. headings in the current buffer.
  2064. - A specification of how to find candidate refile targets. This may be
  2065. any of:
  2066. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2067. This tag has to be present in all target headlines, inheritance will
  2068. not be considered.
  2069. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2070. todo keyword.
  2071. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2072. headlines that are refiling targets.
  2073. - a cons cell (:level . N). Any headline of level N is considered a target.
  2074. Note that, when `org-odd-levels-only' is set, level corresponds to
  2075. order in hierarchy, not to the number of stars.
  2076. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2077. Note that, when `org-odd-levels-only' is set, level corresponds to
  2078. order in hierarchy, not to the number of stars.
  2079. Each element of this list generates a set of possible targets.
  2080. The union of these sets is presented (with completion) to
  2081. the user by `org-refile'.
  2082. You can set the variable `org-refile-target-verify-function' to a function
  2083. to verify each headline found by the simple criteria above.
  2084. When this variable is nil, all top-level headlines in the current buffer
  2085. are used, equivalent to the value `((nil . (:level . 1))'."
  2086. :group 'org-refile
  2087. :type '(repeat
  2088. (cons
  2089. (choice :value org-agenda-files
  2090. (const :tag "All agenda files" org-agenda-files)
  2091. (const :tag "Current buffer" nil)
  2092. (function) (variable) (file))
  2093. (choice :tag "Identify target headline by"
  2094. (cons :tag "Specific tag" (const :value :tag) (string))
  2095. (cons :tag "TODO keyword" (const :value :todo) (string))
  2096. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2097. (cons :tag "Level number" (const :value :level) (integer))
  2098. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2099. (defcustom org-refile-target-verify-function nil
  2100. "Function to verify if the headline at point should be a refile target.
  2101. The function will be called without arguments, with point at the
  2102. beginning of the headline. It should return t and leave point
  2103. where it is if the headline is a valid target for refiling.
  2104. If the target should not be selected, the function must return nil.
  2105. In addition to this, it may move point to a place from where the search
  2106. should be continued. For example, the function may decide that the entire
  2107. subtree of the current entry should be excluded and move point to the end
  2108. of the subtree."
  2109. :group 'org-refile
  2110. :type '(choice
  2111. (const nil)
  2112. (function)))
  2113. (defcustom org-refile-use-cache nil
  2114. "Non-nil means cache refile targets to speed up the process.
  2115. The cache for a particular file will be updated automatically when
  2116. the buffer has been killed, or when any of the marker used for flagging
  2117. refile targets no longer points at a live buffer.
  2118. If you have added new entries to a buffer that might themselves be targets,
  2119. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2120. find that easier, `C-u C-u C-u C-c C-w'."
  2121. :group 'org-refile
  2122. :version "24.1"
  2123. :type 'boolean)
  2124. (defcustom org-refile-use-outline-path nil
  2125. "Non-nil means provide refile targets as paths.
  2126. So a level 3 headline will be available as level1/level2/level3.
  2127. When the value is `file', also include the file name (without directory)
  2128. into the path. In this case, you can also stop the completion after
  2129. the file name, to get entries inserted as top level in the file.
  2130. When `full-file-path', include the full file path."
  2131. :group 'org-refile
  2132. :type '(choice
  2133. (const :tag "Not" nil)
  2134. (const :tag "Yes" t)
  2135. (const :tag "Start with file name" file)
  2136. (const :tag "Start with full file path" full-file-path)))
  2137. (defcustom org-outline-path-complete-in-steps t
  2138. "Non-nil means complete the outline path in hierarchical steps.
  2139. When Org-mode uses the refile interface to select an outline path
  2140. \(see variable `org-refile-use-outline-path'), the completion of
  2141. the path can be done in a single go, or it can be done in steps down
  2142. the headline hierarchy. Going in steps is probably the best if you
  2143. do not use a special completion package like `ido' or `icicles'.
  2144. However, when using these packages, going in one step can be very
  2145. fast, while still showing the whole path to the entry."
  2146. :group 'org-refile
  2147. :type 'boolean)
  2148. (defcustom org-refile-allow-creating-parent-nodes nil
  2149. "Non-nil means allow to create new nodes as refile targets.
  2150. New nodes are then created by adding \"/new node name\" to the completion
  2151. of an existing node. When the value of this variable is `confirm',
  2152. new node creation must be confirmed by the user (recommended).
  2153. When nil, the completion must match an existing entry.
  2154. Note that, if the new heading is not seen by the criteria
  2155. listed in `org-refile-targets', multiple instances of the same
  2156. heading would be created by trying again to file under the new
  2157. heading."
  2158. :group 'org-refile
  2159. :type '(choice
  2160. (const :tag "Never" nil)
  2161. (const :tag "Always" t)
  2162. (const :tag "Prompt for confirmation" confirm)))
  2163. (defcustom org-refile-active-region-within-subtree nil
  2164. "Non-nil means also refile active region within a subtree.
  2165. By default `org-refile' doesn't allow refiling regions if they
  2166. don't contain a set of subtrees, but it might be convenient to
  2167. do so sometimes: in that case, the first line of the region is
  2168. converted to a headline before refiling."
  2169. :group 'org-refile
  2170. :version "24.1"
  2171. :type 'boolean)
  2172. (defgroup org-todo nil
  2173. "Options concerning TODO items in Org-mode."
  2174. :tag "Org TODO"
  2175. :group 'org)
  2176. (defgroup org-progress nil
  2177. "Options concerning Progress logging in Org-mode."
  2178. :tag "Org Progress"
  2179. :group 'org-time)
  2180. (defvar org-todo-interpretation-widgets
  2181. '((:tag "Sequence (cycling hits every state)" sequence)
  2182. (:tag "Type (cycling directly to DONE)" type))
  2183. "The available interpretation symbols for customizing `org-todo-keywords'.
  2184. Interested libraries should add to this list.")
  2185. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2186. "List of TODO entry keyword sequences and their interpretation.
  2187. \\<org-mode-map>This is a list of sequences.
  2188. Each sequence starts with a symbol, either `sequence' or `type',
  2189. indicating if the keywords should be interpreted as a sequence of
  2190. action steps, or as different types of TODO items. The first
  2191. keywords are states requiring action - these states will select a headline
  2192. for inclusion into the global TODO list Org-mode produces. If one of
  2193. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2194. signify that no further action is necessary. If \"|\" is not found,
  2195. the last keyword is treated as the only DONE state of the sequence.
  2196. The command \\[org-todo] cycles an entry through these states, and one
  2197. additional state where no keyword is present. For details about this
  2198. cycling, see the manual.
  2199. TODO keywords and interpretation can also be set on a per-file basis with
  2200. the special #+SEQ_TODO and #+TYP_TODO lines.
  2201. Each keyword can optionally specify a character for fast state selection
  2202. \(in combination with the variable `org-use-fast-todo-selection')
  2203. and specifiers for state change logging, using the same syntax that
  2204. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2205. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2206. indicates to record a time stamp each time this state is selected.
  2207. Each keyword may also specify if a timestamp or a note should be
  2208. recorded when entering or leaving the state, by adding additional
  2209. characters in the parenthesis after the keyword. This looks like this:
  2210. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2211. record only the time of the state change. With X and Y being either
  2212. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2213. Y when leaving the state if and only if the *target* state does not
  2214. define X. You may omit any of the fast-selection key or X or /Y,
  2215. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2216. For backward compatibility, this variable may also be just a list
  2217. of keywords. In this case the interpretation (sequence or type) will be
  2218. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2219. :group 'org-todo
  2220. :group 'org-keywords
  2221. :type '(choice
  2222. (repeat :tag "Old syntax, just keywords"
  2223. (string :tag "Keyword"))
  2224. (repeat :tag "New syntax"
  2225. (cons
  2226. (choice
  2227. :tag "Interpretation"
  2228. ;;Quick and dirty way to see
  2229. ;;`org-todo-interpretations'. This takes the
  2230. ;;place of item arguments
  2231. :convert-widget
  2232. (lambda (widget)
  2233. (widget-put widget
  2234. :args (mapcar
  2235. (lambda (x)
  2236. (widget-convert
  2237. (cons 'const x)))
  2238. org-todo-interpretation-widgets))
  2239. widget))
  2240. (repeat
  2241. (string :tag "Keyword"))))))
  2242. (defvar org-todo-keywords-1 nil
  2243. "All TODO and DONE keywords active in a buffer.")
  2244. (make-variable-buffer-local 'org-todo-keywords-1)
  2245. (defvar org-todo-keywords-for-agenda nil)
  2246. (defvar org-done-keywords-for-agenda nil)
  2247. (defvar org-todo-keyword-alist-for-agenda nil)
  2248. (defvar org-tag-alist-for-agenda nil
  2249. "Alist of all tags from all agenda files.")
  2250. (defvar org-tag-groups-alist-for-agenda nil
  2251. "Alist of all groups tags from all current agenda files.")
  2252. (defvar org-tag-groups-alist nil)
  2253. (make-variable-buffer-local 'org-tag-groups-alist)
  2254. (defvar org-agenda-contributing-files nil)
  2255. (defvar org-not-done-keywords nil)
  2256. (make-variable-buffer-local 'org-not-done-keywords)
  2257. (defvar org-done-keywords nil)
  2258. (make-variable-buffer-local 'org-done-keywords)
  2259. (defvar org-todo-heads nil)
  2260. (make-variable-buffer-local 'org-todo-heads)
  2261. (defvar org-todo-sets nil)
  2262. (make-variable-buffer-local 'org-todo-sets)
  2263. (defvar org-todo-log-states nil)
  2264. (make-variable-buffer-local 'org-todo-log-states)
  2265. (defvar org-todo-kwd-alist nil)
  2266. (make-variable-buffer-local 'org-todo-kwd-alist)
  2267. (defvar org-todo-key-alist nil)
  2268. (make-variable-buffer-local 'org-todo-key-alist)
  2269. (defvar org-todo-key-trigger nil)
  2270. (make-variable-buffer-local 'org-todo-key-trigger)
  2271. (defcustom org-todo-interpretation 'sequence
  2272. "Controls how TODO keywords are interpreted.
  2273. This variable is in principle obsolete and is only used for
  2274. backward compatibility, if the interpretation of todo keywords is
  2275. not given already in `org-todo-keywords'. See that variable for
  2276. more information."
  2277. :group 'org-todo
  2278. :group 'org-keywords
  2279. :type '(choice (const sequence)
  2280. (const type)))
  2281. (defcustom org-use-fast-todo-selection t
  2282. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2283. This variable describes if and under what circumstances the cycling
  2284. mechanism for TODO keywords will be replaced by a single-key, direct
  2285. selection scheme.
  2286. When nil, fast selection is never used.
  2287. When the symbol `prefix', it will be used when `org-todo' is called
  2288. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2289. `C-u t' in an agenda buffer.
  2290. When t, fast selection is used by default. In this case, the prefix
  2291. argument forces cycling instead.
  2292. In all cases, the special interface is only used if access keys have
  2293. actually been assigned by the user, i.e. if keywords in the configuration
  2294. are followed by a letter in parenthesis, like TODO(t)."
  2295. :group 'org-todo
  2296. :type '(choice
  2297. (const :tag "Never" nil)
  2298. (const :tag "By default" t)
  2299. (const :tag "Only with C-u C-c C-t" prefix)))
  2300. (defcustom org-provide-todo-statistics t
  2301. "Non-nil means update todo statistics after insert and toggle.
  2302. ALL-HEADLINES means update todo statistics by including headlines
  2303. with no TODO keyword as well, counting them as not done.
  2304. A list of TODO keywords means the same, but skip keywords that are
  2305. not in this list.
  2306. When set to a list of two lists, the first list contains keywords
  2307. to consider as TODO keywords, the second list contains keywords
  2308. to consider as DONE keywords.
  2309. When this is set, todo statistics is updated in the parent of the
  2310. current entry each time a todo state is changed."
  2311. :group 'org-todo
  2312. :type '(choice
  2313. (const :tag "Yes, only for TODO entries" t)
  2314. (const :tag "Yes, including all entries" all-headlines)
  2315. (repeat :tag "Yes, for TODOs in this list"
  2316. (string :tag "TODO keyword"))
  2317. (list :tag "Yes, for TODOs and DONEs in these lists"
  2318. (repeat (string :tag "TODO keyword"))
  2319. (repeat (string :tag "DONE keyword")))
  2320. (other :tag "No TODO statistics" nil)))
  2321. (defcustom org-hierarchical-todo-statistics t
  2322. "Non-nil means TODO statistics covers just direct children.
  2323. When nil, all entries in the subtree are considered.
  2324. This has only an effect if `org-provide-todo-statistics' is set.
  2325. To set this to nil for only a single subtree, use a COOKIE_DATA
  2326. property and include the word \"recursive\" into the value."
  2327. :group 'org-todo
  2328. :type 'boolean)
  2329. (defcustom org-after-todo-state-change-hook nil
  2330. "Hook which is run after the state of a TODO item was changed.
  2331. The new state (a string with a TODO keyword, or nil) is available in the
  2332. Lisp variable `org-state'."
  2333. :group 'org-todo
  2334. :type 'hook)
  2335. (defvar org-blocker-hook nil
  2336. "Hook for functions that are allowed to block a state change.
  2337. Functions in this hook should not modify the buffer.
  2338. Each function gets as its single argument a property list,
  2339. see `org-trigger-hook' for more information about this list.
  2340. If any of the functions in this hook returns nil, the state change
  2341. is blocked.")
  2342. (defvar org-trigger-hook nil
  2343. "Hook for functions that are triggered by a state change.
  2344. Each function gets as its single argument a property list with at
  2345. least the following elements:
  2346. (:type type-of-change :position pos-at-entry-start
  2347. :from old-state :to new-state)
  2348. Depending on the type, more properties may be present.
  2349. This mechanism is currently implemented for:
  2350. TODO state changes
  2351. ------------------
  2352. :type todo-state-change
  2353. :from previous state (keyword as a string), or nil, or a symbol
  2354. 'todo' or 'done', to indicate the general type of state.
  2355. :to new state, like in :from")
  2356. (defcustom org-enforce-todo-dependencies nil
  2357. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2358. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2359. be blocked if any prior sibling is not yet done.
  2360. Finally, if the parent is blocked because of ordered siblings of its own,
  2361. the child will also be blocked."
  2362. :set (lambda (var val)
  2363. (set var val)
  2364. (if val
  2365. (add-hook 'org-blocker-hook
  2366. 'org-block-todo-from-children-or-siblings-or-parent)
  2367. (remove-hook 'org-blocker-hook
  2368. 'org-block-todo-from-children-or-siblings-or-parent)))
  2369. :group 'org-todo
  2370. :type 'boolean)
  2371. (defcustom org-enforce-todo-checkbox-dependencies nil
  2372. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2373. When this is nil, checkboxes have no influence on switching TODO states.
  2374. When non-nil, you first need to check off all check boxes before the TODO
  2375. entry can be switched to DONE.
  2376. This variable needs to be set before org.el is loaded, and you need to
  2377. restart Emacs after a change to make the change effective. The only way
  2378. to change is while Emacs is running is through the customize interface."
  2379. :set (lambda (var val)
  2380. (set var val)
  2381. (if val
  2382. (add-hook 'org-blocker-hook
  2383. 'org-block-todo-from-checkboxes)
  2384. (remove-hook 'org-blocker-hook
  2385. 'org-block-todo-from-checkboxes)))
  2386. :group 'org-todo
  2387. :type 'boolean)
  2388. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2389. "Non-nil means inserting a TODO heading is treated as state change.
  2390. So when the command \\[org-insert-todo-heading] is used, state change
  2391. logging will apply if appropriate. When nil, the new TODO item will
  2392. be inserted directly, and no logging will take place."
  2393. :group 'org-todo
  2394. :type 'boolean)
  2395. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2396. "Non-nil means switching TODO states with S-cursor counts as state change.
  2397. This is the default behavior. However, setting this to nil allows a
  2398. convenient way to select a TODO state and bypass any logging associated
  2399. with that."
  2400. :group 'org-todo
  2401. :type 'boolean)
  2402. (defcustom org-todo-state-tags-triggers nil
  2403. "Tag changes that should be triggered by TODO state changes.
  2404. This is a list. Each entry is
  2405. (state-change (tag . flag) .......)
  2406. State-change can be a string with a state, and empty string to indicate the
  2407. state that has no TODO keyword, or it can be one of the symbols `todo'
  2408. or `done', meaning any not-done or done state, respectively."
  2409. :group 'org-todo
  2410. :group 'org-tags
  2411. :type '(repeat
  2412. (cons (choice :tag "When changing to"
  2413. (const :tag "Not-done state" todo)
  2414. (const :tag "Done state" done)
  2415. (string :tag "State"))
  2416. (repeat
  2417. (cons :tag "Tag action"
  2418. (string :tag "Tag")
  2419. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2420. (defcustom org-log-done nil
  2421. "Information to record when a task moves to the DONE state.
  2422. Possible values are:
  2423. nil Don't add anything, just change the keyword
  2424. time Add a time stamp to the task
  2425. note Prompt for a note and add it with template `org-log-note-headings'
  2426. This option can also be set with on a per-file-basis with
  2427. #+STARTUP: nologdone
  2428. #+STARTUP: logdone
  2429. #+STARTUP: lognotedone
  2430. You can have local logging settings for a subtree by setting the LOGGING
  2431. property to one or more of these keywords."
  2432. :group 'org-todo
  2433. :group 'org-progress
  2434. :type '(choice
  2435. (const :tag "No logging" nil)
  2436. (const :tag "Record CLOSED timestamp" time)
  2437. (const :tag "Record CLOSED timestamp with note." note)))
  2438. ;; Normalize old uses of org-log-done.
  2439. (cond
  2440. ((eq org-log-done t) (setq org-log-done 'time))
  2441. ((and (listp org-log-done) (memq 'done org-log-done))
  2442. (setq org-log-done 'note)))
  2443. (defcustom org-log-reschedule nil
  2444. "Information to record when the scheduling date of a tasks is modified.
  2445. Possible values are:
  2446. nil Don't add anything, just change the date
  2447. time Add a time stamp to the task
  2448. note Prompt for a note and add it with template `org-log-note-headings'
  2449. This option can also be set with on a per-file-basis with
  2450. #+STARTUP: nologreschedule
  2451. #+STARTUP: logreschedule
  2452. #+STARTUP: lognotereschedule"
  2453. :group 'org-todo
  2454. :group 'org-progress
  2455. :type '(choice
  2456. (const :tag "No logging" nil)
  2457. (const :tag "Record timestamp" time)
  2458. (const :tag "Record timestamp with note." note)))
  2459. (defcustom org-log-redeadline nil
  2460. "Information to record when the deadline date of a tasks is modified.
  2461. Possible values are:
  2462. nil Don't add anything, just change the date
  2463. time Add a time stamp to the task
  2464. note Prompt for a note and add it with template `org-log-note-headings'
  2465. This option can also be set with on a per-file-basis with
  2466. #+STARTUP: nologredeadline
  2467. #+STARTUP: logredeadline
  2468. #+STARTUP: lognoteredeadline
  2469. You can have local logging settings for a subtree by setting the LOGGING
  2470. property to one or more of these keywords."
  2471. :group 'org-todo
  2472. :group 'org-progress
  2473. :type '(choice
  2474. (const :tag "No logging" nil)
  2475. (const :tag "Record timestamp" time)
  2476. (const :tag "Record timestamp with note." note)))
  2477. (defcustom org-log-note-clock-out nil
  2478. "Non-nil means record a note when clocking out of an item.
  2479. This can also be configured on a per-file basis by adding one of
  2480. the following lines anywhere in the buffer:
  2481. #+STARTUP: lognoteclock-out
  2482. #+STARTUP: nolognoteclock-out"
  2483. :group 'org-todo
  2484. :group 'org-progress
  2485. :type 'boolean)
  2486. (defcustom org-log-done-with-time t
  2487. "Non-nil means the CLOSED time stamp will contain date and time.
  2488. When nil, only the date will be recorded."
  2489. :group 'org-progress
  2490. :type 'boolean)
  2491. (defcustom org-log-note-headings
  2492. '((done . "CLOSING NOTE %t")
  2493. (state . "State %-12s from %-12S %t")
  2494. (note . "Note taken on %t")
  2495. (reschedule . "Rescheduled from %S on %t")
  2496. (delschedule . "Not scheduled, was %S on %t")
  2497. (redeadline . "New deadline from %S on %t")
  2498. (deldeadline . "Removed deadline, was %S on %t")
  2499. (refile . "Refiled on %t")
  2500. (clock-out . ""))
  2501. "Headings for notes added to entries.
  2502. The value is an alist, with the car being a symbol indicating the
  2503. note context, and the cdr is the heading to be used. The heading
  2504. may also be the empty string. The following placeholders can be
  2505. used:
  2506. %t a time stamp.
  2507. %T an active time stamp instead the default inactive one
  2508. %d a short-format time stamp.
  2509. %D an active short-format time stamp.
  2510. %s the new TODO state or time stamp (inactive), in double quotes.
  2511. %S the old TODO state or time stamp (inactive), in double quotes.
  2512. %u the user name.
  2513. %U full user name.
  2514. In fact, it is not a good idea to change the `state' entry,
  2515. because Agenda Log mode depends on the format of these entries."
  2516. :group 'org-todo
  2517. :group 'org-progress
  2518. :type '(list :greedy t
  2519. (cons (const :tag "Heading when closing an item" done) string)
  2520. (cons (const :tag
  2521. "Heading when changing todo state (todo sequence only)"
  2522. state) string)
  2523. (cons (const :tag "Heading when just taking a note" note) string)
  2524. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2525. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2526. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2527. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2528. (cons (const :tag "Heading when refiling" refile) string)
  2529. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2530. (unless (assq 'note org-log-note-headings)
  2531. (push '(note . "%t") org-log-note-headings))
  2532. (defcustom org-log-into-drawer nil
  2533. "Non-nil means insert state change notes and time stamps into a drawer.
  2534. When nil, state changes notes will be inserted after the headline and
  2535. any scheduling and clock lines, but not inside a drawer.
  2536. The value of this variable should be the name of the drawer to use.
  2537. LOGBOOK is proposed as the default drawer for this purpose, you can
  2538. also set this to a string to define the drawer of your choice.
  2539. A value of t is also allowed, representing \"LOGBOOK\".
  2540. A value of t or nil can also be set with on a per-file-basis with
  2541. #+STARTUP: logdrawer
  2542. #+STARTUP: nologdrawer
  2543. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2544. will be ignored.
  2545. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2546. a subtree.
  2547. Do not check directly this variable in a Lisp program. Call
  2548. function `org-log-into-drawer' instead."
  2549. :group 'org-todo
  2550. :group 'org-progress
  2551. :type '(choice
  2552. (const :tag "Not into a drawer" nil)
  2553. (const :tag "LOGBOOK" t)
  2554. (string :tag "Other")))
  2555. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2556. (defun org-log-into-drawer ()
  2557. "Name of the log drawer, as a string, or nil.
  2558. This is the value of `org-log-into-drawer'. However, if the
  2559. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2560. be used instead of the default value."
  2561. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2562. (cond ((equal p "nil") nil)
  2563. ((equal p "t") "LOGBOOK")
  2564. ((stringp p) p)
  2565. (p "LOGBOOK")
  2566. ((stringp org-log-into-drawer) org-log-into-drawer)
  2567. (org-log-into-drawer "LOGBOOK"))))
  2568. (defcustom org-log-state-notes-insert-after-drawers nil
  2569. "Non-nil means insert state change notes after any drawers in entry.
  2570. Only the drawers that *immediately* follow the headline and the
  2571. deadline/scheduled line are skipped.
  2572. When nil, insert notes right after the heading and perhaps the line
  2573. with deadline/scheduling if present.
  2574. This variable will have no effect if `org-log-into-drawer' is
  2575. set."
  2576. :group 'org-todo
  2577. :group 'org-progress
  2578. :type 'boolean)
  2579. (defcustom org-log-states-order-reversed t
  2580. "Non-nil means the latest state note will be directly after heading.
  2581. When nil, the state change notes will be ordered according to time.
  2582. This option can also be set with on a per-file-basis with
  2583. #+STARTUP: logstatesreversed
  2584. #+STARTUP: nologstatesreversed"
  2585. :group 'org-todo
  2586. :group 'org-progress
  2587. :type 'boolean)
  2588. (defcustom org-todo-repeat-to-state nil
  2589. "The TODO state to which a repeater should return the repeating task.
  2590. By default this is the first task in a TODO sequence, or the previous state
  2591. in a TODO_TYP set. But you can specify another task here.
  2592. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2593. :group 'org-todo
  2594. :version "24.1"
  2595. :type '(choice (const :tag "Head of sequence" nil)
  2596. (string :tag "Specific state")))
  2597. (defcustom org-log-repeat 'time
  2598. "Non-nil means record moving through the DONE state when triggering repeat.
  2599. An auto-repeating task is immediately switched back to TODO when
  2600. marked DONE. If you are not logging state changes (by adding \"@\"
  2601. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2602. record a closing note, there will be no record of the task moving
  2603. through DONE. This variable forces taking a note anyway.
  2604. nil Don't force a record
  2605. time Record a time stamp
  2606. note Prompt for a note and add it with template `org-log-note-headings'
  2607. This option can also be set with on a per-file-basis with
  2608. #+STARTUP: nologrepeat
  2609. #+STARTUP: logrepeat
  2610. #+STARTUP: lognoterepeat
  2611. You can have local logging settings for a subtree by setting the LOGGING
  2612. property to one or more of these keywords."
  2613. :group 'org-todo
  2614. :group 'org-progress
  2615. :type '(choice
  2616. (const :tag "Don't force a record" nil)
  2617. (const :tag "Force recording the DONE state" time)
  2618. (const :tag "Force recording a note with the DONE state" note)))
  2619. (defgroup org-priorities nil
  2620. "Priorities in Org-mode."
  2621. :tag "Org Priorities"
  2622. :group 'org-todo)
  2623. (defcustom org-enable-priority-commands t
  2624. "Non-nil means priority commands are active.
  2625. When nil, these commands will be disabled, so that you never accidentally
  2626. set a priority."
  2627. :group 'org-priorities
  2628. :type 'boolean)
  2629. (defcustom org-highest-priority ?A
  2630. "The highest priority of TODO items. A character like ?A, ?B etc.
  2631. Must have a smaller ASCII number than `org-lowest-priority'."
  2632. :group 'org-priorities
  2633. :type 'character)
  2634. (defcustom org-lowest-priority ?C
  2635. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2636. Must have a larger ASCII number than `org-highest-priority'."
  2637. :group 'org-priorities
  2638. :type 'character)
  2639. (defcustom org-default-priority ?B
  2640. "The default priority of TODO items.
  2641. This is the priority an item gets if no explicit priority is given.
  2642. When starting to cycle on an empty priority the first step in the cycle
  2643. depends on `org-priority-start-cycle-with-default'. The resulting first
  2644. step priority must not exceed the range from `org-highest-priority' to
  2645. `org-lowest-priority' which means that `org-default-priority' has to be
  2646. in this range exclusive or inclusive the range boundaries. Else the
  2647. first step refuses to set the default and the second will fall back
  2648. to (depending on the command used) the highest or lowest priority."
  2649. :group 'org-priorities
  2650. :type 'character)
  2651. (defcustom org-priority-start-cycle-with-default t
  2652. "Non-nil means start with default priority when starting to cycle.
  2653. When this is nil, the first step in the cycle will be (depending on the
  2654. command used) one higher or lower than the default priority.
  2655. See also `org-default-priority'."
  2656. :group 'org-priorities
  2657. :type 'boolean)
  2658. (defcustom org-get-priority-function nil
  2659. "Function to extract the priority from a string.
  2660. The string is normally the headline. If this is nil Org computes the
  2661. priority from the priority cookie like [#A] in the headline. It returns
  2662. an integer, increasing by 1000 for each priority level.
  2663. The user can set a different function here, which should take a string
  2664. as an argument and return the numeric priority."
  2665. :group 'org-priorities
  2666. :version "24.1"
  2667. :type '(choice
  2668. (const nil)
  2669. (function)))
  2670. (defgroup org-time nil
  2671. "Options concerning time stamps and deadlines in Org-mode."
  2672. :tag "Org Time"
  2673. :group 'org)
  2674. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2675. "Number of minutes to round time stamps to.
  2676. These are two values, the first applies when first creating a time stamp.
  2677. The second applies when changing it with the commands `S-up' and `S-down'.
  2678. When changing the time stamp, this means that it will change in steps
  2679. of N minutes, as given by the second value.
  2680. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2681. numbers should be factors of 60, so for example 5, 10, 15.
  2682. When this is larger than 1, you can still force an exact time stamp by using
  2683. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2684. and by using a prefix arg to `S-up/down' to specify the exact number
  2685. of minutes to shift."
  2686. :group 'org-time
  2687. :get (lambda (var) ; Make sure both elements are there
  2688. (if (integerp (default-value var))
  2689. (list (default-value var) 5)
  2690. (default-value var)))
  2691. :type '(list
  2692. (integer :tag "when inserting times")
  2693. (integer :tag "when modifying times")))
  2694. ;; Normalize old customizations of this variable.
  2695. (when (integerp org-time-stamp-rounding-minutes)
  2696. (setq org-time-stamp-rounding-minutes
  2697. (list org-time-stamp-rounding-minutes
  2698. org-time-stamp-rounding-minutes)))
  2699. (defcustom org-display-custom-times nil
  2700. "Non-nil means overlay custom formats over all time stamps.
  2701. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2702. To turn this on on a per-file basis, insert anywhere in the file:
  2703. #+STARTUP: customtime"
  2704. :group 'org-time
  2705. :set 'set-default
  2706. :type 'sexp)
  2707. (make-variable-buffer-local 'org-display-custom-times)
  2708. (defcustom org-time-stamp-custom-formats
  2709. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2710. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2711. These are overlaid over the default ISO format if the variable
  2712. `org-display-custom-times' is set. Time like %H:%M should be at the
  2713. end of the second format. The custom formats are also honored by export
  2714. commands, if custom time display is turned on at the time of export."
  2715. :group 'org-time
  2716. :type 'sexp)
  2717. (defun org-time-stamp-format (&optional long inactive)
  2718. "Get the right format for a time string."
  2719. (let ((f (if long (cdr org-time-stamp-formats)
  2720. (car org-time-stamp-formats))))
  2721. (if inactive
  2722. (concat "[" (substring f 1 -1) "]")
  2723. f)))
  2724. (defcustom org-time-clocksum-format
  2725. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2726. "The format string used when creating CLOCKSUM lines.
  2727. This is also used when Org mode generates a time duration.
  2728. The value can be a single format string containing two
  2729. %-sequences, which will be filled with the number of hours and
  2730. minutes in that order.
  2731. Alternatively, the value can be a plist associating any of the
  2732. keys :years, :months, :weeks, :days, :hours or :minutes with
  2733. format strings. The time duration is formatted using only the
  2734. time components that are needed and concatenating the results.
  2735. If a time unit in absent, it falls back to the next smallest
  2736. unit.
  2737. The keys :require-years, :require-months, :require-days,
  2738. :require-weeks, :require-hours, :require-minutes are also
  2739. meaningful. A non-nil value for these keys indicates that the
  2740. corresponding time component should always be included, even if
  2741. its value is 0.
  2742. For example,
  2743. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2744. :require-minutes t)
  2745. means durations longer than a day will be expressed in days,
  2746. hours and minutes, and durations less than a day will always be
  2747. expressed in hours and minutes (even for durations less than an
  2748. hour).
  2749. The value
  2750. \(:days \"%dd\" :minutes \"%dm\")
  2751. means durations longer than a day will be expressed in days and
  2752. minutes, and durations less than a day will be expressed entirely
  2753. in minutes (even for durations longer than an hour)."
  2754. :group 'org-time
  2755. :group 'org-clock
  2756. :version "24.4"
  2757. :package-version '(Org . "8.0")
  2758. :type '(choice (string :tag "Format string")
  2759. (set :tag "Plist"
  2760. (group :inline t (const :tag "Years" :years)
  2761. (string :tag "Format string"))
  2762. (group :inline t
  2763. (const :tag "Always show years" :require-years)
  2764. (const t))
  2765. (group :inline t (const :tag "Months" :months)
  2766. (string :tag "Format string"))
  2767. (group :inline t
  2768. (const :tag "Always show months" :require-months)
  2769. (const t))
  2770. (group :inline t (const :tag "Weeks" :weeks)
  2771. (string :tag "Format string"))
  2772. (group :inline t
  2773. (const :tag "Always show weeks" :require-weeks)
  2774. (const t))
  2775. (group :inline t (const :tag "Days" :days)
  2776. (string :tag "Format string"))
  2777. (group :inline t
  2778. (const :tag "Always show days" :require-days)
  2779. (const t))
  2780. (group :inline t (const :tag "Hours" :hours)
  2781. (string :tag "Format string"))
  2782. (group :inline t
  2783. (const :tag "Always show hours" :require-hours)
  2784. (const t))
  2785. (group :inline t (const :tag "Minutes" :minutes)
  2786. (string :tag "Format string"))
  2787. (group :inline t
  2788. (const :tag "Always show minutes" :require-minutes)
  2789. (const t)))))
  2790. (defcustom org-time-clocksum-use-fractional nil
  2791. "When non-nil, \\[org-clock-display] uses fractional times.
  2792. See `org-time-clocksum-format' for more on time clock formats."
  2793. :group 'org-time
  2794. :group 'org-clock
  2795. :version "24.3"
  2796. :type 'boolean)
  2797. (defcustom org-time-clocksum-use-effort-durations nil
  2798. "When non-nil, \\[org-clock-display] uses effort durations.
  2799. E.g. by default, one day is considered to be a 8 hours effort,
  2800. so a task that has been clocked for 16 hours will be displayed
  2801. as during 2 days in the clock display or in the clocktable.
  2802. See `org-effort-durations' on how to set effort durations
  2803. and `org-time-clocksum-format' for more on time clock formats."
  2804. :group 'org-time
  2805. :group 'org-clock
  2806. :version "24.4"
  2807. :package-version '(Org . "8.0")
  2808. :type 'boolean)
  2809. (defcustom org-time-clocksum-fractional-format "%.2f"
  2810. "The format string used when creating CLOCKSUM lines,
  2811. or when Org mode generates a time duration, if
  2812. `org-time-clocksum-use-fractional' is enabled.
  2813. The value can be a single format string containing one
  2814. %-sequence, which will be filled with the number of hours as
  2815. a float.
  2816. Alternatively, the value can be a plist associating any of the
  2817. keys :years, :months, :weeks, :days, :hours or :minutes with
  2818. a format string. The time duration is formatted using the
  2819. largest time unit which gives a non-zero integer part. If all
  2820. specified formats have zero integer part, the smallest time unit
  2821. is used."
  2822. :group 'org-time
  2823. :type '(choice (string :tag "Format string")
  2824. (set (group :inline t (const :tag "Years" :years)
  2825. (string :tag "Format string"))
  2826. (group :inline t (const :tag "Months" :months)
  2827. (string :tag "Format string"))
  2828. (group :inline t (const :tag "Weeks" :weeks)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Days" :days)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Hours" :hours)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Minutes" :minutes)
  2835. (string :tag "Format string")))))
  2836. (defcustom org-deadline-warning-days 14
  2837. "Number of days before expiration during which a deadline becomes active.
  2838. This variable governs the display in sparse trees and in the agenda.
  2839. When 0 or negative, it means use this number (the absolute value of it)
  2840. even if a deadline has a different individual lead time specified.
  2841. Custom commands can set this variable in the options section."
  2842. :group 'org-time
  2843. :group 'org-agenda-daily/weekly
  2844. :type 'integer)
  2845. (defcustom org-scheduled-delay-days 0
  2846. "Number of days before a scheduled item becomes active.
  2847. This variable governs the display in sparse trees and in the agenda.
  2848. The default value (i.e. 0) means: don't delay scheduled item.
  2849. When negative, it means use this number (the absolute value of it)
  2850. even if a scheduled item has a different individual delay time
  2851. specified.
  2852. Custom commands can set this variable in the options section."
  2853. :group 'org-time
  2854. :group 'org-agenda-daily/weekly
  2855. :version "24.4"
  2856. :package-version '(Org . "8.0")
  2857. :type 'integer)
  2858. (defcustom org-read-date-prefer-future t
  2859. "Non-nil means assume future for incomplete date input from user.
  2860. This affects the following situations:
  2861. 1. The user gives a month but not a year.
  2862. For example, if it is April and you enter \"feb 2\", this will be read
  2863. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2864. 2. The user gives a day, but no month.
  2865. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2866. read this as the third of *next* month. However, if you enter \"17\",
  2867. it will be considered as *this* month.
  2868. If you set this variable to the symbol `time', then also the following
  2869. will work:
  2870. 3. If the user gives a time.
  2871. If the time is before now, it will be interpreted as tomorrow.
  2872. Currently none of this works for ISO week specifications.
  2873. When this option is nil, the current day, month and year will always be
  2874. used as defaults.
  2875. See also `org-agenda-jump-prefer-future'."
  2876. :group 'org-time
  2877. :type '(choice
  2878. (const :tag "Never" nil)
  2879. (const :tag "Check month and day" t)
  2880. (const :tag "Check month, day, and time" time)))
  2881. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2882. "Should the agenda jump command prefer the future for incomplete dates?
  2883. The default is to do the same as configured in `org-read-date-prefer-future'.
  2884. But you can also set a deviating value here.
  2885. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2886. :group 'org-agenda
  2887. :group 'org-time
  2888. :version "24.1"
  2889. :type '(choice
  2890. (const :tag "Use org-read-date-prefer-future"
  2891. org-read-date-prefer-future)
  2892. (const :tag "Never" nil)
  2893. (const :tag "Always" t)))
  2894. (defcustom org-read-date-force-compatible-dates t
  2895. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2896. Depending on the system Emacs is running on, certain dates cannot
  2897. be represented with the type used internally to represent time.
  2898. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2899. correctly. Some systems allow for earlier dates, some for later,
  2900. some for both. One way to find out it to insert any date into an
  2901. Org buffer, putting the cursor on the year and hitting S-up and
  2902. S-down to test the range.
  2903. When this variable is set to t, the date/time prompt will not let
  2904. you specify dates outside the 1970-2037 range, so it is certain that
  2905. these dates will work in whatever version of Emacs you are
  2906. running, and also that you can move a file from one Emacs implementation
  2907. to another. WHenever Org is forcing the year for you, it will display
  2908. a message and beep.
  2909. When this variable is nil, Org will check if the date is
  2910. representable in the specific Emacs implementation you are using.
  2911. If not, it will force a year, usually the current year, and beep
  2912. to remind you. Currently this setting is not recommended because
  2913. the likelihood that you will open your Org files in an Emacs that
  2914. has limited date range is not negligible.
  2915. A workaround for this problem is to use diary sexp dates for time
  2916. stamps outside of this range."
  2917. :group 'org-time
  2918. :version "24.1"
  2919. :type 'boolean)
  2920. (defcustom org-read-date-display-live t
  2921. "Non-nil means display current interpretation of date prompt live.
  2922. This display will be in an overlay, in the minibuffer."
  2923. :group 'org-time
  2924. :type 'boolean)
  2925. (defcustom org-read-date-popup-calendar t
  2926. "Non-nil means pop up a calendar when prompting for a date.
  2927. In the calendar, the date can be selected with mouse-1. However, the
  2928. minibuffer will also be active, and you can simply enter the date as well.
  2929. When nil, only the minibuffer will be available."
  2930. :group 'org-time
  2931. :type 'boolean)
  2932. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2933. 'org-read-date-popup-calendar)
  2934. (make-obsolete-variable
  2935. 'org-read-date-minibuffer-setup-hook
  2936. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2937. (defcustom org-read-date-minibuffer-setup-hook nil
  2938. "Hook to be used to set up keys for the date/time interface.
  2939. Add key definitions to `minibuffer-local-map', which will be a
  2940. temporary copy.
  2941. WARNING: This option is obsolete, you should use
  2942. `org-read-date-minibuffer-local-map' to set up keys."
  2943. :group 'org-time
  2944. :type 'hook)
  2945. (defcustom org-extend-today-until 0
  2946. "The hour when your day really ends. Must be an integer.
  2947. This has influence for the following applications:
  2948. - When switching the agenda to \"today\". It it is still earlier than
  2949. the time given here, the day recognized as TODAY is actually yesterday.
  2950. - When a date is read from the user and it is still before the time given
  2951. here, the current date and time will be assumed to be yesterday, 23:59.
  2952. Also, timestamps inserted in capture templates follow this rule.
  2953. IMPORTANT: This is a feature whose implementation is and likely will
  2954. remain incomplete. Really, it is only here because past midnight seems to
  2955. be the favorite working time of John Wiegley :-)"
  2956. :group 'org-time
  2957. :type 'integer)
  2958. (defcustom org-use-effective-time nil
  2959. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2960. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2961. \"effective time\" of any timestamps between midnight and 8am will be
  2962. 23:59 of the previous day."
  2963. :group 'org-time
  2964. :version "24.1"
  2965. :type 'boolean)
  2966. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2967. "When non-nil, use the last clock out time for `org-todo'.
  2968. Note that this option has precedence over the combined use of
  2969. `org-use-effective-time' and `org-extend-today-until'."
  2970. :group 'org-time
  2971. :version "24.4"
  2972. :package-version '(Org . "8.0")
  2973. :type 'boolean)
  2974. (defcustom org-edit-timestamp-down-means-later nil
  2975. "Non-nil means S-down will increase the time in a time stamp.
  2976. When nil, S-up will increase."
  2977. :group 'org-time
  2978. :type 'boolean)
  2979. (defcustom org-calendar-follow-timestamp-change t
  2980. "Non-nil means make the calendar window follow timestamp changes.
  2981. When a timestamp is modified and the calendar window is visible, it will be
  2982. moved to the new date."
  2983. :group 'org-time
  2984. :type 'boolean)
  2985. (defgroup org-tags nil
  2986. "Options concerning tags in Org-mode."
  2987. :tag "Org Tags"
  2988. :group 'org)
  2989. (defcustom org-tag-alist nil
  2990. "List of tags allowed in Org-mode files.
  2991. When this list is nil, Org-mode will base TAG input on what is already in the
  2992. buffer.
  2993. The value of this variable is an alist, the car of each entry must be a
  2994. keyword as a string, the cdr may be a character that is used to select
  2995. that tag through the fast-tag-selection interface.
  2996. See the manual for details."
  2997. :group 'org-tags
  2998. :type '(repeat
  2999. (choice
  3000. (cons (string :tag "Tag name")
  3001. (character :tag "Access char"))
  3002. (list :tag "Start radio group"
  3003. (const :startgroup)
  3004. (option (string :tag "Group description")))
  3005. (list :tag "Start tag group, non distinct"
  3006. (const :startgrouptag)
  3007. (option (string :tag "Group description")))
  3008. (list :tag "Group tags delimiter"
  3009. (const :grouptags))
  3010. (list :tag "End radio group"
  3011. (const :endgroup)
  3012. (option (string :tag "Group description")))
  3013. (list :tag "End tag group, non distinct"
  3014. (const :endgrouptag)
  3015. (option (string :tag "Group description")))
  3016. (const :tag "New line" (:newline)))))
  3017. (defcustom org-tag-persistent-alist nil
  3018. "List of tags that will always appear in all Org-mode files.
  3019. This is in addition to any in buffer settings or customizations
  3020. of `org-tag-alist'.
  3021. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3022. The value of this variable is an alist, the car of each entry must be a
  3023. keyword as a string, the cdr may be a character that is used to select
  3024. that tag through the fast-tag-selection interface.
  3025. See the manual for details.
  3026. To disable these tags on a per-file basis, insert anywhere in the file:
  3027. #+STARTUP: noptag"
  3028. :group 'org-tags
  3029. :type '(repeat
  3030. (choice
  3031. (cons (string :tag "Tag name")
  3032. (character :tag "Access char"))
  3033. (const :tag "Start radio group" (:startgroup))
  3034. (const :tag "Group tags delimiter" (:grouptags))
  3035. (const :tag "End radio group" (:endgroup))
  3036. (const :tag "New line" (:newline)))))
  3037. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3038. "If non-nil, always offer completion for all tags of all agenda files.
  3039. Instead of customizing this variable directly, you might want to
  3040. set it locally for capture buffers, because there no list of
  3041. tags in that file can be created dynamically (there are none).
  3042. (add-hook 'org-capture-mode-hook
  3043. (lambda ()
  3044. (set (make-local-variable
  3045. 'org-complete-tags-always-offer-all-agenda-tags)
  3046. t)))"
  3047. :group 'org-tags
  3048. :version "24.1"
  3049. :type 'boolean)
  3050. (defvar org-file-tags nil
  3051. "List of tags that can be inherited by all entries in the file.
  3052. The tags will be inherited if the variable `org-use-tag-inheritance'
  3053. says they should be.
  3054. This variable is populated from #+FILETAGS lines.")
  3055. (defcustom org-use-fast-tag-selection 'auto
  3056. "Non-nil means use fast tag selection scheme.
  3057. This is a special interface to select and deselect tags with single keys.
  3058. When nil, fast selection is never used.
  3059. When the symbol `auto', fast selection is used if and only if selection
  3060. characters for tags have been configured, either through the variable
  3061. `org-tag-alist' or through a #+TAGS line in the buffer.
  3062. When t, fast selection is always used and selection keys are assigned
  3063. automatically if necessary."
  3064. :group 'org-tags
  3065. :type '(choice
  3066. (const :tag "Always" t)
  3067. (const :tag "Never" nil)
  3068. (const :tag "When selection characters are configured" auto)))
  3069. (defcustom org-fast-tag-selection-single-key nil
  3070. "Non-nil means fast tag selection exits after first change.
  3071. When nil, you have to press RET to exit it.
  3072. During fast tag selection, you can toggle this flag with `C-c'.
  3073. This variable can also have the value `expert'. In this case, the window
  3074. displaying the tags menu is not even shown, until you press C-c again."
  3075. :group 'org-tags
  3076. :type '(choice
  3077. (const :tag "No" nil)
  3078. (const :tag "Yes" t)
  3079. (const :tag "Expert" expert)))
  3080. (defvar org-fast-tag-selection-include-todo nil
  3081. "Non-nil means fast tags selection interface will also offer TODO states.
  3082. This is an undocumented feature, you should not rely on it.")
  3083. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3084. "The column to which tags should be indented in a headline.
  3085. If this number is positive, it specifies the column. If it is negative,
  3086. it means that the tags should be flushright to that column. For example,
  3087. -80 works well for a normal 80 character screen.
  3088. When 0, place tags directly after headline text, with only one space in
  3089. between."
  3090. :group 'org-tags
  3091. :type 'integer)
  3092. (defcustom org-auto-align-tags t
  3093. "Non-nil keeps tags aligned when modifying headlines.
  3094. Some operations (i.e. demoting) change the length of a headline and
  3095. therefore shift the tags around. With this option turned on, after
  3096. each such operation the tags are again aligned to `org-tags-column'."
  3097. :group 'org-tags
  3098. :type 'boolean)
  3099. (defcustom org-use-tag-inheritance t
  3100. "Non-nil means tags in levels apply also for sublevels.
  3101. When nil, only the tags directly given in a specific line apply there.
  3102. This may also be a list of tags that should be inherited, or a regexp that
  3103. matches tags that should be inherited. Additional control is possible
  3104. with the variable `org-tags-exclude-from-inheritance' which gives an
  3105. explicit list of tags to be excluded from inheritance, even if the value of
  3106. `org-use-tag-inheritance' would select it for inheritance.
  3107. If this option is t, a match early-on in a tree can lead to a large
  3108. number of matches in the subtree when constructing the agenda or creating
  3109. a sparse tree. If you only want to see the first match in a tree during
  3110. a search, check out the variable `org-tags-match-list-sublevels'."
  3111. :group 'org-tags
  3112. :type '(choice
  3113. (const :tag "Not" nil)
  3114. (const :tag "Always" t)
  3115. (repeat :tag "Specific tags" (string :tag "Tag"))
  3116. (regexp :tag "Tags matched by regexp")))
  3117. (defcustom org-tags-exclude-from-inheritance nil
  3118. "List of tags that should never be inherited.
  3119. This is a way to exclude a few tags from inheritance. For way to do
  3120. the opposite, to actively allow inheritance for selected tags,
  3121. see the variable `org-use-tag-inheritance'."
  3122. :group 'org-tags
  3123. :type '(repeat (string :tag "Tag")))
  3124. (defun org-tag-inherit-p (tag)
  3125. "Check if TAG is one that should be inherited."
  3126. (cond
  3127. ((member tag org-tags-exclude-from-inheritance) nil)
  3128. ((eq org-use-tag-inheritance t) t)
  3129. ((not org-use-tag-inheritance) nil)
  3130. ((stringp org-use-tag-inheritance)
  3131. (string-match org-use-tag-inheritance tag))
  3132. ((listp org-use-tag-inheritance)
  3133. (member tag org-use-tag-inheritance))
  3134. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3135. (defcustom org-tags-match-list-sublevels t
  3136. "Non-nil means list also sublevels of headlines matching a search.
  3137. This variable applies to tags/property searches, and also to stuck
  3138. projects because this search is based on a tags match as well.
  3139. When set to the symbol `indented', sublevels are indented with
  3140. leading dots.
  3141. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3142. the sublevels of a headline matching a tag search often also match
  3143. the same search. Listing all of them can create very long lists.
  3144. Setting this variable to nil causes subtrees of a match to be skipped.
  3145. This variable is semi-obsolete and probably should always be true. It
  3146. is better to limit inheritance to certain tags using the variables
  3147. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3148. :group 'org-tags
  3149. :type '(choice
  3150. (const :tag "No, don't list them" nil)
  3151. (const :tag "Yes, do list them" t)
  3152. (const :tag "List them, indented with leading dots" indented)))
  3153. (defcustom org-tags-sort-function nil
  3154. "When set, tags are sorted using this function as a comparator."
  3155. :group 'org-tags
  3156. :type '(choice
  3157. (const :tag "No sorting" nil)
  3158. (const :tag "Alphabetical" string<)
  3159. (const :tag "Reverse alphabetical" string>)
  3160. (function :tag "Custom function" nil)))
  3161. (defvar org-tags-history nil
  3162. "History of minibuffer reads for tags.")
  3163. (defvar org-last-tags-completion-table nil
  3164. "The last used completion table for tags.")
  3165. (defvar org-after-tags-change-hook nil
  3166. "Hook that is run after the tags in a line have changed.")
  3167. (defgroup org-properties nil
  3168. "Options concerning properties in Org-mode."
  3169. :tag "Org Properties"
  3170. :group 'org)
  3171. (defcustom org-property-format "%-10s %s"
  3172. "How property key/value pairs should be formatted by `indent-line'.
  3173. When `indent-line' hits a property definition, it will format the line
  3174. according to this format, mainly to make sure that the values are
  3175. lined-up with respect to each other."
  3176. :group 'org-properties
  3177. :type 'string)
  3178. (defcustom org-properties-postprocess-alist nil
  3179. "Alist of properties and functions to adjust inserted values.
  3180. Elements of this alist must be of the form
  3181. ([string] [function])
  3182. where [string] must be a property name and [function] must be a
  3183. lambda expression: this lambda expression must take one argument,
  3184. the value to adjust, and return the new value as a string.
  3185. For example, this element will allow the property \"Remaining\"
  3186. to be updated wrt the relation between the \"Effort\" property
  3187. and the clock summary:
  3188. ((\"Remaining\" (lambda(value)
  3189. (let ((clocksum (org-clock-sum-current-item))
  3190. (effort (org-duration-string-to-minutes
  3191. (org-entry-get (point) \"Effort\"))))
  3192. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3193. :group 'org-properties
  3194. :version "24.1"
  3195. :type '(alist :key-type (string :tag "Property")
  3196. :value-type (function :tag "Function")))
  3197. (defcustom org-use-property-inheritance nil
  3198. "Non-nil means properties apply also for sublevels.
  3199. This setting is chiefly used during property searches. Turning it on can
  3200. cause significant overhead when doing a search, which is why it is not
  3201. on by default.
  3202. When nil, only the properties directly given in the current entry count.
  3203. When t, every property is inherited. The value may also be a list of
  3204. properties that should have inheritance, or a regular expression matching
  3205. properties that should be inherited.
  3206. However, note that some special properties use inheritance under special
  3207. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3208. and the properties ending in \"_ALL\" when they are used as descriptor
  3209. for valid values of a property.
  3210. Note for programmers:
  3211. When querying an entry with `org-entry-get', you can control if inheritance
  3212. should be used. By default, `org-entry-get' looks only at the local
  3213. properties. You can request inheritance by setting the inherit argument
  3214. to t (to force inheritance) or to `selective' (to respect the setting
  3215. in this variable)."
  3216. :group 'org-properties
  3217. :type '(choice
  3218. (const :tag "Not" nil)
  3219. (const :tag "Always" t)
  3220. (repeat :tag "Specific properties" (string :tag "Property"))
  3221. (regexp :tag "Properties matched by regexp")))
  3222. (defun org-property-inherit-p (property)
  3223. "Check if PROPERTY is one that should be inherited."
  3224. (cond
  3225. ((eq org-use-property-inheritance t) t)
  3226. ((not org-use-property-inheritance) nil)
  3227. ((stringp org-use-property-inheritance)
  3228. (string-match org-use-property-inheritance property))
  3229. ((listp org-use-property-inheritance)
  3230. (member property org-use-property-inheritance))
  3231. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3232. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3233. "The default column format, if no other format has been defined.
  3234. This variable can be set on the per-file basis by inserting a line
  3235. #+COLUMNS: %25ITEM ....."
  3236. :group 'org-properties
  3237. :type 'string)
  3238. (defcustom org-columns-ellipses ".."
  3239. "The ellipses to be used when a field in column view is truncated.
  3240. When this is the empty string, as many characters as possible are shown,
  3241. but then there will be no visual indication that the field has been truncated.
  3242. When this is a string of length N, the last N characters of a truncated
  3243. field are replaced by this string. If the column is narrower than the
  3244. ellipses string, only part of the ellipses string will be shown."
  3245. :group 'org-properties
  3246. :type 'string)
  3247. (defcustom org-columns-modify-value-for-display-function nil
  3248. "Function that modifies values for display in column view.
  3249. For example, it can be used to cut out a certain part from a time stamp.
  3250. The function must take 2 arguments:
  3251. column-title The title of the column (*not* the property name)
  3252. value The value that should be modified.
  3253. The function should return the value that should be displayed,
  3254. or nil if the normal value should be used."
  3255. :group 'org-properties
  3256. :type '(choice (const nil) (function)))
  3257. (defconst org-global-properties-fixed
  3258. '(("VISIBILITY_ALL" . "folded children content all")
  3259. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3260. "List of property/value pairs that can be inherited by any entry.
  3261. These are fixed values, for the preset properties. The user variable
  3262. that can be used to add to this list is `org-global-properties'.
  3263. The entries in this list are cons cells where the car is a property
  3264. name and cdr is a string with the value. If the value represents
  3265. multiple items like an \"_ALL\" property, separate the items by
  3266. spaces.")
  3267. (defcustom org-global-properties nil
  3268. "List of property/value pairs that can be inherited by any entry.
  3269. This list will be combined with the constant `org-global-properties-fixed'.
  3270. The entries in this list are cons cells where the car is a property
  3271. name and cdr is a string with the value.
  3272. You can set buffer-local values for the same purpose in the variable
  3273. `org-file-properties' this by adding lines like
  3274. #+PROPERTY: NAME VALUE"
  3275. :group 'org-properties
  3276. :type '(repeat
  3277. (cons (string :tag "Property")
  3278. (string :tag "Value"))))
  3279. (defvar org-file-properties nil
  3280. "List of property/value pairs that can be inherited by any entry.
  3281. Valid for the current buffer.
  3282. This variable is populated from #+PROPERTY lines.")
  3283. (make-variable-buffer-local 'org-file-properties)
  3284. (defgroup org-agenda nil
  3285. "Options concerning agenda views in Org-mode."
  3286. :tag "Org Agenda"
  3287. :group 'org)
  3288. (defvar org-category nil
  3289. "Variable used by org files to set a category for agenda display.
  3290. Such files should use a file variable to set it, for example
  3291. # -*- mode: org; org-category: \"ELisp\"
  3292. or contain a special line
  3293. #+CATEGORY: ELisp
  3294. If the file does not specify a category, then file's base name
  3295. is used instead.")
  3296. (make-variable-buffer-local 'org-category)
  3297. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3298. (defcustom org-agenda-files nil
  3299. "The files to be used for agenda display.
  3300. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3301. \\[org-remove-file]. You can also use customize to edit the list.
  3302. If an entry is a directory, all files in that directory that are matched by
  3303. `org-agenda-file-regexp' will be part of the file list.
  3304. If the value of the variable is not a list but a single file name, then
  3305. the list of agenda files is actually stored and maintained in that file, one
  3306. agenda file per line. In this file paths can be given relative to
  3307. `org-directory'. Tilde expansion and environment variable substitution
  3308. are also made."
  3309. :group 'org-agenda
  3310. :type '(choice
  3311. (repeat :tag "List of files and directories" file)
  3312. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3313. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3314. "Regular expression to match files for `org-agenda-files'.
  3315. If any element in the list in that variable contains a directory instead
  3316. of a normal file, all files in that directory that are matched by this
  3317. regular expression will be included."
  3318. :group 'org-agenda
  3319. :type 'regexp)
  3320. (defcustom org-agenda-text-search-extra-files nil
  3321. "List of extra files to be searched by text search commands.
  3322. These files will be searched in addition to the agenda files by the
  3323. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3324. Note that these files will only be searched for text search commands,
  3325. not for the other agenda views like todo lists, tag searches or the weekly
  3326. agenda. This variable is intended to list notes and possibly archive files
  3327. that should also be searched by these two commands.
  3328. In fact, if the first element in the list is the symbol `agenda-archives',
  3329. then all archive files of all agenda files will be added to the search
  3330. scope."
  3331. :group 'org-agenda
  3332. :type '(set :greedy t
  3333. (const :tag "Agenda Archives" agenda-archives)
  3334. (repeat :inline t (file))))
  3335. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3336. 'org-agenda-text-search-extra-files)
  3337. (defcustom org-agenda-skip-unavailable-files nil
  3338. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3339. A nil value means to remove them, after a query, from the list."
  3340. :group 'org-agenda
  3341. :type 'boolean)
  3342. (defcustom org-calendar-to-agenda-key [?c]
  3343. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3344. The command `org-calendar-goto-agenda' will be bound to this key. The
  3345. default is the character `c' because then `c' can be used to switch back and
  3346. forth between agenda and calendar."
  3347. :group 'org-agenda
  3348. :type 'sexp)
  3349. (defcustom org-calendar-insert-diary-entry-key [?i]
  3350. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3351. This option is irrelevant until `org-agenda-diary-file' has been configured
  3352. to point to an Org-mode file. When that is the case, the command
  3353. `org-agenda-diary-entry' will be bound to the key given here, by default
  3354. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3355. if you want to continue doing this, you need to change this to a different
  3356. key."
  3357. :group 'org-agenda
  3358. :type 'sexp)
  3359. (defcustom org-agenda-diary-file 'diary-file
  3360. "File to which to add new entries with the `i' key in agenda and calendar.
  3361. When this is the symbol `diary-file', the functionality in the Emacs
  3362. calendar will be used to add entries to the `diary-file'. But when this
  3363. points to a file, `org-agenda-diary-entry' will be used instead."
  3364. :group 'org-agenda
  3365. :type '(choice
  3366. (const :tag "The standard Emacs diary file" diary-file)
  3367. (file :tag "Special Org file diary entries")))
  3368. (eval-after-load "calendar"
  3369. '(progn
  3370. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3371. 'org-calendar-goto-agenda)
  3372. (add-hook 'calendar-mode-hook
  3373. (lambda ()
  3374. (unless (eq org-agenda-diary-file 'diary-file)
  3375. (define-key calendar-mode-map
  3376. org-calendar-insert-diary-entry-key
  3377. 'org-agenda-diary-entry))))))
  3378. (defgroup org-latex nil
  3379. "Options for embedding LaTeX code into Org-mode."
  3380. :tag "Org LaTeX"
  3381. :group 'org)
  3382. (defcustom org-format-latex-options
  3383. '(:foreground default :background default :scale 1.0
  3384. :html-foreground "Black" :html-background "Transparent"
  3385. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3386. "Options for creating images from LaTeX fragments.
  3387. This is a property list with the following properties:
  3388. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3389. `default' means use the foreground of the default face.
  3390. `auto' means use the foreground from the text face.
  3391. :background the background color, or \"Transparent\".
  3392. `default' means use the background of the default face.
  3393. `auto' means use the background from the text face.
  3394. :scale a scaling factor for the size of the images, to get more pixels
  3395. :html-foreground, :html-background, :html-scale
  3396. the same numbers for HTML export.
  3397. :matchers a list indicating which matchers should be used to
  3398. find LaTeX fragments. Valid members of this list are:
  3399. \"begin\" find environments
  3400. \"$1\" find single characters surrounded by $.$
  3401. \"$\" find math expressions surrounded by $...$
  3402. \"$$\" find math expressions surrounded by $$....$$
  3403. \"\\(\" find math expressions surrounded by \\(...\\)
  3404. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3405. :group 'org-latex
  3406. :type 'plist)
  3407. (defcustom org-format-latex-signal-error t
  3408. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3409. When nil, just push out a message."
  3410. :group 'org-latex
  3411. :version "24.1"
  3412. :type 'boolean)
  3413. (defcustom org-latex-to-mathml-jar-file nil
  3414. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3415. Use this to specify additional executable file say a jar file.
  3416. When using MathToWeb as the converter, specify the full-path to
  3417. your mathtoweb.jar file."
  3418. :group 'org-latex
  3419. :version "24.1"
  3420. :type '(choice
  3421. (const :tag "None" nil)
  3422. (file :tag "JAR file" :must-match t)))
  3423. (defcustom org-latex-to-mathml-convert-command nil
  3424. "Command to convert LaTeX fragments to MathML.
  3425. Replace format-specifiers in the command as noted below and use
  3426. `shell-command' to convert LaTeX to MathML.
  3427. %j: Executable file in fully expanded form as specified by
  3428. `org-latex-to-mathml-jar-file'.
  3429. %I: Input LaTeX file in fully expanded form.
  3430. %i: The latex fragment to be converted.
  3431. %o: Output MathML file.
  3432. This command is used by `org-create-math-formula'.
  3433. When using MathToWeb as the converter, set this option to
  3434. \"java -jar %j -unicode -force -df %o %I\".
  3435. When using LaTeXML set this option to
  3436. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3437. :group 'org-latex
  3438. :version "24.1"
  3439. :type '(choice
  3440. (const :tag "None" nil)
  3441. (string :tag "\nShell command")))
  3442. (defcustom org-latex-create-formula-image-program 'dvipng
  3443. "Program to convert LaTeX fragments with.
  3444. dvipng Process the LaTeX fragments to dvi file, then convert
  3445. dvi files to png files using dvipng.
  3446. This will also include processing of non-math environments.
  3447. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3448. to convert pdf files to png files"
  3449. :group 'org-latex
  3450. :version "24.1"
  3451. :type '(choice
  3452. (const :tag "dvipng" dvipng)
  3453. (const :tag "imagemagick" imagemagick)))
  3454. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3455. "Path to store latex preview images.
  3456. A relative path here creates many directories relative to the
  3457. processed org files paths. An absolute path puts all preview
  3458. images at the same place."
  3459. :group 'org-latex
  3460. :version "24.3"
  3461. :type 'string)
  3462. (defun org-format-latex-mathml-available-p ()
  3463. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3464. (save-match-data
  3465. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3466. org-latex-to-mathml-convert-command)
  3467. (let ((executable (car (split-string
  3468. org-latex-to-mathml-convert-command))))
  3469. (when (executable-find executable)
  3470. (if (string-match
  3471. "%j" org-latex-to-mathml-convert-command)
  3472. (file-readable-p org-latex-to-mathml-jar-file)
  3473. t))))))
  3474. (defcustom org-format-latex-header "\\documentclass{article}
  3475. \\usepackage[usenames]{color}
  3476. \[PACKAGES]
  3477. \[DEFAULT-PACKAGES]
  3478. \\pagestyle{empty} % do not remove
  3479. % The settings below are copied from fullpage.sty
  3480. \\setlength{\\textwidth}{\\paperwidth}
  3481. \\addtolength{\\textwidth}{-3cm}
  3482. \\setlength{\\oddsidemargin}{1.5cm}
  3483. \\addtolength{\\oddsidemargin}{-2.54cm}
  3484. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3485. \\setlength{\\textheight}{\\paperheight}
  3486. \\addtolength{\\textheight}{-\\headheight}
  3487. \\addtolength{\\textheight}{-\\headsep}
  3488. \\addtolength{\\textheight}{-\\footskip}
  3489. \\addtolength{\\textheight}{-3cm}
  3490. \\setlength{\\topmargin}{1.5cm}
  3491. \\addtolength{\\topmargin}{-2.54cm}"
  3492. "The document header used for processing LaTeX fragments.
  3493. It is imperative that this header make sure that no page number
  3494. appears on the page. The package defined in the variables
  3495. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3496. will either replace the placeholder \"[PACKAGES]\" in this
  3497. header, or they will be appended."
  3498. :group 'org-latex
  3499. :type 'string)
  3500. (defun org-set-packages-alist (var val)
  3501. "Set the packages alist and make sure it has 3 elements per entry."
  3502. (set var (mapcar (lambda (x)
  3503. (if (and (consp x) (= (length x) 2))
  3504. (list (car x) (nth 1 x) t)
  3505. x))
  3506. val)))
  3507. (defun org-get-packages-alist (var)
  3508. "Get the packages alist and make sure it has 3 elements per entry."
  3509. (mapcar (lambda (x)
  3510. (if (and (consp x) (= (length x) 2))
  3511. (list (car x) (nth 1 x) t)
  3512. x))
  3513. (default-value var)))
  3514. (defcustom org-latex-default-packages-alist
  3515. '(("AUTO" "inputenc" t)
  3516. ("T1" "fontenc" t)
  3517. ("" "fixltx2e" nil)
  3518. ("" "graphicx" t)
  3519. ("" "grffile" t)
  3520. ("" "longtable" nil)
  3521. ("" "wrapfig" nil)
  3522. ("" "rotating" nil)
  3523. ("normalem" "ulem" t)
  3524. ("" "amsmath" t)
  3525. ("" "textcomp" t)
  3526. ("" "amssymb" t)
  3527. ("" "capt-of" nil)
  3528. ("" "hyperref" nil))
  3529. "Alist of default packages to be inserted in the header.
  3530. Change this only if one of the packages here causes an
  3531. incompatibility with another package you are using.
  3532. The packages in this list are needed by one part or another of
  3533. Org mode to function properly:
  3534. - inputenc, fontenc: for basic font and character selection
  3535. - fixltx2e: Important patches of LaTeX itself
  3536. - graphicx: for including images
  3537. - grffile: allow periods and spaces in graphics file names
  3538. - longtable: For multipage tables
  3539. - wrapfig: for figure placement
  3540. - rotating: for sideways figures and tables
  3541. - ulem: for underline and strike-through
  3542. - amsmath: for subscript and superscript and math environments
  3543. - textcomp, amssymb: for various symbols used
  3544. for interpreting the entities in `org-entities'. You can skip
  3545. some of these packages if you don't use any of their symbols.
  3546. - capt-of: for captions outside of floats
  3547. - hyperref: for cross references
  3548. Therefore you should not modify this variable unless you know
  3549. what you are doing. The one reason to change it anyway is that
  3550. you might be loading some other package that conflicts with one
  3551. of the default packages. Each element is either a cell or
  3552. a string.
  3553. A cell is of the format
  3554. \(\"options\" \"package\" SNIPPET-FLAG)
  3555. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3556. when compiling LaTeX snippets into images for inclusion into
  3557. non-LaTeX output.
  3558. A string will be inserted as-is in the header of the document."
  3559. :group 'org-latex
  3560. :group 'org-export-latex
  3561. :set 'org-set-packages-alist
  3562. :get 'org-get-packages-alist
  3563. :version "25.1"
  3564. :package-version '(Org . "8.3")
  3565. :type '(repeat
  3566. (choice
  3567. (list :tag "options/package pair"
  3568. (string :tag "options")
  3569. (string :tag "package")
  3570. (boolean :tag "Snippet"))
  3571. (string :tag "A line of LaTeX"))))
  3572. (defcustom org-latex-packages-alist nil
  3573. "Alist of packages to be inserted in every LaTeX header.
  3574. These will be inserted after `org-latex-default-packages-alist'.
  3575. Each element is either a cell or a string.
  3576. A cell is of the format:
  3577. \(\"options\" \"package\" SNIPPET-FLAG)
  3578. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3579. needed when turning LaTeX snippets into images for inclusion into
  3580. non-LaTeX output.
  3581. A string will be inserted as-is in the header of the document.
  3582. Make sure that you only list packages here which:
  3583. - you want in every file;
  3584. - do not conflict with the setup in `org-format-latex-header';
  3585. - do not conflict with the default packages in
  3586. `org-latex-default-packages-alist'."
  3587. :group 'org-latex
  3588. :group 'org-export-latex
  3589. :set 'org-set-packages-alist
  3590. :get 'org-get-packages-alist
  3591. :type '(repeat
  3592. (choice
  3593. (list :tag "options/package pair"
  3594. (string :tag "options")
  3595. (string :tag "package")
  3596. (boolean :tag "Snippet"))
  3597. (string :tag "A line of LaTeX"))))
  3598. (defgroup org-appearance nil
  3599. "Settings for Org-mode appearance."
  3600. :tag "Org Appearance"
  3601. :group 'org)
  3602. (defcustom org-level-color-stars-only nil
  3603. "Non-nil means fontify only the stars in each headline.
  3604. When nil, the entire headline is fontified.
  3605. Changing it requires restart of `font-lock-mode' to become effective
  3606. also in regions already fontified."
  3607. :group 'org-appearance
  3608. :type 'boolean)
  3609. (defcustom org-hide-leading-stars nil
  3610. "Non-nil means hide the first N-1 stars in a headline.
  3611. This works by using the face `org-hide' for these stars. This
  3612. face is white for a light background, and black for a dark
  3613. background. You may have to customize the face `org-hide' to
  3614. make this work.
  3615. Changing it requires restart of `font-lock-mode' to become effective
  3616. also in regions already fontified.
  3617. You may also set this on a per-file basis by adding one of the following
  3618. lines to the buffer:
  3619. #+STARTUP: hidestars
  3620. #+STARTUP: showstars"
  3621. :group 'org-appearance
  3622. :type 'boolean)
  3623. (defcustom org-hidden-keywords nil
  3624. "List of symbols corresponding to keywords to be hidden the org buffer.
  3625. For example, a value '(title) for this list will make the document's title
  3626. appear in the buffer without the initial #+TITLE: keyword."
  3627. :group 'org-appearance
  3628. :version "24.1"
  3629. :type '(set (const :tag "#+AUTHOR" author)
  3630. (const :tag "#+DATE" date)
  3631. (const :tag "#+EMAIL" email)
  3632. (const :tag "#+TITLE" title)))
  3633. (defcustom org-custom-properties nil
  3634. "List of properties (as strings) with a special meaning.
  3635. The default use of these custom properties is to let the user
  3636. hide them with `org-toggle-custom-properties-visibility'."
  3637. :group 'org-properties
  3638. :group 'org-appearance
  3639. :version "24.3"
  3640. :type '(repeat (string :tag "Property Name")))
  3641. (defcustom org-fontify-done-headline nil
  3642. "Non-nil means change the face of a headline if it is marked DONE.
  3643. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3644. When this is non-nil, the headline after the keyword is set to the
  3645. `org-headline-done' as an additional indication."
  3646. :group 'org-appearance
  3647. :type 'boolean)
  3648. (defcustom org-fontify-emphasized-text t
  3649. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3650. Changing this variable requires a restart of Emacs to take effect."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-fontify-whole-heading-line nil
  3654. "Non-nil means fontify the whole line for headings.
  3655. This is useful when setting a background color for the
  3656. org-level-* faces."
  3657. :group 'org-appearance
  3658. :type 'boolean)
  3659. (defcustom org-highlight-latex-and-related nil
  3660. "Non-nil means highlight LaTeX related syntax in the buffer.
  3661. When non nil, the value should be a list containing any of the
  3662. following symbols:
  3663. `latex' Highlight LaTeX snippets and environments.
  3664. `script' Highlight subscript and superscript.
  3665. `entities' Highlight entities."
  3666. :group 'org-appearance
  3667. :version "24.4"
  3668. :package-version '(Org . "8.0")
  3669. :type '(choice
  3670. (const :tag "No highlighting" nil)
  3671. (set :greedy t :tag "Highlight"
  3672. (const :tag "LaTeX snippets and environments" latex)
  3673. (const :tag "Subscript and superscript" script)
  3674. (const :tag "Entities" entities))))
  3675. (defcustom org-hide-emphasis-markers nil
  3676. "Non-nil mean font-lock should hide the emphasis marker characters."
  3677. :group 'org-appearance
  3678. :type 'boolean)
  3679. (defcustom org-hide-macro-markers nil
  3680. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3681. :group 'org-appearance
  3682. :type 'boolean)
  3683. (defcustom org-pretty-entities nil
  3684. "Non-nil means show entities as UTF8 characters.
  3685. When nil, the \\name form remains in the buffer."
  3686. :group 'org-appearance
  3687. :version "24.1"
  3688. :type 'boolean)
  3689. (defcustom org-pretty-entities-include-sub-superscripts t
  3690. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3691. :group 'org-appearance
  3692. :version "24.1"
  3693. :type 'boolean)
  3694. (defvar org-emph-re nil
  3695. "Regular expression for matching emphasis.
  3696. After a match, the match groups contain these elements:
  3697. 0 The match of the full regular expression, including the characters
  3698. before and after the proper match
  3699. 1 The character before the proper match, or empty at beginning of line
  3700. 2 The proper match, including the leading and trailing markers
  3701. 3 The leading marker like * or /, indicating the type of highlighting
  3702. 4 The text between the emphasis markers, not including the markers
  3703. 5 The character after the match, empty at the end of a line")
  3704. (defvar org-verbatim-re nil
  3705. "Regular expression for matching verbatim text.")
  3706. (defvar org-emphasis-regexp-components) ; defined just below
  3707. (defvar org-emphasis-alist) ; defined just below
  3708. (defun org-set-emph-re (var val)
  3709. "Set variable and compute the emphasis regular expression."
  3710. (set var val)
  3711. (when (and (boundp 'org-emphasis-alist)
  3712. (boundp 'org-emphasis-regexp-components)
  3713. org-emphasis-alist org-emphasis-regexp-components)
  3714. (let* ((e org-emphasis-regexp-components)
  3715. (pre (car e))
  3716. (post (nth 1 e))
  3717. (border (nth 2 e))
  3718. (body (nth 3 e))
  3719. (nl (nth 4 e))
  3720. (body1 (concat body "*?"))
  3721. (markers (mapconcat 'car org-emphasis-alist ""))
  3722. (vmarkers (mapconcat
  3723. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3724. org-emphasis-alist "")))
  3725. ;; make sure special characters appear at the right position in the class
  3726. (if (string-match "\\^" markers)
  3727. (setq markers (concat (replace-match "" t t markers) "^")))
  3728. (if (string-match "-" markers)
  3729. (setq markers (concat (replace-match "" t t markers) "-")))
  3730. (if (string-match "\\^" vmarkers)
  3731. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3732. (if (string-match "-" vmarkers)
  3733. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3734. (if (> nl 0)
  3735. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3736. (int-to-string nl) "\\}")))
  3737. ;; Make the regexp
  3738. (setq org-emph-re
  3739. (concat "\\([" pre "]\\|^\\)"
  3740. "\\("
  3741. "\\([" markers "]\\)"
  3742. "\\("
  3743. "[^" border "]\\|"
  3744. "[^" border "]"
  3745. body1
  3746. "[^" border "]"
  3747. "\\)"
  3748. "\\3\\)"
  3749. "\\([" post "]\\|$\\)"))
  3750. (setq org-verbatim-re
  3751. (concat "\\([" pre "]\\|^\\)"
  3752. "\\("
  3753. "\\([" vmarkers "]\\)"
  3754. "\\("
  3755. "[^" border "]\\|"
  3756. "[^" border "]"
  3757. body1
  3758. "[^" border "]"
  3759. "\\)"
  3760. "\\3\\)"
  3761. "\\([" post "]\\|$\\)")))))
  3762. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3763. ;; set this option proved cumbersome. See this message/thread:
  3764. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3765. (defvar org-emphasis-regexp-components
  3766. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3767. "Components used to build the regular expression for emphasis.
  3768. This is a list with five entries. Terminology: In an emphasis string
  3769. like \" *strong word* \", we call the initial space PREMATCH, the final
  3770. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3771. and \"trong wor\" is the body. The different components in this variable
  3772. specify what is allowed/forbidden in each part:
  3773. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3774. post Chars allowed as postmatch. End of line will be allowed too.
  3775. border The chars *forbidden* as border characters.
  3776. body-regexp A regexp like \".\" to match a body character. Don't use
  3777. non-shy groups here, and don't allow newline here.
  3778. newline The maximum number of newlines allowed in an emphasis exp.
  3779. You need to reload Org or to restart Emacs after customizing this.")
  3780. (defcustom org-emphasis-alist
  3781. `(("*" bold)
  3782. ("/" italic)
  3783. ("_" underline)
  3784. ("=" org-verbatim verbatim)
  3785. ("~" org-code verbatim)
  3786. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3787. "Alist of characters and faces to emphasize text.
  3788. Text starting and ending with a special character will be emphasized,
  3789. for example *bold*, _underlined_ and /italic/. This variable sets the
  3790. marker characters and the face to be used by font-lock for highlighting
  3791. in Org-mode Emacs buffers.
  3792. You need to reload Org or to restart Emacs after customizing this."
  3793. :group 'org-appearance
  3794. :set 'org-set-emph-re
  3795. :version "24.4"
  3796. :package-version '(Org . "8.0")
  3797. :type '(repeat
  3798. (list
  3799. (string :tag "Marker character")
  3800. (choice
  3801. (face :tag "Font-lock-face")
  3802. (plist :tag "Face property list"))
  3803. (option (const verbatim)))))
  3804. (defvar org-protecting-blocks
  3805. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3806. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3807. This is needed for font-lock setup.")
  3808. ;;; Miscellaneous options
  3809. (defgroup org-completion nil
  3810. "Completion in Org-mode."
  3811. :tag "Org Completion"
  3812. :group 'org)
  3813. (defcustom org-completion-use-ido nil
  3814. "Non-nil means use ido completion wherever possible.
  3815. Note that `ido-mode' must be active for this variable to be relevant.
  3816. If you decide to turn this variable on, you might well want to turn off
  3817. `org-outline-path-complete-in-steps'.
  3818. See also `org-completion-use-iswitchb'."
  3819. :group 'org-completion
  3820. :type 'boolean)
  3821. (defcustom org-completion-use-iswitchb nil
  3822. "Non-nil means use iswitchb completion wherever possible.
  3823. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3824. If you decide to turn this variable on, you might well want to turn off
  3825. `org-outline-path-complete-in-steps'.
  3826. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3827. :group 'org-completion
  3828. :type 'boolean)
  3829. (defcustom org-completion-fallback-command 'hippie-expand
  3830. "The expansion command called by \\[pcomplete] in normal context.
  3831. Normal means, no org-mode-specific context."
  3832. :group 'org-completion
  3833. :type 'function)
  3834. ;;; Functions and variables from their packages
  3835. ;; Declared here to avoid compiler warnings
  3836. ;; XEmacs only
  3837. (defvar outline-mode-menu-heading)
  3838. (defvar outline-mode-menu-show)
  3839. (defvar outline-mode-menu-hide)
  3840. (defvar zmacs-regions) ; XEmacs regions
  3841. ;; Emacs only
  3842. (defvar mark-active)
  3843. ;; Various packages
  3844. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3845. (declare-function calendar-forward-day "cal-move" (arg))
  3846. (declare-function calendar-goto-date "cal-move" (date))
  3847. (declare-function calendar-goto-today "cal-move" ())
  3848. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3849. (defvar calc-embedded-close-formula)
  3850. (defvar calc-embedded-open-formula)
  3851. (declare-function cdlatex-tab "ext:cdlatex" ())
  3852. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3853. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3854. (defvar font-lock-unfontify-region-function)
  3855. (declare-function iswitchb-read-buffer "iswitchb"
  3856. (prompt &optional default require-match start matches-set))
  3857. (defvar iswitchb-temp-buflist)
  3858. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3859. (defvar org-agenda-tags-todo-honor-ignore-options)
  3860. (declare-function org-agenda-skip "org-agenda" ())
  3861. (declare-function
  3862. org-agenda-format-item "org-agenda"
  3863. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3864. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3865. (declare-function org-agenda-change-all-lines "org-agenda"
  3866. (newhead hdmarker &optional fixface just-this))
  3867. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3868. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3869. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3870. (beg end))
  3871. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3872. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3873. "org-agenda" (&optional end))
  3874. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3875. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3876. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3877. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3878. (declare-function org-indent-mode "org-indent" (&optional arg))
  3879. (declare-function parse-time-string "parse-time" (string))
  3880. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3881. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3882. (defvar remember-data-file)
  3883. (defvar texmathp-why)
  3884. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3885. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3886. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3887. ;;;###autoload
  3888. (defun turn-on-orgtbl ()
  3889. "Unconditionally turn on `orgtbl-mode'."
  3890. (require 'org-table)
  3891. (orgtbl-mode 1))
  3892. (defun org-at-table-p (&optional table-type)
  3893. "Non-nil if the cursor is inside an Org table.
  3894. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3895. If `org-enable-table-editor' is nil, return nil unconditionally."
  3896. (and
  3897. org-enable-table-editor
  3898. (save-excursion
  3899. (beginning-of-line)
  3900. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3901. (or (not (derived-mode-p 'org-mode))
  3902. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3903. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3904. (defsubst org-table-p () (org-at-table-p))
  3905. (defun org-at-table.el-p ()
  3906. "Non-nil when point is at a table.el table."
  3907. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3908. (let ((element (org-element-at-point)))
  3909. (and (eq (org-element-type element) 'table)
  3910. (eq (org-element-property :type element) 'table.el)))))
  3911. (defun org-table-recognize-table.el ()
  3912. "If there is a table.el table nearby, recognize it and move into it."
  3913. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3914. (beginning-of-line)
  3915. (unless (or (looking-at org-table-dataline-regexp)
  3916. (not (looking-at org-table1-hline-regexp)))
  3917. (forward-line)
  3918. (when (looking-at org-table-any-border-regexp)
  3919. (forward-line -2)))
  3920. (if (re-search-forward "|" (org-table-end t) t)
  3921. (progn
  3922. (require 'table)
  3923. (if (table--at-cell-p (point)) t
  3924. (message "recognizing table.el table...")
  3925. (table-recognize-table)
  3926. (message "recognizing table.el table...done")))
  3927. (error "This should not happen"))))
  3928. (defun org-at-table-hline-p ()
  3929. "Non-nil when point is inside a hline in a table.
  3930. Assume point is already in a table. If `org-enable-table-editor'
  3931. is nil, return nil unconditionally."
  3932. (and org-enable-table-editor
  3933. (save-excursion
  3934. (beginning-of-line)
  3935. (looking-at org-table-hline-regexp))))
  3936. (defun org-table-map-tables (function &optional quietly)
  3937. "Apply FUNCTION to the start of all tables in the buffer."
  3938. (save-excursion
  3939. (save-restriction
  3940. (widen)
  3941. (goto-char (point-min))
  3942. (while (re-search-forward org-table-any-line-regexp nil t)
  3943. (unless quietly
  3944. (message "Mapping tables: %d%%"
  3945. (floor (* 100.0 (point)) (buffer-size))))
  3946. (beginning-of-line 1)
  3947. (when (and (looking-at org-table-line-regexp)
  3948. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3949. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3950. (save-excursion (funcall function))
  3951. (or (looking-at org-table-line-regexp)
  3952. (forward-char 1)))
  3953. (re-search-forward org-table-any-border-regexp nil 1))))
  3954. (unless quietly (message "Mapping tables: done")))
  3955. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3956. (declare-function org-clock-update-mode-line "org-clock" ())
  3957. (declare-function org-resolve-clocks "org-clock"
  3958. (&optional also-non-dangling-p prompt last-valid))
  3959. (defun org-at-TBLFM-p (&optional pos)
  3960. "Non-nil when point (or POS) is in #+TBLFM line."
  3961. (save-excursion
  3962. (goto-char (or pos (point)))
  3963. (beginning-of-line)
  3964. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3965. (eq (org-element-type (org-element-at-point)) 'table))))
  3966. (defvar org-clock-start-time)
  3967. (defvar org-clock-marker (make-marker)
  3968. "Marker recording the last clock-in.")
  3969. (defvar org-clock-hd-marker (make-marker)
  3970. "Marker recording the last clock-in, but the headline position.")
  3971. (defvar org-clock-heading ""
  3972. "The heading of the current clock entry.")
  3973. (defun org-clock-is-active ()
  3974. "Return the buffer where the clock is currently running.
  3975. Return nil if no clock is running."
  3976. (marker-buffer org-clock-marker))
  3977. (defun org-check-running-clock ()
  3978. "Check if the current buffer contains the running clock.
  3979. If yes, offer to stop it and to save the buffer with the changes."
  3980. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3981. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3982. (buffer-name))))
  3983. (org-clock-out)
  3984. (when (y-or-n-p "Save changed buffer?")
  3985. (save-buffer))))
  3986. (defun org-clocktable-try-shift (dir n)
  3987. "Check if this line starts a clock table, if yes, shift the time block."
  3988. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3989. (org-clocktable-shift dir n)))
  3990. ;;;###autoload
  3991. (defun org-clock-persistence-insinuate ()
  3992. "Set up hooks for clock persistence."
  3993. (require 'org-clock)
  3994. (add-hook 'org-mode-hook 'org-clock-load)
  3995. (add-hook 'kill-emacs-hook 'org-clock-save))
  3996. (defgroup org-archive nil
  3997. "Options concerning archiving in Org-mode."
  3998. :tag "Org Archive"
  3999. :group 'org-structure)
  4000. (defcustom org-archive-location "%s_archive::"
  4001. "The location where subtrees should be archived.
  4002. The value of this variable is a string, consisting of two parts,
  4003. separated by a double-colon. The first part is a filename and
  4004. the second part is a headline.
  4005. When the filename is omitted, archiving happens in the same file.
  4006. %s in the filename will be replaced by the current file
  4007. name (without the directory part). Archiving to a different file
  4008. is useful to keep archived entries from contributing to the
  4009. Org-mode Agenda.
  4010. The archived entries will be filed as subtrees of the specified
  4011. headline. When the headline is omitted, the subtrees are simply
  4012. filed away at the end of the file, as top-level entries. Also in
  4013. the heading you can use %s to represent the file name, this can be
  4014. useful when using the same archive for a number of different files.
  4015. Here are a few examples:
  4016. \"%s_archive::\"
  4017. If the current file is Projects.org, archive in file
  4018. Projects.org_archive, as top-level trees. This is the default.
  4019. \"::* Archived Tasks\"
  4020. Archive in the current file, under the top-level headline
  4021. \"* Archived Tasks\".
  4022. \"~/org/archive.org::\"
  4023. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4024. \"~/org/archive.org::* From %s\"
  4025. Archive in file ~/org/archive.org (absolute path), under headlines
  4026. \"From FILENAME\" where file name is the current file name.
  4027. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4028. The \"datetree/\" string is special, signifying to archive
  4029. items to the datetree. Items are placed in either the CLOSED
  4030. date of the item, or the current date if there is no CLOSED date.
  4031. The heading will be a subentry to the current date. There doesn't
  4032. need to be a heading, but there always needs to be a slash after
  4033. datetree. For example, to store archived items directly in the
  4034. datetree, use \"~/org/datetree.org::datetree/\".
  4035. \"basement::** Finished Tasks\"
  4036. Archive in file ./basement (relative path), as level 3 trees
  4037. below the level 2 heading \"** Finished Tasks\".
  4038. You may set this option on a per-file basis by adding to the buffer a
  4039. line like
  4040. #+ARCHIVE: basement::** Finished Tasks
  4041. You may also define it locally for a subtree by setting an ARCHIVE property
  4042. in the entry. If such a property is found in an entry, or anywhere up
  4043. the hierarchy, it will be used."
  4044. :group 'org-archive
  4045. :type 'string)
  4046. (defcustom org-agenda-skip-archived-trees t
  4047. "Non-nil means the agenda will skip any items located in archived trees.
  4048. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4049. variable is no longer recommended, you should leave it at the value t.
  4050. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4051. :group 'org-archive
  4052. :group 'org-agenda-skip
  4053. :type 'boolean)
  4054. (defcustom org-columns-skip-archived-trees t
  4055. "Non-nil means ignore archived trees when creating column view."
  4056. :group 'org-archive
  4057. :group 'org-properties
  4058. :type 'boolean)
  4059. (defcustom org-cycle-open-archived-trees nil
  4060. "Non-nil means `org-cycle' will open archived trees.
  4061. An archived tree is a tree marked with the tag ARCHIVE.
  4062. When nil, archived trees will stay folded. You can still open them with
  4063. normal outline commands like `show-all', but not with the cycling commands."
  4064. :group 'org-archive
  4065. :group 'org-cycle
  4066. :type 'boolean)
  4067. (defcustom org-sparse-tree-open-archived-trees nil
  4068. "Non-nil means sparse tree construction shows matches in archived trees.
  4069. When nil, matches in these trees are highlighted, but the trees are kept in
  4070. collapsed state."
  4071. :group 'org-archive
  4072. :group 'org-sparse-trees
  4073. :type 'boolean)
  4074. (defcustom org-sparse-tree-default-date-type nil
  4075. "The default date type when building a sparse tree.
  4076. When this is nil, a date is a scheduled or a deadline timestamp.
  4077. Otherwise, these types are allowed:
  4078. all: all timestamps
  4079. active: only active timestamps (<...>)
  4080. inactive: only inactive timestamps ([...])
  4081. scheduled: only scheduled timestamps
  4082. deadline: only deadline timestamps"
  4083. :type '(choice (const :tag "Scheduled or deadline" nil)
  4084. (const :tag "All timestamps" all)
  4085. (const :tag "Only active timestamps" active)
  4086. (const :tag "Only inactive timestamps" inactive)
  4087. (const :tag "Only scheduled timestamps" scheduled)
  4088. (const :tag "Only deadline timestamps" deadline)
  4089. (const :tag "Only closed timestamps" closed))
  4090. :version "25.1"
  4091. :package-version '(Org . "8.3")
  4092. :group 'org-sparse-trees)
  4093. (defun org-cycle-hide-archived-subtrees (state)
  4094. "Re-hide all archived subtrees after a visibility state change."
  4095. (when (and (not org-cycle-open-archived-trees)
  4096. (not (memq state '(overview folded))))
  4097. (save-excursion
  4098. (let* ((globalp (memq state '(contents all)))
  4099. (beg (if globalp (point-min) (point)))
  4100. (end (if globalp (point-max) (org-end-of-subtree t))))
  4101. (org-hide-archived-subtrees beg end)
  4102. (goto-char beg)
  4103. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4104. (message "%s" (substitute-command-keys
  4105. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4106. (defun org-force-cycle-archived ()
  4107. "Cycle subtree even if it is archived."
  4108. (interactive)
  4109. (setq this-command 'org-cycle)
  4110. (let ((org-cycle-open-archived-trees t))
  4111. (call-interactively 'org-cycle)))
  4112. (defun org-hide-archived-subtrees (beg end)
  4113. "Re-hide all archived subtrees after a visibility state change."
  4114. (org-with-wide-buffer
  4115. (let ((case-fold-search nil)
  4116. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4117. (goto-char beg)
  4118. ;; Include headline point is currently on.
  4119. (beginning-of-line)
  4120. (while (and (< (point) end) (re-search-forward re end t))
  4121. (when (member org-archive-tag (org-get-tags))
  4122. (org-flag-subtree t)
  4123. (org-end-of-subtree t))))))
  4124. (declare-function outline-end-of-heading "outline" ())
  4125. (declare-function outline-flag-region "outline" (from to flag))
  4126. (defun org-flag-subtree (flag)
  4127. (save-excursion
  4128. (org-back-to-heading t)
  4129. (outline-end-of-heading)
  4130. (outline-flag-region (point)
  4131. (progn (org-end-of-subtree t) (point))
  4132. flag)))
  4133. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4134. ;; Declare Column View Code
  4135. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4136. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4137. (declare-function org-columns-compute "org-colview" (property))
  4138. ;; Declare ID code
  4139. (declare-function org-id-store-link "org-id")
  4140. (declare-function org-id-locations-load "org-id")
  4141. (declare-function org-id-locations-save "org-id")
  4142. (defvar org-id-track-globally)
  4143. ;;; Variables for pre-computed regular expressions, all buffer local
  4144. (defvar org-todo-regexp nil
  4145. "Matches any of the TODO state keywords.")
  4146. (make-variable-buffer-local 'org-todo-regexp)
  4147. (defvar org-not-done-regexp nil
  4148. "Matches any of the TODO state keywords except the last one.")
  4149. (make-variable-buffer-local 'org-not-done-regexp)
  4150. (defvar org-not-done-heading-regexp nil
  4151. "Matches a TODO headline that is not done.")
  4152. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4153. (defvar org-todo-line-regexp nil
  4154. "Matches a headline and puts TODO state into group 2 if present.")
  4155. (make-variable-buffer-local 'org-todo-line-regexp)
  4156. (defvar org-complex-heading-regexp nil
  4157. "Matches a headline and puts everything into groups:
  4158. group 1: the stars
  4159. group 2: The todo keyword, maybe
  4160. group 3: Priority cookie
  4161. group 4: True headline
  4162. group 5: Tags")
  4163. (make-variable-buffer-local 'org-complex-heading-regexp)
  4164. (defvar org-complex-heading-regexp-format nil
  4165. "Printf format to make regexp to match an exact headline.
  4166. This regexp will match the headline of any node which has the
  4167. exact headline text that is put into the format, but may have any
  4168. TODO state, priority and tags.")
  4169. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4170. (defvar org-todo-line-tags-regexp nil
  4171. "Matches a headline and puts TODO state into group 2 if present.
  4172. Also put tags into group 4 if tags are present.")
  4173. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4174. (defconst org-plain-time-of-day-regexp
  4175. (concat
  4176. "\\(\\<[012]?[0-9]"
  4177. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4178. "\\(--?"
  4179. "\\(\\<[012]?[0-9]"
  4180. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4181. "\\)?")
  4182. "Regular expression to match a plain time or time range.
  4183. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4184. groups carry important information:
  4185. 0 the full match
  4186. 1 the first time, range or not
  4187. 8 the second time, if it is a range.")
  4188. (defconst org-plain-time-extension-regexp
  4189. (concat
  4190. "\\(\\<[012]?[0-9]"
  4191. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4192. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4193. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4194. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4195. groups carry important information:
  4196. 0 the full match
  4197. 7 hours of duration
  4198. 9 minutes of duration")
  4199. (defconst org-stamp-time-of-day-regexp
  4200. (concat
  4201. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4202. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4203. "\\(--?"
  4204. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4205. "Regular expression to match a timestamp time or time range.
  4206. After a match, the following groups carry important information:
  4207. 0 the full match
  4208. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4209. 2 the first time, range or not
  4210. 4 the second time, if it is a range.")
  4211. (defconst org-startup-options
  4212. '(("fold" org-startup-folded t)
  4213. ("overview" org-startup-folded t)
  4214. ("nofold" org-startup-folded nil)
  4215. ("showall" org-startup-folded nil)
  4216. ("showeverything" org-startup-folded showeverything)
  4217. ("content" org-startup-folded content)
  4218. ("indent" org-startup-indented t)
  4219. ("noindent" org-startup-indented nil)
  4220. ("hidestars" org-hide-leading-stars t)
  4221. ("showstars" org-hide-leading-stars nil)
  4222. ("odd" org-odd-levels-only t)
  4223. ("oddeven" org-odd-levels-only nil)
  4224. ("align" org-startup-align-all-tables t)
  4225. ("noalign" org-startup-align-all-tables nil)
  4226. ("inlineimages" org-startup-with-inline-images t)
  4227. ("noinlineimages" org-startup-with-inline-images nil)
  4228. ("latexpreview" org-startup-with-latex-preview t)
  4229. ("nolatexpreview" org-startup-with-latex-preview nil)
  4230. ("customtime" org-display-custom-times t)
  4231. ("logdone" org-log-done time)
  4232. ("lognotedone" org-log-done note)
  4233. ("nologdone" org-log-done nil)
  4234. ("lognoteclock-out" org-log-note-clock-out t)
  4235. ("nolognoteclock-out" org-log-note-clock-out nil)
  4236. ("logrepeat" org-log-repeat state)
  4237. ("lognoterepeat" org-log-repeat note)
  4238. ("logdrawer" org-log-into-drawer t)
  4239. ("nologdrawer" org-log-into-drawer nil)
  4240. ("logstatesreversed" org-log-states-order-reversed t)
  4241. ("nologstatesreversed" org-log-states-order-reversed nil)
  4242. ("nologrepeat" org-log-repeat nil)
  4243. ("logreschedule" org-log-reschedule time)
  4244. ("lognotereschedule" org-log-reschedule note)
  4245. ("nologreschedule" org-log-reschedule nil)
  4246. ("logredeadline" org-log-redeadline time)
  4247. ("lognoteredeadline" org-log-redeadline note)
  4248. ("nologredeadline" org-log-redeadline nil)
  4249. ("logrefile" org-log-refile time)
  4250. ("lognoterefile" org-log-refile note)
  4251. ("nologrefile" org-log-refile nil)
  4252. ("fninline" org-footnote-define-inline t)
  4253. ("nofninline" org-footnote-define-inline nil)
  4254. ("fnlocal" org-footnote-section nil)
  4255. ("fnauto" org-footnote-auto-label t)
  4256. ("fnprompt" org-footnote-auto-label nil)
  4257. ("fnconfirm" org-footnote-auto-label confirm)
  4258. ("fnplain" org-footnote-auto-label plain)
  4259. ("fnadjust" org-footnote-auto-adjust t)
  4260. ("nofnadjust" org-footnote-auto-adjust nil)
  4261. ("constcgs" constants-unit-system cgs)
  4262. ("constSI" constants-unit-system SI)
  4263. ("noptag" org-tag-persistent-alist nil)
  4264. ("hideblocks" org-hide-block-startup t)
  4265. ("nohideblocks" org-hide-block-startup nil)
  4266. ("beamer" org-startup-with-beamer-mode t)
  4267. ("entitiespretty" org-pretty-entities t)
  4268. ("entitiesplain" org-pretty-entities nil))
  4269. "Variable associated with STARTUP options for org-mode.
  4270. Each element is a list of three items: the startup options (as written
  4271. in the #+STARTUP line), the corresponding variable, and the value to set
  4272. this variable to if the option is found. An optional forth element PUSH
  4273. means to push this value onto the list in the variable.")
  4274. (defcustom org-group-tags t
  4275. "When non-nil (the default), use group tags.
  4276. This can be turned on/off through `org-toggle-tags-groups'."
  4277. :group 'org-tags
  4278. :group 'org-startup
  4279. :type 'boolean)
  4280. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4281. (defun org-toggle-tags-groups ()
  4282. "Toggle support for group tags.
  4283. Support for group tags is controlled by the option
  4284. `org-group-tags', which is non-nil by default."
  4285. (interactive)
  4286. (setq org-group-tags (not org-group-tags))
  4287. (cond ((and (derived-mode-p 'org-agenda-mode)
  4288. org-group-tags)
  4289. (org-agenda-redo))
  4290. ((derived-mode-p 'org-mode)
  4291. (let ((org-inhibit-startup t)) (org-mode))))
  4292. (message "Groups tags support has been turned %s"
  4293. (if org-group-tags "on" "off")))
  4294. (defun org-set-regexps-and-options (&optional tags-only)
  4295. "Precompute regular expressions used in the current buffer.
  4296. When optional argument TAGS-ONLY is non-nil, only compute tags
  4297. related expressions."
  4298. (when (derived-mode-p 'org-mode)
  4299. (let ((alist (org--setup-collect-keywords
  4300. (org-make-options-regexp
  4301. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4302. (and (not tags-only)
  4303. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4304. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4305. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4306. (org--setup-process-tags
  4307. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4308. (unless tags-only
  4309. ;; File properties.
  4310. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4311. ;; Archive location.
  4312. (let ((archive (cdr (assq 'archive alist))))
  4313. (when archive (org-set-local 'org-archive-location archive)))
  4314. ;; Category.
  4315. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4316. (when cat
  4317. (org-set-local 'org-category (intern cat))
  4318. (org-set-local 'org-file-properties
  4319. (org--update-property-plist
  4320. "CATEGORY" cat org-file-properties))))
  4321. ;; Columns.
  4322. (let ((column (cdr (assq 'columns alist))))
  4323. (when column (org-set-local 'org-columns-default-format column)))
  4324. ;; Constants.
  4325. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4326. ;; Link abbreviations.
  4327. (let ((links (cdr (assq 'link alist))))
  4328. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4329. ;; Priorities.
  4330. (let ((priorities (cdr (assq 'priorities alist))))
  4331. (when priorities
  4332. (org-set-local 'org-highest-priority (nth 0 priorities))
  4333. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4334. (org-set-local 'org-default-priority (nth 2 priorities))))
  4335. ;; Scripts.
  4336. (let ((scripts (assq 'scripts alist)))
  4337. (when scripts
  4338. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4339. ;; Startup options.
  4340. (let ((startup (cdr (assq 'startup alist))))
  4341. (dolist (option startup)
  4342. (let ((entry (assoc-string option org-startup-options t)))
  4343. (when entry
  4344. (let ((var (nth 1 entry))
  4345. (val (nth 2 entry)))
  4346. (if (not (nth 3 entry)) (org-set-local var val)
  4347. (unless (listp (symbol-value var))
  4348. (org-set-local var nil))
  4349. (add-to-list var val)))))))
  4350. ;; TODO keywords.
  4351. (org-set-local 'org-todo-kwd-alist nil)
  4352. (org-set-local 'org-todo-key-alist nil)
  4353. (org-set-local 'org-todo-key-trigger nil)
  4354. (org-set-local 'org-todo-keywords-1 nil)
  4355. (org-set-local 'org-done-keywords nil)
  4356. (org-set-local 'org-todo-heads nil)
  4357. (org-set-local 'org-todo-sets nil)
  4358. (org-set-local 'org-todo-log-states nil)
  4359. (let ((todo-sequences
  4360. (or (nreverse (cdr (assq 'todo alist)))
  4361. (let ((d (default-value 'org-todo-keywords)))
  4362. (if (not (stringp (car d))) d
  4363. ;; XXX: Backward compatibility code.
  4364. (list (cons org-todo-interpretation d)))))))
  4365. (dolist (sequence todo-sequences)
  4366. (let* ((sequence (or (run-hook-with-args-until-success
  4367. 'org-todo-setup-filter-hook sequence)
  4368. sequence))
  4369. (sequence-type (car sequence))
  4370. (keywords (cdr sequence))
  4371. (sep (member "|" keywords))
  4372. names alist)
  4373. (dolist (k (remove "|" keywords))
  4374. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4375. k)
  4376. (error "Invalid TODO keyword %s" k))
  4377. (let ((name (match-string 1 k))
  4378. (key (match-string 2 k))
  4379. (log (org-extract-log-state-settings k)))
  4380. (push name names)
  4381. (push (cons name (and key (string-to-char key))) alist)
  4382. (when log (push log org-todo-log-states))))
  4383. (let* ((names (nreverse names))
  4384. (done (if sep (org-remove-keyword-keys (cdr sep))
  4385. (last names)))
  4386. (head (car names))
  4387. (tail (list sequence-type head (car done) (org-last done))))
  4388. (add-to-list 'org-todo-heads head 'append)
  4389. (push names org-todo-sets)
  4390. (setq org-done-keywords (append org-done-keywords done nil))
  4391. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4392. (setq org-todo-key-alist
  4393. (append org-todo-key-alist
  4394. (and alist
  4395. (append '((:startgroup))
  4396. (nreverse alist)
  4397. '((:endgroup))))))
  4398. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4399. (setq org-todo-sets (nreverse org-todo-sets)
  4400. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4401. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4402. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4403. ;; Compute the regular expressions and other local variables.
  4404. ;; Using `org-outline-regexp-bol' would complicate them much,
  4405. ;; because of the fixed white space at the end of that string.
  4406. (if (not org-done-keywords)
  4407. (setq org-done-keywords
  4408. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4409. (setq org-not-done-keywords
  4410. (org-delete-all org-done-keywords
  4411. (copy-sequence org-todo-keywords-1))
  4412. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4413. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4414. org-not-done-heading-regexp
  4415. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4416. org-todo-line-regexp
  4417. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4418. org-complex-heading-regexp
  4419. (concat "^\\(\\*+\\)"
  4420. "\\(?: +" org-todo-regexp "\\)?"
  4421. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4422. "\\(?: +\\(.*?\\)\\)??"
  4423. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4424. "[ \t]*$")
  4425. org-complex-heading-regexp-format
  4426. (concat "^\\(\\*+\\)"
  4427. "\\(?: +" org-todo-regexp "\\)?"
  4428. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4429. "\\(?: +"
  4430. ;; Stats cookies can be stuck to body.
  4431. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4432. "\\(%s\\)"
  4433. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4434. "\\)"
  4435. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4436. "[ \t]*$")
  4437. org-todo-line-tags-regexp
  4438. (concat "^\\(\\*+\\)"
  4439. "\\(?: +" org-todo-regexp "\\)?"
  4440. "\\(?: +\\(.*?\\)\\)??"
  4441. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4442. "[ \t]*$"))
  4443. (org-compute-latex-and-related-regexp)))))
  4444. (defun org--setup-collect-keywords (regexp &optional files alist)
  4445. "Return setup keywords values as an alist.
  4446. REGEXP matches a subset of setup keywords. FILES is a list of
  4447. file names already visited. It is used to avoid circular setup
  4448. files. ALIST, when non-nil, is the alist computed so far.
  4449. Return value contains the following keys: `archive', `category',
  4450. `columns', `constants', `filetags', `link', `priorities',
  4451. `property', `scripts', `startup', `tags' and `todo'."
  4452. (org-with-wide-buffer
  4453. (goto-char (point-min))
  4454. (let ((case-fold-search t))
  4455. (while (re-search-forward regexp nil t)
  4456. (let ((element (org-element-at-point)))
  4457. (when (eq (org-element-type element) 'keyword)
  4458. (let ((key (org-element-property :key element))
  4459. (value (org-element-property :value element)))
  4460. (cond
  4461. ((equal key "ARCHIVE")
  4462. (when (org-string-nw-p value)
  4463. (push (cons 'archive value) alist)))
  4464. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4465. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4466. ((equal key "CONSTANTS")
  4467. (let* ((constants (assq 'constants alist))
  4468. (store (cdr constants)))
  4469. (dolist (pair (org-split-string value))
  4470. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4471. pair)
  4472. (let* ((name (match-string 1 pair))
  4473. (value (match-string 2 pair))
  4474. (old (assoc name store)))
  4475. (if old (setcdr old value)
  4476. (push (cons name value) store)))))
  4477. (if constants (setcdr constants store)
  4478. (push (cons 'constants store) alist))))
  4479. ((equal key "FILETAGS")
  4480. (when (org-string-nw-p value)
  4481. (let ((old (assq 'filetags alist))
  4482. (new (apply #'nconc
  4483. (mapcar (lambda (x) (org-split-string x ":"))
  4484. (org-split-string value)))))
  4485. (if old (setcdr old (append new (cdr old)))
  4486. (push (cons 'filetags new) alist)))))
  4487. ((equal key "LINK")
  4488. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4489. (let ((links (assq 'link alist))
  4490. (pair (cons (org-match-string-no-properties 1 value)
  4491. (org-match-string-no-properties 2 value))))
  4492. (if links (push pair (cdr links))
  4493. (push (list 'link pair) alist)))))
  4494. ((equal key "OPTIONS")
  4495. (when (and (org-string-nw-p value)
  4496. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4497. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4498. ((equal key "PRIORITIES")
  4499. (push (cons 'priorities
  4500. (let ((prio (org-split-string value)))
  4501. (if (< (length prio) 3) '(?A ?C ?B)
  4502. (mapcar #'string-to-char prio))))
  4503. alist))
  4504. ((equal key "PROPERTY")
  4505. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4506. (let* ((property (assq 'property alist))
  4507. (value (org--update-property-plist
  4508. (org-match-string-no-properties 1 value)
  4509. (org-match-string-no-properties 2 value)
  4510. (cdr property))))
  4511. (if property (setcdr property value)
  4512. (push (cons 'property value) alist)))))
  4513. ((equal key "STARTUP")
  4514. (let ((startup (assq 'startup alist)))
  4515. (if startup
  4516. (setcdr startup
  4517. (append (cdr startup) (org-split-string value)))
  4518. (push (cons 'startup (org-split-string value)) alist))))
  4519. ((equal key "TAGS")
  4520. (let ((tag-cell (assq 'tags alist)))
  4521. (if tag-cell
  4522. (setcdr tag-cell
  4523. (append (cdr tag-cell)
  4524. '("\\n")
  4525. (org-split-string value)))
  4526. (push (cons 'tags (org-split-string value)) alist))))
  4527. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4528. (let ((todo (assq 'todo alist))
  4529. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4530. (org-split-string value))))
  4531. (if todo (push value (cdr todo))
  4532. (push (list 'todo value) alist))))
  4533. ((equal key "SETUPFILE")
  4534. (unless buffer-read-only ; Do not check in Gnus messages.
  4535. (let ((f (and (org-string-nw-p value)
  4536. (expand-file-name
  4537. (org-remove-double-quotes value)))))
  4538. (when (and f (file-readable-p f) (not (member f files)))
  4539. (with-temp-buffer
  4540. (insert-file-contents f)
  4541. (setq alist
  4542. ;; Fake Org mode to benefit from cache
  4543. ;; without recurring needlessly.
  4544. (let ((major-mode 'org-mode))
  4545. (org--setup-collect-keywords
  4546. regexp (cons f files) alist)))))))))))))))
  4547. alist)
  4548. (defun org--setup-process-tags (tags filetags)
  4549. "Precompute variables used for tags.
  4550. TAGS is a list of tags and tag group symbols, as strings.
  4551. FILETAGS is a list of tags, as strings."
  4552. ;; Process the file tags.
  4553. (org-set-local 'org-file-tags
  4554. (mapcar #'org-add-prop-inherited filetags))
  4555. ;; Provide default tags if no local tags are found.
  4556. (when (and (not tags) org-tag-alist)
  4557. (setq tags
  4558. (mapcar (lambda (tag)
  4559. (case (car tag)
  4560. (:startgroup "{")
  4561. (:endgroup "}")
  4562. (:startgrouptag "[")
  4563. (:endgrouptag "]")
  4564. (:grouptags ":")
  4565. (:newline "\\n")
  4566. (otherwise (concat (car tag)
  4567. (and (characterp (cdr tag))
  4568. (format "(%c)" (cdr tag)))))))
  4569. org-tag-alist)))
  4570. ;; Process the tags.
  4571. (org-set-local 'org-tag-groups-alist nil)
  4572. (org-set-local 'org-tag-alist nil)
  4573. (let (group-flag)
  4574. (while tags
  4575. (let ((e (car tags)))
  4576. (setq tags (cdr tags))
  4577. (cond
  4578. ((equal e "{")
  4579. (push '(:startgroup) org-tag-alist)
  4580. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4581. ((equal e "}")
  4582. (push '(:endgroup) org-tag-alist)
  4583. (setq group-flag nil))
  4584. ((equal e "[")
  4585. (push '(:startgrouptag) org-tag-alist)
  4586. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4587. ((equal e "]")
  4588. (push '(:endgrouptag) org-tag-alist)
  4589. (setq group-flag nil))
  4590. ((equal e ":")
  4591. (push '(:grouptags) org-tag-alist)
  4592. (setq group-flag 'append))
  4593. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4594. ((string-match
  4595. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4596. "\\|{.+?}\\)" ; regular expression
  4597. "\\(?:(\\(.\\))\\)?\\'")) e)
  4598. (let ((tag (match-string 1 e))
  4599. (key (and (match-beginning 2)
  4600. (string-to-char (match-string 2 e)))))
  4601. (cond ((eq group-flag 'append)
  4602. (setcar org-tag-groups-alist
  4603. (append (car org-tag-groups-alist) (list tag))))
  4604. (group-flag (push (list tag) org-tag-groups-alist)))
  4605. ;; Push all tags in groups, no matter if they already exist.
  4606. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4607. (push (cons tag key) org-tag-alist))))))))
  4608. (setq org-tag-alist (nreverse org-tag-alist)))
  4609. (defun org-file-contents (file &optional noerror)
  4610. "Return the contents of FILE, as a string."
  4611. (if (and file (file-readable-p file))
  4612. (with-temp-buffer
  4613. (insert-file-contents file)
  4614. (buffer-string))
  4615. (funcall (if noerror 'message 'error)
  4616. "Cannot read file \"%s\"%s"
  4617. file
  4618. (let ((from (buffer-file-name (buffer-base-buffer))))
  4619. (if from (concat " (referenced in file \"" from "\")") "")))))
  4620. (defun org-extract-log-state-settings (x)
  4621. "Extract the log state setting from a TODO keyword string.
  4622. This will extract info from a string like \"WAIT(w@/!)\"."
  4623. (let (kw key log1 log2)
  4624. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4625. (setq kw (match-string 1 x)
  4626. key (and (match-end 2) (match-string 2 x))
  4627. log1 (and (match-end 3) (match-string 3 x))
  4628. log2 (and (match-end 4) (match-string 4 x)))
  4629. (and (or log1 log2)
  4630. (list kw
  4631. (and log1 (if (equal log1 "!") 'time 'note))
  4632. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4633. (defun org-remove-keyword-keys (list)
  4634. "Remove a pair of parenthesis at the end of each string in LIST."
  4635. (mapcar (lambda (x)
  4636. (if (string-match "(.*)$" x)
  4637. (substring x 0 (match-beginning 0))
  4638. x))
  4639. list))
  4640. (defun org-assign-fast-keys (alist)
  4641. "Assign fast keys to a keyword-key alist.
  4642. Respect keys that are already there."
  4643. (let (new e (alt ?0))
  4644. (while (setq e (pop alist))
  4645. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4646. (cdr e)) ;; Key already assigned.
  4647. (push e new)
  4648. (let ((clist (string-to-list (downcase (car e))))
  4649. (used (append new alist)))
  4650. (when (= (car clist) ?@)
  4651. (pop clist))
  4652. (while (and clist (rassoc (car clist) used))
  4653. (pop clist))
  4654. (unless clist
  4655. (while (rassoc alt used)
  4656. (incf alt)))
  4657. (push (cons (car e) (or (car clist) alt)) new))))
  4658. (nreverse new)))
  4659. ;;; Some variables used in various places
  4660. (defvar org-window-configuration nil
  4661. "Used in various places to store a window configuration.")
  4662. (defvar org-selected-window nil
  4663. "Used in various places to store a window configuration.")
  4664. (defvar org-finish-function nil
  4665. "Function to be called when `C-c C-c' is used.
  4666. This is for getting out of special buffers like capture.")
  4667. ;; FIXME: Occasionally check by commenting these, to make sure
  4668. ;; no other functions uses these, forgetting to let-bind them.
  4669. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4670. (defvar org-last-state)
  4671. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4672. ;; Defined somewhere in this file, but used before definition.
  4673. (defvar org-entities) ;; defined in org-entities.el
  4674. (defvar org-struct-menu)
  4675. (defvar org-org-menu)
  4676. (defvar org-tbl-menu)
  4677. ;;;; Define the Org-mode
  4678. ;; We use a before-change function to check if a table might need
  4679. ;; an update.
  4680. (defvar org-table-may-need-update t
  4681. "Indicates that a table might need an update.
  4682. This variable is set by `org-before-change-function'.
  4683. `org-table-align' sets it back to nil.")
  4684. (defun org-before-change-function (beg end)
  4685. "Every change indicates that a table might need an update."
  4686. (setq org-table-may-need-update t))
  4687. (defvar org-mode-map)
  4688. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4689. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4690. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4691. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4692. (defvar org-table-buffer-is-an nil)
  4693. (defvar bidi-paragraph-direction)
  4694. (defvar buffer-face-mode-face)
  4695. (require 'outline)
  4696. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4697. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
  4698. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4699. ;; Other stuff we need.
  4700. (require 'time-date)
  4701. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4702. (require 'easymenu)
  4703. (autoload 'easy-menu-add "easymenu")
  4704. (require 'overlay)
  4705. ;; (require 'org-macs) moved higher up in the file before it is first used
  4706. (require 'org-entities)
  4707. ;; (require 'org-compat) moved higher up in the file before it is first used
  4708. (require 'org-faces)
  4709. (require 'org-list)
  4710. (require 'org-pcomplete)
  4711. (require 'org-src)
  4712. (require 'org-footnote)
  4713. (require 'org-macro)
  4714. ;; babel
  4715. (require 'ob)
  4716. ;;;###autoload
  4717. (define-derived-mode org-mode outline-mode "Org"
  4718. "Outline-based notes management and organizer, alias
  4719. \"Carsten's outline-mode for keeping track of everything.\"
  4720. Org-mode develops organizational tasks around a NOTES file which
  4721. contains information about projects as plain text. Org-mode is
  4722. implemented on top of outline-mode, which is ideal to keep the content
  4723. of large files well structured. It supports ToDo items, deadlines and
  4724. time stamps, which magically appear in the diary listing of the Emacs
  4725. calendar. Tables are easily created with a built-in table editor.
  4726. Plain text URL-like links connect to websites, emails (VM), Usenet
  4727. messages (Gnus), BBDB entries, and any files related to the project.
  4728. For printing and sharing of notes, an Org-mode file (or a part of it)
  4729. can be exported as a structured ASCII or HTML file.
  4730. The following commands are available:
  4731. \\{org-mode-map}"
  4732. ;; Get rid of Outline menus, they are not needed
  4733. ;; Need to do this here because define-derived-mode sets up
  4734. ;; the keymap so late. Still, it is a waste to call this each time
  4735. ;; we switch another buffer into org-mode.
  4736. (if (featurep 'xemacs)
  4737. (when (boundp 'outline-mode-menu-heading)
  4738. ;; Assume this is Greg's port, it uses easymenu
  4739. (easy-menu-remove outline-mode-menu-heading)
  4740. (easy-menu-remove outline-mode-menu-show)
  4741. (easy-menu-remove outline-mode-menu-hide))
  4742. (define-key org-mode-map [menu-bar headings] 'undefined)
  4743. (define-key org-mode-map [menu-bar hide] 'undefined)
  4744. (define-key org-mode-map [menu-bar show] 'undefined))
  4745. (org-load-modules-maybe)
  4746. (when (featurep 'xemacs)
  4747. (easy-menu-add org-org-menu)
  4748. (easy-menu-add org-tbl-menu))
  4749. (org-install-agenda-files-menu)
  4750. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4751. (add-to-invisibility-spec '(org-cwidth))
  4752. (add-to-invisibility-spec '(org-hide-block . t))
  4753. (when (featurep 'xemacs)
  4754. (org-set-local 'line-move-ignore-invisible t))
  4755. (org-set-local 'outline-regexp org-outline-regexp)
  4756. (org-set-local 'outline-level 'org-outline-level)
  4757. (setq bidi-paragraph-direction 'left-to-right)
  4758. (when (and org-ellipsis
  4759. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4760. (fboundp 'make-glyph-code))
  4761. (unless org-display-table
  4762. (setq org-display-table (make-display-table)))
  4763. (set-display-table-slot
  4764. org-display-table 4
  4765. (vconcat (mapcar
  4766. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4767. org-ellipsis)))
  4768. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4769. (setq buffer-display-table org-display-table))
  4770. (org-set-regexps-and-options)
  4771. (org-set-font-lock-defaults)
  4772. (when (and org-tag-faces (not org-tags-special-faces-re))
  4773. ;; tag faces set outside customize.... force initialization.
  4774. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4775. ;; Calc embedded
  4776. (org-set-local 'calc-embedded-open-mode "# ")
  4777. ;; Modify a few syntax entries
  4778. (modify-syntax-entry ?@ "w")
  4779. (modify-syntax-entry ?\" "\"")
  4780. (modify-syntax-entry ?\\ "_")
  4781. (modify-syntax-entry ?~ "_")
  4782. (if org-startup-truncated (setq truncate-lines t))
  4783. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4784. (org-set-local 'font-lock-unfontify-region-function
  4785. 'org-unfontify-region)
  4786. ;; Activate before-change-function
  4787. (org-set-local 'org-table-may-need-update t)
  4788. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4789. 'local)
  4790. ;; Check for running clock before killing a buffer
  4791. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4792. ;; Initialize macros templates.
  4793. (org-macro-initialize-templates)
  4794. ;; Initialize radio targets.
  4795. (org-update-radio-target-regexp)
  4796. ;; Indentation.
  4797. (org-set-local 'indent-line-function 'org-indent-line)
  4798. (org-set-local 'indent-region-function 'org-indent-region)
  4799. ;; Filling and auto-filling.
  4800. (org-setup-filling)
  4801. ;; Comments.
  4802. (org-setup-comments-handling)
  4803. ;; Initialize cache.
  4804. (org-element-cache-reset)
  4805. ;; Beginning/end of defun
  4806. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4807. (org-set-local 'end-of-defun-function
  4808. (lambda ()
  4809. (if (not (org-at-heading-p))
  4810. (org-forward-element)
  4811. (org-forward-element)
  4812. (forward-char -1))))
  4813. ;; Next error for sparse trees
  4814. (org-set-local 'next-error-function 'org-occur-next-match)
  4815. ;; Make sure dependence stuff works reliably, even for users who set it
  4816. ;; too late :-(
  4817. (if org-enforce-todo-dependencies
  4818. (add-hook 'org-blocker-hook
  4819. 'org-block-todo-from-children-or-siblings-or-parent)
  4820. (remove-hook 'org-blocker-hook
  4821. 'org-block-todo-from-children-or-siblings-or-parent))
  4822. (if org-enforce-todo-checkbox-dependencies
  4823. (add-hook 'org-blocker-hook
  4824. 'org-block-todo-from-checkboxes)
  4825. (remove-hook 'org-blocker-hook
  4826. 'org-block-todo-from-checkboxes))
  4827. ;; Align options lines
  4828. (org-set-local
  4829. 'align-mode-rules-list
  4830. '((org-in-buffer-settings
  4831. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4832. (modes . '(org-mode)))))
  4833. ;; Imenu
  4834. (org-set-local 'imenu-create-index-function
  4835. 'org-imenu-get-tree)
  4836. ;; Make isearch reveal context
  4837. (if (or (featurep 'xemacs)
  4838. (not (boundp 'outline-isearch-open-invisible-function)))
  4839. ;; Emacs 21 and XEmacs make use of the hook
  4840. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4841. ;; Emacs 22 deals with this through a special variable
  4842. (org-set-local 'outline-isearch-open-invisible-function
  4843. (lambda (&rest ignore) (org-show-context 'isearch))))
  4844. ;; Setup the pcomplete hooks
  4845. (set (make-local-variable 'pcomplete-command-completion-function)
  4846. 'org-pcomplete-initial)
  4847. (set (make-local-variable 'pcomplete-command-name-function)
  4848. 'org-command-at-point)
  4849. (set (make-local-variable 'pcomplete-default-completion-function)
  4850. 'ignore)
  4851. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4852. 'org-parse-arguments)
  4853. (set (make-local-variable 'pcomplete-termination-string) "")
  4854. (when (>= emacs-major-version 23)
  4855. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4856. ;; If empty file that did not turn on org-mode automatically, make it to.
  4857. (if (and org-insert-mode-line-in-empty-file
  4858. (org-called-interactively-p 'any)
  4859. (= (point-min) (point-max)))
  4860. (insert "# -*- mode: org -*-\n\n"))
  4861. (unless org-inhibit-startup
  4862. (org-unmodified
  4863. (and org-startup-with-beamer-mode (org-beamer-mode))
  4864. (when org-startup-align-all-tables
  4865. (org-table-map-tables 'org-table-align 'quietly))
  4866. (when org-startup-with-inline-images
  4867. (org-display-inline-images))
  4868. (when org-startup-with-latex-preview
  4869. (org-toggle-latex-fragment))
  4870. (unless org-inhibit-startup-visibility-stuff
  4871. (org-set-startup-visibility))
  4872. (org-refresh-effort-properties)))
  4873. ;; Try to set org-hide correctly
  4874. (let ((foreground (org-find-invisible-foreground)))
  4875. (if foreground
  4876. (set-face-foreground 'org-hide foreground))))
  4877. ;; Update `customize-package-emacs-version-alist'
  4878. (add-to-list 'customize-package-emacs-version-alist
  4879. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4880. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4881. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4882. (defvar org-mode-transpose-word-syntax-table
  4883. (let ((st (make-syntax-table text-mode-syntax-table)))
  4884. (mapc (lambda(c) (modify-syntax-entry
  4885. (string-to-char (car c)) "w p" st))
  4886. org-emphasis-alist)
  4887. st))
  4888. (when (fboundp 'abbrev-table-put)
  4889. (abbrev-table-put org-mode-abbrev-table
  4890. :parents (list text-mode-abbrev-table)))
  4891. (defun org-find-invisible-foreground ()
  4892. (let ((candidates (remove
  4893. "unspecified-bg"
  4894. (nconc
  4895. (list (face-background 'default)
  4896. (face-background 'org-default))
  4897. (mapcar
  4898. (lambda (alist)
  4899. (when (boundp alist)
  4900. (cdr (assoc 'background-color (symbol-value alist)))))
  4901. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4902. (list (face-foreground 'org-hide))))))
  4903. (car (remove nil candidates))))
  4904. (defun org-current-time (&optional rounding-minutes past)
  4905. "Current time, possibly rounded to ROUNDING-MINUTES.
  4906. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4907. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4908. the rounding returns a past time."
  4909. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4910. (car org-time-stamp-rounding-minutes)))
  4911. (time (decode-time)) res)
  4912. (if (< r 1)
  4913. (current-time)
  4914. (setq res
  4915. (apply 'encode-time
  4916. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4917. (nthcdr 2 time))))
  4918. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4919. (seconds-to-time (- (org-float-time res) (* r 60)))
  4920. res))))
  4921. (defun org-today ()
  4922. "Return today date, considering `org-extend-today-until'."
  4923. (time-to-days
  4924. (time-subtract (current-time)
  4925. (list 0 (* 3600 org-extend-today-until) 0))))
  4926. ;;;; Font-Lock stuff, including the activators
  4927. (defvar org-mouse-map (make-sparse-keymap))
  4928. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4929. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4930. (when org-mouse-1-follows-link
  4931. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4932. (when org-tab-follows-link
  4933. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4934. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4935. (require 'font-lock)
  4936. (defconst org-non-link-chars "]\t\n\r<>")
  4937. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4938. "file+sys" "news" "shell" "elisp" "doi" "message"
  4939. "help"))
  4940. (defvar org-link-types-re nil
  4941. "Matches a link that has a url-like prefix like \"http:\"")
  4942. (defvar org-link-re-with-space nil
  4943. "Matches a link with spaces, optional angular brackets around it.")
  4944. (defvar org-link-re-with-space2 nil
  4945. "Matches a link with spaces, optional angular brackets around it.")
  4946. (defvar org-link-re-with-space3 nil
  4947. "Matches a link with spaces, only for internal part in bracket links.")
  4948. (defvar org-angle-link-re nil
  4949. "Matches link with angular brackets, spaces are allowed.")
  4950. (defvar org-plain-link-re nil
  4951. "Matches plain link, without spaces.")
  4952. (defvar org-bracket-link-regexp nil
  4953. "Matches a link in double brackets.")
  4954. (defvar org-bracket-link-analytic-regexp nil
  4955. "Regular expression used to analyze links.
  4956. Here is what the match groups contain after a match:
  4957. 1: http:
  4958. 2: http
  4959. 3: path
  4960. 4: [desc]
  4961. 5: desc")
  4962. (defvar org-bracket-link-analytic-regexp++ nil
  4963. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4964. (defvar org-any-link-re nil
  4965. "Regular expression matching any link.")
  4966. (defconst org-match-sexp-depth 3
  4967. "Number of stacked braces for sub/superscript matching.")
  4968. (defun org-create-multibrace-regexp (left right n)
  4969. "Create a regular expression which will match a balanced sexp.
  4970. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4971. as single character strings.
  4972. The regexp returned will match the entire expression including the
  4973. delimiters. It will also define a single group which contains the
  4974. match except for the outermost delimiters. The maximum depth of
  4975. stacked delimiters is N. Escaping delimiters is not possible."
  4976. (let* ((nothing (concat "[^" left right "]*?"))
  4977. (or "\\|")
  4978. (re nothing)
  4979. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4980. (while (> n 1)
  4981. (setq n (1- n)
  4982. re (concat re or next)
  4983. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4984. (concat left "\\(" re "\\)" right)))
  4985. (defconst org-match-substring-regexp
  4986. (concat
  4987. "\\(\\S-\\)\\([_^]\\)\\("
  4988. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4989. "\\|"
  4990. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4991. "\\|"
  4992. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4993. "The regular expression matching a sub- or superscript.")
  4994. (defconst org-match-substring-with-braces-regexp
  4995. (concat
  4996. "\\(\\S-\\)\\([_^]\\)"
  4997. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4998. "The regular expression matching a sub- or superscript, forcing braces.")
  4999. (defun org-make-link-regexps ()
  5000. "Update the link regular expressions.
  5001. This should be called after the variable `org-link-types' has changed."
  5002. (let ((types-re (regexp-opt org-link-types t)))
  5003. (setq org-link-types-re
  5004. (concat "\\`" types-re ":")
  5005. org-link-re-with-space
  5006. (concat "<?" types-re ":"
  5007. "\\([^" org-non-link-chars " ]"
  5008. "[^" org-non-link-chars "]*"
  5009. "[^" org-non-link-chars " ]\\)>?")
  5010. org-link-re-with-space2
  5011. (concat "<?" types-re ":"
  5012. "\\([^" org-non-link-chars " ]"
  5013. "[^\t\n\r]*"
  5014. "[^" org-non-link-chars " ]\\)>?")
  5015. org-link-re-with-space3
  5016. (concat "<?" types-re ":"
  5017. "\\([^" org-non-link-chars " ]"
  5018. "[^\t\n\r]*\\)")
  5019. org-angle-link-re
  5020. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5021. types-re)
  5022. org-plain-link-re
  5023. (concat
  5024. "\\<" types-re ":"
  5025. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5026. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5027. org-bracket-link-regexp
  5028. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5029. org-bracket-link-analytic-regexp
  5030. (concat
  5031. "\\[\\["
  5032. "\\(" types-re ":\\)?"
  5033. "\\([^]]+\\)"
  5034. "\\]"
  5035. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5036. "\\]")
  5037. org-bracket-link-analytic-regexp++
  5038. (concat
  5039. "\\[\\["
  5040. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5041. "\\([^]]+\\)"
  5042. "\\]"
  5043. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5044. "\\]")
  5045. org-any-link-re
  5046. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5047. org-angle-link-re "\\)\\|\\("
  5048. org-plain-link-re "\\)"))))
  5049. (org-make-link-regexps)
  5050. (defvar org-emph-face nil)
  5051. (defun org-do-emphasis-faces (limit)
  5052. "Run through the buffer and emphasize strings."
  5053. (let (rtn a)
  5054. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5055. (let* ((border (char-after (match-beginning 3)))
  5056. (bre (regexp-quote (char-to-string border))))
  5057. (if (and (not (= border (char-after (match-beginning 4))))
  5058. (not (save-match-data
  5059. (string-match (concat bre ".*" bre)
  5060. (replace-regexp-in-string
  5061. "\n" " "
  5062. (substring (match-string 2) 1 -1))))))
  5063. (progn
  5064. (setq rtn t)
  5065. (setq a (assoc (match-string 3) org-emphasis-alist))
  5066. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5067. 'face
  5068. (nth 1 a))
  5069. (and (nth 2 a)
  5070. (org-remove-flyspell-overlays-in
  5071. (match-beginning 0) (match-end 0)))
  5072. (add-text-properties (match-beginning 2) (match-end 2)
  5073. '(font-lock-multiline t org-emphasis t))
  5074. (when org-hide-emphasis-markers
  5075. (add-text-properties (match-end 4) (match-beginning 5)
  5076. '(invisible org-link))
  5077. (add-text-properties (match-beginning 3) (match-end 3)
  5078. '(invisible org-link))))))
  5079. (goto-char (1+ (match-beginning 0))))
  5080. rtn))
  5081. (defun org-emphasize (&optional char)
  5082. "Insert or change an emphasis, i.e. a font like bold or italic.
  5083. If there is an active region, change that region to a new emphasis.
  5084. If there is no region, just insert the marker characters and position
  5085. the cursor between them.
  5086. CHAR should be the marker character. If it is a space, it means to
  5087. remove the emphasis of the selected region.
  5088. If CHAR is not given (for example in an interactive call) it will be
  5089. prompted for."
  5090. (interactive)
  5091. (let ((erc org-emphasis-regexp-components)
  5092. (prompt "")
  5093. (string "") beg end move c s)
  5094. (if (org-region-active-p)
  5095. (setq beg (region-beginning) end (region-end)
  5096. string (buffer-substring beg end))
  5097. (setq move t))
  5098. (unless char
  5099. (message "Emphasis marker or tag: [%s]"
  5100. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5101. (setq char (read-char-exclusive)))
  5102. (if (equal char ?\ )
  5103. (setq s "" move nil)
  5104. (unless (assoc (char-to-string char) org-emphasis-alist)
  5105. (user-error "No such emphasis marker: \"%c\"" char))
  5106. (setq s (char-to-string char)))
  5107. (while (and (> (length string) 1)
  5108. (equal (substring string 0 1) (substring string -1))
  5109. (assoc (substring string 0 1) org-emphasis-alist))
  5110. (setq string (substring string 1 -1)))
  5111. (setq string (concat s string s))
  5112. (if beg (delete-region beg end))
  5113. (unless (or (bolp)
  5114. (string-match (concat "[" (nth 0 erc) "\n]")
  5115. (char-to-string (char-before (point)))))
  5116. (insert " "))
  5117. (unless (or (eobp)
  5118. (string-match (concat "[" (nth 1 erc) "\n]")
  5119. (char-to-string (char-after (point)))))
  5120. (insert " ") (backward-char 1))
  5121. (insert string)
  5122. (and move (backward-char 1))))
  5123. (defconst org-nonsticky-props
  5124. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5125. (defsubst org-rear-nonsticky-at (pos)
  5126. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5127. (defun org-activate-plain-links (limit)
  5128. "Add link properties for plain links."
  5129. (let (f hl)
  5130. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5131. (not (member 'org-tag
  5132. (get-text-property (1- (match-beginning 0)) 'face)))
  5133. (not (org-in-src-block-p)))
  5134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5135. (setq f (get-text-property (match-beginning 0) 'face))
  5136. (setq hl (org-match-string-no-properties 0))
  5137. (if (or (eq f 'org-tag)
  5138. (and (listp f) (memq 'org-tag f)))
  5139. nil
  5140. (add-text-properties (match-beginning 0) (match-end 0)
  5141. (list 'mouse-face 'highlight
  5142. 'face 'org-link
  5143. 'htmlize-link `(:uri ,hl)
  5144. 'keymap org-mouse-map))
  5145. (org-rear-nonsticky-at (match-end 0)))
  5146. t)))
  5147. (defun org-activate-code (limit)
  5148. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5149. (progn
  5150. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5151. (remove-text-properties (match-beginning 0) (match-end 0)
  5152. '(display t invisible t intangible t))
  5153. t)))
  5154. (defcustom org-src-fontify-natively t
  5155. "When non-nil, fontify code in code blocks."
  5156. :type 'boolean
  5157. :version "24.4"
  5158. :package-version '(Org . "8.3")
  5159. :group 'org-appearance
  5160. :group 'org-babel)
  5161. (defcustom org-allow-promoting-top-level-subtree nil
  5162. "When non-nil, allow promoting a top level subtree.
  5163. The leading star of the top level headline will be replaced
  5164. by a #."
  5165. :type 'boolean
  5166. :version "24.1"
  5167. :group 'org-appearance)
  5168. (defun org-fontify-meta-lines-and-blocks (limit)
  5169. (condition-case nil
  5170. (org-fontify-meta-lines-and-blocks-1 limit)
  5171. (error (message "org-mode fontification error"))))
  5172. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5173. "Fontify #+ lines and blocks."
  5174. (let ((case-fold-search t))
  5175. (if (re-search-forward
  5176. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5177. limit t)
  5178. (let ((beg (match-beginning 0))
  5179. (block-start (match-end 0))
  5180. (block-end nil)
  5181. (lang (match-string 7))
  5182. (beg1 (line-beginning-position 2))
  5183. (dc1 (downcase (match-string 2)))
  5184. (dc3 (downcase (match-string 3)))
  5185. end end1 quoting block-type ovl)
  5186. (cond
  5187. ((and (match-end 4) (equal dc3 "+begin"))
  5188. ;; Truly a block
  5189. (setq block-type (downcase (match-string 5))
  5190. quoting (member block-type org-protecting-blocks))
  5191. (when (re-search-forward
  5192. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5193. nil t) ;; on purpose, we look further than LIMIT
  5194. (setq end (min (point-max) (match-end 0))
  5195. end1 (min (point-max) (1- (match-beginning 0))))
  5196. (setq block-end (match-beginning 0))
  5197. (when quoting
  5198. (org-remove-flyspell-overlays-in beg1 end1)
  5199. (remove-text-properties beg end
  5200. '(display t invisible t intangible t)))
  5201. (add-text-properties
  5202. beg end '(font-lock-fontified t font-lock-multiline t))
  5203. (add-text-properties beg beg1 '(face org-meta-line))
  5204. (org-remove-flyspell-overlays-in beg beg1)
  5205. (add-text-properties ; For end_src
  5206. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5207. (org-remove-flyspell-overlays-in end1 end)
  5208. (cond
  5209. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5210. (org-src-font-lock-fontify-block lang block-start block-end)
  5211. (add-text-properties beg1 block-end '(src-block t)))
  5212. (quoting
  5213. (add-text-properties beg1 (min (point-max) (1+ end1))
  5214. '(face org-block))) ; end of source block
  5215. ((not org-fontify-quote-and-verse-blocks))
  5216. ((string= block-type "quote")
  5217. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5218. ((string= block-type "verse")
  5219. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5220. (add-text-properties beg beg1 '(face org-block-begin-line))
  5221. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5222. '(face org-block-end-line))
  5223. t))
  5224. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5225. (org-remove-flyspell-overlays-in
  5226. (match-beginning 0)
  5227. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5228. (add-text-properties
  5229. beg (match-end 3)
  5230. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5231. '(font-lock-fontified t invisible t)
  5232. '(font-lock-fontified t face org-document-info-keyword)))
  5233. (add-text-properties
  5234. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5235. (if (string-equal dc1 "+title:")
  5236. '(font-lock-fontified t face org-document-title)
  5237. '(font-lock-fontified t face org-document-info))))
  5238. ((equal dc1 "+caption:")
  5239. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5240. (remove-text-properties (match-beginning 0) (match-end 0)
  5241. '(display t invisible t intangible t))
  5242. (add-text-properties (match-beginning 1) (match-end 3)
  5243. '(font-lock-fontified t face org-meta-line))
  5244. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5245. '(font-lock-fontified t face org-block))
  5246. t)
  5247. ((member dc3 '(" " ""))
  5248. (org-remove-flyspell-overlays-in beg (match-end 0))
  5249. (add-text-properties
  5250. beg (match-end 0)
  5251. '(font-lock-fontified t face font-lock-comment-face)))
  5252. (t ;; just any other in-buffer setting, but not indented
  5253. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5254. (remove-text-properties (match-beginning 0) (match-end 0)
  5255. '(display t invisible t intangible t))
  5256. (add-text-properties beg (match-end 0)
  5257. '(font-lock-fontified t face org-meta-line))
  5258. t))))))
  5259. (defun org-fontify-drawers (limit)
  5260. "Fontify drawers."
  5261. (when (re-search-forward org-drawer-regexp limit t)
  5262. (add-text-properties
  5263. (match-beginning 0) (match-end 0)
  5264. '(font-lock-fontified t face org-special-keyword))
  5265. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5266. t))
  5267. (defun org-fontify-macros (limit)
  5268. "Fontify macros."
  5269. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5270. (add-text-properties
  5271. (match-beginning 0) (match-end 0)
  5272. '(font-lock-fontified t face org-macro))
  5273. (when org-hide-macro-markers
  5274. (add-text-properties (match-end 2) (match-beginning 2)
  5275. '(invisible t))
  5276. (add-text-properties (match-beginning 1) (match-end 1)
  5277. '(invisible t)))
  5278. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5279. t))
  5280. (defun org-activate-angle-links (limit)
  5281. "Add text properties for angle links."
  5282. (if (and (re-search-forward org-angle-link-re limit t)
  5283. (not (org-in-src-block-p)))
  5284. (progn
  5285. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5286. (add-text-properties (match-beginning 0) (match-end 0)
  5287. (list 'mouse-face 'highlight
  5288. 'keymap org-mouse-map
  5289. 'font-lock-multiline t))
  5290. (org-rear-nonsticky-at (match-end 0))
  5291. t)))
  5292. (defun org-activate-footnote-links (limit)
  5293. "Add text properties for footnotes."
  5294. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5295. (when fn
  5296. (let* ((beg (nth 1 fn))
  5297. (end (nth 2 fn))
  5298. (label (car fn))
  5299. (referencep (/= (line-beginning-position) beg)))
  5300. (when (and referencep (nth 3 fn))
  5301. (save-excursion
  5302. (goto-char beg)
  5303. (search-forward (or label "fn:"))
  5304. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5305. (add-text-properties beg end
  5306. (list 'mouse-face 'highlight
  5307. 'keymap org-mouse-map
  5308. 'help-echo
  5309. (if referencep "Footnote reference"
  5310. "Footnote definition")
  5311. 'font-lock-fontified t
  5312. 'font-lock-multiline t
  5313. 'face 'org-footnote))))))
  5314. (defun org-activate-bracket-links (limit)
  5315. "Add text properties for bracketed links."
  5316. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5317. (not (org-in-src-block-p)))
  5318. (let* ((hl (org-match-string-no-properties 1))
  5319. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5320. (ip (org-maybe-intangible
  5321. (list 'invisible 'org-link
  5322. 'keymap org-mouse-map 'mouse-face 'highlight
  5323. 'font-lock-multiline t 'help-echo help
  5324. 'htmlize-link `(:uri ,hl))))
  5325. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5326. 'font-lock-multiline t 'help-echo help
  5327. 'htmlize-link `(:uri ,hl))))
  5328. ;; We need to remove the invisible property here. Table narrowing
  5329. ;; may have made some of this invisible.
  5330. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5331. (remove-text-properties (match-beginning 0) (match-end 0)
  5332. '(invisible nil))
  5333. (if (match-end 3)
  5334. (progn
  5335. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5336. (org-rear-nonsticky-at (match-beginning 3))
  5337. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5338. (org-rear-nonsticky-at (match-end 3))
  5339. (add-text-properties (match-end 3) (match-end 0) ip)
  5340. (org-rear-nonsticky-at (match-end 0)))
  5341. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5342. (org-rear-nonsticky-at (match-beginning 1))
  5343. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5344. (org-rear-nonsticky-at (match-end 1))
  5345. (add-text-properties (match-end 1) (match-end 0) ip)
  5346. (org-rear-nonsticky-at (match-end 0)))
  5347. t)))
  5348. (defun org-activate-dates (limit)
  5349. "Add text properties for dates."
  5350. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5351. (not (equal (char-before (match-beginning 0)) 91)))
  5352. (progn
  5353. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5354. (add-text-properties (match-beginning 0) (match-end 0)
  5355. (list 'mouse-face 'highlight
  5356. 'keymap org-mouse-map))
  5357. (org-rear-nonsticky-at (match-end 0))
  5358. (when org-display-custom-times
  5359. (if (match-end 3)
  5360. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5361. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5362. t)))
  5363. (defvar org-target-link-regexp nil
  5364. "Regular expression matching radio targets in plain text.")
  5365. (make-variable-buffer-local 'org-target-link-regexp)
  5366. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5367. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5368. border border border))
  5369. "Regular expression matching a link target.")
  5370. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5371. "Regular expression matching a radio target.")
  5372. (defconst org-any-target-regexp
  5373. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5374. "Regular expression matching any target.")
  5375. (defun org-activate-target-links (limit)
  5376. "Add text properties for target matches."
  5377. (when org-target-link-regexp
  5378. (let ((case-fold-search t))
  5379. (if (re-search-forward org-target-link-regexp limit t)
  5380. (progn
  5381. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5382. (add-text-properties (match-beginning 1) (match-end 1)
  5383. (list 'mouse-face 'highlight
  5384. 'keymap org-mouse-map
  5385. 'help-echo "Radio target link"
  5386. 'org-linked-text t))
  5387. (org-rear-nonsticky-at (match-end 1))
  5388. t)))))
  5389. (defun org-update-radio-target-regexp ()
  5390. "Find all radio targets in this file and update the regular expression.
  5391. Also refresh fontification if needed."
  5392. (interactive)
  5393. (let ((old-regexp org-target-link-regexp)
  5394. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5395. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5396. (targets
  5397. (org-with-wide-buffer
  5398. (goto-char (point-min))
  5399. (let (rtn)
  5400. (while (re-search-forward org-radio-target-regexp nil t)
  5401. ;; Make sure point is really within the object.
  5402. (backward-char)
  5403. (let ((obj (org-element-context)))
  5404. (when (eq (org-element-type obj) 'radio-target)
  5405. (add-to-list 'rtn (org-element-property :value obj)))))
  5406. rtn))))
  5407. (setq org-target-link-regexp
  5408. (and targets
  5409. (concat before-re
  5410. (mapconcat
  5411. (lambda (x)
  5412. (replace-regexp-in-string
  5413. " +" "\\s-+" (regexp-quote x) t t))
  5414. targets
  5415. "\\|")
  5416. after-re)))
  5417. (unless (equal old-regexp org-target-link-regexp)
  5418. ;; Clean-up cache.
  5419. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5420. ((not org-target-link-regexp) old-regexp)
  5421. (t
  5422. (concat before-re
  5423. (mapconcat
  5424. (lambda (re)
  5425. (substring re (length before-re)
  5426. (- (length after-re))))
  5427. (list old-regexp org-target-link-regexp)
  5428. "\\|")
  5429. after-re)))))
  5430. (org-with-wide-buffer
  5431. (goto-char (point-min))
  5432. (while (re-search-forward regexp nil t)
  5433. (org-element-cache-refresh (match-beginning 1)))))
  5434. ;; Re fontify buffer.
  5435. (when (memq 'radio org-highlight-links)
  5436. (org-restart-font-lock)))))
  5437. (defun org-hide-wide-columns (limit)
  5438. (let (s e)
  5439. (setq s (text-property-any (point) (or limit (point-max))
  5440. 'org-cwidth t))
  5441. (when s
  5442. (setq e (next-single-property-change s 'org-cwidth))
  5443. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5444. (goto-char e)
  5445. t)))
  5446. (defvar org-latex-and-related-regexp nil
  5447. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5448. (defun org-compute-latex-and-related-regexp ()
  5449. "Compute regular expression for LaTeX, entities and sub/superscript.
  5450. Result depends on variable `org-highlight-latex-and-related'."
  5451. (org-set-local
  5452. 'org-latex-and-related-regexp
  5453. (let* ((re-sub
  5454. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5455. ((eq org-use-sub-superscripts '{})
  5456. (list org-match-substring-with-braces-regexp))
  5457. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5458. (re-latex
  5459. (when (memq 'latex org-highlight-latex-and-related)
  5460. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5461. (delq nil
  5462. (mapcar (lambda (x)
  5463. (and (member (car x) matchers) (nth 1 x)))
  5464. org-latex-regexps)))))
  5465. (re-entities
  5466. (when (memq 'entities org-highlight-latex-and-related)
  5467. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5468. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5469. (defun org-do-latex-and-related (limit)
  5470. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5471. LIMIT bounds the search for syntax to highlight. Stop at first
  5472. highlighted object, if any. Return t if some highlighting was
  5473. done, nil otherwise."
  5474. (when (org-string-nw-p org-latex-and-related-regexp)
  5475. (catch 'found
  5476. (while (re-search-forward org-latex-and-related-regexp limit t)
  5477. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5478. 'face))
  5479. '(org-code org-verbatim underline))
  5480. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5481. '(?_ ?^))
  5482. 1
  5483. 0)))
  5484. (font-lock-prepend-text-property
  5485. (+ offset (match-beginning 0)) (match-end 0)
  5486. 'face 'org-latex-and-related)
  5487. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5488. '(font-lock-multiline t)))
  5489. (throw 'found t)))
  5490. nil)))
  5491. (defun org-restart-font-lock ()
  5492. "Restart `font-lock-mode', to force refontification."
  5493. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5494. (font-lock-mode -1)
  5495. (font-lock-mode 1)))
  5496. (defun org-activate-tags (limit)
  5497. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5498. (progn
  5499. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5500. (add-text-properties (match-beginning 1) (match-end 1)
  5501. (list 'mouse-face 'highlight
  5502. 'keymap org-mouse-map))
  5503. (org-rear-nonsticky-at (match-end 1))
  5504. t)))
  5505. (defun org-outline-level ()
  5506. "Compute the outline level of the heading at point.
  5507. If this is called at a normal headline, the level is the number
  5508. of stars. Use `org-reduced-level' to remove the effect of
  5509. `org-odd-levels'. Unlike to `org-current-level', this function
  5510. takes into consideration inlinetasks."
  5511. (org-with-wide-buffer
  5512. (end-of-line)
  5513. (if (re-search-backward org-outline-regexp-bol nil t)
  5514. (1- (- (match-end 0) (match-beginning 0)))
  5515. 0)))
  5516. (defvar org-font-lock-keywords nil)
  5517. (defsubst org-re-property (property &optional literal allow-null value)
  5518. "Return a regexp matching a PROPERTY line.
  5519. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5520. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5521. non-nil, match properties even without a value.
  5522. Match group 3 is set to the value when it exists. If there is no
  5523. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5524. With optional argument VALUE, match only property lines with
  5525. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5526. unless LITERAL is non-nil."
  5527. (concat
  5528. "^\\(?4:[ \t]*\\)"
  5529. (format "\\(?1::\\(?2:%s\\):\\)"
  5530. (if literal property (regexp-quote property)))
  5531. (cond (value
  5532. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5533. (if literal value (regexp-quote value))))
  5534. (allow-null
  5535. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5536. (t
  5537. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5538. (defconst org-property-re
  5539. (org-re-property "\\S-+" 'literal t)
  5540. "Regular expression matching a property line.
  5541. There are four matching groups:
  5542. 1: :PROPKEY: including the leading and trailing colon,
  5543. 2: PROPKEY without the leading and trailing colon,
  5544. 3: PROPVAL without leading or trailing spaces,
  5545. 4: the indentation of the current line,
  5546. 5: trailing whitespace.")
  5547. (defvar org-font-lock-hook nil
  5548. "Functions to be called for special font lock stuff.")
  5549. (defvar org-font-lock-set-keywords-hook nil
  5550. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5551. This is called after `org-font-lock-extra-keywords' is defined, but before
  5552. it is installed to be used by font lock. This can be useful if something
  5553. needs to be inserted at a specific position in the font-lock sequence.")
  5554. (defun org-font-lock-hook (limit)
  5555. "Run `org-font-lock-hook' within LIMIT."
  5556. (run-hook-with-args 'org-font-lock-hook limit))
  5557. (defun org-set-font-lock-defaults ()
  5558. "Set font lock defaults for the current buffer."
  5559. (let* ((em org-fontify-emphasized-text)
  5560. (lk org-highlight-links)
  5561. (org-font-lock-extra-keywords
  5562. (list
  5563. ;; Call the hook
  5564. '(org-font-lock-hook)
  5565. ;; Headlines
  5566. `(,(if org-fontify-whole-heading-line
  5567. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5568. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5569. (1 (org-get-level-face 1))
  5570. (2 (org-get-level-face 2))
  5571. (3 (org-get-level-face 3)))
  5572. ;; Table lines
  5573. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5574. (1 'org-table t))
  5575. ;; Table internals
  5576. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5577. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5578. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5579. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5580. ;; Drawers
  5581. '(org-fontify-drawers)
  5582. ;; Properties
  5583. (list org-property-re
  5584. '(1 'org-special-keyword t)
  5585. '(3 'org-property-value t))
  5586. ;; Links
  5587. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5588. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5589. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5590. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5591. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5592. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5593. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5594. ;; Targets.
  5595. (list org-any-target-regexp '(0 'org-target t))
  5596. ;; Diary sexps.
  5597. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5598. ;; Macro
  5599. '(org-fontify-macros)
  5600. '(org-hide-wide-columns (0 nil append))
  5601. ;; TODO keyword
  5602. (list (format org-heading-keyword-regexp-format
  5603. org-todo-regexp)
  5604. '(2 (org-get-todo-face 2) t))
  5605. ;; DONE
  5606. (if org-fontify-done-headline
  5607. (list (format org-heading-keyword-regexp-format
  5608. (concat
  5609. "\\(?:"
  5610. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5611. "\\)"))
  5612. '(2 'org-headline-done t))
  5613. nil)
  5614. ;; Priorities
  5615. '(org-font-lock-add-priority-faces)
  5616. ;; Tags
  5617. '(org-font-lock-add-tag-faces)
  5618. ;; Tags groups
  5619. (if (and org-group-tags org-tag-groups-alist)
  5620. (list (concat org-outline-regexp-bol ".+\\(:"
  5621. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5622. ":\\).*$")
  5623. '(1 'org-tag-group prepend)))
  5624. ;; Special keywords
  5625. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5626. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5628. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5629. ;; Emphasis
  5630. (if em
  5631. (if (featurep 'xemacs)
  5632. '(org-do-emphasis-faces (0 nil append))
  5633. '(org-do-emphasis-faces)))
  5634. ;; Checkboxes
  5635. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5636. 1 'org-checkbox prepend)
  5637. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5638. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5639. (0 (org-get-checkbox-statistics-face) t)))
  5640. ;; Description list items
  5641. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5642. 1 'org-list-dt prepend)
  5643. ;; ARCHIVEd headings
  5644. (list (concat
  5645. org-outline-regexp-bol
  5646. "\\(.*:" org-archive-tag ":.*\\)")
  5647. '(1 'org-archived prepend))
  5648. ;; Specials
  5649. '(org-do-latex-and-related)
  5650. '(org-fontify-entities)
  5651. '(org-raise-scripts)
  5652. ;; Code
  5653. '(org-activate-code (1 'org-code t))
  5654. ;; COMMENT
  5655. (list (format
  5656. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5657. org-todo-regexp
  5658. org-comment-string)
  5659. '(9 'org-special-keyword t))
  5660. ;; Blocks and meta lines
  5661. '(org-fontify-meta-lines-and-blocks))))
  5662. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5663. (run-hooks 'org-font-lock-set-keywords-hook)
  5664. ;; Now set the full font-lock-keywords
  5665. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5666. (org-set-local 'font-lock-defaults
  5667. '(org-font-lock-keywords t nil nil backward-paragraph))
  5668. (kill-local-variable 'font-lock-keywords) nil))
  5669. (defun org-toggle-pretty-entities ()
  5670. "Toggle the composition display of entities as UTF8 characters."
  5671. (interactive)
  5672. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5673. (org-restart-font-lock)
  5674. (if org-pretty-entities
  5675. (message "Entities are now displayed as UTF8 characters")
  5676. (save-restriction
  5677. (widen)
  5678. (org-decompose-region (point-min) (point-max))
  5679. (message "Entities are now displayed as plain text"))))
  5680. (defvar org-custom-properties-overlays nil
  5681. "List of overlays used for custom properties.")
  5682. (make-variable-buffer-local 'org-custom-properties-overlays)
  5683. (defun org-toggle-custom-properties-visibility ()
  5684. "Display or hide properties in `org-custom-properties'."
  5685. (interactive)
  5686. (if org-custom-properties-overlays
  5687. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5688. (setq org-custom-properties-overlays nil))
  5689. (when org-custom-properties
  5690. (org-with-wide-buffer
  5691. (goto-char (point-min))
  5692. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5693. (while (re-search-forward regexp nil t)
  5694. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5695. (when (and end (< (point) end))
  5696. ;; Hide first custom property in current drawer.
  5697. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5698. (overlay-put o 'invisible t)
  5699. (overlay-put o 'org-custom-property t)
  5700. (push o org-custom-properties-overlays))
  5701. ;; Hide additional custom properties in the same drawer.
  5702. (while (re-search-forward regexp end t)
  5703. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5704. (overlay-put o 'invisible t)
  5705. (overlay-put o 'org-custom-property t)
  5706. (push o org-custom-properties-overlays)))))
  5707. ;; Each entry is limited to a single property drawer.
  5708. (outline-next-heading)))))))
  5709. (defun org-fontify-entities (limit)
  5710. "Find an entity to fontify."
  5711. (let (ee)
  5712. (when org-pretty-entities
  5713. (catch 'match
  5714. ;; "\_ "-family is left out on purpose. Only the first one,
  5715. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5716. ;; confusing when adding a second white space character.
  5717. (while (re-search-forward
  5718. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5719. limit t)
  5720. (if (and (not (org-at-comment-p))
  5721. (setq ee (org-entity-get (match-string 1)))
  5722. (= (length (nth 6 ee)) 1))
  5723. (let*
  5724. ((end (if (equal (match-string 2) "{}")
  5725. (match-end 2)
  5726. (match-end 1))))
  5727. (add-text-properties
  5728. (match-beginning 0) end
  5729. (list 'font-lock-fontified t))
  5730. (compose-region (match-beginning 0) end
  5731. (nth 6 ee) nil)
  5732. (backward-char 1)
  5733. (throw 'match t))))
  5734. nil))))
  5735. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5736. "Fontify string S like in Org-mode."
  5737. (with-temp-buffer
  5738. (insert s)
  5739. (let ((org-odd-levels-only odd-levels))
  5740. (org-mode)
  5741. (font-lock-ensure)
  5742. (buffer-string))))
  5743. (defvar org-m nil)
  5744. (defvar org-l nil)
  5745. (defvar org-f nil)
  5746. (defun org-get-level-face (n)
  5747. "Get the right face for match N in font-lock matching of headlines."
  5748. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5749. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5750. (if org-cycle-level-faces
  5751. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5752. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5753. (cond
  5754. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5755. ((eq n 2) org-f)
  5756. (t (if org-level-color-stars-only nil org-f))))
  5757. (defun org-face-from-face-or-color (context inherit face-or-color)
  5758. "Create a face list that inherits INHERIT, but sets the foreground color.
  5759. When FACE-OR-COLOR is not a string, just return it."
  5760. (if (stringp face-or-color)
  5761. (list :inherit inherit
  5762. (cdr (assoc context org-faces-easy-properties))
  5763. face-or-color)
  5764. face-or-color))
  5765. (defun org-get-todo-face (kwd)
  5766. "Get the right face for a TODO keyword KWD.
  5767. If KWD is a number, get the corresponding match group."
  5768. (if (numberp kwd) (setq kwd (match-string kwd)))
  5769. (or (org-face-from-face-or-color
  5770. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5771. (and (member kwd org-done-keywords) 'org-done)
  5772. 'org-todo))
  5773. (defun org-get-priority-face (priority)
  5774. "Get the right face for PRIORITY.
  5775. PRIORITY is a character."
  5776. (or (org-face-from-face-or-color
  5777. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5778. 'org-priority))
  5779. (defun org-get-tag-face (tag)
  5780. "Get the right face for TAG.
  5781. If TAG is a number, get the corresponding match group."
  5782. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5783. (or (org-face-from-face-or-color
  5784. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5785. 'org-tag)))
  5786. (defun org-font-lock-add-priority-faces (limit)
  5787. "Add the special priority faces."
  5788. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5789. (add-text-properties
  5790. (match-beginning 1) (match-end 1)
  5791. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5792. 'font-lock-fontified t))))
  5793. (defun org-font-lock-add-tag-faces (limit)
  5794. "Add the special tag faces."
  5795. (when (and org-tag-faces org-tags-special-faces-re)
  5796. (while (re-search-forward org-tags-special-faces-re limit t)
  5797. (add-text-properties (match-beginning 1) (match-end 1)
  5798. (list 'face (org-get-tag-face 1)
  5799. 'font-lock-fontified t))
  5800. (backward-char 1))))
  5801. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5802. "Remove fontification and activation overlays from links."
  5803. (font-lock-default-unfontify-region beg end)
  5804. (let* ((buffer-undo-list t)
  5805. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5806. (inhibit-modification-hooks t)
  5807. deactivate-mark buffer-file-name buffer-file-truename)
  5808. (org-decompose-region beg end)
  5809. (remove-text-properties beg end
  5810. '(mouse-face t keymap t org-linked-text t
  5811. invisible t intangible t
  5812. org-emphasis t))
  5813. (org-remove-font-lock-display-properties beg end)))
  5814. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5815. ((raise 0.3) (height 0.7))
  5816. ((raise -0.5))
  5817. ((raise 0.5)))
  5818. "Display properties for showing superscripts and subscripts.")
  5819. (defun org-remove-font-lock-display-properties (beg end)
  5820. "Remove specific display properties that have been added by font lock.
  5821. The will remove the raise properties that are used to show superscripts
  5822. and subscripts."
  5823. (let (next prop)
  5824. (while (< beg end)
  5825. (setq next (next-single-property-change beg 'display nil end)
  5826. prop (get-text-property beg 'display))
  5827. (if (member prop org-script-display)
  5828. (put-text-property beg next 'display nil))
  5829. (setq beg next))))
  5830. (defun org-raise-scripts (limit)
  5831. "Add raise properties to sub/superscripts."
  5832. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5833. (if (re-search-forward
  5834. (if (eq org-use-sub-superscripts t)
  5835. org-match-substring-regexp
  5836. org-match-substring-with-braces-regexp)
  5837. limit t)
  5838. (let* ((pos (point)) table-p comment-p
  5839. (mpos (match-beginning 3))
  5840. (emph-p (get-text-property mpos 'org-emphasis))
  5841. (link-p (get-text-property mpos 'mouse-face))
  5842. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5843. (goto-char (point-at-bol))
  5844. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5845. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5846. (goto-char pos)
  5847. ;; Handle a_b^c
  5848. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5849. (if (or comment-p emph-p link-p keyw-p)
  5850. t
  5851. (put-text-property (match-beginning 3) (match-end 0)
  5852. 'display
  5853. (if (equal (char-after (match-beginning 2)) ?^)
  5854. (nth (if table-p 3 1) org-script-display)
  5855. (nth (if table-p 2 0) org-script-display)))
  5856. (add-text-properties (match-beginning 2) (match-end 2)
  5857. (list 'invisible t
  5858. 'org-dwidth t 'org-dwidth-n 1))
  5859. (if (and (eq (char-after (match-beginning 3)) ?{)
  5860. (eq (char-before (match-end 3)) ?}))
  5861. (progn
  5862. (add-text-properties
  5863. (match-beginning 3) (1+ (match-beginning 3))
  5864. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5865. (add-text-properties
  5866. (1- (match-end 3)) (match-end 3)
  5867. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5868. t)))))
  5869. ;;;; Visibility cycling, including org-goto and indirect buffer
  5870. ;;; Cycling
  5871. (defvar org-cycle-global-status nil)
  5872. (make-variable-buffer-local 'org-cycle-global-status)
  5873. (put 'org-cycle-global-status 'org-state t)
  5874. (defvar org-cycle-subtree-status nil)
  5875. (make-variable-buffer-local 'org-cycle-subtree-status)
  5876. (put 'org-cycle-subtree-status 'org-state t)
  5877. (defvar org-inlinetask-min-level)
  5878. (defun org-unlogged-message (&rest args)
  5879. "Display a message, but avoid logging it in the *Messages* buffer."
  5880. (let ((message-log-max nil))
  5881. (apply 'message args)))
  5882. ;;;###autoload
  5883. (defun org-cycle (&optional arg)
  5884. "TAB-action and visibility cycling for Org-mode.
  5885. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5886. is outline visibility cycling, but it also invokes other actions
  5887. in special contexts.
  5888. - When this function is called with a prefix argument, rotate the entire
  5889. buffer through 3 states (global cycling)
  5890. 1. OVERVIEW: Show only top-level headlines.
  5891. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5892. 3. SHOW ALL: Show everything.
  5893. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5894. determined by the variable `org-startup-folded', and by any VISIBILITY
  5895. properties in the buffer.
  5896. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5897. including any drawers.
  5898. - When inside a table, re-align the table and move to the next field.
  5899. - When point is at the beginning of a headline, rotate the subtree started
  5900. by this line through 3 different states (local cycling)
  5901. 1. FOLDED: Only the main headline is shown.
  5902. 2. CHILDREN: The main headline and the direct children are shown.
  5903. From this state, you can move to one of the children
  5904. and zoom in further.
  5905. 3. SUBTREE: Show the entire subtree, including body text.
  5906. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5907. - When point is at the beginning of an empty headline and the variable
  5908. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5909. of the headline by demoting and promoting it to likely levels. This
  5910. speeds up creation document structure by pressing TAB once or several
  5911. times right after creating a new headline.
  5912. - When there is a numeric prefix, go up to a heading with level ARG, do
  5913. a `show-subtree' and return to the previous cursor position. If ARG
  5914. is negative, go up that many levels.
  5915. - When point is not at the beginning of a headline, execute the global
  5916. binding for TAB, which is re-indenting the line. See the option
  5917. `org-cycle-emulate-tab' for details.
  5918. - Special case: if point is at the beginning of the buffer and there is
  5919. no headline in line 1, this function will act as if called with prefix arg
  5920. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5921. But only if also the variable `org-cycle-global-at-bob' is t."
  5922. (interactive "P")
  5923. (org-load-modules-maybe)
  5924. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5925. (and org-cycle-level-after-item/entry-creation
  5926. (or (org-cycle-level)
  5927. (org-cycle-item-indentation))))
  5928. (let* ((limit-level
  5929. (or org-cycle-max-level
  5930. (and (boundp 'org-inlinetask-min-level)
  5931. org-inlinetask-min-level
  5932. (1- org-inlinetask-min-level))))
  5933. (nstars (and limit-level
  5934. (if org-odd-levels-only
  5935. (and limit-level (1- (* limit-level 2)))
  5936. limit-level)))
  5937. (org-outline-regexp
  5938. (if (not (derived-mode-p 'org-mode))
  5939. outline-regexp
  5940. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5941. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5942. (not (looking-at org-outline-regexp))))
  5943. (org-cycle-hook
  5944. (if bob-special
  5945. (delq 'org-optimize-window-after-visibility-change
  5946. (copy-sequence org-cycle-hook))
  5947. org-cycle-hook))
  5948. (pos (point)))
  5949. (if (or bob-special (equal arg '(4)))
  5950. ;; special case: use global cycling
  5951. (setq arg t))
  5952. (cond
  5953. ((equal arg '(16))
  5954. (setq last-command 'dummy)
  5955. (org-set-startup-visibility)
  5956. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5957. ((equal arg '(64))
  5958. (show-all)
  5959. (org-unlogged-message "Entire buffer visible, including drawers"))
  5960. ;; Try cdlatex TAB completion
  5961. ((org-try-cdlatex-tab))
  5962. ;; Table: enter it or move to the next field.
  5963. ((org-at-table-p 'any)
  5964. (if (org-at-table.el-p)
  5965. (message "Use C-c ' to edit table.el tables")
  5966. (if arg (org-table-edit-field t)
  5967. (org-table-justify-field-maybe)
  5968. (call-interactively 'org-table-next-field))))
  5969. ((run-hook-with-args-until-success
  5970. 'org-tab-after-check-for-table-hook))
  5971. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5972. ((eq arg t) (org-cycle-internal-global))
  5973. ;; Drawers: delegate to `org-flag-drawer'.
  5974. ((save-excursion
  5975. (beginning-of-line 1)
  5976. (looking-at org-drawer-regexp))
  5977. (org-flag-drawer ; toggle block visibility
  5978. (not (get-char-property (match-end 0) 'invisible))))
  5979. ;; Show-subtree, ARG levels up from here.
  5980. ((integerp arg)
  5981. (save-excursion
  5982. (org-back-to-heading)
  5983. (outline-up-heading (if (< arg 0) (- arg)
  5984. (- (funcall outline-level) arg)))
  5985. (org-show-subtree)))
  5986. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5987. ((and (featurep 'org-inlinetask)
  5988. (org-inlinetask-at-task-p)
  5989. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5990. (org-inlinetask-toggle-visibility))
  5991. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5992. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5993. (save-excursion (move-beginning-of-line 1)
  5994. (looking-at org-outline-regexp)))
  5995. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5996. (org-cycle-internal-local))
  5997. ;; From there: TAB emulation and template completion.
  5998. (buffer-read-only (org-back-to-heading))
  5999. ((run-hook-with-args-until-success
  6000. 'org-tab-after-check-for-cycling-hook))
  6001. ((org-try-structure-completion))
  6002. ((run-hook-with-args-until-success
  6003. 'org-tab-before-tab-emulation-hook))
  6004. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6005. (or (not (bolp))
  6006. (not (looking-at org-outline-regexp))))
  6007. (call-interactively (global-key-binding "\t")))
  6008. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6009. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6010. (or (and (eq org-cycle-emulate-tab 'white)
  6011. (= (match-end 0) (point-at-eol)))
  6012. (and (eq org-cycle-emulate-tab 'whitestart)
  6013. (>= (match-end 0) pos))))
  6014. t
  6015. (eq org-cycle-emulate-tab t))
  6016. (call-interactively (global-key-binding "\t")))
  6017. (t (save-excursion
  6018. (org-back-to-heading)
  6019. (org-cycle)))))))
  6020. (defun org-cycle-internal-global ()
  6021. "Do the global cycling action."
  6022. ;; Hack to avoid display of messages for .org attachments in Gnus
  6023. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6024. (cond
  6025. ((and (eq last-command this-command)
  6026. (eq org-cycle-global-status 'overview))
  6027. ;; We just created the overview - now do table of contents
  6028. ;; This can be slow in very large buffers, so indicate action
  6029. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6030. (unless ga (org-unlogged-message "CONTENTS..."))
  6031. (org-content)
  6032. (unless ga (org-unlogged-message "CONTENTS...done"))
  6033. (setq org-cycle-global-status 'contents)
  6034. (run-hook-with-args 'org-cycle-hook 'contents))
  6035. ((and (eq last-command this-command)
  6036. (eq org-cycle-global-status 'contents))
  6037. ;; We just showed the table of contents - now show everything
  6038. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6039. (show-all)
  6040. (unless ga (org-unlogged-message "SHOW ALL"))
  6041. (setq org-cycle-global-status 'all)
  6042. (run-hook-with-args 'org-cycle-hook 'all))
  6043. (t
  6044. ;; Default action: go to overview
  6045. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6046. (org-overview)
  6047. (unless ga (org-unlogged-message "OVERVIEW"))
  6048. (setq org-cycle-global-status 'overview)
  6049. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6050. (defvar org-called-with-limited-levels nil
  6051. "Non-nil when `org-with-limited-levels' is currently active.")
  6052. (defun org-cycle-internal-local ()
  6053. "Do the local cycling action."
  6054. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6055. ;; First, determine end of headline (EOH), end of subtree or item
  6056. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6057. (save-excursion
  6058. (if (org-at-item-p)
  6059. (progn
  6060. (beginning-of-line)
  6061. (setq struct (org-list-struct))
  6062. (setq eoh (point-at-eol))
  6063. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6064. (setq has-children (org-list-has-child-p (point) struct)))
  6065. (org-back-to-heading)
  6066. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6067. (setq eos (save-excursion (org-end-of-subtree t t)
  6068. (when (bolp) (backward-char)) (point)))
  6069. (setq has-children
  6070. (or (save-excursion
  6071. (let ((level (funcall outline-level)))
  6072. (outline-next-heading)
  6073. (and (org-at-heading-p t)
  6074. (> (funcall outline-level) level))))
  6075. (save-excursion
  6076. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6077. ;; Determine end invisible part of buffer (EOL)
  6078. (beginning-of-line 2)
  6079. ;; XEmacs doesn't have `next-single-char-property-change'
  6080. (if (featurep 'xemacs)
  6081. (while (and (not (eobp)) ;; this is like `next-line'
  6082. (get-char-property (1- (point)) 'invisible))
  6083. (beginning-of-line 2))
  6084. (while (and (not (eobp)) ;; this is like `next-line'
  6085. (get-char-property (1- (point)) 'invisible))
  6086. (goto-char (next-single-char-property-change (point) 'invisible))
  6087. (and (eolp) (beginning-of-line 2))))
  6088. (setq eol (point)))
  6089. ;; Find out what to do next and set `this-command'
  6090. (cond
  6091. ((= eos eoh)
  6092. ;; Nothing is hidden behind this heading
  6093. (unless (org-before-first-heading-p)
  6094. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6095. (org-unlogged-message "EMPTY ENTRY")
  6096. (setq org-cycle-subtree-status nil)
  6097. (save-excursion
  6098. (goto-char eos)
  6099. (outline-next-heading)
  6100. (if (outline-invisible-p) (org-flag-heading nil))))
  6101. ((and (or (>= eol eos)
  6102. (not (string-match "\\S-" (buffer-substring eol eos))))
  6103. (or has-children
  6104. (not (setq children-skipped
  6105. org-cycle-skip-children-state-if-no-children))))
  6106. ;; Entire subtree is hidden in one line: children view
  6107. (unless (org-before-first-heading-p)
  6108. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6109. (if (org-at-item-p)
  6110. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6111. (org-show-entry)
  6112. (org-with-limited-levels (show-children))
  6113. ;; FIXME: This slows down the func way too much.
  6114. ;; How keep drawers hidden in subtree anyway?
  6115. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6116. ;; (org-cycle-hide-drawers 'subtree))
  6117. ;; Fold every list in subtree to top-level items.
  6118. (when (eq org-cycle-include-plain-lists 'integrate)
  6119. (save-excursion
  6120. (org-back-to-heading)
  6121. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6122. (beginning-of-line 1)
  6123. (let* ((struct (org-list-struct))
  6124. (prevs (org-list-prevs-alist struct))
  6125. (end (org-list-get-bottom-point struct)))
  6126. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6127. (org-list-get-all-items (point) struct prevs))
  6128. (goto-char (if (< end eos) end eos)))))))
  6129. (org-unlogged-message "CHILDREN")
  6130. (save-excursion
  6131. (goto-char eos)
  6132. (outline-next-heading)
  6133. (if (outline-invisible-p) (org-flag-heading nil)))
  6134. (setq org-cycle-subtree-status 'children)
  6135. (unless (org-before-first-heading-p)
  6136. (run-hook-with-args 'org-cycle-hook 'children)))
  6137. ((or children-skipped
  6138. (and (eq last-command this-command)
  6139. (eq org-cycle-subtree-status 'children)))
  6140. ;; We just showed the children, or no children are there,
  6141. ;; now show everything.
  6142. (unless (org-before-first-heading-p)
  6143. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6144. (outline-flag-region eoh eos nil)
  6145. (org-unlogged-message
  6146. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6147. (setq org-cycle-subtree-status 'subtree)
  6148. (unless (org-before-first-heading-p)
  6149. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6150. (t
  6151. ;; Default action: hide the subtree.
  6152. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6153. (outline-flag-region eoh eos t)
  6154. (org-unlogged-message "FOLDED")
  6155. (setq org-cycle-subtree-status 'folded)
  6156. (unless (org-before-first-heading-p)
  6157. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6158. ;;;###autoload
  6159. (defun org-global-cycle (&optional arg)
  6160. "Cycle the global visibility. For details see `org-cycle'.
  6161. With \\[universal-argument] prefix arg, switch to startup visibility.
  6162. With a numeric prefix, show all headlines up to that level."
  6163. (interactive "P")
  6164. (let ((org-cycle-include-plain-lists
  6165. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6166. (cond
  6167. ((integerp arg)
  6168. (show-all)
  6169. (hide-sublevels arg)
  6170. (setq org-cycle-global-status 'contents))
  6171. ((equal arg '(4))
  6172. (org-set-startup-visibility)
  6173. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6174. (t
  6175. (org-cycle '(4))))))
  6176. (defun org-set-startup-visibility ()
  6177. "Set the visibility required by startup options and properties."
  6178. (cond
  6179. ((eq org-startup-folded t)
  6180. (org-overview))
  6181. ((eq org-startup-folded 'content)
  6182. (org-content))
  6183. ((or (eq org-startup-folded 'showeverything)
  6184. (eq org-startup-folded nil))
  6185. (show-all)))
  6186. (unless (eq org-startup-folded 'showeverything)
  6187. (if org-hide-block-startup (org-hide-block-all))
  6188. (org-set-visibility-according-to-property 'no-cleanup)
  6189. (org-cycle-hide-archived-subtrees 'all)
  6190. (org-cycle-hide-drawers 'all)
  6191. (org-cycle-show-empty-lines t)))
  6192. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6193. "Switch subtree visibilities according to :VISIBILITY: property."
  6194. (interactive)
  6195. (let (org-show-entry-below)
  6196. (org-with-wide-buffer
  6197. (goto-char (point-min))
  6198. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6199. (if (not (org-at-property-p)) (outline-next-heading)
  6200. (let ((state (match-string 3)))
  6201. (save-excursion
  6202. (org-back-to-heading t)
  6203. (hide-subtree)
  6204. (org-reveal)
  6205. (cond
  6206. ((equal state "folded")
  6207. (hide-subtree))
  6208. ((equal state "children")
  6209. (org-show-hidden-entry)
  6210. (show-children))
  6211. ((equal state "content")
  6212. (save-excursion
  6213. (save-restriction
  6214. (org-narrow-to-subtree)
  6215. (org-content))))
  6216. ((member state '("all" "showall"))
  6217. (show-subtree)))))))
  6218. (unless no-cleanup
  6219. (org-cycle-hide-archived-subtrees 'all)
  6220. (org-cycle-hide-drawers 'all)
  6221. (org-cycle-show-empty-lines 'all)))))
  6222. ;; This function uses outline-regexp instead of the more fundamental
  6223. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6224. ;; buffers, where outline-regexp is needed.
  6225. (defun org-overview ()
  6226. "Switch to overview mode, showing only top-level headlines.
  6227. This shows all headlines with a level equal or greater than the level
  6228. of the first headline in the buffer. This is important, because if the
  6229. first headline is not level one, then (hide-sublevels 1) gives confusing
  6230. results."
  6231. (interactive)
  6232. (save-excursion
  6233. (let ((level
  6234. (save-excursion
  6235. (goto-char (point-min))
  6236. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6237. (progn
  6238. (goto-char (match-beginning 0))
  6239. (funcall outline-level))))))
  6240. (and level (hide-sublevels level)))))
  6241. (defun org-content (&optional arg)
  6242. "Show all headlines in the buffer, like a table of contents.
  6243. With numerical argument N, show content up to level N."
  6244. (interactive "P")
  6245. (org-overview)
  6246. (save-excursion
  6247. ;; Visit all headings and show their offspring
  6248. (and (integerp arg) (org-overview))
  6249. (goto-char (point-max))
  6250. (catch 'exit
  6251. (while (and (progn (condition-case nil
  6252. (outline-previous-visible-heading 1)
  6253. (error (goto-char (point-min))))
  6254. t)
  6255. (looking-at org-outline-regexp))
  6256. (if (integerp arg)
  6257. (show-children (1- arg))
  6258. (show-branches))
  6259. (if (bobp) (throw 'exit nil))))))
  6260. (defun org-optimize-window-after-visibility-change (state)
  6261. "Adjust the window after a change in outline visibility.
  6262. This function is the default value of the hook `org-cycle-hook'."
  6263. (when (get-buffer-window (current-buffer))
  6264. (cond
  6265. ((eq state 'content) nil)
  6266. ((eq state 'all) nil)
  6267. ((eq state 'folded) nil)
  6268. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6269. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6270. (defun org-remove-empty-overlays-at (pos)
  6271. "Remove outline overlays that do not contain non-white stuff."
  6272. (mapc
  6273. (lambda (o)
  6274. (and (eq 'outline (overlay-get o 'invisible))
  6275. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6276. (overlay-end o))))
  6277. (delete-overlay o)))
  6278. (overlays-at pos)))
  6279. (defun org-clean-visibility-after-subtree-move ()
  6280. "Fix visibility issues after moving a subtree."
  6281. ;; First, find a reasonable region to look at:
  6282. ;; Start two siblings above, end three below
  6283. (let* ((beg (save-excursion
  6284. (and (org-get-last-sibling)
  6285. (org-get-last-sibling))
  6286. (point)))
  6287. (end (save-excursion
  6288. (and (org-get-next-sibling)
  6289. (org-get-next-sibling)
  6290. (org-get-next-sibling))
  6291. (if (org-at-heading-p)
  6292. (point-at-eol)
  6293. (point))))
  6294. (level (looking-at "\\*+"))
  6295. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6296. (save-excursion
  6297. (save-restriction
  6298. (narrow-to-region beg end)
  6299. (when re
  6300. ;; Properly fold already folded siblings
  6301. (goto-char (point-min))
  6302. (while (re-search-forward re nil t)
  6303. (if (and (not (outline-invisible-p))
  6304. (save-excursion
  6305. (goto-char (point-at-eol)) (outline-invisible-p)))
  6306. (hide-entry))))
  6307. (org-cycle-show-empty-lines 'overview)
  6308. (org-cycle-hide-drawers 'overview)))))
  6309. (defun org-cycle-show-empty-lines (state)
  6310. "Show empty lines above all visible headlines.
  6311. The region to be covered depends on STATE when called through
  6312. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6313. entire buffer covered. Note that an empty line is only shown if there
  6314. are at least `org-cycle-separator-lines' empty lines before the headline."
  6315. (when (/= org-cycle-separator-lines 0)
  6316. (save-excursion
  6317. (let* ((n (abs org-cycle-separator-lines))
  6318. (re (cond
  6319. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6320. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6321. (t (let ((ns (number-to-string (- n 2))))
  6322. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6323. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6324. beg end)
  6325. (cond
  6326. ((memq state '(overview contents t))
  6327. (setq beg (point-min) end (point-max)))
  6328. ((memq state '(children folded))
  6329. (setq beg (point)
  6330. end (progn (org-end-of-subtree t t)
  6331. (line-beginning-position 2)))))
  6332. (when beg
  6333. (goto-char beg)
  6334. (while (re-search-forward re end t)
  6335. (unless (get-char-property (match-end 1) 'invisible)
  6336. (let ((e (match-end 1))
  6337. (b (if (>= org-cycle-separator-lines 0)
  6338. (match-beginning 1)
  6339. (save-excursion
  6340. (goto-char (match-beginning 0))
  6341. (skip-chars-backward " \t\n")
  6342. (line-end-position)))))
  6343. (outline-flag-region b e nil))))))))
  6344. ;; Never hide empty lines at the end of the file.
  6345. (save-excursion
  6346. (goto-char (point-max))
  6347. (outline-previous-heading)
  6348. (outline-end-of-heading)
  6349. (if (and (looking-at "[ \t\n]+")
  6350. (= (match-end 0) (point-max)))
  6351. (outline-flag-region (point) (match-end 0) nil))))
  6352. (defun org-show-empty-lines-in-parent ()
  6353. "Move to the parent and re-show empty lines before visible headlines."
  6354. (save-excursion
  6355. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6356. (org-cycle-show-empty-lines context))))
  6357. (defun org-files-list ()
  6358. "Return `org-agenda-files' list, plus all open org-mode files.
  6359. This is useful for operations that need to scan all of a user's
  6360. open and agenda-wise Org files."
  6361. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6362. (dolist (buf (buffer-list))
  6363. (with-current-buffer buf
  6364. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6365. (let ((file (expand-file-name (buffer-file-name))))
  6366. (unless (member file files)
  6367. (push file files))))))
  6368. files))
  6369. (defsubst org-entry-beginning-position ()
  6370. "Return the beginning position of the current entry."
  6371. (save-excursion (outline-back-to-heading t) (point)))
  6372. (defsubst org-entry-end-position ()
  6373. "Return the end position of the current entry."
  6374. (save-excursion (outline-next-heading) (point)))
  6375. (defun org-cycle-hide-drawers (state &optional exceptions)
  6376. "Re-hide all drawers after a visibility state change.
  6377. When non-nil, optional argument EXCEPTIONS is a list of strings
  6378. specifying which drawers should not be hidden."
  6379. (when (and (derived-mode-p 'org-mode)
  6380. (not (memq state '(overview folded contents))))
  6381. (save-excursion
  6382. (let* ((globalp (memq state '(contents all)))
  6383. (beg (if globalp (point-min) (point)))
  6384. (end (if globalp (point-max)
  6385. (if (eq state 'children)
  6386. (save-excursion (outline-next-heading) (point))
  6387. (org-end-of-subtree t)))))
  6388. (goto-char beg)
  6389. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6390. (unless (member-ignore-case (match-string 1) exceptions)
  6391. (let ((drawer (org-element-at-point)))
  6392. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6393. (org-flag-drawer t drawer)
  6394. ;; Make sure to skip drawer entirely or we might flag
  6395. ;; it another time when matching its ending line with
  6396. ;; `org-drawer-regexp'.
  6397. (goto-char (org-element-property :end drawer))))))))))
  6398. (defun org-flag-drawer (flag &optional element)
  6399. "When FLAG is non-nil, hide the drawer we are at.
  6400. Otherwise make it visible. When optional argument ELEMENT is
  6401. a parsed drawer, as returned by `org-element-at-point', hide or
  6402. show that drawer instead."
  6403. (when (save-excursion
  6404. (beginning-of-line)
  6405. (org-looking-at-p org-drawer-regexp))
  6406. (let ((drawer (or element (org-element-at-point))))
  6407. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6408. (let ((post (org-element-property :post-affiliated drawer)))
  6409. (save-excursion
  6410. (outline-flag-region
  6411. (progn (goto-char post) (line-end-position))
  6412. (progn (goto-char (org-element-property :end drawer))
  6413. (skip-chars-backward " \r\t\n")
  6414. (line-end-position))
  6415. flag))
  6416. ;; When the drawer is hidden away, make sure point lies in
  6417. ;; a visible part of the buffer.
  6418. (when (and flag (> (line-beginning-position) post))
  6419. (goto-char post)))))))
  6420. (defun org-subtree-end-visible-p ()
  6421. "Is the end of the current subtree visible?"
  6422. (pos-visible-in-window-p
  6423. (save-excursion (org-end-of-subtree t) (point))))
  6424. (defun org-first-headline-recenter ()
  6425. "Move cursor to the first headline and recenter the headline."
  6426. (goto-char (point-min))
  6427. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6428. (set-window-start (selected-window) (point-at-bol))))
  6429. ;;; Saving and restoring visibility
  6430. (defun org-outline-overlay-data (&optional use-markers)
  6431. "Return a list of the locations of all outline overlays.
  6432. These are overlays with the `invisible' property value `outline'.
  6433. The return value is a list of cons cells, with start and stop
  6434. positions for each overlay.
  6435. If USE-MARKERS is set, return the positions as markers."
  6436. (let (beg end)
  6437. (save-excursion
  6438. (save-restriction
  6439. (widen)
  6440. (delq nil
  6441. (mapcar (lambda (o)
  6442. (when (eq (overlay-get o 'invisible) 'outline)
  6443. (setq beg (overlay-start o)
  6444. end (overlay-end o))
  6445. (and beg end (> end beg)
  6446. (if use-markers
  6447. (cons (copy-marker beg)
  6448. (copy-marker end t))
  6449. (cons beg end)))))
  6450. (overlays-in (point-min) (point-max))))))))
  6451. (defun org-set-outline-overlay-data (data)
  6452. "Create visibility overlays for all positions in DATA.
  6453. DATA should have been made by `org-outline-overlay-data'."
  6454. (let (o)
  6455. (save-excursion
  6456. (save-restriction
  6457. (widen)
  6458. (show-all)
  6459. (mapc (lambda (c)
  6460. (outline-flag-region (car c) (cdr c) t))
  6461. data)))))
  6462. ;;; Folding of blocks
  6463. (defvar org-hide-block-overlays nil
  6464. "Overlays hiding blocks.")
  6465. (make-variable-buffer-local 'org-hide-block-overlays)
  6466. (defun org-block-map (function &optional start end)
  6467. "Call FUNCTION at the head of all source blocks in the current buffer.
  6468. Optional arguments START and END can be used to limit the range."
  6469. (let ((start (or start (point-min)))
  6470. (end (or end (point-max))))
  6471. (save-excursion
  6472. (goto-char start)
  6473. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6474. (save-excursion
  6475. (save-match-data
  6476. (goto-char (match-beginning 0))
  6477. (funcall function)))))))
  6478. (defun org-hide-block-toggle-all ()
  6479. "Toggle the visibility of all blocks in the current buffer."
  6480. (org-block-map 'org-hide-block-toggle))
  6481. (defun org-hide-block-all ()
  6482. "Fold all blocks in the current buffer."
  6483. (interactive)
  6484. (org-show-block-all)
  6485. (org-block-map 'org-hide-block-toggle-maybe))
  6486. (defun org-show-block-all ()
  6487. "Unfold all blocks in the current buffer."
  6488. (interactive)
  6489. (mapc 'delete-overlay org-hide-block-overlays)
  6490. (setq org-hide-block-overlays nil))
  6491. (defun org-hide-block-toggle-maybe ()
  6492. "Toggle visibility of block at point.
  6493. Unlike to `org-hide-block-toggle', this function does not throw
  6494. an error. Return a non-nil value when toggling is successful."
  6495. (interactive)
  6496. (ignore-errors (org-hide-block-toggle)))
  6497. (defun org-hide-block-toggle (&optional force)
  6498. "Toggle the visibility of the current block.
  6499. When optional argument FORCE is `off', make block visible. If it
  6500. is non-nil, hide it unconditionally. Throw an error when not at
  6501. a block. Return a non-nil value when toggling is successful."
  6502. (interactive)
  6503. (let ((element (org-element-at-point)))
  6504. (unless (memq (org-element-type element)
  6505. '(center-block comment-block dynamic-block example-block
  6506. export-block quote-block special-block
  6507. src-block verse-block))
  6508. (user-error "Not at a block"))
  6509. (let* ((start (save-excursion
  6510. (goto-char (org-element-property :post-affiliated element))
  6511. (line-end-position)))
  6512. (end (save-excursion
  6513. (goto-char (org-element-property :end element))
  6514. (skip-chars-backward " \r\t\n")
  6515. (line-end-position)))
  6516. (overlays (overlays-at start)))
  6517. (cond
  6518. ;; Do nothing when not before or at the block opening line or
  6519. ;; at the block closing line.
  6520. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6521. ((and (not (eq force 'off))
  6522. (not (memq t (mapcar
  6523. (lambda (o)
  6524. (eq (overlay-get o 'invisible) 'org-hide-block))
  6525. overlays))))
  6526. (let ((ov (make-overlay start end)))
  6527. (overlay-put ov 'invisible 'org-hide-block)
  6528. ;; Make the block accessible to `isearch'.
  6529. (overlay-put
  6530. ov 'isearch-open-invisible
  6531. (lambda (ov)
  6532. (when (memq ov org-hide-block-overlays)
  6533. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6534. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6535. (delete-overlay ov))))
  6536. (push ov org-hide-block-overlays)
  6537. ;; When the block is hidden away, make sure point is left in
  6538. ;; a visible part of the buffer.
  6539. (when (> (line-beginning-position) start)
  6540. (goto-char start)
  6541. (beginning-of-line))
  6542. ;; Signal successful toggling.
  6543. t))
  6544. ((or (not force) (eq force 'off))
  6545. (dolist (ov overlays t)
  6546. (when (memq ov org-hide-block-overlays)
  6547. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6548. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6549. (delete-overlay ov))))))))
  6550. ;; org-tab-after-check-for-cycling-hook
  6551. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6552. ;; Remove overlays when changing major mode
  6553. (add-hook 'org-mode-hook
  6554. (lambda () (org-add-hook 'change-major-mode-hook
  6555. 'org-show-block-all 'append 'local)))
  6556. ;;; Org-goto
  6557. (defvar org-goto-window-configuration nil)
  6558. (defvar org-goto-marker nil)
  6559. (defvar org-goto-map)
  6560. (defun org-goto-map ()
  6561. "Set the keymap `org-goto'."
  6562. (setq org-goto-map
  6563. (let ((map (make-sparse-keymap)))
  6564. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6565. mouse-drag-region universal-argument org-occur)))
  6566. (dolist (cmd cmds)
  6567. (substitute-key-definition cmd cmd map global-map)))
  6568. (suppress-keymap map)
  6569. (org-defkey map "\C-m" 'org-goto-ret)
  6570. (org-defkey map [(return)] 'org-goto-ret)
  6571. (org-defkey map [(left)] 'org-goto-left)
  6572. (org-defkey map [(right)] 'org-goto-right)
  6573. (org-defkey map [(control ?g)] 'org-goto-quit)
  6574. (org-defkey map "\C-i" 'org-cycle)
  6575. (org-defkey map [(tab)] 'org-cycle)
  6576. (org-defkey map [(down)] 'outline-next-visible-heading)
  6577. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6578. (if org-goto-auto-isearch
  6579. (if (fboundp 'define-key-after)
  6580. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6581. nil)
  6582. (org-defkey map "q" 'org-goto-quit)
  6583. (org-defkey map "n" 'outline-next-visible-heading)
  6584. (org-defkey map "p" 'outline-previous-visible-heading)
  6585. (org-defkey map "f" 'outline-forward-same-level)
  6586. (org-defkey map "b" 'outline-backward-same-level)
  6587. (org-defkey map "u" 'outline-up-heading))
  6588. (org-defkey map "/" 'org-occur)
  6589. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6590. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6591. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6592. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6593. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6594. map)))
  6595. (defconst org-goto-help
  6596. "Browse buffer copy, to find location or copy text.%s
  6597. RET=jump to location C-g=quit and return to previous location
  6598. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6599. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6600. (defun org-goto (&optional alternative-interface)
  6601. "Look up a different location in the current file, keeping current visibility.
  6602. When you want look-up or go to a different location in a
  6603. document, the fastest way is often to fold the entire buffer and
  6604. then dive into the tree. This method has the disadvantage, that
  6605. the previous location will be folded, which may not be what you
  6606. want.
  6607. This command works around this by showing a copy of the current
  6608. buffer in an indirect buffer, in overview mode. You can dive
  6609. into the tree in that copy, use org-occur and incremental search
  6610. to find a location. When pressing RET or `Q', the command
  6611. returns to the original buffer in which the visibility is still
  6612. unchanged. After RET it will also jump to the location selected
  6613. in the indirect buffer and expose the headline hierarchy above.
  6614. With a prefix argument, use the alternative interface: e.g. if
  6615. `org-goto-interface' is 'outline use 'outline-path-completion."
  6616. (interactive "P")
  6617. (org-goto-map)
  6618. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6619. (org-refile-use-outline-path t)
  6620. (org-refile-target-verify-function nil)
  6621. (interface
  6622. (if (not alternative-interface)
  6623. org-goto-interface
  6624. (if (eq org-goto-interface 'outline)
  6625. 'outline-path-completion
  6626. 'outline)))
  6627. (org-goto-start-pos (point))
  6628. (selected-point
  6629. (if (eq interface 'outline)
  6630. (car (org-get-location (current-buffer) org-goto-help))
  6631. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6632. (org-refile-check-position pa)
  6633. (nth 3 pa)))))
  6634. (if selected-point
  6635. (progn
  6636. (org-mark-ring-push org-goto-start-pos)
  6637. (goto-char selected-point)
  6638. (if (or (outline-invisible-p) (org-invisible-p2))
  6639. (org-show-context 'org-goto)))
  6640. (message "Quit"))))
  6641. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6642. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6643. (defvar org-goto-local-auto-isearch-map) ; defined below
  6644. (defun org-get-location (buf help)
  6645. "Let the user select a location in the Org-mode buffer BUF.
  6646. This function uses a recursive edit. It returns the selected position
  6647. or nil."
  6648. (org-no-popups
  6649. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6650. (isearch-hide-immediately nil)
  6651. (isearch-search-fun-function
  6652. (lambda () 'org-goto-local-search-headings))
  6653. (org-goto-selected-point org-goto-exit-command))
  6654. (save-excursion
  6655. (save-window-excursion
  6656. (delete-other-windows)
  6657. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6658. (org-pop-to-buffer-same-window
  6659. (condition-case nil
  6660. (make-indirect-buffer (current-buffer) "*org-goto*")
  6661. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6662. (with-output-to-temp-buffer "*Org Help*"
  6663. (princ (format help (if org-goto-auto-isearch
  6664. " Just type for auto-isearch."
  6665. " n/p/f/b/u to navigate, q to quit."))))
  6666. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6667. (setq buffer-read-only nil)
  6668. (let ((org-startup-truncated t)
  6669. (org-startup-folded nil)
  6670. (org-startup-align-all-tables nil))
  6671. (org-mode)
  6672. (org-overview))
  6673. (setq buffer-read-only t)
  6674. (if (and (boundp 'org-goto-start-pos)
  6675. (integer-or-marker-p org-goto-start-pos))
  6676. (progn (goto-char org-goto-start-pos)
  6677. (when (outline-invisible-p)
  6678. (org-show-set-visibility 'lineage)))
  6679. (goto-char (point-min)))
  6680. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6681. (message "Select location and press RET")
  6682. (use-local-map org-goto-map)
  6683. (recursive-edit)))
  6684. (kill-buffer "*org-goto*")
  6685. (cons org-goto-selected-point org-goto-exit-command))))
  6686. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6687. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6688. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6689. (if (fboundp 'isearch-other-control-char)
  6690. (progn
  6691. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6692. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6693. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6694. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6695. (define-key org-goto-local-auto-isearch-map [return] nil))
  6696. (defun org-goto-local-search-headings (string bound noerror)
  6697. "Search and make sure that any matches are in headlines."
  6698. (catch 'return
  6699. (while (if isearch-forward
  6700. (search-forward string bound noerror)
  6701. (search-backward string bound noerror))
  6702. (when (save-match-data
  6703. (and (save-excursion
  6704. (beginning-of-line)
  6705. (looking-at org-complex-heading-regexp))
  6706. (or (not (match-beginning 5))
  6707. (< (point) (match-beginning 5)))))
  6708. (throw 'return (point))))))
  6709. (defun org-goto-local-auto-isearch ()
  6710. "Start isearch."
  6711. (interactive)
  6712. (goto-char (point-min))
  6713. (let ((keys (this-command-keys)))
  6714. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6715. (isearch-mode t)
  6716. (isearch-process-search-char (string-to-char keys)))))
  6717. (defun org-goto-ret (&optional arg)
  6718. "Finish `org-goto' by going to the new location."
  6719. (interactive "P")
  6720. (setq org-goto-selected-point (point)
  6721. org-goto-exit-command 'return)
  6722. (throw 'exit nil))
  6723. (defun org-goto-left ()
  6724. "Finish `org-goto' by going to the new location."
  6725. (interactive)
  6726. (if (org-at-heading-p)
  6727. (progn
  6728. (beginning-of-line 1)
  6729. (setq org-goto-selected-point (point)
  6730. org-goto-exit-command 'left)
  6731. (throw 'exit nil))
  6732. (user-error "Not on a heading")))
  6733. (defun org-goto-right ()
  6734. "Finish `org-goto' by going to the new location."
  6735. (interactive)
  6736. (if (org-at-heading-p)
  6737. (progn
  6738. (setq org-goto-selected-point (point)
  6739. org-goto-exit-command 'right)
  6740. (throw 'exit nil))
  6741. (user-error "Not on a heading")))
  6742. (defun org-goto-quit ()
  6743. "Finish `org-goto' without cursor motion."
  6744. (interactive)
  6745. (setq org-goto-selected-point nil)
  6746. (setq org-goto-exit-command 'quit)
  6747. (throw 'exit nil))
  6748. ;;; Indirect buffer display of subtrees
  6749. (defvar org-indirect-dedicated-frame nil
  6750. "This is the frame being used for indirect tree display.")
  6751. (defvar org-last-indirect-buffer nil)
  6752. (defun org-tree-to-indirect-buffer (&optional arg)
  6753. "Create indirect buffer and narrow it to current subtree.
  6754. With a numerical prefix ARG, go up to this level and then take that tree.
  6755. If ARG is negative, go up that many levels.
  6756. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6757. indirect buffer previously made with this command, to avoid proliferation of
  6758. indirect buffers. However, when you call the command with a \
  6759. \\[universal-argument] prefix, or
  6760. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6761. is kept so that you can work with several indirect buffers at the same time.
  6762. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6763. \\[universal-argument] prefix also
  6764. requests that a new frame be made for the new buffer, so that the dedicated
  6765. frame is not changed."
  6766. (interactive "P")
  6767. (let ((cbuf (current-buffer))
  6768. (cwin (selected-window))
  6769. (pos (point))
  6770. beg end level heading ibuf)
  6771. (save-excursion
  6772. (org-back-to-heading t)
  6773. (when (numberp arg)
  6774. (setq level (org-outline-level))
  6775. (if (< arg 0) (setq arg (+ level arg)))
  6776. (while (> (setq level (org-outline-level)) arg)
  6777. (org-up-heading-safe)))
  6778. (setq beg (point)
  6779. heading (org-get-heading))
  6780. (org-end-of-subtree t t)
  6781. (if (org-at-heading-p) (backward-char 1))
  6782. (setq end (point)))
  6783. (if (and (buffer-live-p org-last-indirect-buffer)
  6784. (not (eq org-indirect-buffer-display 'new-frame))
  6785. (not arg))
  6786. (kill-buffer org-last-indirect-buffer))
  6787. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6788. org-last-indirect-buffer ibuf)
  6789. (cond
  6790. ((or (eq org-indirect-buffer-display 'new-frame)
  6791. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6792. (select-frame (make-frame))
  6793. (delete-other-windows)
  6794. (org-pop-to-buffer-same-window ibuf)
  6795. (org-set-frame-title heading))
  6796. ((eq org-indirect-buffer-display 'dedicated-frame)
  6797. (raise-frame
  6798. (select-frame (or (and org-indirect-dedicated-frame
  6799. (frame-live-p org-indirect-dedicated-frame)
  6800. org-indirect-dedicated-frame)
  6801. (setq org-indirect-dedicated-frame (make-frame)))))
  6802. (delete-other-windows)
  6803. (org-pop-to-buffer-same-window ibuf)
  6804. (org-set-frame-title (concat "Indirect: " heading)))
  6805. ((eq org-indirect-buffer-display 'current-window)
  6806. (org-pop-to-buffer-same-window ibuf))
  6807. ((eq org-indirect-buffer-display 'other-window)
  6808. (pop-to-buffer ibuf))
  6809. (t (error "Invalid value")))
  6810. (if (featurep 'xemacs)
  6811. (save-excursion (org-mode) (turn-on-font-lock)))
  6812. (narrow-to-region beg end)
  6813. (show-all)
  6814. (goto-char pos)
  6815. (run-hook-with-args 'org-cycle-hook 'all)
  6816. (and (window-live-p cwin) (select-window cwin))))
  6817. (defun org-get-indirect-buffer (&optional buffer heading)
  6818. (setq buffer (or buffer (current-buffer)))
  6819. (let ((n 1) (base (buffer-name buffer)) bname)
  6820. (while (buffer-live-p
  6821. (get-buffer
  6822. (setq bname
  6823. (concat base "-"
  6824. (if heading (concat heading "-" (number-to-string n))
  6825. (number-to-string n))))))
  6826. (setq n (1+ n)))
  6827. (condition-case nil
  6828. (make-indirect-buffer buffer bname 'clone)
  6829. (error (make-indirect-buffer buffer bname)))))
  6830. (defun org-set-frame-title (title)
  6831. "Set the title of the current frame to the string TITLE."
  6832. ;; FIXME: how to name a single frame in XEmacs???
  6833. (unless (featurep 'xemacs)
  6834. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6835. ;;;; Structure editing
  6836. ;;; Inserting headlines
  6837. (defun org-previous-line-empty-p (&optional next)
  6838. "Is the previous line a blank line?
  6839. When NEXT is non-nil, check the next line instead."
  6840. (save-excursion
  6841. (and (not (bobp))
  6842. (or (beginning-of-line (if next 2 0)) t)
  6843. (save-match-data
  6844. (looking-at "[ \t]*$")))))
  6845. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6846. "Insert a new heading or an item with the same depth at point.
  6847. If point is at the beginning of a heading or a list item, insert
  6848. a new heading or a new item above the current one. If point is
  6849. at the beginning of a normal line, turn the line into a heading.
  6850. If point is in the middle of a headline or a list item, split the
  6851. headline or the item and create a new headline/item with the text
  6852. in the current line after point \(see `org-M-RET-may-split-line'
  6853. on how to modify this behavior).
  6854. With one universal prefix argument, set the user option
  6855. `org-insert-heading-respect-content' to t for the duration of
  6856. the command. This modifies the behavior described above in this
  6857. ways: on list items and at the beginning of normal lines, force
  6858. the insertion of a heading after the current subtree.
  6859. With two universal prefix arguments, insert the heading at the
  6860. end of the grandparent subtree. For example, if point is within
  6861. a 2nd-level heading, then it will insert a 2nd-level heading at
  6862. the end of the 1st-level parent heading.
  6863. If point is at the beginning of a headline, insert a sibling
  6864. before the current headline. If point is not at the beginning,
  6865. split the line and create a new headline with the text in the
  6866. current line after point \(see `org-M-RET-may-split-line' on how
  6867. to modify this behavior).
  6868. If point is at the beginning of a normal line, turn this line
  6869. into a heading.
  6870. When INVISIBLE-OK is set, stop at invisible headlines when going
  6871. back. This is important for non-interactive uses of the
  6872. command.
  6873. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6874. heading, unconditionally."
  6875. (interactive "P")
  6876. (if (org-called-interactively-p 'any) (org-reveal))
  6877. (let ((itemp (and (not top-level) (org-in-item-p)))
  6878. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6879. (respect-content (or org-insert-heading-respect-content
  6880. (equal arg '(4))))
  6881. (initial-content ""))
  6882. (cond
  6883. ((or (= (buffer-size) 0)
  6884. (and (not (save-excursion
  6885. (and (ignore-errors (org-back-to-heading invisible-ok))
  6886. (org-at-heading-p))))
  6887. (or arg (not itemp))))
  6888. ;; At beginning of buffer or so high up that only a heading
  6889. ;; makes sense.
  6890. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6891. ((not respect-content)
  6892. (unless may-split (end-of-line))
  6893. (insert "\n* "))
  6894. ((re-search-forward org-outline-regexp-bol nil t)
  6895. (beginning-of-line)
  6896. (insert "* \n")
  6897. (backward-char))
  6898. (t (goto-char (point-max))
  6899. (insert "\n* ")))
  6900. (run-hooks 'org-insert-heading-hook))
  6901. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6902. (t
  6903. ;; Maybe move at the end of the subtree
  6904. (when (equal arg '(16))
  6905. (org-up-heading-safe)
  6906. (org-end-of-subtree t))
  6907. ;; Insert a heading
  6908. (save-restriction
  6909. (widen)
  6910. (let* ((level nil)
  6911. (on-heading (org-at-heading-p))
  6912. (empty-line-p (if on-heading
  6913. (org-previous-line-empty-p)
  6914. ;; We will decide later
  6915. nil))
  6916. ;; Get a level string to fall back on.
  6917. (fix-level
  6918. (if (org-before-first-heading-p) "*"
  6919. (save-excursion
  6920. (org-back-to-heading t)
  6921. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6922. (looking-at org-outline-regexp)
  6923. (make-string (1- (length (match-string 0))) ?*))))
  6924. (stars
  6925. (save-excursion
  6926. (condition-case nil
  6927. (if top-level "* "
  6928. (org-back-to-heading invisible-ok)
  6929. (when (and (not on-heading)
  6930. (featurep 'org-inlinetask)
  6931. (integerp org-inlinetask-min-level)
  6932. (>= (length (match-string 0))
  6933. org-inlinetask-min-level))
  6934. ;; Find a heading level before the inline
  6935. ;; task.
  6936. (while (and (setq level (org-up-heading-safe))
  6937. (>= level org-inlinetask-min-level)))
  6938. (if (org-at-heading-p)
  6939. (org-back-to-heading invisible-ok)
  6940. (error "This should not happen")))
  6941. (unless (and (save-excursion
  6942. (save-match-data
  6943. (org-backward-heading-same-level
  6944. 1 invisible-ok))
  6945. (= (point) (match-beginning 0)))
  6946. (not (org-previous-line-empty-p t)))
  6947. (setq empty-line-p (or empty-line-p
  6948. (org-previous-line-empty-p))))
  6949. (match-string 0))
  6950. (error (or fix-level "* ")))))
  6951. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6952. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6953. pos hide-previous previous-pos)
  6954. ;; If we insert after content, move there and clean up
  6955. ;; whitespace.
  6956. (when (and respect-content
  6957. (not (org-looking-at-p org-outline-regexp-bol)))
  6958. (if (not (org-before-first-heading-p))
  6959. (org-end-of-subtree nil t)
  6960. (re-search-forward org-outline-regexp-bol)
  6961. (beginning-of-line 0))
  6962. (skip-chars-backward " \r\t\n")
  6963. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6964. (looking-at "[ \t]+") (replace-match ""))
  6965. (unless (eobp) (forward-char 1))
  6966. (when (looking-at "^\\*")
  6967. (unless (bobp) (backward-char 1))
  6968. (insert "\n")))
  6969. ;; If we are splitting, grab the text that should be moved
  6970. ;; to the new headline.
  6971. (when may-split
  6972. (if (org-on-heading-p)
  6973. ;; This is a heading: split intelligently (keeping
  6974. ;; tags).
  6975. (let ((pos (point)))
  6976. (beginning-of-line)
  6977. (unless (looking-at org-complex-heading-regexp)
  6978. (error "This should not happen"))
  6979. (when (and (match-beginning 4)
  6980. (> pos (match-beginning 4))
  6981. (< pos (match-end 4)))
  6982. (setq initial-content (buffer-substring pos (match-end 4)))
  6983. (goto-char pos)
  6984. (delete-region (point) (match-end 4))
  6985. (if (looking-at "[ \t]*$")
  6986. (replace-match "")
  6987. (insert (make-string (length initial-content) ?\s)))
  6988. (setq initial-content (org-trim initial-content)))
  6989. (goto-char pos))
  6990. ;; A normal line.
  6991. (setq initial-content
  6992. (org-trim
  6993. (delete-and-extract-region (point) (line-end-position))))))
  6994. ;; If we are at the beginning of the line, insert before it.
  6995. ;; Otherwise, after it.
  6996. (cond
  6997. ((and (bolp) (looking-at "[ \t]*$")))
  6998. ((bolp) (save-excursion (insert "\n")))
  6999. (t (end-of-line)
  7000. (insert "\n")))
  7001. ;; Insert the new heading
  7002. (insert stars)
  7003. (just-one-space)
  7004. (insert initial-content)
  7005. (unless (and blank (org-previous-line-empty-p))
  7006. (org-N-empty-lines-before-current (if blank 1 0)))
  7007. ;; Adjust visibility, which may be messed up if we removed
  7008. ;; blank lines while previous entry was hidden.
  7009. (let ((bol (line-beginning-position)))
  7010. (dolist (o (overlays-at (1- bol)))
  7011. (when (and (eq (overlay-get o 'invisible) 'outline)
  7012. (eq (overlay-end o) bol))
  7013. (move-overlay o (overlay-start o) (1- bol)))))
  7014. (run-hooks 'org-insert-heading-hook)))))))
  7015. (defun org-N-empty-lines-before-current (N)
  7016. "Make the number of empty lines before current exactly N.
  7017. So this will delete or add empty lines."
  7018. (save-excursion
  7019. (beginning-of-line)
  7020. (let ((p (point)))
  7021. (skip-chars-backward " \r\t\n")
  7022. (unless (bolp) (forward-line))
  7023. (delete-region (point) p))
  7024. (when (> N 0) (insert (make-string N ?\n)))))
  7025. (defun org-get-heading (&optional no-tags no-todo)
  7026. "Return the heading of the current entry, without the stars.
  7027. When NO-TAGS is non-nil, don't include tags.
  7028. When NO-TODO is non-nil, don't include TODO keywords."
  7029. (save-excursion
  7030. (org-back-to-heading t)
  7031. (cond
  7032. ((and no-tags no-todo)
  7033. (looking-at org-complex-heading-regexp)
  7034. (match-string 4))
  7035. (no-tags
  7036. (looking-at (concat org-outline-regexp
  7037. "\\(.*?\\)"
  7038. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7039. (match-string 1))
  7040. (no-todo
  7041. (looking-at org-todo-line-regexp)
  7042. (match-string 3))
  7043. (t (looking-at org-heading-regexp)
  7044. (match-string 2)))))
  7045. (defvar orgstruct-mode) ; defined below
  7046. (defun org-heading-components ()
  7047. "Return the components of the current heading.
  7048. This is a list with the following elements:
  7049. - the level as an integer
  7050. - the reduced level, different if `org-odd-levels-only' is set.
  7051. - the TODO keyword, or nil
  7052. - the priority character, like ?A, or nil if no priority is given
  7053. - the headline text itself, or the tags string if no headline text
  7054. - the tags string, or nil."
  7055. (save-excursion
  7056. (org-back-to-heading t)
  7057. (if (let (case-fold-search)
  7058. (looking-at
  7059. (if orgstruct-mode
  7060. org-heading-regexp
  7061. org-complex-heading-regexp)))
  7062. (if orgstruct-mode
  7063. (list (length (match-string 1))
  7064. (org-reduced-level (length (match-string 1)))
  7065. nil
  7066. nil
  7067. (match-string 2)
  7068. nil)
  7069. (list (length (match-string 1))
  7070. (org-reduced-level (length (match-string 1)))
  7071. (org-match-string-no-properties 2)
  7072. (and (match-end 3) (aref (match-string 3) 2))
  7073. (org-match-string-no-properties 4)
  7074. (org-match-string-no-properties 5))))))
  7075. (defun org-get-entry ()
  7076. "Get the entry text, after heading, entire subtree."
  7077. (save-excursion
  7078. (org-back-to-heading t)
  7079. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7080. (defun org-insert-heading-after-current ()
  7081. "Insert a new heading with same level as current, after current subtree."
  7082. (interactive)
  7083. (org-back-to-heading)
  7084. (org-insert-heading)
  7085. (org-move-subtree-down)
  7086. (end-of-line 1))
  7087. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7088. "Insert heading with `org-insert-heading-respect-content' set to t."
  7089. (interactive)
  7090. (org-insert-heading '(4) invisible-ok))
  7091. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7092. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7093. (interactive)
  7094. (org-insert-todo-heading force-state '(4)))
  7095. (defun org-insert-todo-heading (arg &optional force-heading)
  7096. "Insert a new heading with the same level and TODO state as current heading.
  7097. If the heading has no TODO state, or if the state is DONE, use the first
  7098. state (TODO by default). Also with one prefix arg, force first state. With
  7099. two prefix args, force inserting at the end of the parent subtree."
  7100. (interactive "P")
  7101. (when (or force-heading (not (org-insert-item 'checkbox)))
  7102. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7103. force-heading))
  7104. (save-excursion
  7105. (org-back-to-heading)
  7106. (outline-previous-heading)
  7107. (looking-at org-todo-line-regexp))
  7108. (let*
  7109. ((new-mark-x
  7110. (if (or (equal arg '(4))
  7111. (not (match-beginning 2))
  7112. (member (match-string 2) org-done-keywords))
  7113. (car org-todo-keywords-1)
  7114. (match-string 2)))
  7115. (new-mark
  7116. (or
  7117. (run-hook-with-args-until-success
  7118. 'org-todo-get-default-hook new-mark-x nil)
  7119. new-mark-x)))
  7120. (beginning-of-line 1)
  7121. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7122. (if org-treat-insert-todo-heading-as-state-change
  7123. (org-todo new-mark)
  7124. (insert new-mark " "))))
  7125. (when org-provide-todo-statistics
  7126. (org-update-parent-todo-statistics))))
  7127. (defun org-insert-subheading (arg)
  7128. "Insert a new subheading and demote it.
  7129. Works for outline headings and for plain lists alike."
  7130. (interactive "P")
  7131. (org-insert-heading arg)
  7132. (cond
  7133. ((org-at-heading-p) (org-do-demote))
  7134. ((org-at-item-p) (org-indent-item))))
  7135. (defun org-insert-todo-subheading (arg)
  7136. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7137. Works for outline headings and for plain lists alike."
  7138. (interactive "P")
  7139. (org-insert-todo-heading arg)
  7140. (cond
  7141. ((org-at-heading-p) (org-do-demote))
  7142. ((org-at-item-p) (org-indent-item))))
  7143. ;;; Promotion and Demotion
  7144. (defvar org-after-demote-entry-hook nil
  7145. "Hook run after an entry has been demoted.
  7146. The cursor will be at the beginning of the entry.
  7147. When a subtree is being demoted, the hook will be called for each node.")
  7148. (defvar org-after-promote-entry-hook nil
  7149. "Hook run after an entry has been promoted.
  7150. The cursor will be at the beginning of the entry.
  7151. When a subtree is being promoted, the hook will be called for each node.")
  7152. (defun org-promote-subtree ()
  7153. "Promote the entire subtree.
  7154. See also `org-promote'."
  7155. (interactive)
  7156. (save-excursion
  7157. (org-with-limited-levels (org-map-tree 'org-promote)))
  7158. (org-fix-position-after-promote))
  7159. (defun org-demote-subtree ()
  7160. "Demote the entire subtree. See `org-demote'.
  7161. See also `org-promote'."
  7162. (interactive)
  7163. (save-excursion
  7164. (org-with-limited-levels (org-map-tree 'org-demote)))
  7165. (org-fix-position-after-promote))
  7166. (defun org-do-promote ()
  7167. "Promote the current heading higher up the tree.
  7168. If the region is active in `transient-mark-mode', promote all headings
  7169. in the region."
  7170. (interactive)
  7171. (save-excursion
  7172. (if (org-region-active-p)
  7173. (org-map-region 'org-promote (region-beginning) (region-end))
  7174. (org-promote)))
  7175. (org-fix-position-after-promote))
  7176. (defun org-do-demote ()
  7177. "Demote the current heading lower down the tree.
  7178. If the region is active in `transient-mark-mode', demote all headings
  7179. in the region."
  7180. (interactive)
  7181. (save-excursion
  7182. (if (org-region-active-p)
  7183. (org-map-region 'org-demote (region-beginning) (region-end))
  7184. (org-demote)))
  7185. (org-fix-position-after-promote))
  7186. (defun org-fix-position-after-promote ()
  7187. "Make sure that after pro/demotion cursor position is right."
  7188. (let ((pos (point)))
  7189. (when (save-excursion
  7190. (beginning-of-line 1)
  7191. (looking-at org-todo-line-regexp)
  7192. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7193. (cond ((eobp) (insert " "))
  7194. ((eolp) (insert " "))
  7195. ((equal (char-after) ?\ ) (forward-char 1))))))
  7196. (defun org-current-level ()
  7197. "Return the level of the current entry, or nil if before the first headline.
  7198. The level is the number of stars at the beginning of the
  7199. headline. Use `org-reduced-level' to remove the effect of
  7200. `org-odd-levels'. Unlike to `org-outline-level', this function
  7201. ignores inlinetasks."
  7202. (let ((level (org-with-limited-levels (org-outline-level))))
  7203. (and (> level 0) level)))
  7204. (defun org-get-previous-line-level ()
  7205. "Return the outline depth of the last headline before the current line.
  7206. Returns 0 for the first headline in the buffer, and nil if before the
  7207. first headline."
  7208. (and (org-current-level)
  7209. (or (and (/= (line-beginning-position) (point-min))
  7210. (save-excursion (beginning-of-line 0) (org-current-level)))
  7211. 0)))
  7212. (defun org-reduced-level (l)
  7213. "Compute the effective level of a heading.
  7214. This takes into account the setting of `org-odd-levels-only'."
  7215. (cond
  7216. ((zerop l) 0)
  7217. (org-odd-levels-only (1+ (floor (/ l 2))))
  7218. (t l)))
  7219. (defun org-level-increment ()
  7220. "Return the number of stars that will be added or removed at a
  7221. time to headlines when structure editing, based on the value of
  7222. `org-odd-levels-only'."
  7223. (if org-odd-levels-only 2 1))
  7224. (defun org-get-valid-level (level &optional change)
  7225. "Rectify a level change under the influence of `org-odd-levels-only'
  7226. LEVEL is a current level, CHANGE is by how much the level should be
  7227. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7228. even level numbers will become the next higher odd number."
  7229. (if org-odd-levels-only
  7230. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7231. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7232. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7233. (max 1 (+ level (or change 0)))))
  7234. (if (boundp 'define-obsolete-function-alias)
  7235. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7236. (define-obsolete-function-alias 'org-get-legal-level
  7237. 'org-get-valid-level)
  7238. (define-obsolete-function-alias 'org-get-legal-level
  7239. 'org-get-valid-level "23.1")))
  7240. (defun org-promote ()
  7241. "Promote the current heading higher up the tree."
  7242. (org-with-wide-buffer
  7243. (org-back-to-heading t)
  7244. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7245. after-change-functions))
  7246. (level (save-match-data (funcall outline-level)))
  7247. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7248. (diff (abs (- level (length up-head) -1))))
  7249. (cond
  7250. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7251. (replace-match "# " nil t))
  7252. ((= level 1)
  7253. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7254. (t (replace-match up-head nil t)))
  7255. (unless (= level 1)
  7256. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7257. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7258. (run-hooks 'org-after-promote-entry-hook))))
  7259. (defun org-demote ()
  7260. "Demote the current heading lower down the tree."
  7261. (org-with-wide-buffer
  7262. (org-back-to-heading t)
  7263. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7264. after-change-functions))
  7265. (level (save-match-data (funcall outline-level)))
  7266. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7267. (diff (abs (- level (length down-head) -1))))
  7268. (replace-match down-head nil t)
  7269. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7270. (when org-adapt-indentation (org-fixup-indentation diff))
  7271. (run-hooks 'org-after-demote-entry-hook))))
  7272. (defun org-cycle-level ()
  7273. "Cycle the level of an empty headline through possible states.
  7274. This goes first to child, then to parent, level, then up the hierarchy.
  7275. After top level, it switches back to sibling level."
  7276. (interactive)
  7277. (let ((org-adapt-indentation nil))
  7278. (when (org-point-at-end-of-empty-headline)
  7279. (setq this-command 'org-cycle-level) ; Only needed for caching
  7280. (let ((cur-level (org-current-level))
  7281. (prev-level (org-get-previous-line-level)))
  7282. (cond
  7283. ;; If first headline in file, promote to top-level.
  7284. ((= prev-level 0)
  7285. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7286. do (org-do-promote)))
  7287. ;; If same level as prev, demote one.
  7288. ((= prev-level cur-level)
  7289. (org-do-demote))
  7290. ;; If parent is top-level, promote to top level if not already.
  7291. ((= prev-level 1)
  7292. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7293. do (org-do-promote)))
  7294. ;; If top-level, return to prev-level.
  7295. ((= cur-level 1)
  7296. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7297. do (org-do-demote)))
  7298. ;; If less than prev-level, promote one.
  7299. ((< cur-level prev-level)
  7300. (org-do-promote))
  7301. ;; If deeper than prev-level, promote until higher than
  7302. ;; prev-level.
  7303. ((> cur-level prev-level)
  7304. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7305. do (org-do-promote))))
  7306. t))))
  7307. (defun org-map-tree (fun)
  7308. "Call FUN for every heading underneath the current one."
  7309. (org-back-to-heading)
  7310. (let ((level (funcall outline-level)))
  7311. (save-excursion
  7312. (funcall fun)
  7313. (while (and (progn
  7314. (outline-next-heading)
  7315. (> (funcall outline-level) level))
  7316. (not (eobp)))
  7317. (funcall fun)))))
  7318. (defun org-map-region (fun beg end)
  7319. "Call FUN for every heading between BEG and END."
  7320. (let ((org-ignore-region t))
  7321. (save-excursion
  7322. (setq end (copy-marker end))
  7323. (goto-char beg)
  7324. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7325. (< (point) end))
  7326. (funcall fun))
  7327. (while (and (progn
  7328. (outline-next-heading)
  7329. (< (point) end))
  7330. (not (eobp)))
  7331. (funcall fun)))))
  7332. (defun org-fixup-indentation (diff)
  7333. "Change the indentation in the current entry by DIFF.
  7334. DIFF is an integer. Indentation is done according to the
  7335. following rules:
  7336. - Planning information and property drawers are always indented
  7337. according to the new level of the headline;
  7338. - Footnote definitions and their contents are ignored;
  7339. - Inlinetasks' boundaries are not shifted;
  7340. - Empty lines are ignored;
  7341. - Other lines' indentation are shifted by DIFF columns, unless
  7342. it would introduce a structural change in the document, in
  7343. which case no shifting is done at all.
  7344. Assume point is at a heading or an inlinetask beginning."
  7345. (org-with-wide-buffer
  7346. (narrow-to-region (line-beginning-position)
  7347. (save-excursion
  7348. (if (org-with-limited-levels (org-at-heading-p))
  7349. (org-with-limited-levels (outline-next-heading))
  7350. (org-inlinetask-goto-end))
  7351. (point)))
  7352. (forward-line)
  7353. ;; Indent properly planning info and property drawer.
  7354. (when (org-looking-at-p org-planning-line-re)
  7355. (org-indent-line)
  7356. (forward-line))
  7357. (when (looking-at org-property-drawer-re)
  7358. (goto-char (match-end 0))
  7359. (forward-line)
  7360. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7361. (catch 'no-shift
  7362. (when (zerop diff) (throw 'no-shift nil))
  7363. ;; If DIFF is negative, first check if a shift is possible at all
  7364. ;; (e.g., it doesn't break structure). This can only happen if
  7365. ;; some contents are not properly indented.
  7366. (let ((case-fold-search t))
  7367. (when (< diff 0)
  7368. (let ((diff (- diff))
  7369. (forbidden-re (concat org-outline-regexp
  7370. "\\|"
  7371. (substring org-footnote-definition-re 1))))
  7372. (save-excursion
  7373. (while (not (eobp))
  7374. (cond
  7375. ((org-looking-at-p "[ \t]*$") (forward-line))
  7376. ((and (org-looking-at-p org-footnote-definition-re)
  7377. (let ((e (org-element-at-point)))
  7378. (and (eq (org-element-type e) 'footnote-definition)
  7379. (goto-char (org-element-property :end e))))))
  7380. ((org-looking-at-p org-outline-regexp) (forward-line))
  7381. ;; Give up if shifting would move before column 0 or
  7382. ;; if it would introduce a headline or a footnote
  7383. ;; definition.
  7384. (t
  7385. (skip-chars-forward " \t")
  7386. (let ((ind (current-column)))
  7387. (when (or (< ind diff)
  7388. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7389. (throw 'no-shift nil)))
  7390. ;; Ignore contents of example blocks and source
  7391. ;; blocks if their indentation is meant to be
  7392. ;; preserved. Jump to block's closing line.
  7393. (beginning-of-line)
  7394. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7395. (let ((e (org-element-at-point)))
  7396. (and (memq (org-element-type e)
  7397. '(example-block src-block))
  7398. (or org-src-preserve-indentation
  7399. (org-element-property :preserve-indent e))
  7400. (goto-char (org-element-property :end e))
  7401. (progn (skip-chars-backward " \r\t\n")
  7402. (beginning-of-line)
  7403. t))))
  7404. (forward-line))))))))
  7405. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7406. ;; by DIFF. Also skip contents of source or example blocks
  7407. ;; when indentation is meant to be preserved.
  7408. (while (not (eobp))
  7409. (cond
  7410. ((and (org-looking-at-p org-footnote-definition-re)
  7411. (let ((e (org-element-at-point)))
  7412. (and (eq (org-element-type e) 'footnote-definition)
  7413. (goto-char (org-element-property :end e))))))
  7414. ((org-looking-at-p org-outline-regexp) (forward-line))
  7415. ((org-looking-at-p "[ \t]*$") (forward-line))
  7416. (t
  7417. (org-indent-line-to (+ (org-get-indentation) diff))
  7418. (beginning-of-line)
  7419. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7420. (let ((e (org-element-at-point)))
  7421. (and (memq (org-element-type e)
  7422. '(example-block src-block))
  7423. (or org-src-preserve-indentation
  7424. (org-element-property :preserve-indent e))
  7425. (goto-char (org-element-property :end e))
  7426. (progn (skip-chars-backward " \r\t\n")
  7427. (beginning-of-line)
  7428. t))))
  7429. (forward-line)))))))))
  7430. (defun org-convert-to-odd-levels ()
  7431. "Convert an org-mode file with all levels allowed to one with odd levels.
  7432. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7433. level 5 etc."
  7434. (interactive)
  7435. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7436. (let ((outline-level 'org-outline-level)
  7437. (org-odd-levels-only nil) n)
  7438. (save-excursion
  7439. (goto-char (point-min))
  7440. (while (re-search-forward "^\\*\\*+ " nil t)
  7441. (setq n (- (length (match-string 0)) 2))
  7442. (while (>= (setq n (1- n)) 0)
  7443. (org-demote))
  7444. (end-of-line 1))))))
  7445. (defun org-convert-to-oddeven-levels ()
  7446. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7447. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7448. file contains a section with an even level, conversion would
  7449. destroy the structure of the file. An error is signaled in this
  7450. case."
  7451. (interactive)
  7452. (goto-char (point-min))
  7453. ;; First check if there are no even levels
  7454. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7455. (org-show-set-visibility 'canonical)
  7456. (error "Not all levels are odd in this file. Conversion not possible"))
  7457. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7458. (let ((outline-regexp org-outline-regexp)
  7459. (outline-level 'org-outline-level)
  7460. (org-odd-levels-only nil) n)
  7461. (save-excursion
  7462. (goto-char (point-min))
  7463. (while (re-search-forward "^\\*\\*+ " nil t)
  7464. (setq n (/ (1- (length (match-string 0))) 2))
  7465. (while (>= (setq n (1- n)) 0)
  7466. (org-promote))
  7467. (end-of-line 1))))))
  7468. (defun org-tr-level (n)
  7469. "Make N odd if required."
  7470. (if org-odd-levels-only (1+ (/ n 2)) n))
  7471. ;;; Vertical tree motion, cutting and pasting of subtrees
  7472. (defun org-move-subtree-up (&optional arg)
  7473. "Move the current subtree up past ARG headlines of the same level."
  7474. (interactive "p")
  7475. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7476. (defun org-move-subtree-down (&optional arg)
  7477. "Move the current subtree down past ARG headlines of the same level."
  7478. (interactive "p")
  7479. (setq arg (prefix-numeric-value arg))
  7480. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7481. 'org-get-last-sibling))
  7482. (ins-point (make-marker))
  7483. (cnt (abs arg))
  7484. (col (current-column))
  7485. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7486. ;; Select the tree
  7487. (org-back-to-heading)
  7488. (setq beg0 (point))
  7489. (save-excursion
  7490. (setq ne-beg (org-back-over-empty-lines))
  7491. (setq beg (point)))
  7492. (save-match-data
  7493. (save-excursion (outline-end-of-heading)
  7494. (setq folded (outline-invisible-p)))
  7495. (progn (org-end-of-subtree nil t)
  7496. (unless (eobp) (backward-char))))
  7497. (outline-next-heading)
  7498. (setq ne-end (org-back-over-empty-lines))
  7499. (setq end (point))
  7500. (goto-char beg0)
  7501. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7502. ;; include less whitespace
  7503. (save-excursion
  7504. (goto-char beg)
  7505. (forward-line (- ne-beg ne-end))
  7506. (setq beg (point))))
  7507. ;; Find insertion point, with error handling
  7508. (while (> cnt 0)
  7509. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7510. (progn (goto-char beg0)
  7511. (user-error "Cannot move past superior level or buffer limit")))
  7512. (setq cnt (1- cnt)))
  7513. (if (> arg 0)
  7514. ;; Moving forward - still need to move over subtree
  7515. (progn (org-end-of-subtree t t)
  7516. (save-excursion
  7517. (org-back-over-empty-lines)
  7518. (or (bolp) (newline)))))
  7519. (setq ne-ins (org-back-over-empty-lines))
  7520. (move-marker ins-point (point))
  7521. (setq txt (buffer-substring beg end))
  7522. (org-save-markers-in-region beg end)
  7523. (delete-region beg end)
  7524. (org-remove-empty-overlays-at beg)
  7525. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7526. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7527. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7528. (let ((bbb (point)))
  7529. (insert-before-markers txt)
  7530. (org-reinstall-markers-in-region bbb)
  7531. (move-marker ins-point bbb))
  7532. (or (bolp) (insert "\n"))
  7533. (setq ins-end (point))
  7534. (goto-char ins-point)
  7535. (org-skip-whitespace)
  7536. (when (and (< arg 0)
  7537. (org-first-sibling-p)
  7538. (> ne-ins ne-beg))
  7539. ;; Move whitespace back to beginning
  7540. (save-excursion
  7541. (goto-char ins-end)
  7542. (let ((kill-whole-line t))
  7543. (kill-line (- ne-ins ne-beg)) (point)))
  7544. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7545. (move-marker ins-point nil)
  7546. (if folded
  7547. (hide-subtree)
  7548. (org-show-entry)
  7549. (show-children)
  7550. (org-cycle-hide-drawers 'children))
  7551. (org-clean-visibility-after-subtree-move)
  7552. ;; move back to the initial column we were at
  7553. (move-to-column col)))
  7554. (defvar org-subtree-clip ""
  7555. "Clipboard for cut and paste of subtrees.
  7556. This is actually only a copy of the kill, because we use the normal kill
  7557. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7558. (defvar org-subtree-clip-folded nil
  7559. "Was the last copied subtree folded?
  7560. This is used to fold the tree back after pasting.")
  7561. (defun org-cut-subtree (&optional n)
  7562. "Cut the current subtree into the clipboard.
  7563. With prefix arg N, cut this many sequential subtrees.
  7564. This is a short-hand for marking the subtree and then cutting it."
  7565. (interactive "p")
  7566. (org-copy-subtree n 'cut))
  7567. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7568. "Copy the current subtree it in the clipboard.
  7569. With prefix arg N, copy this many sequential subtrees.
  7570. This is a short-hand for marking the subtree and then copying it.
  7571. If CUT is non-nil, actually cut the subtree.
  7572. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7573. of some markers in the region, even if CUT is non-nil. This is
  7574. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7575. (interactive "p")
  7576. (let (beg end folded (beg0 (point)))
  7577. (if (org-called-interactively-p 'any)
  7578. (org-back-to-heading nil) ; take what looks like a subtree
  7579. (org-back-to-heading t)) ; take what is really there
  7580. (setq beg (point))
  7581. (skip-chars-forward " \t\r\n")
  7582. (save-match-data
  7583. (if nosubtrees
  7584. (outline-next-heading)
  7585. (save-excursion (outline-end-of-heading)
  7586. (setq folded (outline-invisible-p)))
  7587. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7588. (org-end-of-subtree t t)))
  7589. ;; Include the end of an inlinetask
  7590. (when (and (featurep 'org-inlinetask)
  7591. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7592. "END[ \t]*$")))
  7593. (end-of-line))
  7594. (setq end (point))
  7595. (goto-char beg0)
  7596. (when (> end beg)
  7597. (setq org-subtree-clip-folded folded)
  7598. (when (or cut force-store-markers)
  7599. (org-save-markers-in-region beg end))
  7600. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7601. (setq org-subtree-clip (current-kill 0))
  7602. (message "%s: Subtree(s) with %d characters"
  7603. (if cut "Cut" "Copied")
  7604. (length org-subtree-clip)))))
  7605. (defun org-paste-subtree (&optional level tree for-yank remove)
  7606. "Paste the clipboard as a subtree, with modification of headline level.
  7607. The entire subtree is promoted or demoted in order to match a new headline
  7608. level.
  7609. If the cursor is at the beginning of a headline, the same level as
  7610. that headline is used to paste the tree.
  7611. If not, the new level is derived from the *visible* headings
  7612. before and after the insertion point, and taken to be the inferior headline
  7613. level of the two. So if the previous visible heading is level 3 and the
  7614. next is level 4 (or vice versa), level 4 will be used for insertion.
  7615. This makes sure that the subtree remains an independent subtree and does
  7616. not swallow low level entries.
  7617. You can also force a different level, either by using a numeric prefix
  7618. argument, or by inserting the heading marker by hand. For example, if the
  7619. cursor is after \"*****\", then the tree will be shifted to level 5.
  7620. If optional TREE is given, use this text instead of the kill ring.
  7621. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7622. move back over whitespace before inserting, and move point to the end of
  7623. the inserted text when done.
  7624. When REMOVE is non-nil, remove the subtree from the clipboard."
  7625. (interactive "P")
  7626. (setq tree (or tree (and kill-ring (current-kill 0))))
  7627. (unless (org-kill-is-subtree-p tree)
  7628. (user-error "%s"
  7629. (substitute-command-keys
  7630. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7631. (org-with-limited-levels
  7632. (let* ((visp (not (outline-invisible-p)))
  7633. (txt tree)
  7634. (^re_ "\\(\\*+\\)[ \t]*")
  7635. (old-level (if (string-match org-outline-regexp-bol txt)
  7636. (- (match-end 0) (match-beginning 0) 1)
  7637. -1))
  7638. (force-level (cond (level (prefix-numeric-value level))
  7639. ((and (looking-at "[ \t]*$")
  7640. (string-match
  7641. "^\\*+$" (buffer-substring
  7642. (point-at-bol) (point))))
  7643. (- (match-end 0) (match-beginning 0)))
  7644. ((and (bolp)
  7645. (looking-at org-outline-regexp))
  7646. (- (match-end 0) (point) 1))))
  7647. (previous-level (save-excursion
  7648. (condition-case nil
  7649. (progn
  7650. (outline-previous-visible-heading 1)
  7651. (if (looking-at ^re_)
  7652. (- (match-end 0) (match-beginning 0) 1)
  7653. 1))
  7654. (error 1))))
  7655. (next-level (save-excursion
  7656. (condition-case nil
  7657. (progn
  7658. (or (looking-at org-outline-regexp)
  7659. (outline-next-visible-heading 1))
  7660. (if (looking-at ^re_)
  7661. (- (match-end 0) (match-beginning 0) 1)
  7662. 1))
  7663. (error 1))))
  7664. (new-level (or force-level (max previous-level next-level)))
  7665. (shift (if (or (= old-level -1)
  7666. (= new-level -1)
  7667. (= old-level new-level))
  7668. 0
  7669. (- new-level old-level)))
  7670. (delta (if (> shift 0) -1 1))
  7671. (func (if (> shift 0) 'org-demote 'org-promote))
  7672. (org-odd-levels-only nil)
  7673. beg end newend)
  7674. ;; Remove the forced level indicator
  7675. (if force-level
  7676. (delete-region (point-at-bol) (point)))
  7677. ;; Paste
  7678. (beginning-of-line (if (bolp) 1 2))
  7679. (setq beg (point))
  7680. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7681. (insert-before-markers txt)
  7682. (unless (string-match "\n\\'" txt) (insert "\n"))
  7683. (setq newend (point))
  7684. (org-reinstall-markers-in-region beg)
  7685. (setq end (point))
  7686. (goto-char beg)
  7687. (skip-chars-forward " \t\n\r")
  7688. (setq beg (point))
  7689. (if (and (outline-invisible-p) visp)
  7690. (save-excursion (outline-show-heading)))
  7691. ;; Shift if necessary
  7692. (unless (= shift 0)
  7693. (save-restriction
  7694. (narrow-to-region beg end)
  7695. (while (not (= shift 0))
  7696. (org-map-region func (point-min) (point-max))
  7697. (setq shift (+ delta shift)))
  7698. (goto-char (point-min))
  7699. (setq newend (point-max))))
  7700. (when (or (org-called-interactively-p 'interactive) for-yank)
  7701. (message "Clipboard pasted as level %d subtree" new-level))
  7702. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7703. kill-ring
  7704. (eq org-subtree-clip (current-kill 0))
  7705. org-subtree-clip-folded)
  7706. ;; The tree was folded before it was killed/copied
  7707. (hide-subtree))
  7708. (and for-yank (goto-char newend))
  7709. (and remove (setq kill-ring (cdr kill-ring))))))
  7710. (defun org-kill-is-subtree-p (&optional txt)
  7711. "Check if the current kill is an outline subtree, or a set of trees.
  7712. Returns nil if kill does not start with a headline, or if the first
  7713. headline level is not the largest headline level in the tree.
  7714. So this will actually accept several entries of equal levels as well,
  7715. which is OK for `org-paste-subtree'.
  7716. If optional TXT is given, check this string instead of the current kill."
  7717. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7718. (re (org-get-limited-outline-regexp))
  7719. (^re (concat "^" re))
  7720. (start-level (and kill
  7721. (string-match
  7722. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7723. kill)
  7724. (- (match-end 2) (match-beginning 2) 1)))
  7725. (start (1+ (or (match-beginning 2) -1))))
  7726. (if (not start-level)
  7727. (progn
  7728. nil) ;; does not even start with a heading
  7729. (catch 'exit
  7730. (while (setq start (string-match ^re kill (1+ start)))
  7731. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7732. (throw 'exit nil)))
  7733. t))))
  7734. (defvar org-markers-to-move nil
  7735. "Markers that should be moved with a cut-and-paste operation.
  7736. Those markers are stored together with their positions relative to
  7737. the start of the region.")
  7738. (defun org-save-markers-in-region (beg end)
  7739. "Check markers in region.
  7740. If these markers are between BEG and END, record their position relative
  7741. to BEG, so that after moving the block of text, we can put the markers back
  7742. into place.
  7743. This function gets called just before an entry or tree gets cut from the
  7744. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7745. called immediately, to move the markers with the entries."
  7746. (setq org-markers-to-move nil)
  7747. (when (featurep 'org-clock)
  7748. (org-clock-save-markers-for-cut-and-paste beg end))
  7749. (when (featurep 'org-agenda)
  7750. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7751. (defun org-check-and-save-marker (marker beg end)
  7752. "Check if MARKER is between BEG and END.
  7753. If yes, remember the marker and the distance to BEG."
  7754. (when (and (marker-buffer marker)
  7755. (equal (marker-buffer marker) (current-buffer)))
  7756. (if (and (>= marker beg) (< marker end))
  7757. (push (cons marker (- marker beg)) org-markers-to-move))))
  7758. (defun org-reinstall-markers-in-region (beg)
  7759. "Move all remembered markers to their position relative to BEG."
  7760. (mapc (lambda (x)
  7761. (move-marker (car x) (+ beg (cdr x))))
  7762. org-markers-to-move)
  7763. (setq org-markers-to-move nil))
  7764. (defun org-narrow-to-subtree ()
  7765. "Narrow buffer to the current subtree."
  7766. (interactive)
  7767. (save-excursion
  7768. (save-match-data
  7769. (org-with-limited-levels
  7770. (narrow-to-region
  7771. (progn (org-back-to-heading t) (point))
  7772. (progn (org-end-of-subtree t t)
  7773. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7774. (point)))))))
  7775. (defun org-narrow-to-block ()
  7776. "Narrow buffer to the current block."
  7777. (interactive)
  7778. (let* ((case-fold-search t)
  7779. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7780. "^[ \t]*#\\+end_.*")))
  7781. (if blockp
  7782. (narrow-to-region (car blockp) (cdr blockp))
  7783. (user-error "Not in a block"))))
  7784. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7785. "Clone the task (subtree) at point N times.
  7786. The clones will be inserted as siblings.
  7787. In interactive use, the user will be prompted for the number of
  7788. clones to be produced. If the entry has a timestamp, the user
  7789. will also be prompted for a time shift, which may be a repeater
  7790. as used in time stamps, for example `+3d'. To disable this,
  7791. you can call the function with a universal prefix argument.
  7792. When a valid repeater is given and the entry contains any time
  7793. stamps, the clones will become a sequence in time, with time
  7794. stamps in the subtree shifted for each clone produced. If SHIFT
  7795. is nil or the empty string, time stamps will be left alone. The
  7796. ID property of the original subtree is removed.
  7797. If the original subtree did contain time stamps with a repeater,
  7798. the following will happen:
  7799. - the repeater will be removed in each clone
  7800. - an additional clone will be produced, with the current, unshifted
  7801. date(s) in the entry.
  7802. - the original entry will be placed *after* all the clones, with
  7803. repeater intact.
  7804. - the start days in the repeater in the original entry will be shifted
  7805. to past the last clone.
  7806. In this way you can spell out a number of instances of a repeating task,
  7807. and still retain the repeater to cover future instances of the task.
  7808. As described above, N+1 clones are produced when the original
  7809. subtree has a repeater. Setting N to 0, then, can be used to
  7810. remove the repeater from a subtree and create a shifted clone
  7811. with the original repeater."
  7812. (interactive "nNumber of clones to produce: ")
  7813. (let ((shift
  7814. (or shift
  7815. (if (and (not (equal current-prefix-arg '(4)))
  7816. (save-excursion
  7817. (re-search-forward org-ts-regexp-both
  7818. (save-excursion
  7819. (org-end-of-subtree t)
  7820. (point)) t)))
  7821. (read-from-minibuffer
  7822. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7823. ""))) ;; No time shift
  7824. (n-no-remove -1)
  7825. (drawer-re org-drawer-regexp)
  7826. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7827. beg end template task idprop
  7828. shift-n shift-what doshift nmin nmax)
  7829. (unless (wholenump n)
  7830. (user-error "Invalid number of replications %s" n))
  7831. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7832. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7833. shift)))
  7834. (user-error "Invalid shift specification %s" shift))
  7835. (when doshift
  7836. (setq shift-n (string-to-number (match-string 1 shift))
  7837. shift-what (cdr (assoc (match-string 2 shift)
  7838. '(("d" . day) ("w" . week)
  7839. ("m" . month) ("y" . year))))))
  7840. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7841. (setq nmin 1 nmax n)
  7842. (org-back-to-heading t)
  7843. (setq beg (point))
  7844. (setq idprop (org-entry-get nil "ID"))
  7845. (org-end-of-subtree t t)
  7846. (or (bolp) (insert "\n"))
  7847. (setq end (point))
  7848. (setq template (buffer-substring beg end))
  7849. (when (and doshift
  7850. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7851. (delete-region beg end)
  7852. (setq end beg)
  7853. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7854. (goto-char end)
  7855. (loop for n from nmin to nmax do
  7856. ;; prepare clone
  7857. (with-temp-buffer
  7858. (insert template)
  7859. (org-mode)
  7860. (goto-char (point-min))
  7861. (org-show-subtree)
  7862. (and idprop (if org-clone-delete-id
  7863. (org-entry-delete nil "ID")
  7864. (org-id-get-create t)))
  7865. (unless (= n 0)
  7866. (while (re-search-forward org-clock-re nil t)
  7867. (kill-whole-line))
  7868. (goto-char (point-min))
  7869. (while (re-search-forward drawer-re nil t)
  7870. (org-remove-empty-drawer-at (point))))
  7871. (goto-char (point-min))
  7872. (when doshift
  7873. (while (re-search-forward org-ts-regexp-both nil t)
  7874. (org-timestamp-change (* n shift-n) shift-what))
  7875. (unless (= n n-no-remove)
  7876. (goto-char (point-min))
  7877. (while (re-search-forward org-ts-regexp nil t)
  7878. (save-excursion
  7879. (goto-char (match-beginning 0))
  7880. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7881. (delete-region (match-beginning 1) (match-end 1)))))))
  7882. (setq task (buffer-string)))
  7883. (insert task))
  7884. (goto-char beg)))
  7885. ;;; Outline Sorting
  7886. (defun org-sort (with-case)
  7887. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7888. Optional argument WITH-CASE means sort case-sensitively."
  7889. (interactive "P")
  7890. (cond
  7891. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7892. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7893. (t
  7894. (org-call-with-arg 'org-sort-entries with-case))))
  7895. (defun org-sort-remove-invisible (s)
  7896. "Remove invisible links from string S."
  7897. (remove-text-properties 0 (length s) org-rm-props s)
  7898. (while (string-match org-bracket-link-regexp s)
  7899. (setq s (replace-match (if (match-end 2)
  7900. (match-string 3 s)
  7901. (match-string 1 s))
  7902. t t s)))
  7903. (let ((st (format " %s " s)))
  7904. (while (string-match org-emph-re st)
  7905. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7906. (setq s (substring st 1 -1)))
  7907. s)
  7908. (defvar org-priority-regexp) ; defined later in the file
  7909. (defvar org-after-sorting-entries-or-items-hook nil
  7910. "Hook that is run after a bunch of entries or items have been sorted.
  7911. When children are sorted, the cursor is in the parent line when this
  7912. hook gets called. When a region or a plain list is sorted, the cursor
  7913. will be in the first entry of the sorted region/list.")
  7914. (defun org-sort-entries
  7915. (&optional with-case sorting-type getkey-func compare-func property)
  7916. "Sort entries on a certain level of an outline tree.
  7917. If there is an active region, the entries in the region are sorted.
  7918. Else, if the cursor is before the first entry, sort the top-level items.
  7919. Else, the children of the entry at point are sorted.
  7920. Sorting can be alphabetically, numerically, by date/time as given by
  7921. a time stamp, by a property, by priority order, or by a custom function.
  7922. The command prompts for the sorting type unless it has been given to the
  7923. function through the SORTING-TYPE argument, which needs to be a character,
  7924. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7925. the precise meaning of each character:
  7926. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7927. c By creation time, which is assumed to be the first inactive time stamp
  7928. at the beginning of a line.
  7929. d By deadline date/time.
  7930. k By clocking time.
  7931. n Numerically, by converting the beginning of the entry/item to a number.
  7932. o By order of TODO keywords.
  7933. p By priority according to the cookie.
  7934. r By the value of a property.
  7935. s By scheduled date/time.
  7936. t By date/time, either the first active time stamp in the entry, or, if
  7937. none exist, by the first inactive one.
  7938. Capital letters will reverse the sort order.
  7939. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7940. called with point at the beginning of the record. It must return either
  7941. a string or a number that should serve as the sorting key for that record.
  7942. Comparing entries ignores case by default. However, with an optional argument
  7943. WITH-CASE, the sorting considers case as well.
  7944. Sorting is done against the visible part of the headlines, it ignores hidden
  7945. links.
  7946. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7947. (interactive "P")
  7948. (let ((case-func (if with-case 'identity 'downcase))
  7949. (cmstr
  7950. ;; The clock marker is lost when using `sort-subr', let's
  7951. ;; store the clocking string.
  7952. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7953. (save-excursion
  7954. (goto-char org-clock-marker)
  7955. (buffer-substring-no-properties (line-beginning-position)
  7956. (point)))))
  7957. start beg end stars re re2
  7958. txt what tmp)
  7959. ;; Find beginning and end of region to sort
  7960. (cond
  7961. ((org-region-active-p)
  7962. ;; we will sort the region
  7963. (setq end (region-end)
  7964. what "region")
  7965. (goto-char (region-beginning))
  7966. (if (not (org-at-heading-p)) (outline-next-heading))
  7967. (setq start (point)))
  7968. ((or (org-at-heading-p)
  7969. (ignore-errors (progn (org-back-to-heading) t)))
  7970. ;; we will sort the children of the current headline
  7971. (org-back-to-heading)
  7972. (setq start (point)
  7973. end (progn (org-end-of-subtree t t)
  7974. (or (bolp) (insert "\n"))
  7975. (when (>= (org-back-over-empty-lines) 1)
  7976. (forward-line 1))
  7977. (point))
  7978. what "children")
  7979. (goto-char start)
  7980. (show-subtree)
  7981. (outline-next-heading))
  7982. (t
  7983. ;; we will sort the top-level entries in this file
  7984. (goto-char (point-min))
  7985. (or (org-at-heading-p) (outline-next-heading))
  7986. (setq start (point))
  7987. (goto-char (point-max))
  7988. (beginning-of-line 1)
  7989. (when (looking-at ".*?\\S-")
  7990. ;; File ends in a non-white line
  7991. (end-of-line 1)
  7992. (insert "\n"))
  7993. (setq end (point-max))
  7994. (setq what "top-level")
  7995. (goto-char start)
  7996. (show-all)))
  7997. (setq beg (point))
  7998. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7999. (looking-at "\\(\\*+\\)")
  8000. (setq stars (match-string 1)
  8001. re (concat "^" (regexp-quote stars) " +")
  8002. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8003. txt (buffer-substring beg end))
  8004. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8005. (if (and (not (equal stars "*")) (string-match re2 txt))
  8006. (user-error "Region to sort contains a level above the first entry"))
  8007. (unless sorting-type
  8008. (message
  8009. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8010. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8011. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8012. what)
  8013. (setq sorting-type (read-char-exclusive))
  8014. (unless getkey-func
  8015. (and (= (downcase sorting-type) ?f)
  8016. (setq getkey-func
  8017. (org-icompleting-read "Sort using function: "
  8018. obarray 'fboundp t nil nil))
  8019. (setq getkey-func (intern getkey-func))))
  8020. (and (= (downcase sorting-type) ?r)
  8021. (not property)
  8022. (setq property
  8023. (org-icompleting-read "Property: "
  8024. (mapcar 'list (org-buffer-property-keys t))
  8025. nil t))))
  8026. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8027. (message "Sorting entries...")
  8028. (save-restriction
  8029. (narrow-to-region start end)
  8030. (let ((dcst (downcase sorting-type))
  8031. (case-fold-search nil)
  8032. (now (current-time)))
  8033. (sort-subr
  8034. (/= dcst sorting-type)
  8035. ;; This function moves to the beginning character of the "record" to
  8036. ;; be sorted.
  8037. (lambda nil
  8038. (if (re-search-forward re nil t)
  8039. (goto-char (match-beginning 0))
  8040. (goto-char (point-max))))
  8041. ;; This function moves to the last character of the "record" being
  8042. ;; sorted.
  8043. (lambda nil
  8044. (save-match-data
  8045. (condition-case nil
  8046. (outline-forward-same-level 1)
  8047. (error
  8048. (goto-char (point-max))))))
  8049. ;; This function returns the value that gets sorted against.
  8050. (lambda nil
  8051. (cond
  8052. ((= dcst ?n)
  8053. (if (looking-at org-complex-heading-regexp)
  8054. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8055. nil))
  8056. ((= dcst ?a)
  8057. (if (looking-at org-complex-heading-regexp)
  8058. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8059. nil))
  8060. ((= dcst ?k)
  8061. (or (get-text-property (point) :org-clock-minutes) 0))
  8062. ((= dcst ?t)
  8063. (let ((end (save-excursion (outline-next-heading) (point))))
  8064. (if (or (re-search-forward org-ts-regexp end t)
  8065. (re-search-forward org-ts-regexp-both end t))
  8066. (org-time-string-to-seconds (match-string 0))
  8067. (org-float-time now))))
  8068. ((= dcst ?c)
  8069. (let ((end (save-excursion (outline-next-heading) (point))))
  8070. (if (re-search-forward
  8071. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8072. end t)
  8073. (org-time-string-to-seconds (match-string 0))
  8074. (org-float-time now))))
  8075. ((= dcst ?s)
  8076. (let ((end (save-excursion (outline-next-heading) (point))))
  8077. (if (re-search-forward org-scheduled-time-regexp end t)
  8078. (org-time-string-to-seconds (match-string 1))
  8079. (org-float-time now))))
  8080. ((= dcst ?d)
  8081. (let ((end (save-excursion (outline-next-heading) (point))))
  8082. (if (re-search-forward org-deadline-time-regexp end t)
  8083. (org-time-string-to-seconds (match-string 1))
  8084. (org-float-time now))))
  8085. ((= dcst ?p)
  8086. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8087. (string-to-char (match-string 2))
  8088. org-default-priority))
  8089. ((= dcst ?r)
  8090. (or (org-entry-get nil property) ""))
  8091. ((= dcst ?o)
  8092. (if (looking-at org-complex-heading-regexp)
  8093. (let* ((m (match-string 2))
  8094. (s (if (member m org-done-keywords) '- '+)))
  8095. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8096. ((= dcst ?f)
  8097. (if getkey-func
  8098. (progn
  8099. (setq tmp (funcall getkey-func))
  8100. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8101. tmp)
  8102. (error "Invalid key function `%s'" getkey-func)))
  8103. (t (error "Invalid sorting type `%c'" sorting-type))))
  8104. nil
  8105. (cond
  8106. ((= dcst ?a) 'string<)
  8107. ((= dcst ?f) compare-func)
  8108. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8109. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8110. ;; Reset the clock marker if needed
  8111. (when cmstr
  8112. (save-excursion
  8113. (goto-char start)
  8114. (search-forward cmstr nil t)
  8115. (move-marker org-clock-marker (point))))
  8116. (message "Sorting entries...done")))
  8117. ;;; The orgstruct minor mode
  8118. ;; Define a minor mode which can be used in other modes in order to
  8119. ;; integrate the org-mode structure editing commands.
  8120. ;; This is really a hack, because the org-mode structure commands use
  8121. ;; keys which normally belong to the major mode. Here is how it
  8122. ;; works: The minor mode defines all the keys necessary to operate the
  8123. ;; structure commands, but wraps the commands into a function which
  8124. ;; tests if the cursor is currently at a headline or a plain list
  8125. ;; item. If that is the case, the structure command is used,
  8126. ;; temporarily setting many Org-mode variables like regular
  8127. ;; expressions for filling etc. However, when any of those keys is
  8128. ;; used at a different location, function uses `key-binding' to look
  8129. ;; up if the key has an associated command in another currently active
  8130. ;; keymap (minor modes, major mode, global), and executes that
  8131. ;; command. There might be problems if any of the keys is otherwise
  8132. ;; used as a prefix key.
  8133. (defcustom orgstruct-heading-prefix-regexp ""
  8134. "Regexp that matches the custom prefix of Org headlines in
  8135. orgstruct(++)-mode."
  8136. :group 'org
  8137. :version "24.4"
  8138. :package-version '(Org . "8.3")
  8139. :type 'regexp)
  8140. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8141. (defcustom orgstruct-setup-hook nil
  8142. "Hook run after orgstruct-mode-map is filled."
  8143. :group 'org
  8144. :version "24.4"
  8145. :package-version '(Org . "8.0")
  8146. :type 'hook)
  8147. (defvar orgstruct-initialized nil)
  8148. (defvar org-local-vars nil
  8149. "List of local variables, for use by `orgstruct-mode'.")
  8150. ;;;###autoload
  8151. (define-minor-mode orgstruct-mode
  8152. "Toggle the minor mode `orgstruct-mode'.
  8153. This mode is for using Org-mode structure commands in other
  8154. modes. The following keys behave as if Org-mode were active, if
  8155. the cursor is on a headline, or on a plain list item (both as
  8156. defined by Org-mode)."
  8157. nil " OrgStruct" (make-sparse-keymap)
  8158. (funcall (if orgstruct-mode
  8159. 'add-to-invisibility-spec
  8160. 'remove-from-invisibility-spec)
  8161. '(outline . t))
  8162. (when orgstruct-mode
  8163. (org-load-modules-maybe)
  8164. (unless orgstruct-initialized
  8165. (orgstruct-setup)
  8166. (setq orgstruct-initialized t))))
  8167. ;;;###autoload
  8168. (defun turn-on-orgstruct ()
  8169. "Unconditionally turn on `orgstruct-mode'."
  8170. (orgstruct-mode 1))
  8171. (defvar org-fb-vars nil)
  8172. (make-variable-buffer-local 'org-fb-vars)
  8173. (defun orgstruct++-mode (&optional arg)
  8174. "Toggle `orgstruct-mode', the enhanced version of it.
  8175. In addition to setting orgstruct-mode, this also exports all
  8176. indentation and autofilling variables from org-mode into the
  8177. buffer. It will also recognize item context in multiline items."
  8178. (interactive "P")
  8179. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8180. (if (< arg 1)
  8181. (progn (orgstruct-mode -1)
  8182. (mapc (lambda(v)
  8183. (org-set-local (car v)
  8184. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8185. org-fb-vars))
  8186. (orgstruct-mode 1)
  8187. (setq org-fb-vars nil)
  8188. (unless org-local-vars
  8189. (setq org-local-vars (org-get-local-variables)))
  8190. (let (var val)
  8191. (mapc
  8192. (lambda (x)
  8193. (when (string-match
  8194. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8195. (symbol-name (car x)))
  8196. (setq var (car x) val (nth 1 x))
  8197. (push (list var `(quote ,(eval var))) org-fb-vars)
  8198. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8199. org-local-vars)
  8200. (org-set-local 'orgstruct-is-++ t))))
  8201. (defvar orgstruct-is-++ nil
  8202. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8203. (make-variable-buffer-local 'orgstruct-is-++)
  8204. ;;;###autoload
  8205. (defun turn-on-orgstruct++ ()
  8206. "Unconditionally turn on `orgstruct++-mode'."
  8207. (orgstruct++-mode 1))
  8208. (defun orgstruct-error ()
  8209. "Error when there is no default binding for a structure key."
  8210. (interactive)
  8211. (funcall (if (fboundp 'user-error)
  8212. 'user-error
  8213. 'error)
  8214. "This key has no function outside structure elements"))
  8215. (defun orgstruct-setup ()
  8216. "Setup orgstruct keymap."
  8217. (dolist (cell '((org-demote . t)
  8218. (org-metaleft . t)
  8219. (org-metaright . t)
  8220. (org-promote . t)
  8221. (org-shiftmetaleft . t)
  8222. (org-shiftmetaright . t)
  8223. org-backward-element
  8224. org-backward-heading-same-level
  8225. org-ctrl-c-ret
  8226. org-ctrl-c-minus
  8227. org-ctrl-c-star
  8228. org-cycle
  8229. org-forward-heading-same-level
  8230. org-insert-heading
  8231. org-insert-heading-respect-content
  8232. org-kill-note-or-show-branches
  8233. org-mark-subtree
  8234. org-meta-return
  8235. org-metadown
  8236. org-metaup
  8237. org-narrow-to-subtree
  8238. org-promote-subtree
  8239. org-reveal
  8240. org-shiftdown
  8241. org-shiftleft
  8242. org-shiftmetadown
  8243. org-shiftmetaup
  8244. org-shiftright
  8245. org-shifttab
  8246. org-shifttab
  8247. org-shiftup
  8248. org-show-subtree
  8249. org-sort
  8250. org-up-element
  8251. outline-demote
  8252. outline-next-visible-heading
  8253. outline-previous-visible-heading
  8254. outline-promote
  8255. outline-up-heading
  8256. show-children))
  8257. (let ((f (or (car-safe cell) cell))
  8258. (disable-when-heading-prefix (cdr-safe cell)))
  8259. (when (fboundp f)
  8260. (let ((new-bindings))
  8261. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8262. (where-is-internal f outline-mode-map)))
  8263. (push binding new-bindings)
  8264. ;; TODO use local-function-key-map
  8265. (dolist (rep '(("<tab>" . "TAB")
  8266. ("<return>" . "RET")
  8267. ("<escape>" . "ESC")
  8268. ("<delete>" . "DEL")))
  8269. (setq binding (read-kbd-macro
  8270. (let ((case-fold-search))
  8271. (replace-regexp-in-string
  8272. (regexp-quote (cdr rep))
  8273. (car rep)
  8274. (key-description binding)))))
  8275. (pushnew binding new-bindings :test 'equal)))
  8276. (dolist (binding new-bindings)
  8277. (let ((key (lookup-key orgstruct-mode-map binding)))
  8278. (when (or (not key) (numberp key))
  8279. (ignore-errors
  8280. (org-defkey orgstruct-mode-map
  8281. binding
  8282. (orgstruct-make-binding
  8283. f binding disable-when-heading-prefix))))))))))
  8284. (run-hooks 'orgstruct-setup-hook))
  8285. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8286. "Create a function for binding in the structure minor mode.
  8287. FUN is the command to call inside a table. KEY is the key that
  8288. should be checked in for a command to execute outside of tables.
  8289. Non-nil `disable-when-heading-prefix' means to disable the command
  8290. if `orgstruct-heading-prefix-regexp' is not empty."
  8291. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8292. (let ((nname name)
  8293. (i 0))
  8294. (while (fboundp (intern nname))
  8295. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8296. (setq name (intern nname)))
  8297. (eval
  8298. (let ((bindings '((org-heading-regexp
  8299. (concat "^"
  8300. orgstruct-heading-prefix-regexp
  8301. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8302. (org-outline-regexp
  8303. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8304. (org-outline-regexp-bol
  8305. (concat "^" org-outline-regexp))
  8306. (outline-regexp org-outline-regexp)
  8307. (outline-heading-end-regexp "\n")
  8308. (outline-level 'org-outline-level)
  8309. (outline-heading-alist))))
  8310. `(defun ,name (arg)
  8311. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8312. "Outside of structure, run the binding of `"
  8313. (key-description key) "'."
  8314. (when disable-when-heading-prefix
  8315. (concat
  8316. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8317. "back to the default binding due to limitations of Org's implementation of\n"
  8318. "`" (symbol-name fun) "'.")))
  8319. (interactive "p")
  8320. (let* ((disable
  8321. ,(and disable-when-heading-prefix
  8322. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8323. (fallback
  8324. (or disable
  8325. (not
  8326. (let* ,bindings
  8327. (org-context-p 'headline 'item
  8328. ,(when (memq fun
  8329. '(org-insert-heading
  8330. org-insert-heading-respect-content
  8331. org-meta-return))
  8332. '(when orgstruct-is-++
  8333. 'item-body))))))))
  8334. (if fallback
  8335. (let* ((orgstruct-mode)
  8336. (binding
  8337. (let ((key ,key))
  8338. (catch 'exit
  8339. (dolist
  8340. (rep
  8341. '(nil
  8342. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8343. ("<\\([^>]*\\)return>" . "\\1RET")
  8344. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8345. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8346. nil)
  8347. (when rep
  8348. (setq key (read-kbd-macro
  8349. (let ((case-fold-search))
  8350. (replace-regexp-in-string
  8351. (car rep)
  8352. (cdr rep)
  8353. (key-description key))))))
  8354. (when (key-binding key)
  8355. (throw 'exit (key-binding key))))))))
  8356. (if (keymapp binding)
  8357. (org-set-transient-map binding)
  8358. (let ((func (or binding
  8359. (unless disable
  8360. 'orgstruct-error))))
  8361. (when func
  8362. (call-interactively func)))))
  8363. (org-run-like-in-org-mode
  8364. (lambda ()
  8365. (interactive)
  8366. (let* ,bindings
  8367. (call-interactively ',fun)))))))))
  8368. name))
  8369. (defun org-contextualize-keys (alist contexts)
  8370. "Return valid elements in ALIST depending on CONTEXTS.
  8371. `org-agenda-custom-commands' or `org-capture-templates' are the
  8372. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8373. or `org-capture-templates-contexts' are the associated contexts
  8374. definitions."
  8375. (let ((contexts
  8376. ;; normalize contexts
  8377. (mapcar
  8378. (lambda(c) (cond ((listp (cadr c))
  8379. (list (car c) (car c) (cadr c)))
  8380. ((string= "" (cadr c))
  8381. (list (car c) (car c) (caddr c)))
  8382. (t c)))
  8383. contexts))
  8384. (a alist) r s)
  8385. ;; loop over all commands or templates
  8386. (dolist (c a)
  8387. (let (vrules repl)
  8388. (cond
  8389. ((not (assoc (car c) contexts))
  8390. (push c r))
  8391. ((and (assoc (car c) contexts)
  8392. (setq vrules (org-contextualize-validate-key
  8393. (car c) contexts)))
  8394. (mapc (lambda (vr)
  8395. (when (not (equal (car vr) (cadr vr)))
  8396. (setq repl vr)))
  8397. vrules)
  8398. (if (not repl) (push c r)
  8399. (push (cadr repl) s)
  8400. (push
  8401. (cons (car c)
  8402. (cdr (or (assoc (cadr repl) alist)
  8403. (error "Undefined key `%s' as contextual replacement for `%s'"
  8404. (cadr repl) (car c)))))
  8405. r))))))
  8406. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8407. (delq
  8408. nil
  8409. (delete-dups
  8410. (mapcar (lambda (x)
  8411. (let ((tpl (car x)))
  8412. (when (not (delq
  8413. nil
  8414. (mapcar (lambda (y)
  8415. (equal y tpl))
  8416. s)))
  8417. x)))
  8418. (reverse r))))))
  8419. (defun org-contextualize-validate-key (key contexts)
  8420. "Check CONTEXTS for agenda or capture KEY."
  8421. (let (rr res)
  8422. (dolist (r contexts)
  8423. (mapc
  8424. (lambda (rr)
  8425. (when
  8426. (and (equal key (car r))
  8427. (if (functionp rr) (funcall rr)
  8428. (or (and (eq (car rr) 'in-file)
  8429. (buffer-file-name)
  8430. (string-match (cdr rr) (buffer-file-name)))
  8431. (and (eq (car rr) 'in-mode)
  8432. (string-match (cdr rr) (symbol-name major-mode)))
  8433. (and (eq (car rr) 'in-buffer)
  8434. (string-match (cdr rr) (buffer-name)))
  8435. (when (and (eq (car rr) 'not-in-file)
  8436. (buffer-file-name))
  8437. (not (string-match (cdr rr) (buffer-file-name))))
  8438. (when (eq (car rr) 'not-in-mode)
  8439. (not (string-match (cdr rr) (symbol-name major-mode))))
  8440. (when (eq (car rr) 'not-in-buffer)
  8441. (not (string-match (cdr rr) (buffer-name)))))))
  8442. (push r res)))
  8443. (car (last r))))
  8444. (delete-dups (delq nil res))))
  8445. (defun org-context-p (&rest contexts)
  8446. "Check if local context is any of CONTEXTS.
  8447. Possible values in the list of contexts are `table', `headline', and `item'."
  8448. (let ((pos (point)))
  8449. (goto-char (point-at-bol))
  8450. (prog1 (or (and (memq 'table contexts)
  8451. (looking-at "[ \t]*|"))
  8452. (and (memq 'headline contexts)
  8453. (looking-at org-outline-regexp))
  8454. (and (memq 'item contexts)
  8455. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8456. (and (memq 'item-body contexts)
  8457. (org-in-item-p)))
  8458. (goto-char pos))))
  8459. (defun org-get-local-variables ()
  8460. "Return a list of all local variables in an Org mode buffer."
  8461. (let (varlist)
  8462. (with-current-buffer (get-buffer-create "*Org tmp*")
  8463. (erase-buffer)
  8464. (org-mode)
  8465. (setq varlist (buffer-local-variables)))
  8466. (kill-buffer "*Org tmp*")
  8467. (delq nil
  8468. (mapcar
  8469. (lambda (x)
  8470. (setq x
  8471. (if (symbolp x)
  8472. (list x)
  8473. (list (car x) (cdr x))))
  8474. (if (and (not (get (car x) 'org-state))
  8475. (string-match
  8476. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8477. (symbol-name (car x))))
  8478. x nil))
  8479. varlist))))
  8480. (defun org-clone-local-variables (from-buffer &optional regexp)
  8481. "Clone local variables from FROM-BUFFER.
  8482. Optional argument REGEXP selects variables to clone."
  8483. (mapc
  8484. (lambda (pair)
  8485. (and (symbolp (car pair))
  8486. (or (null regexp)
  8487. (string-match regexp (symbol-name (car pair))))
  8488. (set (make-local-variable (car pair))
  8489. (cdr pair))))
  8490. (buffer-local-variables from-buffer)))
  8491. ;;;###autoload
  8492. (defun org-run-like-in-org-mode (cmd)
  8493. "Run a command, pretending that the current buffer is in Org-mode.
  8494. This will temporarily bind local variables that are typically bound in
  8495. Org-mode to the values they have in Org-mode, and then interactively
  8496. call CMD."
  8497. (org-load-modules-maybe)
  8498. (unless org-local-vars
  8499. (setq org-local-vars (org-get-local-variables)))
  8500. (let (binds)
  8501. (dolist (var org-local-vars)
  8502. (when (or (not (boundp (car var)))
  8503. (eq (symbol-value (car var))
  8504. (default-value (car var))))
  8505. (push (list (car var) `(quote ,(cadr var))) binds)))
  8506. (eval `(let ,binds
  8507. (call-interactively (quote ,cmd))))))
  8508. (defun org-get-category (&optional pos force-refresh)
  8509. "Get the category applying to position POS."
  8510. (save-match-data
  8511. (if force-refresh (org-refresh-category-properties))
  8512. (let ((pos (or pos (point))))
  8513. (or (get-text-property pos 'org-category)
  8514. (progn (org-refresh-category-properties)
  8515. (get-text-property pos 'org-category))))))
  8516. ;;; Refresh properties
  8517. (defun org-refresh-properties (dprop tprop)
  8518. "Refresh buffer text properties.
  8519. DPROP is the drawer property and TPROP is either the
  8520. corresponding text property to set, or an alist with each element
  8521. being a text property (as a symbol) and a function to apply to
  8522. the value of the drawer property."
  8523. (let ((case-fold-search t)
  8524. (inhibit-read-only t))
  8525. (org-with-silent-modifications
  8526. (save-excursion
  8527. (save-restriction
  8528. (widen)
  8529. (goto-char (point-min))
  8530. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8531. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8532. (defun org-refresh-property (tprop p)
  8533. "Refresh the buffer text property TPROP from the drawer property P.
  8534. The refresh happens only for the current tree (not subtree)."
  8535. (unless (org-before-first-heading-p)
  8536. (save-excursion
  8537. (org-back-to-heading t)
  8538. (if (symbolp tprop)
  8539. ;; TPROP is a text property symbol
  8540. (put-text-property
  8541. (point) (or (outline-next-heading) (point-max)) tprop p)
  8542. ;; TPROP is an alist with (properties . function) elements
  8543. (dolist (al tprop)
  8544. (save-excursion
  8545. (put-text-property
  8546. (line-beginning-position) (or (outline-next-heading) (point-max))
  8547. (car al)
  8548. (funcall (cdr al) p))))))))
  8549. (defun org-refresh-category-properties ()
  8550. "Refresh category text properties in the buffer."
  8551. (let ((case-fold-search t)
  8552. (inhibit-read-only t)
  8553. (default-category
  8554. (cond ((null org-category)
  8555. (if buffer-file-name
  8556. (file-name-sans-extension
  8557. (file-name-nondirectory buffer-file-name))
  8558. "???"))
  8559. ((symbolp org-category) (symbol-name org-category))
  8560. (t org-category))))
  8561. (org-with-silent-modifications
  8562. (org-with-wide-buffer
  8563. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8564. ;; This is the default category for the buffer. If none is
  8565. ;; found, fall-back to `org-category' or buffer file name.
  8566. (put-text-property
  8567. (point-min) (point-max)
  8568. 'org-category
  8569. (catch 'buffer-category
  8570. (goto-char (point-max))
  8571. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8572. (let ((element (org-element-at-point)))
  8573. (when (eq (org-element-type element) 'keyword)
  8574. (throw 'buffer-category
  8575. (org-element-property :value element)))))
  8576. default-category))
  8577. ;; Set sub-tree specific categories.
  8578. (goto-char (point-min))
  8579. (let ((regexp (org-re-property "CATEGORY")))
  8580. (while (re-search-forward regexp nil t)
  8581. (let ((value (org-match-string-no-properties 3)))
  8582. (when (org-at-property-p)
  8583. (put-text-property
  8584. (save-excursion (org-back-to-heading t) (point))
  8585. (save-excursion (org-end-of-subtree t t) (point))
  8586. 'org-category
  8587. value)))))))))
  8588. (defun org-refresh-stats-properties ()
  8589. "Refresh stats text properties in the buffer."
  8590. (let (stats)
  8591. (org-with-silent-modifications
  8592. (save-excursion
  8593. (save-restriction
  8594. (widen)
  8595. (goto-char (point-min))
  8596. (while (re-search-forward
  8597. (concat org-outline-regexp-bol ".*"
  8598. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8599. nil t)
  8600. (setq stats (cond ((equal (match-string 3) "0") 0)
  8601. ((match-string 2)
  8602. (/ (* (string-to-number (match-string 2)) 100)
  8603. (string-to-number (match-string 3))))
  8604. (t (string-to-number (match-string 1)))))
  8605. (org-back-to-heading t)
  8606. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8607. 'org-stats stats)))))))
  8608. (defun org-refresh-effort-properties ()
  8609. "Refresh effort properties"
  8610. (org-refresh-properties
  8611. org-effort-property
  8612. '((effort . identity)
  8613. (effort-minutes . org-duration-string-to-minutes))))
  8614. ;;;; Link Stuff
  8615. ;;; Link abbreviations
  8616. (defun org-link-expand-abbrev (link)
  8617. "Apply replacements as defined in `org-link-abbrev-alist'."
  8618. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8619. (let* ((key (match-string 1 link))
  8620. (as (or (assoc key org-link-abbrev-alist-local)
  8621. (assoc key org-link-abbrev-alist)))
  8622. (tag (and (match-end 2) (match-string 3 link)))
  8623. rpl)
  8624. (if (not as)
  8625. link
  8626. (setq rpl (cdr as))
  8627. (cond
  8628. ((symbolp rpl) (funcall rpl tag))
  8629. ((string-match "%(\\([^)]+\\))" rpl)
  8630. (replace-match
  8631. (save-match-data
  8632. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8633. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8634. ((string-match "%h" rpl)
  8635. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8636. (t (concat rpl tag)))))
  8637. link))
  8638. ;;; Storing and inserting links
  8639. (defvar org-insert-link-history nil
  8640. "Minibuffer history for links inserted with `org-insert-link'.")
  8641. (defvar org-stored-links nil
  8642. "Contains the links stored with `org-store-link'.")
  8643. (defvar org-store-link-plist nil
  8644. "Plist with info about the most recently link created with `org-store-link'.")
  8645. (defvar org-link-protocols nil
  8646. "Link protocols added to Org-mode using `org-add-link-type'.")
  8647. (defvar org-store-link-functions nil
  8648. "List of functions that are called to create and store a link.
  8649. Each function will be called in turn until one returns a non-nil
  8650. value. Each function should check if it is responsible for creating
  8651. this link (for example by looking at the major mode).
  8652. If not, it must exit and return nil.
  8653. If yes, it should return a non-nil value after a calling
  8654. `org-store-link-props' with a list of properties and values.
  8655. Special properties are:
  8656. :type The link prefix, like \"http\". This must be given.
  8657. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8658. This is obligatory as well.
  8659. :description Optional default description for the second pair
  8660. of brackets in an Org-mode link. The user can still change
  8661. this when inserting this link into an Org-mode buffer.
  8662. In addition to these, any additional properties can be specified
  8663. and then used in capture templates.")
  8664. (defun org-add-link-type (type &optional follow export)
  8665. "Add TYPE to the list of `org-link-types'.
  8666. Re-compute all regular expressions depending on `org-link-types'
  8667. FOLLOW and EXPORT are two functions.
  8668. FOLLOW should take the link path as the single argument and do whatever
  8669. is necessary to follow the link, for example find a file or display
  8670. a mail message.
  8671. EXPORT should format the link path for export to one of the export formats.
  8672. It should be a function accepting three arguments:
  8673. path the path of the link, the text after the prefix (like \"http:\")
  8674. desc the description of the link, if any
  8675. format the export format, a symbol like `html' or `latex' or `ascii'.
  8676. The function may use the FORMAT information to return different values
  8677. depending on the format. The return value will be put literally into
  8678. the exported file. If the return value is nil, this means Org should
  8679. do what it normally does with links which do not have EXPORT defined.
  8680. Org mode has a built-in default for exporting links. If you are happy with
  8681. this default, there is no need to define an export function for the link
  8682. type. For a simple example of an export function, see `org-bbdb.el'."
  8683. (add-to-list 'org-link-types type t)
  8684. (org-make-link-regexps)
  8685. (org-element-update-syntax)
  8686. (if (assoc type org-link-protocols)
  8687. (setcdr (assoc type org-link-protocols) (list follow export))
  8688. (push (list type follow export) org-link-protocols)))
  8689. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8690. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8691. ;;;###autoload
  8692. (defun org-store-link (arg)
  8693. "\\<org-mode-map>Store an org-link to the current location.
  8694. This link is added to `org-stored-links' and can later be inserted
  8695. into an Org buffer with \\[org-insert-link].
  8696. For some link types, a prefix ARG is interpreted.
  8697. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8698. For file links, ARG negates `org-context-in-file-links'.
  8699. A double prefix ARG force skipping storing functions that are not
  8700. part of Org's core.
  8701. A triple prefix ARG force storing a link for each line in the
  8702. active region."
  8703. (interactive "P")
  8704. (org-load-modules-maybe)
  8705. (if (and (equal arg '(64)) (org-region-active-p))
  8706. (save-excursion
  8707. (let ((end (region-end)))
  8708. (goto-char (region-beginning))
  8709. (set-mark (point))
  8710. (while (< (point-at-eol) end)
  8711. (move-end-of-line 1) (activate-mark)
  8712. (let (current-prefix-arg)
  8713. (call-interactively 'org-store-link))
  8714. (move-beginning-of-line 2)
  8715. (set-mark (point)))))
  8716. (org-with-limited-levels
  8717. (setq org-store-link-plist nil)
  8718. (let (link cpltxt desc description search
  8719. txt custom-id agenda-link sfuns sfunsn)
  8720. (cond
  8721. ;; Store a link using an external link type
  8722. ((and (not (equal arg '(16)))
  8723. (setq sfuns
  8724. (delq
  8725. nil (mapcar (lambda (f)
  8726. (let (fs) (if (funcall f) (push f fs))))
  8727. org-store-link-functions))
  8728. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8729. (or (and (cdr sfuns)
  8730. (funcall (intern
  8731. (completing-read
  8732. "Which function for creating the link? "
  8733. sfunsn nil t (car sfunsn)))))
  8734. (funcall (caar sfuns)))
  8735. (setq link (plist-get org-store-link-plist :link)
  8736. desc (or (plist-get org-store-link-plist
  8737. :description)
  8738. link))))
  8739. ;; Store a link from a source code buffer.
  8740. ((org-src-edit-buffer-p)
  8741. (cond
  8742. ((save-excursion
  8743. (beginning-of-line)
  8744. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8745. "[ \t]*$")))
  8746. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8747. ((org-called-interactively-p 'any)
  8748. (let (label)
  8749. (while (or (not label)
  8750. (org-with-wide-buffer
  8751. (goto-char (point-min))
  8752. (re-search-forward
  8753. (regexp-quote (format org-coderef-label-format label))
  8754. nil t)))
  8755. (when label (message "Label exists already") (sit-for 2))
  8756. (setq label (read-string "Code line label: " label)))
  8757. (end-of-line)
  8758. (setq link (format org-coderef-label-format label))
  8759. (let ((gc (- 79 (length link))))
  8760. (if (< (current-column) gc) (org-move-to-column gc t)
  8761. (insert " ")))
  8762. (insert link)
  8763. (setq link (concat "(" label ")") desc nil)))
  8764. (t (setq link nil))))
  8765. ;; We are in the agenda, link to referenced location
  8766. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8767. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8768. (get-text-property (point) 'org-marker))))
  8769. (when m
  8770. (org-with-point-at m
  8771. (setq agenda-link
  8772. (if (org-called-interactively-p 'any)
  8773. (call-interactively 'org-store-link)
  8774. (org-store-link nil)))))))
  8775. ((eq major-mode 'calendar-mode)
  8776. (let ((cd (calendar-cursor-to-date)))
  8777. (setq link
  8778. (format-time-string
  8779. (car org-time-stamp-formats)
  8780. (apply 'encode-time
  8781. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8782. nil nil nil))))
  8783. (org-store-link-props :type "calendar" :date cd)))
  8784. ((eq major-mode 'help-mode)
  8785. (setq link (concat "help:" (save-excursion
  8786. (goto-char (point-min))
  8787. (looking-at "^[^ ]+")
  8788. (match-string 0))))
  8789. (org-store-link-props :type "help"))
  8790. ((eq major-mode 'w3-mode)
  8791. (setq cpltxt (if (and (buffer-name)
  8792. (not (string-match "Untitled" (buffer-name))))
  8793. (buffer-name)
  8794. (url-view-url t))
  8795. link (url-view-url t))
  8796. (org-store-link-props :type "w3" :url (url-view-url t)))
  8797. ((eq major-mode 'image-mode)
  8798. (setq cpltxt (concat "file:"
  8799. (abbreviate-file-name buffer-file-name))
  8800. link cpltxt)
  8801. (org-store-link-props :type "image" :file buffer-file-name))
  8802. ;; In dired, store a link to the file of the current line
  8803. ((derived-mode-p 'dired-mode)
  8804. (let ((file (dired-get-filename nil t)))
  8805. (setq file (if file
  8806. (abbreviate-file-name
  8807. (expand-file-name (dired-get-filename nil t)))
  8808. ;; otherwise, no file so use current directory.
  8809. default-directory))
  8810. (setq cpltxt (concat "file:" file)
  8811. link cpltxt)))
  8812. ((setq search (run-hook-with-args-until-success
  8813. 'org-create-file-search-functions))
  8814. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8815. "::" search))
  8816. (setq cpltxt (or description link)))
  8817. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8818. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8819. (cond
  8820. ;; Store a link using the target at point
  8821. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8822. (setq cpltxt
  8823. (concat "file:"
  8824. (abbreviate-file-name
  8825. (buffer-file-name (buffer-base-buffer)))
  8826. "::" (match-string 1))
  8827. link cpltxt))
  8828. ((and (featurep 'org-id)
  8829. (or (eq org-id-link-to-org-use-id t)
  8830. (and (org-called-interactively-p 'any)
  8831. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8832. (and (eq org-id-link-to-org-use-id
  8833. 'create-if-interactive-and-no-custom-id)
  8834. (not custom-id))))
  8835. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8836. ;; Store a link using the ID at point
  8837. (setq link (condition-case nil
  8838. (prog1 (org-id-store-link)
  8839. (setq desc (or (plist-get org-store-link-plist
  8840. :description)
  8841. "")))
  8842. (error
  8843. ;; Probably before first headline, link only to file
  8844. (concat "file:"
  8845. (abbreviate-file-name
  8846. (buffer-file-name (buffer-base-buffer))))))))
  8847. (t
  8848. ;; Just link to current headline
  8849. (setq cpltxt (concat "file:"
  8850. (abbreviate-file-name
  8851. (buffer-file-name (buffer-base-buffer)))))
  8852. ;; Add a context search string
  8853. (when (org-xor org-context-in-file-links arg)
  8854. (let* ((ee (org-element-at-point))
  8855. (et (org-element-type ee))
  8856. (ev (plist-get (cadr ee) :value))
  8857. (ek (plist-get (cadr ee) :key))
  8858. (eok (and (stringp ek) (string-match "name" ek))))
  8859. (setq txt (cond
  8860. ((org-at-heading-p) nil)
  8861. ((and (eq et 'keyword) eok) ev)
  8862. ((org-region-active-p)
  8863. (buffer-substring (region-beginning) (region-end)))))
  8864. (when (or (null txt) (string-match "\\S-" txt))
  8865. (setq cpltxt
  8866. (concat cpltxt "::"
  8867. (condition-case nil
  8868. (org-make-org-heading-search-string txt)
  8869. (error "")))
  8870. desc (or (and (eq et 'keyword) eok ev)
  8871. (nth 4 (ignore-errors (org-heading-components)))
  8872. "NONE")))))
  8873. (if (string-match "::\\'" cpltxt)
  8874. (setq cpltxt (substring cpltxt 0 -2)))
  8875. (setq link cpltxt))))
  8876. ((buffer-file-name (buffer-base-buffer))
  8877. ;; Just link to this file here.
  8878. (setq cpltxt (concat "file:"
  8879. (abbreviate-file-name
  8880. (buffer-file-name (buffer-base-buffer)))))
  8881. ;; Add a context string.
  8882. (when (org-xor org-context-in-file-links arg)
  8883. (setq txt (if (org-region-active-p)
  8884. (buffer-substring (region-beginning) (region-end))
  8885. (buffer-substring (point-at-bol) (point-at-eol))))
  8886. ;; Only use search option if there is some text.
  8887. (when (string-match "\\S-" txt)
  8888. (setq cpltxt
  8889. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8890. desc "NONE")))
  8891. (setq link cpltxt))
  8892. ((org-called-interactively-p 'interactive)
  8893. (user-error "No method for storing a link from this buffer"))
  8894. (t (setq link nil)))
  8895. ;; We're done setting link and desc, clean up
  8896. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8897. (setq link (or link cpltxt)
  8898. desc (or desc cpltxt))
  8899. (cond ((equal desc "NONE") (setq desc nil))
  8900. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8901. (let ((d0 (match-string 3 desc))
  8902. (p0 (match-string 5 desc)))
  8903. (setq desc
  8904. (replace-regexp-in-string
  8905. org-bracket-link-regexp
  8906. (concat (or p0 d0)
  8907. (if (equal (length (match-string 0 desc))
  8908. (length desc)) "*" "")) desc)))))
  8909. ;; Return the link
  8910. (if (not (and (or (org-called-interactively-p 'any)
  8911. executing-kbd-macro)
  8912. link))
  8913. (or agenda-link (and link (org-make-link-string link desc)))
  8914. (push (list link desc) org-stored-links)
  8915. (message "Stored: %s" (or desc link))
  8916. (when custom-id
  8917. (setq link (concat "file:" (abbreviate-file-name
  8918. (buffer-file-name)) "::#" custom-id))
  8919. (push (list link desc) org-stored-links))
  8920. (car org-stored-links))))))
  8921. (defun org-store-link-props (&rest plist)
  8922. "Store link properties, extract names and addresses."
  8923. (let (x adr)
  8924. (when (setq x (plist-get plist :from))
  8925. (setq adr (mail-extract-address-components x))
  8926. (setq plist (plist-put plist :fromname (car adr)))
  8927. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8928. (when (setq x (plist-get plist :to))
  8929. (setq adr (mail-extract-address-components x))
  8930. (setq plist (plist-put plist :toname (car adr)))
  8931. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8932. (let ((from (plist-get plist :from))
  8933. (to (plist-get plist :to)))
  8934. (when (and from to org-from-is-user-regexp)
  8935. (setq plist
  8936. (plist-put plist :fromto
  8937. (if (string-match org-from-is-user-regexp from)
  8938. (concat "to %t")
  8939. (concat "from %f"))))))
  8940. (setq org-store-link-plist plist))
  8941. (defun org-add-link-props (&rest plist)
  8942. "Add these properties to the link property list."
  8943. (let (key value)
  8944. (while plist
  8945. (setq key (pop plist) value (pop plist))
  8946. (setq org-store-link-plist
  8947. (plist-put org-store-link-plist key value)))))
  8948. (defun org-email-link-description (&optional fmt)
  8949. "Return the description part of an email link.
  8950. This takes information from `org-store-link-plist' and formats it
  8951. according to FMT (default from `org-email-link-description-format')."
  8952. (setq fmt (or fmt org-email-link-description-format))
  8953. (let* ((p org-store-link-plist)
  8954. (to (plist-get p :toaddress))
  8955. (from (plist-get p :fromaddress))
  8956. (table
  8957. (list
  8958. (cons "%c" (plist-get p :fromto))
  8959. (cons "%F" (plist-get p :from))
  8960. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8961. (cons "%T" (plist-get p :to))
  8962. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8963. (cons "%s" (plist-get p :subject))
  8964. (cons "%d" (plist-get p :date))
  8965. (cons "%m" (plist-get p :message-id)))))
  8966. (when (string-match "%c" fmt)
  8967. ;; Check if the user wrote this message
  8968. (if (and org-from-is-user-regexp from to
  8969. (save-match-data (string-match org-from-is-user-regexp from)))
  8970. (setq fmt (replace-match "to %t" t t fmt))
  8971. (setq fmt (replace-match "from %f" t t fmt))))
  8972. (org-replace-escapes fmt table)))
  8973. (defun org-make-org-heading-search-string (&optional string)
  8974. "Make search string for the current headline or STRING."
  8975. (let ((s (or string
  8976. (and (derived-mode-p 'org-mode)
  8977. (save-excursion
  8978. (org-back-to-heading t)
  8979. (org-element-property :raw-value (org-element-at-point))))))
  8980. (lines org-context-in-file-links))
  8981. (or string (setq s (concat "*" s))) ; Add * for headlines
  8982. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8983. (when (and string (integerp lines) (> lines 0))
  8984. (let ((slines (org-split-string s "\n")))
  8985. (when (< lines (length slines))
  8986. (setq s (mapconcat
  8987. 'identity
  8988. (reverse (nthcdr (- (length slines) lines)
  8989. (reverse slines))) "\n")))))
  8990. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8991. (defun org-make-link-string (link &optional description)
  8992. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8993. (unless (org-string-nw-p link) (error "Empty link"))
  8994. (let ((uri (cond ((string-match org-link-types-re link)
  8995. (concat (match-string 1 link)
  8996. (org-link-escape (substring link (match-end 1)))))
  8997. ;; For readability, url-encode internal links only
  8998. ;; when absolutely needed (i.e, when they contain
  8999. ;; square brackets). File links however, are
  9000. ;; encoded since, e.g., spaces are significant.
  9001. ((or (file-name-absolute-p link)
  9002. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9003. (org-link-escape link))
  9004. (t link)))
  9005. (description
  9006. (and (org-string-nw-p description)
  9007. ;; Remove brackets from description, as they are fatal.
  9008. (replace-regexp-in-string
  9009. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9010. (org-trim description)))))
  9011. (format "[[%s]%s]"
  9012. uri
  9013. (if description (format "[%s]" description) ""))))
  9014. (defconst org-link-escape-chars
  9015. ;;%20 %5B %5D %25
  9016. '(?\s ?\[ ?\] ?%)
  9017. "List of characters that should be escaped in a link when stored to Org.
  9018. This is the list that is used for internal purposes.")
  9019. (defconst org-link-escape-chars-browser
  9020. ;;%20 %22
  9021. '(?\s ?\")
  9022. "List of characters to be escaped before handing over to the browser.
  9023. If you consider using this constant then you probably want to use
  9024. the function `org-link-escape-browser' instead. See there why
  9025. this constant is a candidate to be removed once Org drops support
  9026. for Emacs 24.1 and 24.2.")
  9027. (defun org-link-escape (text &optional table merge)
  9028. "Return percent escaped representation of TEXT.
  9029. TEXT is a string with the text to escape.
  9030. Optional argument TABLE is a list with characters that should be
  9031. escaped. When nil, `org-link-escape-chars' is used.
  9032. If optional argument MERGE is set, merge TABLE into
  9033. `org-link-escape-chars'."
  9034. (let ((characters-to-encode
  9035. (cond ((null table) org-link-escape-chars)
  9036. (merge (append org-link-escape-chars table))
  9037. (t table))))
  9038. (mapconcat
  9039. (lambda (c)
  9040. (if (or (memq c characters-to-encode)
  9041. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9042. (mapconcat (lambda (e) (format "%%%.2X" e))
  9043. (or (encode-coding-char c 'utf-8)
  9044. (error "Unable to percent escape character: %c" c))
  9045. "")
  9046. (char-to-string c)))
  9047. text "")))
  9048. (defun org-link-escape-browser (text)
  9049. "Escape some characters before handing over to the browser.
  9050. This function is a candidate to be removed together with the
  9051. constant `org-link-escape-chars-browser' once Org drops support
  9052. for Emacs 24.1 and 24.2. All calls to this function will have to
  9053. be replaced with `url-encode-url' which is available since Emacs
  9054. 24.3.1."
  9055. ;; Example with the Org link
  9056. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9057. ;; to open the browser with +subject:"Release 8.2" filled into the
  9058. ;; query field: In this case the variable TEXT contains the
  9059. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9060. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9061. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9062. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9063. (if (fboundp 'url-encode-url)
  9064. (url-encode-url text)
  9065. (if (org-string-match-p
  9066. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9067. text)
  9068. (org-link-escape text org-link-escape-chars-browser)
  9069. text)))
  9070. (defun org-link-unescape (str)
  9071. "Unhex hexified Unicode parts in string STR.
  9072. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9073. reciprocal of `org-link-escape', which see."
  9074. (if (org-string-nw-p str)
  9075. (replace-regexp-in-string
  9076. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9077. str))
  9078. (defun org-link-unescape-compound (hex)
  9079. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9080. Note: this function also decodes single byte encodings like
  9081. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9082. (save-match-data
  9083. (let* ((bytes (cdr (split-string hex "%")))
  9084. (ret "")
  9085. (eat 0)
  9086. (sum 0))
  9087. (while bytes
  9088. (let* ((val (string-to-number (pop bytes) 16))
  9089. (shift-xor
  9090. (if (= 0 eat)
  9091. (cond
  9092. ((>= val 252) (cons 6 252))
  9093. ((>= val 248) (cons 5 248))
  9094. ((>= val 240) (cons 4 240))
  9095. ((>= val 224) (cons 3 224))
  9096. ((>= val 192) (cons 2 192))
  9097. (t (cons 0 0)))
  9098. (cons 6 128))))
  9099. (if (>= val 192) (setq eat (car shift-xor)))
  9100. (setq val (logxor val (cdr shift-xor)))
  9101. (setq sum (+ (lsh sum (car shift-xor)) val))
  9102. (if (> eat 0) (setq eat (- eat 1)))
  9103. (cond
  9104. ((= 0 eat) ;multi byte
  9105. (setq ret (concat ret (org-char-to-string sum)))
  9106. (setq sum 0))
  9107. ((not bytes) ; single byte(s)
  9108. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9109. ret)))
  9110. (defun org-link-unescape-single-byte-sequence (hex)
  9111. "Unhexify hex-encoded single byte character sequences."
  9112. (mapconcat (lambda (byte)
  9113. (char-to-string (string-to-number byte 16)))
  9114. (cdr (split-string hex "%")) ""))
  9115. (defun org-xor (a b)
  9116. "Exclusive or."
  9117. (if a (not b) b))
  9118. (defun org-fixup-message-id-for-http (s)
  9119. "Replace special characters in a message id, so it can be used in an http query."
  9120. (when (string-match "%" s)
  9121. (setq s (mapconcat (lambda (c)
  9122. (if (eq c ?%)
  9123. "%25"
  9124. (char-to-string c)))
  9125. s "")))
  9126. (while (string-match "<" s)
  9127. (setq s (replace-match "%3C" t t s)))
  9128. (while (string-match ">" s)
  9129. (setq s (replace-match "%3E" t t s)))
  9130. (while (string-match "@" s)
  9131. (setq s (replace-match "%40" t t s)))
  9132. s)
  9133. (defun org-link-prettify (link)
  9134. "Return a human-readable representation of LINK.
  9135. The car of LINK must be a raw link.
  9136. The cdr of LINK must be either a link description or nil."
  9137. (let ((desc (or (cadr link) "<no description>")))
  9138. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9139. "<" (car link) ">")))
  9140. ;;;###autoload
  9141. (defun org-insert-link-global ()
  9142. "Insert a link like Org-mode does.
  9143. This command can be called in any mode to insert a link in Org-mode syntax."
  9144. (interactive)
  9145. (org-load-modules-maybe)
  9146. (org-run-like-in-org-mode 'org-insert-link))
  9147. (defun org-insert-all-links (arg &optional pre post)
  9148. "Insert all links in `org-stored-links'.
  9149. When a universal prefix, do not delete the links from `org-stored-links'.
  9150. When `ARG' is a number, insert the last N link(s).
  9151. `PRE' and `POST' are optional arguments to define a string to
  9152. prepend or to append."
  9153. (interactive "P")
  9154. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9155. (links (copy-seq org-stored-links))
  9156. (pr (or pre "- "))
  9157. (po (or post "\n"))
  9158. (cnt 1) l)
  9159. (if (null org-stored-links)
  9160. (message "No link to insert")
  9161. (while (and (or (listp arg) (>= arg cnt))
  9162. (setq l (if (listp arg)
  9163. (pop links)
  9164. (pop org-stored-links))))
  9165. (setq cnt (1+ cnt))
  9166. (insert pr)
  9167. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9168. (insert po)))))
  9169. (defun org-insert-last-stored-link (arg)
  9170. "Insert the last link stored in `org-stored-links'."
  9171. (interactive "p")
  9172. (org-insert-all-links arg "" "\n"))
  9173. (defun org-link-fontify-links-to-this-file ()
  9174. "Fontify links to the current file in `org-stored-links'."
  9175. (let ((f (buffer-file-name)) a b)
  9176. (setq a (mapcar (lambda(l)
  9177. (let ((ll (car l)))
  9178. (when (and (string-match "^file:\\(.+\\)::" ll)
  9179. (equal f (expand-file-name (match-string 1 ll))))
  9180. ll)))
  9181. org-stored-links))
  9182. (when (featurep 'org-id)
  9183. (setq b (mapcar (lambda(l)
  9184. (let ((ll (car l)))
  9185. (when (and (string-match "^id:\\(.+\\)$" ll)
  9186. (equal f (expand-file-name
  9187. (or (org-id-find-id-file
  9188. (match-string 1 ll)) ""))))
  9189. ll)))
  9190. org-stored-links)))
  9191. (mapcar (lambda(l)
  9192. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9193. (delq nil (append a b)))))
  9194. (defvar org-link-links-in-this-file nil)
  9195. (defun org-insert-link (&optional complete-file link-location default-description)
  9196. "Insert a link. At the prompt, enter the link.
  9197. Completion can be used to insert any of the link protocol prefixes like
  9198. http or ftp in use.
  9199. The history can be used to select a link previously stored with
  9200. `org-store-link'. When the empty string is entered (i.e. if you just
  9201. press RET at the prompt), the link defaults to the most recently
  9202. stored link. As SPC triggers completion in the minibuffer, you need to
  9203. use M-SPC or C-q SPC to force the insertion of a space character.
  9204. You will also be prompted for a description, and if one is given, it will
  9205. be displayed in the buffer instead of the link.
  9206. If there is already a link at point, this command will allow you to edit link
  9207. and description parts.
  9208. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9209. be selected using completion. The path to the file will be relative to the
  9210. current directory if the file is in the current directory or a subdirectory.
  9211. Otherwise, the link will be the absolute path as completed in the minibuffer
  9212. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9213. option `org-link-file-path-type'.
  9214. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9215. the current directory or below.
  9216. With three \\[universal-argument] prefixes, negate the meaning of
  9217. `org-keep-stored-link-after-insertion'.
  9218. If `org-make-link-description-function' is non-nil, this function will be
  9219. called with the link target, and the result will be the default
  9220. link description.
  9221. If the LINK-LOCATION parameter is non-nil, this value will be
  9222. used as the link location instead of reading one interactively.
  9223. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9224. be used as the default description."
  9225. (interactive "P")
  9226. (let* ((wcf (current-window-configuration))
  9227. (origbuf (current-buffer))
  9228. (region (if (org-region-active-p)
  9229. (buffer-substring (region-beginning) (region-end))))
  9230. (remove (and region (list (region-beginning) (region-end))))
  9231. (desc region)
  9232. tmphist ; byte-compile incorrectly complains about this
  9233. (link link-location)
  9234. (abbrevs org-link-abbrev-alist-local)
  9235. entry file all-prefixes auto-desc)
  9236. (cond
  9237. (link-location) ; specified by arg, just use it.
  9238. ((org-in-regexp org-bracket-link-regexp 1)
  9239. ;; We do have a link at point, and we are going to edit it.
  9240. (setq remove (list (match-beginning 0) (match-end 0)))
  9241. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9242. (setq link (read-string "Link: "
  9243. (org-link-unescape
  9244. (org-match-string-no-properties 1)))))
  9245. ((or (org-in-regexp org-angle-link-re)
  9246. (org-in-regexp org-plain-link-re))
  9247. ;; Convert to bracket link
  9248. (setq remove (list (match-beginning 0) (match-end 0))
  9249. link (read-string "Link: "
  9250. (org-remove-angle-brackets (match-string 0)))))
  9251. ((member complete-file '((4) (16)))
  9252. ;; Completing read for file names.
  9253. (setq link (org-file-complete-link complete-file)))
  9254. (t
  9255. ;; Read link, with completion for stored links.
  9256. (org-link-fontify-links-to-this-file)
  9257. (org-switch-to-buffer-other-window "*Org Links*")
  9258. (with-current-buffer "*Org Links*"
  9259. (erase-buffer)
  9260. (insert "Insert a link.
  9261. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9262. (when org-stored-links
  9263. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9264. (insert (mapconcat 'org-link-prettify
  9265. (reverse org-stored-links) "\n")))
  9266. (goto-char (point-min)))
  9267. (let ((cw (selected-window)))
  9268. (select-window (get-buffer-window "*Org Links*" 'visible))
  9269. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9270. (unless (pos-visible-in-window-p (point-max))
  9271. (org-fit-window-to-buffer))
  9272. (and (window-live-p cw) (select-window cw)))
  9273. ;; Fake a link history, containing the stored links.
  9274. (setq tmphist (append (mapcar 'car org-stored-links)
  9275. org-insert-link-history))
  9276. (setq all-prefixes (append (mapcar 'car abbrevs)
  9277. (mapcar 'car org-link-abbrev-alist)
  9278. org-link-types))
  9279. (unwind-protect
  9280. (progn
  9281. (setq link
  9282. (org-completing-read
  9283. "Link: "
  9284. (append
  9285. (mapcar (lambda (x) (concat x ":"))
  9286. all-prefixes)
  9287. (mapcar 'car org-stored-links))
  9288. nil nil nil
  9289. 'tmphist
  9290. (caar org-stored-links)))
  9291. (if (not (string-match "\\S-" link))
  9292. (user-error "No link selected"))
  9293. (mapc (lambda(l)
  9294. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9295. org-stored-links)
  9296. (if (or (member link all-prefixes)
  9297. (and (equal ":" (substring link -1))
  9298. (member (substring link 0 -1) all-prefixes)
  9299. (setq link (substring link 0 -1))))
  9300. (setq link (with-current-buffer origbuf
  9301. (org-link-try-special-completion link)))))
  9302. (set-window-configuration wcf)
  9303. (kill-buffer "*Org Links*"))
  9304. (setq entry (assoc link org-stored-links))
  9305. (or entry (push link org-insert-link-history))
  9306. (setq desc (or desc (nth 1 entry)))))
  9307. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9308. (not org-keep-stored-link-after-insertion))
  9309. (setq org-stored-links (delq (assoc link org-stored-links)
  9310. org-stored-links)))
  9311. (if (and (string-match org-plain-link-re link)
  9312. (not (string-match org-ts-regexp link)))
  9313. ;; URL-like link, normalize the use of angular brackets.
  9314. (setq link (org-remove-angle-brackets link)))
  9315. ;; Check if we are linking to the current file with a search
  9316. ;; option If yes, simplify the link by using only the search
  9317. ;; option.
  9318. (when (and buffer-file-name
  9319. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9320. (let* ((path (match-string 1 link))
  9321. (case-fold-search nil)
  9322. (search (match-string 2 link)))
  9323. (save-match-data
  9324. (if (equal (file-truename buffer-file-name) (file-truename path))
  9325. ;; We are linking to this same file, with a search option
  9326. (setq link search)))))
  9327. ;; Check if we can/should use a relative path. If yes, simplify the link
  9328. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9329. (let* ((type (match-string 1 link))
  9330. (path (match-string 2 link))
  9331. (origpath path)
  9332. (case-fold-search nil))
  9333. (cond
  9334. ((or (eq org-link-file-path-type 'absolute)
  9335. (equal complete-file '(16)))
  9336. (setq path (abbreviate-file-name (expand-file-name path))))
  9337. ((eq org-link-file-path-type 'noabbrev)
  9338. (setq path (expand-file-name path)))
  9339. ((eq org-link-file-path-type 'relative)
  9340. (setq path (file-relative-name path)))
  9341. (t
  9342. (save-match-data
  9343. (if (string-match (concat "^" (regexp-quote
  9344. (expand-file-name
  9345. (file-name-as-directory
  9346. default-directory))))
  9347. (expand-file-name path))
  9348. ;; We are linking a file with relative path name.
  9349. (setq path (substring (expand-file-name path)
  9350. (match-end 0)))
  9351. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9352. (setq link (concat type path))
  9353. (if (equal desc origpath)
  9354. (setq desc path))))
  9355. (if org-make-link-description-function
  9356. (setq desc
  9357. (or (condition-case nil
  9358. (funcall org-make-link-description-function link desc)
  9359. (error (progn (message "Can't get link description from `%s'"
  9360. (symbol-name org-make-link-description-function))
  9361. (sit-for 2) nil)))
  9362. (read-string "Description: " default-description)))
  9363. (if default-description (setq desc default-description)
  9364. (setq desc (or (and auto-desc desc)
  9365. (read-string "Description: " desc)))))
  9366. (unless (string-match "\\S-" desc) (setq desc nil))
  9367. (if remove (apply 'delete-region remove))
  9368. (insert (org-make-link-string link desc))))
  9369. (defun org-link-try-special-completion (type)
  9370. "If there is completion support for link type TYPE, offer it."
  9371. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9372. (if (functionp fun)
  9373. (funcall fun)
  9374. (read-string "Link (no completion support): " (concat type ":")))))
  9375. (defun org-file-complete-link (&optional arg)
  9376. "Create a file link using completion."
  9377. (let ((file (org-iread-file-name "File: "))
  9378. (pwd (file-name-as-directory (expand-file-name ".")))
  9379. (pwd1 (file-name-as-directory (abbreviate-file-name
  9380. (expand-file-name ".")))))
  9381. (cond ((equal arg '(16))
  9382. (concat "file:"
  9383. (abbreviate-file-name (expand-file-name file))))
  9384. ((string-match
  9385. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9386. (concat "file:" (match-string 1 file)))
  9387. ((string-match
  9388. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9389. (expand-file-name file))
  9390. (concat "file:"
  9391. (match-string 1 (expand-file-name file))))
  9392. (t (concat "file:" file)))))
  9393. (defun org-iread-file-name (&rest args)
  9394. "Read-file-name using `ido-mode' speedup if available.
  9395. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9396. See `read-file-name' for a description of parameters."
  9397. (org-without-partial-completion
  9398. (if (and org-completion-use-ido
  9399. (fboundp 'ido-read-file-name)
  9400. (org-bound-and-true-p ido-mode)
  9401. (listp (nth 1 args)))
  9402. (let ((ido-enter-matching-directory nil))
  9403. (apply #'ido-read-file-name args))
  9404. (apply #'read-file-name args))))
  9405. (defun org-completing-read (&rest args)
  9406. "Completing-read with SPACE being a normal character."
  9407. (let ((enable-recursive-minibuffers t)
  9408. (minibuffer-local-completion-map
  9409. (copy-keymap minibuffer-local-completion-map)))
  9410. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9411. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9412. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9413. (apply 'org-icompleting-read args)))
  9414. (defun org-completing-read-no-i (&rest args)
  9415. (let (org-completion-use-ido org-completion-use-iswitchb)
  9416. (apply 'org-completing-read args)))
  9417. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9418. "Use iswitch as a completing-read replacement to choose from choices.
  9419. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9420. from."
  9421. (let* ((iswitchb-use-virtual-buffers nil)
  9422. (iswitchb-make-buflist-hook
  9423. (lambda ()
  9424. (setq iswitchb-temp-buflist choices))))
  9425. (iswitchb-read-buffer prompt)))
  9426. (defun org-icompleting-read (&rest args)
  9427. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9428. Should be called like `completing-read'."
  9429. (org-without-partial-completion
  9430. (if (not (listp (nth 1 args)))
  9431. ;; Ido only supports lists as the COLLECTION argument. Use
  9432. ;; default completion function when second argument is not
  9433. ;; a list.
  9434. (apply #'completing-read args)
  9435. (let ((ido-enter-matching-directory nil))
  9436. (apply (cond ((and org-completion-use-ido
  9437. (fboundp 'ido-completing-read)
  9438. (org-bound-and-true-p ido-mode))
  9439. #'ido-completing-read)
  9440. ((and org-completion-use-iswitchb
  9441. (org-bound-and-true-p iswitchb-mode))
  9442. #'org-iswitchb-completing-read)
  9443. (t #'completing-read))
  9444. args)))))
  9445. ;;; Opening/following a link
  9446. (defvar org-link-search-failed nil)
  9447. (defvar org-open-link-functions nil
  9448. "Hook for functions finding a plain text link.
  9449. These functions must take a single argument, the link content.
  9450. They will be called for links that look like [[link text][description]]
  9451. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9452. like a filename (e.g. \"./blue.png\").
  9453. These functions will be called *before* Org attempts to resolve the
  9454. link by doing text searches in the current buffer - so if you want a
  9455. link \"[[target]]\" to still find \"<<target>>\", your function should
  9456. handle this as a special case.
  9457. When the function does handle the link, it must return a non-nil value.
  9458. If it decides that it is not responsible for this link, it must return
  9459. nil to indicate that that Org-mode can continue with other options
  9460. like exact and fuzzy text search.")
  9461. (defun org-next-link (&optional search-backward)
  9462. "Move forward to the next link.
  9463. If the link is in hidden text, expose it."
  9464. (interactive "P")
  9465. (when (and org-link-search-failed (eq this-command last-command))
  9466. (goto-char (point-min))
  9467. (message "Link search wrapped back to beginning of buffer"))
  9468. (setq org-link-search-failed nil)
  9469. (let* ((pos (point))
  9470. (ct (org-context))
  9471. (a (assoc :link ct))
  9472. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9473. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9474. ((looking-at org-any-link-re)
  9475. ;; Don't stay stuck at link without an org-link face
  9476. (forward-char (if search-backward -1 1))))
  9477. (if (funcall srch-fun org-any-link-re nil t)
  9478. (progn
  9479. (goto-char (match-beginning 0))
  9480. (if (outline-invisible-p) (org-show-context)))
  9481. (goto-char pos)
  9482. (setq org-link-search-failed t)
  9483. (message "No further link found"))))
  9484. (defun org-previous-link ()
  9485. "Move backward to the previous link.
  9486. If the link is in hidden text, expose it."
  9487. (interactive)
  9488. (funcall 'org-next-link t))
  9489. (defun org-translate-link (s)
  9490. "Translate a link string if a translation function has been defined."
  9491. (with-temp-buffer
  9492. (insert (org-trim s))
  9493. (org-trim (org-element-interpret-data (org-element-context)))))
  9494. (defun org-translate-link-from-planner (type path)
  9495. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9496. This is still an experimental function, your mileage may vary."
  9497. (cond
  9498. ((member type '("http" "https" "news" "ftp"))
  9499. ;; standard Internet links are the same.
  9500. nil)
  9501. ((and (equal type "irc") (string-match "^//" path))
  9502. ;; Planner has two / at the beginning of an irc link, we have 1.
  9503. ;; We should have zero, actually....
  9504. (setq path (substring path 1)))
  9505. ((and (equal type "lisp") (string-match "^/" path))
  9506. ;; Planner has a slash, we do not.
  9507. (setq type "elisp" path (substring path 1)))
  9508. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9509. ;; A typical message link. Planner has the id after the final slash,
  9510. ;; we separate it with a hash mark
  9511. (setq path (concat (match-string 1 path) "#"
  9512. (org-remove-angle-brackets (match-string 2 path))))))
  9513. (cons type path))
  9514. (defun org-find-file-at-mouse (ev)
  9515. "Open file link or URL at mouse."
  9516. (interactive "e")
  9517. (mouse-set-point ev)
  9518. (org-open-at-point 'in-emacs))
  9519. (defun org-open-at-mouse (ev)
  9520. "Open file link or URL at mouse.
  9521. See the docstring of `org-open-file' for details."
  9522. (interactive "e")
  9523. (mouse-set-point ev)
  9524. (if (eq major-mode 'org-agenda-mode)
  9525. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9526. (org-open-at-point))
  9527. (defvar org-window-config-before-follow-link nil
  9528. "The window configuration before following a link.
  9529. This is saved in case the need arises to restore it.")
  9530. ;;;###autoload
  9531. (defun org-open-at-point-global ()
  9532. "Follow a link like Org-mode does.
  9533. This command can be called in any mode to follow a link that has
  9534. Org-mode syntax."
  9535. (interactive)
  9536. (org-run-like-in-org-mode 'org-open-at-point))
  9537. ;;;###autoload
  9538. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9539. "Open a link in the string S, as if it was in Org-mode."
  9540. (interactive "sLink: \nP")
  9541. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9542. (with-temp-buffer
  9543. (let ((org-inhibit-startup (not reference-buffer)))
  9544. (org-mode)
  9545. (insert s)
  9546. (goto-char (point-min))
  9547. (when reference-buffer
  9548. (setq org-link-abbrev-alist-local
  9549. (with-current-buffer reference-buffer
  9550. org-link-abbrev-alist-local)))
  9551. (org-open-at-point arg reference-buffer)))))
  9552. (defvar org-open-at-point-functions nil
  9553. "Hook that is run when following a link at point.
  9554. Functions in this hook must return t if they identify and follow
  9555. a link at point. If they don't find anything interesting at point,
  9556. they must return nil.")
  9557. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9558. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9559. (defun org-open-at-point (&optional arg reference-buffer)
  9560. "Open link, timestamp, footnote or tags at point.
  9561. When point is on a link, follow it. Normally, files will be
  9562. opened by an appropriate application. If the optional prefix
  9563. argument ARG is non-nil, Emacs will visit the file. With
  9564. a double prefix argument, try to open outside of Emacs, in the
  9565. application the system uses for this file type.
  9566. When point is on a timestamp, open the agenda at the day
  9567. specified.
  9568. When point is a footnote definition, move to the first reference
  9569. found. If it is on a reference, move to the associated
  9570. definition.
  9571. When point is on a headline, display a list of every link in the
  9572. entry, so it is possible to pick one, or all, of them. If point
  9573. is on a tag, call `org-tags-view' instead.
  9574. When optional argument REFERENCE-BUFFER is non-nil, it should
  9575. specify a buffer from where the link search should happen. This
  9576. is used internally by `org-open-link-from-string'.
  9577. On top of syntactically correct links, this function will open
  9578. the link at point in comments or comment blocks and the first
  9579. link in a property drawer line."
  9580. (interactive "P")
  9581. ;; On a code block, open block's results.
  9582. (unless (call-interactively 'org-babel-open-src-block-result)
  9583. (org-load-modules-maybe)
  9584. (setq org-window-config-before-follow-link (current-window-configuration))
  9585. (org-remove-occur-highlights nil nil t)
  9586. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9587. (let* ((context
  9588. ;; Only consider supported types, even if they are not
  9589. ;; the closest one.
  9590. (org-element-lineage
  9591. (org-element-context)
  9592. '(clock comment comment-block footnote-definition
  9593. footnote-reference headline inlinetask keyword link
  9594. node-property timestamp)
  9595. t))
  9596. (type (org-element-type context))
  9597. (value (org-element-property :value context)))
  9598. (cond
  9599. ((not context) (user-error "No link found"))
  9600. ;; Exception: open timestamps and links in properties
  9601. ;; drawers, keywords and comments.
  9602. ((memq type '(comment comment-block keyword node-property))
  9603. (cond ((org-in-regexp org-any-link-re)
  9604. (org-open-link-from-string (match-string-no-properties 0)))
  9605. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9606. (org-follow-timestamp-link))
  9607. (t (user-error "No link found"))))
  9608. ;; On a headline or an inlinetask, but not on a timestamp,
  9609. ;; a link, a footnote reference or on tags.
  9610. ((and (memq type '(headline inlinetask))
  9611. ;; Not on tags.
  9612. (progn (save-excursion (beginning-of-line)
  9613. (looking-at org-complex-heading-regexp))
  9614. (or (not (match-beginning 5))
  9615. (< (point) (match-beginning 5)))))
  9616. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9617. (links (car data))
  9618. (links-end (cdr data)))
  9619. (if links
  9620. (dolist (link (if (stringp links) (list links) links))
  9621. (search-forward link nil links-end)
  9622. (goto-char (match-beginning 0))
  9623. (org-open-at-point))
  9624. (require 'org-attach)
  9625. (org-attach-reveal 'if-exists))))
  9626. ;; On a clock line, make sure point is on the timestamp
  9627. ;; before opening it.
  9628. ((and (eq type 'clock)
  9629. value
  9630. (>= (point) (org-element-property :begin value))
  9631. (<= (point) (org-element-property :end value)))
  9632. (org-follow-timestamp-link))
  9633. ;; Do nothing on white spaces after an object, unless point
  9634. ;; is right after it.
  9635. ((> (point)
  9636. (save-excursion
  9637. (goto-char (org-element-property :end context))
  9638. (skip-chars-backward " \t")
  9639. (point)))
  9640. (user-error "No link found"))
  9641. ((eq type 'timestamp) (org-follow-timestamp-link))
  9642. ;; On tags within a headline or an inlinetask.
  9643. ((and (memq type '(headline inlinetask))
  9644. (progn (save-excursion (beginning-of-line)
  9645. (looking-at org-complex-heading-regexp))
  9646. (and (match-beginning 5)
  9647. (>= (point) (match-beginning 5)))))
  9648. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9649. ((eq type 'link)
  9650. ;; When link is located within the description of another
  9651. ;; link (e.g., an inline image), always open the parent
  9652. ;; link.
  9653. (let*((link (let ((up (org-element-property :parent context)))
  9654. (if (eq (org-element-type up) 'link) up context)))
  9655. (type (org-element-property :type link))
  9656. (path (org-link-unescape (org-element-property :path link))))
  9657. ;; Switch back to REFERENCE-BUFFER needed when called in
  9658. ;; a temporary buffer through `org-open-link-from-string'.
  9659. (with-current-buffer (or reference-buffer (current-buffer))
  9660. (cond
  9661. ((equal type "file")
  9662. (if (string-match "[*?{]" (file-name-nondirectory path))
  9663. (dired path)
  9664. ;; Look into `org-link-protocols' in order to find
  9665. ;; a DEDICATED-FUNCTION to open file. The function
  9666. ;; will be applied on raw link instead of parsed
  9667. ;; link due to the limitation in `org-add-link-type'
  9668. ;; ("open" function called with a single argument).
  9669. ;; If no such function is found, fallback to
  9670. ;; `org-open-file'.
  9671. ;;
  9672. ;; Note : "file+emacs" and "file+sys" types are
  9673. ;; hard-coded in order to escape the previous
  9674. ;; limitation.
  9675. (let* ((option (org-element-property :search-option link))
  9676. (app (org-element-property :application link))
  9677. (dedicated-function
  9678. (nth 1 (assoc app org-link-protocols))))
  9679. (if dedicated-function
  9680. (funcall dedicated-function
  9681. (concat path
  9682. (and option (concat "::" option))))
  9683. (apply #'org-open-file
  9684. path
  9685. (cond (arg)
  9686. ((equal app "emacs") 'emacs)
  9687. ((equal app "sys") 'system))
  9688. (cond ((not option) nil)
  9689. ((org-string-match-p "\\`[0-9]+\\'" option)
  9690. (list (string-to-number option)))
  9691. (t (list nil
  9692. (org-link-unescape option)))))))))
  9693. ((assoc type org-link-protocols)
  9694. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9695. ((equal type "help")
  9696. (let ((f-or-v (intern path)))
  9697. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9698. ((boundp f-or-v) (describe-variable f-or-v))
  9699. (t (error "Not a known function or variable")))))
  9700. ((member type '("http" "https" "ftp" "mailto" "news"))
  9701. (browse-url (org-link-escape-browser (concat type ":" path))))
  9702. ((equal type "doi")
  9703. (browse-url
  9704. (org-link-escape-browser (concat org-doi-server-url path))))
  9705. ((equal type "message") (browse-url (concat type ":" path)))
  9706. ((equal type "shell")
  9707. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9708. (cmd path))
  9709. (if (or (and (org-string-nw-p
  9710. org-confirm-shell-link-not-regexp)
  9711. (string-match
  9712. org-confirm-shell-link-not-regexp cmd))
  9713. (not org-confirm-shell-link-function)
  9714. (funcall org-confirm-shell-link-function
  9715. (format "Execute \"%s\" in shell? "
  9716. (org-add-props cmd nil
  9717. 'face 'org-warning))))
  9718. (progn
  9719. (message "Executing %s" cmd)
  9720. (shell-command cmd buf)
  9721. (when (featurep 'midnight)
  9722. (setq clean-buffer-list-kill-buffer-names
  9723. (cons (buffer-name buf)
  9724. clean-buffer-list-kill-buffer-names))))
  9725. (user-error "Abort"))))
  9726. ((equal type "elisp")
  9727. (let ((cmd path))
  9728. (if (or (and (org-string-nw-p
  9729. org-confirm-elisp-link-not-regexp)
  9730. (org-string-match-p
  9731. org-confirm-elisp-link-not-regexp cmd))
  9732. (not org-confirm-elisp-link-function)
  9733. (funcall org-confirm-elisp-link-function
  9734. (format "Execute \"%s\" as elisp? "
  9735. (org-add-props cmd nil
  9736. 'face 'org-warning))))
  9737. (message "%s => %s" cmd
  9738. (if (eq (string-to-char cmd) ?\()
  9739. (eval (read cmd))
  9740. (call-interactively (read cmd))))
  9741. (user-error "Abort"))))
  9742. ((equal type "id")
  9743. (require 'org-id)
  9744. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9745. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9746. (unless (run-hook-with-args-until-success
  9747. 'org-open-link-functions path)
  9748. (if (not arg) (org-mark-ring-push)
  9749. (switch-to-buffer-other-window
  9750. (org-get-buffer-for-internal-link (current-buffer))))
  9751. (let ((destination
  9752. (org-with-wide-buffer
  9753. (if (equal type "radio")
  9754. (org-search-radio-target
  9755. (org-element-property :path link))
  9756. (org-link-search
  9757. (if (member type '("custom-id" "coderef"))
  9758. (org-element-property :raw-link link)
  9759. path)
  9760. ;; Prevent fuzzy links from matching
  9761. ;; themselves.
  9762. (and (equal type "fuzzy")
  9763. (+ 2 (org-element-property :begin link)))))
  9764. (point))))
  9765. (unless (and (<= (point-min) destination)
  9766. (>= (point-max) destination))
  9767. (widen))
  9768. (goto-char destination))))
  9769. (t (browse-url-at-point))))))
  9770. ;; On a footnote reference or at a footnote definition's label.
  9771. ((or (eq type 'footnote-reference)
  9772. (and (eq type 'footnote-definition)
  9773. (save-excursion
  9774. ;; Do not validate action when point is on the
  9775. ;; spaces right after the footnote label, in
  9776. ;; order to be on par with behaviour on links.
  9777. (skip-chars-forward " \t")
  9778. (let ((begin
  9779. (org-element-property :contents-begin context)))
  9780. (if begin (< (point) begin)
  9781. (= (org-element-property :post-affiliated context)
  9782. (line-beginning-position)))))))
  9783. (org-footnote-action))
  9784. (t (user-error "No link found")))))
  9785. (run-hook-with-args 'org-follow-link-hook)))
  9786. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9787. "Offer links in the current entry and return the selected link.
  9788. If there is only one link, return it.
  9789. If NTH is an integer, return the NTH link found.
  9790. If ZERO is a string, check also this string for a link, and if
  9791. there is one, return it."
  9792. (with-current-buffer buffer
  9793. (save-excursion
  9794. (save-restriction
  9795. (widen)
  9796. (goto-char marker)
  9797. (let ((cnt ?0)
  9798. (in-emacs (if (integerp nth) nil nth))
  9799. have-zero end links link c)
  9800. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9801. (push (match-string 0 zero) links)
  9802. (setq cnt (1- cnt) have-zero t))
  9803. (save-excursion
  9804. (org-back-to-heading t)
  9805. (setq end (save-excursion (outline-next-heading) (point)))
  9806. (while (re-search-forward org-any-link-re end t)
  9807. (push (match-string 0) links))
  9808. (setq links (org-uniquify (reverse links))))
  9809. (cond
  9810. ((null links)
  9811. (message "No links"))
  9812. ((equal (length links) 1)
  9813. (setq link (car links)))
  9814. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9815. (setq link (nth (if have-zero nth (1- nth)) links)))
  9816. (t ; we have to select a link
  9817. (save-excursion
  9818. (save-window-excursion
  9819. (delete-other-windows)
  9820. (with-output-to-temp-buffer "*Select Link*"
  9821. (mapc (lambda (l)
  9822. (if (not (string-match org-bracket-link-regexp l))
  9823. (princ (format "[%c] %s\n" (incf cnt)
  9824. (org-remove-angle-brackets l)))
  9825. (if (match-end 3)
  9826. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9827. (match-string 3 l) (match-string 1 l)))
  9828. (princ (format "[%c] %s\n" (incf cnt)
  9829. (match-string 1 l))))))
  9830. links))
  9831. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9832. (message "Select link to open, RET to open all:")
  9833. (setq c (read-char-exclusive))
  9834. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9835. (when (equal c ?q) (user-error "Abort"))
  9836. (if (equal c ?\C-m)
  9837. (setq link links)
  9838. (setq nth (- c ?0))
  9839. (if have-zero (setq nth (1+ nth)))
  9840. (unless (and (integerp nth) (>= (length links) nth))
  9841. (user-error "Invalid link selection"))
  9842. (setq link (nth (1- nth) links)))))
  9843. (cons link end))))))
  9844. ;; TODO: These functions are deprecated since `org-open-at-point'
  9845. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9846. (defun org-open-file-with-system (path)
  9847. "Open file at PATH using the system way of opening it."
  9848. (org-open-file path 'system))
  9849. (defun org-open-file-with-emacs (path)
  9850. "Open file at PATH in Emacs."
  9851. (org-open-file path 'emacs))
  9852. ;;; File search
  9853. (defvar org-create-file-search-functions nil
  9854. "List of functions to construct the right search string for a file link.
  9855. These functions are called in turn with point at the location to
  9856. which the link should point.
  9857. A function in the hook should first test if it would like to
  9858. handle this file type, for example by checking the `major-mode'
  9859. or the file extension. If it decides not to handle this file, it
  9860. should just return nil to give other functions a chance. If it
  9861. does handle the file, it must return the search string to be used
  9862. when following the link. The search string will be part of the
  9863. file link, given after a double colon, and `org-open-at-point'
  9864. will automatically search for it. If special measures must be
  9865. taken to make the search successful, another function should be
  9866. added to the companion hook `org-execute-file-search-functions',
  9867. which see.
  9868. A function in this hook may also use `setq' to set the variable
  9869. `description' to provide a suggestion for the descriptive text to
  9870. be used for this link when it gets inserted into an Org-mode
  9871. buffer with \\[org-insert-link].")
  9872. (defvar org-execute-file-search-functions nil
  9873. "List of functions to execute a file search triggered by a link.
  9874. Functions added to this hook must accept a single argument, the
  9875. search string that was part of the file link, the part after the
  9876. double colon. The function must first check if it would like to
  9877. handle this search, for example by checking the `major-mode' or
  9878. the file extension. If it decides not to handle this search, it
  9879. should just return nil to give other functions a chance. If it
  9880. does handle the search, it must return a non-nil value to keep
  9881. other functions from trying.
  9882. Each function can access the current prefix argument through the
  9883. variable `current-prefix-arg'. Note that a single prefix is used
  9884. to force opening a link in Emacs, so it may be good to only use a
  9885. numeric or double prefix to guide the search function.
  9886. In case this is needed, a function in this hook can also restore
  9887. the window configuration before `org-open-at-point' was called using:
  9888. (set-window-configuration org-window-config-before-follow-link)")
  9889. (defun org-search-radio-target (target)
  9890. "Search a radio target matching TARGET in current buffer.
  9891. White spaces are not significant."
  9892. (let ((re (format "<<<%s>>>"
  9893. (mapconcat #'regexp-quote
  9894. (org-split-string target "[ \t\n]+")
  9895. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9896. (origin (point)))
  9897. (goto-char (point-min))
  9898. (catch :radio-match
  9899. (while (re-search-forward re nil t)
  9900. (backward-char)
  9901. (let ((object (org-element-context)))
  9902. (when (eq (org-element-type object) 'radio-target)
  9903. (goto-char (org-element-property :begin object))
  9904. (org-show-context 'link-search)
  9905. (throw :radio-match nil))))
  9906. (goto-char origin)
  9907. (user-error "No match for radio target: %s" target))))
  9908. (defun org-link-search (s &optional avoid-pos stealth)
  9909. "Search for a search string S.
  9910. If S starts with \"#\", it triggers a custom ID search.
  9911. If S is enclosed within parenthesis, it initiates a coderef
  9912. search.
  9913. If S is surrounded by forward slashes, it is interpreted as
  9914. a regular expression. In Org mode files, this will create an
  9915. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9916. to list matches. If the current buffer is in `dired-mode', grep
  9917. will be used to search in all files.
  9918. When AVOID-POS is given, ignore matches near that position.
  9919. When optional argument STEALTH is non-nil, do not modify
  9920. visibility around point, thus ignoring `org-show-context-detail'
  9921. variable.
  9922. Search is case-insensitive and ignores white spaces. Return type
  9923. of matched result, with is either `dedicated' or `fuzzy'."
  9924. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9925. (let* ((case-fold-search t)
  9926. (origin (point))
  9927. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9928. (words (org-split-string s "[ \t\n]+"))
  9929. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9930. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9931. type)
  9932. (cond
  9933. ;; Check if there are any special search functions.
  9934. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9935. ((eq (string-to-char s) ?#)
  9936. ;; Look for a custom ID S if S starts with "#".
  9937. (let* ((id (substring normalized 1))
  9938. (match (org-find-property "CUSTOM_ID" id)))
  9939. (if match (progn (goto-char match) (setf type 'dedicated))
  9940. (error "No match for custom ID: %s" id))))
  9941. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9942. ;; Look for coderef targets if S is enclosed within parenthesis.
  9943. (let ((coderef (match-string-no-properties 1 normalized))
  9944. (re (substring s-single-re 1 -1)))
  9945. (goto-char (point-min))
  9946. (catch :coderef-match
  9947. (while (re-search-forward re nil t)
  9948. (let ((element (org-element-at-point)))
  9949. (when (and (memq (org-element-type element)
  9950. '(example-block src-block))
  9951. ;; Build proper regexp according to current
  9952. ;; block's label format.
  9953. (let ((label-fmt
  9954. (regexp-quote
  9955. (or (org-element-property :label-fmt element)
  9956. org-coderef-label-format))))
  9957. (save-excursion
  9958. (beginning-of-line)
  9959. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9960. (format label-fmt coderef))))))
  9961. (setq type 'dedicated)
  9962. (goto-char (match-beginning 1))
  9963. (throw :coderef-match nil))))
  9964. (goto-char origin)
  9965. (error "No match for coderef: %s" coderef))))
  9966. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9967. ;; Look for a regular expression.
  9968. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9969. (match-string 1 s)))
  9970. ;; Fuzzy links.
  9971. (t
  9972. (let* ((starred (eq (string-to-char normalized) ?*))
  9973. (headline-search (and (derived-mode-p 'org-mode)
  9974. (or org-link-search-must-match-exact-headline
  9975. starred))))
  9976. (cond
  9977. ;; Look for targets, only if not in a headline search.
  9978. ((and (not starred)
  9979. (let ((target (format "<<%s>>" s-multi-re)))
  9980. (catch :target-match
  9981. (goto-char (point-min))
  9982. (while (re-search-forward target nil t)
  9983. (backward-char)
  9984. (let ((context (org-element-context)))
  9985. (when (eq (org-element-type context) 'target)
  9986. (setq type 'dedicated)
  9987. (goto-char (org-element-property :begin context))
  9988. (throw :target-match t))))
  9989. nil))))
  9990. ;; Look for elements named after S, only if not in a headline
  9991. ;; search.
  9992. ((and (not starred)
  9993. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9994. (catch :name-match
  9995. (goto-char (point-min))
  9996. (while (re-search-forward name nil t)
  9997. (let ((element (org-element-at-point)))
  9998. (when (equal (org-split-string
  9999. (org-element-property :name element)
  10000. "[ \t]+")
  10001. words)
  10002. (setq type 'dedicated)
  10003. (beginning-of-line)
  10004. (throw :name-match t))))
  10005. nil))))
  10006. ;; Regular text search. Prefer headlines in Org mode
  10007. ;; buffers.
  10008. ((and (derived-mode-p 'org-mode)
  10009. (let* ((wspace "[ \t]")
  10010. (wspaceopt (concat wspace "*"))
  10011. (cookie (concat "\\(?:"
  10012. wspaceopt
  10013. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10014. wspaceopt
  10015. "\\)"))
  10016. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10017. (re (concat
  10018. org-outline-regexp-bol
  10019. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10020. "\\(?:\\[#.\\][ \t]+\\)?"
  10021. "\\(?:" org-comment-string "[ \t]+\\)?"
  10022. sep "?"
  10023. (let ((title (mapconcat #'regexp-quote
  10024. words
  10025. sep)))
  10026. (if starred (substring title 1) title))
  10027. sep "?"
  10028. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10029. "[ \t]*$")))
  10030. (goto-char (point-min))
  10031. (re-search-forward re nil t)))
  10032. (goto-char (match-beginning 0))
  10033. (setq type 'dedicated))
  10034. ;; Offer to create non-existent headline depending on
  10035. ;; `org-link-search-must-match-exact-headline'.
  10036. ((and (derived-mode-p 'org-mode)
  10037. (not org-link-search-inhibit-query)
  10038. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10039. (yes-or-no-p "No match - create this as a new heading? "))
  10040. (goto-char (point-max))
  10041. (unless (bolp) (newline))
  10042. (org-insert-heading nil t t)
  10043. (insert s "\n")
  10044. (beginning-of-line 0))
  10045. ;; Only headlines are looked after. No need to process
  10046. ;; further: throw an error.
  10047. ((and (derived-mode-p 'org-mode)
  10048. (or starred org-link-search-must-match-exact-headline))
  10049. (goto-char origin)
  10050. (error "No match for fuzzy expression: %s" normalized))
  10051. ;; Regular text search.
  10052. ((catch :fuzzy-match
  10053. (goto-char (point-min))
  10054. (while (re-search-forward s-multi-re nil t)
  10055. ;; Skip match if it contains AVOID-POS or it is included
  10056. ;; in a link with a description but outside the
  10057. ;; description.
  10058. (unless (or (and avoid-pos
  10059. (<= (match-beginning 0) avoid-pos)
  10060. (> (match-end 0) avoid-pos))
  10061. (and (save-match-data
  10062. (org-in-regexp org-bracket-link-regexp))
  10063. (match-beginning 3)
  10064. (or (> (match-beginning 3) (point))
  10065. (<= (match-end 3) (point)))
  10066. (org-element-lineage
  10067. (save-match-data (org-element-context))
  10068. '(link) t)))
  10069. (goto-char (match-beginning 0))
  10070. (setq type 'fuzzy)
  10071. (throw :fuzzy-match t)))
  10072. nil))
  10073. ;; All failed. Throw an error.
  10074. (t (goto-char origin)
  10075. (error "No match for fuzzy expression: %s" normalized))))))
  10076. ;; Disclose surroundings of match, if appropriate.
  10077. (when (and (derived-mode-p 'org-mode) (not stealth))
  10078. (org-show-context 'link-search))
  10079. type))
  10080. (defun org-get-buffer-for-internal-link (buffer)
  10081. "Return a buffer to be used for displaying the link target of internal links."
  10082. (cond
  10083. ((not org-display-internal-link-with-indirect-buffer)
  10084. buffer)
  10085. ((string-match "(Clone)$" (buffer-name buffer))
  10086. (message "Buffer is already a clone, not making another one")
  10087. ;; we also do not modify visibility in this case
  10088. buffer)
  10089. (t ; make a new indirect buffer for displaying the link
  10090. (let* ((bn (buffer-name buffer))
  10091. (ibn (concat bn "(Clone)"))
  10092. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10093. (with-current-buffer ib (org-overview))
  10094. ib))))
  10095. (defun org-do-occur (regexp &optional cleanup)
  10096. "Call the Emacs command `occur'.
  10097. If CLEANUP is non-nil, remove the printout of the regular expression
  10098. in the *Occur* buffer. This is useful if the regex is long and not useful
  10099. to read."
  10100. (occur regexp)
  10101. (when cleanup
  10102. (let ((cwin (selected-window)) win beg end)
  10103. (when (setq win (get-buffer-window "*Occur*"))
  10104. (select-window win))
  10105. (goto-char (point-min))
  10106. (when (re-search-forward "match[a-z]+" nil t)
  10107. (setq beg (match-end 0))
  10108. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10109. (setq end (1- (match-beginning 0)))))
  10110. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10111. (goto-char (point-min))
  10112. (select-window cwin))))
  10113. ;;; The mark ring for links jumps
  10114. (defvar org-mark-ring nil
  10115. "Mark ring for positions before jumps in Org-mode.")
  10116. (defvar org-mark-ring-last-goto nil
  10117. "Last position in the mark ring used to go back.")
  10118. ;; Fill and close the ring
  10119. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10120. (loop for i from 1 to org-mark-ring-length do
  10121. (push (make-marker) org-mark-ring))
  10122. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10123. org-mark-ring)
  10124. (defun org-mark-ring-push (&optional pos buffer)
  10125. "Put the current position or POS into the mark ring and rotate it."
  10126. (interactive)
  10127. (setq pos (or pos (point)))
  10128. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10129. (move-marker (car org-mark-ring)
  10130. (or pos (point))
  10131. (or buffer (current-buffer)))
  10132. (message "%s"
  10133. (substitute-command-keys
  10134. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10135. (defun org-mark-ring-goto (&optional n)
  10136. "Jump to the previous position in the mark ring.
  10137. With prefix arg N, jump back that many stored positions. When
  10138. called several times in succession, walk through the entire ring.
  10139. Org-mode commands jumping to a different position in the current file,
  10140. or to another Org-mode file, automatically push the old position
  10141. onto the ring."
  10142. (interactive "p")
  10143. (let (p m)
  10144. (if (eq last-command this-command)
  10145. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10146. (setq p org-mark-ring))
  10147. (setq org-mark-ring-last-goto p)
  10148. (setq m (car p))
  10149. (org-pop-to-buffer-same-window (marker-buffer m))
  10150. (goto-char m)
  10151. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10152. (defun org-remove-angle-brackets (s)
  10153. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10154. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10155. s)
  10156. (defun org-add-angle-brackets (s)
  10157. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10158. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10159. s)
  10160. (defun org-remove-double-quotes (s)
  10161. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10162. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10163. s)
  10164. ;;; Following specific links
  10165. (defun org-follow-timestamp-link ()
  10166. "Open an agenda view for the time-stamp date/range at point."
  10167. (cond
  10168. ((org-at-date-range-p t)
  10169. (let ((org-agenda-start-on-weekday)
  10170. (t1 (match-string 1))
  10171. (t2 (match-string 2)) tt1 tt2)
  10172. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10173. tt2 (time-to-days (org-time-string-to-time t2)))
  10174. (let ((org-agenda-buffer-tmp-name
  10175. (format "*Org Agenda(a:%s)"
  10176. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10177. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10178. ((org-at-timestamp-p t)
  10179. (let ((org-agenda-buffer-tmp-name
  10180. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10181. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10182. (substring (match-string 1) 0 10)))
  10183. 1)))
  10184. (t (error "This should not happen"))))
  10185. ;;; Following file links
  10186. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10187. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10188. (declare-function mailcap-mime-info
  10189. "mailcap" (string &optional request no-decode))
  10190. (defvar org-wait nil)
  10191. (defun org-open-file (path &optional in-emacs line search)
  10192. "Open the file at PATH.
  10193. First, this expands any special file name abbreviations. Then the
  10194. configuration variable `org-file-apps' is checked if it contains an
  10195. entry for this file type, and if yes, the corresponding command is launched.
  10196. If no application is found, Emacs simply visits the file.
  10197. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10198. With a double \\[universal-argument] \\[universal-argument] \
  10199. prefix arg, Org tries to avoid opening in Emacs
  10200. and to use an external application to visit the file.
  10201. Optional LINE specifies a line to go to, optional SEARCH a string
  10202. to search for. If LINE or SEARCH is given, the file will be
  10203. opened in Emacs, unless an entry from org-file-apps that makes
  10204. use of groups in a regexp matches.
  10205. If you want to change the way frames are used when following a
  10206. link, please customize `org-link-frame-setup'.
  10207. If the file does not exist, an error is thrown."
  10208. (let* ((file (if (equal path "")
  10209. buffer-file-name
  10210. (substitute-in-file-name (expand-file-name path))))
  10211. (file-apps (append org-file-apps (org-default-apps)))
  10212. (apps (org-remove-if
  10213. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10214. (apps-dlink (org-remove-if-not
  10215. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10216. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10217. (dirp (if remp nil (file-directory-p file)))
  10218. (file (if (and dirp org-open-directory-means-index-dot-org)
  10219. (concat (file-name-as-directory file) "index.org")
  10220. file))
  10221. (a-m-a-p (assq 'auto-mode apps))
  10222. (dfile (downcase file))
  10223. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10224. (link (cond ((and (eq line nil)
  10225. (eq search nil))
  10226. file)
  10227. (line
  10228. (concat file "::" (number-to-string line)))
  10229. (search
  10230. (concat file "::" search))))
  10231. (dlink (downcase link))
  10232. (old-buffer (current-buffer))
  10233. (old-pos (point))
  10234. (old-mode major-mode)
  10235. ext cmd link-match-data)
  10236. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10237. (setq ext (match-string 1 dfile))
  10238. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10239. (setq ext (match-string 1 dfile))))
  10240. (cond
  10241. ((member in-emacs '((16) system))
  10242. (setq cmd (cdr (assoc 'system apps))))
  10243. (in-emacs (setq cmd 'emacs))
  10244. (t
  10245. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10246. (and dirp (cdr (assoc 'directory apps)))
  10247. ; first, try matching against apps-dlink
  10248. ; if we get a match here, store the match data for later
  10249. (let ((match (assoc-default dlink apps-dlink
  10250. 'string-match)))
  10251. (if match
  10252. (progn (setq link-match-data (match-data))
  10253. match)
  10254. (progn (setq in-emacs (or in-emacs line search))
  10255. nil))) ; if we have no match in apps-dlink,
  10256. ; always open the file in emacs if line or search
  10257. ; is given (for backwards compatibility)
  10258. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10259. 'string-match)
  10260. (cdr (assoc ext apps))
  10261. (cdr (assoc t apps))))))
  10262. (when (eq cmd 'system)
  10263. (setq cmd (cdr (assoc 'system apps))))
  10264. (when (eq cmd 'default)
  10265. (setq cmd (cdr (assoc t apps))))
  10266. (when (eq cmd 'mailcap)
  10267. (require 'mailcap)
  10268. (mailcap-parse-mailcaps)
  10269. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10270. (command (mailcap-mime-info mime-type)))
  10271. (if (stringp command)
  10272. (setq cmd command)
  10273. (setq cmd 'emacs))))
  10274. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10275. (not (file-exists-p file))
  10276. (not org-open-non-existing-files))
  10277. (user-error "No such file: %s" file))
  10278. (cond
  10279. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10280. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10281. (while (string-match "['\"]%s['\"]" cmd)
  10282. (setq cmd (replace-match "%s" t t cmd)))
  10283. (while (string-match "%s" cmd)
  10284. (setq cmd (replace-match
  10285. (save-match-data
  10286. (shell-quote-argument
  10287. (convert-standard-filename file)))
  10288. t t cmd)))
  10289. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10290. (save-match-data
  10291. (let ((match-index 1)
  10292. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10293. (set-match-data link-match-data)
  10294. (while (<= match-index number-of-groups)
  10295. (let ((regex (concat "%" (number-to-string match-index)))
  10296. (replace-with (match-string match-index dlink)))
  10297. (while (string-match regex cmd)
  10298. (setq cmd (replace-match replace-with t t cmd))))
  10299. (setq match-index (+ match-index 1)))))
  10300. (save-window-excursion
  10301. (message "Running %s...done" cmd)
  10302. (start-process-shell-command cmd nil cmd)
  10303. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10304. ((or (stringp cmd)
  10305. (eq cmd 'emacs))
  10306. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10307. (widen)
  10308. (if line (progn (org-goto-line line)
  10309. (if (derived-mode-p 'org-mode)
  10310. (org-reveal)))
  10311. (if search (org-link-search search))))
  10312. ((consp cmd)
  10313. (let ((file (convert-standard-filename file)))
  10314. (save-match-data
  10315. (set-match-data link-match-data)
  10316. (eval cmd))))
  10317. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10318. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10319. (or (not (equal old-buffer (current-buffer)))
  10320. (not (equal old-pos (point))))
  10321. (org-mark-ring-push old-pos old-buffer))))
  10322. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10323. "This function returns non-nil if `entry' uses a regular
  10324. expression which should be matched against the whole link by
  10325. org-open-file.
  10326. It assumes that is the case when the entry uses a regular
  10327. expression which has at least one grouping construct and the
  10328. action is either a lisp form or a command string containing
  10329. '%1', i.e. using at least one subexpression match as a
  10330. parameter."
  10331. (let ((selector (car entry))
  10332. (action (cdr entry)))
  10333. (if (stringp selector)
  10334. (and (> (regexp-opt-depth selector) 0)
  10335. (or (and (stringp action)
  10336. (string-match "%[0-9]" action))
  10337. (consp action)))
  10338. nil)))
  10339. (defun org-default-apps ()
  10340. "Return the default applications for this operating system."
  10341. (cond
  10342. ((eq system-type 'darwin)
  10343. org-file-apps-defaults-macosx)
  10344. ((eq system-type 'windows-nt)
  10345. org-file-apps-defaults-windowsnt)
  10346. (t org-file-apps-defaults-gnu)))
  10347. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10348. "Convert extensions to regular expressions in the cars of LIST.
  10349. Also, weed out any non-string entries, because the return value is used
  10350. only for regexp matching.
  10351. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10352. point to the symbol `emacs', indicating that the file should
  10353. be opened in Emacs."
  10354. (append
  10355. (delq nil
  10356. (mapcar (lambda (x)
  10357. (if (not (stringp (car x)))
  10358. nil
  10359. (if (string-match "\\W" (car x))
  10360. x
  10361. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10362. list))
  10363. (if add-auto-mode
  10364. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10365. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10366. (defun org-file-remote-p (file)
  10367. "Test whether FILE specifies a location on a remote system.
  10368. Return non-nil if the location is indeed remote.
  10369. For example, the filename \"/user@host:/foo\" specifies a location
  10370. on the system \"/user@host:\"."
  10371. (cond ((fboundp 'file-remote-p)
  10372. (file-remote-p file))
  10373. ((fboundp 'tramp-handle-file-remote-p)
  10374. (tramp-handle-file-remote-p file))
  10375. ((and (boundp 'ange-ftp-name-format)
  10376. (string-match (car ange-ftp-name-format) file))
  10377. t)))
  10378. ;;;; Refiling
  10379. (defun org-get-org-file ()
  10380. "Read a filename, with default directory `org-directory'."
  10381. (let ((default (or org-default-notes-file remember-data-file)))
  10382. (read-file-name (format "File name [%s]: " default)
  10383. (file-name-as-directory org-directory)
  10384. default)))
  10385. (defun org-notes-order-reversed-p ()
  10386. "Check if the current file should receive notes in reversed order."
  10387. (cond
  10388. ((not org-reverse-note-order) nil)
  10389. ((eq t org-reverse-note-order) t)
  10390. ((not (listp org-reverse-note-order)) nil)
  10391. (t (catch 'exit
  10392. (dolist (entry org-reverse-note-order)
  10393. (if (string-match (car entry) buffer-file-name)
  10394. (throw 'exit (cdr entry))))))))
  10395. (defvar org-refile-target-table nil
  10396. "The list of refile targets, created by `org-refile'.")
  10397. (defvar org-agenda-new-buffers nil
  10398. "Buffers created to visit agenda files.")
  10399. (defvar org-refile-cache nil
  10400. "Cache for refile targets.")
  10401. (defvar org-refile-markers nil
  10402. "All the markers used for caching refile locations.")
  10403. (defun org-refile-marker (pos)
  10404. "Get a new refile marker, but only if caching is in use."
  10405. (if (not org-refile-use-cache)
  10406. pos
  10407. (let ((m (make-marker)))
  10408. (move-marker m pos)
  10409. (push m org-refile-markers)
  10410. m)))
  10411. (defun org-refile-cache-clear ()
  10412. "Clear the refile cache and disable all the markers."
  10413. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10414. (setq org-refile-markers nil)
  10415. (setq org-refile-cache nil)
  10416. (message "Refile cache has been cleared"))
  10417. (defun org-refile-cache-check-set (set)
  10418. "Check if all the markers in the cache still have live buffers."
  10419. (let (marker)
  10420. (catch 'exit
  10421. (while (and set (setq marker (nth 3 (pop set))))
  10422. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10423. (when (and marker (null (marker-buffer marker)))
  10424. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10425. (sit-for 3)
  10426. (throw 'exit nil)))
  10427. t)))
  10428. (defun org-refile-cache-put (set &rest identifiers)
  10429. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10430. (let* ((key (sha1 (prin1-to-string identifiers)))
  10431. (entry (assoc key org-refile-cache)))
  10432. (if entry
  10433. (setcdr entry set)
  10434. (push (cons key set) org-refile-cache))))
  10435. (defun org-refile-cache-get (&rest identifiers)
  10436. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10437. (cond
  10438. ((not org-refile-cache) nil)
  10439. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10440. (t
  10441. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10442. org-refile-cache))))
  10443. (and set (org-refile-cache-check-set set) set)))))
  10444. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10445. "Produce a table with refile targets."
  10446. (let ((case-fold-search nil)
  10447. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10448. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10449. targets tgs txt re files desc descre fast-path-p level pos0)
  10450. (message "Getting targets...")
  10451. (with-current-buffer (or default-buffer (current-buffer))
  10452. (dolist (entry entries)
  10453. (setq files (car entry) desc (cdr entry))
  10454. (setq fast-path-p nil)
  10455. (cond
  10456. ((null files) (setq files (list (current-buffer))))
  10457. ((eq files 'org-agenda-files)
  10458. (setq files (org-agenda-files 'unrestricted)))
  10459. ((and (symbolp files) (fboundp files))
  10460. (setq files (funcall files)))
  10461. ((and (symbolp files) (boundp files))
  10462. (setq files (symbol-value files))))
  10463. (if (stringp files) (setq files (list files)))
  10464. (cond
  10465. ((eq (car desc) :tag)
  10466. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10467. ((eq (car desc) :todo)
  10468. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10469. ((eq (car desc) :regexp)
  10470. (setq descre (cdr desc)))
  10471. ((eq (car desc) :level)
  10472. (setq descre (concat "^\\*\\{" (number-to-string
  10473. (if org-odd-levels-only
  10474. (1- (* 2 (cdr desc)))
  10475. (cdr desc)))
  10476. "\\}[ \t]")))
  10477. ((eq (car desc) :maxlevel)
  10478. (setq fast-path-p t)
  10479. (setq descre (concat "^\\*\\{1," (number-to-string
  10480. (if org-odd-levels-only
  10481. (1- (* 2 (cdr desc)))
  10482. (cdr desc)))
  10483. "\\}[ \t]")))
  10484. (t (error "Bad refiling target description %s" desc)))
  10485. (dolist (f files)
  10486. (with-current-buffer
  10487. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10488. (or
  10489. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10490. (progn
  10491. (if (bufferp f) (setq f (buffer-file-name
  10492. (buffer-base-buffer f))))
  10493. (setq f (and f (expand-file-name f)))
  10494. (if (eq org-refile-use-outline-path 'file)
  10495. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10496. (save-excursion
  10497. (save-restriction
  10498. (widen)
  10499. (goto-char (point-min))
  10500. (while (re-search-forward descre nil t)
  10501. (goto-char (setq pos0 (point-at-bol)))
  10502. (catch 'next
  10503. (when org-refile-target-verify-function
  10504. (save-match-data
  10505. (or (funcall org-refile-target-verify-function)
  10506. (throw 'next t))))
  10507. (when (and (looking-at org-complex-heading-regexp)
  10508. (not (member (match-string 4) excluded-entries))
  10509. (match-string 4))
  10510. (setq level (org-reduced-level
  10511. (- (match-end 1) (match-beginning 1)))
  10512. txt (org-link-display-format (match-string 4))
  10513. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10514. re (format org-complex-heading-regexp-format
  10515. (regexp-quote (match-string 4))))
  10516. (when org-refile-use-outline-path
  10517. (setq txt (mapconcat
  10518. 'org-protect-slash
  10519. (append
  10520. (if (eq org-refile-use-outline-path
  10521. 'file)
  10522. (list (file-name-nondirectory
  10523. (buffer-file-name
  10524. (buffer-base-buffer))))
  10525. (if (eq org-refile-use-outline-path
  10526. 'full-file-path)
  10527. (list (buffer-file-name
  10528. (buffer-base-buffer)))))
  10529. (org-get-outline-path fast-path-p
  10530. level txt)
  10531. (list txt))
  10532. "/")))
  10533. (push (list txt f re (org-refile-marker (point)))
  10534. tgs)))
  10535. (when (= (point) pos0)
  10536. ;; verification function has not moved point
  10537. (goto-char (point-at-eol))))))))
  10538. (when org-refile-use-cache
  10539. (org-refile-cache-put tgs (buffer-file-name) descre))
  10540. (setq targets (append tgs targets))))))
  10541. (message "Getting targets...done")
  10542. (nreverse targets)))
  10543. (defun org-protect-slash (s)
  10544. (while (string-match "/" s)
  10545. (setq s (replace-match "\\" t t s)))
  10546. s)
  10547. (defvar org-olpa (make-vector 20 nil))
  10548. (defun org-get-outline-path (&optional fastp level heading)
  10549. "Return the outline path to the current entry, as a list.
  10550. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10551. routine which makes outline path derivations for an entire file,
  10552. avoiding backtracing. Refile target collection makes use of that."
  10553. (if fastp
  10554. (progn
  10555. (if (> level 19)
  10556. (error "Outline path failure, more than 19 levels"))
  10557. (loop for i from level upto 19 do
  10558. (aset org-olpa i nil))
  10559. (prog1
  10560. (delq nil (append org-olpa nil))
  10561. (aset org-olpa level heading)))
  10562. (let (rtn case-fold-search)
  10563. (save-excursion
  10564. (save-restriction
  10565. (widen)
  10566. (while (org-up-heading-safe)
  10567. (when (looking-at org-complex-heading-regexp)
  10568. (push (org-trim
  10569. (replace-regexp-in-string
  10570. ;; Remove statistical/checkboxes cookies
  10571. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10572. (org-match-string-no-properties 4)))
  10573. rtn)))
  10574. rtn)))))
  10575. (defun org-format-outline-path (path &optional width prefix separator)
  10576. "Format the outline path PATH for display.
  10577. WIDTH is the maximum number of characters that is available.
  10578. PREFIX is a prefix to be included in the returned string,
  10579. such as the file name.
  10580. SEPARATOR is inserted between the different parts of the path,
  10581. the default is \"/\"."
  10582. (setq width (or width 79))
  10583. (if prefix (setq width (- width (length prefix))))
  10584. (if (not path)
  10585. (or prefix "")
  10586. (let* ((nsteps (length path))
  10587. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10588. (maxwidth (if (<= total-width width)
  10589. 10000 ;; everything fits
  10590. ;; we need to shorten the level headings
  10591. (/ (- width nsteps) nsteps)))
  10592. (org-odd-levels-only nil)
  10593. (n 0)
  10594. (total (1+ (length prefix))))
  10595. (setq maxwidth (max maxwidth 10))
  10596. (concat prefix
  10597. (if prefix (or separator "/"))
  10598. (mapconcat
  10599. (lambda (h)
  10600. (setq n (1+ n))
  10601. (if (and (= n nsteps) (< maxwidth 10000))
  10602. (setq maxwidth (- total-width total)))
  10603. (if (< (length h) maxwidth)
  10604. (progn (setq total (+ total (length h) 1)) h)
  10605. (setq h (substring h 0 (- maxwidth 2))
  10606. total (+ total maxwidth 1))
  10607. (if (string-match "[ \t]+\\'" h)
  10608. (setq h (substring h 0 (match-beginning 0))))
  10609. (setq h (concat h "..")))
  10610. (org-add-props h nil 'face
  10611. (nth (% (1- n) org-n-level-faces)
  10612. org-level-faces))
  10613. h)
  10614. path (or separator "/"))))))
  10615. (defun org-display-outline-path (&optional file current separator just-return-string)
  10616. "Display the current outline path in the echo area.
  10617. If FILE is non-nil, prepend the output with the file name.
  10618. If CURRENT is non-nil, append the current heading to the output.
  10619. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10620. the different parts of the path and defaults to \"/\".
  10621. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10622. (interactive "P")
  10623. (let* (case-fold-search
  10624. (bfn (buffer-file-name (buffer-base-buffer)))
  10625. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10626. res)
  10627. (if current (setq path (append path
  10628. (save-excursion
  10629. (org-back-to-heading t)
  10630. (if (looking-at org-complex-heading-regexp)
  10631. (list (match-string 4)))))))
  10632. (setq res
  10633. (org-format-outline-path
  10634. path
  10635. (1- (frame-width))
  10636. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10637. separator))
  10638. (if just-return-string
  10639. (org-no-properties res)
  10640. (org-unlogged-message "%s" res))))
  10641. (defvar org-refile-history nil
  10642. "History for refiling operations.")
  10643. (defvar org-after-refile-insert-hook nil
  10644. "Hook run after `org-refile' has inserted its stuff at the new location.
  10645. Note that this is still *before* the stuff will be removed from
  10646. the *old* location.")
  10647. (defvar org-capture-last-stored-marker)
  10648. (defvar org-refile-keep nil
  10649. "Non-nil means `org-refile' will copy instead of refile.")
  10650. (defun org-copy ()
  10651. "Like `org-refile', but copy."
  10652. (interactive)
  10653. (let ((org-refile-keep t))
  10654. (funcall 'org-refile nil nil nil "Copy")))
  10655. (defun org-refile (&optional arg default-buffer rfloc msg)
  10656. "Move the entry or entries at point to another heading.
  10657. The list of target headings is compiled using the information in
  10658. `org-refile-targets', which see.
  10659. At the target location, the entry is filed as a subitem of the
  10660. target heading. Depending on `org-reverse-note-order', the new
  10661. subitem will either be the first or the last subitem.
  10662. If there is an active region, all entries in that region will be
  10663. refiled. However, the region must fulfill the requirement that
  10664. the first heading sets the top-level of the moved text.
  10665. With prefix arg ARG, the command will only visit the target
  10666. location and not actually move anything.
  10667. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10668. refiling operation has put the subtree.
  10669. With a numeric prefix argument of `2', refile to the running clock.
  10670. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10671. being set to t and copy to the target location, don't move it.
  10672. Beware that keeping refiled entries may result in duplicated ID
  10673. properties.
  10674. RFLOC can be a refile location obtained in a different way.
  10675. MSG is a string to replace \"Refile\" in the default prompt with
  10676. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10677. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10678. If you are using target caching (see `org-refile-use-cache'), you
  10679. have to clear the target cache in order to find new targets.
  10680. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10681. prefix argument (`C-u C-u C-u C-c C-w')."
  10682. (interactive "P")
  10683. (if (member arg '(0 (64)))
  10684. (org-refile-cache-clear)
  10685. (let* ((actionmsg (cond (msg msg)
  10686. ((equal arg 3) "Refile (and keep)")
  10687. (t "Refile")))
  10688. (cbuf (current-buffer))
  10689. (regionp (org-region-active-p))
  10690. (region-start (and regionp (region-beginning)))
  10691. (region-end (and regionp (region-end)))
  10692. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10693. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10694. pos it nbuf file re level reversed)
  10695. (setq last-command nil)
  10696. (when regionp
  10697. (goto-char region-start)
  10698. (or (bolp) (goto-char (point-at-bol)))
  10699. (setq region-start (point))
  10700. (unless (or (org-kill-is-subtree-p
  10701. (buffer-substring region-start region-end))
  10702. (prog1 org-refile-active-region-within-subtree
  10703. (let ((s (point-at-eol)))
  10704. (org-toggle-heading)
  10705. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10706. (user-error "The region is not a (sequence of) subtree(s)")))
  10707. (if (equal arg '(16))
  10708. (org-refile-goto-last-stored)
  10709. (when (or
  10710. (and (equal arg 2)
  10711. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10712. (prog1
  10713. (setq it (list (or org-clock-heading "running clock")
  10714. (buffer-file-name
  10715. (marker-buffer org-clock-hd-marker))
  10716. ""
  10717. (marker-position org-clock-hd-marker)))
  10718. (setq arg nil)))
  10719. (setq it (or rfloc
  10720. (let (heading-text)
  10721. (save-excursion
  10722. (unless (and arg (listp arg))
  10723. (org-back-to-heading t)
  10724. (setq heading-text
  10725. (replace-regexp-in-string
  10726. org-bracket-link-regexp
  10727. "\\3"
  10728. (nth 4 (org-heading-components)))))
  10729. (org-refile-get-location
  10730. (cond ((and arg (listp arg)) "Goto")
  10731. (regionp (concat actionmsg " region to"))
  10732. (t (concat actionmsg " subtree \""
  10733. heading-text "\" to")))
  10734. default-buffer
  10735. (and (not (equal '(4) arg))
  10736. org-refile-allow-creating-parent-nodes)
  10737. arg))))))
  10738. (setq file (nth 1 it)
  10739. re (nth 2 it)
  10740. pos (nth 3 it))
  10741. (if (and (not arg)
  10742. pos
  10743. (equal (buffer-file-name) file)
  10744. (if regionp
  10745. (and (>= pos region-start)
  10746. (<= pos region-end))
  10747. (and (>= pos (point))
  10748. (< pos (save-excursion
  10749. (org-end-of-subtree t t))))))
  10750. (error "Cannot refile to position inside the tree or region"))
  10751. (setq nbuf (or (find-buffer-visiting file)
  10752. (find-file-noselect file)))
  10753. (if (and arg (not (equal arg 3)))
  10754. (progn
  10755. (org-pop-to-buffer-same-window nbuf)
  10756. (goto-char pos)
  10757. (org-show-context 'org-goto))
  10758. (if regionp
  10759. (progn
  10760. (org-kill-new (buffer-substring region-start region-end))
  10761. (org-save-markers-in-region region-start region-end))
  10762. (org-copy-subtree 1 nil t))
  10763. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10764. (find-file-noselect file)))
  10765. (setq reversed (org-notes-order-reversed-p))
  10766. (save-excursion
  10767. (save-restriction
  10768. (widen)
  10769. (if pos
  10770. (progn
  10771. (goto-char pos)
  10772. (looking-at org-outline-regexp)
  10773. (setq level (org-get-valid-level (funcall outline-level) 1))
  10774. (goto-char
  10775. (if reversed
  10776. (or (outline-next-heading) (point-max))
  10777. (or (save-excursion (org-get-next-sibling))
  10778. (org-end-of-subtree t t)
  10779. (point-max)))))
  10780. (setq level 1)
  10781. (if (not reversed)
  10782. (goto-char (point-max))
  10783. (goto-char (point-min))
  10784. (or (outline-next-heading) (goto-char (point-max)))))
  10785. (if (not (bolp)) (newline))
  10786. (org-paste-subtree level nil nil t)
  10787. (when org-log-refile
  10788. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10789. (unless (eq org-log-refile 'note)
  10790. (save-excursion (org-add-log-note))))
  10791. (and org-auto-align-tags
  10792. (let ((org-loop-over-headlines-in-active-region nil))
  10793. (org-set-tags nil t)))
  10794. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10795. :last-refile)))
  10796. (when bookmark-name
  10797. (with-demoted-errors
  10798. (bookmark-set bookmark-name))))
  10799. ;; If we are refiling for capture, make sure that the
  10800. ;; last-capture pointers point here
  10801. (when (org-bound-and-true-p org-refile-for-capture)
  10802. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10803. :last-capture-marker)))
  10804. (when bookmark-name
  10805. (with-demoted-errors
  10806. (bookmark-set bookmark-name))))
  10807. (move-marker org-capture-last-stored-marker (point)))
  10808. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10809. (run-hooks 'org-after-refile-insert-hook))))
  10810. (unless org-refile-keep
  10811. (if regionp
  10812. (delete-region (point) (+ (point) (- region-end region-start)))
  10813. (delete-region
  10814. (and (org-back-to-heading t) (point))
  10815. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10816. (when (featurep 'org-inlinetask)
  10817. (org-inlinetask-remove-END-maybe))
  10818. (setq org-markers-to-move nil)
  10819. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10820. (defun org-refile-goto-last-stored ()
  10821. "Go to the location where the last refile was stored."
  10822. (interactive)
  10823. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10824. (message "This is the location of the last refile"))
  10825. (defun org-refile--get-location (refloc tbl)
  10826. "When user refile to REFLOC, find the associated target in TBL.
  10827. Also check `org-refile-target-table'."
  10828. (car (delq
  10829. nil
  10830. (mapcar
  10831. (lambda (r) (or (assoc r tbl)
  10832. (assoc r org-refile-target-table)))
  10833. (list (replace-regexp-in-string "/$" "" refloc)
  10834. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10835. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10836. no-exclude)
  10837. "Prompt the user for a refile location, using PROMPT.
  10838. PROMPT should not be suffixed with a colon and a space, because
  10839. this function appends the default value from
  10840. `org-refile-history' automatically, if that is not empty.
  10841. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10842. this is used for the GOTO interface."
  10843. (let ((org-refile-targets org-refile-targets)
  10844. (org-refile-use-outline-path org-refile-use-outline-path)
  10845. excluded-entries)
  10846. (when (and (derived-mode-p 'org-mode)
  10847. (not org-refile-use-cache)
  10848. (not no-exclude))
  10849. (org-map-tree
  10850. (lambda()
  10851. (setq excluded-entries
  10852. (append excluded-entries (list (org-get-heading t t)))))))
  10853. (setq org-refile-target-table
  10854. (org-refile-get-targets default-buffer excluded-entries)))
  10855. (unless org-refile-target-table
  10856. (user-error "No refile targets"))
  10857. (let* ((cbuf (current-buffer))
  10858. (partial-completion-mode nil)
  10859. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10860. (cfunc (if (and org-refile-use-outline-path
  10861. org-outline-path-complete-in-steps)
  10862. 'org-olpath-completing-read
  10863. 'org-icompleting-read))
  10864. (extra (if org-refile-use-outline-path "/" ""))
  10865. (cbnex (concat (buffer-name) extra))
  10866. (filename (and cfn (expand-file-name cfn)))
  10867. (tbl (mapcar
  10868. (lambda (x)
  10869. (if (and (not (member org-refile-use-outline-path
  10870. '(file full-file-path)))
  10871. (not (equal filename (nth 1 x))))
  10872. (cons (concat (car x) extra " ("
  10873. (file-name-nondirectory (nth 1 x)) ")")
  10874. (cdr x))
  10875. (cons (concat (car x) extra) (cdr x))))
  10876. org-refile-target-table))
  10877. (completion-ignore-case t)
  10878. cdef
  10879. (prompt (concat prompt
  10880. (or (and (car org-refile-history)
  10881. (concat " (default " (car org-refile-history) ")"))
  10882. (and (assoc cbnex tbl) (setq cdef cbnex)
  10883. (concat " (default " cbnex ")"))) ": "))
  10884. pa answ parent-target child parent old-hist)
  10885. (setq old-hist org-refile-history)
  10886. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10887. nil 'org-refile-history (or cdef (car org-refile-history))))
  10888. (if (setq pa (org-refile--get-location answ tbl))
  10889. (progn
  10890. (org-refile-check-position pa)
  10891. (when (or (not org-refile-history)
  10892. (not (eq old-hist org-refile-history))
  10893. (not (equal (car pa) (car org-refile-history))))
  10894. (setq org-refile-history
  10895. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10896. org-refile-history
  10897. (cdr org-refile-history))))
  10898. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10899. (pop org-refile-history)))
  10900. pa)
  10901. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10902. (progn
  10903. (setq parent (match-string 1 answ)
  10904. child (match-string 2 answ))
  10905. (setq parent-target (org-refile--get-location parent tbl))
  10906. (when (and parent-target
  10907. (or (eq new-nodes t)
  10908. (and (eq new-nodes 'confirm)
  10909. (y-or-n-p (format "Create new node \"%s\"? "
  10910. child)))))
  10911. (org-refile-new-child parent-target child)))
  10912. (user-error "Invalid target location")))))
  10913. (declare-function org-string-nw-p "org-macs" (s))
  10914. (defun org-refile-check-position (refile-pointer)
  10915. "Check if the refile pointer matches the headline to which it points."
  10916. (let* ((file (nth 1 refile-pointer))
  10917. (re (nth 2 refile-pointer))
  10918. (pos (nth 3 refile-pointer))
  10919. buffer)
  10920. (if (and (not (markerp pos)) (not file))
  10921. (user-error "Please indicate a target file in the refile path")
  10922. (when (org-string-nw-p re)
  10923. (setq buffer (if (markerp pos)
  10924. (marker-buffer pos)
  10925. (or (find-buffer-visiting file)
  10926. (find-file-noselect file))))
  10927. (with-current-buffer buffer
  10928. (save-excursion
  10929. (save-restriction
  10930. (widen)
  10931. (goto-char pos)
  10932. (beginning-of-line 1)
  10933. (unless (org-looking-at-p re)
  10934. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10935. (defun org-refile-new-child (parent-target child)
  10936. "Use refile target PARENT-TARGET to add new CHILD below it."
  10937. (unless parent-target
  10938. (error "Cannot find parent for new node"))
  10939. (let ((file (nth 1 parent-target))
  10940. (pos (nth 3 parent-target))
  10941. level)
  10942. (with-current-buffer (or (find-buffer-visiting file)
  10943. (find-file-noselect file))
  10944. (save-excursion
  10945. (save-restriction
  10946. (widen)
  10947. (if pos
  10948. (goto-char pos)
  10949. (goto-char (point-max))
  10950. (if (not (bolp)) (newline)))
  10951. (when (looking-at org-outline-regexp)
  10952. (setq level (funcall outline-level))
  10953. (org-end-of-subtree t t))
  10954. (org-back-over-empty-lines)
  10955. (insert "\n" (make-string
  10956. (if pos (org-get-valid-level level 1) 1) ?*)
  10957. " " child "\n")
  10958. (beginning-of-line 0)
  10959. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10960. (defun org-olpath-completing-read (prompt collection &rest args)
  10961. "Read an outline path like a file name."
  10962. (let ((thetable collection)
  10963. (org-completion-use-ido nil) ; does not work with ido.
  10964. (org-completion-use-iswitchb nil)) ; or iswitchb
  10965. (apply #'org-icompleting-read
  10966. prompt
  10967. (lambda (string predicate &optional flag)
  10968. (cond
  10969. ((eq flag nil) (try-completion string thetable))
  10970. ((eq flag t)
  10971. (let ((l (length string)))
  10972. (mapcar (lambda (x)
  10973. (let ((r (substring x l))
  10974. (f (if (string-match " ([^)]*)$" x)
  10975. (match-string 0 x)
  10976. "")))
  10977. (if (string-match "/" r)
  10978. (concat string (substring r 0 (match-end 0)) f)
  10979. x)))
  10980. (all-completions string thetable predicate))))
  10981. ;; Exact match?
  10982. ((eq flag 'lambda) (assoc string thetable))))
  10983. args)))
  10984. ;;;; Dynamic blocks
  10985. (defun org-find-dblock (name)
  10986. "Find the first dynamic block with name NAME in the buffer.
  10987. If not found, stay at current position and return nil."
  10988. (let ((case-fold-search t) pos)
  10989. (save-excursion
  10990. (goto-char (point-min))
  10991. (setq pos (and (re-search-forward
  10992. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10993. (match-beginning 0))))
  10994. (if pos (goto-char pos))
  10995. pos))
  10996. (defun org-create-dblock (plist)
  10997. "Create a dynamic block section, with parameters taken from PLIST.
  10998. PLIST must contain a :name entry which is used as the name of the block."
  10999. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11000. (end-of-line 1)
  11001. (newline))
  11002. (let ((col (current-column))
  11003. (name (plist-get plist :name)))
  11004. (insert "#+BEGIN: " name)
  11005. (while plist
  11006. (if (eq (car plist) :name)
  11007. (setq plist (cddr plist))
  11008. (insert " " (prin1-to-string (pop plist)))))
  11009. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11010. (beginning-of-line -2)))
  11011. (defun org-prepare-dblock ()
  11012. "Prepare dynamic block for refresh.
  11013. This empties the block, puts the cursor at the insert position and returns
  11014. the property list including an extra property :name with the block name."
  11015. (unless (looking-at org-dblock-start-re)
  11016. (user-error "Not at a dynamic block"))
  11017. (let* ((begdel (1+ (match-end 0)))
  11018. (name (org-no-properties (match-string 1)))
  11019. (params (append (list :name name)
  11020. (read (concat "(" (match-string 3) ")")))))
  11021. (save-excursion
  11022. (beginning-of-line 1)
  11023. (skip-chars-forward " \t")
  11024. (setq params (plist-put params :indentation-column (current-column))))
  11025. (unless (re-search-forward org-dblock-end-re nil t)
  11026. (error "Dynamic block not terminated"))
  11027. (setq params
  11028. (append params
  11029. (list :content (buffer-substring
  11030. begdel (match-beginning 0)))))
  11031. (delete-region begdel (match-beginning 0))
  11032. (goto-char begdel)
  11033. (open-line 1)
  11034. params))
  11035. (defun org-map-dblocks (&optional command)
  11036. "Apply COMMAND to all dynamic blocks in the current buffer.
  11037. If COMMAND is not given, use `org-update-dblock'."
  11038. (let ((cmd (or command 'org-update-dblock)))
  11039. (save-excursion
  11040. (goto-char (point-min))
  11041. (while (re-search-forward org-dblock-start-re nil t)
  11042. (goto-char (match-beginning 0))
  11043. (save-excursion
  11044. (condition-case nil
  11045. (funcall cmd)
  11046. (error (message "Error during update of dynamic block"))))
  11047. (unless (re-search-forward org-dblock-end-re nil t)
  11048. (error "Dynamic block not terminated"))))))
  11049. (defun org-dblock-update (&optional arg)
  11050. "User command for updating dynamic blocks.
  11051. Update the dynamic block at point. With prefix ARG, update all dynamic
  11052. blocks in the buffer."
  11053. (interactive "P")
  11054. (if arg
  11055. (org-update-all-dblocks)
  11056. (or (looking-at org-dblock-start-re)
  11057. (org-beginning-of-dblock))
  11058. (org-update-dblock)))
  11059. (defun org-update-dblock ()
  11060. "Update the dynamic block at point.
  11061. This means to empty the block, parse for parameters and then call
  11062. the correct writing function."
  11063. (interactive)
  11064. (save-excursion
  11065. (let* ((win (selected-window))
  11066. (pos (point))
  11067. (line (org-current-line))
  11068. (params (org-prepare-dblock))
  11069. (name (plist-get params :name))
  11070. (indent (plist-get params :indentation-column))
  11071. (cmd (intern (concat "org-dblock-write:" name))))
  11072. (message "Updating dynamic block `%s' at line %d..." name line)
  11073. (funcall cmd params)
  11074. (message "Updating dynamic block `%s' at line %d...done" name line)
  11075. (goto-char pos)
  11076. (when (and indent (> indent 0))
  11077. (setq indent (make-string indent ?\ ))
  11078. (save-excursion
  11079. (select-window win)
  11080. (org-beginning-of-dblock)
  11081. (forward-line 1)
  11082. (while (not (looking-at org-dblock-end-re))
  11083. (insert indent)
  11084. (beginning-of-line 2))
  11085. (when (looking-at org-dblock-end-re)
  11086. (and (looking-at "[ \t]+")
  11087. (replace-match ""))
  11088. (insert indent)))))))
  11089. (defun org-beginning-of-dblock ()
  11090. "Find the beginning of the dynamic block at point.
  11091. Error if there is no such block at point."
  11092. (let ((pos (point))
  11093. beg)
  11094. (end-of-line 1)
  11095. (if (and (re-search-backward org-dblock-start-re nil t)
  11096. (setq beg (match-beginning 0))
  11097. (re-search-forward org-dblock-end-re nil t)
  11098. (> (match-end 0) pos))
  11099. (goto-char beg)
  11100. (goto-char pos)
  11101. (error "Not in a dynamic block"))))
  11102. (defun org-update-all-dblocks ()
  11103. "Update all dynamic blocks in the buffer.
  11104. This function can be used in a hook."
  11105. (interactive)
  11106. (when (derived-mode-p 'org-mode)
  11107. (org-map-dblocks 'org-update-dblock)))
  11108. ;;;; Completion
  11109. (declare-function org-export-backend-name "org-export" (cl-x))
  11110. (declare-function org-export-backend-options "org-export" (cl-x))
  11111. (defun org-get-export-keywords ()
  11112. "Return a list of all currently understood export keywords.
  11113. Export keywords include options, block names, attributes and
  11114. keywords relative to each registered export back-end."
  11115. (let (keywords)
  11116. (dolist (backend
  11117. (org-bound-and-true-p org-export-registered-backends)
  11118. (delq nil keywords))
  11119. ;; Back-end name (for keywords, like #+LATEX:)
  11120. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11121. (dolist (option-entry (org-export-backend-options backend))
  11122. ;; Back-end options.
  11123. (push (nth 1 option-entry) keywords)))))
  11124. (defconst org-options-keywords
  11125. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11126. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11127. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11128. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11129. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11130. (defcustom org-structure-template-alist
  11131. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11132. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11133. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11134. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11135. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11136. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11137. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11138. ("L" "#+LaTeX: ")
  11139. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11140. ("H" "#+HTML: ")
  11141. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11142. ("A" "#+ASCII: ")
  11143. ("i" "#+INDEX: ?")
  11144. ("I" "#+INCLUDE: %file ?"))
  11145. "Structure completion elements.
  11146. This is a list of abbreviation keys and values. The value gets inserted
  11147. if you type `<' followed by the key and then press the completion key,
  11148. usually `TAB'. %file will be replaced by a file name after prompting
  11149. for the file using completion. The cursor will be placed at the position
  11150. of the `?' in the template.
  11151. There are two templates for each key, the first uses the original Org syntax,
  11152. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11153. the default when the /org-mtags.el/ module has been loaded. See also the
  11154. variable `org-mtags-prefer-muse-templates'."
  11155. :group 'org-completion
  11156. :type '(repeat
  11157. (list
  11158. (string :tag "Key")
  11159. (string :tag "Template")))
  11160. :version "25.1"
  11161. :package-version '(Org . "8.3"))
  11162. (defun org-try-structure-completion ()
  11163. "Try to complete a structure template before point.
  11164. This looks for strings like \"<e\" on an otherwise empty line and
  11165. expands them."
  11166. (let ((l (buffer-substring (point-at-bol) (point)))
  11167. a)
  11168. (when (and (looking-at "[ \t]*$")
  11169. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11170. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11171. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11172. (match-beginning 1)) a)
  11173. t)))
  11174. (defun org-complete-expand-structure-template (start cell)
  11175. "Expand a structure template."
  11176. (let ((rpl (nth 1 cell))
  11177. (ind ""))
  11178. (delete-region start (point))
  11179. (when (string-match "\\`[ \t]*#\\+" rpl)
  11180. (cond
  11181. ((bolp))
  11182. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11183. (setq ind (buffer-substring (point-at-bol) (point))))
  11184. (t (newline))))
  11185. (setq start (point))
  11186. (if (string-match "%file" rpl)
  11187. (setq rpl (replace-match
  11188. (concat
  11189. "\""
  11190. (save-match-data
  11191. (abbreviate-file-name (read-file-name "Include file: ")))
  11192. "\"")
  11193. t t rpl)))
  11194. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11195. (concat "\n" ind)))
  11196. (insert rpl)
  11197. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11198. ;;;; TODO, DEADLINE, Comments
  11199. (defun org-toggle-comment ()
  11200. "Change the COMMENT state of an entry."
  11201. (interactive)
  11202. (save-excursion
  11203. (org-back-to-heading)
  11204. (looking-at org-complex-heading-regexp)
  11205. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11206. (skip-chars-forward " \t")
  11207. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11208. (if (org-in-commented-heading-p t)
  11209. (delete-region (point)
  11210. (progn (search-forward " " (line-end-position) 'move)
  11211. (skip-chars-forward " \t")
  11212. (point)))
  11213. (insert org-comment-string)
  11214. (unless (eolp) (insert " ")))))
  11215. (defvar org-last-todo-state-is-todo nil
  11216. "This is non-nil when the last TODO state change led to a TODO state.
  11217. If the last change removed the TODO tag or switched to DONE, then
  11218. this is nil.")
  11219. (defvar org-setting-tags nil) ; dynamically skipped
  11220. (defvar org-todo-setup-filter-hook nil
  11221. "Hook for functions that pre-filter todo specs.
  11222. Each function takes a todo spec and returns either nil or the spec
  11223. transformed into canonical form." )
  11224. (defvar org-todo-get-default-hook nil
  11225. "Hook for functions that get a default item for todo.
  11226. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11227. nil or a string to be used for the todo mark." )
  11228. (defvar org-agenda-headline-snapshot-before-repeat)
  11229. (defun org-current-effective-time ()
  11230. "Return current time adjusted for `org-extend-today-until' variable."
  11231. (let* ((ct (org-current-time))
  11232. (dct (decode-time ct))
  11233. (ct1
  11234. (cond
  11235. (org-use-last-clock-out-time-as-effective-time
  11236. (or (org-clock-get-last-clock-out-time) ct))
  11237. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11238. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11239. (t ct))))
  11240. ct1))
  11241. (defun org-todo-yesterday (&optional arg)
  11242. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11243. (interactive "P")
  11244. (if (eq major-mode 'org-agenda-mode)
  11245. (apply 'org-agenda-todo-yesterday arg)
  11246. (let* ((org-use-effective-time t)
  11247. (hour (third (decode-time
  11248. (org-current-time))))
  11249. (org-extend-today-until (1+ hour)))
  11250. (org-todo arg))))
  11251. (defvar org-block-entry-blocking ""
  11252. "First entry preventing the TODO state change.")
  11253. (defun org-cancel-repeater ()
  11254. "Cancel a repeater by setting its numeric value to zero."
  11255. (interactive)
  11256. (save-excursion
  11257. (org-back-to-heading t)
  11258. (let ((bound1 (point))
  11259. (bound0 (save-excursion (outline-next-heading) (point))))
  11260. (when (re-search-forward
  11261. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11262. org-deadline-time-regexp "\\)\\|\\("
  11263. org-ts-regexp "\\)")
  11264. bound0 t)
  11265. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11266. (replace-match "0" t nil nil 1))))))
  11267. (defun org-todo (&optional arg)
  11268. "Change the TODO state of an item.
  11269. The state of an item is given by a keyword at the start of the heading,
  11270. like
  11271. *** TODO Write paper
  11272. *** DONE Call mom
  11273. The different keywords are specified in the variable `org-todo-keywords'.
  11274. By default the available states are \"TODO\" and \"DONE\".
  11275. So for this example: when the item starts with TODO, it is changed to DONE.
  11276. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11277. DONE are present, add TODO at the beginning of the heading.
  11278. With \\[universal-argument] prefix arg, use completion to determine the new \
  11279. state.
  11280. With numeric prefix arg, switch to that state.
  11281. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11282. keywords (nextset).
  11283. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11284. With a numeric prefix arg of 0, inhibit note taking for the change.
  11285. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11286. When called through ELisp, arg is also interpreted in the following way:
  11287. 'none -> empty state
  11288. \"\"(empty string) -> switch to empty state
  11289. 'done -> switch to DONE
  11290. 'nextset -> switch to the next set of keywords
  11291. 'previousset -> switch to the previous set of keywords
  11292. \"WAITING\" -> switch to the specified keyword, but only if it
  11293. really is a member of `org-todo-keywords'."
  11294. (interactive "P")
  11295. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11296. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11297. 'region-start-level 'region))
  11298. org-loop-over-headlines-in-active-region)
  11299. (org-map-entries
  11300. `(org-todo ,arg)
  11301. org-loop-over-headlines-in-active-region
  11302. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11303. (if (equal arg '(16)) (setq arg 'nextset))
  11304. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11305. (let ((org-blocker-hook org-blocker-hook)
  11306. commentp
  11307. case-fold-search)
  11308. (when (equal arg '(64))
  11309. (setq arg nil org-blocker-hook nil))
  11310. (when (and org-blocker-hook
  11311. (or org-inhibit-blocking
  11312. (org-entry-get nil "NOBLOCKING")))
  11313. (setq org-blocker-hook nil))
  11314. (save-excursion
  11315. (catch 'exit
  11316. (org-back-to-heading t)
  11317. (when (org-in-commented-heading-p t)
  11318. (org-toggle-comment)
  11319. (setq commentp t))
  11320. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11321. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11322. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11323. (let* ((match-data (match-data))
  11324. (startpos (point-at-bol))
  11325. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11326. (org-log-done org-log-done)
  11327. (org-log-repeat org-log-repeat)
  11328. (org-todo-log-states org-todo-log-states)
  11329. (org-inhibit-logging
  11330. (if (equal arg 0)
  11331. (progn (setq arg nil) 'note) org-inhibit-logging))
  11332. (this (match-string 1))
  11333. (hl-pos (match-beginning 0))
  11334. (head (org-get-todo-sequence-head this))
  11335. (ass (assoc head org-todo-kwd-alist))
  11336. (interpret (nth 1 ass))
  11337. (done-word (nth 3 ass))
  11338. (final-done-word (nth 4 ass))
  11339. (org-last-state (or this ""))
  11340. (completion-ignore-case t)
  11341. (member (member this org-todo-keywords-1))
  11342. (tail (cdr member))
  11343. (org-state (cond
  11344. ((and org-todo-key-trigger
  11345. (or (and (equal arg '(4))
  11346. (eq org-use-fast-todo-selection 'prefix))
  11347. (and (not arg) org-use-fast-todo-selection
  11348. (not (eq org-use-fast-todo-selection
  11349. 'prefix)))))
  11350. ;; Use fast selection
  11351. (org-fast-todo-selection))
  11352. ((and (equal arg '(4))
  11353. (or (not org-use-fast-todo-selection)
  11354. (not org-todo-key-trigger)))
  11355. ;; Read a state with completion
  11356. (org-icompleting-read
  11357. "State: " (mapcar 'list org-todo-keywords-1)
  11358. nil t))
  11359. ((eq arg 'right)
  11360. (if this
  11361. (if tail (car tail) nil)
  11362. (car org-todo-keywords-1)))
  11363. ((eq arg 'left)
  11364. (if (equal member org-todo-keywords-1)
  11365. nil
  11366. (if this
  11367. (nth (- (length org-todo-keywords-1)
  11368. (length tail) 2)
  11369. org-todo-keywords-1)
  11370. (org-last org-todo-keywords-1))))
  11371. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11372. (setq arg nil))) ; hack to fall back to cycling
  11373. (arg
  11374. ;; user or caller requests a specific state
  11375. (cond
  11376. ((equal arg "") nil)
  11377. ((eq arg 'none) nil)
  11378. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11379. ((eq arg 'nextset)
  11380. (or (car (cdr (member head org-todo-heads)))
  11381. (car org-todo-heads)))
  11382. ((eq arg 'previousset)
  11383. (let ((org-todo-heads (reverse org-todo-heads)))
  11384. (or (car (cdr (member head org-todo-heads)))
  11385. (car org-todo-heads))))
  11386. ((car (member arg org-todo-keywords-1)))
  11387. ((stringp arg)
  11388. (user-error "State `%s' not valid in this file" arg))
  11389. ((nth (1- (prefix-numeric-value arg))
  11390. org-todo-keywords-1))))
  11391. ((null member) (or head (car org-todo-keywords-1)))
  11392. ((equal this final-done-word) nil) ;; -> make empty
  11393. ((null tail) nil) ;; -> first entry
  11394. ((memq interpret '(type priority))
  11395. (if (eq this-command last-command)
  11396. (car tail)
  11397. (if (> (length tail) 0)
  11398. (or done-word (car org-done-keywords))
  11399. nil)))
  11400. (t
  11401. (car tail))))
  11402. (org-state (or
  11403. (run-hook-with-args-until-success
  11404. 'org-todo-get-default-hook org-state org-last-state)
  11405. org-state))
  11406. (next (if org-state (concat " " org-state " ") " "))
  11407. (change-plist (list :type 'todo-state-change :from this :to org-state
  11408. :position startpos))
  11409. dolog now-done-p)
  11410. (when org-blocker-hook
  11411. (setq org-last-todo-state-is-todo
  11412. (not (member this org-done-keywords)))
  11413. (unless (save-excursion
  11414. (save-match-data
  11415. (org-with-wide-buffer
  11416. (run-hook-with-args-until-failure
  11417. 'org-blocker-hook change-plist))))
  11418. (if (org-called-interactively-p 'interactive)
  11419. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11420. this org-state org-block-entry-blocking)
  11421. ;; fail silently
  11422. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11423. this org-state org-block-entry-blocking)
  11424. (throw 'exit nil))))
  11425. (store-match-data match-data)
  11426. (replace-match next t t)
  11427. (cond ((equal this org-state)
  11428. (message "TODO state was already %s" (org-trim next)))
  11429. ((pos-visible-in-window-p hl-pos)
  11430. (message "TODO state changed to %s" (org-trim next))))
  11431. (unless head
  11432. (setq head (org-get-todo-sequence-head org-state)
  11433. ass (assoc head org-todo-kwd-alist)
  11434. interpret (nth 1 ass)
  11435. done-word (nth 3 ass)
  11436. final-done-word (nth 4 ass)))
  11437. (when (memq arg '(nextset previousset))
  11438. (message "Keyword-Set %d/%d: %s"
  11439. (- (length org-todo-sets) -1
  11440. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11441. (length org-todo-sets)
  11442. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11443. (setq org-last-todo-state-is-todo
  11444. (not (member org-state org-done-keywords)))
  11445. (setq now-done-p (and (member org-state org-done-keywords)
  11446. (not (member this org-done-keywords))))
  11447. (and logging (org-local-logging logging))
  11448. (when (and (or org-todo-log-states org-log-done)
  11449. (not (eq org-inhibit-logging t))
  11450. (not (memq arg '(nextset previousset))))
  11451. ;; we need to look at recording a time and note
  11452. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11453. (nth 2 (assoc this org-todo-log-states))))
  11454. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11455. (setq dolog 'time))
  11456. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11457. (and org-state
  11458. (member org-state org-not-done-keywords)
  11459. (not (member this org-not-done-keywords))))
  11460. ;; This is now a todo state and was not one before
  11461. ;; If there was a CLOSED time stamp, get rid of it.
  11462. (org-add-planning-info nil nil 'closed))
  11463. (when (and now-done-p org-log-done)
  11464. ;; It is now done, and it was not done before
  11465. (org-add-planning-info 'closed (org-current-effective-time))
  11466. (if (and (not dolog) (eq 'note org-log-done))
  11467. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11468. (when (and org-state dolog)
  11469. ;; This is a non-nil state, and we need to log it
  11470. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11471. ;; Fixup tag positioning
  11472. (org-todo-trigger-tag-changes org-state)
  11473. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11474. (when org-provide-todo-statistics
  11475. (org-update-parent-todo-statistics))
  11476. (run-hooks 'org-after-todo-state-change-hook)
  11477. (if (and arg (not (member org-state org-done-keywords)))
  11478. (setq head (org-get-todo-sequence-head org-state)))
  11479. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11480. ;; Do we need to trigger a repeat?
  11481. (when now-done-p
  11482. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11483. ;; This is for the agenda, take a snapshot of the headline.
  11484. (save-match-data
  11485. (setq org-agenda-headline-snapshot-before-repeat
  11486. (org-get-heading))))
  11487. (org-auto-repeat-maybe org-state))
  11488. ;; Fixup cursor location if close to the keyword
  11489. (if (and (outline-on-heading-p)
  11490. (not (bolp))
  11491. (save-excursion (beginning-of-line 1)
  11492. (looking-at org-todo-line-regexp))
  11493. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11494. (progn
  11495. (goto-char (or (match-end 2) (match-end 1)))
  11496. (and (looking-at " ") (just-one-space))))
  11497. (when org-trigger-hook
  11498. (save-excursion
  11499. (run-hook-with-args 'org-trigger-hook change-plist)))
  11500. (when commentp (org-toggle-comment))))))))
  11501. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11502. "Block turning an entry into a TODO, using the hierarchy.
  11503. This checks whether the current task should be blocked from state
  11504. changes. Such blocking occurs when:
  11505. 1. The task has children which are not all in a completed state.
  11506. 2. A task has a parent with the property :ORDERED:, and there
  11507. are siblings prior to the current task with incomplete
  11508. status.
  11509. 3. The parent of the task is blocked because it has siblings that should
  11510. be done first, or is child of a block grandparent TODO entry."
  11511. (if (not org-enforce-todo-dependencies)
  11512. t ; if locally turned off don't block
  11513. (catch 'dont-block
  11514. ;; If this is not a todo state change, or if this entry is already DONE,
  11515. ;; do not block
  11516. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11517. (member (plist-get change-plist :from)
  11518. (cons 'done org-done-keywords))
  11519. (member (plist-get change-plist :to)
  11520. (cons 'todo org-not-done-keywords))
  11521. (not (plist-get change-plist :to)))
  11522. (throw 'dont-block t))
  11523. ;; If this task has children, and any are undone, it's blocked
  11524. (save-excursion
  11525. (org-back-to-heading t)
  11526. (let ((this-level (funcall outline-level)))
  11527. (outline-next-heading)
  11528. (let ((child-level (funcall outline-level)))
  11529. (while (and (not (eobp))
  11530. (> child-level this-level))
  11531. ;; this todo has children, check whether they are all
  11532. ;; completed
  11533. (if (and (not (org-entry-is-done-p))
  11534. (org-entry-is-todo-p))
  11535. (progn (setq org-block-entry-blocking (org-get-heading))
  11536. (throw 'dont-block nil)))
  11537. (outline-next-heading)
  11538. (setq child-level (funcall outline-level))))))
  11539. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11540. ;; any previous siblings are undone, it's blocked
  11541. (save-excursion
  11542. (org-back-to-heading t)
  11543. (let* ((pos (point))
  11544. (parent-pos (and (org-up-heading-safe) (point))))
  11545. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11546. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11547. (forward-line 1)
  11548. (re-search-forward org-not-done-heading-regexp pos t))
  11549. (setq org-block-entry-blocking (match-string 0))
  11550. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11551. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11552. (while t
  11553. (goto-char parent-pos)
  11554. (if (not (looking-at org-not-done-heading-regexp))
  11555. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11556. (setq pos (point))
  11557. (setq parent-pos (and (org-up-heading-safe) (point)))
  11558. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11559. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11560. (forward-line 1)
  11561. (re-search-forward org-not-done-heading-regexp pos t)
  11562. (setq org-block-entry-blocking (org-get-heading)))
  11563. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11564. (defcustom org-track-ordered-property-with-tag nil
  11565. "Should the ORDERED property also be shown as a tag?
  11566. The ORDERED property decides if an entry should require subtasks to be
  11567. completed in sequence. Since a property is not very visible, setting
  11568. this option means that toggling the ORDERED property with the command
  11569. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11570. not relevant for the behavior, but it makes things more visible.
  11571. Note that toggling the tag with tags commands will not change the property
  11572. and therefore not influence behavior!
  11573. This can be t, meaning the tag ORDERED should be used, It can also be a
  11574. string to select a different tag for this task."
  11575. :group 'org-todo
  11576. :type '(choice
  11577. (const :tag "No tracking" nil)
  11578. (const :tag "Track with ORDERED tag" t)
  11579. (string :tag "Use other tag")))
  11580. (defun org-toggle-ordered-property ()
  11581. "Toggle the ORDERED property of the current entry.
  11582. For better visibility, you can track the value of this property with a tag.
  11583. See variable `org-track-ordered-property-with-tag'."
  11584. (interactive)
  11585. (let* ((t1 org-track-ordered-property-with-tag)
  11586. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11587. (save-excursion
  11588. (org-back-to-heading)
  11589. (if (org-entry-get nil "ORDERED")
  11590. (progn
  11591. (org-delete-property "ORDERED")
  11592. (and tag (org-toggle-tag tag 'off))
  11593. (message "Subtasks can be completed in arbitrary order"))
  11594. (org-entry-put nil "ORDERED" "t")
  11595. (and tag (org-toggle-tag tag 'on))
  11596. (message "Subtasks must be completed in sequence")))))
  11597. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11598. (defun org-block-todo-from-checkboxes (change-plist)
  11599. "Block turning an entry into a TODO, using checkboxes.
  11600. This checks whether the current task should be blocked from state
  11601. changes because there are unchecked boxes in this entry."
  11602. (if (not org-enforce-todo-checkbox-dependencies)
  11603. t ; if locally turned off don't block
  11604. (catch 'dont-block
  11605. ;; If this is not a todo state change, or if this entry is already DONE,
  11606. ;; do not block
  11607. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11608. (member (plist-get change-plist :from)
  11609. (cons 'done org-done-keywords))
  11610. (member (plist-get change-plist :to)
  11611. (cons 'todo org-not-done-keywords))
  11612. (not (plist-get change-plist :to)))
  11613. (throw 'dont-block t))
  11614. ;; If this task has checkboxes that are not checked, it's blocked
  11615. (save-excursion
  11616. (org-back-to-heading t)
  11617. (let ((beg (point)) end)
  11618. (outline-next-heading)
  11619. (setq end (point))
  11620. (goto-char beg)
  11621. (if (org-list-search-forward
  11622. (concat (org-item-beginning-re)
  11623. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11624. "\\[[- ]\\]")
  11625. end t)
  11626. (progn
  11627. (if (boundp 'org-blocked-by-checkboxes)
  11628. (setq org-blocked-by-checkboxes t))
  11629. (throw 'dont-block nil)))))
  11630. t))) ; do not block
  11631. (defun org-entry-blocked-p ()
  11632. "Non-nil if entry at point is blocked."
  11633. (and (not (org-entry-get nil "NOBLOCKING"))
  11634. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11635. (not (run-hook-with-args-until-failure
  11636. 'org-blocker-hook
  11637. (list :type 'todo-state-change
  11638. :position (point)
  11639. :from 'todo
  11640. :to 'done)))))
  11641. (defun org-update-statistics-cookies (all)
  11642. "Update the statistics cookie, either from TODO or from checkboxes.
  11643. This should be called with the cursor in a line with a statistics cookie."
  11644. (interactive "P")
  11645. (if all
  11646. (progn
  11647. (org-update-checkbox-count 'all)
  11648. (org-map-entries 'org-update-parent-todo-statistics))
  11649. (if (not (org-at-heading-p))
  11650. (org-update-checkbox-count)
  11651. (let ((pos (point-marker))
  11652. end l1 l2)
  11653. (ignore-errors (org-back-to-heading t))
  11654. (if (not (org-at-heading-p))
  11655. (org-update-checkbox-count)
  11656. (setq l1 (org-outline-level))
  11657. (setq end (save-excursion
  11658. (outline-next-heading)
  11659. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11660. (point)))
  11661. (if (and (save-excursion
  11662. (re-search-forward
  11663. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11664. (not (save-excursion (re-search-forward
  11665. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11666. (org-update-checkbox-count)
  11667. (if (and l2 (> l2 l1))
  11668. (progn
  11669. (goto-char end)
  11670. (org-update-parent-todo-statistics))
  11671. (goto-char pos)
  11672. (beginning-of-line 1)
  11673. (while (re-search-forward
  11674. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11675. (point-at-eol) t)
  11676. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11677. (goto-char pos)
  11678. (move-marker pos nil)))))
  11679. (defvar org-entry-property-inherited-from) ;; defined below
  11680. (defun org-update-parent-todo-statistics ()
  11681. "Update any statistics cookie in the parent of the current headline.
  11682. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11683. the entire subtree and this will travel up the hierarchy and update
  11684. statistics everywhere."
  11685. (let* ((prop (save-excursion (org-up-heading-safe)
  11686. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11687. (recursive (or (not org-hierarchical-todo-statistics)
  11688. (and prop (string-match "\\<recursive\\>" prop))))
  11689. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11690. 0))
  11691. (first t)
  11692. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11693. level ltoggle l1 new ndel
  11694. (cnt-all 0) (cnt-done 0) is-percent kwd
  11695. checkbox-beg ov ovs ove cookie-present)
  11696. (catch 'exit
  11697. (save-excursion
  11698. (beginning-of-line 1)
  11699. (setq ltoggle (funcall outline-level))
  11700. ;; Three situations are to consider:
  11701. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11702. ;; to the top-level ancestor on the headline;
  11703. ;; 2. If parent has "recursive" property, repeat up to the
  11704. ;; headline setting that property, taking inheritance into
  11705. ;; account;
  11706. ;; 3. Else, move up to direct parent and proceed only once.
  11707. (while (and (setq level (org-up-heading-safe))
  11708. (or recursive first)
  11709. (>= (point) lim))
  11710. (setq first nil cookie-present nil)
  11711. (unless (and level
  11712. (not (string-match
  11713. "\\<checkbox\\>"
  11714. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11715. "")))))
  11716. (throw 'exit nil))
  11717. (while (re-search-forward box-re (point-at-eol) t)
  11718. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11719. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11720. (save-match-data
  11721. (unless (outline-next-heading) (throw 'exit nil))
  11722. (while (and (looking-at org-complex-heading-regexp)
  11723. (> (setq l1 (length (match-string 1))) level))
  11724. (setq kwd (and (or recursive (= l1 ltoggle))
  11725. (match-string 2)))
  11726. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11727. (and (eq org-provide-todo-statistics t)
  11728. (or (member kwd org-done-keywords)))
  11729. (and (listp org-provide-todo-statistics)
  11730. (stringp (car org-provide-todo-statistics))
  11731. (or (member kwd org-provide-todo-statistics)
  11732. (member kwd org-done-keywords)))
  11733. (and (listp org-provide-todo-statistics)
  11734. (listp (car org-provide-todo-statistics))
  11735. (or (member kwd (car org-provide-todo-statistics))
  11736. (and (member kwd org-done-keywords)
  11737. (member kwd (cadr org-provide-todo-statistics))))))
  11738. (setq cnt-all (1+ cnt-all))
  11739. (if (eq org-provide-todo-statistics t)
  11740. (and kwd (setq cnt-all (1+ cnt-all)))))
  11741. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11742. (member kwd org-done-keywords))
  11743. (and (listp org-provide-todo-statistics)
  11744. (listp (car org-provide-todo-statistics))
  11745. (member kwd org-done-keywords)
  11746. (member kwd (cadr org-provide-todo-statistics)))
  11747. (and (listp org-provide-todo-statistics)
  11748. (stringp (car org-provide-todo-statistics))
  11749. (member kwd org-done-keywords)))
  11750. (setq cnt-done (1+ cnt-done)))
  11751. (outline-next-heading)))
  11752. (setq new
  11753. (if is-percent
  11754. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11755. (max 1 cnt-all)))
  11756. (format "[%d/%d]" cnt-done cnt-all))
  11757. ndel (- (match-end 0) checkbox-beg))
  11758. ;; handle overlays when updating cookie from column view
  11759. (when (setq ov (car (overlays-at checkbox-beg)))
  11760. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11761. (delete-overlay ov))
  11762. (goto-char checkbox-beg)
  11763. (insert new)
  11764. (delete-region (point) (+ (point) ndel))
  11765. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11766. (when ov (move-overlay ov ovs ove)))
  11767. (when cookie-present
  11768. (run-hook-with-args 'org-after-todo-statistics-hook
  11769. cnt-done (- cnt-all cnt-done))))))
  11770. (run-hooks 'org-todo-statistics-hook)))
  11771. (defvar org-after-todo-statistics-hook nil
  11772. "Hook that is called after a TODO statistics cookie has been updated.
  11773. Each function is called with two arguments: the number of not-done entries
  11774. and the number of done entries.
  11775. For example, the following function, when added to this hook, will switch
  11776. an entry to DONE when all children are done, and back to TODO when new
  11777. entries are set to a TODO status. Note that this hook is only called
  11778. when there is a statistics cookie in the headline!
  11779. (defun org-summary-todo (n-done n-not-done)
  11780. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11781. (let (org-log-done org-log-states) ; turn off logging
  11782. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11783. ")
  11784. (defvar org-todo-statistics-hook nil
  11785. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11786. This hook runs even if there is no statistics cookie present, in which case
  11787. `org-after-todo-statistics-hook' would not run.")
  11788. (defun org-todo-trigger-tag-changes (state)
  11789. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11790. (let ((l org-todo-state-tags-triggers)
  11791. changes)
  11792. (when (or (not state) (equal state ""))
  11793. (setq changes (append changes (cdr (assoc "" l)))))
  11794. (when (and (stringp state) (> (length state) 0))
  11795. (setq changes (append changes (cdr (assoc state l)))))
  11796. (when (member state org-not-done-keywords)
  11797. (setq changes (append changes (cdr (assoc 'todo l)))))
  11798. (when (member state org-done-keywords)
  11799. (setq changes (append changes (cdr (assoc 'done l)))))
  11800. (dolist (c changes)
  11801. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11802. (defun org-local-logging (value)
  11803. "Get logging settings from a property VALUE."
  11804. ;; Directly set the variables, they are already local.
  11805. (setq org-log-done nil
  11806. org-log-repeat nil
  11807. org-todo-log-states nil)
  11808. (dolist (w (org-split-string value))
  11809. (let (a)
  11810. (cond
  11811. ((setq a (assoc w org-startup-options))
  11812. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11813. (set (nth 1 a) (nth 2 a))))
  11814. ((setq a (org-extract-log-state-settings w))
  11815. (and (member (car a) org-todo-keywords-1)
  11816. (push a org-todo-log-states)))))))
  11817. (defun org-get-todo-sequence-head (kwd)
  11818. "Return the head of the TODO sequence to which KWD belongs.
  11819. If KWD is not set, check if there is a text property remembering the
  11820. right sequence."
  11821. (let (p)
  11822. (cond
  11823. ((not kwd)
  11824. (or (get-text-property (point-at-bol) 'org-todo-head)
  11825. (progn
  11826. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11827. nil (point-at-eol)))
  11828. (get-text-property p 'org-todo-head))))
  11829. ((not (member kwd org-todo-keywords-1))
  11830. (car org-todo-keywords-1))
  11831. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11832. (defun org-fast-todo-selection ()
  11833. "Fast TODO keyword selection with single keys.
  11834. Returns the new TODO keyword, or nil if no state change should occur."
  11835. (let* ((fulltable org-todo-key-alist)
  11836. (done-keywords org-done-keywords) ;; needed for the faces.
  11837. (maxlen (apply 'max (mapcar
  11838. (lambda (x)
  11839. (if (stringp (car x)) (string-width (car x)) 0))
  11840. fulltable)))
  11841. (expert nil)
  11842. (fwidth (+ maxlen 3 1 3))
  11843. (ncol (/ (- (window-width) 4) fwidth))
  11844. tg cnt e c tbl
  11845. groups ingroup)
  11846. (save-excursion
  11847. (save-window-excursion
  11848. (if expert
  11849. (set-buffer (get-buffer-create " *Org todo*"))
  11850. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11851. (erase-buffer)
  11852. (org-set-local 'org-done-keywords done-keywords)
  11853. (setq tbl fulltable cnt 0)
  11854. (while (setq e (pop tbl))
  11855. (cond
  11856. ((equal e '(:startgroup))
  11857. (push '() groups) (setq ingroup t)
  11858. (when (not (= cnt 0))
  11859. (setq cnt 0)
  11860. (insert "\n"))
  11861. (insert "{ "))
  11862. ((equal e '(:endgroup))
  11863. (setq ingroup nil cnt 0)
  11864. (insert "}\n"))
  11865. ((equal e '(:newline))
  11866. (when (not (= cnt 0))
  11867. (setq cnt 0)
  11868. (insert "\n")
  11869. (setq e (car tbl))
  11870. (while (equal (car tbl) '(:newline))
  11871. (insert "\n")
  11872. (setq tbl (cdr tbl)))))
  11873. (t
  11874. (setq tg (car e) c (cdr e))
  11875. (if ingroup (push tg (car groups)))
  11876. (setq tg (org-add-props tg nil 'face
  11877. (org-get-todo-face tg)))
  11878. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11879. (insert "[" c "] " tg (make-string
  11880. (- fwidth 4 (length tg)) ?\ ))
  11881. (when (= (setq cnt (1+ cnt)) ncol)
  11882. (insert "\n")
  11883. (if ingroup (insert " "))
  11884. (setq cnt 0)))))
  11885. (insert "\n")
  11886. (goto-char (point-min))
  11887. (if (not expert) (org-fit-window-to-buffer))
  11888. (message "[a-z..]:Set [SPC]:clear")
  11889. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11890. (cond
  11891. ((or (= c ?\C-g)
  11892. (and (= c ?q) (not (rassoc c fulltable))))
  11893. (setq quit-flag t))
  11894. ((= c ?\ ) nil)
  11895. ((setq e (rassoc c fulltable) tg (car e))
  11896. tg)
  11897. (t (setq quit-flag t)))))))
  11898. (defun org-entry-is-todo-p ()
  11899. (member (org-get-todo-state) org-not-done-keywords))
  11900. (defun org-entry-is-done-p ()
  11901. (member (org-get-todo-state) org-done-keywords))
  11902. (defun org-get-todo-state ()
  11903. "Return the TODO keyword of the current subtree."
  11904. (save-excursion
  11905. (org-back-to-heading t)
  11906. (and (looking-at org-todo-line-regexp)
  11907. (match-end 2)
  11908. (match-string 2))))
  11909. (defun org-at-date-range-p (&optional inactive-ok)
  11910. "Non-nil if point is inside a date range.
  11911. When optional argument INACTIVE-OK is non-nil, also consider
  11912. inactive time ranges.
  11913. When this function returns a non-nil value, match data is set
  11914. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11915. on INACTIVE-OK."
  11916. (interactive)
  11917. (save-excursion
  11918. (catch 'exit
  11919. (let ((pos (point)))
  11920. (skip-chars-backward "^[<\r\n")
  11921. (skip-chars-backward "<[")
  11922. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11923. (>= (match-end 0) pos)
  11924. (throw 'exit t))
  11925. (skip-chars-backward "^<[\r\n")
  11926. (skip-chars-backward "<[")
  11927. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11928. (>= (match-end 0) pos)
  11929. (throw 'exit t)))
  11930. nil)))
  11931. (defun org-get-repeat (&optional tagline)
  11932. "Check if there is a deadline/schedule with repeater in this entry."
  11933. (save-match-data
  11934. (save-excursion
  11935. (org-back-to-heading t)
  11936. (and (re-search-forward (if tagline
  11937. (concat tagline "\\s-*" org-repeat-re)
  11938. org-repeat-re)
  11939. (org-entry-end-position) t)
  11940. (match-string-no-properties 1)))))
  11941. (defvar org-last-changed-timestamp)
  11942. (defvar org-last-inserted-timestamp)
  11943. (defvar org-log-post-message)
  11944. (defvar org-log-note-purpose)
  11945. (defvar org-log-note-how nil)
  11946. (defvar org-log-note-extra)
  11947. (defun org-auto-repeat-maybe (done-word)
  11948. "Check if the current headline contains a repeated deadline/schedule.
  11949. If yes, set TODO state back to what it was and change the base date
  11950. of repeating deadline/scheduled time stamps to new date.
  11951. This function is run automatically after each state change to a DONE state."
  11952. ;; last-state is dynamically scoped into this function
  11953. (let* ((repeat (org-get-repeat))
  11954. (aa (assoc org-last-state org-todo-kwd-alist))
  11955. (interpret (nth 1 aa))
  11956. (head (nth 2 aa))
  11957. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11958. (msg "Entry repeats: ")
  11959. (org-log-done nil)
  11960. (org-todo-log-states nil)
  11961. re type n what ts time to-state)
  11962. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11963. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11964. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11965. org-todo-repeat-to-state))
  11966. (unless (and to-state (member to-state org-todo-keywords-1))
  11967. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11968. (org-todo to-state)
  11969. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11970. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11971. (org-time-stamp-format t t))))
  11972. (when org-log-repeat
  11973. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11974. (memq 'org-add-log-note post-command-hook))
  11975. ;; OK, we are already setup for some record
  11976. (if (eq org-log-repeat 'note)
  11977. ;; make sure we take a note, not only a time stamp
  11978. (setq org-log-note-how 'note))
  11979. ;; Set up for taking a record
  11980. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11981. org-last-state
  11982. 'findpos org-log-repeat)))
  11983. (org-back-to-heading t)
  11984. (org-add-planning-info nil nil 'closed)
  11985. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11986. org-deadline-time-regexp "\\)\\|\\("
  11987. org-ts-regexp "\\)"))
  11988. (while (re-search-forward
  11989. re (save-excursion (outline-next-heading) (point)) t)
  11990. (setq type (if (match-end 1) org-scheduled-string
  11991. (if (match-end 3) org-deadline-string "Plain:"))
  11992. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11993. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11994. (setq n (string-to-number (match-string 2 ts))
  11995. what (match-string 3 ts))
  11996. (if (equal what "w") (setq n (* n 7) what "d"))
  11997. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11998. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11999. ;; Preparation, see if we need to modify the start date for the change
  12000. (when (match-end 1)
  12001. (setq time (save-match-data (org-time-string-to-time ts)))
  12002. (cond
  12003. ((equal (match-string 1 ts) ".")
  12004. ;; Shift starting date to today
  12005. (org-timestamp-change
  12006. (- (org-today) (time-to-days time))
  12007. 'day))
  12008. ((equal (match-string 1 ts) "+")
  12009. (let ((nshiftmax 10) (nshift 0))
  12010. (while (or (= nshift 0)
  12011. (<= (time-to-days time)
  12012. (time-to-days (current-time))))
  12013. (when (= (incf nshift) nshiftmax)
  12014. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12015. (user-error "Abort")))
  12016. (org-timestamp-change n (cdr (assoc what whata)))
  12017. (org-at-timestamp-p t)
  12018. (setq ts (match-string 1))
  12019. (setq time (save-match-data (org-time-string-to-time ts)))))
  12020. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12021. ;; rematch, so that we have everything in place for the real shift
  12022. (org-at-timestamp-p t)
  12023. (setq ts (match-string 1))
  12024. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12025. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12026. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12027. (setq org-log-post-message msg)
  12028. (message "%s" msg))))
  12029. (defun org-show-todo-tree (arg)
  12030. "Make a compact tree which shows all headlines marked with TODO.
  12031. The tree will show the lines where the regexp matches, and all higher
  12032. headlines above the match.
  12033. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12034. With a numeric prefix N, construct a sparse tree for the Nth element
  12035. of `org-todo-keywords-1'."
  12036. (interactive "P")
  12037. (let ((case-fold-search nil)
  12038. (kwd-re
  12039. (cond ((null arg) org-not-done-regexp)
  12040. ((equal arg '(4))
  12041. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12042. (mapcar 'list org-todo-keywords-1))))
  12043. (concat "\\("
  12044. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12045. "\\)\\>")))
  12046. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12047. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12048. org-todo-keywords-1)))
  12049. (t (user-error "Invalid prefix argument: %s" arg)))))
  12050. (message "%d TODO entries found"
  12051. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12052. (defun org-deadline (arg &optional time)
  12053. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12054. With one universal prefix argument, remove any deadline from the item.
  12055. With two universal prefix arguments, prompt for a warning delay.
  12056. With argument TIME, set the deadline at the corresponding date. TIME
  12057. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12058. (interactive "P")
  12059. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12060. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12061. 'region-start-level 'region))
  12062. org-loop-over-headlines-in-active-region)
  12063. (org-map-entries
  12064. `(org-deadline ',arg ,time)
  12065. org-loop-over-headlines-in-active-region
  12066. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12067. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12068. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12069. (repeater (and old-date
  12070. (string-match
  12071. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12072. old-date)
  12073. (match-string 1 old-date))))
  12074. (cond
  12075. ((equal arg '(4))
  12076. (when (and old-date org-log-redeadline)
  12077. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12078. org-log-redeadline))
  12079. (org-remove-timestamp-with-keyword org-deadline-string)
  12080. (message "Item no longer has a deadline."))
  12081. ((equal arg '(16))
  12082. (save-excursion
  12083. (org-back-to-heading t)
  12084. (if (re-search-forward
  12085. org-deadline-time-regexp
  12086. (save-excursion (outline-next-heading) (point)) t)
  12087. (let* ((rpl0 (match-string 1))
  12088. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12089. (replace-match
  12090. (concat org-deadline-string
  12091. " <" rpl
  12092. (format " -%dd"
  12093. (abs
  12094. (- (time-to-days
  12095. (save-match-data
  12096. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12097. (time-to-days old-date-time))))
  12098. ">") t t))
  12099. (user-error "No deadline information to update"))))
  12100. (t
  12101. (org-add-planning-info 'deadline time 'closed)
  12102. (when (and old-date
  12103. org-log-redeadline
  12104. (not (equal old-date org-last-inserted-timestamp)))
  12105. (org-add-log-setup
  12106. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12107. org-log-redeadline))
  12108. (when repeater
  12109. (save-excursion
  12110. (org-back-to-heading t)
  12111. (when (re-search-forward (concat org-deadline-string " "
  12112. org-last-inserted-timestamp)
  12113. (save-excursion
  12114. (outline-next-heading) (point)) t)
  12115. (goto-char (1- (match-end 0)))
  12116. (insert " " repeater)
  12117. (setq org-last-inserted-timestamp
  12118. (concat (substring org-last-inserted-timestamp 0 -1)
  12119. " " repeater
  12120. (substring org-last-inserted-timestamp -1))))))
  12121. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12122. (defun org-schedule (arg &optional time)
  12123. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12124. With one universal prefix argument, remove any scheduling date from the item.
  12125. With two universal prefix arguments, prompt for a delay cookie.
  12126. With argument TIME, scheduled at the corresponding date. TIME can
  12127. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12128. (interactive "P")
  12129. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12130. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12131. 'region-start-level 'region))
  12132. org-loop-over-headlines-in-active-region)
  12133. (org-map-entries
  12134. `(org-schedule ',arg ,time)
  12135. org-loop-over-headlines-in-active-region
  12136. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12137. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12138. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12139. (repeater (and old-date
  12140. (string-match
  12141. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12142. old-date)
  12143. (match-string 1 old-date))))
  12144. (cond
  12145. ((equal arg '(4))
  12146. (progn
  12147. (when (and old-date org-log-reschedule)
  12148. (org-add-log-setup 'delschedule nil old-date 'findpos
  12149. org-log-reschedule))
  12150. (org-remove-timestamp-with-keyword org-scheduled-string)
  12151. (message "Item is no longer scheduled.")))
  12152. ((equal arg '(16))
  12153. (save-excursion
  12154. (org-back-to-heading t)
  12155. (if (re-search-forward
  12156. org-scheduled-time-regexp
  12157. (save-excursion (outline-next-heading) (point)) t)
  12158. (let* ((rpl0 (match-string 1))
  12159. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12160. (replace-match
  12161. (concat org-scheduled-string
  12162. " <" rpl
  12163. (format " -%dd"
  12164. (abs
  12165. (- (time-to-days
  12166. (save-match-data
  12167. (org-read-date nil t nil "Delay until" old-date-time)))
  12168. (time-to-days old-date-time))))
  12169. ">") t t))
  12170. (user-error "No scheduled information to update"))))
  12171. (t
  12172. (org-add-planning-info 'scheduled time 'closed)
  12173. (when (and old-date
  12174. org-log-reschedule
  12175. (not (equal old-date org-last-inserted-timestamp)))
  12176. (org-add-log-setup
  12177. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12178. org-log-reschedule))
  12179. (when repeater
  12180. (save-excursion
  12181. (org-back-to-heading t)
  12182. (when (re-search-forward (concat org-scheduled-string " "
  12183. org-last-inserted-timestamp)
  12184. (save-excursion
  12185. (outline-next-heading) (point)) t)
  12186. (goto-char (1- (match-end 0)))
  12187. (insert " " repeater)
  12188. (setq org-last-inserted-timestamp
  12189. (concat (substring org-last-inserted-timestamp 0 -1)
  12190. " " repeater
  12191. (substring org-last-inserted-timestamp -1))))))
  12192. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12193. (defun org-get-scheduled-time (pom &optional inherit)
  12194. "Get the scheduled time as a time tuple, of a format suitable
  12195. for calling org-schedule with, or if there is no scheduling,
  12196. returns nil."
  12197. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12198. (when time
  12199. (apply 'encode-time (org-parse-time-string time)))))
  12200. (defun org-get-deadline-time (pom &optional inherit)
  12201. "Get the deadline as a time tuple, of a format suitable for
  12202. calling org-deadline with, or if there is no scheduling, returns
  12203. nil."
  12204. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12205. (when time
  12206. (apply 'encode-time (org-parse-time-string time)))))
  12207. (defun org-remove-timestamp-with-keyword (keyword)
  12208. "Remove all time stamps with KEYWORD in the current entry."
  12209. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12210. beg)
  12211. (save-excursion
  12212. (org-back-to-heading t)
  12213. (setq beg (point))
  12214. (outline-next-heading)
  12215. (while (re-search-backward re beg t)
  12216. (replace-match "")
  12217. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12218. (equal (char-before) ?\ ))
  12219. (backward-delete-char 1)
  12220. (if (string-match "^[ \t]*$" (buffer-substring
  12221. (point-at-bol) (point-at-eol)))
  12222. (delete-region (point-at-bol)
  12223. (min (point-max) (1+ (point-at-eol))))))))))
  12224. (defvar org-time-was-given) ; dynamically scoped parameter
  12225. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12226. (defun org-at-planning-p ()
  12227. "Non-nil when point is on a planning info line."
  12228. ;; This is as accurate and faster than `org-element-at-point' since
  12229. ;; planning info location is fixed in the section.
  12230. (org-with-wide-buffer
  12231. (beginning-of-line)
  12232. (and (org-looking-at-p org-planning-line-re)
  12233. (eq (point)
  12234. (ignore-errors
  12235. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12236. (org-back-to-heading t)
  12237. (org-with-limited-levels (org-back-to-heading t)))
  12238. (line-beginning-position 2))))))
  12239. (defun org-add-planning-info (what &optional time &rest remove)
  12240. "Insert new timestamp with keyword in the planning line.
  12241. WHAT indicates what kind of time stamp to add. It is a symbol
  12242. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12243. the time to use. If none is given, the user is prompted for
  12244. a date. REMOVE indicates what kind of entries to remove. An old
  12245. WHAT entry will also be removed."
  12246. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12247. (catch 'exit
  12248. (when (and (memq what '(scheduled deadline))
  12249. (or (not time)
  12250. (and (stringp time)
  12251. (string-match "^[-+]+[0-9]" time))))
  12252. ;; Try to get a default date/time from existing timestamp
  12253. (save-excursion
  12254. (org-back-to-heading t)
  12255. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12256. (when (re-search-forward (if (eq what 'scheduled)
  12257. org-scheduled-time-regexp
  12258. org-deadline-time-regexp)
  12259. end t)
  12260. (setq ts (match-string 1)
  12261. default-time (apply 'encode-time (org-parse-time-string ts))
  12262. default-input (and ts (org-get-compact-tod ts)))))))
  12263. (when what
  12264. (setq time
  12265. (if (stringp time)
  12266. ;; This is a string (relative or absolute), set
  12267. ;; proper date.
  12268. (apply #'encode-time
  12269. (org-read-date-analyze
  12270. time default-time (decode-time default-time)))
  12271. ;; If necessary, get the time from the user
  12272. (or time (org-read-date nil 'to-time nil nil
  12273. default-time default-input)))))
  12274. (org-with-wide-buffer
  12275. (org-back-to-heading t)
  12276. (forward-line)
  12277. (unless (bolp) (insert "\n"))
  12278. (cond ((org-looking-at-p org-planning-line-re)
  12279. ;; Move to current indentation.
  12280. (skip-chars-forward " \t")
  12281. ;; Check if we have to remove something.
  12282. (dolist (type (if what (cons what remove) remove))
  12283. (save-excursion
  12284. (when (re-search-forward
  12285. (concat
  12286. " *"
  12287. (case type
  12288. (closed org-closed-time-regexp)
  12289. (deadline org-deadline-time-regexp)
  12290. (scheduled org-scheduled-time-regexp)
  12291. (otherwise
  12292. (error "Invalid planning type: %s" type))))
  12293. (line-end-position) t)
  12294. (replace-match "")
  12295. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12296. (when (and (not what) (eq type 'closed))
  12297. (save-excursion
  12298. (beginning-of-line)
  12299. (when (looking-at "[ \t]*$")
  12300. (delete-region (point)
  12301. (line-beginning-position 2)))))))
  12302. ;; Remove leading white spaces.
  12303. (when (looking-at "[ \t]+") (replace-match ""))))
  12304. ((not what) (throw 'exit nil)) ; Nothing to do.
  12305. (t (insert-before-markers "\n")
  12306. (backward-char 1)
  12307. (when org-adapt-indentation
  12308. (org-indent-to-column (1+ (org-outline-level))))))
  12309. (when what
  12310. ;; Insert planning keyword.
  12311. (insert (case what
  12312. (closed org-closed-string)
  12313. (deadline org-deadline-string)
  12314. (scheduled org-scheduled-string)
  12315. (otherwise (error "Invalid planning type: %s" what)))
  12316. " ")
  12317. ;; Insert associated timestamp.
  12318. (let ((ts (org-insert-time-stamp
  12319. time
  12320. (or org-time-was-given
  12321. (and (eq what 'closed) org-log-done-with-time))
  12322. (eq what 'closed)
  12323. nil nil (list org-end-time-was-given))))
  12324. (unless (eolp) (insert " "))
  12325. ts))))))
  12326. (defvar org-log-note-marker (make-marker))
  12327. (defvar org-log-note-purpose nil)
  12328. (defvar org-log-note-state nil)
  12329. (defvar org-log-note-previous-state nil)
  12330. (defvar org-log-note-extra nil)
  12331. (defvar org-log-note-window-configuration nil)
  12332. (defvar org-log-note-return-to (make-marker))
  12333. (defvar org-log-note-effective-time nil
  12334. "Remembered current time so that dynamically scoped
  12335. `org-extend-today-until' affects tha timestamps in state change
  12336. log")
  12337. (defvar org-log-post-message nil
  12338. "Message to be displayed after a log note has been stored.
  12339. The auto-repeater uses this.")
  12340. (defun org-add-note ()
  12341. "Add a note to the current entry.
  12342. This is done in the same way as adding a state change note."
  12343. (interactive)
  12344. (org-add-log-setup 'note nil nil 'findpos nil))
  12345. (defun org-log-beginning (&optional create)
  12346. "Return expected start of log notes in current entry.
  12347. When optional argument CREATE is non-nil, the function creates
  12348. a drawer to store notes, if necessary. Returned position ignores
  12349. narrowing."
  12350. (org-with-wide-buffer
  12351. (org-end-of-meta-data)
  12352. (let ((end (if (org-at-heading-p) (point)
  12353. (save-excursion (outline-next-heading) (point))))
  12354. (drawer (org-log-into-drawer)))
  12355. (cond
  12356. (drawer
  12357. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12358. (case-fold-search t))
  12359. (catch 'exit
  12360. ;; Try to find existing drawer.
  12361. (while (re-search-forward regexp end t)
  12362. (let ((element (org-element-at-point)))
  12363. (when (eq (org-element-type element) 'drawer)
  12364. (let ((cend (org-element-property :contents-end element)))
  12365. (when (and (not org-log-states-order-reversed) cend)
  12366. (goto-char cend)))
  12367. (throw 'exit nil))))
  12368. ;; No drawer found. Create one, if permitted.
  12369. (when create
  12370. (unless (bolp) (insert "\n"))
  12371. (let ((beg (point)))
  12372. (insert ":" drawer ":\n:END:\n")
  12373. (org-indent-region beg (point)))
  12374. (end-of-line -1)))))
  12375. (org-log-state-notes-insert-after-drawers
  12376. (while (and (looking-at org-drawer-regexp)
  12377. (progn (goto-char (match-end 0))
  12378. (re-search-forward org-property-end-re end t)))
  12379. (forward-line)))))
  12380. (if (bolp) (point) (line-beginning-position 2))))
  12381. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12382. "Set up the post command hook to take a note.
  12383. If this is about to TODO state change, the new state is expected in STATE.
  12384. When FINDPOS is non-nil, find the correct position for the note in
  12385. the current entry. If not, assume that it can be inserted at point.
  12386. HOW is an indicator what kind of note should be created.
  12387. EXTRA is additional text that will be inserted into the notes buffer."
  12388. (org-with-wide-buffer
  12389. (when findpos
  12390. (goto-char (org-log-beginning t))
  12391. (unless org-log-states-order-reversed
  12392. (org-skip-over-state-notes)
  12393. (skip-chars-backward " \t\n\r")
  12394. (forward-line)))
  12395. (move-marker org-log-note-marker (point))
  12396. ;; Preserve position even if a property drawer is inserted in the
  12397. ;; process.
  12398. (set-marker-insertion-type org-log-note-marker t)
  12399. (setq org-log-note-purpose purpose
  12400. org-log-note-state state
  12401. org-log-note-previous-state prev-state
  12402. org-log-note-how how
  12403. org-log-note-extra extra
  12404. org-log-note-effective-time (org-current-effective-time))
  12405. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12406. (defun org-skip-over-state-notes ()
  12407. "Skip past the list of State notes in an entry."
  12408. (when (ignore-errors (goto-char (org-in-item-p)))
  12409. (let* ((struct (org-list-struct))
  12410. (prevs (org-list-prevs-alist struct))
  12411. (regexp
  12412. (concat "[ \t]*- +"
  12413. (replace-regexp-in-string
  12414. " +" " +"
  12415. (org-replace-escapes
  12416. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12417. `(("%d" . ,org-ts-regexp-inactive)
  12418. ("%D" . ,org-ts-regexp)
  12419. ("%s" . "\"\\S-+\"")
  12420. ("%S" . "\"\\S-+\"")
  12421. ("%t" . ,org-ts-regexp-inactive)
  12422. ("%T" . ,org-ts-regexp)
  12423. ("%u" . ".*?")
  12424. ("%U" . ".*?")))))))
  12425. (while (org-looking-at-p regexp)
  12426. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12427. (org-list-get-item-end (point) struct)))))))
  12428. (defun org-add-log-note (&optional purpose)
  12429. "Pop up a window for taking a note, and add this note later at point."
  12430. (remove-hook 'post-command-hook 'org-add-log-note)
  12431. (setq org-log-note-window-configuration (current-window-configuration))
  12432. (delete-other-windows)
  12433. (move-marker org-log-note-return-to (point))
  12434. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12435. (goto-char org-log-note-marker)
  12436. (org-switch-to-buffer-other-window "*Org Note*")
  12437. (erase-buffer)
  12438. (if (memq org-log-note-how '(time state))
  12439. (let (current-prefix-arg) (org-store-log-note))
  12440. (let ((org-inhibit-startup t)) (org-mode))
  12441. (insert (format "# Insert note for %s.
  12442. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12443. (cond
  12444. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12445. ((eq org-log-note-purpose 'done) "closed todo item")
  12446. ((eq org-log-note-purpose 'state)
  12447. (format "state change from \"%s\" to \"%s\""
  12448. (or org-log-note-previous-state "")
  12449. (or org-log-note-state "")))
  12450. ((eq org-log-note-purpose 'reschedule)
  12451. "rescheduling")
  12452. ((eq org-log-note-purpose 'delschedule)
  12453. "no longer scheduled")
  12454. ((eq org-log-note-purpose 'redeadline)
  12455. "changing deadline")
  12456. ((eq org-log-note-purpose 'deldeadline)
  12457. "removing deadline")
  12458. ((eq org-log-note-purpose 'refile)
  12459. "refiling")
  12460. ((eq org-log-note-purpose 'note)
  12461. "this entry")
  12462. (t (error "This should not happen")))))
  12463. (if org-log-note-extra (insert org-log-note-extra))
  12464. (org-set-local 'org-finish-function 'org-store-log-note)
  12465. (run-hooks 'org-log-buffer-setup-hook)))
  12466. (defvar org-note-abort nil) ; dynamically scoped
  12467. (defun org-store-log-note ()
  12468. "Finish taking a log note, and insert it to where it belongs."
  12469. (let ((txt (buffer-string)))
  12470. (kill-buffer (current-buffer))
  12471. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12472. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12473. (setq txt (replace-match "" t t txt)))
  12474. (if (string-match "\\s-+\\'" txt)
  12475. (setq txt (replace-match "" t t txt)))
  12476. (setq lines (org-split-string txt "\n"))
  12477. (when (and note (string-match "\\S-" note))
  12478. (setq note
  12479. (org-replace-escapes
  12480. note
  12481. (list (cons "%u" (user-login-name))
  12482. (cons "%U" user-full-name)
  12483. (cons "%t" (format-time-string
  12484. (org-time-stamp-format 'long 'inactive)
  12485. org-log-note-effective-time))
  12486. (cons "%T" (format-time-string
  12487. (org-time-stamp-format 'long nil)
  12488. org-log-note-effective-time))
  12489. (cons "%d" (format-time-string
  12490. (org-time-stamp-format nil 'inactive)
  12491. org-log-note-effective-time))
  12492. (cons "%D" (format-time-string
  12493. (org-time-stamp-format nil nil)
  12494. org-log-note-effective-time))
  12495. (cons "%s" (cond
  12496. ((not org-log-note-state) "")
  12497. ((org-string-match-p org-ts-regexp
  12498. org-log-note-state)
  12499. (format "\"[%s]\""
  12500. (substring org-log-note-state 1 -1)))
  12501. (t (format "\"%s\"" org-log-note-state))))
  12502. (cons "%S"
  12503. (cond
  12504. ((not org-log-note-previous-state) "")
  12505. ((org-string-match-p org-ts-regexp
  12506. org-log-note-previous-state)
  12507. (format "\"[%s]\""
  12508. (substring
  12509. org-log-note-previous-state 1 -1)))
  12510. (t (format "\"%s\""
  12511. org-log-note-previous-state)))))))
  12512. (when lines (setq note (concat note " \\\\")))
  12513. (push note lines))
  12514. (when (or current-prefix-arg org-note-abort)
  12515. (when (org-log-into-drawer)
  12516. (org-remove-empty-drawer-at org-log-note-marker))
  12517. (setq lines nil))
  12518. (when lines
  12519. (with-current-buffer (marker-buffer org-log-note-marker)
  12520. (org-with-wide-buffer
  12521. (goto-char org-log-note-marker)
  12522. (move-marker org-log-note-marker nil)
  12523. ;; Make sure point is at the beginning of an empty line.
  12524. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12525. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12526. ;; In an existing list, add a new item at the top level.
  12527. ;; Otherwise, indent line like a regular one.
  12528. (let ((itemp (org-in-item-p)))
  12529. (if itemp
  12530. (org-indent-line-to
  12531. (let ((struct (save-excursion
  12532. (goto-char itemp) (org-list-struct))))
  12533. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12534. (org-indent-line)))
  12535. (insert (org-list-bullet-string "-") (pop lines))
  12536. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12537. (dolist (line lines)
  12538. (insert "\n")
  12539. (org-indent-line-to ind)
  12540. (insert line)))
  12541. (message "Note stored")
  12542. (org-back-to-heading t)
  12543. (org-cycle-hide-drawers 'children))
  12544. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12545. ;; from within `org-add-log-note' because `buffer-undo-list'
  12546. ;; is then modified outside of `org-with-remote-undo'.
  12547. (when (eq this-command 'org-agenda-todo)
  12548. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12549. ;; Don't add undo information when called from `org-agenda-todo'
  12550. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12551. (set-window-configuration org-log-note-window-configuration)
  12552. (with-current-buffer (marker-buffer org-log-note-return-to)
  12553. (goto-char org-log-note-return-to))
  12554. (move-marker org-log-note-return-to nil)
  12555. (and org-log-post-message (message "%s" org-log-post-message))))
  12556. (defun org-remove-empty-drawer-at (pos)
  12557. "Remove an empty drawer at position POS.
  12558. POS may also be a marker."
  12559. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12560. (org-with-wide-buffer
  12561. (goto-char pos)
  12562. (let ((drawer (org-element-at-point)))
  12563. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12564. (not (org-element-property :contents-begin drawer)))
  12565. (delete-region (org-element-property :begin drawer)
  12566. (progn (goto-char (org-element-property :end drawer))
  12567. (skip-chars-backward " \r\t\n")
  12568. (forward-line)
  12569. (point))))))))
  12570. (defvar org-ts-type nil)
  12571. (defun org-sparse-tree (&optional arg type)
  12572. "Create a sparse tree, prompt for the details.
  12573. This command can create sparse trees. You first need to select the type
  12574. of match used to create the tree:
  12575. t Show all TODO entries.
  12576. T Show entries with a specific TODO keyword.
  12577. m Show entries selected by a tags/property match.
  12578. p Enter a property name and its value (both with completion on existing
  12579. names/values) and show entries with that property.
  12580. r Show entries matching a regular expression (`/' can be used as well).
  12581. b Show deadlines and scheduled items before a date.
  12582. a Show deadlines and scheduled items after a date.
  12583. d Show deadlines due within `org-deadline-warning-days'.
  12584. D Show deadlines and scheduled items between a date range."
  12585. (interactive "P")
  12586. (setq type (or type org-sparse-tree-default-date-type))
  12587. (setq org-ts-type type)
  12588. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12589. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12590. [c]ycle through date types: %s"
  12591. (case type
  12592. (all "all timestamps")
  12593. (scheduled "only scheduled")
  12594. (deadline "only deadline")
  12595. (active "only active timestamps")
  12596. (inactive "only inactive timestamps")
  12597. (closed "with a closed time-stamp")
  12598. (otherwise "scheduled/deadline")))
  12599. (let ((answer (read-char-exclusive)))
  12600. (case answer
  12601. (?c
  12602. (org-sparse-tree
  12603. arg
  12604. (cadr
  12605. (memq type '(nil all scheduled deadline active inactive closed)))))
  12606. (?d (call-interactively 'org-check-deadlines))
  12607. (?b (call-interactively 'org-check-before-date))
  12608. (?a (call-interactively 'org-check-after-date))
  12609. (?D (call-interactively 'org-check-dates-range))
  12610. (?t (call-interactively 'org-show-todo-tree))
  12611. (?T (org-show-todo-tree '(4)))
  12612. (?m (call-interactively 'org-match-sparse-tree))
  12613. ((?p ?P)
  12614. (let* ((kwd (org-icompleting-read
  12615. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12616. (value (org-icompleting-read
  12617. "Value: " (mapcar 'list (org-property-values kwd)))))
  12618. (unless (string-match "\\`{.*}\\'" value)
  12619. (setq value (concat "\"" value "\"")))
  12620. (org-match-sparse-tree arg (concat kwd "=" value))))
  12621. ((?r ?R ?/) (call-interactively 'org-occur))
  12622. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12623. (defvar org-occur-highlights nil
  12624. "List of overlays used for occur matches.")
  12625. (make-variable-buffer-local 'org-occur-highlights)
  12626. (defvar org-occur-parameters nil
  12627. "Parameters of the active org-occur calls.
  12628. This is a list, each call to org-occur pushes as cons cell,
  12629. containing the regular expression and the callback, onto the list.
  12630. The list can contain several entries if `org-occur' has been called
  12631. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12632. will only contain one set of parameters. When the highlights are
  12633. removed (for example with `C-c C-c', or with the next edit (depending
  12634. on `org-remove-highlights-with-change'), this variable is emptied
  12635. as well.")
  12636. (make-variable-buffer-local 'org-occur-parameters)
  12637. (defun org-occur (regexp &optional keep-previous callback)
  12638. "Make a compact tree which shows all matches of REGEXP.
  12639. The tree will show the lines where the regexp matches, and all higher
  12640. headlines above the match. It will also show the heading after the match,
  12641. to make sure editing the matching entry is easy.
  12642. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12643. call to `org-occur' will be kept, to allow stacking of calls to this
  12644. command.
  12645. If CALLBACK is non-nil, it is a function which is called to confirm
  12646. that the match should indeed be shown."
  12647. (interactive "sRegexp: \nP")
  12648. (when (equal regexp "")
  12649. (user-error "Regexp cannot be empty"))
  12650. (unless keep-previous
  12651. (org-remove-occur-highlights nil nil t))
  12652. (push (cons regexp callback) org-occur-parameters)
  12653. (let ((cnt 0))
  12654. (save-excursion
  12655. (goto-char (point-min))
  12656. (if (or (not keep-previous) ; do not want to keep
  12657. (not org-occur-highlights)) ; no previous matches
  12658. ;; hide everything
  12659. (org-overview))
  12660. (while (re-search-forward regexp nil t)
  12661. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12662. (when (or (not callback)
  12663. (save-match-data (funcall callback)))
  12664. (setq cnt (1+ cnt))
  12665. (when org-highlight-sparse-tree-matches
  12666. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12667. (org-show-context 'occur-tree))))
  12668. (when org-remove-highlights-with-change
  12669. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12670. nil 'local))
  12671. (unless org-sparse-tree-open-archived-trees
  12672. (org-hide-archived-subtrees (point-min) (point-max)))
  12673. (run-hooks 'org-occur-hook)
  12674. (if (org-called-interactively-p 'interactive)
  12675. (message "%d match(es) for regexp %s" cnt regexp))
  12676. cnt))
  12677. (defun org-occur-next-match (&optional n reset)
  12678. "Function for `next-error-function' to find sparse tree matches.
  12679. N is the number of matches to move, when negative move backwards.
  12680. RESET is entirely ignored - this function always goes back to the
  12681. starting point when no match is found."
  12682. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12683. (search-func (if (< n 0)
  12684. 'previous-single-char-property-change
  12685. 'next-single-char-property-change))
  12686. (n (abs n))
  12687. (pos (point))
  12688. p1)
  12689. (catch 'exit
  12690. (while (setq p1 (funcall search-func (point) 'org-type))
  12691. (when (equal p1 limit)
  12692. (goto-char pos)
  12693. (user-error "No more matches"))
  12694. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12695. (setq n (1- n))
  12696. (when (= n 0)
  12697. (goto-char p1)
  12698. (throw 'exit (point))))
  12699. (goto-char p1))
  12700. (goto-char p1)
  12701. (user-error "No more matches"))))
  12702. (defun org-show-context (&optional key)
  12703. "Make sure point and context are visible.
  12704. Optional argument KEY, when non-nil, is a symbol. See
  12705. `org-show-context-detail' for allowed values and how much is to
  12706. be shown."
  12707. (org-show-set-visibility
  12708. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12709. ((cdr (assq key org-show-context-detail)))
  12710. (t (cdr (assq 'default org-show-context-detail))))))
  12711. (defun org-show-set-visibility (detail)
  12712. "Set visibility around point according to DETAIL.
  12713. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12714. `tree', `canonical' or t. See `org-show-context-detail' for more
  12715. information."
  12716. (unless (org-before-first-heading-p)
  12717. ;; Show current heading and possibly its entry, following headline
  12718. ;; or all children.
  12719. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12720. (org-flag-heading nil)
  12721. (org-show-entry)
  12722. (org-with-limited-levels
  12723. (case detail
  12724. ((tree canonical t) (show-children))
  12725. ((nil minimal ancestors))
  12726. (t (save-excursion
  12727. (outline-next-heading)
  12728. (org-flag-heading nil))))))
  12729. ;; Show all siblings.
  12730. (when (eq detail 'lineage) (org-show-siblings))
  12731. ;; Show ancestors, possibly with their children.
  12732. (when (memq detail '(ancestors lineage tree canonical t))
  12733. (save-excursion
  12734. (while (org-up-heading-safe)
  12735. (org-flag-heading nil)
  12736. (when (memq detail '(canonical t)) (org-show-entry))
  12737. (when (memq detail '(tree canonical t)) (show-children)))))))
  12738. (defvar org-reveal-start-hook nil
  12739. "Hook run before revealing a location.")
  12740. (defun org-reveal (&optional siblings)
  12741. "Show current entry, hierarchy above it, and the following headline.
  12742. This can be used to show a consistent set of context around
  12743. locations exposed with `org-show-context'.
  12744. With optional argument SIBLINGS, on each level of the hierarchy all
  12745. siblings are shown. This repairs the tree structure to what it would
  12746. look like when opened with hierarchical calls to `org-cycle'.
  12747. With double optional argument \\[universal-argument] \\[universal-argument], \
  12748. go to the parent and show the
  12749. entire tree."
  12750. (interactive "P")
  12751. (run-hooks 'org-reveal-start-hook)
  12752. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12753. ((equal siblings '(16))
  12754. (save-excursion
  12755. (when (org-up-heading-safe)
  12756. (org-show-subtree)
  12757. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12758. (t (org-show-set-visibility 'lineage))))
  12759. (defun org-highlight-new-match (beg end)
  12760. "Highlight from BEG to END and mark the highlight is an occur headline."
  12761. (let ((ov (make-overlay beg end)))
  12762. (overlay-put ov 'face 'secondary-selection)
  12763. (overlay-put ov 'org-type 'org-occur)
  12764. (push ov org-occur-highlights)))
  12765. (defun org-remove-occur-highlights (&optional beg end noremove)
  12766. "Remove the occur highlights from the buffer.
  12767. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12768. from the `before-change-functions' in the current buffer."
  12769. (interactive)
  12770. (unless org-inhibit-highlight-removal
  12771. (mapc 'delete-overlay org-occur-highlights)
  12772. (setq org-occur-highlights nil)
  12773. (setq org-occur-parameters nil)
  12774. (unless noremove
  12775. (remove-hook 'before-change-functions
  12776. 'org-remove-occur-highlights 'local))))
  12777. ;;;; Priorities
  12778. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12779. "Regular expression matching the priority indicator.")
  12780. (defvar org-remove-priority-next-time nil)
  12781. (defun org-priority-up ()
  12782. "Increase the priority of the current item."
  12783. (interactive)
  12784. (org-priority 'up))
  12785. (defun org-priority-down ()
  12786. "Decrease the priority of the current item."
  12787. (interactive)
  12788. (org-priority 'down))
  12789. (defun org-priority (&optional action show)
  12790. "Change the priority of an item.
  12791. ACTION can be `set', `up', `down', or a character."
  12792. (interactive "P")
  12793. (if (equal action '(4))
  12794. (org-show-priority)
  12795. (unless org-enable-priority-commands
  12796. (user-error "Priority commands are disabled"))
  12797. (setq action (or action 'set))
  12798. (let (current new news have remove)
  12799. (save-excursion
  12800. (org-back-to-heading t)
  12801. (if (looking-at org-priority-regexp)
  12802. (setq current (string-to-char (match-string 2))
  12803. have t))
  12804. (cond
  12805. ((eq action 'remove)
  12806. (setq remove t new ?\ ))
  12807. ((or (eq action 'set)
  12808. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12809. (if (not (eq action 'set))
  12810. (setq new action)
  12811. (message "Priority %c-%c, SPC to remove: "
  12812. org-highest-priority org-lowest-priority)
  12813. (save-match-data
  12814. (setq new (read-char-exclusive))))
  12815. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12816. (= (upcase org-lowest-priority) org-lowest-priority))
  12817. (setq new (upcase new)))
  12818. (cond ((equal new ?\ ) (setq remove t))
  12819. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12820. (user-error "Priority must be between `%c' and `%c'"
  12821. org-highest-priority org-lowest-priority))))
  12822. ((eq action 'up)
  12823. (setq new (if have
  12824. (1- current) ; normal cycling
  12825. ;; last priority was empty
  12826. (if (eq last-command this-command)
  12827. org-lowest-priority ; wrap around empty to lowest
  12828. ;; default
  12829. (if org-priority-start-cycle-with-default
  12830. org-default-priority
  12831. (1- org-default-priority))))))
  12832. ((eq action 'down)
  12833. (setq new (if have
  12834. (1+ current) ; normal cycling
  12835. ;; last priority was empty
  12836. (if (eq last-command this-command)
  12837. org-highest-priority ; wrap around empty to highest
  12838. ;; default
  12839. (if org-priority-start-cycle-with-default
  12840. org-default-priority
  12841. (1+ org-default-priority))))))
  12842. (t (user-error "Invalid action")))
  12843. (if (or (< (upcase new) org-highest-priority)
  12844. (> (upcase new) org-lowest-priority))
  12845. (if (and (memq action '(up down))
  12846. (not have) (not (eq last-command this-command)))
  12847. ;; `new' is from default priority
  12848. (error
  12849. "The default can not be set, see `org-default-priority' why")
  12850. ;; normal cycling: `new' is beyond highest/lowest priority
  12851. ;; and is wrapped around to the empty priority
  12852. (setq remove t)))
  12853. (setq news (format "%c" new))
  12854. (if have
  12855. (if remove
  12856. (replace-match "" t t nil 1)
  12857. (replace-match news t t nil 2))
  12858. (if remove
  12859. (user-error "No priority cookie found in line")
  12860. (let ((case-fold-search nil))
  12861. (looking-at org-todo-line-regexp))
  12862. (if (match-end 2)
  12863. (progn
  12864. (goto-char (match-end 2))
  12865. (insert " [#" news "]"))
  12866. (goto-char (match-beginning 3))
  12867. (insert "[#" news "] "))))
  12868. (org-set-tags nil 'align))
  12869. (if remove
  12870. (message "Priority removed")
  12871. (message "Priority of current item set to %s" news)))))
  12872. (defun org-show-priority ()
  12873. "Show the priority of the current item.
  12874. This priority is composed of the main priority given with the [#A] cookies,
  12875. and by additional input from the age of a schedules or deadline entry."
  12876. (interactive)
  12877. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12878. (org-get-at-bol 'priority)
  12879. (save-excursion
  12880. (save-match-data
  12881. (beginning-of-line)
  12882. (and (looking-at org-heading-regexp)
  12883. (org-get-priority (match-string 0))))))))
  12884. (message "Priority is %d" (if pri pri -1000))))
  12885. (defun org-get-priority (s)
  12886. "Find priority cookie and return priority."
  12887. (save-match-data
  12888. (if (functionp org-get-priority-function)
  12889. (funcall org-get-priority-function)
  12890. (if (not (string-match org-priority-regexp s))
  12891. (* 1000 (- org-lowest-priority org-default-priority))
  12892. (* 1000 (- org-lowest-priority
  12893. (string-to-char (match-string 2 s))))))))
  12894. ;;;; Tags
  12895. (defvar org-agenda-archives-mode)
  12896. (defvar org-map-continue-from nil
  12897. "Position from where mapping should continue.
  12898. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12899. (defvar org-scanner-tags nil
  12900. "The current tag list while the tags scanner is running.")
  12901. (defvar org-trust-scanner-tags nil
  12902. "Should `org-get-tags-at' use the tags for the scanner.
  12903. This is for internal dynamical scoping only.
  12904. When this is non-nil, the function `org-get-tags-at' will return the value
  12905. of `org-scanner-tags' instead of building the list by itself. This
  12906. can lead to large speed-ups when the tags scanner is used in a file with
  12907. many entries, and when the list of tags is retrieved, for example to
  12908. obtain a list of properties. Building the tags list for each entry in such
  12909. a file becomes an N^2 operation - but with this variable set, it scales
  12910. as N.")
  12911. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12912. "Scan headline tags with inheritance and produce output ACTION.
  12913. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12914. or `agenda' to produce an entry list for an agenda view. It can also be
  12915. a Lisp form or a function that should be called at each matched headline, in
  12916. this case the return value is a list of all return values from these calls.
  12917. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12918. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12919. only lines with a not-done TODO keyword are included in the output.
  12920. This should be the same variable that was scoped into
  12921. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12922. START-LEVEL can be a string with asterisks, reducing the scope to
  12923. headlines matching this string."
  12924. (require 'org-agenda)
  12925. (let* ((re (concat "^"
  12926. (if start-level
  12927. ;; Get the correct level to match
  12928. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12929. org-outline-regexp)
  12930. " *\\(\\<\\("
  12931. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12932. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12933. (props (list 'face 'default
  12934. 'done-face 'org-agenda-done
  12935. 'undone-face 'default
  12936. 'mouse-face 'highlight
  12937. 'org-not-done-regexp org-not-done-regexp
  12938. 'org-todo-regexp org-todo-regexp
  12939. 'org-complex-heading-regexp org-complex-heading-regexp
  12940. 'help-echo
  12941. (format "mouse-2 or RET jump to org file %s"
  12942. (abbreviate-file-name
  12943. (or (buffer-file-name (buffer-base-buffer))
  12944. (buffer-name (buffer-base-buffer)))))))
  12945. (org-map-continue-from nil)
  12946. lspos tags tags-list
  12947. (tags-alist (list (cons 0 org-file-tags)))
  12948. (llast 0) rtn rtn1 level category i txt
  12949. todo marker entry priority
  12950. ts-date ts-date-type ts-date-pair)
  12951. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12952. (setq action (list 'lambda nil action)))
  12953. (save-excursion
  12954. (goto-char (point-min))
  12955. (when (eq action 'sparse-tree)
  12956. (org-overview)
  12957. (org-remove-occur-highlights))
  12958. (while (let (case-fold-search)
  12959. (re-search-forward re nil t))
  12960. (setq org-map-continue-from nil)
  12961. (catch :skip
  12962. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12963. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12964. (goto-char (setq lspos (match-beginning 0)))
  12965. (setq level (org-reduced-level (org-outline-level))
  12966. category (org-get-category))
  12967. (when (eq action 'agenda)
  12968. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12969. ts-date (car ts-date-pair)
  12970. ts-date-type (cdr ts-date-pair)))
  12971. (setq i llast llast level)
  12972. ;; remove tag lists from same and sublevels
  12973. (while (>= i level)
  12974. (when (setq entry (assoc i tags-alist))
  12975. (setq tags-alist (delete entry tags-alist)))
  12976. (setq i (1- i)))
  12977. ;; add the next tags
  12978. (when tags
  12979. (setq tags (org-split-string tags ":")
  12980. tags-alist
  12981. (cons (cons level tags) tags-alist)))
  12982. ;; compile tags for current headline
  12983. (setq tags-list
  12984. (if org-use-tag-inheritance
  12985. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12986. tags)
  12987. org-scanner-tags tags-list)
  12988. (when org-use-tag-inheritance
  12989. (setcdr (car tags-alist)
  12990. (mapcar (lambda (x)
  12991. (setq x (copy-sequence x))
  12992. (org-add-prop-inherited x))
  12993. (cdar tags-alist))))
  12994. (when (and tags org-use-tag-inheritance
  12995. (or (not (eq t org-use-tag-inheritance))
  12996. org-tags-exclude-from-inheritance))
  12997. ;; selective inheritance, remove uninherited ones
  12998. (setcdr (car tags-alist)
  12999. (org-remove-uninherited-tags (cdar tags-alist))))
  13000. (when (and
  13001. ;; eval matcher only when the todo condition is OK
  13002. (and (or (not todo-only) (member todo org-not-done-keywords))
  13003. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13004. (eval matcher)))
  13005. ;; Call the skipper, but return t if it does not skip,
  13006. ;; so that the `and' form continues evaluating
  13007. (progn
  13008. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13009. t)
  13010. ;; Check if timestamps are deselecting this entry
  13011. (or (not todo-only)
  13012. (and (member todo org-not-done-keywords)
  13013. (or (not org-agenda-tags-todo-honor-ignore-options)
  13014. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13015. ;; select this headline
  13016. (cond
  13017. ((eq action 'sparse-tree)
  13018. (and org-highlight-sparse-tree-matches
  13019. (org-get-heading) (match-end 0)
  13020. (org-highlight-new-match
  13021. (match-beginning 1) (match-end 1)))
  13022. (org-show-context 'tags-tree))
  13023. ((eq action 'agenda)
  13024. (setq txt (org-agenda-format-item
  13025. ""
  13026. (concat
  13027. (if (eq org-tags-match-list-sublevels 'indented)
  13028. (make-string (1- level) ?.) "")
  13029. (org-get-heading))
  13030. (make-string level ?\s)
  13031. category
  13032. tags-list)
  13033. priority (org-get-priority txt))
  13034. (goto-char lspos)
  13035. (setq marker (org-agenda-new-marker))
  13036. (org-add-props txt props
  13037. 'org-marker marker 'org-hd-marker marker 'org-category category
  13038. 'todo-state todo
  13039. 'ts-date ts-date
  13040. 'priority priority
  13041. 'type (concat "tagsmatch" ts-date-type))
  13042. (push txt rtn))
  13043. ((functionp action)
  13044. (setq org-map-continue-from nil)
  13045. (save-excursion
  13046. (setq rtn1 (funcall action))
  13047. (push rtn1 rtn)))
  13048. (t (user-error "Invalid action")))
  13049. ;; if we are to skip sublevels, jump to end of subtree
  13050. (unless org-tags-match-list-sublevels
  13051. (org-end-of-subtree t)
  13052. (backward-char 1))))
  13053. ;; Get the correct position from where to continue
  13054. (if org-map-continue-from
  13055. (goto-char org-map-continue-from)
  13056. (and (= (point) lspos) (end-of-line 1)))))
  13057. (when (and (eq action 'sparse-tree)
  13058. (not org-sparse-tree-open-archived-trees))
  13059. (org-hide-archived-subtrees (point-min) (point-max)))
  13060. (nreverse rtn)))
  13061. (defun org-remove-uninherited-tags (tags)
  13062. "Remove all tags that are not inherited from the list TAGS."
  13063. (cond
  13064. ((eq org-use-tag-inheritance t)
  13065. (if org-tags-exclude-from-inheritance
  13066. (org-delete-all org-tags-exclude-from-inheritance tags)
  13067. tags))
  13068. ((not org-use-tag-inheritance) nil)
  13069. ((stringp org-use-tag-inheritance)
  13070. (delq nil (mapcar
  13071. (lambda (x)
  13072. (if (and (string-match org-use-tag-inheritance x)
  13073. (not (member x org-tags-exclude-from-inheritance)))
  13074. x nil))
  13075. tags)))
  13076. ((listp org-use-tag-inheritance)
  13077. (delq nil (mapcar
  13078. (lambda (x)
  13079. (if (member x org-use-tag-inheritance) x nil))
  13080. tags)))))
  13081. (defun org-match-sparse-tree (&optional todo-only match)
  13082. "Create a sparse tree according to tags string MATCH.
  13083. MATCH can contain positive and negative selection of tags, like
  13084. \"+WORK+URGENT-WITHBOSS\".
  13085. If optional argument TODO-ONLY is non-nil, only select lines that are
  13086. also TODO lines."
  13087. (interactive "P")
  13088. (org-agenda-prepare-buffers (list (current-buffer)))
  13089. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13090. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13091. (defvar org-cached-props nil)
  13092. (defun org-cached-entry-get (pom property)
  13093. (if (or (eq t org-use-property-inheritance)
  13094. (and (stringp org-use-property-inheritance)
  13095. (let ((case-fold-search t))
  13096. (org-string-match-p org-use-property-inheritance property)))
  13097. (and (listp org-use-property-inheritance)
  13098. (member-ignore-case property org-use-property-inheritance)))
  13099. ;; Caching is not possible, check it directly.
  13100. (org-entry-get pom property 'inherit)
  13101. ;; Get all properties, so we can do complicated checks easily.
  13102. (cdr (assoc-string property
  13103. (or org-cached-props
  13104. (setq org-cached-props (org-entry-properties pom)))
  13105. t))))
  13106. (defun org-global-tags-completion-table (&optional files)
  13107. "Return the list of all tags in all agenda buffer/files.
  13108. Optional FILES argument is a list of files which can be used
  13109. instead of the agenda files."
  13110. (save-excursion
  13111. (org-uniquify
  13112. (delq nil
  13113. (apply 'append
  13114. (mapcar
  13115. (lambda (file)
  13116. (set-buffer (find-file-noselect file))
  13117. (append (org-get-buffer-tags)
  13118. (mapcar (lambda (x) (if (stringp (car-safe x))
  13119. (list (car-safe x)) nil))
  13120. org-tag-alist)))
  13121. (if (and files (car files))
  13122. files
  13123. (org-agenda-files))))))))
  13124. (defun org-make-tags-matcher (match)
  13125. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13126. The variable `todo-only' is scoped dynamically into this function.
  13127. It will be set to t if the matcher restricts matching to TODO entries,
  13128. otherwise will not be touched.
  13129. Returns a cons of the selection string MATCH and the constructed
  13130. lisp form implementing the matcher. The matcher is to be evaluated
  13131. at an Org entry, with point on the headline, and returns t if the
  13132. entry matches the selection string MATCH. The returned lisp form
  13133. references two variables with information about the entry, which
  13134. must be bound around the form's evaluation: todo, the TODO keyword
  13135. at the entry (or nil of none); and tags-list, the list of all tags
  13136. at the entry including inherited ones. Additionally, the category
  13137. of the entry (if any) must be specified as the text property
  13138. 'org-category on the headline.
  13139. See also `org-scan-tags'.
  13140. "
  13141. (declare (special todo-only))
  13142. (unless (boundp 'todo-only)
  13143. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13144. (unless match
  13145. ;; Get a new match request, with completion against the global
  13146. ;; tags table and the local tags in current buffer
  13147. (let ((org-last-tags-completion-table
  13148. (org-uniquify
  13149. (delq nil (append (org-get-buffer-tags)
  13150. (org-global-tags-completion-table))))))
  13151. (setq match (org-completing-read-no-i
  13152. "Match: " 'org-tags-completion-function nil nil nil
  13153. 'org-tags-history))))
  13154. ;; Parse the string and create a lisp form
  13155. (let ((match0 match)
  13156. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13157. minus tag mm
  13158. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13159. orterms term orlist re-p str-p level-p level-op time-p
  13160. prop-p pn pv po gv rest (start 0) (ss 0))
  13161. ;; Expand group tags
  13162. (setq match (org-tags-expand match))
  13163. ;; Check if there is a TODO part of this match, which would be the
  13164. ;; part after a "/". TO make sure that this slash is not part of
  13165. ;; a property value to be matched against, we also check that there
  13166. ;; is no " after that slash.
  13167. ;; First, find the last slash
  13168. (while (string-match "/+" match ss)
  13169. (setq start (match-beginning 0) ss (match-end 0)))
  13170. (if (and (string-match "/+" match start)
  13171. (not (save-match-data (string-match "\"" match start))))
  13172. ;; match contains also a todo-matching request
  13173. (progn
  13174. (setq tagsmatch (substring match 0 (match-beginning 0))
  13175. todomatch (substring match (match-end 0)))
  13176. (if (string-match "^!" todomatch)
  13177. (setq todo-only t todomatch (substring todomatch 1)))
  13178. (if (string-match "^\\s-*$" todomatch)
  13179. (setq todomatch nil)))
  13180. ;; only matching tags
  13181. (setq tagsmatch match todomatch nil))
  13182. ;; Make the tags matcher
  13183. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13184. (setq tagsmatcher t)
  13185. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13186. (while (setq term (pop orterms))
  13187. (while (and (equal (substring term -1) "\\") orterms)
  13188. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13189. (while (string-match re term)
  13190. (setq rest (substring term (match-end 0))
  13191. minus (and (match-end 1)
  13192. (equal (match-string 1 term) "-"))
  13193. tag (save-match-data (replace-regexp-in-string
  13194. "\\\\-" "-"
  13195. (match-string 2 term)))
  13196. re-p (equal (string-to-char tag) ?{)
  13197. level-p (match-end 4)
  13198. prop-p (match-end 5)
  13199. mm (cond
  13200. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13201. (level-p
  13202. (setq level-op (org-op-to-function (match-string 3 term)))
  13203. `(,level-op level ,(string-to-number
  13204. (match-string 4 term))))
  13205. (prop-p
  13206. (setq pn (match-string 5 term)
  13207. po (match-string 6 term)
  13208. pv (match-string 7 term)
  13209. re-p (equal (string-to-char pv) ?{)
  13210. str-p (equal (string-to-char pv) ?\")
  13211. time-p (save-match-data
  13212. (string-match "^\"[[<].*[]>]\"$" pv))
  13213. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13214. (if time-p (setq pv (org-matcher-time pv)))
  13215. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13216. (cond
  13217. ((equal pn "CATEGORY")
  13218. (setq gv '(get-text-property (point) 'org-category)))
  13219. ((equal pn "TODO")
  13220. (setq gv 'todo))
  13221. (t
  13222. (setq gv `(org-cached-entry-get nil ,pn))))
  13223. (if re-p
  13224. (if (eq po 'org<>)
  13225. `(not (string-match ,pv (or ,gv "")))
  13226. `(string-match ,pv (or ,gv "")))
  13227. (if str-p
  13228. `(,po (or ,gv "") ,pv)
  13229. `(,po (string-to-number (or ,gv ""))
  13230. ,(string-to-number pv) ))))
  13231. (t `(member ,tag tags-list)))
  13232. mm (if minus (list 'not mm) mm)
  13233. term rest)
  13234. (push mm tagsmatcher))
  13235. (push (if (> (length tagsmatcher) 1)
  13236. (cons 'and tagsmatcher)
  13237. (car tagsmatcher))
  13238. orlist)
  13239. (setq tagsmatcher nil))
  13240. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13241. (setq tagsmatcher
  13242. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13243. ;; Make the todo matcher
  13244. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13245. (setq todomatcher t)
  13246. (setq orterms (org-split-string todomatch "|") orlist nil)
  13247. (dolist (term orterms)
  13248. (while (string-match re term)
  13249. (setq minus (and (match-end 1)
  13250. (equal (match-string 1 term) "-"))
  13251. kwd (match-string 2 term)
  13252. re-p (equal (string-to-char kwd) ?{)
  13253. term (substring term (match-end 0))
  13254. mm (if re-p
  13255. `(string-match ,(substring kwd 1 -1) todo)
  13256. (list 'equal 'todo kwd))
  13257. mm (if minus (list 'not mm) mm))
  13258. (push mm todomatcher))
  13259. (push (if (> (length todomatcher) 1)
  13260. (cons 'and todomatcher)
  13261. (car todomatcher))
  13262. orlist)
  13263. (setq todomatcher nil))
  13264. (setq todomatcher (if (> (length orlist) 1)
  13265. (cons 'or orlist) (car orlist))))
  13266. ;; Return the string and lisp forms of the matcher
  13267. (setq matcher (if todomatcher
  13268. (list 'and tagsmatcher todomatcher)
  13269. tagsmatcher))
  13270. (when todo-only
  13271. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13272. matcher)))
  13273. (cons match0 matcher)))
  13274. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13275. "Expand group tags in MATCH.
  13276. This replaces every group tag in MATCH with a regexp tag search.
  13277. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13278. will be replaced like this:
  13279. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13280. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13281. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13282. Replacing by a regexp preserves the structure of the match.
  13283. E.g., this expansion
  13284. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13285. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13286. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13287. A group tag in MATCH can contain regular expressions of its own.
  13288. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13289. will be replaced like this:
  13290. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13291. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13292. assumed to be a single group tag, and the function will return
  13293. the list of tags in this group.
  13294. When DOWNCASE is non-nil, expand downcased TAGS."
  13295. (if org-group-tags
  13296. (let* ((case-fold-search t)
  13297. (stable org-mode-syntax-table)
  13298. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13299. (taggroups (if downcased
  13300. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13301. taggroups)
  13302. taggroups))
  13303. (taggroups-keys (mapcar #'car taggroups))
  13304. (return-match (if downcased (downcase match) match))
  13305. (count 0)
  13306. (work-already-expanded tags-already-expanded)
  13307. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13308. ;; @ and _ are allowed as word-components in tags.
  13309. (modify-syntax-entry ?@ "w" stable)
  13310. (modify-syntax-entry ?_ "w" stable)
  13311. ;; Temporarily replace regexp-expressions in the match-expression.
  13312. (while (string-match "{.+?}" return-match)
  13313. (incf count)
  13314. (push (match-string 0 return-match) regexps-in-match)
  13315. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13316. (while (and taggroups-keys
  13317. (with-syntax-table stable
  13318. (string-match
  13319. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13320. (regexp-opt taggroups-keys) "\\>\\)")
  13321. return-match)))
  13322. (let* ((dir (match-string 1 return-match))
  13323. (tag (match-string 2 return-match))
  13324. (tag (if downcased (downcase tag) tag)))
  13325. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13326. (member tag work-already-expanded))
  13327. (setq tags-in-group (assoc tag taggroups))
  13328. (push tag work-already-expanded)
  13329. ;; Recursively expand each tag in the group, if the tag hasn't
  13330. ;; already been expanded. Restore the match-data after all recursive calls.
  13331. (save-match-data
  13332. (let (tags-expanded)
  13333. (dolist (x (cdr tags-in-group))
  13334. (if (and (member x taggroups-keys)
  13335. (not (member x work-already-expanded)))
  13336. (setq tags-expanded
  13337. (delete-dups
  13338. (append
  13339. (org-tags-expand x t downcased
  13340. work-already-expanded)
  13341. tags-expanded)))
  13342. (setq tags-expanded
  13343. (append (list x) tags-expanded)))
  13344. (setq work-already-expanded
  13345. (delete-dups
  13346. (append tags-expanded
  13347. work-already-expanded))))
  13348. (setq tags-in-group
  13349. (delete-dups (cons (car tags-in-group)
  13350. tags-expanded)))))
  13351. ;; Filter tag-regexps from tags.
  13352. (setq regexp-in-group-escaped
  13353. (delq nil (mapcar (lambda (x)
  13354. (if (stringp x)
  13355. (and (equal "{" (substring x 0 1))
  13356. (equal "}" (substring x -1))
  13357. x)
  13358. x))
  13359. tags-in-group))
  13360. regexp-in-group
  13361. (mapcar (lambda (x)
  13362. (substring x 1 -1))
  13363. regexp-in-group-escaped)
  13364. tags-in-group
  13365. (delq nil (mapcar (lambda (x)
  13366. (if (stringp x)
  13367. (and (not (equal "{" (substring x 0 1)))
  13368. (not (equal "}" (substring x -1)))
  13369. x)
  13370. x))
  13371. tags-in-group)))
  13372. ;; If single-as-list, do no more in the while-loop.
  13373. (if (not single-as-list)
  13374. (progn
  13375. (when regexp-in-group
  13376. (setq regexp-in-group
  13377. (concat "\\|"
  13378. (mapconcat 'identity regexp-in-group
  13379. "\\|"))))
  13380. (setq tags-in-group
  13381. (concat dir
  13382. "{\\<"
  13383. (regexp-opt tags-in-group)
  13384. "\\>"
  13385. regexp-in-group
  13386. "}"))
  13387. (when (stringp tags-in-group)
  13388. (org-add-props tags-in-group '(grouptag t)))
  13389. (setq return-match
  13390. (replace-match tags-in-group t t return-match)))
  13391. (setq tags-in-group
  13392. (append regexp-in-group-escaped tags-in-group))))
  13393. (setq taggroups-keys (delete tag taggroups-keys))))
  13394. ;; Add the regular expressions back into the match-expression again.
  13395. (while regexps-in-match
  13396. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13397. (pop regexps-in-match)
  13398. return-match t t))
  13399. (decf count))
  13400. (if single-as-list
  13401. (if tags-in-group tags-in-group (list return-match))
  13402. return-match))
  13403. (if single-as-list
  13404. (list (if downcased (downcase match) match))
  13405. match)))
  13406. (defun org-op-to-function (op &optional stringp)
  13407. "Turn an operator into the appropriate function."
  13408. (setq op
  13409. (cond
  13410. ((equal op "<" ) '(< string< org-time<))
  13411. ((equal op ">" ) '(> org-string> org-time>))
  13412. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13413. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13414. ((member op '("=" "==")) '(= string= org-time=))
  13415. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13416. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13417. (defun org<> (a b) (not (= a b)))
  13418. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13419. (defun org-string>= (a b) (not (string< a b)))
  13420. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13421. (defun org-string<> (a b) (not (string= a b)))
  13422. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13423. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13424. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13425. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13426. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13427. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13428. (defun org-2ft (s)
  13429. "Convert S to a floating point time.
  13430. If S is already a number, just return it. If it is a string, parse
  13431. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13432. (cond
  13433. ((numberp s) s)
  13434. ((stringp s)
  13435. (condition-case nil
  13436. (float-time (apply 'encode-time (org-parse-time-string s)))
  13437. (error 0.)))
  13438. (t 0.)))
  13439. (defun org-time-today ()
  13440. "Time in seconds today at 0:00.
  13441. Returns the float number of seconds since the beginning of the
  13442. epoch to the beginning of today (00:00)."
  13443. (float-time (apply 'encode-time
  13444. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13445. (defun org-matcher-time (s)
  13446. "Interpret a time comparison value."
  13447. (save-match-data
  13448. (cond
  13449. ((string= s "<now>") (float-time))
  13450. ((string= s "<today>") (org-time-today))
  13451. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13452. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13453. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13454. (+ (org-time-today)
  13455. (* (string-to-number (match-string 1 s))
  13456. (cdr (assoc (match-string 2 s)
  13457. '(("d" . 86400.0) ("w" . 604800.0)
  13458. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13459. (t (org-2ft s)))))
  13460. (defun org-match-any-p (re list)
  13461. "Does re match any element of list?"
  13462. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13463. (delq nil list))
  13464. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13465. (defvar org-tags-overlay (make-overlay 1 1))
  13466. (org-detach-overlay org-tags-overlay)
  13467. (defun org-get-local-tags-at (&optional pos)
  13468. "Get a list of tags defined in the current headline."
  13469. (org-get-tags-at pos 'local))
  13470. (defun org-get-local-tags ()
  13471. "Get a list of tags defined in the current headline."
  13472. (org-get-tags-at nil 'local))
  13473. (defun org-get-tags-at (&optional pos local)
  13474. "Get a list of all headline tags applicable at POS.
  13475. POS defaults to point. If tags are inherited, the list contains
  13476. the targets in the same sequence as the headlines appear, i.e.
  13477. the tags of the current headline come last.
  13478. When LOCAL is non-nil, only return tags from the current headline,
  13479. ignore inherited ones."
  13480. (interactive)
  13481. (if (and org-trust-scanner-tags
  13482. (or (not pos) (equal pos (point)))
  13483. (not local))
  13484. org-scanner-tags
  13485. (let (tags ltags lastpos parent)
  13486. (save-excursion
  13487. (save-restriction
  13488. (widen)
  13489. (goto-char (or pos (point)))
  13490. (save-match-data
  13491. (catch 'done
  13492. (condition-case nil
  13493. (progn
  13494. (org-back-to-heading t)
  13495. (while (not (equal lastpos (point)))
  13496. (setq lastpos (point))
  13497. (when (looking-at
  13498. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13499. (setq ltags (org-split-string
  13500. (org-match-string-no-properties 1) ":"))
  13501. (when parent
  13502. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13503. (setq tags (append
  13504. (if parent
  13505. (org-remove-uninherited-tags ltags)
  13506. ltags)
  13507. tags)))
  13508. (or org-use-tag-inheritance (throw 'done t))
  13509. (if local (throw 'done t))
  13510. (or (org-up-heading-safe) (error nil))
  13511. (setq parent t)))
  13512. (error nil)))))
  13513. (if local
  13514. tags
  13515. (reverse (delete-dups
  13516. (reverse (append
  13517. (org-remove-uninherited-tags
  13518. org-file-tags)
  13519. tags)))))))))
  13520. (defun org-add-prop-inherited (s)
  13521. (add-text-properties 0 (length s) '(inherited t) s)
  13522. s)
  13523. (defun org-toggle-tag (tag &optional onoff)
  13524. "Toggle the tag TAG for the current line.
  13525. If ONOFF is `on' or `off', don't toggle but set to this state."
  13526. (let (res current)
  13527. (save-excursion
  13528. (org-back-to-heading t)
  13529. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13530. (point-at-eol) t)
  13531. (progn
  13532. (setq current (match-string 1))
  13533. (replace-match ""))
  13534. (setq current ""))
  13535. (setq current (nreverse (org-split-string current ":")))
  13536. (cond
  13537. ((eq onoff 'on)
  13538. (setq res t)
  13539. (or (member tag current) (push tag current)))
  13540. ((eq onoff 'off)
  13541. (or (not (member tag current)) (setq current (delete tag current))))
  13542. (t (if (member tag current)
  13543. (setq current (delete tag current))
  13544. (setq res t)
  13545. (push tag current))))
  13546. (end-of-line 1)
  13547. (if current
  13548. (progn
  13549. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13550. (org-set-tags nil t))
  13551. (delete-horizontal-space))
  13552. (run-hooks 'org-after-tags-change-hook))
  13553. res))
  13554. (defun org-align-tags-here (to-col)
  13555. ;; Assumes that this is a headline
  13556. "Align tags on the current headline to TO-COL."
  13557. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13558. (beginning-of-line 1)
  13559. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13560. (< pos (match-beginning 2)))
  13561. (progn
  13562. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13563. (goto-char (match-beginning 1))
  13564. (insert " ")
  13565. (delete-region (point) (1+ (match-beginning 2)))
  13566. (setq ncol (max (current-column)
  13567. (1+ col)
  13568. (if (> to-col 0)
  13569. to-col
  13570. (- (abs to-col) tags-l))))
  13571. (setq p (point))
  13572. (insert (make-string (- ncol (current-column)) ?\ ))
  13573. (setq ncol (current-column))
  13574. (when indent-tabs-mode (tabify p (point-at-eol)))
  13575. (org-move-to-column (min ncol col)))
  13576. (goto-char pos))))
  13577. (defun org-set-tags-command (&optional arg just-align)
  13578. "Call the set-tags command for the current entry."
  13579. (interactive "P")
  13580. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13581. (org-set-tags arg just-align)
  13582. (save-excursion
  13583. (unless (and (org-region-active-p)
  13584. org-loop-over-headlines-in-active-region)
  13585. (org-back-to-heading t))
  13586. (org-set-tags arg just-align))))
  13587. (defun org-set-tags-to (data)
  13588. "Set the tags of the current entry to DATA, replacing the current tags.
  13589. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13590. If DATA is nil or the empty string, any tags will be removed."
  13591. (interactive "sTags: ")
  13592. (setq data
  13593. (cond
  13594. ((eq data nil) "")
  13595. ((equal data "") "")
  13596. ((stringp data)
  13597. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13598. ":"))
  13599. ((listp data)
  13600. (concat ":" (mapconcat 'identity data ":") ":"))))
  13601. (when data
  13602. (save-excursion
  13603. (org-back-to-heading t)
  13604. (when (looking-at org-complex-heading-regexp)
  13605. (if (match-end 5)
  13606. (progn
  13607. (goto-char (match-beginning 5))
  13608. (insert data)
  13609. (delete-region (point) (point-at-eol))
  13610. (org-set-tags nil 'align))
  13611. (goto-char (point-at-eol))
  13612. (insert " " data)
  13613. (org-set-tags nil 'align)))
  13614. (beginning-of-line 1)
  13615. (if (looking-at ".*?\\([ \t]+\\)$")
  13616. (delete-region (match-beginning 1) (match-end 1))))))
  13617. (defun org-align-all-tags ()
  13618. "Align the tags i all headings."
  13619. (interactive)
  13620. (save-excursion
  13621. (or (ignore-errors (org-back-to-heading t))
  13622. (outline-next-heading))
  13623. (if (org-at-heading-p)
  13624. (org-set-tags t)
  13625. (message "No headings"))))
  13626. (defvar org-indent-indentation-per-level)
  13627. (defun org-set-tags (&optional arg just-align)
  13628. "Set the tags for the current headline.
  13629. With prefix ARG, realign all tags in headings in the current buffer.
  13630. When JUST-ALIGN is non-nil, only align tags."
  13631. (interactive "P")
  13632. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13633. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13634. 'region-start-level
  13635. 'region))
  13636. org-loop-over-headlines-in-active-region)
  13637. (org-map-entries
  13638. ;; We don't use ARG and JUST-ALIGN here because these args
  13639. ;; are not useful when looping over headlines.
  13640. #'org-set-tags
  13641. org-loop-over-headlines-in-active-region
  13642. cl
  13643. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13644. (let ((org-setting-tags t))
  13645. (if arg
  13646. (save-excursion
  13647. (goto-char (point-min))
  13648. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13649. (while (re-search-forward org-outline-regexp-bol nil t)
  13650. (org-set-tags nil t)
  13651. (end-of-line)))
  13652. (message "All tags realigned to column %d" org-tags-column))
  13653. (let* ((current (org-get-tags-string))
  13654. (col (current-column))
  13655. (tags
  13656. (if just-align current
  13657. ;; Get a new set of tags from the user.
  13658. (save-excursion
  13659. (let* ((table
  13660. (setq
  13661. org-last-tags-completion-table
  13662. (append
  13663. org-tag-persistent-alist
  13664. (or org-tag-alist (org-get-buffer-tags))
  13665. (and
  13666. org-complete-tags-always-offer-all-agenda-tags
  13667. (org-global-tags-completion-table
  13668. (org-agenda-files))))))
  13669. (current-tags (org-split-string current ":"))
  13670. (inherited-tags
  13671. (nreverse (nthcdr (length current-tags)
  13672. (nreverse (org-get-tags-at))))))
  13673. (replace-regexp-in-string
  13674. "\\([-+&]+\\|,\\)"
  13675. ":"
  13676. (if (or (eq t org-use-fast-tag-selection)
  13677. (and org-use-fast-tag-selection
  13678. (delq nil (mapcar #'cdr table))))
  13679. (org-fast-tag-selection
  13680. current-tags inherited-tags table
  13681. (and org-fast-tag-selection-include-todo
  13682. org-todo-key-alist))
  13683. (let ((org-add-colon-after-tag-completion
  13684. (< 1 (length table))))
  13685. (org-trim
  13686. (completing-read
  13687. "Tags: "
  13688. #'org-tags-completion-function
  13689. nil nil current 'org-tags-history))))))))))
  13690. (when org-tags-sort-function
  13691. (setq tags
  13692. (mapconcat
  13693. #'identity
  13694. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13695. org-tags-sort-function)
  13696. ":")))
  13697. (if (not (org-string-nw-p tags)) (setq tags "")
  13698. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13699. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13700. ;; Insert new tags at the correct column
  13701. (beginning-of-line)
  13702. (let ((level (if (looking-at org-outline-regexp)
  13703. (- (match-end 0) (point) 1)
  13704. 1)))
  13705. (cond
  13706. ((and (equal current "") (equal tags "")))
  13707. ((re-search-forward
  13708. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13709. (line-end-position)
  13710. t)
  13711. (if (equal tags "") (replace-match "" t t)
  13712. (goto-char (match-beginning 0))
  13713. (let* ((c0 (current-column))
  13714. ;; Compute offset for the case of org-indent-mode
  13715. ;; active.
  13716. (di (if (org-bound-and-true-p org-indent-mode)
  13717. (* (1- org-indent-indentation-per-level)
  13718. (1- level))
  13719. 0))
  13720. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13721. (tc (+ org-tags-column
  13722. (if (> org-tags-column 0) (- di) di)))
  13723. (c1 (max (1+ c0)
  13724. (if (> tc 0) tc
  13725. (- (- tc) (string-width tags)))))
  13726. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13727. (replace-match rpl t t)
  13728. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13729. (tabify p0 (point))))))
  13730. (t (error "Tags alignment failed"))))
  13731. (org-move-to-column col))
  13732. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13733. (defun org-change-tag-in-region (beg end tag off)
  13734. "Add or remove TAG for each entry in the region.
  13735. This works in the agenda, and also in an org-mode buffer."
  13736. (interactive
  13737. (list (region-beginning) (region-end)
  13738. (let ((org-last-tags-completion-table
  13739. (if (derived-mode-p 'org-mode)
  13740. (org-uniquify
  13741. (delq nil (append (org-get-buffer-tags)
  13742. (org-global-tags-completion-table))))
  13743. (org-global-tags-completion-table))))
  13744. (org-icompleting-read
  13745. "Tag: " 'org-tags-completion-function nil nil nil
  13746. 'org-tags-history))
  13747. (progn
  13748. (message "[s]et or [r]emove? ")
  13749. (equal (read-char-exclusive) ?r))))
  13750. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13751. (let ((agendap (equal major-mode 'org-agenda-mode))
  13752. l1 l2 m buf pos newhead (cnt 0))
  13753. (goto-char end)
  13754. (setq l2 (1- (org-current-line)))
  13755. (goto-char beg)
  13756. (setq l1 (org-current-line))
  13757. (loop for l from l1 to l2 do
  13758. (org-goto-line l)
  13759. (setq m (get-text-property (point) 'org-hd-marker))
  13760. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13761. (and agendap m))
  13762. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13763. pos (if agendap m (point)))
  13764. (with-current-buffer buf
  13765. (save-excursion
  13766. (save-restriction
  13767. (goto-char pos)
  13768. (setq cnt (1+ cnt))
  13769. (org-toggle-tag tag (if off 'off 'on))
  13770. (setq newhead (org-get-heading)))))
  13771. (and agendap (org-agenda-change-all-lines newhead m))))
  13772. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13773. (defun org-tags-completion-function (string predicate &optional flag)
  13774. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13775. (confirm (lambda (x) (stringp (car x)))))
  13776. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13777. (setq s1 (match-string 1 string)
  13778. s2 (match-string 2 string))
  13779. (setq s1 "" s2 string))
  13780. (cond
  13781. ((eq flag nil)
  13782. ;; try completion
  13783. (setq rtn (try-completion s2 ctable confirm))
  13784. (if (stringp rtn)
  13785. (setq rtn
  13786. (concat s1 s2 (substring rtn (length s2))
  13787. (if (and org-add-colon-after-tag-completion
  13788. (assoc rtn ctable))
  13789. ":" ""))))
  13790. rtn)
  13791. ((eq flag t)
  13792. ;; all-completions
  13793. (all-completions s2 ctable confirm))
  13794. ((eq flag 'lambda)
  13795. ;; exact match?
  13796. (assoc s2 ctable)))))
  13797. (defun org-fast-tag-insert (kwd tags face &optional end)
  13798. "Insert KDW, and the TAGS, the latter with face FACE.
  13799. Also insert END."
  13800. (insert (format "%-12s" (concat kwd ":"))
  13801. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13802. (or end "")))
  13803. (defun org-fast-tag-show-exit (flag)
  13804. (save-excursion
  13805. (org-goto-line 3)
  13806. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13807. (replace-match ""))
  13808. (when flag
  13809. (end-of-line 1)
  13810. (org-move-to-column (- (window-width) 19) t)
  13811. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13812. (defun org-set-current-tags-overlay (current prefix)
  13813. "Add an overlay to CURRENT tag with PREFIX."
  13814. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13815. (if (featurep 'xemacs)
  13816. (org-overlay-display org-tags-overlay (concat prefix s)
  13817. 'secondary-selection)
  13818. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13819. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13820. (defvar org-last-tag-selection-key nil)
  13821. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13822. "Fast tag selection with single keys.
  13823. CURRENT is the current list of tags in the headline, INHERITED is the
  13824. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13825. possibly with grouping information. TODO-TABLE is a similar table with
  13826. TODO keywords, should these have keys assigned to them.
  13827. If the keys are nil, a-z are automatically assigned.
  13828. Returns the new tags string, or nil to not change the current settings."
  13829. (let* ((fulltable (append table todo-table))
  13830. (maxlen (apply 'max (mapcar
  13831. (lambda (x)
  13832. (if (stringp (car x)) (string-width (car x)) 0))
  13833. fulltable)))
  13834. (buf (current-buffer))
  13835. (expert (eq org-fast-tag-selection-single-key 'expert))
  13836. (buffer-tags nil)
  13837. (fwidth (+ maxlen 3 1 3))
  13838. (ncol (/ (- (window-width) 4) fwidth))
  13839. (i-face 'org-done)
  13840. (c-face 'org-todo)
  13841. tg cnt e c char c1 c2 ntable tbl rtn
  13842. ov-start ov-end ov-prefix
  13843. (exit-after-next org-fast-tag-selection-single-key)
  13844. (done-keywords org-done-keywords)
  13845. groups ingroup intaggroup)
  13846. (save-excursion
  13847. (beginning-of-line 1)
  13848. (if (looking-at
  13849. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13850. (setq ov-start (match-beginning 1)
  13851. ov-end (match-end 1)
  13852. ov-prefix "")
  13853. (setq ov-start (1- (point-at-eol))
  13854. ov-end (1+ ov-start))
  13855. (skip-chars-forward "^\n\r")
  13856. (setq ov-prefix
  13857. (concat
  13858. (buffer-substring (1- (point)) (point))
  13859. (if (> (current-column) org-tags-column)
  13860. " "
  13861. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13862. (move-overlay org-tags-overlay ov-start ov-end)
  13863. (save-window-excursion
  13864. (if expert
  13865. (set-buffer (get-buffer-create " *Org tags*"))
  13866. (delete-other-windows)
  13867. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13868. (org-switch-to-buffer-other-window " *Org tags*"))
  13869. (erase-buffer)
  13870. (org-set-local 'org-done-keywords done-keywords)
  13871. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13872. (org-fast-tag-insert "Current" current c-face "\n\n")
  13873. (org-fast-tag-show-exit exit-after-next)
  13874. (org-set-current-tags-overlay current ov-prefix)
  13875. (setq tbl fulltable char ?a cnt 0)
  13876. (while (setq e (pop tbl))
  13877. (cond
  13878. ((eq (car e) :startgroup)
  13879. (push '() groups) (setq ingroup t)
  13880. (unless (zerop cnt)
  13881. (setq cnt 0)
  13882. (insert "\n"))
  13883. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13884. ((eq (car e) :endgroup)
  13885. (setq ingroup nil cnt 0)
  13886. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13887. ((eq (car e) :startgrouptag)
  13888. (setq intaggroup t)
  13889. (unless (zerop cnt)
  13890. (setq cnt 0)
  13891. (insert "\n"))
  13892. (insert "[ "))
  13893. ((eq (car e) :endgrouptag)
  13894. (setq intaggroup nil cnt 0)
  13895. (insert "]\n"))
  13896. ((equal e '(:newline))
  13897. (unless (zerop cnt)
  13898. (setq cnt 0)
  13899. (insert "\n")
  13900. (setq e (car tbl))
  13901. (while (equal (car tbl) '(:newline))
  13902. (insert "\n")
  13903. (setq tbl (cdr tbl)))))
  13904. ((equal e '(:grouptags)) (insert " : "))
  13905. (t
  13906. (setq tg (copy-sequence (car e)) c2 nil)
  13907. (if (cdr e)
  13908. (setq c (cdr e))
  13909. ;; automatically assign a character.
  13910. (setq c1 (string-to-char
  13911. (downcase (substring
  13912. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13913. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13914. (while (or (rassoc char ntable) (rassoc char table))
  13915. (setq char (1+ char)))
  13916. (setq c2 c1))
  13917. (setq c (or c2 char)))
  13918. (when ingroup (push tg (car groups)))
  13919. (setq tg (org-add-props tg nil 'face
  13920. (cond
  13921. ((not (assoc tg table))
  13922. (org-get-todo-face tg))
  13923. ((member tg current) c-face)
  13924. ((member tg inherited) i-face))))
  13925. (when (equal (caar tbl) :grouptags)
  13926. (org-add-props tg nil 'face 'org-tag-group))
  13927. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13928. (insert "[" c "] " tg (make-string
  13929. (- fwidth 4 (length tg)) ?\ ))
  13930. (push (cons tg c) ntable)
  13931. (when (= (incf cnt) ncol)
  13932. (insert "\n")
  13933. (when (or ingroup intaggroup) (insert " "))
  13934. (setq cnt 0)))))
  13935. (setq ntable (nreverse ntable))
  13936. (insert "\n")
  13937. (goto-char (point-min))
  13938. (unless expert (org-fit-window-to-buffer))
  13939. (setq rtn
  13940. (catch 'exit
  13941. (while t
  13942. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13943. (if (not groups) "no " "")
  13944. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13945. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13946. (setq org-last-tag-selection-key c)
  13947. (cond
  13948. ((= c ?\r) (throw 'exit t))
  13949. ((= c ?!)
  13950. (setq groups (not groups))
  13951. (goto-char (point-min))
  13952. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13953. ((= c ?\C-c)
  13954. (if (not expert)
  13955. (org-fast-tag-show-exit
  13956. (setq exit-after-next (not exit-after-next)))
  13957. (setq expert nil)
  13958. (delete-other-windows)
  13959. (set-window-buffer (split-window-vertically) " *Org tags*")
  13960. (org-switch-to-buffer-other-window " *Org tags*")
  13961. (org-fit-window-to-buffer)))
  13962. ((or (= c ?\C-g)
  13963. (and (= c ?q) (not (rassoc c ntable))))
  13964. (org-detach-overlay org-tags-overlay)
  13965. (setq quit-flag t))
  13966. ((= c ?\ )
  13967. (setq current nil)
  13968. (when exit-after-next (setq exit-after-next 'now)))
  13969. ((= c ?\t)
  13970. (condition-case nil
  13971. (setq tg (org-icompleting-read
  13972. "Tag: "
  13973. (or buffer-tags
  13974. (with-current-buffer buf
  13975. (setq buffer-tags
  13976. (org-get-buffer-tags))))))
  13977. (quit (setq tg "")))
  13978. (when (string-match "\\S-" tg)
  13979. (add-to-list 'buffer-tags (list tg))
  13980. (if (member tg current)
  13981. (setq current (delete tg current))
  13982. (push tg current)))
  13983. (when exit-after-next (setq exit-after-next 'now)))
  13984. ((setq e (rassoc c todo-table) tg (car e))
  13985. (with-current-buffer buf
  13986. (save-excursion (org-todo tg)))
  13987. (when exit-after-next (setq exit-after-next 'now)))
  13988. ((setq e (rassoc c ntable) tg (car e))
  13989. (if (member tg current)
  13990. (setq current (delete tg current))
  13991. (loop for g in groups do
  13992. (when (member tg g)
  13993. (dolist (x g) (setq current (delete x current)))))
  13994. (push tg current))
  13995. (when exit-after-next (setq exit-after-next 'now))))
  13996. ;; Create a sorted list
  13997. (setq current
  13998. (sort current
  13999. (lambda (a b)
  14000. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14001. (when (eq exit-after-next 'now) (throw 'exit t))
  14002. (goto-char (point-min))
  14003. (beginning-of-line 2)
  14004. (delete-region (point) (point-at-eol))
  14005. (org-fast-tag-insert "Current" current c-face)
  14006. (org-set-current-tags-overlay current ov-prefix)
  14007. (while (re-search-forward
  14008. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14009. (setq tg (match-string 1))
  14010. (add-text-properties
  14011. (match-beginning 1) (match-end 1)
  14012. (list 'face
  14013. (cond
  14014. ((member tg current) c-face)
  14015. ((member tg inherited) i-face)
  14016. (t (get-text-property (match-beginning 1) 'face))))))
  14017. (goto-char (point-min)))))
  14018. (org-detach-overlay org-tags-overlay)
  14019. (if rtn
  14020. (mapconcat 'identity current ":")
  14021. nil))))
  14022. (defun org-get-tags-string ()
  14023. "Get the TAGS string in the current headline."
  14024. (unless (org-at-heading-p t)
  14025. (user-error "Not on a heading"))
  14026. (save-excursion
  14027. (beginning-of-line 1)
  14028. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14029. (org-match-string-no-properties 1)
  14030. "")))
  14031. (defun org-get-tags ()
  14032. "Get the list of tags specified in the current headline."
  14033. (org-split-string (org-get-tags-string) ":"))
  14034. (defun org-get-buffer-tags ()
  14035. "Get a table of all tags used in the buffer, for completion."
  14036. (org-with-wide-buffer
  14037. (goto-char (point-min))
  14038. (let ((tag-re (concat org-outline-regexp-bol
  14039. "\\(?:.*?[ \t]\\)?"
  14040. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14041. tags)
  14042. (while (re-search-forward tag-re nil t)
  14043. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14044. (push tag tags)))
  14045. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14046. ;;;; The mapping API
  14047. (defun org-map-entries (func &optional match scope &rest skip)
  14048. "Call FUNC at each headline selected by MATCH in SCOPE.
  14049. FUNC is a function or a lisp form. The function will be called without
  14050. arguments, with the cursor positioned at the beginning of the headline.
  14051. The return values of all calls to the function will be collected and
  14052. returned as a list.
  14053. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14054. does not need to preserve point. After evaluation, the cursor will be
  14055. moved to the end of the line (presumably of the headline of the
  14056. processed entry) and search continues from there. Under some
  14057. circumstances, this may not produce the wanted results. For example,
  14058. if you have removed (e.g. archived) the current (sub)tree it could
  14059. mean that the next entry will be skipped entirely. In such cases, you
  14060. can specify the position from where search should continue by making
  14061. FUNC set the variable `org-map-continue-from' to the desired buffer
  14062. position.
  14063. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14064. Only headlines that are matched by this query will be considered during
  14065. the iteration. When MATCH is nil or t, all headlines will be
  14066. visited by the iteration.
  14067. SCOPE determines the scope of this command. It can be any of:
  14068. nil The current buffer, respecting the restriction if any
  14069. tree The subtree started with the entry at point
  14070. region The entries within the active region, if any
  14071. region-start-level
  14072. The entries within the active region, but only those at
  14073. the same level than the first one.
  14074. file The current buffer, without restriction
  14075. file-with-archives
  14076. The current buffer, and any archives associated with it
  14077. agenda All agenda files
  14078. agenda-with-archives
  14079. All agenda files with any archive files associated with them
  14080. \(file1 file2 ...)
  14081. If this is a list, all files in the list will be scanned
  14082. The remaining args are treated as settings for the skipping facilities of
  14083. the scanner. The following items can be given here:
  14084. archive skip trees with the archive tag
  14085. comment skip trees with the COMMENT keyword
  14086. function or Emacs Lisp form:
  14087. will be used as value for `org-agenda-skip-function', so
  14088. whenever the function returns a position, FUNC will not be
  14089. called for that entry and search will continue from the
  14090. position returned
  14091. If your function needs to retrieve the tags including inherited tags
  14092. at the *current* entry, you can use the value of the variable
  14093. `org-scanner-tags' which will be much faster than getting the value
  14094. with `org-get-tags-at'. If your function gets properties with
  14095. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14096. to t around the call to `org-entry-properties' to get the same speedup.
  14097. Note that if your function moves around to retrieve tags and properties at
  14098. a *different* entry, you cannot use these techniques."
  14099. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14100. (not (org-region-active-p)))
  14101. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14102. (org-agenda-skip-archived-trees (memq 'archive skip))
  14103. (org-agenda-skip-comment-trees (memq 'comment skip))
  14104. (org-agenda-skip-function
  14105. (car (org-delete-all '(comment archive) skip)))
  14106. (org-tags-match-list-sublevels t)
  14107. (start-level (eq scope 'region-start-level))
  14108. matcher file res
  14109. org-todo-keywords-for-agenda
  14110. org-done-keywords-for-agenda
  14111. org-todo-keyword-alist-for-agenda
  14112. org-tag-alist-for-agenda
  14113. todo-only)
  14114. (cond
  14115. ((eq match t) (setq matcher t))
  14116. ((eq match nil) (setq matcher t))
  14117. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14118. (save-excursion
  14119. (save-restriction
  14120. (cond ((eq scope 'tree)
  14121. (org-back-to-heading t)
  14122. (org-narrow-to-subtree)
  14123. (setq scope nil))
  14124. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14125. (org-region-active-p))
  14126. ;; If needed, set start-level to a string like "2"
  14127. (when start-level
  14128. (save-excursion
  14129. (goto-char (region-beginning))
  14130. (unless (org-at-heading-p) (outline-next-heading))
  14131. (setq start-level (org-current-level))))
  14132. (narrow-to-region (region-beginning)
  14133. (save-excursion
  14134. (goto-char (region-end))
  14135. (unless (and (bolp) (org-at-heading-p))
  14136. (outline-next-heading))
  14137. (point)))
  14138. (setq scope nil)))
  14139. (if (not scope)
  14140. (progn
  14141. (org-agenda-prepare-buffers
  14142. (and buffer-file-name (list buffer-file-name)))
  14143. (setq res (org-scan-tags func matcher todo-only start-level)))
  14144. ;; Get the right scope
  14145. (cond
  14146. ((and scope (listp scope) (symbolp (car scope)))
  14147. (setq scope (eval scope)))
  14148. ((eq scope 'agenda)
  14149. (setq scope (org-agenda-files t)))
  14150. ((eq scope 'agenda-with-archives)
  14151. (setq scope (org-agenda-files t))
  14152. (setq scope (org-add-archive-files scope)))
  14153. ((eq scope 'file)
  14154. (setq scope (and buffer-file-name (list buffer-file-name))))
  14155. ((eq scope 'file-with-archives)
  14156. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14157. (org-agenda-prepare-buffers scope)
  14158. (dolist (file scope)
  14159. (with-current-buffer (org-find-base-buffer-visiting file)
  14160. (save-excursion
  14161. (save-restriction
  14162. (widen)
  14163. (goto-char (point-min))
  14164. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14165. res)))
  14166. ;;; Properties API
  14167. (defconst org-special-properties
  14168. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14169. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14170. "The special properties valid in Org mode.
  14171. These are properties that are not defined in the property drawer,
  14172. but in some other way.")
  14173. (defconst org-default-properties
  14174. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14175. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14176. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14177. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14178. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14179. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14180. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14181. "Some properties that are used by Org mode for various purposes.
  14182. Being in this list makes sure that they are offered for completion.")
  14183. (defun org--update-property-plist (key val props)
  14184. "Associate KEY to VAL in alist PROPS.
  14185. Modifications are made by side-effect. Return new alist."
  14186. (let* ((appending (string= (substring key -1) "+"))
  14187. (key (if appending (substring key 0 -1) key))
  14188. (old (assoc-string key props t)))
  14189. (if (not old) (cons (cons key val) props)
  14190. (setcdr old (if appending (concat (cdr old) " " val) val))
  14191. props)))
  14192. (defun org-get-property-block (&optional beg force)
  14193. "Return the (beg . end) range of the body of the property drawer.
  14194. BEG is the beginning of the current subtree, or of the part
  14195. before the first headline. If it is not given, it will be found.
  14196. If the drawer does not exist, create it if FORCE is non-nil, or
  14197. return nil."
  14198. (org-with-wide-buffer
  14199. (when beg (goto-char beg))
  14200. (unless (org-before-first-heading-p)
  14201. (let ((beg (cond (beg)
  14202. ((or (not (featurep 'org-inlinetask))
  14203. (org-inlinetask-in-task-p))
  14204. (org-back-to-heading t))
  14205. (t (org-with-limited-levels (org-back-to-heading t))))))
  14206. (forward-line)
  14207. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14208. (cond ((looking-at org-property-drawer-re)
  14209. (forward-line)
  14210. (cons (point) (progn (goto-char (match-end 0))
  14211. (line-beginning-position))))
  14212. (force
  14213. (goto-char beg)
  14214. (org-insert-property-drawer)
  14215. (let ((pos (save-excursion (search-forward ":END:")
  14216. (line-beginning-position))))
  14217. (cons pos pos))))))))
  14218. (defun org-at-property-p ()
  14219. "Non-nil when point is inside a property drawer.
  14220. See `org-property-re' for match data, if applicable."
  14221. (save-excursion
  14222. (beginning-of-line)
  14223. (and (looking-at org-property-re)
  14224. (let ((property-drawer (save-match-data (org-get-property-block))))
  14225. (and property-drawer
  14226. (>= (point) (car property-drawer))
  14227. (< (point) (cdr property-drawer)))))))
  14228. (defun org-property-action ()
  14229. "Do an action on properties."
  14230. (interactive)
  14231. (unless (org-at-property-p) (user-error "Not at a property"))
  14232. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14233. (let ((c (read-char-exclusive)))
  14234. (case c
  14235. (?s (call-interactively #'org-set-property))
  14236. (?d (call-interactively #'org-delete-property))
  14237. (?D (call-interactively #'org-delete-property-globally))
  14238. (?c (call-interactively #'org-compute-property-at-point))
  14239. (otherwise (user-error "No such property action %c" c)))))
  14240. (defun org-inc-effort ()
  14241. "Increment the value of the effort property in the current entry."
  14242. (interactive)
  14243. (org-set-effort nil t))
  14244. (defvar org-clock-effort) ; Defined in org-clock.el.
  14245. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14246. (defun org-set-effort (&optional value increment)
  14247. "Set the effort property of the current entry.
  14248. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14249. 10th allowed value.
  14250. When INCREMENT is non-nil, set the property to the next allowed value."
  14251. (interactive "P")
  14252. (if (equal value 0) (setq value 10))
  14253. (let* ((completion-ignore-case t)
  14254. (prop org-effort-property)
  14255. (cur (org-entry-get nil prop))
  14256. (allowed (org-property-get-allowed-values nil prop 'table))
  14257. (existing (mapcar 'list (org-property-values prop)))
  14258. (heading (nth 4 (org-heading-components)))
  14259. rpl
  14260. (val (cond
  14261. ((stringp value) value)
  14262. ((and allowed (integerp value))
  14263. (or (car (nth (1- value) allowed))
  14264. (car (org-last allowed))))
  14265. ((and allowed increment)
  14266. (or (caadr (member (list cur) allowed))
  14267. (user-error "Allowed effort values are not set")))
  14268. (allowed
  14269. (message "Select 1-9,0, [RET%s]: %s"
  14270. (if cur (concat "=" cur) "")
  14271. (mapconcat 'car allowed " "))
  14272. (setq rpl (read-char-exclusive))
  14273. (if (equal rpl ?\r)
  14274. cur
  14275. (setq rpl (- rpl ?0))
  14276. (if (equal rpl 0) (setq rpl 10))
  14277. (if (and (> rpl 0) (<= rpl (length allowed)))
  14278. (car (nth (1- rpl) allowed))
  14279. (org-completing-read "Effort: " allowed nil))))
  14280. (t
  14281. (let (org-completion-use-ido org-completion-use-iswitchb)
  14282. (org-completing-read
  14283. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14284. (concat "[" cur "]") "")
  14285. ": ")
  14286. existing nil nil "" nil cur))))))
  14287. (unless (equal (org-entry-get nil prop) val)
  14288. (org-entry-put nil prop val))
  14289. (org-refresh-property
  14290. '((effort . identity)
  14291. (effort-minutes . org-duration-string-to-minutes))
  14292. val)
  14293. (when (string= heading org-clock-current-task)
  14294. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14295. (org-clock-update-mode-line))
  14296. (message "%s is now %s" prop val)))
  14297. (defun org-entry-properties (&optional pom which)
  14298. "Get all properties of the current entry.
  14299. When POM is a buffer position, get all properties from the entry
  14300. there instead.
  14301. This includes the TODO keyword, the tags, time strings for
  14302. deadline, scheduled, and clocking, and any additional properties
  14303. defined in the entry.
  14304. If WHICH is nil or `all', get all properties. If WHICH is
  14305. `special' or `standard', only get that subclass. If WHICH is
  14306. a string, only get that property.
  14307. Return value is an alist. Keys are properties, as upcased
  14308. strings."
  14309. (org-with-point-at pom
  14310. (when (and (derived-mode-p 'org-mode)
  14311. (ignore-errors (org-back-to-heading t)))
  14312. (catch 'exit
  14313. (let* ((beg (point))
  14314. (specific (and (stringp which) (upcase which)))
  14315. (which (cond ((not specific) which)
  14316. ((member specific org-special-properties) 'special)
  14317. (t 'standard)))
  14318. props)
  14319. ;; Get the special properties, like TODO and TAGS.
  14320. (when (memq which '(nil all special))
  14321. (when (or (not specific) (string= specific "CLOCKSUM"))
  14322. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14323. (when clocksum
  14324. (push (cons "CLOCKSUM"
  14325. (org-columns-number-to-string
  14326. (/ (float clocksum) 60.) 'add_times))
  14327. props)))
  14328. (when specific (throw 'exit props)))
  14329. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14330. (let ((clocksumt (get-text-property (point)
  14331. :org-clock-minutes-today)))
  14332. (when clocksumt
  14333. (push (cons "CLOCKSUM_T"
  14334. (org-columns-number-to-string
  14335. (/ (float clocksumt) 60.) 'add_times))
  14336. props)))
  14337. (when specific (throw 'exit props)))
  14338. (when (or (not specific) (string= specific "ITEM"))
  14339. (when (looking-at org-complex-heading-regexp)
  14340. (push (cons "ITEM"
  14341. (concat
  14342. (org-match-string-no-properties 1)
  14343. (let ((title (org-match-string-no-properties 4)))
  14344. (when (org-string-nw-p title)
  14345. (concat " " (org-remove-tabs title))))))
  14346. props))
  14347. (when specific (throw 'exit props)))
  14348. (when (or (not specific) (string= specific "TODO"))
  14349. (let ((case-fold-search nil))
  14350. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14351. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14352. (when specific (throw 'exit props)))
  14353. (when (or (not specific) (string= specific "PRIORITY"))
  14354. (push (cons "PRIORITY"
  14355. (if (looking-at org-priority-regexp)
  14356. (org-match-string-no-properties 2)
  14357. (char-to-string org-default-priority)))
  14358. props)
  14359. (when specific (throw 'exit props)))
  14360. (when (or (not specific) (string= specific "FILE"))
  14361. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14362. props)
  14363. (when specific (throw 'exit props)))
  14364. (when (or (not specific) (string= specific "TAGS"))
  14365. (let ((value (org-string-nw-p (org-get-tags-string))))
  14366. (when value (push (cons "TAGS" value) props)))
  14367. (when specific (throw 'exit props)))
  14368. (when (or (not specific) (string= specific "ALLTAGS"))
  14369. (let ((value (org-get-tags-at)))
  14370. (when value
  14371. (push (cons "ALLTAGS"
  14372. (format ":%s:" (mapconcat #'identity value ":")))
  14373. props)))
  14374. (when specific (throw 'exit props)))
  14375. (when (or (not specific) (string= specific "BLOCKED"))
  14376. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14377. (when specific (throw 'exit props)))
  14378. (when (or (not specific)
  14379. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14380. (forward-line)
  14381. (when (org-looking-at-p org-planning-line-re)
  14382. (end-of-line)
  14383. (let ((bol (line-beginning-position))
  14384. ;; Backward compatibility: time keywords used to
  14385. ;; be configurable (before 8.3). Make sure we
  14386. ;; get the correct keyword.
  14387. (key-assoc `(("CLOSED" . ,org-closed-string)
  14388. ("DEADLINE" . ,org-deadline-string)
  14389. ("SCHEDULED" . ,org-scheduled-string))))
  14390. (dolist (pair (if specific (list (assoc specific key-assoc))
  14391. key-assoc))
  14392. (save-excursion
  14393. (when (search-backward (cdr pair) bol t)
  14394. (goto-char (match-end 0))
  14395. (skip-chars-forward " \t")
  14396. (and (looking-at org-ts-regexp-both)
  14397. (push (cons (car pair)
  14398. (org-match-string-no-properties 0))
  14399. props)))))))
  14400. (when specific (throw 'exit props)))
  14401. (when (or (not specific)
  14402. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14403. (let ((find-ts
  14404. (lambda (end ts)
  14405. (let ((regexp (cond
  14406. ((string= specific "TIMESTAMP")
  14407. org-ts-regexp)
  14408. ((string= specific "TIMESTAMP_IA")
  14409. org-ts-regexp-inactive)
  14410. ((assoc "TIMESTAMP_IA" ts)
  14411. org-ts-regexp)
  14412. ((assoc "TIMESTAMP" ts)
  14413. org-ts-regexp-inactive)
  14414. (t org-ts-regexp-both))))
  14415. (catch 'next
  14416. (while (re-search-forward regexp end t)
  14417. (backward-char)
  14418. (let ((object (org-element-context)))
  14419. ;; Accept to match timestamps in node
  14420. ;; properties, too.
  14421. (when (memq (org-element-type object)
  14422. '(node-property timestamp))
  14423. (let ((type
  14424. (org-element-property :type object)))
  14425. (cond
  14426. ((and (memq type '(active active-range))
  14427. (not (equal specific "TIMESTAMP_IA")))
  14428. (unless (assoc "TIMESTAMP" ts)
  14429. (push (cons "TIMESTAMP"
  14430. (org-element-property
  14431. :raw-value object))
  14432. ts)
  14433. (when specific (throw 'exit ts))))
  14434. ((and (memq type '(inactive inactive-range))
  14435. (not (string= specific "TIMESTAMP")))
  14436. (unless (assoc "TIMESTAMP_IA" ts)
  14437. (push (cons "TIMESTAMP_IA"
  14438. (org-element-property
  14439. :raw-value object))
  14440. ts)
  14441. (when specific (throw 'exit ts))))))
  14442. ;; Both timestamp types are found,
  14443. ;; move to next part.
  14444. (when (= (length ts) 2) (throw 'next ts)))))
  14445. ts)))))
  14446. (goto-char beg)
  14447. ;; First look for timestamps within headline.
  14448. (let ((ts (funcall find-ts (line-end-position) nil)))
  14449. (if (= (length ts) 2) (setq props (nconc ts props))
  14450. (forward-line)
  14451. ;; Then find timestamps in the section, skipping
  14452. ;; planning line.
  14453. (when (org-looking-at-p org-planning-line-re)
  14454. (forward-line))
  14455. (let ((end (save-excursion (outline-next-heading))))
  14456. (setq props (nconc (funcall find-ts end ts) props))))))))
  14457. ;; Get the standard properties, like :PROP:.
  14458. (when (memq which '(nil all standard))
  14459. ;; If we are looking after a specific property, delegate
  14460. ;; to `org-entry-get', which is faster. However, make an
  14461. ;; exception for "CATEGORY", since it can be also set
  14462. ;; through keywords (i.e. #+CATEGORY).
  14463. (if (and specific (not (equal specific "CATEGORY")))
  14464. (let ((value (org-entry-get beg specific nil t)))
  14465. (throw 'exit (and value (list (cons specific value)))))
  14466. (let ((range (org-get-property-block beg)))
  14467. (when range
  14468. (let ((end (cdr range)) seen-base)
  14469. (goto-char (car range))
  14470. ;; Unlike to `org--update-property-plist', we
  14471. ;; handle the case where base values is found
  14472. ;; after its extension. We also forbid standard
  14473. ;; properties to be named as special properties.
  14474. (while (re-search-forward org-property-re end t)
  14475. (let* ((key (upcase (org-match-string-no-properties 2)))
  14476. (extendp (org-string-match-p "\\+\\'" key))
  14477. (key-base (if extendp (substring key 0 -1) key))
  14478. (value (org-match-string-no-properties 3)))
  14479. (cond
  14480. ((member-ignore-case key-base org-special-properties))
  14481. (extendp
  14482. (setq props
  14483. (org--update-property-plist key value props)))
  14484. ((member key seen-base))
  14485. (t (push key seen-base)
  14486. (let ((p (assoc-string key props t)))
  14487. (if p (setcdr p (concat value " " (cdr p)))
  14488. (push (cons key value) props))))))))))))
  14489. (unless (assoc "CATEGORY" props)
  14490. (push (cons "CATEGORY" (org-get-category beg)) props)
  14491. (when (string= specific "CATEGORY") (throw 'exit props)))
  14492. ;; Return value.
  14493. props)))))
  14494. (defun org-property--local-values (property literal-nil)
  14495. "Return value for PROPERTY in current entry.
  14496. Value is a list whose care is the base value for PROPERTY and cdr
  14497. a list of accumulated values. Return nil if neither is found in
  14498. the entry. Also return nil when PROPERTY is set to \"nil\",
  14499. unless LITERAL-NIL is non-nil."
  14500. (let ((range (org-get-property-block)))
  14501. (when range
  14502. (goto-char (car range))
  14503. (let* ((case-fold-search t)
  14504. (end (cdr range))
  14505. (value
  14506. ;; Base value.
  14507. (save-excursion
  14508. (let ((v (and (re-search-forward
  14509. (org-re-property property nil t) end t)
  14510. (org-match-string-no-properties 3))))
  14511. (list (if literal-nil v (org-not-nil v)))))))
  14512. ;; Find additional values.
  14513. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14514. (while (re-search-forward property+ end t)
  14515. (push (org-match-string-no-properties 3) value)))
  14516. ;; Return final values.
  14517. (and (not (equal value '(nil))) (nreverse value))))))
  14518. (defun org-entry-get (pom property &optional inherit literal-nil)
  14519. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14520. If INHERIT is non-nil and the entry does not have the property,
  14521. then also check higher levels of the hierarchy. If INHERIT is
  14522. the symbol `selective', use inheritance only if the setting in
  14523. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14524. If the property is present but empty, the return value is the
  14525. empty string. If the property is not present at all, nil is
  14526. returned. In any other case, return the value as a string.
  14527. Search is case-insensitive.
  14528. If LITERAL-NIL is set, return the string value \"nil\" as
  14529. a string, do not interpret it as the list atom nil. This is used
  14530. for inheritance when a \"nil\" value can supersede a non-nil
  14531. value higher up the hierarchy."
  14532. (org-with-point-at pom
  14533. (cond
  14534. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14535. ;; We need a special property. Use `org-entry-properties' to
  14536. ;; retrieve it, but specify the wanted property.
  14537. (cdr (assoc-string property (org-entry-properties nil property))))
  14538. ((and inherit
  14539. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14540. (org-entry-get-with-inheritance property literal-nil))
  14541. (t
  14542. (let* ((local (org-property--local-values property literal-nil))
  14543. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14544. (if literal-nil value (org-not-nil value)))))))
  14545. (defun org-property-or-variable-value (var &optional inherit)
  14546. "Check if there is a property fixing the value of VAR.
  14547. If yes, return this value. If not, return the current value of the variable."
  14548. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14549. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14550. (read prop)
  14551. (symbol-value var))))
  14552. (defun org-entry-delete (pom property)
  14553. "Delete PROPERTY from entry at point-or-marker POM.
  14554. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14555. non-nil when a property was removed."
  14556. (unless (member property org-special-properties)
  14557. (org-with-point-at pom
  14558. (let ((range (org-get-property-block)))
  14559. (when range
  14560. (let* ((begin (car range))
  14561. (origin (cdr range))
  14562. (end (copy-marker origin))
  14563. (re (org-re-property
  14564. (concat (regexp-quote property) "\\+?") t t)))
  14565. (goto-char begin)
  14566. (while (re-search-forward re end t)
  14567. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14568. ;; If drawer is empty, remove it altogether.
  14569. (when (= begin end)
  14570. (delete-region (line-beginning-position 0)
  14571. (line-beginning-position 2)))
  14572. ;; Return non-nil if some property was removed.
  14573. (prog1 (/= end origin) (set-marker end nil))))))))
  14574. ;; Multi-values properties are properties that contain multiple values
  14575. ;; These values are assumed to be single words, separated by whitespace.
  14576. (defun org-entry-add-to-multivalued-property (pom property value)
  14577. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14578. (let* ((old (org-entry-get pom property))
  14579. (values (and old (org-split-string old "[ \t]"))))
  14580. (setq value (org-entry-protect-space value))
  14581. (unless (member value values)
  14582. (setq values (append values (list value)))
  14583. (org-entry-put pom property
  14584. (mapconcat 'identity values " ")))))
  14585. (defun org-entry-remove-from-multivalued-property (pom property value)
  14586. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14587. (let* ((old (org-entry-get pom property))
  14588. (values (and old (org-split-string old "[ \t]"))))
  14589. (setq value (org-entry-protect-space value))
  14590. (when (member value values)
  14591. (setq values (delete value values))
  14592. (org-entry-put pom property
  14593. (mapconcat 'identity values " ")))))
  14594. (defun org-entry-member-in-multivalued-property (pom property value)
  14595. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14596. (let* ((old (org-entry-get pom property))
  14597. (values (and old (org-split-string old "[ \t]"))))
  14598. (setq value (org-entry-protect-space value))
  14599. (member value values)))
  14600. (defun org-entry-get-multivalued-property (pom property)
  14601. "Return a list of values in a multivalued property."
  14602. (let* ((value (org-entry-get pom property))
  14603. (values (and value (org-split-string value "[ \t]"))))
  14604. (mapcar 'org-entry-restore-space values)))
  14605. (defun org-entry-put-multivalued-property (pom property &rest values)
  14606. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14607. VALUES should be a list of strings. Spaces will be protected."
  14608. (org-entry-put pom property
  14609. (mapconcat 'org-entry-protect-space values " "))
  14610. (let* ((value (org-entry-get pom property))
  14611. (values (and value (org-split-string value "[ \t]"))))
  14612. (mapcar 'org-entry-restore-space values)))
  14613. (defun org-entry-protect-space (s)
  14614. "Protect spaces and newline in string S."
  14615. (while (string-match " " s)
  14616. (setq s (replace-match "%20" t t s)))
  14617. (while (string-match "\n" s)
  14618. (setq s (replace-match "%0A" t t s)))
  14619. s)
  14620. (defun org-entry-restore-space (s)
  14621. "Restore spaces and newline in string S."
  14622. (while (string-match "%20" s)
  14623. (setq s (replace-match " " t t s)))
  14624. (while (string-match "%0A" s)
  14625. (setq s (replace-match "\n" t t s)))
  14626. s)
  14627. (defvar org-entry-property-inherited-from (make-marker)
  14628. "Marker pointing to the entry from where a property was inherited.
  14629. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14630. location of the entry where the inheritance search matched. If there was
  14631. no match, the marker will point nowhere.
  14632. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14633. is set.")
  14634. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14635. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14636. The search will stop at the first ancestor which has the property defined.
  14637. If the value found is \"nil\", return nil to show that the property
  14638. should be considered as undefined (this is the meaning of nil here).
  14639. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14640. (move-marker org-entry-property-inherited-from nil)
  14641. (org-with-wide-buffer
  14642. (let (value)
  14643. (catch 'exit
  14644. (while t
  14645. (let ((v (org-property--local-values property literal-nil)))
  14646. (when v
  14647. (setq value
  14648. (concat (mapconcat #'identity (delq nil v) " ")
  14649. (and value " ")
  14650. value)))
  14651. (cond
  14652. ((car v)
  14653. (org-back-to-heading t)
  14654. (move-marker org-entry-property-inherited-from (point))
  14655. (throw 'exit nil))
  14656. ((org-up-heading-safe))
  14657. (t
  14658. (let ((global
  14659. (cdr (or (assoc-string property org-file-properties t)
  14660. (assoc-string property org-global-properties t)
  14661. (assoc-string property org-global-properties-fixed t)))))
  14662. (cond ((not global))
  14663. (value (setq value (concat global " " value)))
  14664. (t (setq value global))))
  14665. (throw 'exit nil))))))
  14666. (if literal-nil value (org-not-nil value)))))
  14667. (defvar org-property-changed-functions nil
  14668. "Hook called when the value of a property has changed.
  14669. Each hook function should accept two arguments, the name of the property
  14670. and the new value.")
  14671. (defun org-entry-put (pom property value)
  14672. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14673. If the value is nil, it is converted to the empty string. If
  14674. it is not a string, an error is raised.
  14675. PROPERTY can be any regular property (see
  14676. `org-special-properties'). It can also be \"TODO\",
  14677. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14678. For the last two properties, VALUE may have any of the special
  14679. values \"earlier\" and \"later\". The function then increases or
  14680. decreases scheduled or deadline date by one day."
  14681. (cond ((null value) (setq value ""))
  14682. ((not (stringp value)) (error "Properties values should be strings")))
  14683. (org-with-point-at pom
  14684. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14685. (org-back-to-heading t)
  14686. (org-with-limited-levels (org-back-to-heading t)))
  14687. (let ((beg (point)))
  14688. (cond
  14689. ((equal property "TODO")
  14690. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14691. ((not (member value org-todo-keywords-1))
  14692. (user-error "\"%s\" is not a valid TODO state" value)))
  14693. (org-todo value)
  14694. (org-set-tags nil 'align))
  14695. ((equal property "PRIORITY")
  14696. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14697. (org-set-tags nil 'align))
  14698. ((equal property "SCHEDULED")
  14699. (forward-line)
  14700. (if (and (org-looking-at-p org-planning-line-re)
  14701. (re-search-forward
  14702. org-scheduled-time-regexp (line-end-position) t))
  14703. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14704. ((string= value "later") (org-timestamp-change 1 'day))
  14705. ((string= value "") (org-schedule '(4)))
  14706. (t (org-schedule nil value)))
  14707. (if (member value '("earlier" "later" ""))
  14708. (call-interactively #'org-schedule)
  14709. (org-schedule nil value))))
  14710. ((equal property "DEADLINE")
  14711. (forward-line)
  14712. (if (and (org-looking-at-p org-planning-line-re)
  14713. (re-search-forward
  14714. org-deadline-time-regexp (line-end-position) t))
  14715. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14716. ((string= value "later") (org-timestamp-change 1 'day))
  14717. ((string= value "") (org-deadline '(4)))
  14718. (t (org-deadline nil value)))
  14719. (if (member value '("earlier" "later" ""))
  14720. (call-interactively #'org-deadline)
  14721. (org-deadline nil value))))
  14722. ((member property org-special-properties)
  14723. (error "The %s property cannot be set with `org-entry-put'" property))
  14724. (t
  14725. (let* ((range (org-get-property-block beg 'force))
  14726. (end (cdr range))
  14727. (case-fold-search t))
  14728. (goto-char (car range))
  14729. (if (re-search-forward (org-re-property property nil t) end t)
  14730. (progn (delete-region (match-beginning 0) (match-end 0))
  14731. (goto-char (match-beginning 0)))
  14732. (goto-char end)
  14733. (insert "\n")
  14734. (backward-char))
  14735. (insert ":" property ":")
  14736. (when value (insert " " value))
  14737. (org-indent-line)))))
  14738. (run-hook-with-args 'org-property-changed-functions property value)))
  14739. (defun org-buffer-property-keys (&optional specials defaults columns)
  14740. "Get all property keys in the current buffer.
  14741. When SPECIALS is non-nil, also list the special properties that
  14742. reflect things like tags and TODO state.
  14743. When DEFAULTS is non-nil, also include properties that has
  14744. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14745. DESCRIPTION, LOCATION, and LOGGING and others.
  14746. When COLUMNS in non-nil, also include property names given in
  14747. COLUMN formats in the current buffer."
  14748. (let ((case-fold-search t)
  14749. (props (append
  14750. (and specials org-special-properties)
  14751. (and defaults (cons org-effort-property org-default-properties))
  14752. nil)))
  14753. (org-with-wide-buffer
  14754. (goto-char (point-min))
  14755. (while (re-search-forward org-property-start-re nil t)
  14756. (let ((range (org-get-property-block)))
  14757. (catch 'skip
  14758. (unless range
  14759. (when (and (not (org-before-first-heading-p))
  14760. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14761. (line-beginning-position))))
  14762. (org-get-property-block nil t))
  14763. (throw 'skip nil))
  14764. (goto-char (car range))
  14765. (let ((begin (car range))
  14766. (end (cdr range)))
  14767. ;; Make sure that found property block is not located
  14768. ;; before current point, as it would generate an infloop.
  14769. ;; It can happen, for example, in the following
  14770. ;; situation:
  14771. ;;
  14772. ;; * Headline
  14773. ;; :PROPERTIES:
  14774. ;; ...
  14775. ;; :END:
  14776. ;; *************** Inlinetask
  14777. ;; #+BEGIN_EXAMPLE
  14778. ;; :PROPERTIES:
  14779. ;; #+END_EXAMPLE
  14780. ;;
  14781. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14782. (while (< (point) end)
  14783. (let ((p (progn (looking-at org-property-re)
  14784. (org-match-string-no-properties 2))))
  14785. ;; Only add true property name, not extension symbol.
  14786. (add-to-list 'props
  14787. (if (not (org-string-match-p "\\+\\'" p)) p
  14788. (substring p 0 -1))))
  14789. (forward-line))))
  14790. (outline-next-heading)))
  14791. (when columns
  14792. (goto-char (point-min))
  14793. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14794. (let ((element (org-element-at-point)))
  14795. (when (memq (org-element-type element) '(keyword node-property))
  14796. (let ((value (org-element-property :value element))
  14797. (start 0))
  14798. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14799. (setq start (match-end 0))
  14800. (let ((p (org-match-string-no-properties 1 value)))
  14801. (unless (member-ignore-case p org-special-properties)
  14802. (add-to-list 'props p))))))))))
  14803. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14804. (defun org-property-values (key)
  14805. "List all non-nil values of property KEY in current buffer."
  14806. (org-with-wide-buffer
  14807. (goto-char (point-min))
  14808. (let ((case-fold-search t)
  14809. (re (org-re-property key))
  14810. values)
  14811. (while (re-search-forward re nil t)
  14812. (add-to-list 'values (org-entry-get (point) key)))
  14813. values)))
  14814. (defun org-insert-property-drawer ()
  14815. "Insert a property drawer into the current entry."
  14816. (org-with-wide-buffer
  14817. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14818. (org-back-to-heading t)
  14819. (org-with-limited-levels (org-back-to-heading t)))
  14820. (forward-line)
  14821. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14822. (unless (org-looking-at-p org-property-drawer-re)
  14823. (let ((inhibit-read-only t))
  14824. (unless (bolp) (insert "\n"))
  14825. (let ((begin (point)))
  14826. (insert ":PROPERTIES:\n:END:\n")
  14827. (org-indent-region begin (point)))))))
  14828. (defun org-insert-drawer (&optional arg drawer)
  14829. "Insert a drawer at point.
  14830. When optional argument ARG is non-nil, insert a property drawer.
  14831. Optional argument DRAWER, when non-nil, is a string representing
  14832. drawer's name. Otherwise, the user is prompted for a name.
  14833. If a region is active, insert the drawer around that region
  14834. instead.
  14835. Point is left between drawer's boundaries."
  14836. (interactive "P")
  14837. (let* ((drawer (if arg "PROPERTIES"
  14838. (or drawer (read-from-minibuffer "Drawer: ")))))
  14839. (cond
  14840. ;; With C-u, fall back on `org-insert-property-drawer'
  14841. (arg (org-insert-property-drawer))
  14842. ;; Check validity of suggested drawer's name.
  14843. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14844. (user-error "Invalid drawer name"))
  14845. ;; With an active region, insert a drawer at point.
  14846. ((not (org-region-active-p))
  14847. (progn
  14848. (unless (bolp) (insert "\n"))
  14849. (insert (format ":%s:\n\n:END:\n" drawer))
  14850. (forward-line -2)))
  14851. ;; Otherwise, insert the drawer at point
  14852. (t
  14853. (let ((rbeg (region-beginning))
  14854. (rend (copy-marker (region-end))))
  14855. (unwind-protect
  14856. (progn
  14857. (goto-char rbeg)
  14858. (beginning-of-line)
  14859. (when (save-excursion
  14860. (re-search-forward org-outline-regexp-bol rend t))
  14861. (user-error "Drawers cannot contain headlines"))
  14862. ;; Position point at the beginning of the first
  14863. ;; non-blank line in region. Insert drawer's opening
  14864. ;; there, then indent it.
  14865. (org-skip-whitespace)
  14866. (beginning-of-line)
  14867. (insert ":" drawer ":\n")
  14868. (forward-line -1)
  14869. (indent-for-tab-command)
  14870. ;; Move point to the beginning of the first blank line
  14871. ;; after the last non-blank line in region. Insert
  14872. ;; drawer's closing, then indent it.
  14873. (goto-char rend)
  14874. (skip-chars-backward " \r\t\n")
  14875. (insert "\n:END:")
  14876. (deactivate-mark t)
  14877. (indent-for-tab-command)
  14878. (unless (eolp) (insert "\n")))
  14879. ;; Clear marker, whatever the outcome of insertion is.
  14880. (set-marker rend nil)))))))
  14881. (defvar org-property-set-functions-alist nil
  14882. "Property set function alist.
  14883. Each entry should have the following format:
  14884. (PROPERTY . READ-FUNCTION)
  14885. The read function will be called with the same argument as
  14886. `org-completing-read'.")
  14887. (defun org-set-property-function (property)
  14888. "Get the function that should be used to set PROPERTY.
  14889. This is computed according to `org-property-set-functions-alist'."
  14890. (or (cdr (assoc property org-property-set-functions-alist))
  14891. 'org-completing-read))
  14892. (defun org-read-property-value (property)
  14893. "Read PROPERTY value from user."
  14894. (let* ((completion-ignore-case t)
  14895. (allowed (org-property-get-allowed-values nil property 'table))
  14896. (cur (org-entry-get nil property))
  14897. (prompt (concat property " value"
  14898. (if (and cur (string-match "\\S-" cur))
  14899. (concat " [" cur "]") "") ": "))
  14900. (set-function (org-set-property-function property))
  14901. (val (if allowed
  14902. (funcall set-function prompt allowed nil
  14903. (not (get-text-property 0 'org-unrestricted
  14904. (caar allowed))))
  14905. (let (org-completion-use-ido org-completion-use-iswitchb)
  14906. (funcall set-function prompt
  14907. (mapcar 'list (org-property-values property))
  14908. nil nil "" nil cur)))))
  14909. (org-trim val)))
  14910. (defvar org-last-set-property nil)
  14911. (defvar org-last-set-property-value nil)
  14912. (defun org-read-property-name ()
  14913. "Read a property name."
  14914. (let ((completion-ignore-case t)
  14915. (default-prop (or (and (org-at-property-p)
  14916. (org-match-string-no-properties 2))
  14917. org-last-set-property)))
  14918. (org-completing-read
  14919. (concat "Property"
  14920. (if default-prop (concat " [" default-prop "]") "")
  14921. ": ")
  14922. (mapcar #'list (org-buffer-property-keys nil t t))
  14923. nil nil nil nil default-prop)))
  14924. (defun org-set-property-and-value (use-last)
  14925. "Allow to set [PROPERTY]: [value] direction from prompt.
  14926. When use-default, don't even ask, just use the last
  14927. \"[PROPERTY]: [value]\" string from the history."
  14928. (interactive "P")
  14929. (let* ((completion-ignore-case t)
  14930. (pv (or (and use-last org-last-set-property-value)
  14931. (org-completing-read
  14932. "Enter a \"[Property]: [value]\" pair: "
  14933. nil nil nil nil nil
  14934. org-last-set-property-value)))
  14935. prop val)
  14936. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14937. (setq prop (match-string 1 pv)
  14938. val (match-string 2 pv))
  14939. (org-set-property prop val))))
  14940. (defun org-set-property (property value)
  14941. "In the current entry, set PROPERTY to VALUE.
  14942. When called interactively, this will prompt for a property name, offering
  14943. completion on existing and default properties. And then it will prompt
  14944. for a value, offering completion either on allowed values (via an inherited
  14945. xxx_ALL property) or on existing values in other instances of this property
  14946. in the current file."
  14947. (interactive (list nil nil))
  14948. (let* ((property (or property (org-read-property-name)))
  14949. (value (or value (org-read-property-value property)))
  14950. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14951. (setq org-last-set-property property)
  14952. (setq org-last-set-property-value (concat property ": " value))
  14953. ;; Possibly postprocess the inserted value:
  14954. (when fn (setq value (funcall fn value)))
  14955. (unless (equal (org-entry-get nil property) value)
  14956. (org-entry-put nil property value))))
  14957. (defun org-find-property (property &optional value)
  14958. "Find first entry in buffer that sets PROPERTY.
  14959. When optional argument VALUE is non-nil, only consider an entry
  14960. if it contains PROPERTY set to this value. If PROPERTY should be
  14961. explicitly set to nil, use string \"nil\" for VALUE.
  14962. Return position where the entry begins, or nil if there is no
  14963. such entry. If narrowing is in effect, only search the visible
  14964. part of the buffer."
  14965. (save-excursion
  14966. (goto-char (point-min))
  14967. (let ((case-fold-search t)
  14968. (re (org-re-property property nil (not value) value)))
  14969. (catch 'exit
  14970. (while (re-search-forward re nil t)
  14971. (when (if value (org-at-property-p)
  14972. (org-entry-get (point) property nil t))
  14973. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14974. (defun org-delete-property (property)
  14975. "In the current entry, delete PROPERTY."
  14976. (interactive
  14977. (let* ((completion-ignore-case t)
  14978. (cat (org-entry-get (point) "CATEGORY"))
  14979. (props0 (org-entry-properties nil 'standard))
  14980. (props (if cat props0
  14981. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14982. (prop (if (< 1 (length props))
  14983. (org-icompleting-read "Property: " props nil t)
  14984. (caar props))))
  14985. (list prop)))
  14986. (if (not property)
  14987. (message "No property to delete in this entry")
  14988. (org-entry-delete nil property)
  14989. (message "Property \"%s\" deleted" property)))
  14990. (defun org-delete-property-globally (property)
  14991. "Remove PROPERTY globally, from all entries.
  14992. This function ignores narrowing, if any."
  14993. (interactive
  14994. (let* ((completion-ignore-case t)
  14995. (prop (org-icompleting-read
  14996. "Globally remove property: "
  14997. (mapcar #'list (org-buffer-property-keys)))))
  14998. (list prop)))
  14999. (org-with-wide-buffer
  15000. (goto-char (point-min))
  15001. (let ((count 0)
  15002. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15003. (while (re-search-forward re nil t)
  15004. (when (org-entry-delete (point) property) (incf count)))
  15005. (message "Property \"%s\" removed from %d entries" property count))))
  15006. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15007. (defun org-compute-property-at-point ()
  15008. "Compute the property at point.
  15009. This looks for an enclosing column format, extracts the operator and
  15010. then applies it to the property in the column format's scope."
  15011. (interactive)
  15012. (unless (org-at-property-p)
  15013. (user-error "Not at a property"))
  15014. (let ((prop (org-match-string-no-properties 2)))
  15015. (org-columns-get-format-and-top-level)
  15016. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15017. (user-error "No operator defined for property %s" prop))
  15018. (org-columns-compute prop)))
  15019. (defvar org-property-allowed-value-functions nil
  15020. "Hook for functions supplying allowed values for a specific property.
  15021. The functions must take a single argument, the name of the property, and
  15022. return a flat list of allowed values. If \":ETC\" is one of
  15023. the values, this means that these values are intended as defaults for
  15024. completion, but that other values should be allowed too.
  15025. The functions must return nil if they are not responsible for this
  15026. property.")
  15027. (defun org-property-get-allowed-values (pom property &optional table)
  15028. "Get allowed values for the property PROPERTY.
  15029. When TABLE is non-nil, return an alist that can directly be used for
  15030. completion."
  15031. (let (vals)
  15032. (cond
  15033. ((equal property "TODO")
  15034. (setq vals (org-with-point-at pom
  15035. (append org-todo-keywords-1 '("")))))
  15036. ((equal property "PRIORITY")
  15037. (let ((n org-lowest-priority))
  15038. (while (>= n org-highest-priority)
  15039. (push (char-to-string n) vals)
  15040. (setq n (1- n)))))
  15041. ((equal property "CATEGORY"))
  15042. ((member property org-special-properties))
  15043. ((setq vals (run-hook-with-args-until-success
  15044. 'org-property-allowed-value-functions property)))
  15045. (t
  15046. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15047. (when (and vals (string-match "\\S-" vals))
  15048. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15049. (setq vals (mapcar (lambda (x)
  15050. (cond ((stringp x) x)
  15051. ((numberp x) (number-to-string x))
  15052. ((symbolp x) (symbol-name x))
  15053. (t "???")))
  15054. vals)))))
  15055. (when (member ":ETC" vals)
  15056. (setq vals (remove ":ETC" vals))
  15057. (org-add-props (car vals) '(org-unrestricted t)))
  15058. (if table (mapcar 'list vals) vals)))
  15059. (defun org-property-previous-allowed-value (&optional previous)
  15060. "Switch to the next allowed value for this property."
  15061. (interactive)
  15062. (org-property-next-allowed-value t))
  15063. (defun org-property-next-allowed-value (&optional previous)
  15064. "Switch to the next allowed value for this property."
  15065. (interactive)
  15066. (unless (org-at-property-p)
  15067. (user-error "Not at a property"))
  15068. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15069. (key (match-string 2))
  15070. (value (match-string 3))
  15071. (allowed (or (org-property-get-allowed-values (point) key)
  15072. (and (member value '("[ ]" "[-]" "[X]"))
  15073. '("[ ]" "[X]"))))
  15074. (heading (save-match-data (nth 4 (org-heading-components))))
  15075. nval)
  15076. (unless allowed
  15077. (user-error "Allowed values for this property have not been defined"))
  15078. (if previous (setq allowed (reverse allowed)))
  15079. (if (member value allowed)
  15080. (setq nval (car (cdr (member value allowed)))))
  15081. (setq nval (or nval (car allowed)))
  15082. (if (equal nval value)
  15083. (user-error "Only one allowed value for this property"))
  15084. (org-at-property-p)
  15085. (replace-match (concat " :" key ": " nval) t t)
  15086. (org-indent-line)
  15087. (beginning-of-line 1)
  15088. (skip-chars-forward " \t")
  15089. (when (equal prop org-effort-property)
  15090. (org-refresh-property
  15091. '((effort . identity)
  15092. (effort-minutes . org-duration-string-to-minutes))
  15093. nval)
  15094. (when (string= org-clock-current-task heading)
  15095. (setq org-clock-effort nval)
  15096. (org-clock-update-mode-line)))
  15097. (run-hook-with-args 'org-property-changed-functions key nval)))
  15098. (defun org-find-olp (path &optional this-buffer)
  15099. "Return a marker pointing to the entry at outline path OLP.
  15100. If anything goes wrong, throw an error.
  15101. You can wrap this call to catch the error like this:
  15102. (condition-case msg
  15103. (org-mobile-locate-entry (match-string 4))
  15104. (error (nth 1 msg)))
  15105. The return value will then be either a string with the error message,
  15106. or a marker if everything is OK.
  15107. If THIS-BUFFER is set, the outline path does not contain a file,
  15108. only headings."
  15109. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15110. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15111. (level 1)
  15112. (lmin 1)
  15113. (lmax 1)
  15114. limit re end found pos heading cnt flevel)
  15115. (unless buffer (error "File not found :%s" file))
  15116. (with-current-buffer buffer
  15117. (save-excursion
  15118. (save-restriction
  15119. (widen)
  15120. (setq limit (point-max))
  15121. (goto-char (point-min))
  15122. (dolist (heading path)
  15123. (setq re (format org-complex-heading-regexp-format
  15124. (regexp-quote heading)))
  15125. (setq cnt 0 pos (point))
  15126. (while (re-search-forward re end t)
  15127. (setq level (- (match-end 1) (match-beginning 1)))
  15128. (if (and (>= level lmin) (<= level lmax))
  15129. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15130. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15131. lmax heading))
  15132. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15133. lmax heading))
  15134. (goto-char found)
  15135. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15136. (setq end (save-excursion (org-end-of-subtree t t))))
  15137. (when (org-at-heading-p)
  15138. (point-marker)))))))
  15139. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15140. "Find node HEADING in BUFFER.
  15141. Return a marker to the heading if it was found, or nil if not.
  15142. If POS-ONLY is set, return just the position instead of a marker.
  15143. The heading text must match exact, but it may have a TODO keyword,
  15144. a priority cookie and tags in the standard locations."
  15145. (with-current-buffer (or buffer (current-buffer))
  15146. (save-excursion
  15147. (save-restriction
  15148. (widen)
  15149. (goto-char (point-min))
  15150. (let (case-fold-search)
  15151. (if (re-search-forward
  15152. (format org-complex-heading-regexp-format
  15153. (regexp-quote heading)) nil t)
  15154. (if pos-only
  15155. (match-beginning 0)
  15156. (move-marker (make-marker) (match-beginning 0)))))))))
  15157. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15158. "Find Org node headline HEADING in all .org files in directory DIR.
  15159. When the target headline is found, return a marker to this location."
  15160. (let ((files (directory-files (or dir default-directory)
  15161. t "\\`[^.#].*\\.org\\'"))
  15162. visiting m buffer)
  15163. (catch 'found
  15164. (dolist (file files)
  15165. (message "trying %s" file)
  15166. (setq visiting (org-find-base-buffer-visiting file))
  15167. (setq buffer (or visiting (find-file-noselect file)))
  15168. (setq m (org-find-exact-headline-in-buffer
  15169. heading buffer))
  15170. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15171. (and m (throw 'found m))))))
  15172. (defun org-find-entry-with-id (ident)
  15173. "Locate the entry that contains the ID property with exact value IDENT.
  15174. IDENT can be a string, a symbol or a number, this function will search for
  15175. the string representation of it.
  15176. Return the position where this entry starts, or nil if there is no such entry."
  15177. (interactive "sID: ")
  15178. (let ((id (cond
  15179. ((stringp ident) ident)
  15180. ((symbolp ident) (symbol-name ident))
  15181. ((numberp ident) (number-to-string ident))
  15182. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15183. (org-with-wide-buffer (org-find-property "ID" id))))
  15184. ;;;; Timestamps
  15185. (defvar org-last-changed-timestamp nil)
  15186. (defvar org-last-inserted-timestamp nil
  15187. "The last time stamp inserted with `org-insert-time-stamp'.")
  15188. (defvar org-ts-what) ; dynamically scoped parameter
  15189. (defun org-time-stamp (arg &optional inactive)
  15190. "Prompt for a date/time and insert a time stamp.
  15191. If the user specifies a time like HH:MM or if this command is
  15192. called with at least one prefix argument, the time stamp contains
  15193. the date and the time. Otherwise, only the date is included.
  15194. All parts of a date not specified by the user are filled in from
  15195. the timestamp at point, if any, or the current date/time
  15196. otherwise.
  15197. If there is already a timestamp at the cursor, it is replaced.
  15198. With two universal prefix arguments, insert an active timestamp
  15199. with the current time without prompting the user.
  15200. When called from lisp, the timestamp is inactive if INACTIVE is
  15201. non-nil."
  15202. (interactive "P")
  15203. (let* ((ts (cond
  15204. ((org-at-date-range-p t)
  15205. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15206. ((org-at-timestamp-p t) (match-string 0))))
  15207. ;; Default time is either the timestamp at point or today.
  15208. ;; When entering a range, only the range start is considered.
  15209. (default-time (if (not ts) (current-time)
  15210. (apply #'encode-time (org-parse-time-string ts))))
  15211. (default-input (and ts (org-get-compact-tod ts)))
  15212. (repeater (and ts
  15213. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15214. (match-string 0 ts)))
  15215. org-time-was-given
  15216. org-end-time-was-given
  15217. (time
  15218. (and (if (equal arg '(16)) (current-time)
  15219. ;; Preserve `this-command' and `last-command'.
  15220. (let ((this-command this-command)
  15221. (last-command last-command))
  15222. (org-read-date
  15223. arg 'totime nil nil default-time default-input
  15224. inactive))))))
  15225. (cond
  15226. ((and ts
  15227. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15228. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15229. (insert "--")
  15230. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15231. (ts
  15232. ;; Make sure we're on a timestamp. When in the middle of a date
  15233. ;; range, move arbitrarily to range end.
  15234. (unless (org-at-timestamp-p t)
  15235. (skip-chars-forward "-")
  15236. (org-at-timestamp-p t))
  15237. (replace-match "")
  15238. (setq org-last-changed-timestamp
  15239. (org-insert-time-stamp
  15240. time (or org-time-was-given arg)
  15241. inactive nil nil (list org-end-time-was-given)))
  15242. (when repeater
  15243. (backward-char)
  15244. (insert " " repeater)
  15245. (setq org-last-changed-timestamp
  15246. (concat (substring org-last-inserted-timestamp 0 -1)
  15247. " " repeater ">")))
  15248. (message "Timestamp updated"))
  15249. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15250. (t (org-insert-time-stamp
  15251. time (or org-time-was-given arg) inactive nil nil
  15252. (list org-end-time-was-given))))))
  15253. ;; FIXME: can we use this for something else, like computing time differences?
  15254. (defun org-get-compact-tod (s)
  15255. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15256. (let* ((t1 (match-string 1 s))
  15257. (h1 (string-to-number (match-string 2 s)))
  15258. (m1 (string-to-number (match-string 3 s)))
  15259. (t2 (and (match-end 4) (match-string 5 s)))
  15260. (h2 (and t2 (string-to-number (match-string 6 s))))
  15261. (m2 (and t2 (string-to-number (match-string 7 s))))
  15262. dh dm)
  15263. (if (not t2)
  15264. t1
  15265. (setq dh (- h2 h1) dm (- m2 m1))
  15266. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15267. (concat t1 "+" (number-to-string dh)
  15268. (and (/= 0 dm) (format ":%02d" dm)))))))
  15269. (defun org-time-stamp-inactive (&optional arg)
  15270. "Insert an inactive time stamp.
  15271. An inactive time stamp is enclosed in square brackets instead of angle
  15272. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15273. does not link to the calendar and cannot be changed with the S-cursor keys.
  15274. So these are more for recording a certain time/date."
  15275. (interactive "P")
  15276. (org-time-stamp arg 'inactive))
  15277. (defvar org-date-ovl (make-overlay 1 1))
  15278. (overlay-put org-date-ovl 'face 'org-date-selected)
  15279. (org-detach-overlay org-date-ovl)
  15280. (defvar org-ans1) ; dynamically scoped parameter
  15281. (defvar org-ans2) ; dynamically scoped parameter
  15282. (defvar org-plain-time-of-day-regexp) ; defined below
  15283. (defvar org-overriding-default-time nil) ; dynamically scoped
  15284. (defvar org-read-date-overlay nil)
  15285. (defvar org-dcst nil) ; dynamically scoped
  15286. (defvar org-read-date-history nil)
  15287. (defvar org-read-date-final-answer nil)
  15288. (defvar org-read-date-analyze-futurep nil)
  15289. (defvar org-read-date-analyze-forced-year nil)
  15290. (defvar org-read-date-inactive)
  15291. (defvar org-read-date-minibuffer-local-map
  15292. (let* ((map (make-sparse-keymap)))
  15293. (set-keymap-parent map minibuffer-local-map)
  15294. (org-defkey map (kbd ".")
  15295. (lambda () (interactive)
  15296. ;; Are we at the beginning of the prompt?
  15297. (if (looking-back "^[^:]+: ")
  15298. (org-eval-in-calendar '(calendar-goto-today))
  15299. (insert "."))))
  15300. (org-defkey map (kbd "C-.")
  15301. (lambda () (interactive)
  15302. (org-eval-in-calendar '(calendar-goto-today))))
  15303. (org-defkey map [(meta shift left)]
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar '(calendar-backward-month 1))))
  15306. (org-defkey map [(meta shift right)]
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(calendar-forward-month 1))))
  15309. (org-defkey map [(meta shift up)]
  15310. (lambda () (interactive)
  15311. (org-eval-in-calendar '(calendar-backward-year 1))))
  15312. (org-defkey map [(meta shift down)]
  15313. (lambda () (interactive)
  15314. (org-eval-in-calendar '(calendar-forward-year 1))))
  15315. (org-defkey map [?\e (shift left)]
  15316. (lambda () (interactive)
  15317. (org-eval-in-calendar '(calendar-backward-month 1))))
  15318. (org-defkey map [?\e (shift right)]
  15319. (lambda () (interactive)
  15320. (org-eval-in-calendar '(calendar-forward-month 1))))
  15321. (org-defkey map [?\e (shift up)]
  15322. (lambda () (interactive)
  15323. (org-eval-in-calendar '(calendar-backward-year 1))))
  15324. (org-defkey map [?\e (shift down)]
  15325. (lambda () (interactive)
  15326. (org-eval-in-calendar '(calendar-forward-year 1))))
  15327. (org-defkey map [(shift up)]
  15328. (lambda () (interactive)
  15329. (org-eval-in-calendar '(calendar-backward-week 1))))
  15330. (org-defkey map [(shift down)]
  15331. (lambda () (interactive)
  15332. (org-eval-in-calendar '(calendar-forward-week 1))))
  15333. (org-defkey map [(shift left)]
  15334. (lambda () (interactive)
  15335. (org-eval-in-calendar '(calendar-backward-day 1))))
  15336. (org-defkey map [(shift right)]
  15337. (lambda () (interactive)
  15338. (org-eval-in-calendar '(calendar-forward-day 1))))
  15339. (org-defkey map "!"
  15340. (lambda () (interactive)
  15341. (org-eval-in-calendar '(diary-view-entries))
  15342. (message "")))
  15343. (org-defkey map ">"
  15344. (lambda () (interactive)
  15345. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15346. (org-defkey map "<"
  15347. (lambda () (interactive)
  15348. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15349. (org-defkey map "\C-v"
  15350. (lambda () (interactive)
  15351. (org-eval-in-calendar
  15352. '(calendar-scroll-left-three-months 1))))
  15353. (org-defkey map "\M-v"
  15354. (lambda () (interactive)
  15355. (org-eval-in-calendar
  15356. '(calendar-scroll-right-three-months 1))))
  15357. map)
  15358. "Keymap for minibuffer commands when using `org-read-date'.")
  15359. (defvar org-def)
  15360. (defvar org-defdecode)
  15361. (defvar org-with-time)
  15362. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15363. default-time default-input inactive)
  15364. "Read a date, possibly a time, and make things smooth for the user.
  15365. The prompt will suggest to enter an ISO date, but you can also enter anything
  15366. which will at least partially be understood by `parse-time-string'.
  15367. Unrecognized parts of the date will default to the current day, month, year,
  15368. hour and minute. If this command is called to replace a timestamp at point,
  15369. or to enter the second timestamp of a range, the default time is taken
  15370. from the existing stamp. Furthermore, the command prefers the future,
  15371. so if you are giving a date where the year is not given, and the day-month
  15372. combination is already past in the current year, it will assume you
  15373. mean next year. For details, see the manual. A few examples:
  15374. 3-2-5 --> 2003-02-05
  15375. feb 15 --> currentyear-02-15
  15376. 2/15 --> currentyear-02-15
  15377. sep 12 9 --> 2009-09-12
  15378. 12:45 --> today 12:45
  15379. 22 sept 0:34 --> currentyear-09-22 0:34
  15380. 12 --> currentyear-currentmonth-12
  15381. Fri --> nearest Friday after today
  15382. -Tue --> last Tuesday
  15383. etc.
  15384. Furthermore you can specify a relative date by giving, as the *first* thing
  15385. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15386. change in days weeks, months, years.
  15387. With a single plus or minus, the date is relative to today. With a double
  15388. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15389. +4d --> four days from today
  15390. +4 --> same as above
  15391. +2w --> two weeks from today
  15392. ++5 --> five days from default date
  15393. The function understands only English month and weekday abbreviations.
  15394. While prompting, a calendar is popped up - you can also select the
  15395. date with the mouse (button 1). The calendar shows a period of three
  15396. months. To scroll it to other months, use the keys `>' and `<'.
  15397. If you don't like the calendar, turn it off with
  15398. \(setq org-read-date-popup-calendar nil)
  15399. With optional argument TO-TIME, the date will immediately be converted
  15400. to an internal time.
  15401. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15402. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15403. still enter a time, and this function will inform the calling routine
  15404. about this change. The calling routine may then choose to change the
  15405. format used to insert the time stamp into the buffer to include the time.
  15406. With optional argument FROM-STRING, read from this string instead from
  15407. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15408. the time/date that is used for everything that is not specified by the
  15409. user."
  15410. (require 'parse-time)
  15411. (let* ((org-time-stamp-rounding-minutes
  15412. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15413. (org-dcst org-display-custom-times)
  15414. (ct (org-current-time))
  15415. (org-def (or org-overriding-default-time default-time ct))
  15416. (org-defdecode (decode-time org-def))
  15417. (dummy (progn
  15418. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15419. (setcar (nthcdr 2 org-defdecode) -1)
  15420. (setcar (nthcdr 1 org-defdecode) 59)
  15421. (setq org-def (apply 'encode-time org-defdecode)
  15422. org-defdecode (decode-time org-def)))))
  15423. (cur-frame (selected-frame))
  15424. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15425. (calendar-frame-setup nil)
  15426. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15427. (calendar-move-hook nil)
  15428. (calendar-view-diary-initially-flag nil)
  15429. (calendar-view-holidays-initially-flag nil)
  15430. (timestr (format-time-string
  15431. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15432. (prompt (concat (if prompt (concat prompt " ") "")
  15433. (format "Date+time [%s]: " timestr)))
  15434. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15435. (cond
  15436. (from-string (setq ans from-string))
  15437. (org-read-date-popup-calendar
  15438. (save-excursion
  15439. (save-window-excursion
  15440. (calendar)
  15441. (when (eq calendar-setup 'calendar-only)
  15442. (setq cal-frame
  15443. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15444. (select-frame cal-frame))
  15445. (org-eval-in-calendar '(setq cursor-type nil) t)
  15446. (unwind-protect
  15447. (progn
  15448. (calendar-forward-day (- (time-to-days org-def)
  15449. (calendar-absolute-from-gregorian
  15450. (calendar-current-date))))
  15451. (org-eval-in-calendar nil t)
  15452. (let* ((old-map (current-local-map))
  15453. (map (copy-keymap calendar-mode-map))
  15454. (minibuffer-local-map
  15455. (copy-keymap org-read-date-minibuffer-local-map)))
  15456. (org-defkey map (kbd "RET") 'org-calendar-select)
  15457. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15458. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15459. (unwind-protect
  15460. (progn
  15461. (use-local-map map)
  15462. (setq org-read-date-inactive inactive)
  15463. (add-hook 'post-command-hook 'org-read-date-display)
  15464. (setq org-ans0 (read-string prompt default-input
  15465. 'org-read-date-history nil))
  15466. ;; org-ans0: from prompt
  15467. ;; org-ans1: from mouse click
  15468. ;; org-ans2: from calendar motion
  15469. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15470. (remove-hook 'post-command-hook 'org-read-date-display)
  15471. (use-local-map old-map)
  15472. (when org-read-date-overlay
  15473. (delete-overlay org-read-date-overlay)
  15474. (setq org-read-date-overlay nil)))))
  15475. (bury-buffer "*Calendar*")
  15476. (when cal-frame
  15477. (delete-frame cal-frame)
  15478. (select-frame-set-input-focus cur-frame))))))
  15479. (t ; Naked prompt only
  15480. (unwind-protect
  15481. (setq ans (read-string prompt default-input
  15482. 'org-read-date-history timestr))
  15483. (when org-read-date-overlay
  15484. (delete-overlay org-read-date-overlay)
  15485. (setq org-read-date-overlay nil)))))
  15486. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15487. (when org-read-date-analyze-forced-year
  15488. (message "Year was forced into %s"
  15489. (if org-read-date-force-compatible-dates
  15490. "compatible range (1970-2037)"
  15491. "range representable on this machine"))
  15492. (ding))
  15493. ;; One round trip to get rid of 34th of August and stuff like that....
  15494. (setq final (decode-time (apply 'encode-time final)))
  15495. (setq org-read-date-final-answer ans)
  15496. (if to-time
  15497. (apply 'encode-time final)
  15498. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15499. (format "%04d-%02d-%02d %02d:%02d"
  15500. (nth 5 final) (nth 4 final) (nth 3 final)
  15501. (nth 2 final) (nth 1 final))
  15502. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15503. (defun org-read-date-display ()
  15504. "Display the current date prompt interpretation in the minibuffer."
  15505. (when org-read-date-display-live
  15506. (when org-read-date-overlay
  15507. (delete-overlay org-read-date-overlay))
  15508. (when (minibufferp (current-buffer))
  15509. (save-excursion
  15510. (end-of-line 1)
  15511. (while (not (equal (buffer-substring
  15512. (max (point-min) (- (point) 4)) (point))
  15513. " "))
  15514. (insert " ")))
  15515. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15516. " " (or org-ans1 org-ans2)))
  15517. (org-end-time-was-given nil)
  15518. (f (org-read-date-analyze ans org-def org-defdecode))
  15519. (fmts (if org-dcst
  15520. org-time-stamp-custom-formats
  15521. org-time-stamp-formats))
  15522. (fmt (if (or org-with-time
  15523. (and (boundp 'org-time-was-given) org-time-was-given))
  15524. (cdr fmts)
  15525. (car fmts)))
  15526. (txt (format-time-string fmt (apply 'encode-time f)))
  15527. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15528. (txt (concat "=> " txt)))
  15529. (when (and org-end-time-was-given
  15530. (string-match org-plain-time-of-day-regexp txt))
  15531. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15532. org-end-time-was-given
  15533. (substring txt (match-end 0)))))
  15534. (when org-read-date-analyze-futurep
  15535. (setq txt (concat txt " (=>F)")))
  15536. (setq org-read-date-overlay
  15537. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15538. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15539. (defun org-read-date-analyze (ans org-def org-defdecode)
  15540. "Analyze the combined answer of the date prompt."
  15541. ;; FIXME: cleanup and comment
  15542. ;; Pass `current-time' result to `decode-time' (instead of calling
  15543. ;; without arguments) so that only `current-time' has to be
  15544. ;; overriden in tests.
  15545. (let ((nowdecode (decode-time (current-time)))
  15546. delta deltan deltaw deltadef year month day
  15547. hour minute second wday pm h2 m2 tl wday1
  15548. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15549. (setq org-read-date-analyze-futurep nil
  15550. org-read-date-analyze-forced-year nil)
  15551. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15552. (setq ans "+0"))
  15553. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15554. (setq ans (replace-match "" t t ans)
  15555. deltan (car delta)
  15556. deltaw (nth 1 delta)
  15557. deltadef (nth 2 delta)))
  15558. ;; Check if there is an iso week date in there. If yes, store the
  15559. ;; info and postpone interpreting it until the rest of the parsing
  15560. ;; is done.
  15561. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15562. (setq iso-year (if (match-end 1)
  15563. (org-small-year-to-year
  15564. (string-to-number (match-string 1 ans))))
  15565. iso-weekday (if (match-end 3)
  15566. (string-to-number (match-string 3 ans)))
  15567. iso-week (string-to-number (match-string 2 ans)))
  15568. (setq ans (replace-match "" t t ans)))
  15569. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15570. (when (string-match
  15571. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15572. (setq year (if (match-end 2)
  15573. (string-to-number (match-string 2 ans))
  15574. (progn (setq kill-year t)
  15575. (string-to-number (format-time-string "%Y"))))
  15576. month (string-to-number (match-string 3 ans))
  15577. day (string-to-number (match-string 4 ans)))
  15578. (if (< year 100) (setq year (+ 2000 year)))
  15579. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15580. t nil ans)))
  15581. ;; Help matching dotted european dates
  15582. (when (string-match
  15583. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15584. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15585. (setq kill-year t)
  15586. (string-to-number (format-time-string "%Y")))
  15587. day (string-to-number (match-string 1 ans))
  15588. month (string-to-number (match-string 2 ans))
  15589. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15590. t nil ans)))
  15591. ;; Help matching american dates, like 5/30 or 5/30/7
  15592. (when (string-match
  15593. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15594. (setq year (if (match-end 4)
  15595. (string-to-number (match-string 4 ans))
  15596. (progn (setq kill-year t)
  15597. (string-to-number (format-time-string "%Y"))))
  15598. month (string-to-number (match-string 1 ans))
  15599. day (string-to-number (match-string 2 ans)))
  15600. (if (< year 100) (setq year (+ 2000 year)))
  15601. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15602. t nil ans)))
  15603. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15604. ;; If there is a time with am/pm, and *no* time without it, we convert
  15605. ;; so that matching will be successful.
  15606. (loop for i from 1 to 2 do ; twice, for end time as well
  15607. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15608. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15609. (setq hour (string-to-number (match-string 1 ans))
  15610. minute (if (match-end 3)
  15611. (string-to-number (match-string 3 ans))
  15612. 0)
  15613. pm (equal ?p
  15614. (string-to-char (downcase (match-string 4 ans)))))
  15615. (if (and (= hour 12) (not pm))
  15616. (setq hour 0)
  15617. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15618. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15619. t t ans))))
  15620. ;; Check if a time range is given as a duration
  15621. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15622. (setq hour (string-to-number (match-string 1 ans))
  15623. h2 (+ hour (string-to-number (match-string 3 ans)))
  15624. minute (string-to-number (match-string 2 ans))
  15625. m2 (+ minute (if (match-end 5) (string-to-number
  15626. (match-string 5 ans))0)))
  15627. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15628. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15629. t t ans)))
  15630. ;; Check if there is a time range
  15631. (when (boundp 'org-end-time-was-given)
  15632. (setq org-time-was-given nil)
  15633. (when (and (string-match org-plain-time-of-day-regexp ans)
  15634. (match-end 8))
  15635. (setq org-end-time-was-given (match-string 8 ans))
  15636. (setq ans (concat (substring ans 0 (match-beginning 7))
  15637. (substring ans (match-end 7))))))
  15638. (setq tl (parse-time-string ans)
  15639. day (or (nth 3 tl) (nth 3 org-defdecode))
  15640. month
  15641. (cond ((nth 4 tl))
  15642. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15643. ;; Day was specified. Make sure DAY+MONTH
  15644. ;; combination happens in the future.
  15645. ((nth 3 tl)
  15646. (setq futurep t)
  15647. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15648. (nth 4 nowdecode)))
  15649. (t (nth 4 org-defdecode)))
  15650. year
  15651. (cond ((and (not kill-year) (nth 5 tl)))
  15652. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15653. ;; Month was guessed in the future and is at least
  15654. ;; equal to NOWDECODE's. Fix year accordingly.
  15655. (futurep
  15656. (if (or (> month (nth 4 nowdecode))
  15657. (>= day (nth 3 nowdecode)))
  15658. (nth 5 nowdecode)
  15659. (1+ (nth 5 nowdecode))))
  15660. ;; Month was specified. Make sure MONTH+YEAR
  15661. ;; combination happens in the future.
  15662. ((nth 4 tl)
  15663. (setq futurep t)
  15664. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15665. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15666. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15667. (t (nth 5 nowdecode))))
  15668. (t (nth 5 org-defdecode)))
  15669. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15670. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15671. second (or (nth 0 tl) 0)
  15672. wday (nth 6 tl))
  15673. (when (and (eq org-read-date-prefer-future 'time)
  15674. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15675. (equal day (nth 3 nowdecode))
  15676. (equal month (nth 4 nowdecode))
  15677. (equal year (nth 5 nowdecode))
  15678. (nth 2 tl)
  15679. (or (< (nth 2 tl) (nth 2 nowdecode))
  15680. (and (= (nth 2 tl) (nth 2 nowdecode))
  15681. (nth 1 tl)
  15682. (< (nth 1 tl) (nth 1 nowdecode)))))
  15683. (setq day (1+ day)
  15684. futurep t))
  15685. ;; Special date definitions below
  15686. (cond
  15687. (iso-week
  15688. ;; There was an iso week
  15689. (require 'cal-iso)
  15690. (setq futurep nil)
  15691. (setq year (or iso-year year)
  15692. day (or iso-weekday wday 1)
  15693. wday nil ; to make sure that the trigger below does not match
  15694. iso-date (calendar-gregorian-from-absolute
  15695. (calendar-iso-to-absolute
  15696. (list iso-week day year))))
  15697. ; FIXME: Should we also push ISO weeks into the future?
  15698. ; (when (and org-read-date-prefer-future
  15699. ; (not iso-year)
  15700. ; (< (calendar-absolute-from-gregorian iso-date)
  15701. ; (time-to-days (current-time))))
  15702. ; (setq year (1+ year)
  15703. ; iso-date (calendar-gregorian-from-absolute
  15704. ; (calendar-iso-to-absolute
  15705. ; (list iso-week day year)))))
  15706. (setq month (car iso-date)
  15707. year (nth 2 iso-date)
  15708. day (nth 1 iso-date)))
  15709. (deltan
  15710. (setq futurep nil)
  15711. (unless deltadef
  15712. ;; Pass `current-time' result to `decode-time' (instead of
  15713. ;; calling without arguments) so that only `current-time' has
  15714. ;; to be overriden in tests.
  15715. (let ((now (decode-time (current-time))))
  15716. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15717. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15718. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15719. ((equal deltaw "m") (setq month (+ month deltan)))
  15720. ((equal deltaw "y") (setq year (+ year deltan)))))
  15721. ((and wday (not (nth 3 tl)))
  15722. ;; Weekday was given, but no day, so pick that day in the week
  15723. ;; on or after the derived date.
  15724. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15725. (unless (equal wday wday1)
  15726. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15727. (if (and (boundp 'org-time-was-given)
  15728. (nth 2 tl))
  15729. (setq org-time-was-given t))
  15730. (if (< year 100) (setq year (+ 2000 year)))
  15731. ;; Check of the date is representable
  15732. (if org-read-date-force-compatible-dates
  15733. (progn
  15734. (if (< year 1970)
  15735. (setq year 1970 org-read-date-analyze-forced-year t))
  15736. (if (> year 2037)
  15737. (setq year 2037 org-read-date-analyze-forced-year t)))
  15738. (condition-case nil
  15739. (ignore (encode-time second minute hour day month year))
  15740. (error
  15741. (setq year (nth 5 org-defdecode))
  15742. (setq org-read-date-analyze-forced-year t))))
  15743. (setq org-read-date-analyze-futurep futurep)
  15744. (list second minute hour day month year)))
  15745. (defvar parse-time-weekdays)
  15746. (defun org-read-date-get-relative (s today default)
  15747. "Check string S for special relative date string.
  15748. TODAY and DEFAULT are internal times, for today and for a default.
  15749. Return shift list (N what def-flag)
  15750. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15751. N is the number of WHATs to shift.
  15752. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15753. the DEFAULT date rather than TODAY."
  15754. (require 'parse-time)
  15755. (when (and
  15756. (string-match
  15757. (concat
  15758. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15759. "\\([0-9]+\\)?"
  15760. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15761. "\\([ \t]\\|$\\)") s)
  15762. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15763. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15764. (string-to-char (substring (match-string 1 s) -1))
  15765. ?+))
  15766. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15767. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15768. (what (if (match-end 3) (match-string 3 s) "d"))
  15769. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15770. (date (if rel default today))
  15771. (wday (nth 6 (decode-time date)))
  15772. delta)
  15773. (if wday1
  15774. (progn
  15775. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15776. (if (= delta 0) (setq delta 7))
  15777. (if (= dir ?-)
  15778. (progn
  15779. (setq delta (- delta 7))
  15780. (if (= delta 0) (setq delta -7))))
  15781. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15782. (list delta "d" rel))
  15783. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15784. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15785. "Turn a user-specified date into the internal representation.
  15786. The internal representation needed by the calendar is (month day year).
  15787. This is a wrapper to handle the brain-dead convention in calendar that
  15788. user function argument order change dependent on argument order."
  15789. (if (boundp 'calendar-date-style)
  15790. (cond
  15791. ((eq calendar-date-style 'american)
  15792. (list arg1 arg2 arg3))
  15793. ((eq calendar-date-style 'european)
  15794. (list arg2 arg1 arg3))
  15795. ((eq calendar-date-style 'iso)
  15796. (list arg2 arg3 arg1)))
  15797. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15798. (if (org-bound-and-true-p european-calendar-style)
  15799. (list arg2 arg1 arg3)
  15800. (list arg1 arg2 arg3)))))
  15801. (defun org-eval-in-calendar (form &optional keepdate)
  15802. "Eval FORM in the calendar window and return to current window.
  15803. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15804. otherwise stick to the current value of `org-ans2'."
  15805. (let ((sf (selected-frame))
  15806. (sw (selected-window)))
  15807. (select-window (get-buffer-window "*Calendar*" t))
  15808. (eval form)
  15809. (when (and (not keepdate) (calendar-cursor-to-date))
  15810. (let* ((date (calendar-cursor-to-date))
  15811. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15812. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15813. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15814. (select-window sw)
  15815. (org-select-frame-set-input-focus sf)))
  15816. (defun org-calendar-select ()
  15817. "Return to `org-read-date' with the date currently selected.
  15818. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15819. (interactive)
  15820. (when (calendar-cursor-to-date)
  15821. (let* ((date (calendar-cursor-to-date))
  15822. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15823. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15824. (if (active-minibuffer-window) (exit-minibuffer))))
  15825. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15826. "Insert a date stamp for the date given by the internal TIME.
  15827. See `format-time-string' for the format of TIME.
  15828. WITH-HM means use the stamp format that includes the time of the day.
  15829. INACTIVE means use square brackets instead of angular ones, so that the
  15830. stamp will not contribute to the agenda.
  15831. PRE and POST are optional strings to be inserted before and after the
  15832. stamp.
  15833. The command returns the inserted time stamp."
  15834. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15835. stamp)
  15836. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15837. (insert-before-markers (or pre ""))
  15838. (when (listp extra)
  15839. (setq extra (car extra))
  15840. (if (and (stringp extra)
  15841. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15842. (setq extra (format "-%02d:%02d"
  15843. (string-to-number (match-string 1 extra))
  15844. (string-to-number (match-string 2 extra))))
  15845. (setq extra nil)))
  15846. (when extra
  15847. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15848. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15849. (insert-before-markers (or post ""))
  15850. (setq org-last-inserted-timestamp stamp)))
  15851. (defun org-toggle-time-stamp-overlays ()
  15852. "Toggle the use of custom time stamp formats."
  15853. (interactive)
  15854. (setq org-display-custom-times (not org-display-custom-times))
  15855. (unless org-display-custom-times
  15856. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15857. (while (setq p (next-single-property-change p 'display))
  15858. (if (and (get-text-property p 'display)
  15859. (eq (get-text-property p 'face) 'org-date))
  15860. (remove-text-properties
  15861. p (setq p (next-single-property-change p 'display))
  15862. '(display t))))
  15863. (set-buffer-modified-p bmp)))
  15864. (if (featurep 'xemacs)
  15865. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15866. (org-restart-font-lock)
  15867. (setq org-table-may-need-update t)
  15868. (if org-display-custom-times
  15869. (message "Time stamps are overlaid with custom format")
  15870. (message "Time stamp overlays removed")))
  15871. (defun org-display-custom-time (beg end)
  15872. "Overlay modified time stamp format over timestamp between BEG and END."
  15873. (let* ((ts (buffer-substring beg end))
  15874. t1 w1 with-hm tf time str w2 (off 0))
  15875. (save-match-data
  15876. (setq t1 (org-parse-time-string ts t))
  15877. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15878. (setq off (- (match-end 0) (match-beginning 0)))))
  15879. (setq end (- end off))
  15880. (setq w1 (- end beg)
  15881. with-hm (and (nth 1 t1) (nth 2 t1))
  15882. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15883. time (org-fix-decoded-time t1)
  15884. str (org-add-props
  15885. (format-time-string
  15886. (substring tf 1 -1) (apply 'encode-time time))
  15887. nil 'mouse-face 'highlight)
  15888. w2 (length str))
  15889. (if (not (= w2 w1))
  15890. (add-text-properties (1+ beg) (+ 2 beg)
  15891. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15892. (if (featurep 'xemacs)
  15893. (progn
  15894. (put-text-property beg end 'invisible t)
  15895. (put-text-property beg end 'end-glyph (make-glyph str)))
  15896. (put-text-property beg end 'display str))))
  15897. (defun org-fix-decoded-time (time)
  15898. "Set 0 instead of nil for the first 6 elements of time.
  15899. Don't touch the rest."
  15900. (let ((n 0))
  15901. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15902. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15903. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15904. "Difference between TIMESTAMP-STRING and now in days.
  15905. If SECONDS is non-nil, return the difference in seconds."
  15906. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15907. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15908. (funcall fdiff (current-time)))))
  15909. (defun org-deadline-close (timestamp-string &optional ndays)
  15910. "Is the time in TIMESTAMP-STRING close to the current date?"
  15911. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15912. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15913. (not (org-entry-is-done-p))))
  15914. (defun org-get-wdays (ts &optional delay zero-delay)
  15915. "Get the deadline lead time appropriate for timestring TS.
  15916. When DELAY is non-nil, get the delay time for scheduled items
  15917. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15918. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15919. don't try to find the delay cookie in the scheduled timestamp."
  15920. (let ((tv (if delay org-scheduled-delay-days
  15921. org-deadline-warning-days)))
  15922. (cond
  15923. ((or (and delay (< tv 0))
  15924. (and delay zero-delay (<= tv 0))
  15925. (and (not delay) (<= tv 0)))
  15926. ;; Enforce this value no matter what
  15927. (- tv))
  15928. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15929. ;; lead time is specified.
  15930. (floor (* (string-to-number (match-string 1 ts))
  15931. (cdr (assoc (match-string 2 ts)
  15932. '(("d" . 1) ("w" . 7)
  15933. ("m" . 30.4) ("y" . 365.25)
  15934. ("h" . 0.041667)))))))
  15935. ;; go for the default.
  15936. (t tv))))
  15937. (defun org-calendar-select-mouse (ev)
  15938. "Return to `org-read-date' with the date currently selected.
  15939. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15940. (interactive "e")
  15941. (mouse-set-point ev)
  15942. (when (calendar-cursor-to-date)
  15943. (let* ((date (calendar-cursor-to-date))
  15944. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15945. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15946. (if (active-minibuffer-window) (exit-minibuffer))))
  15947. (defun org-check-deadlines (ndays)
  15948. "Check if there are any deadlines due or past due.
  15949. A deadline is considered due if it happens within `org-deadline-warning-days'
  15950. days from today's date. If the deadline appears in an entry marked DONE,
  15951. it is not shown. The prefix arg NDAYS can be used to test that many
  15952. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15953. (interactive "P")
  15954. (let* ((org-warn-days
  15955. (cond
  15956. ((equal ndays '(4)) 100000)
  15957. (ndays (prefix-numeric-value ndays))
  15958. (t (abs org-deadline-warning-days))))
  15959. (case-fold-search nil)
  15960. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15961. (callback
  15962. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15963. (message "%d deadlines past-due or due within %d days"
  15964. (org-occur regexp nil callback)
  15965. org-warn-days)))
  15966. (defsubst org-re-timestamp (type)
  15967. "Return a regexp for timestamp TYPE.
  15968. Allowed values for TYPE are:
  15969. all: all timestamps
  15970. active: only active timestamps (<...>)
  15971. inactive: only inactive timestamps ([...])
  15972. scheduled: only scheduled timestamps
  15973. deadline: only deadline timestamps
  15974. closed: only closed time-stamps
  15975. When TYPE is nil, fall back on returning a regexp that matches
  15976. both scheduled and deadline timestamps."
  15977. (case type
  15978. (all org-ts-regexp-both)
  15979. (active org-ts-regexp)
  15980. (inactive org-ts-regexp-inactive)
  15981. (scheduled org-scheduled-time-regexp)
  15982. (deadline org-deadline-time-regexp)
  15983. (closed org-closed-time-regexp)
  15984. (otherwise
  15985. (concat "\\<"
  15986. (regexp-opt (list org-deadline-string org-scheduled-string))
  15987. " *<\\([^>]+\\)>"))))
  15988. (defun org-check-before-date (date)
  15989. "Check if there are deadlines or scheduled entries before DATE."
  15990. (interactive (list (org-read-date)))
  15991. (let ((case-fold-search nil)
  15992. (regexp (org-re-timestamp org-ts-type))
  15993. (callback
  15994. `(lambda ()
  15995. (let ((match (match-string 1)))
  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)
  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. (let ((match (match-string 1)))
  16012. (and ,(if (memq org-ts-type '(active inactive all))
  16013. '(eq (org-element-type (org-element-context)) 'timestamp)
  16014. '(org-at-planning-p))
  16015. (not (time-less-p
  16016. (org-time-string-to-time match)
  16017. (org-time-string-to-time date))))))))
  16018. (message "%d entries after %s"
  16019. (org-occur regexp nil callback) date)))
  16020. (defun org-check-dates-range (start-date end-date)
  16021. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16022. (interactive (list (org-read-date nil nil nil "Range starts")
  16023. (org-read-date nil nil nil "Range end")))
  16024. (let ((case-fold-search nil)
  16025. (regexp (org-re-timestamp org-ts-type))
  16026. (callback
  16027. `(lambda ()
  16028. (let ((match (match-string 1)))
  16029. (and
  16030. ,(if (memq org-ts-type '(active inactive all))
  16031. '(eq (org-element-type (org-element-context)) 'timestamp)
  16032. '(org-at-planning-p))
  16033. (not (time-less-p
  16034. (org-time-string-to-time match)
  16035. (org-time-string-to-time start-date)))
  16036. (time-less-p
  16037. (org-time-string-to-time match)
  16038. (org-time-string-to-time end-date)))))))
  16039. (message "%d entries between %s and %s"
  16040. (org-occur regexp nil callback) start-date end-date)))
  16041. (defun org-evaluate-time-range (&optional to-buffer)
  16042. "Evaluate a time range by computing the difference between start and end.
  16043. Normally the result is just printed in the echo area, but with prefix arg
  16044. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16045. If the time range is actually in a table, the result is inserted into the
  16046. next column.
  16047. For time difference computation, a year is assumed to be exactly 365
  16048. days in order to avoid rounding problems."
  16049. (interactive "P")
  16050. (or
  16051. (org-clock-update-time-maybe)
  16052. (save-excursion
  16053. (unless (org-at-date-range-p t)
  16054. (goto-char (point-at-bol))
  16055. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16056. (if (not (org-at-date-range-p t))
  16057. (user-error "Not at a time-stamp range, and none found in current line")))
  16058. (let* ((ts1 (match-string 1))
  16059. (ts2 (match-string 2))
  16060. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16061. (match-end (match-end 0))
  16062. (time1 (org-time-string-to-time ts1))
  16063. (time2 (org-time-string-to-time ts2))
  16064. (t1 (org-float-time time1))
  16065. (t2 (org-float-time time2))
  16066. (diff (abs (- t2 t1)))
  16067. (negative (< (- t2 t1) 0))
  16068. ;; (ys (floor (* 365 24 60 60)))
  16069. (ds (* 24 60 60))
  16070. (hs (* 60 60))
  16071. (fy "%dy %dd %02d:%02d")
  16072. (fy1 "%dy %dd")
  16073. (fd "%dd %02d:%02d")
  16074. (fd1 "%dd")
  16075. (fh "%02d:%02d")
  16076. y d h m align)
  16077. (if havetime
  16078. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16079. y 0
  16080. d (floor (/ diff ds)) diff (mod diff ds)
  16081. h (floor (/ diff hs)) diff (mod diff hs)
  16082. m (floor (/ diff 60)))
  16083. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16084. y 0
  16085. d (floor (+ (/ diff ds) 0.5))
  16086. h 0 m 0))
  16087. (if (not to-buffer)
  16088. (message "%s" (org-make-tdiff-string y d h m))
  16089. (if (org-at-table-p)
  16090. (progn
  16091. (goto-char match-end)
  16092. (setq align t)
  16093. (and (looking-at " *|") (goto-char (match-end 0))))
  16094. (goto-char match-end))
  16095. (if (looking-at
  16096. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16097. (replace-match ""))
  16098. (if negative (insert " -"))
  16099. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16100. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16101. (insert " " (format fh h m))))
  16102. (if align (org-table-align))
  16103. (message "Time difference inserted")))))
  16104. (defun org-make-tdiff-string (y d h m)
  16105. (let ((fmt "")
  16106. (l nil))
  16107. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16108. l (push y l)))
  16109. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16110. l (push d l)))
  16111. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16112. l (push h l)))
  16113. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16114. l (push m l)))
  16115. (apply 'format fmt (nreverse l))))
  16116. (defun org-time-string-to-time (s &optional buffer pos)
  16117. "Convert a timestamp string into internal time."
  16118. (condition-case errdata
  16119. (apply 'encode-time (org-parse-time-string s))
  16120. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16121. s (if (not (and buffer pos))
  16122. ""
  16123. (format " at %d in buffer `%s'" pos buffer))
  16124. (cdr errdata)))))
  16125. (defun org-time-string-to-seconds (s)
  16126. "Convert a timestamp string to a number of seconds."
  16127. (org-float-time (org-time-string-to-time s)))
  16128. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16129. "Convert a time stamp to an absolute day number.
  16130. If there is a specifier for a cyclic time stamp, get the closest
  16131. date to DAYNR.
  16132. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16133. The variable `date' is bound by the calendar when this is called."
  16134. (cond
  16135. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16136. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16137. daynr
  16138. (+ daynr 1000)))
  16139. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16140. (> (string-to-number (match-string 1 s)) 0))
  16141. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16142. (time-to-days (current-time))) (match-string 0 s)
  16143. prefer show-all))
  16144. (t (time-to-days
  16145. (condition-case errdata
  16146. (apply 'encode-time (org-parse-time-string s))
  16147. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16148. s (if (not (and buffer pos))
  16149. ""
  16150. (format " at %d in buffer `%s'" pos buffer))
  16151. (cdr errdata))))))))
  16152. (defun org-days-to-iso-week (days)
  16153. "Return the iso week number."
  16154. (require 'cal-iso)
  16155. (car (calendar-iso-from-absolute days)))
  16156. (defun org-small-year-to-year (year)
  16157. "Convert 2-digit years into 4-digit years.
  16158. YEAR is expanded into one of the 30 next years, if possible, or
  16159. into a past one. Any year larger than 99 is returned unchanged."
  16160. (if (>= year 100) year
  16161. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16162. (century (/ current 100))
  16163. (offset (- year (% current 100))))
  16164. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16165. ((> offset -70) (+ (* century 100) year))
  16166. (t (+ (* (1+ century) 100) year))))))
  16167. (defun org-time-from-absolute (d)
  16168. "Return the time corresponding to date D.
  16169. D may be an absolute day number, or a calendar-type list (month day year)."
  16170. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16171. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16172. (defun org-calendar-holiday ()
  16173. "List of holidays, for Diary display in Org-mode."
  16174. (require 'holidays)
  16175. (let ((hl (funcall
  16176. (if (fboundp 'calendar-check-holidays)
  16177. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16178. (if hl (mapconcat 'identity hl "; "))))
  16179. (defun org-diary-sexp-entry (sexp entry date)
  16180. "Process a SEXP diary ENTRY for DATE."
  16181. (require 'diary-lib)
  16182. (let ((result (if calendar-debug-sexp
  16183. (let ((stack-trace-on-error t))
  16184. (eval (car (read-from-string sexp))))
  16185. (condition-case nil
  16186. (eval (car (read-from-string sexp)))
  16187. (error
  16188. (beep)
  16189. (message "Bad sexp at line %d in %s: %s"
  16190. (org-current-line)
  16191. (buffer-file-name) sexp)
  16192. (sleep-for 2))))))
  16193. (cond ((stringp result) (split-string result "; "))
  16194. ((and (consp result)
  16195. (not (consp (cdr result)))
  16196. (stringp (cdr result))) (cdr result))
  16197. ((and (consp result)
  16198. (stringp (car result))) result)
  16199. (result entry))))
  16200. (defun org-diary-to-ical-string (frombuf)
  16201. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16202. This uses the icalendar.el library."
  16203. (let* ((tmpdir (if (featurep 'xemacs)
  16204. (temp-directory)
  16205. temporary-file-directory))
  16206. (tmpfile (make-temp-name
  16207. (expand-file-name "orgics" tmpdir)))
  16208. buf rtn b e)
  16209. (with-current-buffer frombuf
  16210. (icalendar-export-region (point-min) (point-max) tmpfile)
  16211. (setq buf (find-buffer-visiting tmpfile))
  16212. (set-buffer buf)
  16213. (goto-char (point-min))
  16214. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16215. (setq b (match-beginning 0)))
  16216. (goto-char (point-max))
  16217. (if (re-search-backward "^END:VEVENT" nil t)
  16218. (setq e (match-end 0)))
  16219. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16220. (kill-buffer buf)
  16221. (delete-file tmpfile)
  16222. rtn))
  16223. (defun org-closest-date (start current change prefer show-all)
  16224. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16225. When PREFER is `past', return a date that is either CURRENT or past.
  16226. When PREFER is `future', return a date that is either CURRENT or future.
  16227. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16228. ;; Make the proper lists from the dates
  16229. (catch 'exit
  16230. (let ((a1 '(("h" . hour)
  16231. ("d" . day)
  16232. ("w" . week)
  16233. ("m" . month)
  16234. ("y" . year)))
  16235. (shour (nth 2 (org-parse-time-string start)))
  16236. dn dw sday cday n1 n2 n0
  16237. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16238. (setq start (org-date-to-gregorian start)
  16239. current (org-date-to-gregorian
  16240. (if show-all
  16241. current
  16242. (time-to-days (current-time))))
  16243. sday (calendar-absolute-from-gregorian start)
  16244. cday (calendar-absolute-from-gregorian current))
  16245. (if (<= cday sday) (throw 'exit sday))
  16246. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16247. (setq dn (string-to-number (match-string 1 change))
  16248. dw (cdr (assoc (match-string 2 change) a1))))
  16249. (unless (and dn (> dn 0))
  16250. (user-error "Invalid change specifier: %s" change))
  16251. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16252. (cond
  16253. ((eq dw 'hour)
  16254. (let ((missing-hours
  16255. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16256. dn)))
  16257. (setq n1 (if (zerop missing-hours) cday
  16258. (- cday (1+ (floor (/ missing-hours 24)))))
  16259. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16260. ((eq dw 'day)
  16261. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16262. n2 (+ n1 dn)))
  16263. ((eq dw 'year)
  16264. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16265. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16266. (setq date1 (list m d y1)
  16267. n1 (calendar-absolute-from-gregorian date1)
  16268. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16269. n2 (calendar-absolute-from-gregorian date2)))
  16270. ((eq dw 'month)
  16271. ;; approx number of month between the two dates
  16272. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16273. ;; How often does dn fit in there?
  16274. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16275. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16276. m (+ m nm)
  16277. ny (floor (/ m 12))
  16278. y (+ y ny)
  16279. m (- m (* ny 12)))
  16280. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16281. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16282. (setq m2 (+ m dn) y2 y)
  16283. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16284. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16285. (while (<= n2 cday)
  16286. (setq n1 n2 m m2 y y2)
  16287. (setq m2 (+ m dn) y2 y)
  16288. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16289. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16290. ;; Make sure n1 is the earlier date
  16291. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16292. (if show-all
  16293. (cond
  16294. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16295. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16296. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16297. (cond
  16298. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16299. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16300. (t (if (= cday n1) n1 n2)))))))
  16301. (defun org-date-to-gregorian (date)
  16302. "Turn any specification of DATE into a Gregorian date for the calendar."
  16303. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16304. ((and (listp date) (= (length date) 3)) date)
  16305. ((stringp date)
  16306. (setq date (org-parse-time-string date))
  16307. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16308. ((listp date)
  16309. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16310. (defun org-parse-time-string (s &optional nodefault)
  16311. "Parse the standard Org-mode time string.
  16312. This should be a lot faster than the normal `parse-time-string'.
  16313. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16314. hour and minute fields will be nil if not given."
  16315. (cond ((string-match org-ts-regexp0 s)
  16316. (list 0
  16317. (if (or (match-beginning 8) (not nodefault))
  16318. (string-to-number (or (match-string 8 s) "0")))
  16319. (if (or (match-beginning 7) (not nodefault))
  16320. (string-to-number (or (match-string 7 s) "0")))
  16321. (string-to-number (match-string 4 s))
  16322. (string-to-number (match-string 3 s))
  16323. (string-to-number (match-string 2 s))
  16324. nil nil nil))
  16325. ((string-match "^<[^>]+>$" s)
  16326. (decode-time (seconds-to-time (org-matcher-time s))))
  16327. (t (error "Not a standard Org-mode time string: %s" s))))
  16328. (defun org-timestamp-up (&optional arg)
  16329. "Increase the date item at the cursor by one.
  16330. If the cursor is on the year, change the year. If it is on the month,
  16331. the day or the time, change that.
  16332. With prefix ARG, change by that many units."
  16333. (interactive "p")
  16334. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16335. (defun org-timestamp-down (&optional arg)
  16336. "Decrease the date item at the cursor by one.
  16337. If the cursor is on the year, change the year. If it is on the month,
  16338. the day or the time, change that.
  16339. With prefix ARG, change by that many units."
  16340. (interactive "p")
  16341. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16342. (defun org-timestamp-up-day (&optional arg)
  16343. "Increase the date in the time stamp by one day.
  16344. With prefix ARG, change that many days."
  16345. (interactive "p")
  16346. (if (and (not (org-at-timestamp-p t))
  16347. (org-at-heading-p))
  16348. (org-todo 'up)
  16349. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16350. (defun org-timestamp-down-day (&optional arg)
  16351. "Decrease the date in the time stamp by one day.
  16352. With prefix ARG, change that many days."
  16353. (interactive "p")
  16354. (if (and (not (org-at-timestamp-p t))
  16355. (org-at-heading-p))
  16356. (org-todo 'down)
  16357. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16358. (defun org-at-timestamp-p (&optional inactive-ok)
  16359. "Non-nil if point is inside a timestamp.
  16360. When optional argument INACTIVE-OK is non-nil, also consider
  16361. inactive timestamps.
  16362. When this function returns a non-nil value, match data is set
  16363. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16364. INACTIVE-OK."
  16365. (interactive)
  16366. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16367. (pos (point))
  16368. (ans (or (looking-at tsr)
  16369. (save-excursion
  16370. (skip-chars-backward "^[<\n\r\t")
  16371. (if (> (point) (point-min)) (backward-char 1))
  16372. (and (looking-at tsr)
  16373. (> (- (match-end 0) pos) -1))))))
  16374. (and ans
  16375. (boundp 'org-ts-what)
  16376. (setq org-ts-what
  16377. (cond
  16378. ((= pos (match-beginning 0)) 'bracket)
  16379. ;; Point is considered to be "on the bracket" whether
  16380. ;; it's really on it or right after it.
  16381. ((= pos (1- (match-end 0))) 'bracket)
  16382. ((= pos (match-end 0)) 'after)
  16383. ((org-pos-in-match-range pos 2) 'year)
  16384. ((org-pos-in-match-range pos 3) 'month)
  16385. ((org-pos-in-match-range pos 7) 'hour)
  16386. ((org-pos-in-match-range pos 8) 'minute)
  16387. ((or (org-pos-in-match-range pos 4)
  16388. (org-pos-in-match-range pos 5)) 'day)
  16389. ((and (> pos (or (match-end 8) (match-end 5)))
  16390. (< pos (match-end 0)))
  16391. (- pos (or (match-end 8) (match-end 5))))
  16392. (t 'day))))
  16393. ans))
  16394. (defun org-toggle-timestamp-type ()
  16395. "Toggle the type (<active> or [inactive]) of a time stamp."
  16396. (interactive)
  16397. (when (org-at-timestamp-p t)
  16398. (let ((beg (match-beginning 0)) (end (match-end 0))
  16399. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16400. (save-excursion
  16401. (goto-char beg)
  16402. (while (re-search-forward "[][<>]" end t)
  16403. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16404. t t)))
  16405. (message "Timestamp is now %sactive"
  16406. (if (equal (char-after beg) ?<) "" "in")))))
  16407. (defun org-at-clock-log-p nil
  16408. "Is the cursor on the clock log line?"
  16409. (save-excursion
  16410. (move-beginning-of-line 1)
  16411. (looking-at org-clock-line-re)))
  16412. (defvar org-clock-history) ; defined in org-clock.el
  16413. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16414. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16415. "Change the date in the time stamp at point.
  16416. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16417. `year', `minute', `second'. If WHAT is not given, the cursor position
  16418. in the timestamp determines what will be changed.
  16419. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16420. (let ((origin (point)) origin-cat
  16421. with-hm inactive
  16422. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16423. org-ts-what
  16424. extra rem
  16425. ts time time0 fixnext clrgx)
  16426. (if (not (org-at-timestamp-p t))
  16427. (user-error "Not at a timestamp"))
  16428. (if (and (not what) (eq org-ts-what 'bracket))
  16429. (org-toggle-timestamp-type)
  16430. ;; Point isn't on brackets. Remember the part of the time-stamp
  16431. ;; the point was in. Indeed, size of time-stamps may change,
  16432. ;; but point must be kept in the same category nonetheless.
  16433. (setq origin-cat org-ts-what)
  16434. (if (and (not what) (not (eq org-ts-what 'day))
  16435. org-display-custom-times
  16436. (get-text-property (point) 'display)
  16437. (not (get-text-property (1- (point)) 'display)))
  16438. (setq org-ts-what 'day))
  16439. (setq org-ts-what (or what org-ts-what)
  16440. inactive (= (char-after (match-beginning 0)) ?\[)
  16441. ts (match-string 0))
  16442. (replace-match "")
  16443. (when (string-match
  16444. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16445. ts)
  16446. (setq extra (match-string 1 ts))
  16447. (if suppress-tmp-delay
  16448. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16449. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16450. (setq with-hm t))
  16451. (setq time0 (org-parse-time-string ts))
  16452. (when (and updown
  16453. (eq org-ts-what 'minute)
  16454. (not current-prefix-arg))
  16455. ;; This looks like s-up and s-down. Change by one rounding step.
  16456. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16457. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16458. (setcar (cdr time0) (+ (nth 1 time0)
  16459. (if (> n 0) (- rem) (- dm rem))))))
  16460. (setq time
  16461. (encode-time (or (car time0) 0)
  16462. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16463. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16464. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16465. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16466. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16467. (nthcdr 6 time0)))
  16468. (when (and (member org-ts-what '(hour minute))
  16469. extra
  16470. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16471. (setq extra (org-modify-ts-extra
  16472. extra
  16473. (if (eq org-ts-what 'hour) 2 5)
  16474. n dm)))
  16475. (when (integerp org-ts-what)
  16476. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16477. (if (eq what 'calendar)
  16478. (let ((cal-date (org-get-date-from-calendar)))
  16479. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16480. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16481. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16482. (setcar time0 (or (car time0) 0))
  16483. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16484. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16485. (setq time (apply 'encode-time time0))))
  16486. ;; Insert the new time-stamp, and ensure point stays in the same
  16487. ;; category as before (i.e. not after the last position in that
  16488. ;; category).
  16489. (let ((pos (point)))
  16490. ;; Stay before inserted string. `save-excursion' is of no use.
  16491. (setq org-last-changed-timestamp
  16492. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16493. (goto-char pos))
  16494. (save-match-data
  16495. (looking-at org-ts-regexp3)
  16496. (goto-char (cond
  16497. ;; `day' category ends before `hour' if any, or at
  16498. ;; the end of the day name.
  16499. ((eq origin-cat 'day)
  16500. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16501. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16502. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16503. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16504. ;; `year' and `month' have both fixed size: point
  16505. ;; couldn't have moved into another part.
  16506. (t origin))))
  16507. ;; Update clock if on a CLOCK line.
  16508. (org-clock-update-time-maybe)
  16509. ;; Maybe adjust the closest clock in `org-clock-history'
  16510. (when org-clock-adjust-closest
  16511. (if (not (and (org-at-clock-log-p)
  16512. (< 1 (length (delq nil (mapcar 'marker-position
  16513. org-clock-history))))))
  16514. (message "No clock to adjust")
  16515. (cond ((save-excursion ; fix previous clock?
  16516. (re-search-backward org-ts-regexp0 nil t)
  16517. (org-looking-back (concat org-clock-string " \\[")))
  16518. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16519. ((save-excursion ; fix next clock?
  16520. (re-search-backward org-ts-regexp0 nil t)
  16521. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16522. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16523. (save-window-excursion
  16524. ;; Find closest clock to point, adjust the previous/next one in history
  16525. (let* ((p (save-excursion (org-back-to-heading t)))
  16526. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16527. (clfixnth
  16528. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16529. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16530. (if (not clfixpos)
  16531. (message "No clock to adjust")
  16532. (save-excursion
  16533. (org-goto-marker-or-bmk clfixpos)
  16534. (org-show-subtree)
  16535. (when (re-search-forward clrgx nil t)
  16536. (goto-char (match-beginning 1))
  16537. (let (org-clock-adjust-closest)
  16538. (org-timestamp-change n org-ts-what updown))
  16539. (message "Clock adjusted in %s for heading: %s"
  16540. (file-name-nondirectory (buffer-file-name))
  16541. (org-get-heading t t)))))))))
  16542. ;; Try to recenter the calendar window, if any.
  16543. (if (and org-calendar-follow-timestamp-change
  16544. (get-buffer-window "*Calendar*" t)
  16545. (memq org-ts-what '(day month year)))
  16546. (org-recenter-calendar (time-to-days time))))))
  16547. (defun org-modify-ts-extra (s pos n dm)
  16548. "Change the different parts of the lead-time and repeat fields in timestamp."
  16549. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16550. ng h m new rem)
  16551. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16552. (cond
  16553. ((or (org-pos-in-match-range pos 2)
  16554. (org-pos-in-match-range pos 3))
  16555. (setq m (string-to-number (match-string 3 s))
  16556. h (string-to-number (match-string 2 s)))
  16557. (if (org-pos-in-match-range pos 2)
  16558. (setq h (+ h n))
  16559. (setq n (* dm (org-no-warnings (signum n))))
  16560. (when (not (= 0 (setq rem (% m dm))))
  16561. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16562. (setq m (+ m n)))
  16563. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16564. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16565. (setq h (min 24 (max 0 h)))
  16566. (setq ng 1 new (format "-%02d:%02d" h m)))
  16567. ((org-pos-in-match-range pos 6)
  16568. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16569. ((org-pos-in-match-range pos 5)
  16570. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16571. ((org-pos-in-match-range pos 9)
  16572. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16573. ((org-pos-in-match-range pos 8)
  16574. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16575. (when ng
  16576. (setq s (concat
  16577. (substring s 0 (match-beginning ng))
  16578. new
  16579. (substring s (match-end ng))))))
  16580. s))
  16581. (defun org-recenter-calendar (date)
  16582. "If the calendar is visible, recenter it to DATE."
  16583. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16584. (when cwin
  16585. (let ((calendar-move-hook nil))
  16586. (with-selected-window cwin
  16587. (calendar-goto-date (if (listp date) date
  16588. (calendar-gregorian-from-absolute date))))))))
  16589. (defun org-goto-calendar (&optional arg)
  16590. "Go to the Emacs calendar at the current date.
  16591. If there is a time stamp in the current line, go to that date.
  16592. A prefix ARG can be used to force the current date."
  16593. (interactive "P")
  16594. (let ((tsr org-ts-regexp) diff
  16595. (calendar-move-hook nil)
  16596. (calendar-view-holidays-initially-flag nil)
  16597. (calendar-view-diary-initially-flag nil))
  16598. (if (or (org-at-timestamp-p)
  16599. (save-excursion
  16600. (beginning-of-line 1)
  16601. (looking-at (concat ".*" tsr))))
  16602. (let ((d1 (time-to-days (current-time)))
  16603. (d2 (time-to-days
  16604. (org-time-string-to-time (match-string 1)))))
  16605. (setq diff (- d2 d1))))
  16606. (calendar)
  16607. (calendar-goto-today)
  16608. (if (and diff (not arg)) (calendar-forward-day diff))))
  16609. (defun org-get-date-from-calendar ()
  16610. "Return a list (month day year) of date at point in calendar."
  16611. (with-current-buffer "*Calendar*"
  16612. (save-match-data
  16613. (calendar-cursor-to-date))))
  16614. (defun org-date-from-calendar ()
  16615. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16616. If there is already a time stamp at the cursor position, update it."
  16617. (interactive)
  16618. (if (org-at-timestamp-p t)
  16619. (org-timestamp-change 0 'calendar)
  16620. (let ((cal-date (org-get-date-from-calendar)))
  16621. (org-insert-time-stamp
  16622. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16623. (defcustom org-effort-durations
  16624. `(("min" . 1)
  16625. ("h" . 60)
  16626. ("d" . ,(* 60 8))
  16627. ("w" . ,(* 60 8 5))
  16628. ("m" . ,(* 60 8 5 4))
  16629. ("y" . ,(* 60 8 5 40)))
  16630. "Conversion factor to minutes for an effort modifier.
  16631. Each entry has the form (MODIFIER . MINUTES).
  16632. In an effort string, a number followed by MODIFIER is multiplied
  16633. by the specified number of MINUTES to obtain an effort in
  16634. minutes.
  16635. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16636. effort string \"2hours\" is equivalent to 120 minutes."
  16637. :group 'org-agenda
  16638. :version "25.1"
  16639. :package-version '(Org . "8.3")
  16640. :type '(alist :key-type (string :tag "Modifier")
  16641. :value-type (number :tag "Minutes")))
  16642. (defun org-minutes-to-clocksum-string (m)
  16643. "Format number of minutes as a clocksum string.
  16644. The format is determined by `org-time-clocksum-format',
  16645. `org-time-clocksum-use-fractional' and
  16646. `org-time-clocksum-fractional-format' and
  16647. `org-time-clocksum-use-effort-durations'."
  16648. (let ((clocksum "")
  16649. (m (round m)) ; Don't allow fractions of minutes
  16650. h d w mo y fmt n)
  16651. (setq h (if org-time-clocksum-use-effort-durations
  16652. (cdr (assoc "h" org-effort-durations)) 60)
  16653. d (if org-time-clocksum-use-effort-durations
  16654. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16655. w (if org-time-clocksum-use-effort-durations
  16656. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16657. mo (if org-time-clocksum-use-effort-durations
  16658. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16659. y (if org-time-clocksum-use-effort-durations
  16660. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16661. ;; fractional format
  16662. (if org-time-clocksum-use-fractional
  16663. (cond
  16664. ;; single format string
  16665. ((stringp org-time-clocksum-fractional-format)
  16666. (format org-time-clocksum-fractional-format (/ m (float h))))
  16667. ;; choice of fractional formats for different time units
  16668. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16669. (> (/ (truncate m) (* y d h)) 0))
  16670. (format fmt (/ m (* y d (float h)))))
  16671. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16672. (> (/ (truncate m) (* mo d h)) 0))
  16673. (format fmt (/ m (* mo d (float h)))))
  16674. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16675. (> (/ (truncate m) (* w d h)) 0))
  16676. (format fmt (/ m (* w d (float h)))))
  16677. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16678. (> (/ (truncate m) (* d h)) 0))
  16679. (format fmt (/ m (* d (float h)))))
  16680. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16681. (> (/ (truncate m) h) 0))
  16682. (format fmt (/ m (float h))))
  16683. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16684. (format fmt m))
  16685. ;; fall back to smallest time unit with a format
  16686. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16687. (format fmt (/ m (float h))))
  16688. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16689. (format fmt (/ m (* d (float h)))))
  16690. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16691. (format fmt (/ m (* w d (float h)))))
  16692. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16693. (format fmt (/ m (* mo d (float h)))))
  16694. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16695. (format fmt (/ m (* y d (float h))))))
  16696. ;; standard (non-fractional) format, with single format string
  16697. (if (stringp org-time-clocksum-format)
  16698. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16699. ;; separate formats components
  16700. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16701. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16702. (plist-get org-time-clocksum-format :require-years))
  16703. (setq clocksum (concat clocksum (format fmt n))
  16704. m (- m (* n y d h))))
  16705. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16706. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16707. (plist-get org-time-clocksum-format :require-months))
  16708. (setq clocksum (concat clocksum (format fmt n))
  16709. m (- m (* n mo d h))))
  16710. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16711. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16712. (plist-get org-time-clocksum-format :require-weeks))
  16713. (setq clocksum (concat clocksum (format fmt n))
  16714. m (- m (* n w d h))))
  16715. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16716. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16717. (plist-get org-time-clocksum-format :require-days))
  16718. (setq clocksum (concat clocksum (format fmt n))
  16719. m (- m (* n d h))))
  16720. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16721. (or (> (setq n (/ (truncate m) h)) 0)
  16722. (plist-get org-time-clocksum-format :require-hours))
  16723. (setq clocksum (concat clocksum (format fmt n))
  16724. m (- m (* n h))))
  16725. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16726. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16727. (setq clocksum (concat clocksum (format fmt m))))
  16728. ;; return formatted time duration
  16729. clocksum))))
  16730. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16731. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16732. "Org mode version 8.0")
  16733. (defun org-hours-to-clocksum-string (n)
  16734. (org-minutes-to-clocksum-string (* n 60)))
  16735. (defun org-hh:mm-string-to-minutes (s)
  16736. "Convert a string H:MM to a number of minutes.
  16737. If the string is just a number, interpret it as minutes.
  16738. In fact, the first hh:mm or number in the string will be taken,
  16739. there can be extra stuff in the string.
  16740. If no number is found, the return value is 0."
  16741. (cond
  16742. ((integerp s) s)
  16743. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16744. (+ (* (string-to-number (match-string 1 s)) 60)
  16745. (string-to-number (match-string 2 s))))
  16746. ((string-match "\\([0-9]+\\)" s)
  16747. (string-to-number (match-string 1 s)))
  16748. (t 0)))
  16749. (defcustom org-image-actual-width t
  16750. "Should we use the actual width of images when inlining them?
  16751. When set to t, always use the image width.
  16752. When set to a number, use imagemagick (when available) to set
  16753. the image's width to this value.
  16754. When set to a number in a list, try to get the width from any
  16755. #+ATTR.* keyword if it matches a width specification like
  16756. #+ATTR_HTML: :width 300px
  16757. and fall back on that number if none is found.
  16758. When set to nil, try to get the width from an #+ATTR.* keyword
  16759. and fall back on the original width if none is found.
  16760. This requires Emacs >= 24.1, build with imagemagick support."
  16761. :group 'org-appearance
  16762. :version "24.4"
  16763. :package-version '(Org . "8.0")
  16764. :type '(choice
  16765. (const :tag "Use the image width" t)
  16766. (integer :tag "Use a number of pixels")
  16767. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16768. (const :tag "Use #+ATTR* or don't resize" nil)))
  16769. (defcustom org-agenda-inhibit-startup nil
  16770. "Inhibit startup when preparing agenda buffers.
  16771. When this variable is t, the initialization of the Org agenda
  16772. buffers is inhibited: e.g. the visibility state is not set, the
  16773. tables are not re-aligned, etc."
  16774. :type 'boolean
  16775. :version "24.3"
  16776. :group 'org-agenda)
  16777. (define-obsolete-variable-alias
  16778. 'org-agenda-ignore-drawer-properties
  16779. 'org-agenda-ignore-properties "25.1")
  16780. (defcustom org-agenda-ignore-properties nil
  16781. "Avoid updating text properties when building the agenda.
  16782. Properties are used to prepare buffers for effort estimates,
  16783. appointments, statistics and subtree-local categories.
  16784. If you don't use these in the agenda, you can add them to this
  16785. list and agenda building will be a bit faster.
  16786. The value is a list, with zero or more of the symbols `effort', `appt',
  16787. `stats' or `category'."
  16788. :type '(set :greedy t
  16789. (const effort)
  16790. (const appt)
  16791. (const stats)
  16792. (const category))
  16793. :version "25.1"
  16794. :package-version '(Org . "8.3")
  16795. :group 'org-agenda)
  16796. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16797. "Convert a duration string S to minutes.
  16798. A bare number is interpreted as minutes, modifiers can be set by
  16799. customizing `org-effort-durations' (which see).
  16800. Entries containing a colon are interpreted as H:MM by
  16801. `org-hh:mm-string-to-minutes'."
  16802. (let ((result 0)
  16803. (re (concat "\\([0-9.]+\\) *\\("
  16804. (regexp-opt (mapcar 'car org-effort-durations))
  16805. "\\)")))
  16806. (while (string-match re s)
  16807. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16808. (string-to-number (match-string 1 s))))
  16809. (setq s (replace-match "" nil t s)))
  16810. (setq result (floor result))
  16811. (incf result (org-hh:mm-string-to-minutes s))
  16812. (if output-to-string (number-to-string result) result)))
  16813. ;;;; Files
  16814. (defun org-save-all-org-buffers ()
  16815. "Save all Org-mode buffers without user confirmation."
  16816. (interactive)
  16817. (message "Saving all Org-mode buffers...")
  16818. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16819. (when (featurep 'org-id) (org-id-locations-save))
  16820. (message "Saving all Org-mode buffers... done"))
  16821. (defun org-revert-all-org-buffers ()
  16822. "Revert all Org-mode buffers.
  16823. Prompt for confirmation when there are unsaved changes.
  16824. Be sure you know what you are doing before letting this function
  16825. overwrite your changes.
  16826. This function is useful in a setup where one tracks org files
  16827. with a version control system, to revert on one machine after pulling
  16828. changes from another. I believe the procedure must be like this:
  16829. 1. M-x org-save-all-org-buffers
  16830. 2. Pull changes from the other machine, resolve conflicts
  16831. 3. M-x org-revert-all-org-buffers"
  16832. (interactive)
  16833. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16834. (user-error "Abort"))
  16835. (save-excursion
  16836. (save-window-excursion
  16837. (mapc
  16838. (lambda (b)
  16839. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16840. (with-current-buffer b buffer-file-name))
  16841. (org-pop-to-buffer-same-window b)
  16842. (revert-buffer t 'no-confirm)))
  16843. (buffer-list))
  16844. (when (and (featurep 'org-id) org-id-track-globally)
  16845. (org-id-locations-load)))))
  16846. ;;;; Agenda files
  16847. ;;;###autoload
  16848. (defun org-switchb (&optional arg)
  16849. "Switch between Org buffers.
  16850. With one prefix argument, restrict available buffers to files.
  16851. With two prefix arguments, restrict available buffers to agenda files.
  16852. Defaults to `iswitchb' for buffer name completion.
  16853. Set `org-completion-use-ido' to make it use ido instead."
  16854. (interactive "P")
  16855. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16856. ((equal arg '(16)) (org-buffer-list 'agenda))
  16857. (t (org-buffer-list))))
  16858. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16859. (org-completion-use-ido org-completion-use-ido))
  16860. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16861. (setq org-completion-use-iswitchb t))
  16862. (org-pop-to-buffer-same-window
  16863. (org-icompleting-read "Org buffer: "
  16864. (mapcar 'list (mapcar 'buffer-name blist))
  16865. nil t))))
  16866. ;;; Define some older names previously used for this functionality
  16867. ;;;###autoload
  16868. (defalias 'org-ido-switchb 'org-switchb)
  16869. ;;;###autoload
  16870. (defalias 'org-iswitchb 'org-switchb)
  16871. (defun org-buffer-list (&optional predicate exclude-tmp)
  16872. "Return a list of Org buffers.
  16873. PREDICATE can be `export', `files' or `agenda'.
  16874. export restrict the list to Export buffers.
  16875. files restrict the list to buffers visiting Org files.
  16876. agenda restrict the list to buffers visiting agenda files.
  16877. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16878. (let* ((bfn nil)
  16879. (agenda-files (and (eq predicate 'agenda)
  16880. (mapcar 'file-truename (org-agenda-files t))))
  16881. (filter
  16882. (cond
  16883. ((eq predicate 'files)
  16884. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16885. ((eq predicate 'export)
  16886. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16887. ((eq predicate 'agenda)
  16888. (lambda (b)
  16889. (with-current-buffer b
  16890. (and (derived-mode-p 'org-mode)
  16891. (setq bfn (buffer-file-name b))
  16892. (member (file-truename bfn) agenda-files)))))
  16893. (t (lambda (b) (with-current-buffer b
  16894. (or (derived-mode-p 'org-mode)
  16895. (string-match "\*Org .*Export"
  16896. (buffer-name b)))))))))
  16897. (delq nil
  16898. (mapcar
  16899. (lambda(b)
  16900. (if (and (funcall filter b)
  16901. (or (not exclude-tmp)
  16902. (not (string-match "tmp" (buffer-name b)))))
  16903. b
  16904. nil))
  16905. (buffer-list)))))
  16906. (defun org-agenda-files (&optional unrestricted archives)
  16907. "Get the list of agenda files.
  16908. Optional UNRESTRICTED means return the full list even if a restriction
  16909. is currently in place.
  16910. When ARCHIVES is t, include all archive files that are really being
  16911. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16912. `org-agenda-archives-mode' is t."
  16913. (let ((files
  16914. (cond
  16915. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16916. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16917. ((listp org-agenda-files) org-agenda-files)
  16918. (t (error "Invalid value of `org-agenda-files'")))))
  16919. (setq files (apply 'append
  16920. (mapcar (lambda (f)
  16921. (if (file-directory-p f)
  16922. (directory-files
  16923. f t org-agenda-file-regexp)
  16924. (list f)))
  16925. files)))
  16926. (when org-agenda-skip-unavailable-files
  16927. (setq files (delq nil
  16928. (mapcar (function
  16929. (lambda (file)
  16930. (and (file-readable-p file) file)))
  16931. files))))
  16932. (when (or (eq archives t)
  16933. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16934. (setq files (org-add-archive-files files)))
  16935. files))
  16936. (defun org-agenda-file-p (&optional file)
  16937. "Return non-nil, if FILE is an agenda file.
  16938. If FILE is omitted, use the file associated with the current
  16939. buffer."
  16940. (let ((fname (or file (buffer-file-name))))
  16941. (and fname
  16942. (member (file-truename fname)
  16943. (mapcar #'file-truename (org-agenda-files t))))))
  16944. (defun org-edit-agenda-file-list ()
  16945. "Edit the list of agenda files.
  16946. Depending on setup, this either uses customize to edit the variable
  16947. `org-agenda-files', or it visits the file that is holding the list. In the
  16948. latter case, the buffer is set up in a way that saving it automatically kills
  16949. the buffer and restores the previous window configuration."
  16950. (interactive)
  16951. (if (stringp org-agenda-files)
  16952. (let ((cw (current-window-configuration)))
  16953. (find-file org-agenda-files)
  16954. (org-set-local 'org-window-configuration cw)
  16955. (org-add-hook 'after-save-hook
  16956. (lambda ()
  16957. (set-window-configuration
  16958. (prog1 org-window-configuration
  16959. (kill-buffer (current-buffer))))
  16960. (org-install-agenda-files-menu)
  16961. (message "New agenda file list installed"))
  16962. nil 'local)
  16963. (message "%s" (substitute-command-keys
  16964. "Edit list and finish with \\[save-buffer]")))
  16965. (customize-variable 'org-agenda-files)))
  16966. (defun org-store-new-agenda-file-list (list)
  16967. "Set new value for the agenda file list and save it correctly."
  16968. (if (stringp org-agenda-files)
  16969. (let ((fe (org-read-agenda-file-list t)) b u)
  16970. (while (setq b (find-buffer-visiting org-agenda-files))
  16971. (kill-buffer b))
  16972. (with-temp-file org-agenda-files
  16973. (insert
  16974. (mapconcat
  16975. (lambda (f) ;; Keep un-expanded entries.
  16976. (if (setq u (assoc f fe))
  16977. (cdr u)
  16978. f))
  16979. list "\n")
  16980. "\n")))
  16981. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16982. (org-insert-mode-line-in-empty-file nil))
  16983. (setq org-agenda-files list)
  16984. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16985. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16986. "Read the list of agenda files from a file.
  16987. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16988. filenames, used by `org-store-new-agenda-file-list' to write back
  16989. un-expanded file names."
  16990. (when (file-directory-p org-agenda-files)
  16991. (error "`org-agenda-files' cannot be a single directory"))
  16992. (when (stringp org-agenda-files)
  16993. (with-temp-buffer
  16994. (insert-file-contents org-agenda-files)
  16995. (mapcar
  16996. (lambda (f)
  16997. (let ((e (expand-file-name (substitute-in-file-name f)
  16998. org-directory)))
  16999. (if pair-with-expansion
  17000. (cons e f)
  17001. e)))
  17002. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17003. ;;;###autoload
  17004. (defun org-cycle-agenda-files ()
  17005. "Cycle through the files in `org-agenda-files'.
  17006. If the current buffer visits an agenda file, find the next one in the list.
  17007. If the current buffer does not, find the first agenda file."
  17008. (interactive)
  17009. (let* ((fs (or (org-agenda-files t)
  17010. (user-error "No agenda files")))
  17011. (files (copy-sequence fs))
  17012. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17013. file)
  17014. (when tcf
  17015. (while (and (setq file (pop files))
  17016. (not (equal (file-truename file) tcf)))))
  17017. (find-file (car (or files fs)))
  17018. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17019. (defun org-agenda-file-to-front (&optional to-end)
  17020. "Move/add the current file to the top of the agenda file list.
  17021. If the file is not present in the list, it is added to the front. If it is
  17022. present, it is moved there. With optional argument TO-END, add/move to the
  17023. end of the list."
  17024. (interactive "P")
  17025. (let ((org-agenda-skip-unavailable-files nil)
  17026. (file-alist (mapcar (lambda (x)
  17027. (cons (file-truename x) x))
  17028. (org-agenda-files t)))
  17029. (ctf (file-truename
  17030. (or buffer-file-name
  17031. (user-error "Please save the current buffer to a file"))))
  17032. x had)
  17033. (setq x (assoc ctf file-alist) had x)
  17034. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17035. (if to-end
  17036. (setq file-alist (append (delq x file-alist) (list x)))
  17037. (setq file-alist (cons x (delq x file-alist))))
  17038. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17039. (org-install-agenda-files-menu)
  17040. (message "File %s to %s of agenda file list"
  17041. (if had "moved" "added") (if to-end "end" "front"))))
  17042. (defun org-remove-file (&optional file)
  17043. "Remove current file from the list of files in variable `org-agenda-files'.
  17044. These are the files which are being checked for agenda entries.
  17045. Optional argument FILE means use this file instead of the current."
  17046. (interactive)
  17047. (let* ((org-agenda-skip-unavailable-files nil)
  17048. (file (or file buffer-file-name
  17049. (user-error "Current buffer does not visit a file")))
  17050. (true-file (file-truename file))
  17051. (afile (abbreviate-file-name file))
  17052. (files (delq nil (mapcar
  17053. (lambda (x)
  17054. (if (equal true-file
  17055. (file-truename x))
  17056. nil x))
  17057. (org-agenda-files t)))))
  17058. (if (not (= (length files) (length (org-agenda-files t))))
  17059. (progn
  17060. (org-store-new-agenda-file-list files)
  17061. (org-install-agenda-files-menu)
  17062. (message "Removed from Org Agenda list: %s" afile))
  17063. (message "File was not in list: %s (not removed)" afile))))
  17064. (defun org-file-menu-entry (file)
  17065. (vector file (list 'find-file file) t))
  17066. (defun org-check-agenda-file (file)
  17067. "Make sure FILE exists. If not, ask user what to do."
  17068. (when (not (file-exists-p file))
  17069. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17070. (abbreviate-file-name file))
  17071. (let ((r (downcase (read-char-exclusive))))
  17072. (cond
  17073. ((equal r ?r)
  17074. (org-remove-file file)
  17075. (throw 'nextfile t))
  17076. (t (user-error "Abort"))))))
  17077. (defun org-get-agenda-file-buffer (file)
  17078. "Get an agenda buffer visiting FILE.
  17079. If the buffer needs to be created, add it to the list of buffers
  17080. which might be released later."
  17081. (let ((buf (org-find-base-buffer-visiting file)))
  17082. (if buf
  17083. buf ; just return it
  17084. ;; Make a new buffer and remember it
  17085. (setq buf (find-file-noselect file))
  17086. (if buf (push buf org-agenda-new-buffers))
  17087. buf)))
  17088. (defun org-release-buffers (blist)
  17089. "Release all buffers in list, asking the user for confirmation when needed.
  17090. When a buffer is unmodified, it is just killed. When modified, it is saved
  17091. \(if the user agrees) and then killed."
  17092. (let (file)
  17093. (dolist (buf blist)
  17094. (setq file (buffer-file-name buf))
  17095. (when (and (buffer-modified-p buf)
  17096. file
  17097. (y-or-n-p (format "Save file %s? " file)))
  17098. (with-current-buffer buf (save-buffer)))
  17099. (kill-buffer buf))))
  17100. (defun org-agenda-prepare-buffers (files)
  17101. "Create buffers for all agenda files, protect archived trees and comments."
  17102. (interactive)
  17103. (let ((pa '(:org-archived t))
  17104. (pc '(:org-comment t))
  17105. (pall '(:org-archived t :org-comment t))
  17106. (inhibit-read-only t)
  17107. (org-inhibit-startup org-agenda-inhibit-startup)
  17108. (rea (concat ":" org-archive-tag ":"))
  17109. file re pos)
  17110. (setq org-tag-alist-for-agenda nil
  17111. org-tag-groups-alist-for-agenda nil)
  17112. (save-excursion
  17113. (save-restriction
  17114. (dolist (file files)
  17115. (catch 'nextfile
  17116. (if (bufferp file)
  17117. (set-buffer file)
  17118. (org-check-agenda-file file)
  17119. (set-buffer (org-get-agenda-file-buffer file)))
  17120. (widen)
  17121. (org-set-regexps-and-options 'tags-only)
  17122. (setq pos (point))
  17123. (or (memq 'category org-agenda-ignore-properties)
  17124. (org-refresh-category-properties))
  17125. (or (memq 'stats org-agenda-ignore-properties)
  17126. (org-refresh-stats-properties))
  17127. (or (memq 'effort org-agenda-ignore-properties)
  17128. (org-refresh-effort-properties))
  17129. (or (memq 'appt org-agenda-ignore-properties)
  17130. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17131. (setq org-todo-keywords-for-agenda
  17132. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17133. (setq org-done-keywords-for-agenda
  17134. (append org-done-keywords-for-agenda org-done-keywords))
  17135. (setq org-todo-keyword-alist-for-agenda
  17136. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17137. (setq org-tag-alist-for-agenda
  17138. (org-uniquify
  17139. (append org-tag-alist-for-agenda
  17140. org-tag-alist
  17141. org-tag-persistent-alist)))
  17142. (if org-group-tags
  17143. (setq org-tag-groups-alist-for-agenda
  17144. (org-uniquify-alist
  17145. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17146. (org-with-silent-modifications
  17147. (save-excursion
  17148. (remove-text-properties (point-min) (point-max) pall)
  17149. (when org-agenda-skip-archived-trees
  17150. (goto-char (point-min))
  17151. (while (re-search-forward rea nil t)
  17152. (if (org-at-heading-p t)
  17153. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17154. (goto-char (point-min))
  17155. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17156. (while (re-search-forward re nil t)
  17157. (when (save-match-data (org-in-commented-heading-p t))
  17158. (add-text-properties
  17159. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17160. (goto-char pos)))))
  17161. (setq org-todo-keywords-for-agenda
  17162. (org-uniquify org-todo-keywords-for-agenda))
  17163. (setq org-todo-keyword-alist-for-agenda
  17164. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17165. ;;;; CDLaTeX minor mode
  17166. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17167. "Keymap for the minor `org-cdlatex-mode'.")
  17168. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17169. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17170. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17171. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17172. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17173. (defvar org-cdlatex-texmathp-advice-is-done nil
  17174. "Flag remembering if we have applied the advice to texmathp already.")
  17175. (define-minor-mode org-cdlatex-mode
  17176. "Toggle the minor `org-cdlatex-mode'.
  17177. This mode supports entering LaTeX environment and math in LaTeX fragments
  17178. in Org-mode.
  17179. \\{org-cdlatex-mode-map}"
  17180. nil " OCDL" nil
  17181. (when org-cdlatex-mode
  17182. (require 'cdlatex)
  17183. (run-hooks 'cdlatex-mode-hook)
  17184. (cdlatex-compute-tables))
  17185. (unless org-cdlatex-texmathp-advice-is-done
  17186. (setq org-cdlatex-texmathp-advice-is-done t)
  17187. (defadvice texmathp (around org-math-always-on activate)
  17188. "Always return t in org-mode buffers.
  17189. This is because we want to insert math symbols without dollars even outside
  17190. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17191. an embedded LaTeX fragment, let texmathp do its job.
  17192. \\[org-cdlatex-mode-map]"
  17193. (interactive)
  17194. (let (p)
  17195. (cond
  17196. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17197. ((eq this-command 'cdlatex-math-symbol)
  17198. (setq ad-return-value t
  17199. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17200. (t
  17201. (let ((p (org-inside-LaTeX-fragment-p)))
  17202. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17203. (setq ad-return-value t
  17204. texmathp-why '("Org-mode embedded math" . 0))
  17205. (if p ad-do-it)))))))))
  17206. (defun turn-on-org-cdlatex ()
  17207. "Unconditionally turn on `org-cdlatex-mode'."
  17208. (org-cdlatex-mode 1))
  17209. (defun org-try-cdlatex-tab ()
  17210. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17211. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17212. - inside a LaTeX fragment, or
  17213. - after the first word in a line, where an abbreviation expansion could
  17214. insert a LaTeX environment."
  17215. (when org-cdlatex-mode
  17216. (cond
  17217. ;; Before any word on the line: No expansion possible.
  17218. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17219. ;; Just after first word on the line: Expand it. Make sure it
  17220. ;; cannot happen on headlines, though.
  17221. ((save-excursion
  17222. (skip-chars-backward "a-zA-Z0-9*")
  17223. (skip-chars-backward " \t")
  17224. (and (bolp) (not (org-at-heading-p))))
  17225. (cdlatex-tab) t)
  17226. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17227. (defun org-cdlatex-underscore-caret (&optional arg)
  17228. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17229. Revert to the normal definition outside of these fragments."
  17230. (interactive "P")
  17231. (if (org-inside-LaTeX-fragment-p)
  17232. (call-interactively 'cdlatex-sub-superscript)
  17233. (let (org-cdlatex-mode)
  17234. (call-interactively (key-binding (vector last-input-event))))))
  17235. (defun org-cdlatex-math-modify (&optional arg)
  17236. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17237. Revert to the normal definition outside of these fragments."
  17238. (interactive "P")
  17239. (if (org-inside-LaTeX-fragment-p)
  17240. (call-interactively 'cdlatex-math-modify)
  17241. (let (org-cdlatex-mode)
  17242. (call-interactively (key-binding (vector last-input-event))))))
  17243. (defun org-cdlatex-environment-indent (&optional environment item)
  17244. "Execute `cdlatex-environment' and indent the inserted environment.
  17245. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17246. The inserted environment is indented to current indentation
  17247. unless point is at the beginning of the line, in which the
  17248. environment remains unintended."
  17249. (interactive)
  17250. ;; cdlatex-environment always return nil. Therefore, capture output
  17251. ;; first and determine if an environment was selected.
  17252. (let* ((beg (point-marker))
  17253. (end (copy-marker (point) t))
  17254. (inserted (progn
  17255. (ignore-errors (cdlatex-environment environment item))
  17256. (< beg end)))
  17257. ;; Figure out how many lines to move forward after the
  17258. ;; environment has been inserted.
  17259. (lines (when inserted
  17260. (save-excursion
  17261. (- (loop while (< beg (point))
  17262. with x = 0
  17263. do (forward-line -1)
  17264. (incf x)
  17265. finally return x)
  17266. (if (progn (goto-char beg)
  17267. (and (progn (skip-chars-forward " \t") (eolp))
  17268. (progn (skip-chars-backward " \t") (bolp))))
  17269. 1 0)))))
  17270. (env (org-trim (delete-and-extract-region beg end))))
  17271. (when inserted
  17272. ;; Get indentation of next line unless at column 0.
  17273. (let ((ind (if (bolp) 0
  17274. (save-excursion
  17275. (org-return-indent)
  17276. (prog1 (org-get-indentation)
  17277. (when (progn (skip-chars-forward " \t") (eolp))
  17278. (delete-region beg (point)))))))
  17279. (bol (progn (skip-chars-backward " \t") (bolp))))
  17280. ;; Insert a newline before environment unless at column zero
  17281. ;; to "escape" the current line. Insert a newline if
  17282. ;; something is one the same line as \end{ENVIRONMENT}.
  17283. (insert
  17284. (concat (unless bol "\n") env
  17285. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17286. (unless (zerop ind)
  17287. (save-excursion
  17288. (goto-char beg)
  17289. (while (< (point) end)
  17290. (unless (eolp) (org-indent-line-to ind))
  17291. (forward-line))))
  17292. (goto-char beg)
  17293. (forward-line lines)
  17294. (org-indent-line-to ind)))
  17295. (set-marker beg nil)
  17296. (set-marker end nil)))
  17297. ;;;; LaTeX fragments
  17298. (defun org-inside-LaTeX-fragment-p ()
  17299. "Test if point is inside a LaTeX fragment.
  17300. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17301. sequence appearing also before point.
  17302. Even though the matchers for math are configurable, this function assumes
  17303. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17304. delimiters are skipped when they have been removed by customization.
  17305. The return value is nil, or a cons cell with the delimiter and the
  17306. position of this delimiter.
  17307. This function does a reasonably good job, but can locally be fooled by
  17308. for example currency specifications. For example it will assume being in
  17309. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17310. fragments that are properly closed, but during editing, we have to live
  17311. with the uncertainty caused by missing closing delimiters. This function
  17312. looks only before point, not after."
  17313. (catch 'exit
  17314. (let ((pos (point))
  17315. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17316. (lim (progn
  17317. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17318. (point)))
  17319. dd-on str (start 0) m re)
  17320. (goto-char pos)
  17321. (when dodollar
  17322. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17323. re (nth 1 (assoc "$" org-latex-regexps)))
  17324. (while (string-match re str start)
  17325. (cond
  17326. ((= (match-end 0) (length str))
  17327. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17328. ((= (match-end 0) (- (length str) 5))
  17329. (throw 'exit nil))
  17330. (t (setq start (match-end 0))))))
  17331. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17332. (goto-char pos)
  17333. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17334. (and (match-beginning 2) (throw 'exit nil))
  17335. ;; count $$
  17336. (while (re-search-backward "\\$\\$" lim t)
  17337. (setq dd-on (not dd-on)))
  17338. (goto-char pos)
  17339. (if dd-on (cons "$$" m))))))
  17340. (defun org-inside-latex-macro-p ()
  17341. "Is point inside a LaTeX macro or its arguments?"
  17342. (save-match-data
  17343. (org-in-regexp
  17344. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17345. (defvar org-latex-fragment-image-overlays nil
  17346. "List of overlays carrying the images of latex fragments.")
  17347. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17348. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17349. "Remove all overlays with LaTeX fragment images in current buffer.
  17350. When optional arguments BEG and END are non-nil, remove all
  17351. overlays between them instead. Return t when some overlays were
  17352. removed, nil otherwise."
  17353. (let (removedp)
  17354. (setq org-latex-fragment-image-overlays
  17355. (let ((beg (or beg (point-min)))
  17356. (end (or end (point-max))))
  17357. (org-remove-if
  17358. (lambda (o)
  17359. (and (>= (overlay-start o) beg)
  17360. (<= (overlay-end o) end)
  17361. (progn (delete-overlay o)
  17362. (or removedp (setq removedp t)))))
  17363. org-latex-fragment-image-overlays)))
  17364. removedp))
  17365. (define-obsolete-function-alias
  17366. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17367. (defun org-toggle-latex-fragment (&optional arg)
  17368. "Preview the LaTeX fragment at point, or all locally or globally.
  17369. If the cursor is on a LaTeX fragment, create the image and overlay
  17370. it over the source code, if there is none. Remove it otherwise.
  17371. If there is no fragment at point, display all fragments in the
  17372. current section.
  17373. With prefix ARG, preview or clear image for all fragments in the
  17374. current subtree or in the whole buffer when used before the first
  17375. headline. With a double prefix ARG \\[universal-argument] \
  17376. \\[universal-argument] preview or clear images
  17377. for all fragments in the buffer."
  17378. (interactive "P")
  17379. (unless (buffer-file-name (buffer-base-buffer))
  17380. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17381. (when (display-graphic-p)
  17382. (catch 'exit
  17383. (save-excursion
  17384. (let ((window-start (window-start)) msg)
  17385. (save-restriction
  17386. (cond
  17387. ((or (equal arg '(16))
  17388. (and (equal arg '(4))
  17389. (org-with-limited-levels (org-before-first-heading-p))))
  17390. (if (org-remove-latex-fragment-image-overlays)
  17391. (progn (message "LaTeX fragments images removed from buffer")
  17392. (throw 'exit nil))
  17393. (setq msg "Creating images for buffer...")))
  17394. ((equal arg '(4))
  17395. (org-with-limited-levels (org-back-to-heading t))
  17396. (let ((beg (point))
  17397. (end (progn (org-end-of-subtree t) (point))))
  17398. (if (org-remove-latex-fragment-image-overlays beg end)
  17399. (progn
  17400. (message "LaTeX fragment images removed from subtree")
  17401. (throw 'exit nil))
  17402. (setq msg "Creating images for subtree...")
  17403. (narrow-to-region beg end))))
  17404. ((let ((datum (org-element-context)))
  17405. (when (memq (org-element-type datum)
  17406. '(latex-environment latex-fragment))
  17407. (let* ((beg (org-element-property :begin datum))
  17408. (end (org-element-property :end datum)))
  17409. (if (org-remove-latex-fragment-image-overlays beg end)
  17410. (progn (message "LaTeX fragment image removed")
  17411. (throw 'exit nil))
  17412. (narrow-to-region beg end)
  17413. (setq msg "Creating image..."))))))
  17414. (t
  17415. (org-with-limited-levels
  17416. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17417. (outline-previous-heading)
  17418. (point)))
  17419. (end (progn (outline-next-heading) (point))))
  17420. (if (org-remove-latex-fragment-image-overlays beg end)
  17421. (progn
  17422. (message "LaTeX fragment images removed from section")
  17423. (throw 'exit nil))
  17424. (setq msg "Creating images for section...")
  17425. (narrow-to-region beg end))))))
  17426. (let ((file (buffer-file-name (buffer-base-buffer))))
  17427. (org-format-latex
  17428. (concat org-latex-preview-ltxpng-directory
  17429. (file-name-sans-extension (file-name-nondirectory file)))
  17430. ;; Emacs cannot overlay images from remote hosts.
  17431. ;; Create it in `temporary-file-directory' instead.
  17432. (if (file-remote-p file) temporary-file-directory
  17433. default-directory)
  17434. 'overlays msg 'forbuffer
  17435. org-latex-create-formula-image-program)))
  17436. ;; Work around a bug that doesn't restore window's start
  17437. ;; when widening back the buffer.
  17438. (set-window-start nil window-start)
  17439. (message (concat msg "done")))))))
  17440. (defun org-format-latex
  17441. (prefix &optional dir overlays msg forbuffer processing-type)
  17442. "Replace LaTeX fragments with links to an image, and produce images.
  17443. When optional argument OVERLAYS is non-nil, display the image on
  17444. top of the fragment instead of replacing it.
  17445. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17446. Some of the options can be changed using the variable
  17447. `org-format-latex-options', which see."
  17448. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17449. (unless (eq processing-type 'verbatim)
  17450. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17451. (cnt 0)
  17452. checkdir-flag)
  17453. (goto-char (point-min))
  17454. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17455. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17456. (overlay-recenter (point-max)))
  17457. (while (re-search-forward math-regexp nil t)
  17458. (unless (and overlays
  17459. (eq (get-char-property (point) 'org-overlay-type)
  17460. 'org-latex-overlay))
  17461. (let* ((context (org-element-context))
  17462. (type (org-element-type context)))
  17463. (when (memq type '(latex-environment latex-fragment))
  17464. (let ((block-type (eq type 'latex-environment))
  17465. (value (org-element-property :value context))
  17466. (beg (org-element-property :begin context))
  17467. (end (save-excursion
  17468. (goto-char (org-element-property :end context))
  17469. (skip-chars-backward " \r\t\n")
  17470. (point))))
  17471. (case processing-type
  17472. (mathjax
  17473. ;; Prepare for MathJax processing.
  17474. (if (eq (char-after beg) ?$)
  17475. (save-excursion
  17476. (delete-region beg end)
  17477. (insert "\\(" (substring value 1 -1) "\\)"))
  17478. (goto-char end)))
  17479. ((dvipng imagemagick)
  17480. ;; Process to an image.
  17481. (incf cnt)
  17482. (goto-char beg)
  17483. (let* ((face (face-at-point))
  17484. ;; Get the colors from the face at point.
  17485. (fg
  17486. (let ((color (plist-get org-format-latex-options
  17487. :foreground)))
  17488. (if (and forbuffer (eq color 'auto))
  17489. (face-attribute face :foreground nil 'default)
  17490. color)))
  17491. (bg
  17492. (let ((color (plist-get org-format-latex-options
  17493. :background)))
  17494. (if (and forbuffer (eq color 'auto))
  17495. (face-attribute face :background nil 'default)
  17496. color)))
  17497. (hash (sha1 (prin1-to-string
  17498. (list org-format-latex-header
  17499. org-latex-default-packages-alist
  17500. org-latex-packages-alist
  17501. org-format-latex-options
  17502. forbuffer value fg bg))))
  17503. (absprefix (expand-file-name prefix dir))
  17504. (linkfile (format "%s_%s.png" prefix hash))
  17505. (movefile (format "%s_%s.png" absprefix hash))
  17506. (sep (and block-type "\n\n"))
  17507. (link (concat sep "[[file:" linkfile "]]" sep))
  17508. (options
  17509. (org-combine-plists
  17510. org-format-latex-options
  17511. `(:foreground ,fg :background ,bg))))
  17512. (when msg (message msg cnt))
  17513. (unless checkdir-flag ; Ensure the directory exists.
  17514. (setq checkdir-flag t)
  17515. (let ((todir (file-name-directory absprefix)))
  17516. (unless (file-directory-p todir)
  17517. (make-directory todir t))))
  17518. (unless (file-exists-p movefile)
  17519. (org-create-formula-image
  17520. value movefile options forbuffer processing-type))
  17521. (if overlays
  17522. (progn
  17523. (dolist (o (overlays-in beg end))
  17524. (when (eq (overlay-get o 'org-overlay-type)
  17525. 'org-latex-overlay)
  17526. (delete-overlay o)))
  17527. (let ((ov (make-overlay beg end)))
  17528. (overlay-put ov
  17529. 'org-overlay-type
  17530. 'org-latex-overlay)
  17531. (overlay-put ov 'evaporate t)
  17532. (if (featurep 'xemacs)
  17533. (progn
  17534. (overlay-put ov 'invisible t)
  17535. (overlay-put
  17536. ov 'end-glyph
  17537. (make-glyph
  17538. (vector 'png :file movefile))))
  17539. (overlay-put
  17540. ov 'display
  17541. (list 'image
  17542. :type 'png
  17543. :file movefile
  17544. :ascent 'center)))
  17545. (push ov org-latex-fragment-image-overlays))
  17546. (goto-char end))
  17547. (delete-region beg end)
  17548. (insert
  17549. (org-add-props link
  17550. (list 'org-latex-src
  17551. (replace-regexp-in-string "\"" "" value)
  17552. 'org-latex-src-embed-type
  17553. (if block-type 'paragraph 'character)))))))
  17554. (mathml
  17555. ;; Process to MathML.
  17556. (unless (org-format-latex-mathml-available-p)
  17557. (user-error "LaTeX to MathML converter not configured"))
  17558. (incf cnt)
  17559. (when msg (message msg cnt))
  17560. (goto-char beg)
  17561. (delete-region beg end)
  17562. (insert (org-format-latex-as-mathml
  17563. value block-type prefix dir)))
  17564. (otherwise
  17565. (error "Unknown conversion type %s for LaTeX fragments"
  17566. processing-type)))))))))))
  17567. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17568. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17569. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17570. sucessful, return the portion between \"<math...> </math>\"
  17571. elements otherwise return nil. When MATHML-FILE is specified,
  17572. write the results in to that file. When invoked as an
  17573. interactive command, prompt for LATEX-FRAG, with initial value
  17574. set to the current active region and echo the results for user
  17575. inspection."
  17576. (interactive (list (let ((frag (when (org-region-active-p)
  17577. (buffer-substring-no-properties
  17578. (region-beginning) (region-end)))))
  17579. (read-string "LaTeX Fragment: " frag nil frag))))
  17580. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17581. (let* ((tmp-in-file (file-relative-name
  17582. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17583. (ignore (write-region latex-frag nil tmp-in-file))
  17584. (tmp-out-file (file-relative-name
  17585. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17586. (cmd (format-spec
  17587. org-latex-to-mathml-convert-command
  17588. `((?j . ,(and org-latex-to-mathml-jar-file
  17589. (shell-quote-argument
  17590. (expand-file-name
  17591. org-latex-to-mathml-jar-file))))
  17592. (?I . ,(shell-quote-argument tmp-in-file))
  17593. (?i . ,latex-frag)
  17594. (?o . ,(shell-quote-argument tmp-out-file)))))
  17595. mathml shell-command-output)
  17596. (when (org-called-interactively-p 'any)
  17597. (unless (org-format-latex-mathml-available-p)
  17598. (user-error "LaTeX to MathML converter not configured")))
  17599. (message "Running %s" cmd)
  17600. (setq shell-command-output (shell-command-to-string cmd))
  17601. (setq mathml
  17602. (when (file-readable-p tmp-out-file)
  17603. (with-current-buffer (find-file-noselect tmp-out-file t)
  17604. (goto-char (point-min))
  17605. (when (re-search-forward
  17606. (concat
  17607. (regexp-quote
  17608. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17609. "[^>]*?>"
  17610. "\\(.\\|\n\\)*"
  17611. "</math>")
  17612. nil t)
  17613. (prog1 (match-string 0) (kill-buffer))))))
  17614. (cond
  17615. (mathml
  17616. (setq mathml
  17617. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17618. (when mathml-file
  17619. (write-region mathml nil mathml-file))
  17620. (when (org-called-interactively-p 'any)
  17621. (message mathml)))
  17622. ((message "LaTeX to MathML conversion failed")
  17623. (message shell-command-output)))
  17624. (delete-file tmp-in-file)
  17625. (when (file-exists-p tmp-out-file)
  17626. (delete-file tmp-out-file))
  17627. mathml))
  17628. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17629. prefix &optional dir)
  17630. "Use `org-create-math-formula' but check local cache first."
  17631. (let* ((absprefix (expand-file-name prefix dir))
  17632. (print-length nil) (print-level nil)
  17633. (formula-id (concat
  17634. "formula-"
  17635. (sha1
  17636. (prin1-to-string
  17637. (list latex-frag
  17638. org-latex-to-mathml-convert-command)))))
  17639. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17640. (formula-cache-dir (file-name-directory formula-cache)))
  17641. (unless (file-directory-p formula-cache-dir)
  17642. (make-directory formula-cache-dir t))
  17643. (unless (file-exists-p formula-cache)
  17644. (org-create-math-formula latex-frag formula-cache))
  17645. (if (file-exists-p formula-cache)
  17646. ;; Successful conversion. Return the link to MathML file.
  17647. (org-add-props
  17648. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17649. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17650. 'org-latex-src-embed-type (if latex-frag-type
  17651. 'paragraph 'character)))
  17652. ;; Failed conversion. Return the LaTeX fragment verbatim
  17653. latex-frag)))
  17654. (defun org-create-formula-image (string tofile options buffer &optional type)
  17655. "Create an image from LaTeX source using dvipng or convert.
  17656. This function calls either `org-create-formula-image-with-dvipng'
  17657. or `org-create-formula-image-with-imagemagick' depending on the
  17658. value of `org-latex-create-formula-image-program' or on the value
  17659. of the optional TYPE variable.
  17660. Note: ultimately these two function should be combined as they
  17661. share a good deal of logic."
  17662. (org-check-external-command
  17663. "latex" "needed to convert LaTeX fragments to images")
  17664. (funcall
  17665. (case (or type org-latex-create-formula-image-program)
  17666. (dvipng
  17667. (org-check-external-command
  17668. "dvipng" "needed to convert LaTeX fragments to images")
  17669. #'org-create-formula-image-with-dvipng)
  17670. (imagemagick
  17671. (org-check-external-command
  17672. "convert" "you need to install imagemagick")
  17673. #'org-create-formula-image-with-imagemagick)
  17674. (t (error
  17675. "Invalid value of `org-latex-create-formula-image-program'")))
  17676. string tofile options buffer))
  17677. (declare-function org-export-get-backend "ox" (name))
  17678. (declare-function org-export--get-global-options "ox" (&optional backend))
  17679. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17680. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17681. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17682. (defun org-create-formula--latex-header ()
  17683. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17684. (let ((info (org-combine-plists (org-export--get-global-options
  17685. (org-export-get-backend 'latex))
  17686. (org-export--get-inbuffer-options
  17687. (org-export-get-backend 'latex)))))
  17688. (org-latex-guess-babel-language
  17689. (org-latex-guess-inputenc
  17690. (org-splice-latex-header
  17691. org-format-latex-header
  17692. org-latex-default-packages-alist
  17693. org-latex-packages-alist t
  17694. (plist-get info :latex-header)))
  17695. info)))
  17696. (defun org--get-display-dpi ()
  17697. "Get the DPI of the display.
  17698. Assumes that the display has the same pixel width in the
  17699. horizontal and vertical directions."
  17700. (if (display-graphic-p)
  17701. (round (/ (display-pixel-height)
  17702. (/ (display-mm-height) 25.4)))
  17703. (error "Attempt to calculate the dpi of a non-graphic display")))
  17704. ;; This function borrows from Ganesh Swami's latex2png.el
  17705. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17706. "This calls dvipng."
  17707. (require 'ox-latex)
  17708. (let* ((tmpdir (if (featurep 'xemacs)
  17709. (temp-directory)
  17710. temporary-file-directory))
  17711. (texfilebase (make-temp-name
  17712. (expand-file-name "orgtex" tmpdir)))
  17713. (texfile (concat texfilebase ".tex"))
  17714. (dvifile (concat texfilebase ".dvi"))
  17715. (pngfile (concat texfilebase ".png"))
  17716. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17717. ;; This assumes that the display has the same pixel width in
  17718. ;; the horizontal and vertical directions
  17719. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17720. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17721. "Black"))
  17722. (bg (or (plist-get options (if buffer :background :html-background))
  17723. "Transparent")))
  17724. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17725. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17726. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17727. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17728. (let ((latex-header (org-create-formula--latex-header)))
  17729. (with-temp-file texfile
  17730. (insert latex-header)
  17731. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17732. (let ((dir default-directory))
  17733. (ignore-errors
  17734. (cd tmpdir)
  17735. (call-process "latex" nil nil nil texfile))
  17736. (cd dir))
  17737. (if (not (file-exists-p dvifile))
  17738. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17739. (ignore-errors
  17740. (if (featurep 'xemacs)
  17741. (call-process "dvipng" nil nil nil
  17742. "-fg" fg "-bg" bg
  17743. "-T" "tight"
  17744. "-o" pngfile
  17745. dvifile)
  17746. (call-process "dvipng" nil nil nil
  17747. "-fg" fg "-bg" bg
  17748. "-D" dpi
  17749. ;;"-x" scale "-y" scale
  17750. "-T" "tight"
  17751. "-o" pngfile
  17752. dvifile)))
  17753. (if (not (file-exists-p pngfile))
  17754. (if org-format-latex-signal-error
  17755. (error "Failed to create png file from %s" texfile)
  17756. (message "Failed to create png file from %s" texfile)
  17757. nil)
  17758. ;; Use the requested file name and clean up
  17759. (copy-file pngfile tofile 'replace)
  17760. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17761. (if (file-exists-p (concat texfilebase e))
  17762. (delete-file (concat texfilebase e))))
  17763. pngfile))))
  17764. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17765. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17766. "This calls convert, which is included into imagemagick."
  17767. (require 'ox-latex)
  17768. (let* ((tmpdir (if (featurep 'xemacs)
  17769. (temp-directory)
  17770. temporary-file-directory))
  17771. (texfilebase (make-temp-name
  17772. (expand-file-name "orgtex" tmpdir)))
  17773. (texfile (concat texfilebase ".tex"))
  17774. (pdffile (concat texfilebase ".pdf"))
  17775. (pngfile (concat texfilebase ".png"))
  17776. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17777. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17778. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17779. "black"))
  17780. (bg (or (plist-get options (if buffer :background :html-background))
  17781. "white")))
  17782. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17783. (setq fg (org-latex-color-format fg)))
  17784. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17785. (setq bg (org-latex-color-format
  17786. (if (string= bg "Transparent") "white" bg))))
  17787. (let ((latex-header (org-create-formula--latex-header)))
  17788. (with-temp-file texfile
  17789. (insert latex-header)
  17790. (insert "\n\\begin{document}\n"
  17791. "\\definecolor{fg}{rgb}{" fg "}\n"
  17792. "\\definecolor{bg}{rgb}{" bg "}\n"
  17793. "\n\\pagecolor{bg}\n"
  17794. "\n{\\color{fg}\n"
  17795. string
  17796. "\n}\n"
  17797. "\n\\end{document}\n")))
  17798. (org-latex-compile texfile t)
  17799. (if (not (file-exists-p pdffile))
  17800. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17801. (ignore-errors
  17802. (if (featurep 'xemacs)
  17803. (call-process "convert" nil nil nil
  17804. "-density" "96"
  17805. "-trim"
  17806. "-antialias"
  17807. pdffile
  17808. "-quality" "100"
  17809. ;; "-sharpen" "0x1.0"
  17810. pngfile)
  17811. (call-process "convert" nil nil nil
  17812. "-density" dpi
  17813. "-trim"
  17814. "-antialias"
  17815. pdffile
  17816. "-quality" "100"
  17817. ;; "-sharpen" "0x1.0"
  17818. pngfile)))
  17819. (if (not (file-exists-p pngfile))
  17820. (if org-format-latex-signal-error
  17821. (error "Failed to create png file from %s" texfile)
  17822. (message "Failed to create png file from %s" texfile)
  17823. nil)
  17824. ;; Use the requested file name and clean up
  17825. (copy-file pngfile tofile 'replace)
  17826. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17827. (if (file-exists-p (concat texfilebase e))
  17828. (delete-file (concat texfilebase e))))
  17829. pngfile))))
  17830. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17831. "Fill a LaTeX header template TPL.
  17832. In the template, the following place holders will be recognized:
  17833. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17834. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17835. [PACKAGES] \\usepackage statements for PKG
  17836. [NO-PACKAGES] do not include PKG
  17837. [EXTRA] the string EXTRA
  17838. [NO-EXTRA] do not include EXTRA
  17839. For backward compatibility, if both the positive and the negative place
  17840. holder is missing, the positive one (without the \"NO-\") will be
  17841. assumed to be present at the end of the template.
  17842. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17843. EXTRA is a string.
  17844. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17845. (let (rpl (end ""))
  17846. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17847. (setq rpl (if (or (match-end 1) (not def-pkg))
  17848. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17849. tpl (replace-match rpl t t tpl))
  17850. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17851. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17852. (setq rpl (if (or (match-end 1) (not pkg))
  17853. "" (org-latex-packages-to-string pkg snippets-p t))
  17854. tpl (replace-match rpl t t tpl))
  17855. (if pkg (setq end
  17856. (concat end "\n"
  17857. (org-latex-packages-to-string pkg snippets-p)))))
  17858. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17859. (setq rpl (if (or (match-end 1) (not extra))
  17860. "" (concat extra "\n"))
  17861. tpl (replace-match rpl t t tpl))
  17862. (if (and extra (string-match "\\S-" extra))
  17863. (setq end (concat end "\n" extra))))
  17864. (if (string-match "\\S-" end)
  17865. (concat tpl "\n" end)
  17866. tpl)))
  17867. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17868. "Turn an alist of packages into a string with the \\usepackage macros."
  17869. (setq pkg (mapconcat (lambda(p)
  17870. (cond
  17871. ((stringp p) p)
  17872. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17873. (format "%% Package %s omitted" (cadr p)))
  17874. ((equal "" (car p))
  17875. (format "\\usepackage{%s}" (cadr p)))
  17876. (t
  17877. (format "\\usepackage[%s]{%s}"
  17878. (car p) (cadr p)))))
  17879. pkg
  17880. "\n"))
  17881. (if newline (concat pkg "\n") pkg))
  17882. (defun org-dvipng-color (attr)
  17883. "Return a RGB color specification for dvipng."
  17884. (apply 'format "rgb %s %s %s"
  17885. (mapcar 'org-normalize-color
  17886. (if (featurep 'xemacs)
  17887. (color-rgb-components
  17888. (face-property 'default
  17889. (cond ((eq attr :foreground) 'foreground)
  17890. ((eq attr :background) 'background))))
  17891. (color-values (face-attribute 'default attr nil))))))
  17892. (defun org-dvipng-color-format (color-name)
  17893. "Convert COLOR-NAME to a RGB color value for dvipng."
  17894. (apply 'format "rgb %s %s %s"
  17895. (mapcar 'org-normalize-color
  17896. (color-values color-name))))
  17897. (defun org-latex-color (attr)
  17898. "Return a RGB color for the LaTeX color package."
  17899. (apply 'format "%s,%s,%s"
  17900. (mapcar 'org-normalize-color
  17901. (if (featurep 'xemacs)
  17902. (color-rgb-components
  17903. (face-property 'default
  17904. (cond ((eq attr :foreground) 'foreground)
  17905. ((eq attr :background) 'background))))
  17906. (color-values (face-attribute 'default attr nil))))))
  17907. (defun org-latex-color-format (color-name)
  17908. "Convert COLOR-NAME to a RGB color value."
  17909. (apply 'format "%s,%s,%s"
  17910. (mapcar 'org-normalize-color
  17911. (color-values color-name))))
  17912. (defun org-normalize-color (value)
  17913. "Return string to be used as color value for an RGB component."
  17914. (format "%g" (/ value 65535.0)))
  17915. ;; Image display
  17916. (defvar org-inline-image-overlays nil)
  17917. (make-variable-buffer-local 'org-inline-image-overlays)
  17918. (defun org-toggle-inline-images (&optional include-linked)
  17919. "Toggle the display of inline images.
  17920. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17921. (interactive "P")
  17922. (if org-inline-image-overlays
  17923. (progn
  17924. (org-remove-inline-images)
  17925. (when (org-called-interactively-p 'interactive)
  17926. (message "Inline image display turned off")))
  17927. (org-display-inline-images include-linked)
  17928. (when (org-called-interactively-p 'interactive)
  17929. (message (if org-inline-image-overlays
  17930. (format "%d images displayed inline"
  17931. (length org-inline-image-overlays))
  17932. "No images to display inline")))))
  17933. (defun org-redisplay-inline-images ()
  17934. "Refresh the display of inline images."
  17935. (interactive)
  17936. (if (not org-inline-image-overlays)
  17937. (org-toggle-inline-images)
  17938. (org-toggle-inline-images)
  17939. (org-toggle-inline-images)))
  17940. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17941. "Display inline images.
  17942. An inline image is a link which follows either of these
  17943. conventions:
  17944. 1. Its path is a file with an extension matching return value
  17945. from `image-file-name-regexp' and it has no contents.
  17946. 2. Its description consists in a single link of the previous
  17947. type.
  17948. When optional argument INCLUDE-LINKED is non-nil, also links with
  17949. a text description part will be inlined. This can be nice for
  17950. a quick look at those images, but it does not reflect what
  17951. exported files will look like.
  17952. When optional argument REFRESH is non-nil, refresh existing
  17953. images between BEG and END. This will create new image displays
  17954. only if necessary. BEG and END default to the buffer
  17955. boundaries."
  17956. (interactive "P")
  17957. (when (display-graphic-p)
  17958. (unless refresh
  17959. (org-remove-inline-images)
  17960. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17961. (org-with-wide-buffer
  17962. (goto-char (or beg (point-min)))
  17963. (let ((case-fold-search t)
  17964. (file-extension-re (org-image-file-name-regexp)))
  17965. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17966. (let ((link (save-match-data (org-element-context))))
  17967. ;; Check if we're at an inline image.
  17968. (when (and (equal (org-element-property :type link) "file")
  17969. (or include-linked
  17970. (not (org-element-property :contents-begin link)))
  17971. (let ((parent (org-element-property :parent link)))
  17972. (or (not (eq (org-element-type parent) 'link))
  17973. (not (cdr (org-element-contents parent)))))
  17974. (org-string-match-p file-extension-re
  17975. (org-element-property :path link)))
  17976. (let ((file (expand-file-name
  17977. (org-link-unescape
  17978. (org-element-property :path link)))))
  17979. (when (file-exists-p file)
  17980. (let ((width
  17981. ;; Apply `org-image-actual-width' specifications.
  17982. (cond
  17983. ((not (image-type-available-p 'imagemagick)) nil)
  17984. ((eq org-image-actual-width t) nil)
  17985. ((listp org-image-actual-width)
  17986. (or
  17987. ;; First try to find a width among
  17988. ;; attributes associated to the paragraph
  17989. ;; containing link.
  17990. (let ((paragraph
  17991. (let ((e link))
  17992. (while (and (setq e (org-element-property
  17993. :parent e))
  17994. (not (eq (org-element-type e)
  17995. 'paragraph))))
  17996. e)))
  17997. (when paragraph
  17998. (save-excursion
  17999. (goto-char (org-element-property :begin paragraph))
  18000. (when
  18001. (re-search-forward
  18002. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18003. (org-element-property
  18004. :post-affiliated paragraph)
  18005. t)
  18006. (string-to-number (match-string 1))))))
  18007. ;; Otherwise, fall-back to provided number.
  18008. (car org-image-actual-width)))
  18009. ((numberp org-image-actual-width)
  18010. org-image-actual-width)))
  18011. (old (get-char-property-and-overlay
  18012. (org-element-property :begin link)
  18013. 'org-image-overlay)))
  18014. (if (and (car-safe old) refresh)
  18015. (image-refresh (overlay-get (cdr old) 'display))
  18016. (let ((image (create-image file
  18017. (and width 'imagemagick)
  18018. nil
  18019. :width width)))
  18020. (when image
  18021. (let* ((link
  18022. ;; If inline image is the description
  18023. ;; of another link, be sure to
  18024. ;; consider the latter as the one to
  18025. ;; apply the overlay on.
  18026. (let ((parent
  18027. (org-element-property :parent link)))
  18028. (if (eq (org-element-type parent) 'link)
  18029. parent
  18030. link)))
  18031. (ov (make-overlay
  18032. (org-element-property :begin link)
  18033. (progn
  18034. (goto-char
  18035. (org-element-property :end link))
  18036. (skip-chars-backward " \t")
  18037. (point)))))
  18038. (overlay-put ov 'display image)
  18039. (overlay-put ov 'face 'default)
  18040. (overlay-put ov 'org-image-overlay t)
  18041. (overlay-put
  18042. ov 'modification-hooks
  18043. (list 'org-display-inline-remove-overlay))
  18044. (push ov org-inline-image-overlays)))))))))))))))
  18045. (define-obsolete-function-alias
  18046. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18047. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18048. "Remove inline-display overlay if a corresponding region is modified."
  18049. (let ((inhibit-modification-hooks t))
  18050. (when (and ov after)
  18051. (delete ov org-inline-image-overlays)
  18052. (delete-overlay ov))))
  18053. (defun org-remove-inline-images ()
  18054. "Remove inline display of images."
  18055. (interactive)
  18056. (mapc 'delete-overlay org-inline-image-overlays)
  18057. (setq org-inline-image-overlays nil))
  18058. ;;;; Key bindings
  18059. ;; Outline functions from `outline-mode-prefix-map'
  18060. ;; that can be remapped in Org:
  18061. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18062. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18063. (define-key org-mode-map [remap outline-forward-same-level]
  18064. 'org-forward-heading-same-level)
  18065. (define-key org-mode-map [remap outline-backward-same-level]
  18066. 'org-backward-heading-same-level)
  18067. (define-key org-mode-map [remap show-branches]
  18068. 'org-kill-note-or-show-branches)
  18069. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18070. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18071. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18072. (define-key org-mode-map [remap outline-next-visible-heading]
  18073. 'org-next-visible-heading)
  18074. (define-key org-mode-map [remap outline-previous-visible-heading]
  18075. 'org-previous-visible-heading)
  18076. ;; Outline functions from `outline-mode-prefix-map' that can not
  18077. ;; be remapped in Org:
  18078. ;; - the column "key binding" shows whether the Outline function is still
  18079. ;; available in Org mode on the same key that it has been bound to in
  18080. ;; Outline mode:
  18081. ;; - "overridden": key used for a different functionality in Org mode
  18082. ;; - else: key still bound to the same Outline function in Org mode
  18083. ;; | Outline function | key binding | Org replacement |
  18084. ;; |------------------------------------+-------------+--------------------------|
  18085. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18086. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18087. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18088. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18089. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18090. ;; | `show-entry' | overridden | no replacement |
  18091. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18092. ;; | `show-branches' | `C-c C-k' | still same function |
  18093. ;; | `show-subtree' | overridden | visibility cycling |
  18094. ;; | `show-all' | overridden | no replacement |
  18095. ;; | `hide-subtree' | overridden | visibility cycling |
  18096. ;; | `hide-body' | overridden | no replacement |
  18097. ;; | `hide-entry' | overridden | visibility cycling |
  18098. ;; | `hide-leaves' | overridden | no replacement |
  18099. ;; | `hide-sublevels' | overridden | no replacement |
  18100. ;; | `hide-other' | overridden | no replacement |
  18101. ;; Make `C-c C-x' a prefix key
  18102. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18103. ;; TAB key with modifiers
  18104. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18105. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18106. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18107. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18108. ;; The following line is necessary under Suse GNU/Linux
  18109. (unless (featurep 'xemacs)
  18110. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18111. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18112. (define-key org-mode-map [backtab] 'org-shifttab)
  18113. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18114. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18115. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18116. ;; Cursor keys with modifiers
  18117. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18118. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18119. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18120. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18121. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18122. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18123. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18124. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18125. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18126. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18127. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18128. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18129. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18130. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18131. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18132. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18133. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18134. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18135. ;; Babel keys
  18136. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18137. (mapc (lambda (pair)
  18138. (define-key org-babel-map (car pair) (cdr pair)))
  18139. org-babel-key-bindings)
  18140. ;;; Extra keys for tty access.
  18141. ;; We only set them when really needed because otherwise the
  18142. ;; menus don't show the simple keys
  18143. (when (or org-use-extra-keys
  18144. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18145. (not window-system))
  18146. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18147. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18148. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18149. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18150. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18151. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18152. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18153. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18154. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18155. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18156. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18157. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18158. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18159. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18160. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18161. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18162. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18163. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18164. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18165. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18166. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18167. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18168. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18169. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18170. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18171. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18172. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18173. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18174. ;; All the other keys
  18175. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18176. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18177. (if (boundp 'narrow-map)
  18178. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18179. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18180. (if (boundp 'narrow-map)
  18181. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18182. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18183. (if (boundp 'narrow-map)
  18184. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18185. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18186. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18187. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18188. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18189. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18190. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18191. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18192. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18193. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18194. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18195. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18196. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18197. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18198. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18199. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18200. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18201. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18202. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18203. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18204. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18205. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18206. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18207. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18208. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18209. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18210. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18211. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18212. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18213. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18214. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18215. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18216. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18217. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18218. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18219. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18220. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18221. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18222. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18223. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18224. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18225. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18226. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18227. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18228. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18229. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18230. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18231. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18232. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18233. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18234. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18235. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18236. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18237. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18238. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18239. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18240. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18241. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18242. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18243. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18244. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18245. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18246. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18247. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18248. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18249. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18250. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18251. (org-defkey org-mode-map "\C-m" 'org-return)
  18252. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18253. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18254. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18255. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18256. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18257. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18258. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18259. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18260. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18261. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18262. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18263. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18264. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18265. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18266. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18267. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18268. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18269. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18270. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18271. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18272. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18273. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18274. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18275. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18276. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18278. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18293. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18294. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18295. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18296. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18297. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18298. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18299. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18300. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18301. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18302. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18303. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18304. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18305. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18306. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18307. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18308. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18309. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18310. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18311. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18312. (when (featurep 'xemacs)
  18313. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18314. (defconst org-speed-commands-default
  18315. '(
  18316. ("Outline Navigation")
  18317. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18318. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18319. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18320. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18321. ("F" . org-next-block)
  18322. ("B" . org-previous-block)
  18323. ("u" . (org-speed-move-safe 'outline-up-heading))
  18324. ("j" . org-goto)
  18325. ("g" . (org-refile t))
  18326. ("Outline Visibility")
  18327. ("c" . org-cycle)
  18328. ("C" . org-shifttab)
  18329. (" " . org-display-outline-path)
  18330. ("s" . org-narrow-to-subtree)
  18331. ("=" . org-columns)
  18332. ("Outline Structure Editing")
  18333. ("U" . org-metaup)
  18334. ("D" . org-metadown)
  18335. ("r" . org-metaright)
  18336. ("l" . org-metaleft)
  18337. ("R" . org-shiftmetaright)
  18338. ("L" . org-shiftmetaleft)
  18339. ("i" . (progn (forward-char 1) (call-interactively
  18340. 'org-insert-heading-respect-content)))
  18341. ("^" . org-sort)
  18342. ("w" . org-refile)
  18343. ("a" . org-archive-subtree-default-with-confirmation)
  18344. ("@" . org-mark-subtree)
  18345. ("#" . org-toggle-comment)
  18346. ("Clock Commands")
  18347. ("I" . org-clock-in)
  18348. ("O" . org-clock-out)
  18349. ("Meta Data Editing")
  18350. ("t" . org-todo)
  18351. ("," . (org-priority))
  18352. ("0" . (org-priority ?\ ))
  18353. ("1" . (org-priority ?A))
  18354. ("2" . (org-priority ?B))
  18355. ("3" . (org-priority ?C))
  18356. (":" . org-set-tags-command)
  18357. ("e" . org-set-effort)
  18358. ("E" . org-inc-effort)
  18359. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18360. (org-entry-put (point) "APPT_WARNTIME" m)))
  18361. ("Agenda Views etc")
  18362. ("v" . org-agenda)
  18363. ("/" . org-sparse-tree)
  18364. ("Misc")
  18365. ("o" . org-open-at-point)
  18366. ("?" . org-speed-command-help)
  18367. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18368. (">" . (org-agenda-remove-restriction-lock))
  18369. )
  18370. "The default speed commands.")
  18371. (defun org-print-speed-command (e)
  18372. (if (> (length (car e)) 1)
  18373. (progn
  18374. (princ "\n")
  18375. (princ (car e))
  18376. (princ "\n")
  18377. (princ (make-string (length (car e)) ?-))
  18378. (princ "\n"))
  18379. (princ (car e))
  18380. (princ " ")
  18381. (if (symbolp (cdr e))
  18382. (princ (symbol-name (cdr e)))
  18383. (prin1 (cdr e)))
  18384. (princ "\n")))
  18385. (defun org-speed-command-help ()
  18386. "Show the available speed commands."
  18387. (interactive)
  18388. (if (not org-use-speed-commands)
  18389. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18390. (with-output-to-temp-buffer "*Help*"
  18391. (princ "User-defined Speed commands\n===========================\n")
  18392. (mapc 'org-print-speed-command org-speed-commands-user)
  18393. (princ "\n")
  18394. (princ "Built-in Speed commands\n=======================\n")
  18395. (mapc 'org-print-speed-command org-speed-commands-default))
  18396. (with-current-buffer "*Help*"
  18397. (setq truncate-lines t))))
  18398. (defun org-speed-move-safe (cmd)
  18399. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18400. If not, return to the original position and throw an error."
  18401. (interactive)
  18402. (let ((pos (point)))
  18403. (call-interactively cmd)
  18404. (unless (and (bolp) (org-at-heading-p))
  18405. (goto-char pos)
  18406. (error "Boundary reached while executing %s" cmd))))
  18407. (defvar org-self-insert-command-undo-counter 0)
  18408. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18409. (defvar org-speed-command nil)
  18410. (define-obsolete-function-alias
  18411. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18412. (defun org-speed-command-activate (keys)
  18413. "Hook for activating single-letter speed commands.
  18414. `org-speed-commands-default' specifies a minimal command set.
  18415. Use `org-speed-commands-user' for further customization."
  18416. (when (or (and (bolp) (looking-at org-outline-regexp))
  18417. (and (functionp org-use-speed-commands)
  18418. (funcall org-use-speed-commands)))
  18419. (cdr (assoc keys (append org-speed-commands-user
  18420. org-speed-commands-default)))))
  18421. (define-obsolete-function-alias
  18422. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18423. (defun org-babel-speed-command-activate (keys)
  18424. "Hook for activating single-letter code block commands."
  18425. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18426. (cdr (assoc keys org-babel-key-bindings))))
  18427. (defcustom org-speed-command-hook
  18428. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18429. "Hook for activating speed commands at strategic locations.
  18430. Hook functions are called in sequence until a valid handler is
  18431. found.
  18432. Each hook takes a single argument, a user-pressed command key
  18433. which is also a `self-insert-command' from the global map.
  18434. Within the hook, examine the cursor position and the command key
  18435. and return nil or a valid handler as appropriate. Handler could
  18436. be one of an interactive command, a function, or a form.
  18437. Set `org-use-speed-commands' to non-nil value to enable this
  18438. hook. The default setting is `org-speed-command-activate'."
  18439. :group 'org-structure
  18440. :version "24.1"
  18441. :type 'hook)
  18442. (defun org-self-insert-command (N)
  18443. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18444. If the cursor is in a table looking at whitespace, the whitespace is
  18445. overwritten, and the table is not marked as requiring realignment."
  18446. (interactive "p")
  18447. (org-check-before-invisible-edit 'insert)
  18448. (cond
  18449. ((and org-use-speed-commands
  18450. (let ((kv (this-command-keys-vector)))
  18451. (setq org-speed-command
  18452. (run-hook-with-args-until-success
  18453. 'org-speed-command-hook
  18454. (make-string 1 (aref kv (1- (length kv))))))))
  18455. (cond
  18456. ((commandp org-speed-command)
  18457. (setq this-command org-speed-command)
  18458. (call-interactively org-speed-command))
  18459. ((functionp org-speed-command)
  18460. (funcall org-speed-command))
  18461. ((and org-speed-command (listp org-speed-command))
  18462. (eval org-speed-command))
  18463. (t (let (org-use-speed-commands)
  18464. (call-interactively 'org-self-insert-command)))))
  18465. ((and
  18466. (org-table-p)
  18467. (progn
  18468. ;; check if we blank the field, and if that triggers align
  18469. (and (featurep 'org-table) org-table-auto-blank-field
  18470. (memq last-command
  18471. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18472. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18473. ;; got extra space, this field does not determine column width
  18474. (let (org-table-may-need-update) (org-table-blank-field))
  18475. ;; no extra space, this field may determine column width
  18476. (org-table-blank-field)))
  18477. t)
  18478. (eq N 1)
  18479. (looking-at "[^|\n]* |"))
  18480. (let (org-table-may-need-update)
  18481. (goto-char (1- (match-end 0)))
  18482. (backward-delete-char 1)
  18483. (goto-char (match-beginning 0))
  18484. (self-insert-command N)))
  18485. (t
  18486. (setq org-table-may-need-update t)
  18487. (self-insert-command N)
  18488. (org-fix-tags-on-the-fly)
  18489. (if org-self-insert-cluster-for-undo
  18490. (if (not (eq last-command 'org-self-insert-command))
  18491. (setq org-self-insert-command-undo-counter 1)
  18492. (if (>= org-self-insert-command-undo-counter 20)
  18493. (setq org-self-insert-command-undo-counter 1)
  18494. (and (> org-self-insert-command-undo-counter 0)
  18495. buffer-undo-list (listp buffer-undo-list)
  18496. (not (cadr buffer-undo-list)) ; remove nil entry
  18497. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18498. (setq org-self-insert-command-undo-counter
  18499. (1+ org-self-insert-command-undo-counter))))))))
  18500. (defun org-check-before-invisible-edit (kind)
  18501. "Check is editing if kind KIND would be dangerous with invisible text around.
  18502. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18503. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18504. (if (and org-catch-invisible-edits
  18505. (or (not (boundp 'visible-mode)) (not visible-mode))
  18506. (or (get-char-property (point) 'invisible)
  18507. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18508. ;; OK, we need to take a closer look
  18509. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18510. (invisible-before-point (if (bobp) nil (get-char-property
  18511. (1- (point)) 'invisible)))
  18512. (border-and-ok-direction
  18513. (or
  18514. ;; Check if we are acting predictably before invisible text
  18515. (and invisible-at-point (not invisible-before-point)
  18516. (memq kind '(insert delete-backward)))
  18517. ;; Check if we are acting predictably after invisible text
  18518. ;; This works not well, and I have turned it off. It seems
  18519. ;; better to always show and stop after invisible text.
  18520. ;; (and (not invisible-at-point) invisible-before-point
  18521. ;; (memq kind '(insert delete)))
  18522. )))
  18523. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18524. (memq invisible-before-point '(outline org-hide-block t)))
  18525. (if (eq org-catch-invisible-edits 'error)
  18526. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18527. (if (and org-custom-properties-overlays
  18528. (y-or-n-p "Display invisible properties in this buffer? "))
  18529. (org-toggle-custom-properties-visibility)
  18530. ;; Make the area visible
  18531. (save-excursion
  18532. (if invisible-before-point
  18533. (goto-char (previous-single-char-property-change
  18534. (point) 'invisible)))
  18535. (show-subtree))
  18536. (cond
  18537. ((eq org-catch-invisible-edits 'show)
  18538. ;; That's it, we do the edit after showing
  18539. (message
  18540. "Unfolding invisible region around point before editing")
  18541. (sit-for 1))
  18542. ((and (eq org-catch-invisible-edits 'smart)
  18543. border-and-ok-direction)
  18544. (message "Unfolding invisible region around point before editing"))
  18545. (t
  18546. ;; Don't do the edit, make the user repeat it in full visibility
  18547. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18548. (defun org-fix-tags-on-the-fly ()
  18549. (when (and (equal (char-after (point-at-bol)) ?*)
  18550. (org-at-heading-p))
  18551. (org-align-tags-here org-tags-column)))
  18552. (defun org-delete-backward-char (N)
  18553. "Like `delete-backward-char', insert whitespace at field end in tables.
  18554. When deleting backwards, in tables this function will insert whitespace in
  18555. front of the next \"|\" separator, to keep the table aligned. The table will
  18556. still be marked for re-alignment if the field did fill the entire column,
  18557. because, in this case the deletion might narrow the column."
  18558. (interactive "p")
  18559. (save-match-data
  18560. (org-check-before-invisible-edit 'delete-backward)
  18561. (if (and (org-table-p)
  18562. (eq N 1)
  18563. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18564. (looking-at ".*?|"))
  18565. (let ((pos (point))
  18566. (noalign (looking-at "[^|\n\r]* |"))
  18567. (c org-table-may-need-update))
  18568. (backward-delete-char N)
  18569. (if (not overwrite-mode)
  18570. (progn
  18571. (skip-chars-forward "^|")
  18572. (insert " ")
  18573. (goto-char (1- pos))))
  18574. ;; noalign: if there were two spaces at the end, this field
  18575. ;; does not determine the width of the column.
  18576. (if noalign (setq org-table-may-need-update c)))
  18577. (backward-delete-char N)
  18578. (org-fix-tags-on-the-fly))))
  18579. (defun org-delete-char (N)
  18580. "Like `delete-char', but insert whitespace at field end in tables.
  18581. When deleting characters, in tables this function will insert whitespace in
  18582. front of the next \"|\" separator, to keep the table aligned. The table will
  18583. still be marked for re-alignment if the field did fill the entire column,
  18584. because, in this case the deletion might narrow the column."
  18585. (interactive "p")
  18586. (save-match-data
  18587. (org-check-before-invisible-edit 'delete)
  18588. (if (and (org-table-p)
  18589. (not (bolp))
  18590. (not (= (char-after) ?|))
  18591. (eq N 1))
  18592. (if (looking-at ".*?|")
  18593. (let ((pos (point))
  18594. (noalign (looking-at "[^|\n\r]* |"))
  18595. (c org-table-may-need-update))
  18596. (replace-match
  18597. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18598. (goto-char pos)
  18599. ;; noalign: if there were two spaces at the end, this field
  18600. ;; does not determine the width of the column.
  18601. (if noalign (setq org-table-may-need-update c)))
  18602. (delete-char N))
  18603. (delete-char N)
  18604. (org-fix-tags-on-the-fly))))
  18605. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18606. (put 'org-self-insert-command 'delete-selection
  18607. (lambda ()
  18608. (not (run-hook-with-args-until-success
  18609. 'self-insert-uses-region-functions))))
  18610. (put 'orgtbl-self-insert-command 'delete-selection
  18611. (lambda ()
  18612. (not (run-hook-with-args-until-success
  18613. 'self-insert-uses-region-functions))))
  18614. (put 'org-delete-char 'delete-selection 'supersede)
  18615. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18616. (put 'org-yank 'delete-selection 'yank)
  18617. ;; Make `flyspell-mode' delay after some commands
  18618. (put 'org-self-insert-command 'flyspell-delayed t)
  18619. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18620. (put 'org-delete-char 'flyspell-delayed t)
  18621. (put 'org-delete-backward-char 'flyspell-delayed t)
  18622. ;; Make pabbrev-mode expand after org-mode commands
  18623. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18624. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18625. (defun org-remap (map &rest commands)
  18626. "In MAP, remap the functions given in COMMANDS.
  18627. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18628. (let (new old)
  18629. (while commands
  18630. (setq old (pop commands) new (pop commands))
  18631. (if (fboundp 'command-remapping)
  18632. (org-defkey map (vector 'remap old) new)
  18633. (substitute-key-definition old new map global-map)))))
  18634. (defun org-transpose-words ()
  18635. "Transpose words for Org.
  18636. This uses the `org-mode-transpose-word-syntax-table' syntax
  18637. table, which interprets characters in `org-emphasis-alist' as
  18638. word constituents."
  18639. (interactive)
  18640. (with-syntax-table org-mode-transpose-word-syntax-table
  18641. (call-interactively 'transpose-words)))
  18642. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18643. (when (eq org-enable-table-editor 'optimized)
  18644. ;; If the user wants maximum table support, we need to hijack
  18645. ;; some standard editing functions
  18646. (org-remap org-mode-map
  18647. 'self-insert-command 'org-self-insert-command
  18648. 'delete-char 'org-delete-char
  18649. 'delete-backward-char 'org-delete-backward-char)
  18650. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18651. (defvar org-ctrl-c-ctrl-c-hook nil
  18652. "Hook for functions attaching themselves to `C-c C-c'.
  18653. This can be used to add additional functionality to the C-c C-c
  18654. key which executes context-dependent commands. This hook is run
  18655. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18656. run after the last test.
  18657. Each function will be called with no arguments. The function
  18658. must check if the context is appropriate for it to act. If yes,
  18659. it should do its thing and then return a non-nil value. If the
  18660. context is wrong, just do nothing and return nil.")
  18661. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18662. "Hook for functions attaching themselves to `C-c C-c'.
  18663. This can be used to add additional functionality to the C-c C-c
  18664. key which executes context-dependent commands. This hook is run
  18665. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18666. before the first test.
  18667. Each function will be called with no arguments. The function
  18668. must check if the context is appropriate for it to act. If yes,
  18669. it should do its thing and then return a non-nil value. If the
  18670. context is wrong, just do nothing and return nil.")
  18671. (defvar org-tab-first-hook nil
  18672. "Hook for functions to attach themselves to TAB.
  18673. See `org-ctrl-c-ctrl-c-hook' for more information.
  18674. This hook runs as the first action when TAB is pressed, even before
  18675. `org-cycle' messes around with the `outline-regexp' to cater for
  18676. inline tasks and plain list item folding.
  18677. If any function in this hook returns t, any other actions that
  18678. would have been caused by TAB (such as table field motion or visibility
  18679. cycling) will not occur.")
  18680. (defvar org-tab-after-check-for-table-hook nil
  18681. "Hook for functions to attach themselves to TAB.
  18682. See `org-ctrl-c-ctrl-c-hook' for more information.
  18683. This hook runs after it has been established that the cursor is not in a
  18684. table, but before checking if the cursor is in a headline or if global cycling
  18685. should be done.
  18686. If any function in this hook returns t, not other actions like visibility
  18687. cycling will be done.")
  18688. (defvar org-tab-after-check-for-cycling-hook nil
  18689. "Hook for functions to attach themselves to TAB.
  18690. See `org-ctrl-c-ctrl-c-hook' for more information.
  18691. This hook runs after it has been established that not table field motion and
  18692. not visibility should be done because of current context. This is probably
  18693. the place where a package like yasnippets can hook in.")
  18694. (defvar org-tab-before-tab-emulation-hook nil
  18695. "Hook for functions to attach themselves to TAB.
  18696. See `org-ctrl-c-ctrl-c-hook' for more information.
  18697. This hook runs after every other options for TAB have been exhausted, but
  18698. before indentation and \t insertion takes place.")
  18699. (defvar org-metaleft-hook nil
  18700. "Hook for functions attaching themselves to `M-left'.
  18701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18702. (defvar org-metaright-hook nil
  18703. "Hook for functions attaching themselves to `M-right'.
  18704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18705. (defvar org-metaup-hook nil
  18706. "Hook for functions attaching themselves to `M-up'.
  18707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18708. (defvar org-metadown-hook nil
  18709. "Hook for functions attaching themselves to `M-down'.
  18710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18711. (defvar org-shiftmetaleft-hook nil
  18712. "Hook for functions attaching themselves to `M-S-left'.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18714. (defvar org-shiftmetaright-hook nil
  18715. "Hook for functions attaching themselves to `M-S-right'.
  18716. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18717. (defvar org-shiftmetaup-hook nil
  18718. "Hook for functions attaching themselves to `M-S-up'.
  18719. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18720. (defvar org-shiftmetadown-hook nil
  18721. "Hook for functions attaching themselves to `M-S-down'.
  18722. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18723. (defvar org-metareturn-hook nil
  18724. "Hook for functions attaching themselves to `M-RET'.
  18725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18726. (defvar org-shiftup-hook nil
  18727. "Hook for functions attaching themselves to `S-up'.
  18728. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18729. (defvar org-shiftup-final-hook nil
  18730. "Hook for functions attaching themselves to `S-up'.
  18731. This one runs after all other options except shift-select have been excluded.
  18732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18733. (defvar org-shiftdown-hook nil
  18734. "Hook for functions attaching themselves to `S-down'.
  18735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18736. (defvar org-shiftdown-final-hook nil
  18737. "Hook for functions attaching themselves to `S-down'.
  18738. This one runs after all other options except shift-select have been excluded.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-shiftleft-hook nil
  18741. "Hook for functions attaching themselves to `S-left'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftleft-final-hook nil
  18744. "Hook for functions attaching themselves to `S-left'.
  18745. This one runs after all other options except shift-select have been excluded.
  18746. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18747. (defvar org-shiftright-hook nil
  18748. "Hook for functions attaching themselves to `S-right'.
  18749. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18750. (defvar org-shiftright-final-hook nil
  18751. "Hook for functions attaching themselves to `S-right'.
  18752. This one runs after all other options except shift-select have been excluded.
  18753. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18754. (defun org-modifier-cursor-error ()
  18755. "Throw an error, a modified cursor command was applied in wrong context."
  18756. (user-error "This command is active in special context like tables, headlines or items"))
  18757. (defun org-shiftselect-error ()
  18758. "Throw an error because Shift-Cursor command was applied in wrong context."
  18759. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18760. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18761. (user-error "This command works only in special context like headlines or timestamps")))
  18762. (defun org-call-for-shift-select (cmd)
  18763. (let ((this-command-keys-shift-translated t))
  18764. (call-interactively cmd)))
  18765. (defun org-shifttab (&optional arg)
  18766. "Global visibility cycling or move to previous table field.
  18767. Call `org-table-previous-field' within a table.
  18768. When ARG is nil, cycle globally through visibility states.
  18769. When ARG is a numeric prefix, show contents of this level."
  18770. (interactive "P")
  18771. (cond
  18772. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18773. ((integerp arg)
  18774. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18775. (message "Content view to level: %d" arg)
  18776. (org-content (prefix-numeric-value arg2))
  18777. (org-cycle-show-empty-lines t)
  18778. (setq org-cycle-global-status 'overview)))
  18779. (t (call-interactively 'org-global-cycle))))
  18780. (defun org-shiftmetaleft ()
  18781. "Promote subtree or delete table column.
  18782. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18783. `org-table-delete-column', depending on context. See the
  18784. individual commands for more information."
  18785. (interactive)
  18786. (cond
  18787. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18788. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18789. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18790. ((if (not (org-region-active-p)) (org-at-item-p)
  18791. (save-excursion (goto-char (region-beginning))
  18792. (org-at-item-p)))
  18793. (call-interactively 'org-outdent-item-tree))
  18794. (t (org-modifier-cursor-error))))
  18795. (defun org-shiftmetaright ()
  18796. "Demote subtree or insert table column.
  18797. Calls `org-demote-subtree', `org-indent-item-tree', or
  18798. `org-table-insert-column', depending on context. See the
  18799. individual commands for more information."
  18800. (interactive)
  18801. (cond
  18802. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18803. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18804. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18805. ((if (not (org-region-active-p)) (org-at-item-p)
  18806. (save-excursion (goto-char (region-beginning))
  18807. (org-at-item-p)))
  18808. (call-interactively 'org-indent-item-tree))
  18809. (t (org-modifier-cursor-error))))
  18810. (defun org-shiftmetaup (&optional arg)
  18811. "Drag the line at point up.
  18812. In a table, kill the current row.
  18813. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18814. but also adjust the previous clocked item in the clock history.
  18815. Everywhere else, drag the line at point up."
  18816. (interactive "P")
  18817. (cond
  18818. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18819. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18820. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18821. (call-interactively 'org-timestamp-up)))
  18822. (t (call-interactively 'org-drag-line-backward))))
  18823. (defun org-shiftmetadown (&optional arg)
  18824. "Drag the line at point down.
  18825. In a table, insert an empty row at the current line.
  18826. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18827. but also adjust the previous clocked item in the clock history.
  18828. Everywhere else, drag the line at point down."
  18829. (interactive "P")
  18830. (cond
  18831. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18832. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18833. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18834. (call-interactively 'org-timestamp-down)))
  18835. (t (call-interactively 'org-drag-line-forward))))
  18836. (defsubst org-hidden-tree-error ()
  18837. (user-error
  18838. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18839. (defun org-metaleft (&optional arg)
  18840. "Promote heading, list item at point or move table column left.
  18841. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18842. depending on context. With no specific context, calls the Emacs
  18843. default `backward-word'. See the individual commands for more
  18844. information.
  18845. This function runs the hook `org-metaleft-hook' as a first step,
  18846. and returns at first non-nil value."
  18847. (interactive "P")
  18848. (cond
  18849. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18850. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18851. ((org-with-limited-levels
  18852. (or (org-at-heading-p)
  18853. (and (org-region-active-p)
  18854. (save-excursion
  18855. (goto-char (region-beginning))
  18856. (org-at-heading-p)))))
  18857. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18858. (call-interactively 'org-do-promote))
  18859. ;; At an inline task.
  18860. ((org-at-heading-p)
  18861. (call-interactively 'org-inlinetask-promote))
  18862. ((or (org-at-item-p)
  18863. (and (org-region-active-p)
  18864. (save-excursion
  18865. (goto-char (region-beginning))
  18866. (org-at-item-p))))
  18867. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18868. (call-interactively 'org-outdent-item))
  18869. (t (call-interactively 'backward-word))))
  18870. (defun org-metaright (&optional arg)
  18871. "Demote heading, list item at point or move table column right.
  18872. In front of a drawer or a block keyword, indent it correctly.
  18873. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18874. `org-indnet-drawer' or `org-indent-block' depending on context.
  18875. With no specific context, calls the Emacs default `forward-word'.
  18876. See the individual commands for more information.
  18877. This function runs the hook `org-metaright-hook' as a first step,
  18878. and returns at first non-nil value."
  18879. (interactive "P")
  18880. (cond
  18881. ((run-hook-with-args-until-success 'org-metaright-hook))
  18882. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18883. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18884. ((org-at-block-p) (call-interactively 'org-indent-block))
  18885. ((org-with-limited-levels
  18886. (or (org-at-heading-p)
  18887. (and (org-region-active-p)
  18888. (save-excursion
  18889. (goto-char (region-beginning))
  18890. (org-at-heading-p)))))
  18891. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18892. (call-interactively 'org-do-demote))
  18893. ;; At an inline task.
  18894. ((org-at-heading-p)
  18895. (call-interactively 'org-inlinetask-demote))
  18896. ((or (org-at-item-p)
  18897. (and (org-region-active-p)
  18898. (save-excursion
  18899. (goto-char (region-beginning))
  18900. (org-at-item-p))))
  18901. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18902. (call-interactively 'org-indent-item))
  18903. (t (call-interactively 'forward-word))))
  18904. (defun org-check-for-hidden (what)
  18905. "Check if there are hidden headlines/items in the current visual line.
  18906. WHAT can be either `headlines' or `items'. If the current line is
  18907. an outline or item heading and it has a folded subtree below it,
  18908. this function returns t, nil otherwise."
  18909. (let ((re (cond
  18910. ((eq what 'headlines) org-outline-regexp-bol)
  18911. ((eq what 'items) (org-item-beginning-re))
  18912. (t (error "This should not happen"))))
  18913. beg end)
  18914. (save-excursion
  18915. (catch 'exit
  18916. (unless (org-region-active-p)
  18917. (setq beg (point-at-bol))
  18918. (beginning-of-line 2)
  18919. (while (and (not (eobp)) ;; this is like `next-line'
  18920. (get-char-property (1- (point)) 'invisible))
  18921. (beginning-of-line 2))
  18922. (setq end (point))
  18923. (goto-char beg)
  18924. (goto-char (point-at-eol))
  18925. (setq end (max end (point)))
  18926. (while (re-search-forward re end t)
  18927. (if (get-char-property (match-beginning 0) 'invisible)
  18928. (throw 'exit t))))
  18929. nil))))
  18930. (defun org-metaup (&optional arg)
  18931. "Move subtree up or move table row up.
  18932. Calls `org-move-subtree-up' or `org-table-move-row' or
  18933. `org-move-item-up', depending on context. See the individual commands
  18934. for more information."
  18935. (interactive "P")
  18936. (cond
  18937. ((run-hook-with-args-until-success 'org-metaup-hook))
  18938. ((org-region-active-p)
  18939. (let* ((a (min (region-beginning) (region-end)))
  18940. (b (1- (max (region-beginning) (region-end))))
  18941. (c (save-excursion (goto-char a)
  18942. (move-beginning-of-line 0)))
  18943. (d (save-excursion (goto-char a)
  18944. (move-end-of-line 0) (point))))
  18945. (transpose-regions a b c d)
  18946. (goto-char c)))
  18947. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18948. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18949. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18950. (t (org-drag-element-backward))))
  18951. (defun org-metadown (&optional arg)
  18952. "Move subtree down or move table row down.
  18953. Calls `org-move-subtree-down' or `org-table-move-row' or
  18954. `org-move-item-down', depending on context. See the individual
  18955. commands for more information."
  18956. (interactive "P")
  18957. (cond
  18958. ((run-hook-with-args-until-success 'org-metadown-hook))
  18959. ((org-region-active-p)
  18960. (let* ((a (min (region-beginning) (region-end)))
  18961. (b (max (region-beginning) (region-end)))
  18962. (c (save-excursion (goto-char b)
  18963. (move-beginning-of-line 1)))
  18964. (d (save-excursion (goto-char b)
  18965. (move-end-of-line 1) (1+ (point)))))
  18966. (transpose-regions a b c d)
  18967. (goto-char d)))
  18968. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18969. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18970. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18971. (t (org-drag-element-forward))))
  18972. (defun org-shiftup (&optional arg)
  18973. "Increase item in timestamp or increase priority of current headline.
  18974. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18975. depending on context. See the individual commands for more information."
  18976. (interactive "P")
  18977. (cond
  18978. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18979. ((and org-support-shift-select (org-region-active-p))
  18980. (org-call-for-shift-select 'previous-line))
  18981. ((org-at-timestamp-p t)
  18982. (call-interactively (if org-edit-timestamp-down-means-later
  18983. 'org-timestamp-down 'org-timestamp-up)))
  18984. ((and (not (eq org-support-shift-select 'always))
  18985. org-enable-priority-commands
  18986. (org-at-heading-p))
  18987. (call-interactively 'org-priority-up))
  18988. ((and (not org-support-shift-select) (org-at-item-p))
  18989. (call-interactively 'org-previous-item))
  18990. ((org-clocktable-try-shift 'up arg))
  18991. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18992. (org-support-shift-select
  18993. (org-call-for-shift-select 'previous-line))
  18994. (t (org-shiftselect-error))))
  18995. (defun org-shiftdown (&optional arg)
  18996. "Decrease item in timestamp or decrease priority of current headline.
  18997. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18998. depending on context. See the individual commands for more information."
  18999. (interactive "P")
  19000. (cond
  19001. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19002. ((and org-support-shift-select (org-region-active-p))
  19003. (org-call-for-shift-select 'next-line))
  19004. ((org-at-timestamp-p t)
  19005. (call-interactively (if org-edit-timestamp-down-means-later
  19006. 'org-timestamp-up 'org-timestamp-down)))
  19007. ((and (not (eq org-support-shift-select 'always))
  19008. org-enable-priority-commands
  19009. (org-at-heading-p))
  19010. (call-interactively 'org-priority-down))
  19011. ((and (not org-support-shift-select) (org-at-item-p))
  19012. (call-interactively 'org-next-item))
  19013. ((org-clocktable-try-shift 'down arg))
  19014. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19015. (org-support-shift-select
  19016. (org-call-for-shift-select 'next-line))
  19017. (t (org-shiftselect-error))))
  19018. (defun org-shiftright (&optional arg)
  19019. "Cycle the thing at point or in the current line, depending on context.
  19020. Depending on context, this does one of the following:
  19021. - switch a timestamp at point one day into the future
  19022. - on a headline, switch to the next TODO keyword.
  19023. - on an item, switch entire list to the next bullet type
  19024. - on a property line, switch to the next allowed value
  19025. - on a clocktable definition line, move time block into the future"
  19026. (interactive "P")
  19027. (cond
  19028. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19029. ((and org-support-shift-select (org-region-active-p))
  19030. (org-call-for-shift-select 'forward-char))
  19031. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19032. ((and (not (eq org-support-shift-select 'always))
  19033. (org-at-heading-p))
  19034. (let ((org-inhibit-logging
  19035. (not org-treat-S-cursor-todo-selection-as-state-change))
  19036. (org-inhibit-blocking
  19037. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19038. (org-call-with-arg 'org-todo 'right)))
  19039. ((or (and org-support-shift-select
  19040. (not (eq org-support-shift-select 'always))
  19041. (org-at-item-bullet-p))
  19042. (and (not org-support-shift-select) (org-at-item-p)))
  19043. (org-call-with-arg 'org-cycle-list-bullet nil))
  19044. ((and (not (eq org-support-shift-select 'always))
  19045. (org-at-property-p))
  19046. (call-interactively 'org-property-next-allowed-value))
  19047. ((org-clocktable-try-shift 'right arg))
  19048. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19049. (org-support-shift-select
  19050. (org-call-for-shift-select 'forward-char))
  19051. (t (org-shiftselect-error))))
  19052. (defun org-shiftleft (&optional arg)
  19053. "Cycle the thing at point or in the current line, depending on context.
  19054. Depending on context, this does one of the following:
  19055. - switch a timestamp at point one day into the past
  19056. - on a headline, switch to the previous TODO keyword.
  19057. - on an item, switch entire list to the previous bullet type
  19058. - on a property line, switch to the previous allowed value
  19059. - on a clocktable definition line, move time block into the past"
  19060. (interactive "P")
  19061. (cond
  19062. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19063. ((and org-support-shift-select (org-region-active-p))
  19064. (org-call-for-shift-select 'backward-char))
  19065. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19066. ((and (not (eq org-support-shift-select 'always))
  19067. (org-at-heading-p))
  19068. (let ((org-inhibit-logging
  19069. (not org-treat-S-cursor-todo-selection-as-state-change))
  19070. (org-inhibit-blocking
  19071. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19072. (org-call-with-arg 'org-todo 'left)))
  19073. ((or (and org-support-shift-select
  19074. (not (eq org-support-shift-select 'always))
  19075. (org-at-item-bullet-p))
  19076. (and (not org-support-shift-select) (org-at-item-p)))
  19077. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19078. ((and (not (eq org-support-shift-select 'always))
  19079. (org-at-property-p))
  19080. (call-interactively 'org-property-previous-allowed-value))
  19081. ((org-clocktable-try-shift 'left arg))
  19082. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19083. (org-support-shift-select
  19084. (org-call-for-shift-select 'backward-char))
  19085. (t (org-shiftselect-error))))
  19086. (defun org-shiftcontrolright ()
  19087. "Switch to next TODO set."
  19088. (interactive)
  19089. (cond
  19090. ((and org-support-shift-select (org-region-active-p))
  19091. (org-call-for-shift-select 'forward-word))
  19092. ((and (not (eq org-support-shift-select 'always))
  19093. (org-at-heading-p))
  19094. (org-call-with-arg 'org-todo 'nextset))
  19095. (org-support-shift-select
  19096. (org-call-for-shift-select 'forward-word))
  19097. (t (org-shiftselect-error))))
  19098. (defun org-shiftcontrolleft ()
  19099. "Switch to previous TODO set."
  19100. (interactive)
  19101. (cond
  19102. ((and org-support-shift-select (org-region-active-p))
  19103. (org-call-for-shift-select 'backward-word))
  19104. ((and (not (eq org-support-shift-select 'always))
  19105. (org-at-heading-p))
  19106. (org-call-with-arg 'org-todo 'previousset))
  19107. (org-support-shift-select
  19108. (org-call-for-shift-select 'backward-word))
  19109. (t (org-shiftselect-error))))
  19110. (defun org-shiftcontrolup (&optional n)
  19111. "Change timestamps synchronously up in CLOCK log lines.
  19112. Optional argument N tells to change by that many units."
  19113. (interactive "P")
  19114. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19115. (let (org-support-shift-select)
  19116. (org-clock-timestamps-up n))
  19117. (user-error "Not at a clock log")))
  19118. (defun org-shiftcontroldown (&optional n)
  19119. "Change timestamps synchronously down in CLOCK log lines.
  19120. Optional argument N tells to change by that many units."
  19121. (interactive "P")
  19122. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19123. (let (org-support-shift-select)
  19124. (org-clock-timestamps-down n))
  19125. (user-error "Not at a clock log")))
  19126. (defun org-increase-number-at-point (&optional inc)
  19127. "Increment the number at point.
  19128. With an optional prefix numeric argument INC, increment using
  19129. this numeric value."
  19130. (interactive "p")
  19131. (if (not (number-at-point))
  19132. (user-error "Not on a number")
  19133. (unless inc (setq inc 1))
  19134. (let ((pos (point))
  19135. (beg (skip-chars-backward "-+^/*0-9eE."))
  19136. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19137. (setq nap (buffer-substring-no-properties
  19138. (+ pos beg) (+ pos beg end)))
  19139. (delete-region (+ pos beg) (+ pos beg end))
  19140. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19141. (when (org-at-table-p)
  19142. (org-table-align)
  19143. (org-table-end-of-field 1))))
  19144. (defun org-decrease-number-at-point (&optional inc)
  19145. "Decrement the number at point.
  19146. With an optional prefix numeric argument INC, decrement using
  19147. this numeric value."
  19148. (interactive "p")
  19149. (org-increase-number-at-point (- (or inc 1))))
  19150. (defun org-ctrl-c-ret ()
  19151. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19152. (interactive)
  19153. (cond
  19154. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19155. (t (call-interactively 'org-insert-heading))))
  19156. (defun org-find-visible ()
  19157. (let ((s (point)))
  19158. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19159. (get-char-property s 'invisible)))
  19160. s))
  19161. (defun org-find-invisible ()
  19162. (let ((s (point)))
  19163. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19164. (not (get-char-property s 'invisible))))
  19165. s))
  19166. (defun org-copy-visible (beg end)
  19167. "Copy the visible parts of the region."
  19168. (interactive "r")
  19169. (let (snippets s)
  19170. (save-excursion
  19171. (save-restriction
  19172. (narrow-to-region beg end)
  19173. (setq s (goto-char (point-min)))
  19174. (while (not (= (point) (point-max)))
  19175. (goto-char (org-find-invisible))
  19176. (push (buffer-substring s (point)) snippets)
  19177. (setq s (goto-char (org-find-visible))))))
  19178. (kill-new (apply 'concat (nreverse snippets)))))
  19179. (defun org-copy-special ()
  19180. "Copy region in table or copy current subtree.
  19181. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19182. See the individual commands for more information."
  19183. (interactive)
  19184. (call-interactively
  19185. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19186. (defun org-cut-special ()
  19187. "Cut region in table or cut current subtree.
  19188. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19189. See the individual commands for more information."
  19190. (interactive)
  19191. (call-interactively
  19192. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19193. (defun org-paste-special (arg)
  19194. "Paste rectangular region into table, or past subtree relative to level.
  19195. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19196. See the individual commands for more information."
  19197. (interactive "P")
  19198. (if (org-at-table-p)
  19199. (org-table-paste-rectangle)
  19200. (org-paste-subtree arg)))
  19201. (defsubst org-in-fixed-width-region-p ()
  19202. "Is point in a fixed-width region?"
  19203. (save-match-data
  19204. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19205. (defun org-edit-special (&optional arg)
  19206. "Call a special editor for the element at point.
  19207. When at a table, call the formula editor with `org-table-edit-formulas'.
  19208. When in a source code block, call `org-edit-src-code'.
  19209. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19210. When in an export block, call `org-edit-export-block'.
  19211. When at an #+INCLUDE keyword, visit the included file.
  19212. When at a footnote reference, call `org-edit-footnote-reference'
  19213. On a link, call `ffap' to visit the link at point.
  19214. Otherwise, return a user error."
  19215. (interactive "P")
  19216. (let ((element (org-element-at-point)))
  19217. (assert (not buffer-read-only) nil
  19218. "Buffer is read-only: %s" (buffer-name))
  19219. (case (org-element-type element)
  19220. (src-block
  19221. (if (not arg) (org-edit-src-code)
  19222. (let* ((info (org-babel-get-src-block-info))
  19223. (lang (nth 0 info))
  19224. (params (nth 2 info))
  19225. (session (cdr (assq :session params))))
  19226. (if (not session) (org-edit-src-code)
  19227. ;; At a src-block with a session and function called with
  19228. ;; an ARG: switch to the buffer related to the inferior
  19229. ;; process.
  19230. (switch-to-buffer
  19231. (funcall (intern (concat "org-babel-prep-session:" lang))
  19232. session params))))))
  19233. (keyword
  19234. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19235. (org-open-link-from-string
  19236. (format "[[%s]]"
  19237. (expand-file-name
  19238. (let ((value (org-element-property :value element)))
  19239. (cond ((not (org-string-nw-p value))
  19240. (user-error "No file to edit"))
  19241. ((string-match "\\`\"\\(.*?\\)\"" value)
  19242. (match-string 1 value))
  19243. ((string-match "\\`[^ \t\"]\\S-*" value)
  19244. (match-string 0 value))
  19245. (t (user-error "No valid file specified")))))))
  19246. (user-error "No special environment to edit here")))
  19247. (table
  19248. (if (eq (org-element-property :type element) 'table.el)
  19249. (org-edit-table.el)
  19250. (call-interactively 'org-table-edit-formulas)))
  19251. ;; Only Org tables contain `table-row' type elements.
  19252. (table-row (call-interactively 'org-table-edit-formulas))
  19253. (example-block (org-edit-src-code))
  19254. (export-block (org-edit-export-block))
  19255. (fixed-width (org-edit-fixed-width-region))
  19256. (otherwise
  19257. ;; No notable element at point. Though, we may be at a link or
  19258. ;; a footnote reference, which are objects. Thus, scan deeper.
  19259. (let ((context (org-element-context element)))
  19260. (case (org-element-type context)
  19261. (link (call-interactively #'ffap))
  19262. (footnote-reference (org-edit-footnote-reference))
  19263. (t (user-error "No special environment to edit here"))))))))
  19264. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19265. (defun org-ctrl-c-ctrl-c (&optional arg)
  19266. "Set tags in headline, or update according to changed information at point.
  19267. This command does many different things, depending on context:
  19268. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19269. this is what we do.
  19270. - If the cursor is on a statistics cookie, update it.
  19271. - If the cursor is in a headline, prompt for tags and insert them
  19272. into the current line, aligned to `org-tags-column'. When called
  19273. with prefix arg, realign all tags in the current buffer.
  19274. - If the cursor is in one of the special #+KEYWORD lines, this
  19275. triggers scanning the buffer for these lines and updating the
  19276. information.
  19277. - If the cursor is inside a table, realign the table. This command
  19278. works even if the automatic table editor has been turned off.
  19279. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19280. the entire table.
  19281. - If the cursor is at a footnote reference or definition, jump to
  19282. the corresponding definition or references, respectively.
  19283. - If the cursor is a the beginning of a dynamic block, update it.
  19284. - If the current buffer is a capture buffer, close note and file it.
  19285. - If the cursor is on a <<<target>>>, update radio targets and
  19286. corresponding links in this buffer.
  19287. - If the cursor is on a numbered item in a plain list, renumber the
  19288. ordered list.
  19289. - If the cursor is on a checkbox, toggle it.
  19290. - If the cursor is on a code block, evaluate it. The variable
  19291. `org-confirm-babel-evaluate' can be used to control prompting
  19292. before code block evaluation, by default every code block
  19293. evaluation requires confirmation. Code block evaluation can be
  19294. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19295. (interactive "P")
  19296. (cond
  19297. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19298. org-occur-highlights)
  19299. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19300. (org-remove-occur-highlights)
  19301. (message "Temporary highlights/overlays removed from current buffer"))
  19302. ((and (local-variable-p 'org-finish-function (current-buffer))
  19303. (fboundp org-finish-function))
  19304. (funcall org-finish-function))
  19305. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19306. (t
  19307. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19308. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19309. (user-error "C-c C-c can do nothing useful at this location"))
  19310. (let* ((context (org-element-context))
  19311. (type (org-element-type context)))
  19312. (case type
  19313. ;; When at a link, act according to the parent instead.
  19314. (link (setq context (org-element-property :parent context))
  19315. (setq type (org-element-type context)))
  19316. ;; Unsupported object types: refer to the first supported
  19317. ;; element or object containing it.
  19318. ((bold code entity export-snippet inline-babel-call inline-src-block
  19319. italic latex-fragment line-break macro strike-through subscript
  19320. superscript underline verbatim)
  19321. (setq context
  19322. (org-element-lineage
  19323. context '(radio-target paragraph verse-block table-cell)))))
  19324. ;; For convenience: at the first line of a paragraph on the
  19325. ;; same line as an item, apply function on that item instead.
  19326. (when (eq type 'paragraph)
  19327. (let ((parent (org-element-property :parent context)))
  19328. (when (and (eq (org-element-type parent) 'item)
  19329. (= (line-beginning-position)
  19330. (org-element-property :begin parent)))
  19331. (setq context parent type 'item))))
  19332. ;; Act according to type of element or object at point.
  19333. (case type
  19334. (clock (org-clock-update-time-maybe))
  19335. (dynamic-block
  19336. (save-excursion
  19337. (goto-char (org-element-property :post-affiliated context))
  19338. (org-update-dblock)))
  19339. (footnote-definition
  19340. (goto-char (org-element-property :post-affiliated context))
  19341. (call-interactively 'org-footnote-action))
  19342. (footnote-reference (call-interactively 'org-footnote-action))
  19343. ((headline inlinetask)
  19344. (save-excursion (goto-char (org-element-property :begin context))
  19345. (call-interactively 'org-set-tags)))
  19346. (item
  19347. ;; At an item: a double C-u set checkbox to "[-]"
  19348. ;; unconditionally, whereas a single one will toggle its
  19349. ;; presence. Without a universal argument, if the item
  19350. ;; has a checkbox, toggle it. Otherwise repair the list.
  19351. (let* ((box (org-element-property :checkbox context))
  19352. (struct (org-element-property :structure context))
  19353. (old-struct (copy-tree struct))
  19354. (parents (org-list-parents-alist struct))
  19355. (prevs (org-list-prevs-alist struct))
  19356. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19357. (org-list-set-checkbox
  19358. (org-element-property :begin context) struct
  19359. (cond ((equal arg '(16)) "[-]")
  19360. ((and (not box) (equal arg '(4))) "[ ]")
  19361. ((or (not box) (equal arg '(4))) nil)
  19362. ((eq box 'on) "[ ]")
  19363. (t "[X]")))
  19364. ;; Mimic `org-list-write-struct' but with grabbing
  19365. ;; a return value from `org-list-struct-fix-box'.
  19366. (org-list-struct-fix-ind struct parents 2)
  19367. (org-list-struct-fix-item-end struct)
  19368. (org-list-struct-fix-bul struct prevs)
  19369. (org-list-struct-fix-ind struct parents)
  19370. (let ((block-item
  19371. (org-list-struct-fix-box struct parents prevs orderedp)))
  19372. (if (and box (equal struct old-struct))
  19373. (if (equal arg '(16))
  19374. (message "Checkboxes already reset")
  19375. (user-error "Cannot toggle this checkbox: %s"
  19376. (if (eq box 'on)
  19377. "all subitems checked"
  19378. "unchecked subitems")))
  19379. (org-list-struct-apply-struct struct old-struct)
  19380. (org-update-checkbox-count-maybe))
  19381. (when block-item
  19382. (message "Checkboxes were removed due to empty box at line %d"
  19383. (org-current-line block-item))))))
  19384. (keyword
  19385. (let ((org-inhibit-startup-visibility-stuff t)
  19386. (org-startup-align-all-tables nil))
  19387. (when (boundp 'org-table-coordinate-overlays)
  19388. (mapc 'delete-overlay org-table-coordinate-overlays)
  19389. (setq org-table-coordinate-overlays nil))
  19390. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19391. (message "Local setup has been refreshed"))
  19392. (plain-list
  19393. ;; At a plain list, with a double C-u argument, set
  19394. ;; checkboxes of each item to "[-]", whereas a single one
  19395. ;; will toggle their presence according to the state of the
  19396. ;; first item in the list. Without an argument, repair the
  19397. ;; list.
  19398. (let* ((begin (org-element-property :contents-begin context))
  19399. (beginm (move-marker (make-marker) begin))
  19400. (struct (org-element-property :structure context))
  19401. (old-struct (copy-tree struct))
  19402. (first-box (save-excursion
  19403. (goto-char begin)
  19404. (looking-at org-list-full-item-re)
  19405. (match-string-no-properties 3)))
  19406. (new-box (cond ((equal arg '(16)) "[-]")
  19407. ((equal arg '(4)) (unless first-box "[ ]"))
  19408. ((equal first-box "[X]") "[ ]")
  19409. (t "[X]"))))
  19410. (cond
  19411. (arg
  19412. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19413. (org-list-get-all-items
  19414. begin struct (org-list-prevs-alist struct))))
  19415. ((and first-box (eq (point) begin))
  19416. ;; For convenience, when point is at bol on the first
  19417. ;; item of the list and no argument is provided, simply
  19418. ;; toggle checkbox of that item, if any.
  19419. (org-list-set-checkbox begin struct new-box)))
  19420. (org-list-write-struct
  19421. struct (org-list-parents-alist struct) old-struct)
  19422. (org-update-checkbox-count-maybe)
  19423. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19424. ((property-drawer node-property)
  19425. (call-interactively 'org-property-action))
  19426. ((radio-target target)
  19427. (call-interactively 'org-update-radio-target-regexp))
  19428. (statistics-cookie
  19429. (call-interactively 'org-update-statistics-cookies))
  19430. ((table table-cell table-row)
  19431. ;; At a table, recalculate every field and align it. Also
  19432. ;; send the table if necessary. If the table has
  19433. ;; a `table.el' type, just give up. At a table row or
  19434. ;; cell, maybe recalculate line but always align table.
  19435. (if (eq (org-element-property :type context) 'table.el)
  19436. (message "Use C-c ' to edit table.el tables")
  19437. (let ((org-enable-table-editor t))
  19438. (if (or (eq type 'table)
  19439. ;; Check if point is at a TBLFM line.
  19440. (and (eq type 'table-row)
  19441. (= (point) (org-element-property :end context))))
  19442. (save-excursion
  19443. (if (org-at-TBLFM-p)
  19444. (progn (require 'org-table)
  19445. (org-table-calc-current-TBLFM))
  19446. (goto-char (org-element-property :contents-begin context))
  19447. (org-call-with-arg 'org-table-recalculate (or arg t))
  19448. (orgtbl-send-table 'maybe)))
  19449. (org-table-maybe-eval-formula)
  19450. (cond (arg (call-interactively 'org-table-recalculate))
  19451. ((org-table-maybe-recalculate-line))
  19452. (t (org-table-align)))))))
  19453. (timestamp (org-timestamp-change 0 'day))
  19454. (otherwise
  19455. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19456. (user-error
  19457. "C-c C-c can do nothing useful at this location")))))))))
  19458. (defun org-mode-restart ()
  19459. (interactive)
  19460. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19461. (funcall major-mode)
  19462. (hack-local-variables)
  19463. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19464. (org-indent-mode -1)))
  19465. (message "%s restarted" major-mode))
  19466. (defun org-kill-note-or-show-branches ()
  19467. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19468. (interactive)
  19469. (if (not org-finish-function)
  19470. (progn
  19471. (hide-subtree)
  19472. (call-interactively 'show-branches))
  19473. (let ((org-note-abort t))
  19474. (funcall org-finish-function))))
  19475. (defun org-delete-indentation (&optional ARG)
  19476. "Join current line to previous and fix whitespace at join.
  19477. If previous line is a headline add to headline title. Otherwise
  19478. the function calls `delete-indentation'.
  19479. With argument, join this line to following line."
  19480. (interactive "*P")
  19481. (if (save-excursion
  19482. (if ARG (beginning-of-line)
  19483. (forward-line -1))
  19484. (looking-at org-complex-heading-regexp))
  19485. ;; At headline.
  19486. (let ((tags-column (when (match-beginning 5)
  19487. (save-excursion (goto-char (match-beginning 5))
  19488. (current-column))))
  19489. (string (concat " " (progn (when ARG (forward-line 1))
  19490. (org-trim (delete-and-extract-region
  19491. (line-beginning-position)
  19492. (line-end-position)))))))
  19493. (unless (bobp) (delete-region (point) (1- (point))))
  19494. (goto-char (or (match-end 4)
  19495. (match-beginning 5)
  19496. (match-end 0)))
  19497. (skip-chars-backward " \t")
  19498. (save-excursion (insert string))
  19499. ;; Adjust alignment of tags.
  19500. (when tags-column
  19501. (org-align-tags-here (if org-auto-align-tags
  19502. org-tags-column
  19503. tags-column))))
  19504. (delete-indentation ARG)))
  19505. (defun org-open-line (n)
  19506. "Insert a new row in tables, call `open-line' elsewhere.
  19507. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19508. (interactive "*p")
  19509. (cond
  19510. ((not org-special-ctrl-o)
  19511. (open-line n))
  19512. ((org-at-table-p)
  19513. (org-table-insert-row))
  19514. (t
  19515. (open-line n))))
  19516. (defun org-return (&optional indent)
  19517. "Goto next table row or insert a newline.
  19518. Calls `org-table-next-row' or `newline', depending on context.
  19519. When optional INDENT argument is non-nil, call
  19520. `newline-and-indent' instead of `newline'.
  19521. When `org-return-follows-link' is non-nil and point is on
  19522. a timestamp or a link, call `org-open-at-point'. However, it
  19523. will not happen if point is in a table or on a \"dead\"
  19524. object (e.g., within a comment). In these case, you need to use
  19525. `org-open-at-point' directly."
  19526. (interactive)
  19527. (let* ((context (if org-return-follows-link (org-element-context)
  19528. (org-element-at-point)))
  19529. (type (org-element-type context)))
  19530. (cond
  19531. ;; In a table, call `org-table-next-row'.
  19532. ((or (and (eq type 'table)
  19533. (>= (point) (org-element-property :contents-begin context))
  19534. (< (point) (org-element-property :contents-end context)))
  19535. (org-element-lineage context '(table-row table-cell) t))
  19536. (org-table-justify-field-maybe)
  19537. (call-interactively #'org-table-next-row))
  19538. ;; On a link or a timestamp but not on white spaces after it,
  19539. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19540. ((and org-return-follows-link
  19541. (memq type '(link timestamp))
  19542. (< (point)
  19543. (save-excursion (goto-char (org-element-property :end context))
  19544. (skip-chars-backward " \t")
  19545. (point))))
  19546. (call-interactively #'org-open-at-point))
  19547. ;; Insert newline in heading, but preserve tags.
  19548. ((and (not (bolp))
  19549. (save-excursion (beginning-of-line)
  19550. (looking-at org-complex-heading-regexp)))
  19551. ;; At headline. Split line. However, if point is on keyword,
  19552. ;; priority cookie or tags, do not break any of them: add
  19553. ;; a newline after the headline instead.
  19554. (let ((tags-column (and (match-beginning 5)
  19555. (save-excursion (goto-char (match-beginning 5))
  19556. (current-column))))
  19557. (string
  19558. (when (and (match-end 4)
  19559. (>= (point)
  19560. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19561. (<= (point) (match-end 4)))
  19562. (delete-and-extract-region (point) (match-end 4)))))
  19563. (when (and tags-column string) ; Adjust tag alignment.
  19564. (org-align-tags-here
  19565. (if org-auto-align-tags org-tags-column tags-column)))
  19566. (end-of-line)
  19567. (org-show-entry)
  19568. (if indent (newline-and-indent) (newline))
  19569. (when string (save-excursion (insert (org-trim string))))))
  19570. ;; In a list, make sure indenting keeps trailing text within.
  19571. ((and indent
  19572. (not (eolp))
  19573. (org-element-lineage context '(item)))
  19574. (let ((trailing-data
  19575. (delete-and-extract-region (point) (line-end-position))))
  19576. (newline-and-indent)
  19577. (save-excursion (insert trailing-data))))
  19578. (t (if indent (newline-and-indent) (newline))))))
  19579. (defun org-return-indent ()
  19580. "Goto next table row or insert a newline and indent.
  19581. Calls `org-table-next-row' or `newline-and-indent', depending on
  19582. context. See the individual commands for more information."
  19583. (interactive)
  19584. (org-return t))
  19585. (defun org-ctrl-c-star ()
  19586. "Compute table, or change heading status of lines.
  19587. Calls `org-table-recalculate' or `org-toggle-heading',
  19588. depending on context."
  19589. (interactive)
  19590. (cond
  19591. ((org-at-table-p)
  19592. (call-interactively 'org-table-recalculate))
  19593. (t
  19594. ;; Convert all lines in region to list items
  19595. (call-interactively 'org-toggle-heading))))
  19596. (defun org-ctrl-c-minus ()
  19597. "Insert separator line in table or modify bullet status of line.
  19598. Also turns a plain line or a region of lines into list items.
  19599. Calls `org-table-insert-hline', `org-toggle-item', or
  19600. `org-cycle-list-bullet', depending on context."
  19601. (interactive)
  19602. (cond
  19603. ((org-at-table-p)
  19604. (call-interactively 'org-table-insert-hline))
  19605. ((org-region-active-p)
  19606. (call-interactively 'org-toggle-item))
  19607. ((org-in-item-p)
  19608. (call-interactively 'org-cycle-list-bullet))
  19609. (t
  19610. (call-interactively 'org-toggle-item))))
  19611. (defun org-toggle-item (arg)
  19612. "Convert headings or normal lines to items, items to normal lines.
  19613. If there is no active region, only the current line is considered.
  19614. If the first non blank line in the region is a headline, convert
  19615. all headlines to items, shifting text accordingly.
  19616. If it is an item, convert all items to normal lines.
  19617. If it is normal text, change region into a list of items.
  19618. With a prefix argument ARG, change the region in a single item."
  19619. (interactive "P")
  19620. (let ((shift-text
  19621. (function
  19622. ;; Shift text in current section to IND, from point to END.
  19623. ;; The function leaves point to END line.
  19624. (lambda (ind end)
  19625. (let ((min-i 1000) (end (copy-marker end)))
  19626. ;; First determine the minimum indentation (MIN-I) of
  19627. ;; the text.
  19628. (save-excursion
  19629. (catch 'exit
  19630. (while (< (point) end)
  19631. (let ((i (org-get-indentation)))
  19632. (cond
  19633. ;; Skip blank lines and inline tasks.
  19634. ((looking-at "^[ \t]*$"))
  19635. ((looking-at org-outline-regexp-bol))
  19636. ;; We can't find less than 0 indentation.
  19637. ((zerop i) (throw 'exit (setq min-i 0)))
  19638. ((< i min-i) (setq min-i i))))
  19639. (forward-line))))
  19640. ;; Then indent each line so that a line indented to
  19641. ;; MIN-I becomes indented to IND. Ignore blank lines
  19642. ;; and inline tasks in the process.
  19643. (let ((delta (- ind min-i)))
  19644. (while (< (point) end)
  19645. (unless (or (looking-at "^[ \t]*$")
  19646. (looking-at org-outline-regexp-bol))
  19647. (org-indent-line-to (+ (org-get-indentation) delta)))
  19648. (forward-line)))))))
  19649. (skip-blanks
  19650. (function
  19651. ;; Return beginning of first non-blank line, starting from
  19652. ;; line at POS.
  19653. (lambda (pos)
  19654. (save-excursion
  19655. (goto-char pos)
  19656. (skip-chars-forward " \r\t\n")
  19657. (point-at-bol)))))
  19658. beg end)
  19659. ;; Determine boundaries of changes.
  19660. (if (org-region-active-p)
  19661. (setq beg (funcall skip-blanks (region-beginning))
  19662. end (copy-marker (region-end)))
  19663. (setq beg (funcall skip-blanks (point-at-bol))
  19664. end (copy-marker (point-at-eol))))
  19665. ;; Depending on the starting line, choose an action on the text
  19666. ;; between BEG and END.
  19667. (org-with-limited-levels
  19668. (save-excursion
  19669. (goto-char beg)
  19670. (cond
  19671. ;; Case 1. Start at an item: de-itemize. Note that it only
  19672. ;; happens when a region is active: `org-ctrl-c-minus'
  19673. ;; would call `org-cycle-list-bullet' otherwise.
  19674. ((org-at-item-p)
  19675. (while (< (point) end)
  19676. (when (org-at-item-p)
  19677. (skip-chars-forward " \t")
  19678. (delete-region (point) (match-end 0)))
  19679. (forward-line)))
  19680. ;; Case 2. Start at an heading: convert to items.
  19681. ((org-at-heading-p)
  19682. (let* ((bul (org-list-bullet-string "-"))
  19683. (bul-len (length bul))
  19684. (done (org-entry-is-done-p))
  19685. (todo (org-entry-is-todo-p))
  19686. ;; Indentation of the first heading. It should be
  19687. ;; relative to the indentation of its parent, if any.
  19688. (start-ind (save-excursion
  19689. (cond
  19690. ((not org-adapt-indentation) 0)
  19691. ((not (outline-previous-heading)) 0)
  19692. (t (length (match-string 0))))))
  19693. ;; Level of first heading. Further headings will be
  19694. ;; compared to it to determine hierarchy in the list.
  19695. (ref-level (org-reduced-level (org-outline-level))))
  19696. (when (or done todo) (org-todo ""))
  19697. (while (< (point) end)
  19698. (let* ((level (org-reduced-level (org-outline-level)))
  19699. (delta (max 0 (- level ref-level))))
  19700. ;; If current headline is less indented than the first
  19701. ;; one, set it as reference, in order to preserve
  19702. ;; subtrees.
  19703. (when (< level ref-level) (setq ref-level level))
  19704. (replace-match bul t t)
  19705. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19706. (when (or done todo)
  19707. (let* ((struct (org-list-struct))
  19708. (old (copy-tree struct)))
  19709. (org-list-set-checkbox (line-beginning-position)
  19710. struct
  19711. (if done "[X]" "[ ]"))
  19712. (org-list-write-struct struct
  19713. (org-list-parents-alist struct)
  19714. old)))
  19715. ;; Ensure all text down to END (or SECTION-END) belongs
  19716. ;; to the newly created item.
  19717. (let ((section-end (save-excursion
  19718. (or (outline-next-heading) (point)))))
  19719. (forward-line)
  19720. (funcall shift-text
  19721. (+ start-ind (* (1+ delta) bul-len))
  19722. (min end section-end)))))))
  19723. ;; Case 3. Normal line with ARG: make the first line of region
  19724. ;; an item, and shift indentation of others lines to
  19725. ;; set them as item's body.
  19726. (arg (let* ((bul (org-list-bullet-string "-"))
  19727. (bul-len (length bul))
  19728. (ref-ind (org-get-indentation)))
  19729. (skip-chars-forward " \t")
  19730. (insert bul)
  19731. (forward-line)
  19732. (while (< (point) end)
  19733. ;; Ensure that lines less indented than first one
  19734. ;; still get included in item body.
  19735. (funcall shift-text
  19736. (+ ref-ind bul-len)
  19737. (min end (save-excursion (or (outline-next-heading)
  19738. (point)))))
  19739. (forward-line))))
  19740. ;; Case 4. Normal line without ARG: turn each non-item line
  19741. ;; into an item.
  19742. (t
  19743. (while (< (point) end)
  19744. (unless (or (org-at-heading-p) (org-at-item-p))
  19745. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19746. (replace-match
  19747. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19748. (forward-line))))))))
  19749. (defun org-toggle-heading (&optional nstars)
  19750. "Convert headings to normal text, or items or text to headings.
  19751. If there is no active region, only convert the current line.
  19752. With a \\[universal-argument] prefix, convert the whole list at
  19753. point into heading.
  19754. In a region:
  19755. - If the first non blank line is a headline, remove the stars
  19756. from all headlines in the region.
  19757. - If it is a normal line, turn each and every normal line (i.e.,
  19758. not an heading or an item) in the region into headings. If you
  19759. want to convert only the first line of this region, use one
  19760. universal prefix argument.
  19761. - If it is a plain list item, turn all plain list items into headings.
  19762. When converting a line into a heading, the number of stars is chosen
  19763. such that the lines become children of the current entry. However,
  19764. when a numeric prefix argument is given, its value determines the
  19765. number of stars to add."
  19766. (interactive "P")
  19767. (let ((skip-blanks
  19768. (function
  19769. ;; Return beginning of first non-blank line, starting from
  19770. ;; line at POS.
  19771. (lambda (pos)
  19772. (save-excursion
  19773. (goto-char pos)
  19774. (while (org-at-comment-p) (forward-line))
  19775. (skip-chars-forward " \r\t\n")
  19776. (point-at-bol)))))
  19777. beg end toggled)
  19778. ;; Determine boundaries of changes. If a universal prefix has
  19779. ;; been given, put the list in a region. If region ends at a bol,
  19780. ;; do not consider the last line to be in the region.
  19781. (when (and current-prefix-arg (org-at-item-p))
  19782. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19783. (org-mark-element))
  19784. (if (org-region-active-p)
  19785. (setq beg (funcall skip-blanks (region-beginning))
  19786. end (copy-marker (save-excursion
  19787. (goto-char (region-end))
  19788. (if (bolp) (point) (point-at-eol)))))
  19789. (setq beg (funcall skip-blanks (point-at-bol))
  19790. end (copy-marker (point-at-eol))))
  19791. ;; Ensure inline tasks don't count as headings.
  19792. (org-with-limited-levels
  19793. (save-excursion
  19794. (goto-char beg)
  19795. (cond
  19796. ;; Case 1. Started at an heading: de-star headings.
  19797. ((org-at-heading-p)
  19798. (while (< (point) end)
  19799. (when (org-at-heading-p t)
  19800. (looking-at org-outline-regexp) (replace-match "")
  19801. (setq toggled t))
  19802. (forward-line)))
  19803. ;; Case 2. Started at an item: change items into headlines.
  19804. ;; One star will be added by `org-list-to-subtree'.
  19805. ((org-at-item-p)
  19806. (while (< (point) end)
  19807. (when (org-at-item-p)
  19808. ;; Pay attention to cases when region ends before list.
  19809. (let* ((struct (org-list-struct))
  19810. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19811. (save-restriction
  19812. (narrow-to-region (point) list-end)
  19813. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19814. (setq toggled t))
  19815. (forward-line)))
  19816. ;; Case 3. Started at normal text: make every line an heading,
  19817. ;; skipping headlines and items.
  19818. (t (let* ((stars
  19819. (make-string
  19820. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19821. (add-stars
  19822. (cond (nstars "") ; stars from prefix only
  19823. ((equal stars "") "*") ; before first heading
  19824. (org-odd-levels-only "**") ; inside heading, odd
  19825. (t "*"))) ; inside heading, oddeven
  19826. (rpl (concat stars add-stars " "))
  19827. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19828. (while (< (point) (if (equal nstars '(4)) lend end))
  19829. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19830. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19831. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19832. (forward-line)))))))
  19833. (unless toggled (message "Cannot toggle heading from here"))))
  19834. (defun org-meta-return (&optional _arg)
  19835. "Insert a new heading or wrap a region in a table.
  19836. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19837. on context. See the individual commands for more information."
  19838. (interactive)
  19839. (org-check-before-invisible-edit 'insert)
  19840. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19841. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19842. #'org-insert-heading))))
  19843. ;;; Menu entries
  19844. (defsubst org-in-subtree-not-table-p ()
  19845. "Are we in a subtree and not in a table?"
  19846. (and (not (org-before-first-heading-p))
  19847. (not (org-at-table-p))))
  19848. ;; Define the Org-mode menus
  19849. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19850. '("Tbl"
  19851. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19852. ["Next Field" org-cycle (org-at-table-p)]
  19853. ["Previous Field" org-shifttab (org-at-table-p)]
  19854. ["Next Row" org-return (org-at-table-p)]
  19855. "--"
  19856. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19857. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19858. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19859. "--"
  19860. ("Column"
  19861. ["Move Column Left" org-metaleft (org-at-table-p)]
  19862. ["Move Column Right" org-metaright (org-at-table-p)]
  19863. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19864. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19865. ("Row"
  19866. ["Move Row Up" org-metaup (org-at-table-p)]
  19867. ["Move Row Down" org-metadown (org-at-table-p)]
  19868. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19869. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19870. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19871. "--"
  19872. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19873. ("Rectangle"
  19874. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19875. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19876. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19877. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19878. "--"
  19879. ("Calculate"
  19880. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19881. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19882. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19883. "--"
  19884. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19885. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19886. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19887. "--"
  19888. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19889. "--"
  19890. ["Sum Column/Rectangle" org-table-sum
  19891. (or (org-at-table-p) (org-region-active-p))]
  19892. ["Which Column?" org-table-current-column (org-at-table-p)])
  19893. ["Debug Formulas"
  19894. org-table-toggle-formula-debugger
  19895. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19896. ["Show Col/Row Numbers"
  19897. org-table-toggle-coordinate-overlays
  19898. :style toggle
  19899. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19900. "--"
  19901. ["Create" org-table-create (and (not (org-at-table-p))
  19902. org-enable-table-editor)]
  19903. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19904. ["Import from File" org-table-import (not (org-at-table-p))]
  19905. ["Export to File" org-table-export (org-at-table-p)]
  19906. "--"
  19907. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19908. "--"
  19909. ("Plot"
  19910. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19911. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19912. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19913. '("Org"
  19914. ("Show/Hide"
  19915. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19916. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19917. ["Sparse Tree..." org-sparse-tree t]
  19918. ["Reveal Context" org-reveal t]
  19919. ["Show All" show-all t]
  19920. "--"
  19921. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19922. "--"
  19923. ["New Heading" org-insert-heading t]
  19924. ("Navigate Headings"
  19925. ["Up" outline-up-heading t]
  19926. ["Next" outline-next-visible-heading t]
  19927. ["Previous" outline-previous-visible-heading t]
  19928. ["Next Same Level" outline-forward-same-level t]
  19929. ["Previous Same Level" outline-backward-same-level t]
  19930. "--"
  19931. ["Jump" org-goto t])
  19932. ("Edit Structure"
  19933. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19934. "--"
  19935. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19936. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19937. "--"
  19938. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19939. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19940. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19941. "--"
  19942. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19943. "--"
  19944. ["Copy visible text" org-copy-visible t]
  19945. "--"
  19946. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19947. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19948. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19949. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19950. "--"
  19951. ["Sort Region/Children" org-sort t]
  19952. "--"
  19953. ["Convert to odd levels" org-convert-to-odd-levels t]
  19954. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19955. ("Editing"
  19956. ["Emphasis..." org-emphasize t]
  19957. ["Edit Source Example" org-edit-special t]
  19958. "--"
  19959. ["Footnote new/jump" org-footnote-action t]
  19960. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19961. ("Archive"
  19962. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19963. "--"
  19964. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19965. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19966. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19967. )
  19968. "--"
  19969. ("Hyperlinks"
  19970. ["Store Link (Global)" org-store-link t]
  19971. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19972. ["Insert Link" org-insert-link t]
  19973. ["Follow Link" org-open-at-point t]
  19974. "--"
  19975. ["Next link" org-next-link t]
  19976. ["Previous link" org-previous-link t]
  19977. "--"
  19978. ["Descriptive Links"
  19979. org-toggle-link-display
  19980. :style radio
  19981. :selected org-descriptive-links
  19982. ]
  19983. ["Literal Links"
  19984. org-toggle-link-display
  19985. :style radio
  19986. :selected (not org-descriptive-links)])
  19987. "--"
  19988. ("TODO Lists"
  19989. ["TODO/DONE/-" org-todo t]
  19990. ("Select keyword"
  19991. ["Next keyword" org-shiftright (org-at-heading-p)]
  19992. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19993. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19994. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19995. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19996. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19997. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19998. "--"
  19999. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20000. :selected org-enforce-todo-dependencies :style toggle :active t]
  20001. "Settings for tree at point"
  20002. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20003. :selected (org-entry-get nil "ORDERED")
  20004. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20005. ["Do Children parallel" org-toggle-ordered-property :style radio
  20006. :selected (not (org-entry-get nil "ORDERED"))
  20007. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20008. "--"
  20009. ["Set Priority" org-priority t]
  20010. ["Priority Up" org-shiftup t]
  20011. ["Priority Down" org-shiftdown t]
  20012. "--"
  20013. ["Get news from all feeds" org-feed-update-all t]
  20014. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20015. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20016. ("TAGS and Properties"
  20017. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20018. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20019. "--"
  20020. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20021. ["Column view of properties" org-columns t]
  20022. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20023. ("Dates and Scheduling"
  20024. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20025. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20026. ("Change Date"
  20027. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20028. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20029. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20030. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20031. ["Compute Time Range" org-evaluate-time-range t]
  20032. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20033. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20034. "--"
  20035. ["Custom time format" org-toggle-time-stamp-overlays
  20036. :style radio :selected org-display-custom-times]
  20037. "--"
  20038. ["Goto Calendar" org-goto-calendar t]
  20039. ["Date from Calendar" org-date-from-calendar t]
  20040. "--"
  20041. ["Start/Restart Timer" org-timer-start t]
  20042. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20043. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20044. ["Insert Timer String" org-timer t]
  20045. ["Insert Timer Item" org-timer-item t])
  20046. ("Logging work"
  20047. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20048. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20049. ["Clock out" org-clock-out t]
  20050. ["Clock cancel" org-clock-cancel t]
  20051. "--"
  20052. ["Mark as default task" org-clock-mark-default-task t]
  20053. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20054. ["Goto running clock" org-clock-goto t]
  20055. "--"
  20056. ["Display times" org-clock-display t]
  20057. ["Create clock table" org-clock-report t]
  20058. "--"
  20059. ["Record DONE time"
  20060. (progn (setq org-log-done (not org-log-done))
  20061. (message "Switching to %s will %s record a timestamp"
  20062. (car org-done-keywords)
  20063. (if org-log-done "automatically" "not")))
  20064. :style toggle :selected org-log-done])
  20065. "--"
  20066. ["Agenda Command..." org-agenda t]
  20067. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20068. ("File List for Agenda")
  20069. ("Special views current file"
  20070. ["TODO Tree" org-show-todo-tree t]
  20071. ["Check Deadlines" org-check-deadlines t]
  20072. ["Timeline" org-timeline t]
  20073. ["Tags/Property tree" org-match-sparse-tree t])
  20074. "--"
  20075. ["Export/Publish..." org-export-dispatch t]
  20076. ("LaTeX"
  20077. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20078. :selected org-cdlatex-mode]
  20079. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20080. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20081. ["Modify math symbol" org-cdlatex-math-modify
  20082. (org-inside-LaTeX-fragment-p)]
  20083. ["Insert citation" org-reftex-citation t]
  20084. "--"
  20085. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20086. "--"
  20087. ("MobileOrg"
  20088. ["Push Files and Views" org-mobile-push t]
  20089. ["Get Captured and Flagged" org-mobile-pull t]
  20090. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20091. "--"
  20092. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20093. "--"
  20094. ("Documentation"
  20095. ["Show Version" org-version t]
  20096. ["Info Documentation" org-info t])
  20097. ("Customize"
  20098. ["Browse Org Group" org-customize t]
  20099. "--"
  20100. ["Expand This Menu" org-create-customize-menu
  20101. (fboundp 'customize-menu-create)])
  20102. ["Send bug report" org-submit-bug-report t]
  20103. "--"
  20104. ("Refresh/Reload"
  20105. ["Refresh setup current buffer" org-mode-restart t]
  20106. ["Reload Org (after update)" org-reload t]
  20107. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20108. ))
  20109. (defun org-info (&optional node)
  20110. "Read documentation for Org-mode in the info system.
  20111. With optional NODE, go directly to that node."
  20112. (interactive)
  20113. (info (format "(org)%s" (or node ""))))
  20114. ;;;###autoload
  20115. (defun org-submit-bug-report ()
  20116. "Submit a bug report on Org-mode via mail.
  20117. Don't hesitate to report any problems or inaccurate documentation.
  20118. If you don't have setup sending mail from (X)Emacs, please copy the
  20119. output buffer into your mail program, as it gives us important
  20120. information about your Org-mode version and configuration."
  20121. (interactive)
  20122. (require 'reporter)
  20123. (defvar reporter-prompt-for-summary-p)
  20124. (org-load-modules-maybe)
  20125. (org-require-autoloaded-modules)
  20126. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20127. (reporter-submit-bug-report
  20128. "emacs-orgmode@gnu.org"
  20129. (org-version nil 'full)
  20130. (let (list)
  20131. (save-window-excursion
  20132. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20133. (delete-other-windows)
  20134. (erase-buffer)
  20135. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20136. We would like to add your full Org-mode and Outline configuration to the
  20137. bug report. This greatly simplifies the work of the maintainer and
  20138. other experts on the mailing list.
  20139. HOWEVER, some variables you have customized may contain private
  20140. information. The names of customers, colleagues, or friends, might
  20141. appear in the form of file names, tags, todo states, or search strings.
  20142. If you answer yes to the prompt, you might want to check and remove
  20143. such private information before sending the email.")
  20144. (add-text-properties (point-min) (point-max) '(face org-warning))
  20145. (when (yes-or-no-p "Include your Org-mode configuration ")
  20146. (mapatoms
  20147. (lambda (v)
  20148. (and (boundp v)
  20149. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20150. (or (and (symbol-value v)
  20151. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20152. (and
  20153. (get v 'custom-type) (get v 'standard-value)
  20154. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20155. (push v list)))))
  20156. (kill-buffer (get-buffer "*Warn about privacy*"))
  20157. list))
  20158. nil nil
  20159. "Remember to cover the basics, that is, what you expected to happen and
  20160. what in fact did happen. You don't know how to make a good report? See
  20161. http://orgmode.org/manual/Feedback.html#Feedback
  20162. Your bug report will be posted to the Org-mode mailing list.
  20163. ------------------------------------------------------------------------")
  20164. (save-excursion
  20165. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20166. (replace-match "\\1Bug: \\3 [\\2]")))))
  20167. (defun org-install-agenda-files-menu ()
  20168. (let ((bl (buffer-list)))
  20169. (save-excursion
  20170. (while bl
  20171. (set-buffer (pop bl))
  20172. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20173. (when (derived-mode-p 'org-mode)
  20174. (easy-menu-change
  20175. '("Org") "File List for Agenda"
  20176. (append
  20177. (list
  20178. ["Edit File List" (org-edit-agenda-file-list) t]
  20179. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20180. ["Remove Current File from List" org-remove-file t]
  20181. ["Cycle through agenda files" org-cycle-agenda-files t]
  20182. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20183. "--")
  20184. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20185. ;;;; Documentation
  20186. (defun org-require-autoloaded-modules ()
  20187. (interactive)
  20188. (mapc 'require
  20189. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20190. org-table org-timer)))
  20191. ;;;###autoload
  20192. (defun org-reload (&optional uncompiled)
  20193. "Reload all org lisp files.
  20194. With prefix arg UNCOMPILED, load the uncompiled versions."
  20195. (interactive "P")
  20196. (require 'loadhist)
  20197. (let* ((org-dir (org-find-library-dir "org"))
  20198. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20199. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20200. (remove-re (mapconcat 'identity
  20201. (mapcar (lambda (f) (concat "^" f "$"))
  20202. (list (if (featurep 'xemacs)
  20203. "org-colview"
  20204. "org-colview-xemacs")
  20205. "org" "org-loaddefs" "org-version"))
  20206. "\\|"))
  20207. (feats (delete-dups
  20208. (mapcar 'file-name-sans-extension
  20209. (mapcar 'file-name-nondirectory
  20210. (delq nil
  20211. (mapcar 'feature-file
  20212. features))))))
  20213. (lfeat (append
  20214. (sort
  20215. (setq feats
  20216. (delq nil (mapcar
  20217. (lambda (f)
  20218. (if (and (string-match feature-re f)
  20219. (not (string-match remove-re f)))
  20220. f nil))
  20221. feats)))
  20222. 'string-lessp)
  20223. (list "org-version" "org")))
  20224. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20225. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20226. load-uncore load-misses)
  20227. (setq load-misses
  20228. (delq 't
  20229. (mapcar (lambda (f)
  20230. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20231. (and (string= org-dir contrib-dir)
  20232. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20233. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20234. (add-to-list 'load-uncore f 'append)
  20235. 't)
  20236. f))
  20237. lfeat)))
  20238. (if load-uncore
  20239. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20240. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20241. (if load-misses
  20242. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20243. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20244. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20245. ;;;###autoload
  20246. (defun org-customize ()
  20247. "Call the customize function with org as argument."
  20248. (interactive)
  20249. (org-load-modules-maybe)
  20250. (org-require-autoloaded-modules)
  20251. (customize-browse 'org))
  20252. (defun org-create-customize-menu ()
  20253. "Create a full customization menu for Org-mode, insert it into the menu."
  20254. (interactive)
  20255. (org-load-modules-maybe)
  20256. (org-require-autoloaded-modules)
  20257. (if (fboundp 'customize-menu-create)
  20258. (progn
  20259. (easy-menu-change
  20260. '("Org") "Customize"
  20261. `(["Browse Org group" org-customize t]
  20262. "--"
  20263. ,(customize-menu-create 'org)
  20264. ["Set" Custom-set t]
  20265. ["Save" Custom-save t]
  20266. ["Reset to Current" Custom-reset-current t]
  20267. ["Reset to Saved" Custom-reset-saved t]
  20268. ["Reset to Standard Settings" Custom-reset-standard t]))
  20269. (message "\"Org\"-menu now contains full customization menu"))
  20270. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20271. ;;;; Miscellaneous stuff
  20272. ;;; Generally useful functions
  20273. (defsubst org-get-at-eol (property n)
  20274. "Get text property PROPERTY at the end of line less N characters."
  20275. (get-text-property (- (point-at-eol) n) property))
  20276. (defun org-find-text-property-in-string (prop s)
  20277. "Return the first non-nil value of property PROP in string S."
  20278. (or (get-text-property 0 prop s)
  20279. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20280. prop s)))
  20281. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20282. "Display the given MESSAGE as a warning."
  20283. (if (fboundp 'display-warning)
  20284. (display-warning 'org message
  20285. (if (featurep 'xemacs) 'warning :warning))
  20286. (let ((buf (get-buffer-create "*Org warnings*")))
  20287. (with-current-buffer buf
  20288. (goto-char (point-max))
  20289. (insert "Warning (Org): " message)
  20290. (unless (bolp)
  20291. (newline)))
  20292. (display-buffer buf)
  20293. (sit-for 0))))
  20294. (defun org-eval (form)
  20295. "Eval FORM and return result."
  20296. (condition-case error
  20297. (eval form)
  20298. (error (format "%%![Error: %s]" error))))
  20299. (defun org-in-clocktable-p ()
  20300. "Check if the cursor is in a clocktable."
  20301. (let ((pos (point)) start)
  20302. (save-excursion
  20303. (end-of-line 1)
  20304. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20305. (setq start (match-beginning 0))
  20306. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20307. (>= (match-end 0) pos)
  20308. start))))
  20309. (defun org-in-verbatim-emphasis ()
  20310. (save-match-data
  20311. (and (org-in-regexp org-emph-re 2)
  20312. (>= (point) (match-beginning 3))
  20313. (<= (point) (match-end 4))
  20314. (member (match-string 3) '("=" "~")))))
  20315. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20316. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20317. (if (and marker (marker-buffer marker)
  20318. (buffer-live-p (marker-buffer marker)))
  20319. (progn
  20320. (org-pop-to-buffer-same-window (marker-buffer marker))
  20321. (if (or (> marker (point-max)) (< marker (point-min)))
  20322. (widen))
  20323. (goto-char marker)
  20324. (org-show-context 'org-goto))
  20325. (if bookmark
  20326. (bookmark-jump bookmark)
  20327. (error "Cannot find location"))))
  20328. (defun org-quote-csv-field (s)
  20329. "Quote field for inclusion in CSV material."
  20330. (if (string-match "[\",]" s)
  20331. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20332. s))
  20333. (defun org-force-self-insert (N)
  20334. "Needed to enforce self-insert under remapping."
  20335. (interactive "p")
  20336. (self-insert-command N))
  20337. (defun org-string-width (s)
  20338. "Compute width of string, ignoring invisible characters.
  20339. This ignores character with invisibility property `org-link', and also
  20340. characters with property `org-cwidth', because these will become invisible
  20341. upon the next fontification round."
  20342. (let (b l)
  20343. (when (or (eq t buffer-invisibility-spec)
  20344. (assq 'org-link buffer-invisibility-spec))
  20345. (while (setq b (text-property-any 0 (length s)
  20346. 'invisible 'org-link s))
  20347. (setq s (concat (substring s 0 b)
  20348. (substring s (or (next-single-property-change
  20349. b 'invisible s)
  20350. (length s)))))))
  20351. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20352. (setq s (concat (substring s 0 b)
  20353. (substring s (or (next-single-property-change
  20354. b 'org-cwidth s)
  20355. (length s))))))
  20356. (setq l (string-width s) b -1)
  20357. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20358. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20359. l))
  20360. (defun org-shorten-string (s maxlength)
  20361. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20362. If the string is shorter or has length MAXLENGTH, just return the
  20363. original string. If it is longer, the functions finds a space in the
  20364. string, breaks this string off at that locations and adds three dots
  20365. as ellipsis. Including the ellipsis, the string will not be longer
  20366. than MAXLENGTH. If finding a good breaking point in the string does
  20367. not work, the string is just chopped off in the middle of a word
  20368. if necessary."
  20369. (if (<= (length s) maxlength)
  20370. s
  20371. (let* ((n (max (- maxlength 4) 1))
  20372. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20373. (if (string-match re s)
  20374. (concat (match-string 1 s) "...")
  20375. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20376. (defun org-get-indentation (&optional line)
  20377. "Get the indentation of the current line, interpreting tabs.
  20378. When LINE is given, assume it represents a line and compute its indentation."
  20379. (if line
  20380. (if (string-match "^ *" (org-remove-tabs line))
  20381. (match-end 0))
  20382. (save-excursion
  20383. (beginning-of-line 1)
  20384. (skip-chars-forward " \t")
  20385. (current-column))))
  20386. (defun org-get-string-indentation (s)
  20387. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20388. (let ((n -1) (i 0) (w tab-width) c)
  20389. (catch 'exit
  20390. (while (< (setq n (1+ n)) (length s))
  20391. (setq c (aref s n))
  20392. (cond ((= c ?\ ) (setq i (1+ i)))
  20393. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20394. (t (throw 'exit t)))))
  20395. i))
  20396. (defun org-remove-tabs (s &optional width)
  20397. "Replace tabulators in S with spaces.
  20398. Assumes that s is a single line, starting in column 0."
  20399. (setq width (or width tab-width))
  20400. (while (string-match "\t" s)
  20401. (setq s (replace-match
  20402. (make-string
  20403. (- (* width (/ (+ (match-beginning 0) width) width))
  20404. (match-beginning 0)) ?\ )
  20405. t t s)))
  20406. s)
  20407. (defun org-fix-indentation (line ind)
  20408. "Fix indentation in LINE.
  20409. IND is a cons cell with target and minimum indentation.
  20410. If the current indentation in LINE is smaller than the minimum,
  20411. leave it alone. If it is larger than ind, set it to the target."
  20412. (let* ((l (org-remove-tabs line))
  20413. (i (org-get-indentation l))
  20414. (i1 (car ind)) (i2 (cdr ind)))
  20415. (if (>= i i2) (setq l (substring line i2)))
  20416. (if (> i1 0)
  20417. (concat (make-string i1 ?\ ) l)
  20418. l)))
  20419. (defun org-remove-indentation (code &optional n)
  20420. "Remove the maximum common indentation from the lines in CODE.
  20421. N may optionally be the number of spaces to remove."
  20422. (with-temp-buffer
  20423. (insert code)
  20424. (org-do-remove-indentation n)
  20425. (buffer-string)))
  20426. (defun org-do-remove-indentation (&optional n)
  20427. "Remove the maximum common indentation from the buffer."
  20428. (untabify (point-min) (point-max))
  20429. (let ((min 10000) re)
  20430. (if n
  20431. (setq min n)
  20432. (goto-char (point-min))
  20433. (while (re-search-forward "^ *[^ \n]" nil t)
  20434. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20435. (unless (or (= min 0) (= min 10000))
  20436. (setq re (format "^ \\{%d\\}" min))
  20437. (goto-char (point-min))
  20438. (while (re-search-forward re nil t)
  20439. (replace-match "")
  20440. (end-of-line 1))
  20441. min)))
  20442. (defun org-fill-template (template alist)
  20443. "Find each %key of ALIST in TEMPLATE and replace it."
  20444. (let ((case-fold-search nil))
  20445. (dolist (entry (sort (copy-sequence alist)
  20446. (lambda (a b) (< (length (car a)) (length (car b))))))
  20447. (setq template
  20448. (replace-regexp-in-string
  20449. (concat "%" (regexp-quote (car entry)))
  20450. (or (cdr entry) "") template t t)))
  20451. template))
  20452. (defun org-base-buffer (buffer)
  20453. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20454. (if (not buffer)
  20455. buffer
  20456. (or (buffer-base-buffer buffer)
  20457. buffer)))
  20458. (defun org-wrap (string &optional width lines)
  20459. "Wrap string to either a number of lines, or a width in characters.
  20460. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20461. that costs. If there is a word longer than WIDTH, the text is actually
  20462. wrapped to the length of that word.
  20463. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20464. many lines, whatever width that takes.
  20465. The return value is a list of lines, without newlines at the end."
  20466. (let* ((words (org-split-string string "[ \t\n]+"))
  20467. (maxword (apply 'max (mapcar 'org-string-width words)))
  20468. w ll)
  20469. (cond (width
  20470. (org-do-wrap words (max maxword width)))
  20471. (lines
  20472. (setq w maxword)
  20473. (setq ll (org-do-wrap words maxword))
  20474. (if (<= (length ll) lines)
  20475. ll
  20476. (setq ll words)
  20477. (while (> (length ll) lines)
  20478. (setq w (1+ w))
  20479. (setq ll (org-do-wrap words w)))
  20480. ll))
  20481. (t (error "Cannot wrap this")))))
  20482. (defun org-do-wrap (words width)
  20483. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20484. (let (lines line)
  20485. (while words
  20486. (setq line (pop words))
  20487. (while (and words (< (+ (length line) (length (car words))) width))
  20488. (setq line (concat line " " (pop words))))
  20489. (setq lines (push line lines)))
  20490. (nreverse lines)))
  20491. (defun org-split-string (string &optional separators)
  20492. "Splits STRING into substrings at SEPARATORS.
  20493. SEPARATORS is a regular expression.
  20494. No empty strings are returned if there are matches at the beginning
  20495. and end of string."
  20496. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20497. (let ((start 0) notfirst list)
  20498. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20499. (if (and notfirst
  20500. (= start (match-beginning 0))
  20501. (< start (length string)))
  20502. (1+ start) start))
  20503. (< (match-beginning 0) (length string)))
  20504. (setq notfirst t)
  20505. (or (eq (match-beginning 0) 0)
  20506. (and (eq (match-beginning 0) (match-end 0))
  20507. (eq (match-beginning 0) start))
  20508. (push (substring string start (match-beginning 0)) list))
  20509. (setq start (match-end 0)))
  20510. (or (eq start (length string))
  20511. (push (substring string start) list))
  20512. (nreverse list)))
  20513. (defun org-quote-vert (s)
  20514. "Replace \"|\" with \"\\vert\"."
  20515. (while (string-match "|" s)
  20516. (setq s (replace-match "\\vert" t t s)))
  20517. s)
  20518. (defun org-uuidgen-p (s)
  20519. "Is S an ID created by UUIDGEN?"
  20520. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20521. (defun org-in-src-block-p (&optional inside)
  20522. "Whether point is in a code source block.
  20523. When INSIDE is non-nil, don't consider we are within a src block
  20524. when point is at #+BEGIN_SRC or #+END_SRC."
  20525. (let ((case-fold-search t) ov)
  20526. (or (and (eq (get-char-property (point) 'src-block) t))
  20527. (and (not inside)
  20528. (save-match-data
  20529. (save-excursion
  20530. (beginning-of-line)
  20531. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20532. (defun org-context ()
  20533. "Return a list of contexts of the current cursor position.
  20534. If several contexts apply, all are returned.
  20535. Each context entry is a list with a symbol naming the context, and
  20536. two positions indicating start and end of the context. Possible
  20537. contexts are:
  20538. :headline anywhere in a headline
  20539. :headline-stars on the leading stars in a headline
  20540. :todo-keyword on a TODO keyword (including DONE) in a headline
  20541. :tags on the TAGS in a headline
  20542. :priority on the priority cookie in a headline
  20543. :item on the first line of a plain list item
  20544. :item-bullet on the bullet/number of a plain list item
  20545. :checkbox on the checkbox in a plain list item
  20546. :table in an org-mode table
  20547. :table-special on a special filed in a table
  20548. :table-table in a table.el table
  20549. :clocktable in a clocktable
  20550. :src-block in a source block
  20551. :link on a hyperlink
  20552. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20553. :target on a <<target>>
  20554. :radio-target on a <<<radio-target>>>
  20555. :latex-fragment on a LaTeX fragment
  20556. :latex-preview on a LaTeX fragment with overlaid preview image
  20557. This function expects the position to be visible because it uses font-lock
  20558. faces as a help to recognize the following contexts: :table-special, :link,
  20559. and :keyword."
  20560. (let* ((f (get-text-property (point) 'face))
  20561. (faces (if (listp f) f (list f)))
  20562. (case-fold-search t)
  20563. (p (point)) clist o)
  20564. ;; First the large context
  20565. (cond
  20566. ((org-at-heading-p t)
  20567. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20568. (when (progn
  20569. (beginning-of-line 1)
  20570. (looking-at org-todo-line-tags-regexp))
  20571. (push (org-point-in-group p 1 :headline-stars) clist)
  20572. (push (org-point-in-group p 2 :todo-keyword) clist)
  20573. (push (org-point-in-group p 4 :tags) clist))
  20574. (goto-char p)
  20575. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20576. (if (looking-at "\\[#[A-Z0-9]\\]")
  20577. (push (org-point-in-group p 0 :priority) clist)))
  20578. ((org-at-item-p)
  20579. (push (org-point-in-group p 2 :item-bullet) clist)
  20580. (push (list :item (point-at-bol)
  20581. (save-excursion (org-end-of-item) (point)))
  20582. clist)
  20583. (and (org-at-item-checkbox-p)
  20584. (push (org-point-in-group p 0 :checkbox) clist)))
  20585. ((org-at-table-p)
  20586. (push (list :table (org-table-begin) (org-table-end)) clist)
  20587. (if (memq 'org-formula faces)
  20588. (push (list :table-special
  20589. (previous-single-property-change p 'face)
  20590. (next-single-property-change p 'face)) clist)))
  20591. ((org-at-table-p 'any)
  20592. (push (list :table-table) clist)))
  20593. (goto-char p)
  20594. (let ((case-fold-search t))
  20595. ;; New the "medium" contexts: clocktables, source blocks
  20596. (cond ((org-in-clocktable-p)
  20597. (push (list :clocktable
  20598. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20599. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20600. (match-beginning 1))
  20601. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20602. (match-end 0))) clist))
  20603. ((org-in-src-block-p)
  20604. (push (list :src-block
  20605. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20606. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20607. (match-beginning 1))
  20608. (and (search-forward "#+END_SRC" nil t)
  20609. (match-beginning 0))) clist))))
  20610. (goto-char p)
  20611. ;; Now the small context
  20612. (cond
  20613. ((org-at-timestamp-p)
  20614. (push (org-point-in-group p 0 :timestamp) clist))
  20615. ((memq 'org-link faces)
  20616. (push (list :link
  20617. (previous-single-property-change p 'face)
  20618. (next-single-property-change p 'face)) clist))
  20619. ((memq 'org-special-keyword faces)
  20620. (push (list :keyword
  20621. (previous-single-property-change p 'face)
  20622. (next-single-property-change p 'face)) clist))
  20623. ((org-at-target-p)
  20624. (push (org-point-in-group p 0 :target) clist)
  20625. (goto-char (1- (match-beginning 0)))
  20626. (if (looking-at org-radio-target-regexp)
  20627. (push (org-point-in-group p 0 :radio-target) clist))
  20628. (goto-char p))
  20629. ((setq o (car (delq nil
  20630. (mapcar
  20631. (lambda (x)
  20632. (if (memq x org-latex-fragment-image-overlays) x))
  20633. (overlays-at (point))))))
  20634. (push (list :latex-fragment
  20635. (overlay-start o) (overlay-end o)) clist)
  20636. (push (list :latex-preview
  20637. (overlay-start o) (overlay-end o)) clist))
  20638. ((org-inside-LaTeX-fragment-p)
  20639. ;; FIXME: positions wrong.
  20640. (push (list :latex-fragment (point) (point)) clist)))
  20641. (setq clist (nreverse (delq nil clist)))
  20642. clist))
  20643. (defun org-in-regexp (re &optional nlines visually)
  20644. "Check if point is inside a match of RE.
  20645. Normally only the current line is checked, but you can include
  20646. NLINES extra lines after point into the search. If VISUALLY is
  20647. set, require that the cursor is not after the match but really
  20648. on, so that the block visually is on the match."
  20649. (catch 'exit
  20650. (let ((pos (point))
  20651. (eol (point-at-eol (+ 1 (or nlines 0))))
  20652. (inc (if visually 1 0)))
  20653. (save-excursion
  20654. (beginning-of-line (- 1 (or nlines 0)))
  20655. (while (re-search-forward re eol t)
  20656. (if (and (<= (match-beginning 0) pos)
  20657. (>= (+ inc (match-end 0)) pos))
  20658. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20659. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20660. "Org mode 8.3")
  20661. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20662. "Non-nil when point is between matches of START-RE and END-RE.
  20663. Also return a non-nil value when point is on one of the matches.
  20664. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20665. buffer positions. Default values are the positions of headlines
  20666. surrounding the point.
  20667. The functions returns a cons cell whose car (resp. cdr) is the
  20668. position before START-RE (resp. after END-RE)."
  20669. (save-match-data
  20670. (let ((pos (point))
  20671. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20672. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20673. beg end)
  20674. (save-excursion
  20675. ;; Point is on a block when on START-RE or if START-RE can be
  20676. ;; found before it...
  20677. (and (or (org-in-regexp start-re)
  20678. (re-search-backward start-re limit-up t))
  20679. (setq beg (match-beginning 0))
  20680. ;; ... and END-RE after it...
  20681. (goto-char (match-end 0))
  20682. (re-search-forward end-re limit-down t)
  20683. (> (setq end (match-end 0)) pos)
  20684. ;; ... without another START-RE in-between.
  20685. (goto-char (match-beginning 0))
  20686. (not (re-search-backward start-re (1+ beg) t))
  20687. ;; Return value.
  20688. (cons beg end))))))
  20689. (defun org-in-block-p (names)
  20690. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20691. NAMES is a list of strings containing names of blocks.
  20692. Return first block name matched, or nil. Beware that in case of
  20693. nested blocks, the returned name may not belong to the closest
  20694. block from point."
  20695. (save-match-data
  20696. (catch 'exit
  20697. (let ((case-fold-search t)
  20698. (lim-up (save-excursion (outline-previous-heading)))
  20699. (lim-down (save-excursion (outline-next-heading))))
  20700. (mapc (lambda (name)
  20701. (let ((n (regexp-quote name)))
  20702. (when (org-between-regexps-p
  20703. (concat "^[ \t]*#\\+begin_" n)
  20704. (concat "^[ \t]*#\\+end_" n)
  20705. lim-up lim-down)
  20706. (throw 'exit n))))
  20707. names))
  20708. nil)))
  20709. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20710. "Call `multi-occur' with buffers for all agenda files."
  20711. (interactive "sOrg-files matching: ")
  20712. (let* ((files (org-agenda-files))
  20713. (tnames (mapcar #'file-truename files))
  20714. (extra org-agenda-text-search-extra-files))
  20715. (when (eq (car extra) 'agenda-archives)
  20716. (setq extra (cdr extra))
  20717. (setq files (org-add-archive-files files)))
  20718. (dolist (f extra)
  20719. (unless (member (file-truename f) tnames)
  20720. (unless (member f files) (setq files (append files (list f))))
  20721. (setq tnames (append tnames (list (file-truename f))))))
  20722. (multi-occur
  20723. (mapcar (lambda (x)
  20724. (with-current-buffer
  20725. ;; FIXME: Why not just (find-file-noselect x)?
  20726. ;; Is it to avoid the "revert buffer" prompt?
  20727. (or (get-file-buffer x) (find-file-noselect x))
  20728. (widen)
  20729. (current-buffer)))
  20730. files)
  20731. regexp)))
  20732. (if (boundp 'occur-mode-find-occurrence-hook)
  20733. ;; Emacs 23
  20734. (add-hook 'occur-mode-find-occurrence-hook
  20735. (lambda ()
  20736. (when (derived-mode-p 'org-mode)
  20737. (org-reveal))))
  20738. ;; Emacs 22
  20739. (defadvice occur-mode-goto-occurrence
  20740. (after org-occur-reveal activate)
  20741. (and (derived-mode-p 'org-mode) (org-reveal)))
  20742. (defadvice occur-mode-goto-occurrence-other-window
  20743. (after org-occur-reveal activate)
  20744. (and (derived-mode-p 'org-mode) (org-reveal)))
  20745. (defadvice occur-mode-display-occurrence
  20746. (after org-occur-reveal activate)
  20747. (when (derived-mode-p 'org-mode)
  20748. (let ((pos (occur-mode-find-occurrence)))
  20749. (with-current-buffer (marker-buffer pos)
  20750. (save-excursion
  20751. (goto-char pos)
  20752. (org-reveal)))))))
  20753. (defun org-occur-link-in-agenda-files ()
  20754. "Create a link and search for it in the agendas.
  20755. The link is not stored in `org-stored-links', it is just created
  20756. for the search purpose."
  20757. (interactive)
  20758. (let ((link (condition-case nil
  20759. (org-store-link nil)
  20760. (error "Unable to create a link to here"))))
  20761. (org-occur-in-agenda-files (regexp-quote link))))
  20762. (defun org-reverse-string (string)
  20763. "Return the reverse of STRING."
  20764. (apply 'string (reverse (string-to-list string))))
  20765. ;; defsubst org-uniquify must be defined before first use
  20766. (defun org-uniquify-alist (alist)
  20767. "Merge elements of ALIST with the same key.
  20768. For example, in this alist:
  20769. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20770. => '((a 1 3) (b 2))
  20771. merge (a 1) and (a 3) into (a 1 3).
  20772. The function returns the new ALIST."
  20773. (let (rtn)
  20774. (mapc
  20775. (lambda (e)
  20776. (let (n)
  20777. (if (not (assoc (car e) rtn))
  20778. (push e rtn)
  20779. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20780. (setq rtn (assq-delete-all (car e) rtn))
  20781. (push n rtn))))
  20782. alist)
  20783. rtn))
  20784. (defun org-delete-all (elts list)
  20785. "Remove all elements in ELTS from LIST."
  20786. (while elts
  20787. (setq list (delete (pop elts) list)))
  20788. list)
  20789. (defun org-count (cl-item cl-seq)
  20790. "Count the number of occurrences of ITEM in SEQ.
  20791. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20792. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20793. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20794. (while (< cl-start cl-end)
  20795. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20796. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20797. (setq cl-start (1+ cl-start)))
  20798. cl-count))
  20799. (defun org-remove-if (predicate seq)
  20800. "Remove everything from SEQ that fulfills PREDICATE."
  20801. (let (res e)
  20802. (while seq
  20803. (setq e (pop seq))
  20804. (if (not (funcall predicate e)) (push e res)))
  20805. (nreverse res)))
  20806. (defun org-remove-if-not (predicate seq)
  20807. "Remove everything from SEQ that does not fulfill PREDICATE."
  20808. (let (res e)
  20809. (while seq
  20810. (setq e (pop seq))
  20811. (if (funcall predicate e) (push e res)))
  20812. (nreverse res)))
  20813. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20814. "Reduce two-argument FUNCTION across SEQ.
  20815. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20816. \":initial-value\" removed."
  20817. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20818. (cadr (memq :initial-value cl-keys)))
  20819. (cl-seq (pop cl-seq))
  20820. (t (funcall cl-func)))))
  20821. (while cl-seq
  20822. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20823. cl-accum))
  20824. (defun org-every (pred seq)
  20825. "Return true if PREDICATE is true of every element of SEQ.
  20826. Adapted from `every' in cl.el."
  20827. (catch 'org-every
  20828. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20829. t))
  20830. (defun org-some (pred seq)
  20831. "Return true if PREDICATE is true of any element of SEQ.
  20832. Adapted from `some' in cl.el."
  20833. (catch 'org-some
  20834. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20835. nil))
  20836. (defun org-back-over-empty-lines ()
  20837. "Move backwards over whitespace, to the beginning of the first empty line.
  20838. Returns the number of empty lines passed."
  20839. (let ((pos (point)))
  20840. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20841. (skip-chars-backward " \t\n\r")
  20842. (unless (eobp)
  20843. (forward-line -1)))
  20844. (beginning-of-line 2)
  20845. (goto-char (min (point) pos))
  20846. (count-lines (point) pos)))
  20847. (defun org-skip-whitespace ()
  20848. (skip-chars-forward " \t\n\r"))
  20849. (defun org-point-in-group (point group &optional context)
  20850. "Check if POINT is in match-group GROUP.
  20851. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20852. match. If the match group does not exist or point is not inside it,
  20853. return nil."
  20854. (and (match-beginning group)
  20855. (>= point (match-beginning group))
  20856. (<= point (match-end group))
  20857. (if context
  20858. (list context (match-beginning group) (match-end group))
  20859. t)))
  20860. (defun org-switch-to-buffer-other-window (&rest args)
  20861. "Switch to buffer in a second window on the current frame.
  20862. In particular, do not allow pop-up frames.
  20863. Returns the newly created buffer."
  20864. (org-no-popups
  20865. (apply 'switch-to-buffer-other-window args)))
  20866. (defun org-combine-plists (&rest plists)
  20867. "Create a single property list from all plists in PLISTS.
  20868. The process starts by copying the first list, and then setting properties
  20869. from the other lists. Settings in the last list are the most significant
  20870. ones and overrule settings in the other lists."
  20871. (let ((rtn (copy-sequence (pop plists)))
  20872. p v ls)
  20873. (while plists
  20874. (setq ls (pop plists))
  20875. (while ls
  20876. (setq p (pop ls) v (pop ls))
  20877. (setq rtn (plist-put rtn p v))))
  20878. rtn))
  20879. (defun org-replace-escapes (string table)
  20880. "Replace %-escapes in STRING with values in TABLE.
  20881. TABLE is an association list with keys like \"%a\" and string values.
  20882. The sequences in STRING may contain normal field width and padding information,
  20883. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20884. so values can contain further %-escapes if they are define later in TABLE."
  20885. (let ((tbl (copy-alist table))
  20886. (case-fold-search nil)
  20887. (pchg 0)
  20888. e re rpl)
  20889. (dolist (e tbl)
  20890. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20891. (when (and (cdr e) (string-match re (cdr e)))
  20892. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20893. (safe "SREF"))
  20894. (add-text-properties 0 3 (list 'sref sref) safe)
  20895. (setcdr e (replace-match safe t t (cdr e)))))
  20896. (while (string-match re string)
  20897. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20898. (cdr e)))
  20899. (setq string (replace-match rpl t t string))))
  20900. (while (setq pchg (next-property-change pchg string))
  20901. (let ((sref (get-text-property pchg 'sref string)))
  20902. (when (and sref (string-match "SREF" string pchg))
  20903. (setq string (replace-match sref t t string)))))
  20904. string))
  20905. (defun org-sublist (list start end)
  20906. "Return a section of LIST, from START to END.
  20907. Counting starts at 1."
  20908. (let (rtn (c start))
  20909. (setq list (nthcdr (1- start) list))
  20910. (while (and list (<= c end))
  20911. (push (pop list) rtn)
  20912. (setq c (1+ c)))
  20913. (nreverse rtn)))
  20914. (defun org-find-base-buffer-visiting (file)
  20915. "Like `find-buffer-visiting' but always return the base buffer and
  20916. not an indirect buffer."
  20917. (let ((buf (or (get-file-buffer file)
  20918. (find-buffer-visiting file))))
  20919. (if buf
  20920. (or (buffer-base-buffer buf) buf)
  20921. nil)))
  20922. (defun org-image-file-name-regexp (&optional extensions)
  20923. "Return regexp matching the file names of images.
  20924. If EXTENSIONS is given, only match these."
  20925. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20926. (image-file-name-regexp)
  20927. (let ((image-file-name-extensions
  20928. (or extensions
  20929. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20930. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20931. (concat "\\."
  20932. (regexp-opt (nconc (mapcar 'upcase
  20933. image-file-name-extensions)
  20934. image-file-name-extensions)
  20935. t)
  20936. "\\'"))))
  20937. (defun org-file-image-p (file &optional extensions)
  20938. "Return non-nil if FILE is an image."
  20939. (save-match-data
  20940. (string-match (org-image-file-name-regexp extensions) file)))
  20941. (defun org-get-cursor-date (&optional with-time)
  20942. "Return the date at cursor in as a time.
  20943. This works in the calendar and in the agenda, anywhere else it just
  20944. returns the current time.
  20945. If WITH-TIME is non-nil, returns the time of the event at point (in
  20946. the agenda) or the current time of the day."
  20947. (let (date day defd tp hod mod)
  20948. (when with-time
  20949. (setq tp (get-text-property (point) 'time))
  20950. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20951. (setq hod (string-to-number (match-string 1 tp))
  20952. mod (string-to-number (match-string 2 tp))))
  20953. (or tp (let ((now (decode-time)))
  20954. (setq hod (nth 2 now)
  20955. mod (nth 1 now)))))
  20956. (cond
  20957. ((eq major-mode 'calendar-mode)
  20958. (setq date (calendar-cursor-to-date)
  20959. defd (encode-time 0 (or mod 0) (or hod 0)
  20960. (nth 1 date) (nth 0 date) (nth 2 date))))
  20961. ((eq major-mode 'org-agenda-mode)
  20962. (setq day (get-text-property (point) 'day))
  20963. (if day
  20964. (setq date (calendar-gregorian-from-absolute day)
  20965. defd (encode-time 0 (or mod 0) (or hod 0)
  20966. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20967. (or defd (current-time))))
  20968. (defun org-mark-subtree (&optional up)
  20969. "Mark the current subtree.
  20970. This puts point at the start of the current subtree, and mark at
  20971. the end. If a numeric prefix UP is given, move up into the
  20972. hierarchy of headlines by UP levels before marking the subtree."
  20973. (interactive "P")
  20974. (org-with-limited-levels
  20975. (cond ((org-at-heading-p) (beginning-of-line))
  20976. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20977. (t (outline-previous-visible-heading 1))))
  20978. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20979. (if (org-called-interactively-p 'any)
  20980. (call-interactively 'org-mark-element)
  20981. (org-mark-element)))
  20982. ;;; Indentation
  20983. (defun org--get-expected-indentation (element contentsp)
  20984. "Expected indentation column for current line, according to ELEMENT.
  20985. ELEMENT is an element containing point. CONTENTSP is non-nil
  20986. when indentation is to be computed according to contents of
  20987. ELEMENT."
  20988. (let ((type (org-element-type element))
  20989. (start (org-element-property :begin element))
  20990. (post-affiliated (org-element-property :post-affiliated element)))
  20991. (org-with-wide-buffer
  20992. (cond
  20993. (contentsp
  20994. (case type
  20995. ((diary-sexp footnote-definition) 0)
  20996. ((headline inlinetask nil)
  20997. (if (not org-adapt-indentation) 0
  20998. (let ((level (org-current-level)))
  20999. (if level (1+ level) 0))))
  21000. ((item plain-list) (org-list-item-body-column post-affiliated))
  21001. (t
  21002. (goto-char start)
  21003. (org-get-indentation))))
  21004. ((memq type '(headline inlinetask nil))
  21005. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21006. (org--get-expected-indentation element t)
  21007. 0))
  21008. ((memq type '(diary-sexp footnote-definition)) 0)
  21009. ;; First paragraph of a footnote definition or an item.
  21010. ;; Indent like parent.
  21011. ((< (line-beginning-position) start)
  21012. (org--get-expected-indentation
  21013. (org-element-property :parent element) t))
  21014. ;; At first line: indent according to previous sibling, if any,
  21015. ;; ignoring footnote definitions and inline tasks, or parent's
  21016. ;; contents.
  21017. ((= (line-beginning-position) start)
  21018. (catch 'exit
  21019. (while t
  21020. (if (= (point-min) start) (throw 'exit 0)
  21021. (goto-char (1- start))
  21022. (let* ((previous (org-element-at-point))
  21023. (parent previous))
  21024. (while (and parent (<= (org-element-property :end parent) start))
  21025. (setq previous parent
  21026. parent (org-element-property :parent parent)))
  21027. (cond
  21028. ((not previous) (throw 'exit 0))
  21029. ((> (org-element-property :end previous) start)
  21030. (throw 'exit (org--get-expected-indentation previous t)))
  21031. ((memq (org-element-type previous)
  21032. '(footnote-definition inlinetask))
  21033. (setq start (org-element-property :begin previous)))
  21034. (t (goto-char (org-element-property :begin previous))
  21035. (throw 'exit
  21036. (if (bolp) (org-get-indentation)
  21037. ;; At first paragraph in an item or
  21038. ;; a footnote definition.
  21039. (org--get-expected-indentation
  21040. (org-element-property :parent previous) t))))))))))
  21041. ;; Otherwise, move to the first non-blank line above.
  21042. (t
  21043. (beginning-of-line)
  21044. (let ((pos (point)))
  21045. (skip-chars-backward " \r\t\n")
  21046. (cond
  21047. ;; Two blank lines end a footnote definition or a plain
  21048. ;; list. When we indent an empty line after them, the
  21049. ;; containing list or footnote definition is over, so it
  21050. ;; qualifies as a previous sibling. Therefore, we indent
  21051. ;; like its first line.
  21052. ((and (memq type '(footnote-definition plain-list))
  21053. (> (count-lines (point) pos) 2))
  21054. (goto-char start)
  21055. (org-get-indentation))
  21056. ;; Line above is the first one of a paragraph at the
  21057. ;; beginning of an item or a footnote definition. Indent
  21058. ;; like parent.
  21059. ((< (line-beginning-position) start)
  21060. (org--get-expected-indentation
  21061. (org-element-property :parent element) t))
  21062. ;; Line above is the beginning of an element, i.e., point
  21063. ;; was originally on the blank lines between element's start
  21064. ;; and contents.
  21065. ((= (line-beginning-position) post-affiliated)
  21066. (org--get-expected-indentation element t))
  21067. ;; POS is after contents in a greater element. Indent like
  21068. ;; the beginning of the element.
  21069. ;;
  21070. ;; As a special case, if point is at the end of a footnote
  21071. ;; definition or an item, indent like the very last element
  21072. ;; within.
  21073. ((and (not (eq type 'paragraph))
  21074. (let ((cend (org-element-property :contents-end element)))
  21075. (and cend (<= cend pos))))
  21076. (if (memq type '(footnote-definition item plain-list))
  21077. (let ((last (org-element-at-point)))
  21078. (org--get-expected-indentation
  21079. last
  21080. (memq (org-element-type last)
  21081. '(footnote-definition item plain-list))))
  21082. (goto-char start)
  21083. (org-get-indentation)))
  21084. ;; In any other case, indent like the current line.
  21085. (t (org-get-indentation)))))))))
  21086. (defun org--align-node-property ()
  21087. "Align node property at point.
  21088. Alignment is done according to `org-property-format', which see."
  21089. (when (save-excursion
  21090. (beginning-of-line)
  21091. (looking-at org-property-re))
  21092. (replace-match
  21093. (concat (match-string 4)
  21094. (org-trim
  21095. (format org-property-format (match-string 1) (match-string 3))))
  21096. t t)))
  21097. (defun org-indent-line ()
  21098. "Indent line depending on context.
  21099. Indentation is done according to the following rules:
  21100. - Footnote definitions, diary sexps, headlines and inline tasks
  21101. have to start at column 0.
  21102. - On the very first line of an element, consider, in order, the
  21103. next rules until one matches:
  21104. 1. If there's a sibling element before, ignoring footnote
  21105. definitions and inline tasks, indent like its first line.
  21106. 2. If element has a parent, indent like its contents. More
  21107. precisely, if parent is an item, indent after the
  21108. description part, if any, or the bullet (see
  21109. `org-list-description-max-indent'). Else, indent like
  21110. parent's first line.
  21111. 3. Otherwise, indent relatively to current level, if
  21112. `org-adapt-indentation' is non-nil, or to left margin.
  21113. - On a blank line at the end of an element, indent according to
  21114. the type of the element. More precisely
  21115. 1. If element is a plain list, an item, or a footnote
  21116. definition, indent like the very last element within.
  21117. 2. If element is a paragraph, indent like its last non blank
  21118. line.
  21119. 3. Otherwise, indent like its very first line.
  21120. - In the code part of a source block, use language major mode
  21121. to indent current line if `org-src-tab-acts-natively' is
  21122. non-nil. If it is nil, do nothing.
  21123. - Otherwise, indent like the first non-blank line above.
  21124. The function doesn't indent an item as it could break the whole
  21125. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21126. \\[org-shiftmetaright].
  21127. Also align node properties according to `org-property-format'."
  21128. (interactive)
  21129. (cond
  21130. (orgstruct-is-++
  21131. (let ((indent-line-function
  21132. (cadadr (assq 'indent-line-function org-fb-vars))))
  21133. (indent-according-to-mode)))
  21134. ((org-at-heading-p) 'noindent)
  21135. (t
  21136. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21137. (type (org-element-type element)))
  21138. (cond ((and (memq type '(plain-list item))
  21139. (= (line-beginning-position)
  21140. (org-element-property :post-affiliated element)))
  21141. 'noindent)
  21142. ((and (eq type 'src-block)
  21143. org-src-tab-acts-natively
  21144. (> (line-beginning-position)
  21145. (org-element-property :post-affiliated element))
  21146. (< (line-beginning-position)
  21147. (org-with-wide-buffer
  21148. (goto-char (org-element-property :end element))
  21149. (skip-chars-backward " \r\t\n")
  21150. (line-beginning-position))))
  21151. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21152. (t
  21153. (let ((column (org--get-expected-indentation element nil)))
  21154. ;; Preserve current column.
  21155. (if (<= (current-column) (current-indentation))
  21156. (org-indent-line-to column)
  21157. (save-excursion (org-indent-line-to column))))
  21158. ;; Align node property. Also preserve current column.
  21159. (when (eq type 'node-property)
  21160. (let ((column (current-column)))
  21161. (org--align-node-property)
  21162. (org-move-to-column column)))))))))
  21163. (defun org-indent-region (start end)
  21164. "Indent each non-blank line in the region.
  21165. Called from a program, START and END specify the region to
  21166. indent. The function will not indent contents of example blocks,
  21167. verse blocks and export blocks as leading white spaces are
  21168. assumed to be significant there."
  21169. (interactive "r")
  21170. (save-excursion
  21171. (goto-char start)
  21172. (skip-chars-forward " \r\t\n")
  21173. (unless (eobp) (beginning-of-line))
  21174. (let ((indent-to
  21175. (lambda (ind pos)
  21176. ;; Set IND as indentation for all lines between point and
  21177. ;; POS or END, whichever comes first. Blank lines are
  21178. ;; ignored. Leave point after POS once done.
  21179. (let ((limit (copy-marker (min end pos))))
  21180. (while (< (point) limit)
  21181. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21182. (forward-line))
  21183. (set-marker limit nil))))
  21184. (end (copy-marker end)))
  21185. (while (< (point) end)
  21186. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21187. (let* ((element (org-element-at-point))
  21188. (type (org-element-type element))
  21189. (element-end (copy-marker (org-element-property :end element)))
  21190. (ind (org--get-expected-indentation element nil)))
  21191. (cond
  21192. ((or (memq type '(paragraph table table-row))
  21193. (not (or (org-element-property :contents-begin element)
  21194. (memq type
  21195. '(example-block export-block src-block)))))
  21196. ;; Elements here are indented as a single block. Also
  21197. ;; align node properties.
  21198. (when (eq type 'node-property)
  21199. (org--align-node-property)
  21200. (beginning-of-line))
  21201. (funcall indent-to ind element-end))
  21202. (t
  21203. ;; Elements in this category consist of three parts:
  21204. ;; before the contents, the contents, and after the
  21205. ;; contents. The contents are treated specially,
  21206. ;; according to the element type, or not indented at
  21207. ;; all. Other parts are indented as a single block.
  21208. (let* ((post (copy-marker
  21209. (org-element-property :post-affiliated element)))
  21210. (cbeg
  21211. (copy-marker
  21212. (cond
  21213. ((not (org-element-property :contents-begin element))
  21214. ;; Fake contents for source blocks.
  21215. (org-with-wide-buffer
  21216. (goto-char post)
  21217. (forward-line)
  21218. (point)))
  21219. ((memq type '(footnote-definition item plain-list))
  21220. ;; Contents in these elements could start on
  21221. ;; the same line as the beginning of the
  21222. ;; element. Make sure we start indenting
  21223. ;; from the second line.
  21224. (org-with-wide-buffer
  21225. (goto-char post)
  21226. (end-of-line)
  21227. (skip-chars-forward " \r\t\n")
  21228. (if (eobp) (point) (line-beginning-position))))
  21229. (t (org-element-property :contents-begin element)))))
  21230. (cend (copy-marker
  21231. (or (org-element-property :contents-end element)
  21232. ;; Fake contents for source blocks.
  21233. (org-with-wide-buffer
  21234. (goto-char element-end)
  21235. (skip-chars-backward " \r\t\n")
  21236. (line-beginning-position)))
  21237. t)))
  21238. ;; Do not change items indentation individually as it
  21239. ;; might break the list as a whole. On the other
  21240. ;; hand, when at a plain list, indent it as a whole.
  21241. (cond ((eq type 'plain-list)
  21242. (let ((offset (- ind (org-get-indentation))))
  21243. (unless (zerop offset)
  21244. (indent-rigidly (org-element-property :begin element)
  21245. (org-element-property :end element)
  21246. offset))
  21247. (goto-char cbeg)))
  21248. ((eq type 'item) (goto-char cbeg))
  21249. (t (funcall indent-to ind cbeg)))
  21250. (when (< (point) end)
  21251. (case type
  21252. ((example-block export-block verse-block))
  21253. (src-block
  21254. ;; In a source block, indent source code
  21255. ;; according to language major mode, but only if
  21256. ;; `org-src-tab-acts-natively' is non-nil.
  21257. (when (and (< (point) end) org-src-tab-acts-natively)
  21258. (ignore-errors
  21259. (org-babel-do-in-edit-buffer
  21260. (indent-region (point-min) (point-max))))))
  21261. (t (org-indent-region (point) (min cend end))))
  21262. (goto-char (min cend end))
  21263. (when (< (point) end) (funcall indent-to ind element-end)))
  21264. (set-marker post nil)
  21265. (set-marker cbeg nil)
  21266. (set-marker cend nil))))
  21267. (set-marker element-end nil))))
  21268. (set-marker end nil))))
  21269. (defun org-indent-drawer ()
  21270. "Indent the drawer at point."
  21271. (interactive)
  21272. (unless (save-excursion
  21273. (beginning-of-line)
  21274. (org-looking-at-p org-drawer-regexp))
  21275. (user-error "Not at a drawer"))
  21276. (let ((element (org-element-at-point)))
  21277. (unless (memq (org-element-type element) '(drawer property-drawer))
  21278. (user-error "Not at a drawer"))
  21279. (org-with-wide-buffer
  21280. (org-indent-region (org-element-property :begin element)
  21281. (org-element-property :end element))))
  21282. (message "Drawer at point indented"))
  21283. (defun org-indent-block ()
  21284. "Indent the block at point."
  21285. (interactive)
  21286. (unless (save-excursion
  21287. (beginning-of-line)
  21288. (let ((case-fold-search t))
  21289. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21290. (user-error "Not at a block"))
  21291. (let ((element (org-element-at-point)))
  21292. (unless (memq (org-element-type element)
  21293. '(comment-block center-block dynamic-block example-block
  21294. export-block quote-block special-block
  21295. src-block verse-block))
  21296. (user-error "Not at a block"))
  21297. (org-with-wide-buffer
  21298. (org-indent-region (org-element-property :begin element)
  21299. (org-element-property :end element))))
  21300. (message "Block at point indented"))
  21301. ;;; Filling
  21302. ;; We use our own fill-paragraph and auto-fill functions.
  21303. ;; `org-fill-paragraph' relies on adaptive filling and context
  21304. ;; checking. Appropriate `fill-prefix' is computed with
  21305. ;; `org-adaptive-fill-function'.
  21306. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21307. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21308. ;; `org-adaptive-fill-function' value. Internally,
  21309. ;; `org-comment-line-break-function' breaks the line.
  21310. ;; `org-setup-filling' installs filling and auto-filling related
  21311. ;; variables during `org-mode' initialization.
  21312. (defvar org-element-paragraph-separate) ; org-element.el
  21313. (defun org-setup-filling ()
  21314. (require 'org-element)
  21315. ;; Prevent auto-fill from inserting unwanted new items.
  21316. (when (boundp 'fill-nobreak-predicate)
  21317. (org-set-local
  21318. 'fill-nobreak-predicate
  21319. (org-uniquify
  21320. (append fill-nobreak-predicate
  21321. '(org-fill-line-break-nobreak-p
  21322. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21323. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21324. (org-set-local 'paragraph-start paragraph-ending)
  21325. (org-set-local 'paragraph-separate paragraph-ending))
  21326. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21327. (org-set-local 'auto-fill-inhibit-regexp nil)
  21328. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21329. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21330. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21331. (defun org-fill-line-break-nobreak-p ()
  21332. "Non-nil when a new line at point would create an Org line break."
  21333. (save-excursion
  21334. (skip-chars-backward "[ \t]")
  21335. (skip-chars-backward "\\\\")
  21336. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21337. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21338. "Non-nil when a new line at point would split a timestamp."
  21339. (and (org-at-timestamp-p t)
  21340. (not (looking-at org-ts-regexp-both))))
  21341. (declare-function message-in-body-p "message" ())
  21342. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21343. (defun org-adaptive-fill-function ()
  21344. "Compute a fill prefix for the current line.
  21345. Return fill prefix, as a string, or nil if current line isn't
  21346. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21347. matches in paragraphs or comments, use it."
  21348. (catch 'exit
  21349. (when (derived-mode-p 'message-mode)
  21350. (save-excursion
  21351. (beginning-of-line)
  21352. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21353. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21354. ((looking-at message-cite-prefix-regexp)
  21355. (throw 'exit (match-string-no-properties 0)))
  21356. ((looking-at org-outline-regexp)
  21357. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21358. (org-with-wide-buffer
  21359. (unless (org-at-heading-p)
  21360. (let* ((p (line-beginning-position))
  21361. (element (save-excursion
  21362. (beginning-of-line)
  21363. (org-element-at-point)))
  21364. (type (org-element-type element))
  21365. (post-affiliated (org-element-property :post-affiliated element)))
  21366. (unless (< p post-affiliated)
  21367. (case type
  21368. (comment
  21369. (save-excursion
  21370. (beginning-of-line)
  21371. (looking-at "[ \t]*")
  21372. (concat (match-string 0) "# ")))
  21373. (footnote-definition "")
  21374. ((item plain-list)
  21375. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21376. (paragraph
  21377. ;; Fill prefix is usually the same as the current line,
  21378. ;; unless the paragraph is at the beginning of an item.
  21379. (let ((parent (org-element-property :parent element)))
  21380. (save-excursion
  21381. (beginning-of-line)
  21382. (cond ((eq (org-element-type parent) 'item)
  21383. (make-string (org-list-item-body-column
  21384. (org-element-property :begin parent))
  21385. ?\s))
  21386. ((and adaptive-fill-regexp
  21387. ;; Locally disable
  21388. ;; `adaptive-fill-function' to let
  21389. ;; `fill-context-prefix' handle
  21390. ;; `adaptive-fill-regexp' variable.
  21391. (let (adaptive-fill-function)
  21392. (fill-context-prefix
  21393. post-affiliated
  21394. (org-element-property :end element)))))
  21395. ((looking-at "[ \t]+") (match-string 0))
  21396. (t "")))))
  21397. (comment-block
  21398. ;; Only fill contents if P is within block boundaries.
  21399. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21400. (forward-line)
  21401. (point)))
  21402. (cend (save-excursion
  21403. (goto-char (org-element-property :end element))
  21404. (skip-chars-backward " \r\t\n")
  21405. (line-beginning-position))))
  21406. (when (and (>= p cbeg) (< p cend))
  21407. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21408. (match-string 0)
  21409. "")))))))))))
  21410. (declare-function message-goto-body "message" ())
  21411. (defvar message-cite-prefix-regexp) ; From message.el
  21412. (defun org-fill-paragraph (&optional justify)
  21413. "Fill element at point, when applicable.
  21414. This function only applies to comment blocks, comments, example
  21415. blocks and paragraphs. Also, as a special case, re-align table
  21416. when point is at one.
  21417. If JUSTIFY is non-nil (interactively, with prefix argument),
  21418. justify as well. If `sentence-end-double-space' is non-nil, then
  21419. period followed by one space does not end a sentence, so don't
  21420. break a line there. The variable `fill-column' controls the
  21421. width for filling.
  21422. For convenience, when point is at a plain list, an item or
  21423. a footnote definition, try to fill the first paragraph within."
  21424. (interactive)
  21425. (if (and (derived-mode-p 'message-mode)
  21426. (or (not (message-in-body-p))
  21427. (save-excursion (move-beginning-of-line 1)
  21428. (looking-at message-cite-prefix-regexp))))
  21429. ;; First ensure filling is correct in message-mode.
  21430. (let ((fill-paragraph-function
  21431. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21432. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21433. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21434. (paragraph-separate
  21435. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21436. (fill-paragraph nil))
  21437. (with-syntax-table org-mode-transpose-word-syntax-table
  21438. ;; Move to end of line in order to get the first paragraph
  21439. ;; within a plain list or a footnote definition.
  21440. (let ((element (save-excursion
  21441. (end-of-line)
  21442. (or (ignore-errors (org-element-at-point))
  21443. (user-error "An element cannot be parsed line %d"
  21444. (line-number-at-pos (point)))))))
  21445. ;; First check if point is in a blank line at the beginning of
  21446. ;; the buffer. In that case, ignore filling.
  21447. (case (org-element-type element)
  21448. ;; Use major mode filling function is src blocks.
  21449. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21450. ;; Align Org tables, leave table.el tables as-is.
  21451. (table-row (org-table-align) t)
  21452. (table
  21453. (when (eq (org-element-property :type element) 'org)
  21454. (save-excursion
  21455. (goto-char (org-element-property :post-affiliated element))
  21456. (org-table-align)))
  21457. t)
  21458. (paragraph
  21459. ;; Paragraphs may contain `line-break' type objects.
  21460. (let ((beg (max (point-min)
  21461. (org-element-property :contents-begin element)))
  21462. (end (min (point-max)
  21463. (org-element-property :contents-end element))))
  21464. ;; Do nothing if point is at an affiliated keyword.
  21465. (if (< (line-end-position) beg) t
  21466. (when (derived-mode-p 'message-mode)
  21467. ;; In `message-mode', do not fill following citation
  21468. ;; in current paragraph nor text before message body.
  21469. (let ((body-start (save-excursion (message-goto-body))))
  21470. (when body-start (setq beg (max body-start beg))))
  21471. (when (save-excursion
  21472. (re-search-forward
  21473. (concat "^" message-cite-prefix-regexp) end t))
  21474. (setq end (match-beginning 0))))
  21475. ;; Fill paragraph, taking line breaks into account.
  21476. (save-excursion
  21477. (goto-char beg)
  21478. (let ((cuts (list beg)))
  21479. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21480. (when (eq 'line-break
  21481. (org-element-type
  21482. (save-excursion (backward-char)
  21483. (org-element-context))))
  21484. (push (point) cuts)))
  21485. (dolist (c (delq end cuts))
  21486. (fill-region-as-paragraph c end justify)
  21487. (setq end c))))
  21488. t)))
  21489. ;; Contents of `comment-block' type elements should be
  21490. ;; filled as plain text, but only if point is within block
  21491. ;; markers.
  21492. (comment-block
  21493. (let* ((case-fold-search t)
  21494. (beg (save-excursion
  21495. (goto-char (org-element-property :begin element))
  21496. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21497. (forward-line)
  21498. (point)))
  21499. (end (save-excursion
  21500. (goto-char (org-element-property :end element))
  21501. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21502. (line-beginning-position))))
  21503. (if (or (< (point) beg) (> (point) end)) t
  21504. (fill-region-as-paragraph
  21505. (save-excursion (end-of-line)
  21506. (re-search-backward "^[ \t]*$" beg 'move)
  21507. (line-beginning-position))
  21508. (save-excursion (beginning-of-line)
  21509. (re-search-forward "^[ \t]*$" end 'move)
  21510. (line-beginning-position))
  21511. justify))))
  21512. ;; Fill comments.
  21513. (comment
  21514. (let ((begin (org-element-property :post-affiliated element))
  21515. (end (org-element-property :end element)))
  21516. (when (and (>= (point) begin) (<= (point) end))
  21517. (let ((begin (save-excursion
  21518. (end-of-line)
  21519. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21520. (progn (forward-line) (point))
  21521. begin)))
  21522. (end (save-excursion
  21523. (end-of-line)
  21524. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21525. (1- (line-beginning-position))
  21526. (skip-chars-backward " \r\t\n")
  21527. (line-end-position)))))
  21528. ;; Do not fill comments when at a blank line.
  21529. (when (> end begin)
  21530. (let ((fill-prefix
  21531. (save-excursion
  21532. (beginning-of-line)
  21533. (looking-at "[ \t]*#")
  21534. (let ((comment-prefix (match-string 0)))
  21535. (goto-char (match-end 0))
  21536. (if (looking-at adaptive-fill-regexp)
  21537. (concat comment-prefix (match-string 0))
  21538. (concat comment-prefix " "))))))
  21539. (save-excursion
  21540. (fill-region-as-paragraph begin end justify))))))
  21541. t))
  21542. ;; Ignore every other element.
  21543. (otherwise t))))))
  21544. (defun org-auto-fill-function ()
  21545. "Auto-fill function."
  21546. ;; Check if auto-filling is meaningful.
  21547. (let ((fc (current-fill-column)))
  21548. (when (and fc (> (current-column) fc))
  21549. (let* ((fill-prefix (org-adaptive-fill-function))
  21550. ;; Enforce empty fill prefix, if required. Otherwise, it
  21551. ;; will be computed again.
  21552. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21553. (when fill-prefix (do-auto-fill))))))
  21554. (defun org-comment-line-break-function (&optional soft)
  21555. "Break line at point and indent, continuing comment if within one.
  21556. The inserted newline is marked hard if variable
  21557. `use-hard-newlines' is true, unless optional argument SOFT is
  21558. non-nil."
  21559. (if soft (insert-and-inherit ?\n) (newline 1))
  21560. (save-excursion (forward-char -1) (delete-horizontal-space))
  21561. (delete-horizontal-space)
  21562. (indent-to-left-margin)
  21563. (insert-before-markers-and-inherit fill-prefix))
  21564. ;;; Fixed Width Areas
  21565. (defun org-toggle-fixed-width ()
  21566. "Toggle fixed-width markup.
  21567. Add or remove fixed-width markup on current line, whenever it
  21568. makes sense. Return an error otherwise.
  21569. If a region is active and if it contains only fixed-width areas
  21570. or blank lines, remove all fixed-width markup in it. If the
  21571. region contains anything else, convert all non-fixed-width lines
  21572. to fixed-width ones.
  21573. Blank lines at the end of the region are ignored unless the
  21574. region only contains such lines."
  21575. (interactive)
  21576. (if (not (org-region-active-p))
  21577. ;; No region:
  21578. ;;
  21579. ;; Remove fixed width marker only in a fixed-with element.
  21580. ;;
  21581. ;; Add fixed width maker in paragraphs, in blank lines after
  21582. ;; elements or at the beginning of a headline or an inlinetask,
  21583. ;; and before any one-line elements (e.g., a clock).
  21584. (progn
  21585. (beginning-of-line)
  21586. (let* ((element (org-element-at-point))
  21587. (type (org-element-type element)))
  21588. (cond
  21589. ((and (eq type 'fixed-width)
  21590. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21591. (replace-match
  21592. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21593. ((and (memq type '(babel-call clock comment diary-sexp headline
  21594. horizontal-rule keyword paragraph
  21595. planning))
  21596. (<= (org-element-property :post-affiliated element) (point)))
  21597. (skip-chars-forward " \t")
  21598. (insert ": "))
  21599. ((and (org-looking-at-p "[ \t]*$")
  21600. (or (eq type 'inlinetask)
  21601. (save-excursion
  21602. (skip-chars-forward " \r\t\n")
  21603. (<= (org-element-property :end element) (point)))))
  21604. (delete-region (point) (line-end-position))
  21605. (org-indent-line)
  21606. (insert ": "))
  21607. (t (user-error "Cannot insert a fixed-width line here")))))
  21608. ;; Region active.
  21609. (let* ((begin (save-excursion
  21610. (goto-char (region-beginning))
  21611. (line-beginning-position)))
  21612. (end (copy-marker
  21613. (save-excursion
  21614. (goto-char (region-end))
  21615. (unless (eolp) (beginning-of-line))
  21616. (if (save-excursion (re-search-backward "\\S-" begin t))
  21617. (progn (skip-chars-backward " \r\t\n") (point))
  21618. (point)))))
  21619. (all-fixed-width-p
  21620. (catch 'not-all-p
  21621. (save-excursion
  21622. (goto-char begin)
  21623. (skip-chars-forward " \r\t\n")
  21624. (when (eobp) (throw 'not-all-p nil))
  21625. (while (< (point) end)
  21626. (let ((element (org-element-at-point)))
  21627. (if (eq (org-element-type element) 'fixed-width)
  21628. (goto-char (org-element-property :end element))
  21629. (throw 'not-all-p nil))))
  21630. t))))
  21631. (if all-fixed-width-p
  21632. (save-excursion
  21633. (goto-char begin)
  21634. (while (< (point) end)
  21635. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21636. (replace-match
  21637. "" nil nil nil
  21638. (if (= (line-end-position) (match-end 0)) 0 1)))
  21639. (forward-line)))
  21640. (let ((min-ind (point-max)))
  21641. ;; Find minimum indentation across all lines.
  21642. (save-excursion
  21643. (goto-char begin)
  21644. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21645. (setq min-ind 0)
  21646. (catch 'zerop
  21647. (while (< (point) end)
  21648. (unless (org-looking-at-p "[ \t]*$")
  21649. (let ((ind (org-get-indentation)))
  21650. (setq min-ind (min min-ind ind))
  21651. (when (zerop ind) (throw 'zerop t))))
  21652. (forward-line)))))
  21653. ;; Loop over all lines and add fixed-width markup everywhere
  21654. ;; but in fixed-width lines.
  21655. (save-excursion
  21656. (goto-char begin)
  21657. (while (< (point) end)
  21658. (cond
  21659. ((org-at-heading-p)
  21660. (insert ": ")
  21661. (forward-line)
  21662. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21663. (insert ":")
  21664. (forward-line)))
  21665. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21666. (let* ((element (org-element-at-point))
  21667. (element-end (org-element-property :end element)))
  21668. (if (eq (org-element-type element) 'fixed-width)
  21669. (progn (goto-char element-end)
  21670. (skip-chars-backward " \r\t\n")
  21671. (forward-line))
  21672. (let ((limit (min end element-end)))
  21673. (while (< (point) limit)
  21674. (org-move-to-column min-ind t)
  21675. (insert ": ")
  21676. (forward-line))))))
  21677. (t
  21678. (org-move-to-column min-ind t)
  21679. (insert ": ")
  21680. (forward-line)))))))
  21681. (set-marker end nil))))
  21682. ;;; Comments
  21683. ;; Org comments syntax is quite complex. It requires the entire line
  21684. ;; to be just a comment. Also, even with the right syntax at the
  21685. ;; beginning of line, some some elements (i.e. verse-block or
  21686. ;; example-block) don't accept comments. Usual Emacs comment commands
  21687. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21688. ;; Org still relies on `comment-dwim', but cannot trust
  21689. ;; `comment-only-p'. So, `comment-region-function' and
  21690. ;; `uncomment-region-function' both point
  21691. ;; to`org-comment-or-uncomment-region'. Eventually,
  21692. ;; `org-insert-comment' takes care of insertion of comments at the
  21693. ;; beginning of line.
  21694. ;; `org-setup-comments-handling' install comments related variables
  21695. ;; during `org-mode' initialization.
  21696. (defun org-setup-comments-handling ()
  21697. (interactive)
  21698. (org-set-local 'comment-use-syntax nil)
  21699. (org-set-local 'comment-start "# ")
  21700. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21701. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21702. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21703. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21704. (defun org-insert-comment ()
  21705. "Insert an empty comment above current line.
  21706. If the line is empty, insert comment at its beginning. When
  21707. point is within a source block, comment according to the related
  21708. major mode."
  21709. (if (let ((element (org-element-at-point)))
  21710. (and (eq (org-element-type element) 'src-block)
  21711. (< (save-excursion
  21712. (goto-char (org-element-property :post-affiliated element))
  21713. (line-end-position))
  21714. (point))
  21715. (> (save-excursion
  21716. (goto-char (org-element-property :end element))
  21717. (skip-chars-backward " \r\t\n")
  21718. (line-beginning-position))
  21719. (point))))
  21720. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21721. (beginning-of-line)
  21722. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21723. (open-line 1))
  21724. (org-indent-line)
  21725. (insert "# ")))
  21726. (defvar comment-empty-lines) ; From newcomment.el.
  21727. (defun org-comment-or-uncomment-region (beg end &rest _)
  21728. "Comment or uncomment each non-blank line in the region.
  21729. Uncomment each non-blank line between BEG and END if it only
  21730. contains commented lines. Otherwise, comment them. If region is
  21731. strictly within a source block, use appropriate comment syntax."
  21732. (if (let ((element (org-element-at-point)))
  21733. (and (eq (org-element-type element) 'src-block)
  21734. (< (save-excursion
  21735. (goto-char (org-element-property :post-affiliated element))
  21736. (line-end-position))
  21737. beg)
  21738. (>= (save-excursion
  21739. (goto-char (org-element-property :end element))
  21740. (skip-chars-backward " \r\t\n")
  21741. (line-beginning-position))
  21742. end)))
  21743. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21744. (save-restriction
  21745. ;; Restrict region
  21746. (narrow-to-region (save-excursion (goto-char beg)
  21747. (skip-chars-forward " \r\t\n" end)
  21748. (line-beginning-position))
  21749. (save-excursion (goto-char end)
  21750. (skip-chars-backward " \r\t\n" beg)
  21751. (line-end-position)))
  21752. (let ((uncommentp
  21753. ;; UNCOMMENTP is non-nil when every non blank line between
  21754. ;; BEG and END is a comment.
  21755. (save-excursion
  21756. (goto-char (point-min))
  21757. (while (and (not (eobp))
  21758. (let ((element (org-element-at-point)))
  21759. (and (eq (org-element-type element) 'comment)
  21760. (goto-char (min (point-max)
  21761. (org-element-property
  21762. :end element)))))))
  21763. (eobp))))
  21764. (if uncommentp
  21765. ;; Only blank lines and comments in region: uncomment it.
  21766. (save-excursion
  21767. (goto-char (point-min))
  21768. (while (not (eobp))
  21769. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21770. (replace-match "" nil nil nil 1))
  21771. (forward-line)))
  21772. ;; Comment each line in region.
  21773. (let ((min-indent (point-max)))
  21774. ;; First find the minimum indentation across all lines.
  21775. (save-excursion
  21776. (goto-char (point-min))
  21777. (while (and (not (eobp)) (not (zerop min-indent)))
  21778. (unless (looking-at "[ \t]*$")
  21779. (setq min-indent (min min-indent (current-indentation))))
  21780. (forward-line)))
  21781. ;; Then loop over all lines.
  21782. (save-excursion
  21783. (goto-char (point-min))
  21784. (while (not (eobp))
  21785. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21786. ;; Don't get fooled by invisible text (e.g. link path)
  21787. ;; when moving to column MIN-INDENT.
  21788. (let ((buffer-invisibility-spec nil))
  21789. (org-move-to-column min-indent t))
  21790. (insert comment-start))
  21791. (forward-line)))))))))
  21792. (defun org-comment-dwim (arg)
  21793. "Call `comment-dwim' within a source edit buffer if needed."
  21794. (interactive "*P")
  21795. (if (org-in-src-block-p)
  21796. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21797. (call-interactively 'comment-dwim)))
  21798. ;;; Timestamps API
  21799. ;; This section contains tools to operate on timestamp objects, as
  21800. ;; returned by, e.g. `org-element-context'.
  21801. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21802. "Encode TIMESTAMP object into Emacs internal time.
  21803. Use end of date range or time range when END is non-nil."
  21804. (apply #'encode-time
  21805. (cons 0
  21806. (mapcar
  21807. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21808. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21809. '(:minute-start :hour-start :day-start :month-start
  21810. :year-start))))))
  21811. (defun org-timestamp-has-time-p (timestamp)
  21812. "Non-nil when TIMESTAMP has a time specified."
  21813. (org-element-property :hour-start timestamp))
  21814. (defun org-timestamp-format (timestamp format &optional end utc)
  21815. "Format a TIMESTAMP object into a string.
  21816. FORMAT is a format specifier to be passed to
  21817. `format-time-string'.
  21818. When optional argument END is non-nil, use end of date-range or
  21819. time-range, if possible.
  21820. When optional argument UTC is non-nil, time will be expressed as
  21821. Universal Time."
  21822. (format-time-string
  21823. format (org-timestamp--to-internal-time timestamp end) utc))
  21824. (defun org-timestamp-split-range (timestamp &optional end)
  21825. "Extract a TIMESTAMP object from a date or time range.
  21826. END, when non-nil, means extract the end of the range.
  21827. Otherwise, extract its start.
  21828. Return a new timestamp object."
  21829. (let ((type (org-element-property :type timestamp)))
  21830. (if (memq type '(active inactive diary)) timestamp
  21831. (let ((split-ts (org-element-copy timestamp)))
  21832. ;; Set new type.
  21833. (org-element-put-property
  21834. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21835. ;; Copy start properties over end properties if END is
  21836. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21837. (let ((p-alist '((:minute-start . :minute-end)
  21838. (:hour-start . :hour-end)
  21839. (:day-start . :day-end)
  21840. (:month-start . :month-end)
  21841. (:year-start . :year-end))))
  21842. (dolist (p-cell p-alist)
  21843. (org-element-put-property
  21844. split-ts
  21845. (funcall (if end #'car #'cdr) p-cell)
  21846. (org-element-property
  21847. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21848. ;; Eventually refresh `:raw-value'.
  21849. (org-element-put-property split-ts :raw-value nil)
  21850. (org-element-put-property
  21851. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21852. (defun org-timestamp-translate (timestamp &optional boundary)
  21853. "Translate TIMESTAMP object to custom format.
  21854. Format string is defined in `org-time-stamp-custom-formats',
  21855. which see.
  21856. When optional argument BOUNDARY is non-nil, it is either the
  21857. symbol `start' or `end'. In this case, only translate the
  21858. starting or ending part of TIMESTAMP if it is a date or time
  21859. range. Otherwise, translate both parts.
  21860. Return timestamp as-is if `org-display-custom-times' is nil or if
  21861. it has a `diary' type."
  21862. (let ((type (org-element-property :type timestamp)))
  21863. (if (or (not org-display-custom-times) (eq type 'diary))
  21864. (org-element-interpret-data timestamp)
  21865. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21866. org-time-stamp-custom-formats)))
  21867. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21868. (concat (org-timestamp-format timestamp fmt)
  21869. "--"
  21870. (org-timestamp-format timestamp fmt t))
  21871. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21872. ;;; Other stuff.
  21873. (defvar reftex-docstruct-symbol)
  21874. (defvar reftex-cite-format)
  21875. (defvar org--rds)
  21876. (defun org-reftex-citation ()
  21877. "Use reftex-citation to insert a citation into the buffer.
  21878. This looks for a line like
  21879. #+BIBLIOGRAPHY: foo plain option:-d
  21880. and derives from it that foo.bib is the bibliography file relevant
  21881. for this document. It then installs the necessary environment for RefTeX
  21882. to work in this buffer and calls `reftex-citation' to insert a citation
  21883. into the buffer.
  21884. Export of such citations to both LaTeX and HTML is handled by the contributed
  21885. package ox-bibtex by Taru Karttunen."
  21886. (interactive)
  21887. (let ((reftex-docstruct-symbol 'org--rds)
  21888. (reftex-cite-format "\\cite{%l}")
  21889. org--rds bib)
  21890. (save-excursion
  21891. (save-restriction
  21892. (widen)
  21893. (let ((case-fold-search t)
  21894. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21895. (if (not (save-excursion
  21896. (or (re-search-forward re nil t)
  21897. (re-search-backward re nil t))))
  21898. (user-error "No bibliography defined in file")
  21899. (setq bib (concat (match-string 1) ".bib")
  21900. org--rds (list (list 'bib bib)))))))
  21901. (call-interactively 'reftex-citation)))
  21902. ;;;; Functions extending outline functionality
  21903. (defun org-beginning-of-line (&optional arg)
  21904. "Go to the beginning of the current line. If that is invisible, continue
  21905. to a visible line beginning. This makes the function of C-a more intuitive.
  21906. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21907. first attempt, and only move to after the tags when the cursor is already
  21908. beyond the end of the headline."
  21909. (interactive "P")
  21910. (let ((pos (point))
  21911. (special (if (consp org-special-ctrl-a/e)
  21912. (car org-special-ctrl-a/e)
  21913. org-special-ctrl-a/e))
  21914. deactivate-mark refpos)
  21915. (if (org-bound-and-true-p visual-line-mode)
  21916. (beginning-of-visual-line 1)
  21917. (beginning-of-line 1))
  21918. (if (and arg (fboundp 'move-beginning-of-line))
  21919. (call-interactively 'move-beginning-of-line)
  21920. (if (bobp)
  21921. nil
  21922. (backward-char 1)
  21923. (if (org-truely-invisible-p)
  21924. (while (and (not (bobp)) (org-truely-invisible-p))
  21925. (backward-char 1)
  21926. (beginning-of-line 1))
  21927. (forward-char 1))))
  21928. (when special
  21929. (cond
  21930. ((and (looking-at org-complex-heading-regexp)
  21931. (eq (char-after (match-end 1)) ?\s))
  21932. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21933. (point-at-eol)))
  21934. (goto-char
  21935. (if (eq special t)
  21936. (cond ((> pos refpos) refpos)
  21937. ((= pos (point)) refpos)
  21938. (t (point)))
  21939. (cond ((> pos (point)) (point))
  21940. ((not (eq last-command this-command)) (point))
  21941. (t refpos)))))
  21942. ((org-at-item-p)
  21943. ;; Being at an item and not looking at an the item means point
  21944. ;; was previously moved to beginning of a visual line, which
  21945. ;; doesn't contain the item. Therefore, do nothing special,
  21946. ;; just stay here.
  21947. (when (looking-at org-list-full-item-re)
  21948. ;; Set special position at first white space character after
  21949. ;; bullet, and check-box, if any.
  21950. (let ((after-bullet
  21951. (let ((box (match-end 3)))
  21952. (if (not box) (match-end 1)
  21953. (let ((after (char-after box)))
  21954. (if (and after (= after ? )) (1+ box) box))))))
  21955. ;; Special case: Move point to special position when
  21956. ;; currently after it or at beginning of line.
  21957. (if (eq special t)
  21958. (when (or (> pos after-bullet) (= (point) pos))
  21959. (goto-char after-bullet))
  21960. ;; Reversed case: Move point to special position when
  21961. ;; point was already at beginning of line and command is
  21962. ;; repeated.
  21963. (when (and (= (point) pos) (eq last-command this-command))
  21964. (goto-char after-bullet))))))))
  21965. (org-no-warnings
  21966. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21967. (setq disable-point-adjustment
  21968. (or (not (invisible-p (point)))
  21969. (not (invisible-p (max (point-min) (1- (point))))))))
  21970. (defun org-end-of-line (&optional arg)
  21971. "Go to the end of the line.
  21972. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21973. tags on the first attempt, and only move to after the tags when
  21974. the cursor is already beyond the end of the headline."
  21975. (interactive "P")
  21976. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21977. org-special-ctrl-a/e))
  21978. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21979. 'end-of-visual-line)
  21980. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21981. (t 'end-of-line)))
  21982. deactivate-mark)
  21983. (if (or (not special) arg) (call-interactively move-fun)
  21984. (let* ((element (save-excursion (beginning-of-line)
  21985. (org-element-at-point)))
  21986. (type (org-element-type element)))
  21987. (cond
  21988. ((memq type '(headline inlinetask))
  21989. (let ((pos (point)))
  21990. (beginning-of-line 1)
  21991. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21992. (if (eq special t)
  21993. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21994. (goto-char (match-beginning 1))
  21995. (goto-char (match-end 0)))
  21996. (if (or (< pos (match-end 0))
  21997. (not (eq this-command last-command)))
  21998. (goto-char (match-end 0))
  21999. (goto-char (match-beginning 1))))
  22000. (call-interactively move-fun))))
  22001. ((outline-invisible-p (line-end-position))
  22002. ;; If element is hidden, `move-end-of-line' would put point
  22003. ;; after it. Use `end-of-line' to stay on current line.
  22004. (call-interactively 'end-of-line))
  22005. (t (call-interactively move-fun)))))
  22006. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22007. (setq disable-point-adjustment
  22008. (or (not (invisible-p (point)))
  22009. (not (invisible-p (max (point-min) (1- (point))))))))
  22010. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22011. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22012. (defun org-backward-sentence (&optional _arg)
  22013. "Go to beginning of sentence, or beginning of table field.
  22014. This will call `backward-sentence' or `org-table-beginning-of-field',
  22015. depending on context."
  22016. (interactive)
  22017. (let* ((element (org-element-at-point))
  22018. (contents-begin (org-element-property :contents-begin element))
  22019. (table (org-element-lineage element '(table) t)))
  22020. (if (and table
  22021. (> (point) contents-begin)
  22022. (<= (point) (org-element-property :contents-end table)))
  22023. (call-interactively #'org-table-beginning-of-field)
  22024. (save-restriction
  22025. (when (and contents-begin
  22026. (< (point-min) contents-begin)
  22027. (> (point) contents-begin))
  22028. (narrow-to-region contents-begin
  22029. (org-element-property :contents-end element)))
  22030. (call-interactively #'backward-sentence)))))
  22031. (defun org-forward-sentence (&optional _arg)
  22032. "Go to end of sentence, or end of table field.
  22033. This will call `forward-sentence' or `org-table-end-of-field',
  22034. depending on context."
  22035. (interactive)
  22036. (let* ((element (org-element-at-point))
  22037. (contents-end (org-element-property :contents-end element))
  22038. (table (org-element-lineage element '(table) t)))
  22039. (if (and table
  22040. (>= (point) (org-element-property :contents-begin table))
  22041. (< (point) contents-end))
  22042. (call-interactively #'org-table-end-of-field)
  22043. (save-restriction
  22044. (when (and contents-end
  22045. (> (point-max) contents-end)
  22046. ;; Skip blank lines between elements.
  22047. (< (org-element-property :end element)
  22048. (save-excursion (goto-char contents-end)
  22049. (skip-chars-forward " \r\t\n"))))
  22050. (narrow-to-region (org-element-property :contents-begin element)
  22051. contents-end))
  22052. (call-interactively #'forward-sentence)))))
  22053. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22054. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22055. (defun org-kill-line (&optional _arg)
  22056. "Kill line, to tags or end of line."
  22057. (interactive)
  22058. (cond
  22059. ((or (not org-special-ctrl-k)
  22060. (bolp)
  22061. (not (org-at-heading-p)))
  22062. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22063. org-ctrl-k-protect-subtree)
  22064. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22065. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22066. (user-error "C-k aborted as it would kill a hidden subtree")))
  22067. (call-interactively
  22068. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22069. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22070. (kill-region (point) (match-beginning 1))
  22071. (org-set-tags nil t))
  22072. (t (kill-region (point) (point-at-eol)))))
  22073. (define-key org-mode-map "\C-k" 'org-kill-line)
  22074. (defun org-yank (&optional arg)
  22075. "Yank. If the kill is a subtree, treat it specially.
  22076. This command will look at the current kill and check if is a single
  22077. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22078. cursor is at the beginning of a line or after the stars of a currently
  22079. empty headline, then the yank is handled specially. How exactly depends
  22080. on the value of the following variables, both set by default.
  22081. `org-yank-folded-subtrees'
  22082. When set, the subtree(s) will be folded after insertion, but only
  22083. if doing so would now swallow text after the yanked text.
  22084. `org-yank-adjusted-subtrees'
  22085. When set, the subtree will be promoted or demoted in order to
  22086. fit into the local outline tree structure, which means that the
  22087. level will be adjusted so that it becomes the smaller one of the
  22088. two *visible* surrounding headings.
  22089. Any prefix to this command will cause `yank' to be called directly with
  22090. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22091. will just
  22092. plainly yank the text as it is.
  22093. \[1] The test checks if the first non-white line is a heading
  22094. and if there are no other headings with fewer stars."
  22095. (interactive "P")
  22096. (org-yank-generic 'yank arg))
  22097. (defun org-yank-generic (command arg)
  22098. "Perform some yank-like command.
  22099. This function implements the behavior described in the `org-yank'
  22100. documentation. However, it has been generalized to work for any
  22101. interactive command with similar behavior."
  22102. ;; pretend to be command COMMAND
  22103. (setq this-command command)
  22104. (if arg
  22105. (call-interactively command)
  22106. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22107. (and (org-kill-is-subtree-p)
  22108. (or (bolp)
  22109. (and (looking-at "[ \t]*$")
  22110. (string-match
  22111. "\\`\\*+\\'"
  22112. (buffer-substring (point-at-bol) (point)))))))
  22113. swallowp)
  22114. (cond
  22115. ((and subtreep org-yank-folded-subtrees)
  22116. (let ((beg (point))
  22117. end)
  22118. (if (and subtreep org-yank-adjusted-subtrees)
  22119. (org-paste-subtree nil nil 'for-yank)
  22120. (call-interactively command))
  22121. (setq end (point))
  22122. (goto-char beg)
  22123. (when (and (bolp) subtreep
  22124. (not (setq swallowp
  22125. (org-yank-folding-would-swallow-text beg end))))
  22126. (org-with-limited-levels
  22127. (or (looking-at org-outline-regexp)
  22128. (re-search-forward org-outline-regexp-bol end t))
  22129. (while (and (< (point) end) (looking-at org-outline-regexp))
  22130. (hide-subtree)
  22131. (org-cycle-show-empty-lines 'folded)
  22132. (condition-case nil
  22133. (outline-forward-same-level 1)
  22134. (error (goto-char end))))))
  22135. (when swallowp
  22136. (message
  22137. "Inserted text not folded because that would swallow text"))
  22138. (goto-char end)
  22139. (skip-chars-forward " \t\n\r")
  22140. (beginning-of-line 1)
  22141. (push-mark beg 'nomsg)))
  22142. ((and subtreep org-yank-adjusted-subtrees)
  22143. (let ((beg (point-at-bol)))
  22144. (org-paste-subtree nil nil 'for-yank)
  22145. (push-mark beg 'nomsg)))
  22146. (t
  22147. (call-interactively command))))))
  22148. (defun org-yank-folding-would-swallow-text (beg end)
  22149. "Would hide-subtree at BEG swallow any text after END?"
  22150. (let (level)
  22151. (org-with-limited-levels
  22152. (save-excursion
  22153. (goto-char beg)
  22154. (when (or (looking-at org-outline-regexp)
  22155. (re-search-forward org-outline-regexp-bol end t))
  22156. (setq level (org-outline-level)))
  22157. (goto-char end)
  22158. (skip-chars-forward " \t\r\n\v\f")
  22159. (if (or (eobp)
  22160. (and (bolp) (looking-at org-outline-regexp)
  22161. (<= (org-outline-level) level)))
  22162. nil ; Nothing would be swallowed
  22163. t))))) ; something would swallow
  22164. (define-key org-mode-map "\C-y" 'org-yank)
  22165. (defun org-truely-invisible-p ()
  22166. "Check if point is at a character currently not visible.
  22167. This version does not only check the character property, but also
  22168. `visible-mode'."
  22169. ;; Early versions of noutline don't have `outline-invisible-p'.
  22170. (if (org-bound-and-true-p visible-mode)
  22171. nil
  22172. (outline-invisible-p)))
  22173. (defun org-invisible-p2 ()
  22174. "Check if point is at a character currently not visible."
  22175. (save-excursion
  22176. (if (and (eolp) (not (bobp))) (backward-char 1))
  22177. ;; Early versions of noutline don't have `outline-invisible-p'.
  22178. (outline-invisible-p)))
  22179. (defun org-back-to-heading (&optional invisible-ok)
  22180. "Call `outline-back-to-heading', but provide a better error message."
  22181. (condition-case nil
  22182. (outline-back-to-heading invisible-ok)
  22183. (error (error "Before first headline at position %d in buffer %s"
  22184. (point) (current-buffer)))))
  22185. (defun org-before-first-heading-p ()
  22186. "Before first heading?"
  22187. (save-excursion
  22188. (end-of-line)
  22189. (null (re-search-backward org-outline-regexp-bol nil t))))
  22190. (defun org-at-heading-p (&optional ignored)
  22191. (outline-on-heading-p t))
  22192. ;; Compatibility alias with Org versions < 7.8.03
  22193. (defalias 'org-on-heading-p 'org-at-heading-p)
  22194. (defun org-in-commented-heading-p (&optional no-inheritance)
  22195. "Non-nil if point is under a commented heading.
  22196. This function also checks ancestors of the current headline,
  22197. unless optional argument NO-INHERITANCE is non-nil."
  22198. (cond
  22199. ((org-before-first-heading-p) nil)
  22200. ((let ((headline (nth 4 (org-heading-components))))
  22201. (and headline
  22202. (let ((case-fold-search nil))
  22203. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22204. headline)))))
  22205. (no-inheritance nil)
  22206. (t
  22207. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22208. (defun org-at-comment-p nil
  22209. "Is cursor in a commented line?"
  22210. (save-excursion
  22211. (save-match-data
  22212. (beginning-of-line)
  22213. (looking-at "^[ \t]*# "))))
  22214. (defun org-at-drawer-p nil
  22215. "Is cursor at a drawer keyword?"
  22216. (save-excursion
  22217. (move-beginning-of-line 1)
  22218. (looking-at org-drawer-regexp)))
  22219. (defun org-at-block-p nil
  22220. "Is cursor at a block keyword?"
  22221. (save-excursion
  22222. (move-beginning-of-line 1)
  22223. (looking-at org-block-regexp)))
  22224. (defun org-point-at-end-of-empty-headline ()
  22225. "If point is at the end of an empty headline, return t, else nil.
  22226. If the heading only contains a TODO keyword, it is still still considered
  22227. empty."
  22228. (and (looking-at "[ \t]*$")
  22229. (when org-todo-line-regexp
  22230. (save-excursion
  22231. (beginning-of-line 1)
  22232. (let ((case-fold-search nil))
  22233. (looking-at org-todo-line-regexp)
  22234. (string= (match-string 3) ""))))))
  22235. (defun org-at-heading-or-item-p ()
  22236. (or (org-at-heading-p) (org-at-item-p)))
  22237. (defun org-at-target-p ()
  22238. (or (org-in-regexp org-radio-target-regexp)
  22239. (org-in-regexp org-target-regexp)))
  22240. ;; Compatibility alias with Org versions < 7.8.03
  22241. (defalias 'org-on-target-p 'org-at-target-p)
  22242. (defun org-up-heading-all (arg)
  22243. "Move to the heading line of which the present line is a subheading.
  22244. This function considers both visible and invisible heading lines.
  22245. With argument, move up ARG levels."
  22246. (if (fboundp 'outline-up-heading-all)
  22247. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22248. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22249. (defun org-up-heading-safe ()
  22250. "Move to the heading line of which the present line is a subheading.
  22251. This version will not throw an error. It will return the level of the
  22252. headline found, or nil if no higher level is found.
  22253. Also, this function will be a lot faster than `outline-up-heading',
  22254. because it relies on stars being the outline starters. This can really
  22255. make a significant difference in outlines with very many siblings."
  22256. (when (ignore-errors (org-back-to-heading t))
  22257. (let ((level-up (1- (funcall outline-level))))
  22258. (and (> level-up 0)
  22259. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22260. (funcall outline-level)))))
  22261. (defun org-first-sibling-p ()
  22262. "Is this heading the first child of its parents?"
  22263. (interactive)
  22264. (let ((re org-outline-regexp-bol)
  22265. level l)
  22266. (unless (org-at-heading-p t)
  22267. (user-error "Not at a heading"))
  22268. (setq level (funcall outline-level))
  22269. (save-excursion
  22270. (if (not (re-search-backward re nil t))
  22271. t
  22272. (setq l (funcall outline-level))
  22273. (< l level)))))
  22274. (defun org-goto-sibling (&optional previous)
  22275. "Goto the next sibling, even if it is invisible.
  22276. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22277. when a sibling was found. When none is found, return nil and don't
  22278. move point."
  22279. (let ((fun (if previous 're-search-backward 're-search-forward))
  22280. (pos (point))
  22281. (re org-outline-regexp-bol)
  22282. level l)
  22283. (when (ignore-errors (org-back-to-heading t))
  22284. (setq level (funcall outline-level))
  22285. (catch 'exit
  22286. (or previous (forward-char 1))
  22287. (while (funcall fun re nil t)
  22288. (setq l (funcall outline-level))
  22289. (when (< l level) (goto-char pos) (throw 'exit nil))
  22290. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22291. (goto-char pos)
  22292. nil))))
  22293. (defun org-show-siblings ()
  22294. "Show all siblings of the current headline."
  22295. (save-excursion
  22296. (while (org-goto-sibling) (org-flag-heading nil)))
  22297. (save-excursion
  22298. (while (org-goto-sibling 'previous)
  22299. (org-flag-heading nil))))
  22300. (defun org-goto-first-child ()
  22301. "Goto the first child, even if it is invisible.
  22302. Return t when a child was found. Otherwise don't move point and
  22303. return nil."
  22304. (let (level (pos (point)) (re org-outline-regexp-bol))
  22305. (when (ignore-errors (org-back-to-heading t))
  22306. (setq level (outline-level))
  22307. (forward-char 1)
  22308. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22309. (progn (goto-char (match-beginning 0)) t)
  22310. (goto-char pos) nil))))
  22311. (defun org-show-hidden-entry ()
  22312. "Show an entry where even the heading is hidden."
  22313. (save-excursion
  22314. (org-show-entry)))
  22315. (defun org-flag-heading (flag &optional entry)
  22316. "Flag the current heading. FLAG non-nil means make invisible.
  22317. When ENTRY is non-nil, show the entire entry."
  22318. (save-excursion
  22319. (org-back-to-heading t)
  22320. ;; Check if we should show the entire entry
  22321. (if entry
  22322. (progn
  22323. (org-show-entry)
  22324. (save-excursion
  22325. (and (outline-next-heading)
  22326. (org-flag-heading nil))))
  22327. (outline-flag-region (max (point-min) (1- (point)))
  22328. (save-excursion (outline-end-of-heading) (point))
  22329. flag))))
  22330. (defun org-get-next-sibling ()
  22331. "Move to next heading of the same level, and return point.
  22332. If there is no such heading, return nil.
  22333. This is like outline-next-sibling, but invisible headings are ok."
  22334. (let ((level (funcall outline-level)))
  22335. (outline-next-heading)
  22336. (while (and (not (eobp)) (> (funcall outline-level) level))
  22337. (outline-next-heading))
  22338. (if (or (eobp) (< (funcall outline-level) level))
  22339. nil
  22340. (point))))
  22341. (defun org-get-last-sibling ()
  22342. "Move to previous heading of the same level, and return point.
  22343. If there is no such heading, return nil."
  22344. (let ((opoint (point))
  22345. (level (funcall outline-level)))
  22346. (outline-previous-heading)
  22347. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22348. (while (and (> (funcall outline-level) level)
  22349. (not (bobp)))
  22350. (outline-previous-heading))
  22351. (if (< (funcall outline-level) level)
  22352. nil
  22353. (point)))))
  22354. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22355. "Goto to the end of a subtree."
  22356. ;; This contains an exact copy of the original function, but it uses
  22357. ;; `org-back-to-heading', to make it work also in invisible
  22358. ;; trees. And is uses an invisible-ok argument.
  22359. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22360. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22361. ;; with many children and grandchildren etc, this can be much faster
  22362. ;; than the outline version.
  22363. (org-back-to-heading invisible-ok)
  22364. (let ((first t)
  22365. (level (funcall outline-level)))
  22366. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22367. ;; A true heading (not a plain list item), in Org-mode
  22368. ;; This means we can easily find the end by looking
  22369. ;; only for the right number of stars. Using a regexp to do
  22370. ;; this is so much faster than using a Lisp loop.
  22371. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22372. (forward-char 1)
  22373. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22374. ;; something else, do it the slow way
  22375. (while (and (not (eobp))
  22376. (or first (> (funcall outline-level) level)))
  22377. (setq first nil)
  22378. (outline-next-heading)))
  22379. (unless to-heading
  22380. (if (memq (preceding-char) '(?\n ?\^M))
  22381. (progn
  22382. ;; Go to end of line before heading
  22383. (forward-char -1)
  22384. (if (memq (preceding-char) '(?\n ?\^M))
  22385. ;; leave blank line before heading
  22386. (forward-char -1))))))
  22387. (point))
  22388. (defun org-end-of-meta-data (&optional full)
  22389. "Skip planning line and properties drawer in current entry.
  22390. When optional argument FULL is non-nil, also skip empty lines,
  22391. clocking lines and regular drawers at the beginning of the
  22392. entry."
  22393. (org-back-to-heading t)
  22394. (forward-line)
  22395. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22396. (when (looking-at org-property-drawer-re)
  22397. (goto-char (match-end 0))
  22398. (forward-line))
  22399. (when (and full (not (org-at-heading-p)))
  22400. (catch 'exit
  22401. (let ((end (save-excursion (outline-next-heading) (point)))
  22402. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22403. (while (not (eobp))
  22404. (cond ((org-looking-at-p org-drawer-regexp)
  22405. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22406. (forward-line)
  22407. (throw 'exit t)))
  22408. ((org-looking-at-p re) (forward-line))
  22409. (t (throw 'exit t))))))))
  22410. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22411. "Move forward to the ARG'th subheading at same level as this one.
  22412. Stop at the first and last subheadings of a superior heading.
  22413. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22414. non-nil it will also look at invisible ones."
  22415. (interactive "p")
  22416. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22417. (if (and arg (< arg 0))
  22418. (goto-char (point-min))
  22419. (outline-next-heading))
  22420. (org-at-heading-p)
  22421. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22422. (f (if (and arg (< arg 0))
  22423. 're-search-backward
  22424. 're-search-forward))
  22425. (count (if arg (abs arg) 1))
  22426. (result (point)))
  22427. (while (and (prog1 (> count 0)
  22428. (forward-char (if (and arg (< arg 0)) -1 1)))
  22429. (funcall f org-outline-regexp-bol nil 'move))
  22430. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22431. (cond ((< l level) (setq count 0))
  22432. ((and (= l level)
  22433. (or invisible-ok
  22434. (progn
  22435. (goto-char (line-beginning-position))
  22436. (not (outline-invisible-p)))))
  22437. (setq count (1- count))
  22438. (when (eq l level)
  22439. (setq result (point)))))))
  22440. (goto-char result))
  22441. (beginning-of-line 1)))
  22442. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22443. "Move backward to the ARG'th subheading at same level as this one.
  22444. Stop at the first and last subheadings of a superior heading."
  22445. (interactive "p")
  22446. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22447. (defun org-next-visible-heading (arg)
  22448. "Move to the next visible heading.
  22449. This function wraps `outline-next-visible-heading' with
  22450. `org-with-limited-levels' in order to skip over inline tasks and
  22451. respect customization of `org-odd-levels-only'."
  22452. (interactive "p")
  22453. (org-with-limited-levels
  22454. (outline-next-visible-heading arg)))
  22455. (defun org-previous-visible-heading (arg)
  22456. "Move to the next visible heading.
  22457. This function wraps `outline-previous-visible-heading' with
  22458. `org-with-limited-levels' in order to skip over inline tasks and
  22459. respect customization of `org-odd-levels-only'."
  22460. (interactive "p")
  22461. (org-with-limited-levels
  22462. (outline-previous-visible-heading arg)))
  22463. (defun org-next-block (arg &optional backward block-regexp)
  22464. "Jump to the next block.
  22465. With a prefix argument ARG, jump forward ARG many blocks.
  22466. When BACKWARD is non-nil, jump to the previous block.
  22467. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22468. Match data is set according to this regexp when the function
  22469. returns.
  22470. Return point at beginning of the opening line of found block.
  22471. Throw an error if no block is found."
  22472. (interactive "p")
  22473. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22474. (case-fold-search t)
  22475. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22476. (count (or arg 1))
  22477. (origin (point))
  22478. last-element)
  22479. (if backward (beginning-of-line) (end-of-line))
  22480. (while (and (> count 0) (funcall search-fn re nil t))
  22481. (let ((element (save-excursion
  22482. (goto-char (match-beginning 0))
  22483. (save-match-data (org-element-at-point)))))
  22484. (when (and (memq (org-element-type element)
  22485. '(center-block comment-block dynamic-block
  22486. example-block export-block quote-block
  22487. special-block src-block verse-block))
  22488. (<= (match-beginning 0)
  22489. (org-element-property :post-affiliated element)))
  22490. (setq last-element element)
  22491. (decf count))))
  22492. (if (= count 0)
  22493. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22494. (save-match-data (org-show-context)))
  22495. (goto-char origin)
  22496. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22497. (defun org-previous-block (arg &optional block-regexp)
  22498. "Jump to the previous block.
  22499. With a prefix argument ARG, jump backward ARG many source blocks.
  22500. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22501. (interactive "p")
  22502. (org-next-block arg t block-regexp))
  22503. (defun org-forward-paragraph ()
  22504. "Move forward to beginning of next paragraph or equivalent.
  22505. The function moves point to the beginning of the next visible
  22506. structural element, which can be a paragraph, a table, a list
  22507. item, etc. It also provides some special moves for convenience:
  22508. - On an affiliated keyword, jump to the beginning of the
  22509. relative element.
  22510. - On an item or a footnote definition, move to the second
  22511. element inside, if any.
  22512. - On a table or a property drawer, jump after it.
  22513. - On a verse or source block, stop after blank lines."
  22514. (interactive)
  22515. (when (eobp) (user-error "Cannot move further down"))
  22516. (let* ((deactivate-mark nil)
  22517. (element (org-element-at-point))
  22518. (type (org-element-type element))
  22519. (post-affiliated (org-element-property :post-affiliated element))
  22520. (contents-begin (org-element-property :contents-begin element))
  22521. (contents-end (org-element-property :contents-end element))
  22522. (end (let ((end (org-element-property :end element)) (parent element))
  22523. (while (and (setq parent (org-element-property :parent parent))
  22524. (= (org-element-property :contents-end parent) end))
  22525. (setq end (org-element-property :end parent)))
  22526. end)))
  22527. (cond ((not element)
  22528. (skip-chars-forward " \r\t\n")
  22529. (or (eobp) (beginning-of-line)))
  22530. ;; On affiliated keywords, move to element's beginning.
  22531. ((< (point) post-affiliated)
  22532. (goto-char post-affiliated))
  22533. ;; At a table row, move to the end of the table. Similarly,
  22534. ;; at a node property, move to the end of the property
  22535. ;; drawer.
  22536. ((memq type '(node-property table-row))
  22537. (goto-char (org-element-property
  22538. :end (org-element-property :parent element))))
  22539. ((memq type '(property-drawer table)) (goto-char end))
  22540. ;; Consider blank lines as separators in verse and source
  22541. ;; blocks to ease editing.
  22542. ((memq type '(src-block verse-block))
  22543. (when (eq type 'src-block)
  22544. (setq contents-end
  22545. (save-excursion (goto-char end)
  22546. (skip-chars-backward " \r\t\n")
  22547. (line-beginning-position))))
  22548. (beginning-of-line)
  22549. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22550. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22551. (goto-char end)
  22552. (skip-chars-forward " \r\t\n")
  22553. (if (= (point) contents-end) (goto-char end)
  22554. (beginning-of-line))))
  22555. ;; With no contents, just skip element.
  22556. ((not contents-begin) (goto-char end))
  22557. ;; If contents are invisible, skip the element altogether.
  22558. ((outline-invisible-p (line-end-position))
  22559. (case type
  22560. (headline
  22561. (org-with-limited-levels (outline-next-visible-heading 1)))
  22562. ;; At a plain list, make sure we move to the next item
  22563. ;; instead of skipping the whole list.
  22564. (plain-list (forward-char)
  22565. (org-forward-paragraph))
  22566. (otherwise (goto-char end))))
  22567. ((>= (point) contents-end) (goto-char end))
  22568. ((>= (point) contents-begin)
  22569. ;; This can only happen on paragraphs and plain lists.
  22570. (case type
  22571. (paragraph (goto-char end))
  22572. ;; At a plain list, try to move to second element in
  22573. ;; first item, if possible.
  22574. (plain-list (end-of-line)
  22575. (org-forward-paragraph))))
  22576. ;; When contents start on the middle of a line (e.g. in
  22577. ;; items and footnote definitions), try to reach first
  22578. ;; element starting after current line.
  22579. ((> (line-end-position) contents-begin)
  22580. (end-of-line)
  22581. (org-forward-paragraph))
  22582. (t (goto-char contents-begin)))))
  22583. (defun org-backward-paragraph ()
  22584. "Move backward to start of previous paragraph or equivalent.
  22585. The function moves point to the beginning of the current
  22586. structural element, which can be a paragraph, a table, a list
  22587. item, etc., or to the beginning of the previous visible one if
  22588. point is already there. It also provides some special moves for
  22589. convenience:
  22590. - On an affiliated keyword, jump to the first one.
  22591. - On a table or a property drawer, move to its beginning.
  22592. - On a verse or source block, stop before blank lines."
  22593. (interactive)
  22594. (when (bobp) (user-error "Cannot move further up"))
  22595. (let* ((deactivate-mark nil)
  22596. (element (org-element-at-point))
  22597. (type (org-element-type element))
  22598. (contents-begin (org-element-property :contents-begin element))
  22599. (contents-end (org-element-property :contents-end element))
  22600. (post-affiliated (org-element-property :post-affiliated element))
  22601. (begin (org-element-property :begin element)))
  22602. (cond
  22603. ((not element) (goto-char (point-min)))
  22604. ((= (point) begin)
  22605. (backward-char)
  22606. (org-backward-paragraph))
  22607. ((<= (point) post-affiliated) (goto-char begin))
  22608. ((memq type '(node-property table-row))
  22609. (goto-char (org-element-property
  22610. :post-affiliated (org-element-property :parent element))))
  22611. ((memq type '(property-drawer table)) (goto-char begin))
  22612. ((memq type '(src-block verse-block))
  22613. (when (eq type 'src-block)
  22614. (setq contents-begin
  22615. (save-excursion (goto-char begin) (forward-line) (point))))
  22616. (if (= (point) contents-begin) (goto-char post-affiliated)
  22617. ;; Inside a verse block, see blank lines as paragraph
  22618. ;; separators.
  22619. (let ((origin (point)))
  22620. (skip-chars-backward " \r\t\n" contents-begin)
  22621. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22622. (skip-chars-forward " \r\t\n" origin)
  22623. (if (= (point) origin) (goto-char contents-begin)
  22624. (beginning-of-line))))))
  22625. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22626. ((eq type 'paragraph)
  22627. (goto-char contents-begin)
  22628. ;; When at first paragraph in an item or a footnote definition,
  22629. ;; move directly to beginning of line.
  22630. (let ((parent-contents
  22631. (org-element-property
  22632. :contents-begin (org-element-property :parent element))))
  22633. (when (and parent-contents (= parent-contents contents-begin))
  22634. (beginning-of-line))))
  22635. ;; At the end of a greater element, move to the beginning of the
  22636. ;; last element within.
  22637. ((>= (point) contents-end)
  22638. (goto-char (1- contents-end))
  22639. (org-backward-paragraph))
  22640. (t (goto-char (or post-affiliated begin))))
  22641. ;; Ensure we never leave point invisible.
  22642. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22643. (defun org-forward-element ()
  22644. "Move forward by one element.
  22645. Move to the next element at the same level, when possible."
  22646. (interactive)
  22647. (cond ((eobp) (user-error "Cannot move further down"))
  22648. ((org-with-limited-levels (org-at-heading-p))
  22649. (let ((origin (point)))
  22650. (goto-char (org-end-of-subtree nil t))
  22651. (unless (org-with-limited-levels (org-at-heading-p))
  22652. (goto-char origin)
  22653. (user-error "Cannot move further down"))))
  22654. (t
  22655. (let* ((elem (org-element-at-point))
  22656. (end (org-element-property :end elem))
  22657. (parent (org-element-property :parent elem)))
  22658. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22659. (goto-char (org-element-property :end parent)))
  22660. ((integer-or-marker-p end) (goto-char end))
  22661. (t (message "No element at point")))))))
  22662. (defun org-backward-element ()
  22663. "Move backward by one element.
  22664. Move to the previous element at the same level, when possible."
  22665. (interactive)
  22666. (cond ((bobp) (user-error "Cannot move further up"))
  22667. ((org-with-limited-levels (org-at-heading-p))
  22668. ;; At a headline, move to the previous one, if any, or stay
  22669. ;; here.
  22670. (let ((origin (point)))
  22671. (org-with-limited-levels (org-backward-heading-same-level 1))
  22672. ;; When current headline has no sibling above, move to its
  22673. ;; parent.
  22674. (when (= (point) origin)
  22675. (or (org-with-limited-levels (org-up-heading-safe))
  22676. (progn (goto-char origin)
  22677. (user-error "Cannot move further up"))))))
  22678. (t
  22679. (let* ((elem (org-element-at-point))
  22680. (beg (org-element-property :begin elem)))
  22681. (cond
  22682. ;; Move to beginning of current element if point isn't
  22683. ;; there already.
  22684. ((null beg) (message "No element at point"))
  22685. ((/= (point) beg) (goto-char beg))
  22686. (t (goto-char beg)
  22687. (skip-chars-backward " \r\t\n")
  22688. (unless (bobp)
  22689. (let ((prev (org-element-at-point)))
  22690. (goto-char (org-element-property :begin prev))
  22691. (while (and (setq prev (org-element-property :parent prev))
  22692. (<= (org-element-property :end prev) beg))
  22693. (goto-char (org-element-property :begin prev)))))))))))
  22694. (defun org-up-element ()
  22695. "Move to upper element."
  22696. (interactive)
  22697. (if (org-with-limited-levels (org-at-heading-p))
  22698. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22699. (let* ((elem (org-element-at-point))
  22700. (parent (org-element-property :parent elem)))
  22701. (if parent (goto-char (org-element-property :begin parent))
  22702. (if (org-with-limited-levels (org-before-first-heading-p))
  22703. (user-error "No surrounding element")
  22704. (org-with-limited-levels (org-back-to-heading)))))))
  22705. (defvar org-element-greater-elements)
  22706. (defun org-down-element ()
  22707. "Move to inner element."
  22708. (interactive)
  22709. (let ((element (org-element-at-point)))
  22710. (cond
  22711. ((memq (org-element-type element) '(plain-list table))
  22712. (goto-char (org-element-property :contents-begin element))
  22713. (forward-char))
  22714. ((memq (org-element-type element) org-element-greater-elements)
  22715. ;; If contents are hidden, first disclose them.
  22716. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22717. (goto-char (or (org-element-property :contents-begin element)
  22718. (user-error "No content for this element"))))
  22719. (t (user-error "No inner element")))))
  22720. (defun org-drag-element-backward ()
  22721. "Move backward element at point."
  22722. (interactive)
  22723. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22724. (let* ((elem (org-element-at-point))
  22725. (prev-elem
  22726. (save-excursion
  22727. (goto-char (org-element-property :begin elem))
  22728. (skip-chars-backward " \r\t\n")
  22729. (unless (bobp)
  22730. (let* ((beg (org-element-property :begin elem))
  22731. (prev (org-element-at-point))
  22732. (up prev))
  22733. (while (and (setq up (org-element-property :parent up))
  22734. (<= (org-element-property :end up) beg))
  22735. (setq prev up))
  22736. prev)))))
  22737. ;; Error out if no previous element or previous element is
  22738. ;; a parent of the current one.
  22739. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22740. (user-error "Cannot drag element backward")
  22741. (let ((pos (point)))
  22742. (org-element-swap-A-B prev-elem elem)
  22743. (goto-char (+ (org-element-property :begin prev-elem)
  22744. (- pos (org-element-property :begin elem)))))))))
  22745. (defun org-drag-element-forward ()
  22746. "Move forward element at point."
  22747. (interactive)
  22748. (let* ((pos (point))
  22749. (elem (org-element-at-point)))
  22750. (when (= (point-max) (org-element-property :end elem))
  22751. (user-error "Cannot drag element forward"))
  22752. (goto-char (org-element-property :end elem))
  22753. (let ((next-elem (org-element-at-point)))
  22754. (when (or (org-element-nested-p elem next-elem)
  22755. (and (eq (org-element-type next-elem) 'headline)
  22756. (not (eq (org-element-type elem) 'headline))))
  22757. (goto-char pos)
  22758. (user-error "Cannot drag element forward"))
  22759. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22760. ;; body's length (without final blanks) and by the length of
  22761. ;; blanks between ELEM and NEXT-ELEM.
  22762. (let ((size-next (- (save-excursion
  22763. (goto-char (org-element-property :end next-elem))
  22764. (skip-chars-backward " \r\t\n")
  22765. (forward-line)
  22766. ;; Small correction if buffer doesn't end
  22767. ;; with a newline character.
  22768. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22769. (org-element-property :begin next-elem)))
  22770. (size-blank (- (org-element-property :end elem)
  22771. (save-excursion
  22772. (goto-char (org-element-property :end elem))
  22773. (skip-chars-backward " \r\t\n")
  22774. (forward-line)
  22775. (point)))))
  22776. (org-element-swap-A-B elem next-elem)
  22777. (goto-char (+ pos size-next size-blank))))))
  22778. (defun org-drag-line-forward (arg)
  22779. "Drag the line at point ARG lines forward."
  22780. (interactive "p")
  22781. (dotimes (n (abs arg))
  22782. (let ((c (current-column)))
  22783. (if (< 0 arg)
  22784. (progn
  22785. (beginning-of-line 2)
  22786. (transpose-lines 1)
  22787. (beginning-of-line 0))
  22788. (transpose-lines 1)
  22789. (beginning-of-line -1))
  22790. (org-move-to-column c))))
  22791. (defun org-drag-line-backward (arg)
  22792. "Drag the line at point ARG lines backward."
  22793. (interactive "p")
  22794. (org-drag-line-forward (- arg)))
  22795. (defun org-mark-element ()
  22796. "Put point at beginning of this element, mark at end.
  22797. Interactively, if this command is repeated or (in Transient Mark
  22798. mode) if the mark is active, it marks the next element after the
  22799. ones already marked."
  22800. (interactive)
  22801. (let (deactivate-mark)
  22802. (if (and (org-called-interactively-p 'any)
  22803. (or (and (eq last-command this-command) (mark t))
  22804. (and transient-mark-mode mark-active)))
  22805. (set-mark
  22806. (save-excursion
  22807. (goto-char (mark))
  22808. (goto-char (org-element-property :end (org-element-at-point)))))
  22809. (let ((element (org-element-at-point)))
  22810. (end-of-line)
  22811. (push-mark (org-element-property :end element) t t)
  22812. (goto-char (org-element-property :begin element))))))
  22813. (defun org-narrow-to-element ()
  22814. "Narrow buffer to current element."
  22815. (interactive)
  22816. (let ((elem (org-element-at-point)))
  22817. (cond
  22818. ((eq (car elem) 'headline)
  22819. (narrow-to-region
  22820. (org-element-property :begin elem)
  22821. (org-element-property :end elem)))
  22822. ((memq (car elem) org-element-greater-elements)
  22823. (narrow-to-region
  22824. (org-element-property :contents-begin elem)
  22825. (org-element-property :contents-end elem)))
  22826. (t
  22827. (narrow-to-region
  22828. (org-element-property :begin elem)
  22829. (org-element-property :end elem))))))
  22830. (defun org-transpose-element ()
  22831. "Transpose current and previous elements, keeping blank lines between.
  22832. Point is moved after both elements."
  22833. (interactive)
  22834. (org-skip-whitespace)
  22835. (let ((end (org-element-property :end (org-element-at-point))))
  22836. (org-drag-element-backward)
  22837. (goto-char end)))
  22838. (defun org-unindent-buffer ()
  22839. "Un-indent the visible part of the buffer.
  22840. Relative indentation (between items, inside blocks, etc.) isn't
  22841. modified."
  22842. (interactive)
  22843. (unless (eq major-mode 'org-mode)
  22844. (user-error "Cannot un-indent a buffer not in Org mode"))
  22845. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22846. unindent-tree ; For byte-compiler.
  22847. (unindent-tree
  22848. (function
  22849. (lambda (contents)
  22850. (mapc
  22851. (lambda (element)
  22852. (if (memq (org-element-type element) '(headline section))
  22853. (funcall unindent-tree (org-element-contents element))
  22854. (save-excursion
  22855. (save-restriction
  22856. (narrow-to-region
  22857. (org-element-property :begin element)
  22858. (org-element-property :end element))
  22859. (org-do-remove-indentation)))))
  22860. (reverse contents))))))
  22861. (funcall unindent-tree (org-element-contents parse-tree))))
  22862. (defun org-show-subtree ()
  22863. "Show everything after this heading at deeper levels."
  22864. (interactive)
  22865. (outline-flag-region
  22866. (point)
  22867. (save-excursion
  22868. (org-end-of-subtree t t))
  22869. nil))
  22870. (defun org-show-entry ()
  22871. "Show the body directly following this heading.
  22872. Show the heading too, if it is currently invisible."
  22873. (interactive)
  22874. (save-excursion
  22875. (ignore-errors
  22876. (org-back-to-heading t)
  22877. (outline-flag-region
  22878. (max (point-min) (1- (point)))
  22879. (save-excursion
  22880. (if (re-search-forward
  22881. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22882. (match-beginning 1)
  22883. (point-max)))
  22884. nil)
  22885. (org-cycle-hide-drawers 'children))))
  22886. (defun org-make-options-regexp (kwds &optional extra)
  22887. "Make a regular expression for keyword lines.
  22888. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22889. when non-nil, is a regexp matching keywords names."
  22890. (concat "^[ \t]*#\\+\\("
  22891. (regexp-opt kwds)
  22892. (and extra (concat (and kwds "\\|") extra))
  22893. "\\):[ \t]*\\(.*\\)"))
  22894. ;; Make isearch reveal the necessary context
  22895. (defun org-isearch-end ()
  22896. "Reveal context after isearch exits."
  22897. (when isearch-success ; only if search was successful
  22898. (if (featurep 'xemacs)
  22899. ;; Under XEmacs, the hook is run in the correct place,
  22900. ;; we directly show the context.
  22901. (org-show-context 'isearch)
  22902. ;; In Emacs the hook runs *before* restoring the overlays.
  22903. ;; So we have to use a one-time post-command-hook to do this.
  22904. ;; (Emacs 22 has a special variable, see function `org-mode')
  22905. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22906. isearch-mode-end-hook-quit)
  22907. ;; Only when the isearch was not quitted.
  22908. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22909. 'append 'local)))))
  22910. (defun org-isearch-post-command ()
  22911. "Remove self from hook, and show context."
  22912. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22913. (org-show-context 'isearch))
  22914. ;;;; Integration with and fixes for other packages
  22915. ;;; Imenu support
  22916. (defvar org-imenu-markers nil
  22917. "All markers currently used by Imenu.")
  22918. (make-variable-buffer-local 'org-imenu-markers)
  22919. (defun org-imenu-new-marker (&optional pos)
  22920. "Return a new marker for use by Imenu, and remember the marker."
  22921. (let ((m (make-marker)))
  22922. (move-marker m (or pos (point)))
  22923. (push m org-imenu-markers)
  22924. m))
  22925. (defun org-imenu-get-tree ()
  22926. "Produce the index for Imenu."
  22927. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22928. (setq org-imenu-markers nil)
  22929. (let* ((n org-imenu-depth)
  22930. (re (concat "^" (org-get-limited-outline-regexp)))
  22931. (subs (make-vector (1+ n) nil))
  22932. (last-level 0)
  22933. m level head0 head)
  22934. (save-excursion
  22935. (save-restriction
  22936. (widen)
  22937. (goto-char (point-max))
  22938. (while (re-search-backward re nil t)
  22939. (setq level (org-reduced-level (funcall outline-level)))
  22940. (when (and (<= level n)
  22941. (looking-at org-complex-heading-regexp)
  22942. (setq head0 (org-match-string-no-properties 4)))
  22943. (setq head (org-link-display-format head0)
  22944. m (org-imenu-new-marker))
  22945. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22946. (if (>= level last-level)
  22947. (push (cons head m) (aref subs level))
  22948. (push (cons head (aref subs (1+ level))) (aref subs level))
  22949. (loop for i from (1+ level) to n do (aset subs i nil)))
  22950. (setq last-level level)))))
  22951. (aref subs 1)))
  22952. (eval-after-load "imenu"
  22953. '(progn
  22954. (add-hook 'imenu-after-jump-hook
  22955. (lambda ()
  22956. (if (derived-mode-p 'org-mode)
  22957. (org-show-context 'org-goto))))))
  22958. (defun org-link-display-format (link)
  22959. "Replace a link with its the description.
  22960. If there is no description, use the link target."
  22961. (save-match-data
  22962. (if (string-match org-bracket-link-analytic-regexp link)
  22963. (replace-match (if (match-end 5)
  22964. (match-string 5 link)
  22965. (concat (match-string 1 link)
  22966. (match-string 3 link)))
  22967. nil t link)
  22968. link)))
  22969. (defun org-toggle-link-display ()
  22970. "Toggle the literal or descriptive display of links."
  22971. (interactive)
  22972. (if org-descriptive-links
  22973. (progn (org-remove-from-invisibility-spec '(org-link))
  22974. (org-restart-font-lock)
  22975. (setq org-descriptive-links nil))
  22976. (progn (add-to-invisibility-spec '(org-link))
  22977. (org-restart-font-lock)
  22978. (setq org-descriptive-links t))))
  22979. ;; Speedbar support
  22980. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22981. "Overlay marking the agenda restriction line in speedbar.")
  22982. (overlay-put org-speedbar-restriction-lock-overlay
  22983. 'face 'org-agenda-restriction-lock)
  22984. (overlay-put org-speedbar-restriction-lock-overlay
  22985. 'help-echo "Agendas are currently limited to this item.")
  22986. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22987. (defun org-speedbar-set-agenda-restriction ()
  22988. "Restrict future agenda commands to the location at point in speedbar.
  22989. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22990. (interactive)
  22991. (require 'org-agenda)
  22992. (let (p m tp np dir txt)
  22993. (cond
  22994. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22995. 'org-imenu t))
  22996. (setq m (get-text-property p 'org-imenu-marker))
  22997. (with-current-buffer (marker-buffer m)
  22998. (goto-char m)
  22999. (org-agenda-set-restriction-lock 'subtree)))
  23000. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23001. 'speedbar-function 'speedbar-find-file))
  23002. (setq tp (previous-single-property-change
  23003. (1+ p) 'speedbar-function)
  23004. np (next-single-property-change
  23005. tp 'speedbar-function)
  23006. dir (speedbar-line-directory)
  23007. txt (buffer-substring-no-properties (or tp (point-min))
  23008. (or np (point-max))))
  23009. (with-current-buffer (find-file-noselect
  23010. (let ((default-directory dir))
  23011. (expand-file-name txt)))
  23012. (unless (derived-mode-p 'org-mode)
  23013. (user-error "Cannot restrict to non-Org-mode file"))
  23014. (org-agenda-set-restriction-lock 'file)))
  23015. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23016. (move-overlay org-speedbar-restriction-lock-overlay
  23017. (point-at-bol) (point-at-eol))
  23018. (setq current-prefix-arg nil)
  23019. (org-agenda-maybe-redo)))
  23020. (defvar speedbar-file-key-map)
  23021. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23022. (eval-after-load "speedbar"
  23023. '(progn
  23024. (speedbar-add-supported-extension ".org")
  23025. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23026. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23027. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23028. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23029. (add-hook 'speedbar-visiting-tag-hook
  23030. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23031. ;;; Fixes and Hacks for problems with other packages
  23032. (defun org--flyspell-object-check-p (element)
  23033. "Non-nil when Flyspell can check object at point.
  23034. ELEMENT is the element at point."
  23035. (let ((object (save-excursion
  23036. (when (org-looking-at-p "\\>") (backward-char))
  23037. (org-element-context element))))
  23038. (case (org-element-type object)
  23039. ;; Prevent checks in links due to keybinding conflict with
  23040. ;; Flyspell.
  23041. ((code entity export-snippet inline-babel-call
  23042. inline-src-block line-break latex-fragment link macro
  23043. statistics-cookie target timestamp verbatim)
  23044. nil)
  23045. (footnote-reference
  23046. ;; Only in inline footnotes, within the definition.
  23047. (and (eq (org-element-property :type object) 'inline)
  23048. (< (save-excursion
  23049. (goto-char (org-element-property :begin object))
  23050. (search-forward ":" nil t 2))
  23051. (point))))
  23052. (otherwise t))))
  23053. (defun org-mode-flyspell-verify ()
  23054. "Function used for `flyspell-generic-check-word-predicate'."
  23055. (if (org-at-heading-p)
  23056. ;; At a headline or an inlinetask, check title only. This is
  23057. ;; faster than relying on `org-element-at-point'.
  23058. (and (save-excursion (beginning-of-line)
  23059. (and (let ((case-fold-search t))
  23060. (not (looking-at "\\*+ END[ \t]*$")))
  23061. (looking-at org-complex-heading-regexp)))
  23062. (match-beginning 4)
  23063. (>= (point) (match-beginning 4))
  23064. (or (not (match-beginning 5))
  23065. (< (point) (match-beginning 5))))
  23066. (let* ((element (org-element-at-point))
  23067. (post-affiliated (org-element-property :post-affiliated element)))
  23068. (cond
  23069. ;; Ignore checks in all affiliated keywords but captions.
  23070. ((< (point) post-affiliated)
  23071. (and (save-excursion
  23072. (beginning-of-line)
  23073. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23074. (> (point) (match-end 0))
  23075. (org--flyspell-object-check-p element)))
  23076. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23077. ((let ((log (org-log-into-drawer)))
  23078. (and log
  23079. (let ((drawer (org-element-lineage element '(drawer))))
  23080. (and drawer
  23081. (eq (compare-strings
  23082. log nil nil
  23083. (org-element-property :drawer-name drawer) nil nil t)
  23084. t)))))
  23085. nil)
  23086. (t
  23087. (case (org-element-type element)
  23088. ((comment quote-section) t)
  23089. (comment-block
  23090. ;; Allow checks between block markers, not on them.
  23091. (and (> (line-beginning-position) post-affiliated)
  23092. (save-excursion
  23093. (end-of-line)
  23094. (skip-chars-forward " \r\t\n")
  23095. (< (point) (org-element-property :end element)))))
  23096. ;; Arbitrary list of keywords where checks are meaningful.
  23097. ;; Make sure point is on the value part of the element.
  23098. (keyword
  23099. (and (member (org-element-property :key element)
  23100. '("DESCRIPTION" "TITLE"))
  23101. (save-excursion
  23102. (search-backward ":" (line-beginning-position) t))))
  23103. ;; Check is globally allowed in paragraphs verse blocks and
  23104. ;; table rows (after affiliated keywords) but some objects
  23105. ;; must not be affected.
  23106. ((paragraph table-row verse-block)
  23107. (let ((cbeg (org-element-property :contents-begin element))
  23108. (cend (org-element-property :contents-end element)))
  23109. (and cbeg (>= (point) cbeg) (< (point) cend)
  23110. (org--flyspell-object-check-p element))))))))))
  23111. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23112. (defun org-remove-flyspell-overlays-in (beg end)
  23113. "Remove flyspell overlays in region."
  23114. (and (org-bound-and-true-p flyspell-mode)
  23115. (fboundp 'flyspell-delete-region-overlays)
  23116. (flyspell-delete-region-overlays beg end)))
  23117. (defvar flyspell-delayed-commands)
  23118. (eval-after-load "flyspell"
  23119. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23120. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23121. (eval-after-load "bookmark"
  23122. '(if (boundp 'bookmark-after-jump-hook)
  23123. ;; We can use the hook
  23124. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23125. ;; Hook not available, use advice
  23126. (defadvice bookmark-jump (after org-make-visible activate)
  23127. "Make the position visible."
  23128. (org-bookmark-jump-unhide))))
  23129. ;; Make sure saveplace shows the location if it was hidden
  23130. (eval-after-load "saveplace"
  23131. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23132. "Make the position visible."
  23133. (org-bookmark-jump-unhide)))
  23134. ;; Make sure ecb shows the location if it was hidden
  23135. (eval-after-load "ecb"
  23136. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23137. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23138. (if (derived-mode-p 'org-mode)
  23139. (org-show-context))))
  23140. (defun org-bookmark-jump-unhide ()
  23141. "Unhide the current position, to show the bookmark location."
  23142. (and (derived-mode-p 'org-mode)
  23143. (or (outline-invisible-p)
  23144. (save-excursion (goto-char (max (point-min) (1- (point))))
  23145. (outline-invisible-p)))
  23146. (org-show-context 'bookmark-jump)))
  23147. (defun org-mark-jump-unhide ()
  23148. "Make the point visible with `org-show-context' after jumping to the mark."
  23149. (when (and (derived-mode-p 'org-mode)
  23150. (outline-invisible-p))
  23151. (org-show-context 'mark-goto)))
  23152. (eval-after-load "simple"
  23153. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23154. "Make the point visible with `org-show-context'."
  23155. (org-mark-jump-unhide)))
  23156. (eval-after-load "simple"
  23157. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23158. "Make the point visible with `org-show-context'."
  23159. (org-mark-jump-unhide)))
  23160. (eval-after-load "simple"
  23161. '(defadvice pop-global-mark (after org-make-visible activate)
  23162. "Make the point visible with `org-show-context'."
  23163. (org-mark-jump-unhide)))
  23164. ;; Make session.el ignore our circular variable
  23165. (defvar session-globals-exclude)
  23166. (eval-after-load "session"
  23167. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23168. ;;;; Finish up
  23169. (provide 'org)
  23170. (run-hooks 'org-load-hook)
  23171. ;;; org.el ends here