org.el 957 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; URL: http://orgmode.org
  8. ;; Version: 8.4-git
  9. ;; Keywords: outlines, hypermedia, calendar, wp
  10. ;; Package-Requires: ((emacs "24.3"))
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'find-func)
  73. (require 'format-spec)
  74. (or (equal this-command 'eval-buffer)
  75. (condition-case nil
  76. (load (concat (file-name-directory load-file-name)
  77. "org-loaddefs.el")
  78. nil t t t)
  79. (error
  80. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  81. (sit-for 3)
  82. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  83. (sit-for 3))))
  84. (require 'org-macs)
  85. (require 'org-compat)
  86. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  87. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  88. ;;
  89. ;; In Org buffers, the value of `outline-regexp' is that of
  90. ;; `org-outline-regexp'. The only function still directly relying on
  91. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  92. ;; job when `orgstruct-mode' is active.
  93. (defvar org-outline-regexp "\\*+ "
  94. "Regexp to match Org headlines.")
  95. (defvar org-outline-regexp-bol "^\\*+ "
  96. "Regexp to match Org headlines.
  97. This is similar to `org-outline-regexp' but additionally makes
  98. sure that we are at the beginning of the line.")
  99. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  100. "Matches a headline, putting stars and text into groups.
  101. Stars are put in group 1 and the trimmed body in group 2.")
  102. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  103. (unless (boundp 'calendar-view-holidays-initially-flag)
  104. (org-defvaralias 'calendar-view-holidays-initially-flag
  105. 'view-calendar-holidays-initially))
  106. (unless (boundp 'calendar-view-diary-initially-flag)
  107. (org-defvaralias 'calendar-view-diary-initially-flag
  108. 'view-diary-entries-initially))
  109. (unless (boundp 'diary-fancy-buffer)
  110. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  111. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  112. (declare-function org-add-archive-files "org-archive" (files))
  113. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  114. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  115. (declare-function org-agenda-redo "org-agenda" (&optional all))
  116. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  117. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  118. (declare-function org-beamer-mode "ox-beamer" ())
  119. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  120. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  121. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  122. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  123. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  124. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  125. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  126. (declare-function org-clock-update-time-maybe "org-clock" ())
  127. (declare-function org-clocktable-shift "org-clock" (dir n))
  128. (declare-function org-element-at-point "org-element" ())
  129. (declare-function org-element-cache-refresh "org-element" (pos))
  130. (declare-function org-element-cache-reset "org-element" (&optional all))
  131. (declare-function org-element-contents "org-element" (element))
  132. (declare-function org-element-context "org-element" (&optional element))
  133. (declare-function org-element-copy "org-element" (datum))
  134. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  135. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  136. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  137. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  138. (declare-function org-element-property "org-element" (property element))
  139. (declare-function org-element-put-property "org-element" (element property value))
  140. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  141. (declare-function org-element-type "org-element" (element))
  142. (declare-function org-element-update-syntax "org-element" ())
  143. (declare-function org-id-find-id-file "org-id" (id))
  144. (declare-function org-id-get-create "org-id" (&optional force))
  145. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  146. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  147. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  148. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  149. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  150. (declare-function org-table-align "org-table" ())
  151. (declare-function org-table-begin "org-table" (&optional table-type))
  152. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  153. (declare-function org-table-blank-field "org-table" ())
  154. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  155. (declare-function org-table-edit-field "org-table" (arg))
  156. (declare-function org-table-end "org-table" (&optional table-type))
  157. (declare-function org-table-end-of-field "org-table" (&optional n))
  158. (declare-function org-table-insert-row "org-table" (&optional arg))
  159. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  160. (declare-function org-table-maybe-eval-formula "org-table" ())
  161. (declare-function org-table-maybe-recalculate-line "org-table" ())
  162. (declare-function org-table-next-row "org-table" ())
  163. (declare-function org-table-paste-rectangle "org-table" ())
  164. (declare-function org-table-wrap-region "org-table" (arg))
  165. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  166. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  167. (declare-function orgtbl-mode "org-table" (&optional arg))
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. ;; Tangle-file traversal returns reversed list of tangled files
  216. ;; and we want to evaluate the first target.
  217. (setq exported-file
  218. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  219. (message "%s %s"
  220. (if compile
  221. (progn (byte-compile-file exported-file 'load)
  222. "Compiled and loaded")
  223. (progn (load-file exported-file) "Loaded"))
  224. exported-file)))
  225. (defcustom org-babel-load-languages '((emacs-lisp . t))
  226. "Languages which can be evaluated in Org-mode buffers.
  227. This list can be used to load support for any of the languages
  228. below, note that each language will depend on a different set of
  229. system executables and/or Emacs modes. When a language is
  230. \"loaded\", then code blocks in that language can be evaluated
  231. with `org-babel-execute-src-block' bound by default to C-c
  232. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  233. be set to remove code block evaluation from the C-c C-c
  234. keybinding. By default only Emacs Lisp (which has no
  235. requirements) is loaded."
  236. :group 'org-babel
  237. :set 'org-babel-do-load-languages
  238. :version "24.1"
  239. :type '(alist :tag "Babel Languages"
  240. :key-type
  241. (choice
  242. (const :tag "Awk" awk)
  243. (const :tag "C" C)
  244. (const :tag "R" R)
  245. (const :tag "Asymptote" asymptote)
  246. (const :tag "Calc" calc)
  247. (const :tag "Clojure" clojure)
  248. (const :tag "CSS" css)
  249. (const :tag "Ditaa" ditaa)
  250. (const :tag "Dot" dot)
  251. (const :tag "Emacs Lisp" emacs-lisp)
  252. (const :tag "Forth" forth)
  253. (const :tag "Fortran" fortran)
  254. (const :tag "Gnuplot" gnuplot)
  255. (const :tag "Haskell" haskell)
  256. (const :tag "IO" io)
  257. (const :tag "J" J)
  258. (const :tag "Java" java)
  259. (const :tag "Javascript" js)
  260. (const :tag "LaTeX" latex)
  261. (const :tag "Ledger" ledger)
  262. (const :tag "Lilypond" lilypond)
  263. (const :tag "Lisp" lisp)
  264. (const :tag "Makefile" makefile)
  265. (const :tag "Maxima" maxima)
  266. (const :tag "Matlab" matlab)
  267. (const :tag "Mscgen" mscgen)
  268. (const :tag "Ocaml" ocaml)
  269. (const :tag "Octave" octave)
  270. (const :tag "Org" org)
  271. (const :tag "Perl" perl)
  272. (const :tag "Pico Lisp" picolisp)
  273. (const :tag "PlantUML" plantuml)
  274. (const :tag "Python" python)
  275. (const :tag "Ruby" ruby)
  276. (const :tag "Sass" sass)
  277. (const :tag "Scala" scala)
  278. (const :tag "Scheme" scheme)
  279. (const :tag "Screen" screen)
  280. (const :tag "Shell Script" shell)
  281. (const :tag "Shen" shen)
  282. (const :tag "Sql" sql)
  283. (const :tag "Sqlite" sqlite)
  284. (const :tag "ebnf2ps" ebnf2ps))
  285. :value-type (boolean :tag "Activate" :value t)))
  286. ;;;; Customization variables
  287. (defcustom org-clone-delete-id nil
  288. "Remove ID property of clones of a subtree.
  289. When non-nil, clones of a subtree don't inherit the ID property.
  290. Otherwise they inherit the ID property with a new unique
  291. identifier."
  292. :type 'boolean
  293. :version "24.1"
  294. :group 'org-id)
  295. ;;; Version
  296. (org-check-version)
  297. ;;;###autoload
  298. (defun org-version (&optional here full message)
  299. "Show the org-mode version.
  300. Interactively, or when MESSAGE is non-nil, show it in echo area.
  301. With prefix argument, or when HERE is non-nil, insert it at point.
  302. In non-interactive uses, a reduced version string is output unless
  303. FULL is given."
  304. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  305. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  306. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  307. (load-suffixes (list ".el"))
  308. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  309. (org-trash (or
  310. (and (fboundp 'org-release) (fboundp 'org-git-version))
  311. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  312. (load-suffixes save-load-suffixes)
  313. (org-version (org-release))
  314. (git-version (org-git-version))
  315. (version (format "Org-mode version %s (%s @ %s)"
  316. org-version
  317. git-version
  318. (if org-install-dir
  319. (if (string= org-dir org-install-dir)
  320. org-install-dir
  321. (concat "mixed installation! " org-install-dir " and " org-dir))
  322. "org-loaddefs.el can not be found!")))
  323. (version1 (if full version org-version)))
  324. (when here (insert version1))
  325. (when message (message "%s" version1))
  326. version1))
  327. (defconst org-version (org-version))
  328. ;;; Syntax Constants
  329. ;;;; Block
  330. (defconst org-block-regexp
  331. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  332. "Regular expression for hiding blocks.")
  333. (defconst org-dblock-start-re
  334. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  335. "Matches the start line of a dynamic block, with parameters.")
  336. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  337. "Matches the end of a dynamic block.")
  338. ;;;; Clock and Planning
  339. (defconst org-clock-string "CLOCK:"
  340. "String used as prefix for timestamps clocking work hours on an item.")
  341. (defvar org-closed-string "CLOSED:"
  342. "String used as the prefix for timestamps logging closing a TODO entry.")
  343. (defvar org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].")
  348. (defvar org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].")
  353. (defconst org-ds-keyword-length
  354. (+ 2
  355. (apply #'max
  356. (mapcar #'length
  357. (list org-deadline-string org-scheduled-string
  358. org-clock-string org-closed-string))))
  359. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  360. (defconst org-planning-line-re
  361. (concat "^[ \t]*"
  362. (regexp-opt
  363. (list org-closed-string org-deadline-string org-scheduled-string)
  364. t))
  365. "Matches a line with planning info.
  366. Matched keyword is in group 1.")
  367. (defconst org-clock-line-re
  368. (concat "^[ \t]*" org-clock-string)
  369. "Matches a line with clock info.")
  370. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  371. "Matches the DEADLINE keyword.")
  372. (defconst org-deadline-time-regexp
  373. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  374. "Matches the DEADLINE keyword together with a time stamp.")
  375. (defconst org-deadline-time-hour-regexp
  376. (concat "\\<" org-deadline-string
  377. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  378. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  379. (defconst org-deadline-line-regexp
  380. (concat "\\<\\(" org-deadline-string "\\).*")
  381. "Matches the DEADLINE keyword and the rest of the line.")
  382. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  383. "Matches the SCHEDULED keyword.")
  384. (defconst org-scheduled-time-regexp
  385. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  386. "Matches the SCHEDULED keyword together with a time stamp.")
  387. (defconst org-scheduled-time-hour-regexp
  388. (concat "\\<" org-scheduled-string
  389. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  390. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  391. (defconst org-closed-time-regexp
  392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  393. "Matches the CLOSED keyword together with a time stamp.")
  394. (defconst org-keyword-time-regexp
  395. (concat "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string
  398. org-clock-string)
  399. t)
  400. " *[[<]\\([^]>]+\\)[]>]")
  401. "Matches any of the 4 keywords, together with the time stamp.")
  402. (defconst org-keyword-time-not-clock-regexp
  403. (concat
  404. "\\<"
  405. (regexp-opt
  406. (list org-scheduled-string org-deadline-string org-closed-string) t)
  407. " *[[<]\\([^]>]+\\)[]>]")
  408. "Matches any of the 3 keywords, together with the time stamp.")
  409. (defconst org-maybe-keyword-time-regexp
  410. (concat "\\(\\<"
  411. (regexp-opt
  412. (list org-scheduled-string org-deadline-string org-closed-string
  413. org-clock-string)
  414. t)
  415. "\\)?"
  416. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  417. "\\|"
  418. "<%%([^\r\n>]*>\\)")
  419. "Matches a timestamp, possibly preceded by a keyword.")
  420. (defconst org-all-time-keywords
  421. (mapcar (lambda (w) (substring w 0 -1))
  422. (list org-scheduled-string org-deadline-string
  423. org-clock-string org-closed-string))
  424. "List of time keywords.")
  425. ;;;; Drawer
  426. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  427. "Matches first or last line of a hidden block.
  428. Group 1 contains drawer's name or \"END\".")
  429. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  430. "Regular expression matching the first line of a property drawer.")
  431. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  432. "Regular expression matching the last line of a property drawer.")
  433. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  434. "Regular expression matching the first line of a clock drawer.")
  435. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  436. "Regular expression matching the last line of a clock drawer.")
  437. (defconst org-property-drawer-re
  438. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  439. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  440. "[ \t]*:END:[ \t]*$")
  441. "Matches an entire property drawer.")
  442. (defconst org-clock-drawer-re
  443. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  444. org-clock-drawer-end-re "\\)\n?")
  445. "Matches an entire clock drawer.")
  446. ;;;; Headline
  447. (defconst org-heading-keyword-regexp-format
  448. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  449. "Printf format for a regexp matching a headline with some keyword.
  450. This regexp will match the headline of any node which has the
  451. exact keyword that is put into the format. The keyword isn't in
  452. any group by default, but the stars and the body are.")
  453. (defconst org-heading-keyword-maybe-regexp-format
  454. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  455. "Printf format for a regexp matching a headline, possibly with some keyword.
  456. This regexp can match any headline with the specified keyword, or
  457. without a keyword. The keyword isn't in any group by default,
  458. but the stars and the body are.")
  459. (defconst org-archive-tag "ARCHIVE"
  460. "The tag that marks a subtree as archived.
  461. An archived subtree does not open during visibility cycling, and does
  462. not contribute to the agenda listings.")
  463. (defconst org-comment-string "COMMENT"
  464. "Entries starting with this keyword will never be exported.
  465. An entry can be toggled between COMMENT and normal with
  466. \\[org-toggle-comment].")
  467. ;;;; LaTeX Environments and Fragments
  468. (defconst org-latex-regexps
  469. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  470. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  471. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  472. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  474. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  475. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  476. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  477. "Regular expressions for matching embedded LaTeX.")
  478. ;;;; Node Property
  479. (defconst org-effort-property "Effort"
  480. "The property that is being used to keep track of effort estimates.
  481. Effort estimates given in this property need to have the format H:MM.")
  482. ;;;; Table
  483. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  484. "Detect an org-type or table-type table.")
  485. (defconst org-table-line-regexp "^[ \t]*|"
  486. "Detect an org-type table line.")
  487. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  488. "Detect an org-type table line.")
  489. (defconst org-table-hline-regexp "^[ \t]*|-"
  490. "Detect an org-type table hline.")
  491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  492. "Detect a table-type table hline.")
  493. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  494. "Detect the first line outside a table when searching from within it.
  495. This works for both table types.")
  496. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  497. "Detect a #+TBLFM line.")
  498. ;;;; Timestamp
  499. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  500. "Regular expression for fast time stamp matching.")
  501. (defconst org-ts-regexp-inactive
  502. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  503. "Regular expression for fast inactive time stamp matching.")
  504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  505. "Regular expression for fast time stamp matching.")
  506. (defconst org-ts-regexp0
  507. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  508. "Regular expression matching time strings for analysis.
  509. This one does not require the space after the date, so it can be used
  510. on a string that terminates immediately after the date.")
  511. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  512. "Regular expression matching time strings for analysis.")
  513. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  514. "Regular expression matching time stamps, with groups.")
  515. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  516. "Regular expression matching time stamps (also [..]), with groups.")
  517. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tr-regexp-both
  520. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  521. "Regular expression matching a time stamp range.")
  522. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  523. org-ts-regexp "\\)?")
  524. "Regular expression matching a time stamp or time stamp range.")
  525. (defconst org-tsr-regexp-both
  526. (concat org-ts-regexp-both "\\(--?-?"
  527. org-ts-regexp-both "\\)?")
  528. "Regular expression matching a time stamp or time stamp range.
  529. The time stamps may be either active or inactive.")
  530. (defconst org-repeat-re
  531. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  532. "Regular expression for specifying repeated events.
  533. After a match, group 1 contains the repeat expression.")
  534. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  535. "Formats for `format-time-string' which are used for time stamps.")
  536. ;;; The custom variables
  537. (defgroup org nil
  538. "Outline-based notes management and organizer."
  539. :tag "Org"
  540. :group 'outlines
  541. :group 'calendar)
  542. (defcustom org-mode-hook nil
  543. "Mode hook for Org-mode, run after the mode was turned on."
  544. :group 'org
  545. :type 'hook)
  546. (defcustom org-load-hook nil
  547. "Hook that is run after org.el has been loaded."
  548. :group 'org
  549. :type 'hook)
  550. (defcustom org-log-buffer-setup-hook nil
  551. "Hook that is run after an Org log buffer is created."
  552. :group 'org
  553. :version "24.1"
  554. :type 'hook)
  555. (defvar org-modules) ; defined below
  556. (defvar org-modules-loaded nil
  557. "Have the modules been loaded already?")
  558. (defun org-load-modules-maybe (&optional force)
  559. "Load all extensions listed in `org-modules'."
  560. (when (or force (not org-modules-loaded))
  561. (mapc (lambda (ext)
  562. (condition-case nil (require ext)
  563. (error (message "Problems while trying to load feature `%s'" ext))))
  564. org-modules)
  565. (setq org-modules-loaded t)))
  566. (defun org-set-modules (var value)
  567. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  568. (set var value)
  569. (when (featurep 'org)
  570. (org-load-modules-maybe 'force)
  571. (org-element-cache-reset 'all)))
  572. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  573. "Modules that should always be loaded together with org.el.
  574. If a description starts with <C>, the file is not part of Emacs
  575. and loading it will require that you have downloaded and properly
  576. installed the Org mode distribution.
  577. You can also use this system to load external packages (i.e. neither Org
  578. core modules, nor modules from the CONTRIB directory). Just add symbols
  579. to the end of the list. If the package is called org-xyz.el, then you need
  580. to add the symbol `xyz', and the package must have a call to:
  581. \(provide 'org-xyz)
  582. For export specific modules, see also `org-export-backends'."
  583. :group 'org
  584. :set 'org-set-modules
  585. :version "24.4"
  586. :package-version '(Org . "8.0")
  587. :type
  588. '(set :greedy t
  589. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  590. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  591. (const :tag " crypt: Encryption of subtrees" org-crypt)
  592. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  593. (const :tag " docview: Links to doc-view buffers" org-docview)
  594. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  595. (const :tag " habit: Track your consistency with habits" org-habit)
  596. (const :tag " id: Global IDs for identifying entries" org-id)
  597. (const :tag " info: Links to Info nodes" org-info)
  598. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  599. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  600. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  601. (const :tag " mouse: Additional mouse support" org-mouse)
  602. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  603. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  604. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  605. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  606. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  607. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  608. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  609. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  610. (const :tag "C collector: Collect properties into tables" org-collector)
  611. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  612. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  613. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  614. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  615. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  616. (const :tag "C eval: Include command output as text" org-eval)
  617. (const :tag "C eww: Store link to url of eww" org-eww)
  618. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  619. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  620. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  621. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  622. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  623. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  624. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  625. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  626. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  627. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  628. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  629. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  630. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  631. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  632. (const :tag "C registry: A registry for Org-mode links" org-registry)
  633. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  634. (const :tag "C secretary: Team management with org-mode" org-secretary)
  635. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  636. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  637. (const :tag "C track: Keep up with Org-mode development" org-track)
  638. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  639. (const :tag "C vm: Links to VM folders/messages" org-vm)
  640. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  641. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  642. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  643. (defvar org-export-registered-backends) ; From ox.el.
  644. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  645. (declare-function org-export-backend-name "ox" (backend))
  646. (defcustom org-export-backends '(ascii html icalendar latex)
  647. "List of export back-ends that should be always available.
  648. If a description starts with <C>, the file is not part of Emacs
  649. and loading it will require that you have downloaded and properly
  650. installed the Org mode distribution.
  651. Unlike to `org-modules', libraries in this list will not be
  652. loaded along with Org, but only once the export framework is
  653. needed.
  654. This variable needs to be set before org.el is loaded. If you
  655. need to make a change while Emacs is running, use the customize
  656. interface or run the following code, where VAL stands for the new
  657. value of the variable, after updating it:
  658. \(progn
  659. \(setq org-export-registered-backends
  660. \(org-remove-if-not
  661. \(lambda (backend)
  662. \(let ((name (org-export-backend-name backend)))
  663. \(or (memq name val)
  664. \(catch 'parentp
  665. \(dolist (b val)
  666. \(and (org-export-derived-backend-p b name)
  667. \(throw 'parentp t)))))))
  668. org-export-registered-backends))
  669. \(let ((new-list (mapcar #'org-export-backend-name
  670. org-export-registered-backends)))
  671. \(dolist (backend val)
  672. \(cond
  673. \((not (load (format \"ox-%s\" backend) t t))
  674. \(message \"Problems while trying to load export back-end `%s'\"
  675. backend))
  676. \((not (memq backend new-list)) (push backend new-list))))
  677. \(set-default 'org-export-backends new-list)))
  678. Adding a back-end to this list will also pull the back-end it
  679. depends on, if any."
  680. :group 'org
  681. :group 'org-export
  682. :version "24.4"
  683. :package-version '(Org . "8.0")
  684. :initialize 'custom-initialize-set
  685. :set (lambda (var val)
  686. (if (not (featurep 'ox)) (set-default var val)
  687. ;; Any back-end not required anymore (not present in VAL and not
  688. ;; a parent of any back-end in the new value) is removed from the
  689. ;; list of registered back-ends.
  690. (setq org-export-registered-backends
  691. (org-remove-if-not
  692. (lambda (backend)
  693. (let ((name (org-export-backend-name backend)))
  694. (or (memq name val)
  695. (catch 'parentp
  696. (dolist (b val)
  697. (and (org-export-derived-backend-p b name)
  698. (throw 'parentp t)))))))
  699. org-export-registered-backends))
  700. ;; Now build NEW-LIST of both new back-ends and required
  701. ;; parents.
  702. (let ((new-list (mapcar #'org-export-backend-name
  703. org-export-registered-backends)))
  704. (dolist (backend val)
  705. (cond
  706. ((not (load (format "ox-%s" backend) t t))
  707. (message "Problems while trying to load export back-end `%s'"
  708. backend))
  709. ((not (memq backend new-list)) (push backend new-list))))
  710. ;; Set VAR to that list with fixed dependencies.
  711. (set-default var new-list))))
  712. :type '(set :greedy t
  713. (const :tag " ascii Export buffer to ASCII format" ascii)
  714. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  715. (const :tag " html Export buffer to HTML format" html)
  716. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  717. (const :tag " latex Export buffer to LaTeX format" latex)
  718. (const :tag " man Export buffer to MAN format" man)
  719. (const :tag " md Export buffer to Markdown format" md)
  720. (const :tag " odt Export buffer to ODT format" odt)
  721. (const :tag " org Export buffer to Org format" org)
  722. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  723. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  724. (const :tag "C deck Export buffer to deck.js presentations" deck)
  725. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  726. (const :tag "C groff Export buffer to Groff format" groff)
  727. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  728. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  729. (const :tag "C s5 Export buffer to s5 presentations" s5)
  730. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  731. (eval-after-load 'ox
  732. '(mapc
  733. (lambda (backend)
  734. (condition-case nil (require (intern (format "ox-%s" backend)))
  735. (error (message "Problems while trying to load export back-end `%s'"
  736. backend))))
  737. org-export-backends))
  738. (defcustom org-support-shift-select nil
  739. "Non-nil means make shift-cursor commands select text when possible.
  740. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  741. start selecting a region, or enlarge regions started in this way.
  742. In Org-mode, in special contexts, these same keys are used for
  743. other purposes, important enough to compete with shift selection.
  744. Org tries to balance these needs by supporting `shift-select-mode'
  745. outside these special contexts, under control of this variable.
  746. The default of this variable is nil, to avoid confusing behavior. Shifted
  747. cursor keys will then execute Org commands in the following contexts:
  748. - on a headline, changing TODO state (left/right) and priority (up/down)
  749. - on a time stamp, changing the time
  750. - in a plain list item, changing the bullet type
  751. - in a property definition line, switching between allowed values
  752. - in the BEGIN line of a clock table (changing the time block).
  753. Outside these contexts, the commands will throw an error.
  754. When this variable is t and the cursor is not in a special
  755. context, Org-mode will support shift-selection for making and
  756. enlarging regions. To make this more effective, the bullet
  757. cycling will no longer happen anywhere in an item line, but only
  758. if the cursor is exactly on the bullet.
  759. If you set this variable to the symbol `always', then the keys
  760. will not be special in headlines, property lines, and item lines,
  761. to make shift selection work there as well. If this is what you
  762. want, you can use the following alternative commands: `C-c C-t'
  763. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  764. can be used to switch TODO sets, `C-c -' to cycle item bullet
  765. types, and properties can be edited by hand or in column view.
  766. However, when the cursor is on a timestamp, shift-cursor commands
  767. will still edit the time stamp - this is just too good to give up.
  768. XEmacs user should have this variable set to nil, because
  769. `shift-select-mode' is in Emacs 23 or later only."
  770. :group 'org
  771. :type '(choice
  772. (const :tag "Never" nil)
  773. (const :tag "When outside special context" t)
  774. (const :tag "Everywhere except timestamps" always)))
  775. (defcustom org-loop-over-headlines-in-active-region nil
  776. "Shall some commands act upon headlines in the active region?
  777. When set to t, some commands will be performed in all headlines
  778. within the active region.
  779. When set to `start-level', some commands will be performed in all
  780. headlines within the active region, provided that these headlines
  781. are of the same level than the first one.
  782. When set to a string, those commands will be performed on the
  783. matching headlines within the active region. Such string must be
  784. a tags/property/todo match as it is used in the agenda tags view.
  785. The list of commands is: `org-schedule', `org-deadline',
  786. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  787. `org-archive-to-archive-sibling'. The archiving commands skip
  788. already archived entries."
  789. :type '(choice (const :tag "Don't loop" nil)
  790. (const :tag "All headlines in active region" t)
  791. (const :tag "In active region, headlines at the same level than the first one" start-level)
  792. (string :tag "Tags/Property/Todo matcher"))
  793. :version "24.1"
  794. :group 'org-todo
  795. :group 'org-archive)
  796. (defgroup org-startup nil
  797. "Options concerning startup of Org-mode."
  798. :tag "Org Startup"
  799. :group 'org)
  800. (defcustom org-startup-folded t
  801. "Non-nil means entering Org-mode will switch to OVERVIEW.
  802. This can also be configured on a per-file basis by adding one of
  803. the following lines anywhere in the buffer:
  804. #+STARTUP: fold (or `overview', this is equivalent)
  805. #+STARTUP: nofold (or `showall', this is equivalent)
  806. #+STARTUP: content
  807. #+STARTUP: showeverything
  808. By default, this option is ignored when Org opens agenda files
  809. for the first time. If you want the agenda to honor the startup
  810. option, set `org-agenda-inhibit-startup' to nil."
  811. :group 'org-startup
  812. :type '(choice
  813. (const :tag "nofold: show all" nil)
  814. (const :tag "fold: overview" t)
  815. (const :tag "content: all headlines" content)
  816. (const :tag "show everything, even drawers" showeverything)))
  817. (defcustom org-startup-truncated t
  818. "Non-nil means entering Org-mode will set `truncate-lines'.
  819. This is useful since some lines containing links can be very long and
  820. uninteresting. Also tables look terrible when wrapped."
  821. :group 'org-startup
  822. :type 'boolean)
  823. (defcustom org-startup-indented nil
  824. "Non-nil means turn on `org-indent-mode' on startup.
  825. This can also be configured on a per-file basis by adding one of
  826. the following lines anywhere in the buffer:
  827. #+STARTUP: indent
  828. #+STARTUP: noindent"
  829. :group 'org-structure
  830. :type '(choice
  831. (const :tag "Not" nil)
  832. (const :tag "Globally (slow on startup in large files)" t)))
  833. (defcustom org-use-sub-superscripts t
  834. "Non-nil means interpret \"_\" and \"^\" for display.
  835. If you want to control how Org exports those characters, see
  836. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  837. used to be an alias for `org-export-with-sub-superscripts' in
  838. Org <8.0, it is not anymore.
  839. When this option is turned on, you can use TeX-like syntax for
  840. sub- and superscripts within the buffer. Several characters after
  841. \"_\" or \"^\" will be considered as a single item - so grouping
  842. with {} is normally not needed. For example, the following things
  843. will be parsed as single sub- or superscripts:
  844. 10^24 or 10^tau several digits will be considered 1 item.
  845. 10^-12 or 10^-tau a leading sign with digits or a word
  846. x^2-y^3 will be read as x^2 - y^3, because items are
  847. terminated by almost any nonword/nondigit char.
  848. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  849. Still, ambiguity is possible. So when in doubt, use {} to enclose
  850. the sub/superscript. If you set this variable to the symbol `{}',
  851. the braces are *required* in order to trigger interpretations as
  852. sub/superscript. This can be helpful in documents that need \"_\"
  853. frequently in plain text."
  854. :group 'org-startup
  855. :version "24.4"
  856. :package-version '(Org . "8.0")
  857. :type '(choice
  858. (const :tag "Always interpret" t)
  859. (const :tag "Only with braces" {})
  860. (const :tag "Never interpret" nil)))
  861. (defcustom org-startup-with-beamer-mode nil
  862. "Non-nil means turn on `org-beamer-mode' on startup.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: beamer"
  866. :group 'org-startup
  867. :version "24.1"
  868. :type 'boolean)
  869. (defcustom org-startup-align-all-tables nil
  870. "Non-nil means align all tables when visiting a file.
  871. This is useful when the column width in tables is forced with <N> cookies
  872. in table fields. Such tables will look correct only after the first re-align.
  873. This can also be configured on a per-file basis by adding one of
  874. the following lines anywhere in the buffer:
  875. #+STARTUP: align
  876. #+STARTUP: noalign"
  877. :group 'org-startup
  878. :type 'boolean)
  879. (defcustom org-startup-with-inline-images nil
  880. "Non-nil means show inline images when loading a new Org file.
  881. This can also be configured on a per-file basis by adding one of
  882. the following lines anywhere in the buffer:
  883. #+STARTUP: inlineimages
  884. #+STARTUP: noinlineimages"
  885. :group 'org-startup
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom org-startup-with-latex-preview nil
  889. "Non-nil means preview LaTeX fragments when loading a new Org file.
  890. This can also be configured on a per-file basis by adding one of
  891. the following lines anywhere in the buffer:
  892. #+STARTUP: latexpreview
  893. #+STARTUP: nolatexpreview"
  894. :group 'org-startup
  895. :version "24.4"
  896. :package-version '(Org . "8.0")
  897. :type 'boolean)
  898. (defcustom org-insert-mode-line-in-empty-file nil
  899. "Non-nil means insert the first line setting Org-mode in empty files.
  900. When the function `org-mode' is called interactively in an empty file, this
  901. normally means that the file name does not automatically trigger Org-mode.
  902. To ensure that the file will always be in Org-mode in the future, a
  903. line enforcing Org-mode will be inserted into the buffer, if this option
  904. has been set."
  905. :group 'org-startup
  906. :type 'boolean)
  907. (defcustom org-replace-disputed-keys nil
  908. "Non-nil means use alternative key bindings for some keys.
  909. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  910. These keys are also used by other packages like shift-selection-mode'
  911. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  912. If you want to use Org-mode together with one of these other modes,
  913. or more generally if you would like to move some Org-mode commands to
  914. other keys, set this variable and configure the keys with the variable
  915. `org-disputed-keys'.
  916. This option is only relevant at load-time of Org-mode, and must be set
  917. *before* org.el is loaded. Changing it requires a restart of Emacs to
  918. become effective."
  919. :group 'org-startup
  920. :type 'boolean)
  921. (defcustom org-use-extra-keys nil
  922. "Non-nil means use extra key sequence definitions for certain commands.
  923. This happens automatically if you run XEmacs or if `window-system'
  924. is nil. This variable lets you do the same manually. You must
  925. set it before loading org.
  926. Example: on Carbon Emacs 22 running graphically, with an external
  927. keyboard on a Powerbook, the default way of setting M-left might
  928. not work for either Alt or ESC. Setting this variable will make
  929. it work for ESC."
  930. :group 'org-startup
  931. :type 'boolean)
  932. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  933. (defcustom org-disputed-keys
  934. '(([(shift up)] . [(meta p)])
  935. ([(shift down)] . [(meta n)])
  936. ([(shift left)] . [(meta -)])
  937. ([(shift right)] . [(meta +)])
  938. ([(control shift right)] . [(meta shift +)])
  939. ([(control shift left)] . [(meta shift -)]))
  940. "Keys for which Org-mode and other modes compete.
  941. This is an alist, cars are the default keys, second element specifies
  942. the alternative to use when `org-replace-disputed-keys' is t.
  943. Keys can be specified in any syntax supported by `define-key'.
  944. The value of this option takes effect only at Org-mode's startup,
  945. therefore you'll have to restart Emacs to apply it after changing."
  946. :group 'org-startup
  947. :type 'alist)
  948. (defun org-key (key)
  949. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  950. Or return the original if not disputed.
  951. Also apply the translations defined in `org-xemacs-key-equivalents'."
  952. (when org-replace-disputed-keys
  953. (let* ((nkey (key-description key))
  954. (x (org-find-if (lambda (x)
  955. (equal (key-description (car x)) nkey))
  956. org-disputed-keys)))
  957. (setq key (if x (cdr x) key))))
  958. (when (featurep 'xemacs)
  959. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  960. key)
  961. (defun org-find-if (predicate seq)
  962. (catch 'exit
  963. (while seq
  964. (if (funcall predicate (car seq))
  965. (throw 'exit (car seq))
  966. (pop seq)))))
  967. (defun org-defkey (keymap key def)
  968. "Define a key, possibly translated, as returned by `org-key'."
  969. (define-key keymap (org-key key) def))
  970. (defcustom org-ellipsis nil
  971. "The ellipsis to use in the Org-mode outline.
  972. When nil, just use the standard three dots.
  973. When a string, use that string instead.
  974. When a face, use the standard 3 dots, but with the specified face.
  975. The change affects only Org-mode (which will then use its own display table).
  976. Changing this requires executing \\[org-mode] in a buffer to become
  977. effective."
  978. :group 'org-startup
  979. :type '(choice (const :tag "Default" nil)
  980. (face :tag "Face" :value org-warning)
  981. (string :tag "String" :value "...#")))
  982. (defvar org-display-table nil
  983. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  984. (defgroup org-keywords nil
  985. "Keywords in Org-mode."
  986. :tag "Org Keywords"
  987. :group 'org)
  988. (defcustom org-closed-keep-when-no-todo nil
  989. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  990. :group 'org-todo
  991. :group 'org-keywords
  992. :version "24.4"
  993. :package-version '(Org . "8.0")
  994. :type 'boolean)
  995. (defgroup org-structure nil
  996. "Options concerning the general structure of Org-mode files."
  997. :tag "Org Structure"
  998. :group 'org)
  999. (defgroup org-reveal-location nil
  1000. "Options about how to make context of a location visible."
  1001. :tag "Org Reveal Location"
  1002. :group 'org-structure)
  1003. (defcustom org-show-context-detail '((isearch . lineage)
  1004. (bookmark-jump . lineage)
  1005. (default . ancestors))
  1006. "Alist between context and visibility span when revealing a location.
  1007. \\<org-mode-map>Some actions may move point into invisible
  1008. locations. As a consequence, Org always expose a neighborhood
  1009. around point. How much is shown depends on the initial action,
  1010. or context. Valid contexts are
  1011. agenda when exposing an entry from the agenda
  1012. org-goto when using the command `org-goto' (\\[org-goto])
  1013. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1014. tags-tree when constructing a sparse tree based on tags matches
  1015. link-search when exposing search matches associated with a link
  1016. mark-goto when exposing the jump goal of a mark
  1017. bookmark-jump when exposing a bookmark location
  1018. isearch when exiting from an incremental search
  1019. default default for all contexts not set explicitly
  1020. Allowed visibility spans are
  1021. minimal show current headline; if point is not on headline,
  1022. also show entry
  1023. local show current headline, entry and next headline
  1024. ancestors show current headline and its direct ancestors; if
  1025. point is not on headline, also show entry
  1026. lineage show current headline, its direct ancestors and all
  1027. their children; if point is not on headline, also show
  1028. entry and first child
  1029. tree show current headline, its direct ancestors and all
  1030. their children; if point is not on headline, also show
  1031. entry and all children
  1032. canonical show current headline, its direct ancestors along with
  1033. their entries and children; if point is not located on
  1034. the headline, also show current entry and all children
  1035. As special cases, a nil or t value means show all contexts in
  1036. `minimal' or `canonical' view, respectively.
  1037. Some views can make displayed information very compact, but also
  1038. make it harder to edit the location of the match. In such
  1039. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1040. more context."
  1041. :group 'org-reveal-location
  1042. :version "25.1"
  1043. :package-version '(Org . "8.3")
  1044. :type '(choice
  1045. (const :tag "Canonical" t)
  1046. (const :tag "Minimal" nil)
  1047. (repeat :greedy t :tag "Individual contexts"
  1048. (cons
  1049. (choice :tag "Context"
  1050. (const agenda)
  1051. (const org-goto)
  1052. (const occur-tree)
  1053. (const tags-tree)
  1054. (const link-search)
  1055. (const mark-goto)
  1056. (const bookmark-jump)
  1057. (const isearch)
  1058. (const default))
  1059. (choice :tag "Detail level"
  1060. (const minimal)
  1061. (const local)
  1062. (const ancestors)
  1063. (const lineage)
  1064. (const tree)
  1065. (const canonical))))))
  1066. (defcustom org-indirect-buffer-display 'other-window
  1067. "How should indirect tree buffers be displayed?
  1068. This applies to indirect buffers created with the commands
  1069. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1070. Valid values are:
  1071. current-window Display in the current window
  1072. other-window Just display in another window.
  1073. dedicated-frame Create one new frame, and re-use it each time.
  1074. new-frame Make a new frame each time. Note that in this case
  1075. previously-made indirect buffers are kept, and you need to
  1076. kill these buffers yourself."
  1077. :group 'org-structure
  1078. :group 'org-agenda-windows
  1079. :type '(choice
  1080. (const :tag "In current window" current-window)
  1081. (const :tag "In current frame, other window" other-window)
  1082. (const :tag "Each time a new frame" new-frame)
  1083. (const :tag "One dedicated frame" dedicated-frame)))
  1084. (defcustom org-use-speed-commands nil
  1085. "Non-nil means activate single letter commands at beginning of a headline.
  1086. This may also be a function to test for appropriate locations where speed
  1087. commands should be active.
  1088. For example, to activate speed commands when the point is on any
  1089. star at the beginning of the headline, you can do this:
  1090. (setq org-use-speed-commands
  1091. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1092. :group 'org-structure
  1093. :type '(choice
  1094. (const :tag "Never" nil)
  1095. (const :tag "At beginning of headline stars" t)
  1096. (function)))
  1097. (defcustom org-speed-commands-user nil
  1098. "Alist of additional speed commands.
  1099. This list will be checked before `org-speed-commands-default'
  1100. when the variable `org-use-speed-commands' is non-nil
  1101. and when the cursor is at the beginning of a headline.
  1102. The car if each entry is a string with a single letter, which must
  1103. be assigned to `self-insert-command' in the global map.
  1104. The cdr is either a command to be called interactively, a function
  1105. to be called, or a form to be evaluated.
  1106. An entry that is just a list with a single string will be interpreted
  1107. as a descriptive headline that will be added when listing the speed
  1108. commands in the Help buffer using the `?' speed command."
  1109. :group 'org-structure
  1110. :type '(repeat :value ("k" . ignore)
  1111. (choice :value ("k" . ignore)
  1112. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1113. (cons :tag "Letter and Command"
  1114. (string :tag "Command letter")
  1115. (choice
  1116. (function)
  1117. (sexp))))))
  1118. (defcustom org-bookmark-names-plist
  1119. '(:last-capture "org-capture-last-stored"
  1120. :last-refile "org-refile-last-stored"
  1121. :last-capture-marker "org-capture-last-stored-marker")
  1122. "Names for bookmarks automatically set by some Org commands.
  1123. This can provide strings as names for a number of bookmarks Org sets
  1124. automatically. The following keys are currently implemented:
  1125. :last-capture
  1126. :last-capture-marker
  1127. :last-refile
  1128. When a key does not show up in the property list, the corresponding bookmark
  1129. is not set."
  1130. :group 'org-structure
  1131. :type 'plist)
  1132. (defgroup org-cycle nil
  1133. "Options concerning visibility cycling in Org-mode."
  1134. :tag "Org Cycle"
  1135. :group 'org-structure)
  1136. (defcustom org-cycle-skip-children-state-if-no-children t
  1137. "Non-nil means skip CHILDREN state in entries that don't have any."
  1138. :group 'org-cycle
  1139. :type 'boolean)
  1140. (defcustom org-cycle-max-level nil
  1141. "Maximum level which should still be subject to visibility cycling.
  1142. Levels higher than this will, for cycling, be treated as text, not a headline.
  1143. When `org-odd-levels-only' is set, a value of N in this variable actually
  1144. means 2N-1 stars as the limiting headline.
  1145. When nil, cycle all levels.
  1146. Note that the limiting level of cycling is also influenced by
  1147. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1148. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1149. than its value."
  1150. :group 'org-cycle
  1151. :type '(choice
  1152. (const :tag "No limit" nil)
  1153. (integer :tag "Maximum level")))
  1154. (defcustom org-hide-block-startup nil
  1155. "Non-nil means entering Org-mode will fold all blocks.
  1156. This can also be set in on a per-file basis with
  1157. #+STARTUP: hideblocks
  1158. #+STARTUP: showblocks"
  1159. :group 'org-startup
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-global-at-bob nil
  1163. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1164. This makes it possible to do global cycling without having to use S-TAB or
  1165. \\[universal-argument] TAB. For this special case to work, the first line
  1166. of the buffer must not be a headline -- it may be empty or some other text.
  1167. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1168. sure the cursor stays at the beginning of the buffer. When this option is
  1169. nil, don't do anything special at the beginning of the buffer."
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-level-after-item/entry-creation t
  1173. "Non-nil means cycle entry level or item indentation in new empty entries.
  1174. When the cursor is at the end of an empty headline, i.e., with only stars
  1175. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1176. and then all possible ancestor states, before returning to the original state.
  1177. This makes data entry extremely fast: M-RET to create a new headline,
  1178. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1179. When the cursor is at the end of an empty plain list item, one TAB will
  1180. make it a subitem, two or more tabs will back up to make this an item
  1181. higher up in the item hierarchy."
  1182. :group 'org-cycle
  1183. :type 'boolean)
  1184. (defcustom org-cycle-emulate-tab t
  1185. "Where should `org-cycle' emulate TAB.
  1186. nil Never
  1187. white Only in completely white lines
  1188. whitestart Only at the beginning of lines, before the first non-white char
  1189. t Everywhere except in headlines
  1190. exc-hl-bol Everywhere except at the start of a headline
  1191. If TAB is used in a place where it does not emulate TAB, the current subtree
  1192. visibility is cycled."
  1193. :group 'org-cycle
  1194. :type '(choice (const :tag "Never" nil)
  1195. (const :tag "Only in completely white lines" white)
  1196. (const :tag "Before first char in a line" whitestart)
  1197. (const :tag "Everywhere except in headlines" t)
  1198. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1199. (defcustom org-cycle-separator-lines 2
  1200. "Number of empty lines needed to keep an empty line between collapsed trees.
  1201. If you leave an empty line between the end of a subtree and the following
  1202. headline, this empty line is hidden when the subtree is folded.
  1203. Org-mode will leave (exactly) one empty line visible if the number of
  1204. empty lines is equal or larger to the number given in this variable.
  1205. So the default 2 means at least 2 empty lines after the end of a subtree
  1206. are needed to produce free space between a collapsed subtree and the
  1207. following headline.
  1208. If the number is negative, and the number of empty lines is at least -N,
  1209. all empty lines are shown.
  1210. Special case: when 0, never leave empty lines in collapsed view."
  1211. :group 'org-cycle
  1212. :type 'integer)
  1213. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1214. (defcustom org-pre-cycle-hook nil
  1215. "Hook that is run before visibility cycling is happening.
  1216. The function(s) in this hook must accept a single argument which indicates
  1217. the new state that will be set right after running this hook. The
  1218. argument is a symbol. Before a global state change, it can have the values
  1219. `overview', `content', or `all'. Before a local state change, it can have
  1220. the values `folded', `children', or `subtree'."
  1221. :group 'org-cycle
  1222. :type 'hook)
  1223. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1224. org-cycle-hide-drawers
  1225. org-cycle-show-empty-lines
  1226. org-optimize-window-after-visibility-change)
  1227. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1228. The function(s) in this hook must accept a single argument which indicates
  1229. the new state that was set by the most recent `org-cycle' command. The
  1230. argument is a symbol. After a global state change, it can have the values
  1231. `overview', `contents', or `all'. After a local state change, it can have
  1232. the values `folded', `children', or `subtree'."
  1233. :group 'org-cycle
  1234. :type 'hook
  1235. :version "25.1"
  1236. :package-version '(Org . "8.3"))
  1237. (defgroup org-edit-structure nil
  1238. "Options concerning structure editing in Org-mode."
  1239. :tag "Org Edit Structure"
  1240. :group 'org-structure)
  1241. (defcustom org-odd-levels-only nil
  1242. "Non-nil means skip even levels and only use odd levels for the outline.
  1243. This has the effect that two stars are being added/taken away in
  1244. promotion/demotion commands. It also influences how levels are
  1245. handled by the exporters.
  1246. Changing it requires restart of `font-lock-mode' to become effective
  1247. for fontification also in regions already fontified.
  1248. You may also set this on a per-file basis by adding one of the following
  1249. lines to the buffer:
  1250. #+STARTUP: odd
  1251. #+STARTUP: oddeven"
  1252. :group 'org-edit-structure
  1253. :group 'org-appearance
  1254. :type 'boolean)
  1255. (defcustom org-adapt-indentation t
  1256. "Non-nil means adapt indentation to outline node level.
  1257. When this variable is set, Org assumes that you write outlines by
  1258. indenting text in each node to align with the headline (after the
  1259. stars). The following issues are influenced by this variable:
  1260. - The indentation is increased by one space in a demotion
  1261. command, and decreased by one in a promotion command. However,
  1262. in the latter case, if shifting some line in the entry body
  1263. would alter document structure (e.g., insert a new headline),
  1264. indentation is not changed at all.
  1265. - Property drawers and planning information is inserted indented
  1266. when this variable is set. When nil, they will not be indented.
  1267. - TAB indents a line relative to current level. The lines below
  1268. a headline will be indented when this variable is set.
  1269. Note that this is all about true indentation, by adding and
  1270. removing space characters. See also `org-indent.el' which does
  1271. level-dependent indentation in a virtual way, i.e. at display
  1272. time in Emacs."
  1273. :group 'org-edit-structure
  1274. :type 'boolean)
  1275. (defcustom org-special-ctrl-a/e nil
  1276. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1277. When t, `C-a' will bring back the cursor to the beginning of the
  1278. headline text, i.e. after the stars and after a possible TODO
  1279. keyword. In an item, this will be the position after bullet and
  1280. check-box, if any. When the cursor is already at that position,
  1281. another `C-a' will bring it to the beginning of the line.
  1282. `C-e' will jump to the end of the headline, ignoring the presence
  1283. of tags in the headline. A second `C-e' will then jump to the
  1284. true end of the line, after any tags. This also means that, when
  1285. this variable is non-nil, `C-e' also will never jump beyond the
  1286. end of the heading of a folded section, i.e. not after the
  1287. ellipses.
  1288. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1289. normally, going to the true line boundary first. Only a directly
  1290. following, identical keypress will bring the cursor to the
  1291. special positions.
  1292. This may also be a cons cell where the behavior for `C-a' and
  1293. `C-e' is set separately."
  1294. :group 'org-edit-structure
  1295. :type '(choice
  1296. (const :tag "off" nil)
  1297. (const :tag "on: after stars/bullet and before tags first" t)
  1298. (const :tag "reversed: true line boundary first" reversed)
  1299. (cons :tag "Set C-a and C-e separately"
  1300. (choice :tag "Special C-a"
  1301. (const :tag "off" nil)
  1302. (const :tag "on: after stars/bullet first" t)
  1303. (const :tag "reversed: before stars/bullet first" reversed))
  1304. (choice :tag "Special C-e"
  1305. (const :tag "off" nil)
  1306. (const :tag "on: before tags first" t)
  1307. (const :tag "reversed: after tags first" reversed)))))
  1308. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1309. (defcustom org-special-ctrl-k nil
  1310. "Non-nil means `C-k' will behave specially in headlines.
  1311. When nil, `C-k' will call the default `kill-line' command.
  1312. When t, the following will happen while the cursor is in the headline:
  1313. - When the cursor is at the beginning of a headline, kill the entire
  1314. line and possible the folded subtree below the line.
  1315. - When in the middle of the headline text, kill the headline up to the tags.
  1316. - When after the headline text, kill the tags."
  1317. :group 'org-edit-structure
  1318. :type 'boolean)
  1319. (defcustom org-ctrl-k-protect-subtree nil
  1320. "Non-nil means, do not delete a hidden subtree with C-k.
  1321. When set to the symbol `error', simply throw an error when C-k is
  1322. used to kill (part-of) a headline that has hidden text behind it.
  1323. Any other non-nil value will result in a query to the user, if it is
  1324. OK to kill that hidden subtree. When nil, kill without remorse."
  1325. :group 'org-edit-structure
  1326. :version "24.1"
  1327. :type '(choice
  1328. (const :tag "Do not protect hidden subtrees" nil)
  1329. (const :tag "Protect hidden subtrees with a security query" t)
  1330. (const :tag "Never kill a hidden subtree with C-k" error)))
  1331. (defcustom org-special-ctrl-o t
  1332. "Non-nil means, make `C-o' insert a row in tables."
  1333. :group 'org-edit-structure
  1334. :type 'boolean)
  1335. (defcustom org-catch-invisible-edits nil
  1336. "Check if in invisible region before inserting or deleting a character.
  1337. Valid values are:
  1338. nil Do not check, so just do invisible edits.
  1339. error Throw an error and do nothing.
  1340. show Make point visible, and do the requested edit.
  1341. show-and-error Make point visible, then throw an error and abort the edit.
  1342. smart Make point visible, and do insertion/deletion if it is
  1343. adjacent to visible text and the change feels predictable.
  1344. Never delete a previously invisible character or add in the
  1345. middle or right after an invisible region. Basically, this
  1346. allows insertion and backward-delete right before ellipses.
  1347. FIXME: maybe in this case we should not even show?"
  1348. :group 'org-edit-structure
  1349. :version "24.1"
  1350. :type '(choice
  1351. (const :tag "Do not check" nil)
  1352. (const :tag "Throw error when trying to edit" error)
  1353. (const :tag "Unhide, but do not do the edit" show-and-error)
  1354. (const :tag "Show invisible part and do the edit" show)
  1355. (const :tag "Be smart and do the right thing" smart)))
  1356. (defcustom org-yank-folded-subtrees t
  1357. "Non-nil means when yanking subtrees, fold them.
  1358. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1359. it starts with a heading and all other headings in it are either children
  1360. or siblings, then fold all the subtrees. However, do this only if no
  1361. text after the yank would be swallowed into a folded tree by this action."
  1362. :group 'org-edit-structure
  1363. :type 'boolean)
  1364. (defcustom org-yank-adjusted-subtrees nil
  1365. "Non-nil means when yanking subtrees, adjust the level.
  1366. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1367. this function for details."
  1368. :group 'org-edit-structure
  1369. :type 'boolean)
  1370. (defcustom org-M-RET-may-split-line '((default . t))
  1371. "Non-nil means M-RET will split the line at the cursor position.
  1372. When nil, it will go to the end of the line before making a
  1373. new line.
  1374. You may also set this option in a different way for different
  1375. contexts. Valid contexts are:
  1376. headline when creating a new headline
  1377. item when creating a new item
  1378. table in a table field
  1379. default the value to be used for all contexts not explicitly
  1380. customized"
  1381. :group 'org-structure
  1382. :group 'org-table
  1383. :type '(choice
  1384. (const :tag "Always" t)
  1385. (const :tag "Never" nil)
  1386. (repeat :greedy t :tag "Individual contexts"
  1387. (cons
  1388. (choice :tag "Context"
  1389. (const headline)
  1390. (const item)
  1391. (const table)
  1392. (const default))
  1393. (boolean)))))
  1394. (defcustom org-insert-heading-respect-content nil
  1395. "Non-nil means insert new headings after the current subtree.
  1396. When nil, the new heading is created directly after the current line.
  1397. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1398. this variable on for the duration of the command."
  1399. :group 'org-structure
  1400. :type 'boolean)
  1401. (defcustom org-blank-before-new-entry '((heading . auto)
  1402. (plain-list-item . auto))
  1403. "Should `org-insert-heading' leave a blank line before new heading/item?
  1404. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1405. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1406. which case Org will look at the surrounding headings/items and try to
  1407. make an intelligent decision whether to insert a blank line or not."
  1408. :group 'org-edit-structure
  1409. :type '(list
  1410. (cons (const heading)
  1411. (choice (const :tag "Never" nil)
  1412. (const :tag "Always" t)
  1413. (const :tag "Auto" auto)))
  1414. (cons (const plain-list-item)
  1415. (choice (const :tag "Never" nil)
  1416. (const :tag "Always" t)
  1417. (const :tag "Auto" auto)))))
  1418. (defcustom org-insert-heading-hook nil
  1419. "Hook being run after inserting a new heading."
  1420. :group 'org-edit-structure
  1421. :type 'hook)
  1422. (defcustom org-enable-fixed-width-editor t
  1423. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1424. This currently only means they are never auto-wrapped.
  1425. When nil, such lines will be treated like ordinary lines."
  1426. :group 'org-edit-structure
  1427. :type 'boolean)
  1428. (defcustom org-goto-auto-isearch t
  1429. "Non-nil means typing characters in `org-goto' starts incremental search.
  1430. When nil, you can use these keybindings to navigate the buffer:
  1431. q Quit the org-goto interface
  1432. n Go to the next visible heading
  1433. p Go to the previous visible heading
  1434. f Go one heading forward on same level
  1435. b Go one heading backward on same level
  1436. u Go one heading up"
  1437. :group 'org-edit-structure
  1438. :type 'boolean)
  1439. (defgroup org-sparse-trees nil
  1440. "Options concerning sparse trees in Org-mode."
  1441. :tag "Org Sparse Trees"
  1442. :group 'org-structure)
  1443. (defcustom org-highlight-sparse-tree-matches t
  1444. "Non-nil means highlight all matches that define a sparse tree.
  1445. The highlights will automatically disappear the next time the buffer is
  1446. changed by an edit command."
  1447. :group 'org-sparse-trees
  1448. :type 'boolean)
  1449. (defcustom org-remove-highlights-with-change t
  1450. "Non-nil means any change to the buffer will remove temporary highlights.
  1451. Such highlights are created by `org-occur' and `org-clock-display'.
  1452. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1453. The highlights created by `org-toggle-latex-fragment' always need
  1454. `C-c C-x C-l' to be removed."
  1455. :group 'org-sparse-trees
  1456. :group 'org-time
  1457. :type 'boolean)
  1458. (defcustom org-occur-hook '(org-first-headline-recenter)
  1459. "Hook that is run after `org-occur' has constructed a sparse tree.
  1460. This can be used to recenter the window to show as much of the structure
  1461. as possible."
  1462. :group 'org-sparse-trees
  1463. :type 'hook)
  1464. (defgroup org-imenu-and-speedbar nil
  1465. "Options concerning imenu and speedbar in Org-mode."
  1466. :tag "Org Imenu and Speedbar"
  1467. :group 'org-structure)
  1468. (defcustom org-imenu-depth 2
  1469. "The maximum level for Imenu access to Org-mode headlines.
  1470. This also applied for speedbar access."
  1471. :group 'org-imenu-and-speedbar
  1472. :type 'integer)
  1473. (defgroup org-table nil
  1474. "Options concerning tables in Org-mode."
  1475. :tag "Org Table"
  1476. :group 'org)
  1477. (defcustom org-enable-table-editor 'optimized
  1478. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1479. When nil, such lines will be treated like ordinary lines.
  1480. When equal to the symbol `optimized', the table editor will be optimized to
  1481. do the following:
  1482. - Automatic overwrite mode in front of whitespace in table fields.
  1483. This makes the structure of the table stay in tact as long as the edited
  1484. field does not exceed the column width.
  1485. - Minimize the number of realigns. Normally, the table is aligned each time
  1486. TAB or RET are pressed to move to another field. With optimization this
  1487. happens only if changes to a field might have changed the column width.
  1488. Optimization requires replacing the functions `self-insert-command',
  1489. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1490. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1491. very good at guessing when a re-align will be necessary, but you can always
  1492. force one with \\[org-ctrl-c-ctrl-c].
  1493. If you would like to use the optimized version in Org-mode, but the
  1494. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1495. This variable can be used to turn on and off the table editor during a session,
  1496. but in order to toggle optimization, a restart is required.
  1497. See also the variable `org-table-auto-blank-field'."
  1498. :group 'org-table
  1499. :type '(choice
  1500. (const :tag "off" nil)
  1501. (const :tag "on" t)
  1502. (const :tag "on, optimized" optimized)))
  1503. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1504. (version<= emacs-version "24.1"))
  1505. "Non-nil means cluster self-insert commands for undo when possible.
  1506. If this is set, then, like in the Emacs command loop, 20 consecutive
  1507. characters will be undone together.
  1508. This is configurable, because there is some impact on typing performance."
  1509. :group 'org-table
  1510. :type 'boolean)
  1511. (defcustom org-table-tab-recognizes-table.el t
  1512. "Non-nil means TAB will automatically notice a table.el table.
  1513. When it sees such a table, it moves point into it and - if necessary -
  1514. calls `table-recognize-table'."
  1515. :group 'org-table-editing
  1516. :type 'boolean)
  1517. (defgroup org-link nil
  1518. "Options concerning links in Org-mode."
  1519. :tag "Org Link"
  1520. :group 'org)
  1521. (defvar org-link-abbrev-alist-local nil
  1522. "Buffer-local version of `org-link-abbrev-alist', which see.
  1523. The value of this is taken from the #+LINK lines.")
  1524. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1525. (defcustom org-link-abbrev-alist nil
  1526. "Alist of link abbreviations.
  1527. The car of each element is a string, to be replaced at the start of a link.
  1528. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1529. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1530. [[linkkey:tag][description]]
  1531. The 'linkkey' must be a word word, starting with a letter, followed
  1532. by letters, numbers, '-' or '_'.
  1533. If REPLACE is a string, the tag will simply be appended to create the link.
  1534. If the string contains \"%s\", the tag will be inserted there. If the string
  1535. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1536. at that point (see the function `url-hexify-string'). If the string contains
  1537. the specifier \"%(my-function)\", then the custom function `my-function' will
  1538. be invoked: this function takes the tag as its only argument and must return
  1539. a string.
  1540. REPLACE may also be a function that will be called with the tag as the
  1541. only argument to create the link, which should be returned as a string.
  1542. See the manual for examples."
  1543. :group 'org-link
  1544. :type '(repeat
  1545. (cons
  1546. (string :tag "Protocol")
  1547. (choice
  1548. (string :tag "Format")
  1549. (function)))))
  1550. (defcustom org-descriptive-links t
  1551. "Non-nil means Org will display descriptive links.
  1552. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1553. \"Org Website\", hiding the link itself and just displaying its
  1554. description. When set to nil, Org will display the full links
  1555. literally.
  1556. You can interactively set the value of this variable by calling
  1557. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1558. :group 'org-link
  1559. :type 'boolean)
  1560. (defcustom org-link-file-path-type 'adaptive
  1561. "How the path name in file links should be stored.
  1562. Valid values are:
  1563. relative Relative to the current directory, i.e. the directory of the file
  1564. into which the link is being inserted.
  1565. absolute Absolute path, if possible with ~ for home directory.
  1566. noabbrev Absolute path, no abbreviation of home directory.
  1567. adaptive Use relative path for files in the current directory and sub-
  1568. directories of it. For other files, use an absolute path."
  1569. :group 'org-link
  1570. :type '(choice
  1571. (const relative)
  1572. (const absolute)
  1573. (const noabbrev)
  1574. (const adaptive)))
  1575. (defvaralias 'org-activate-links 'org-highlight-links)
  1576. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1577. "Types of links that should be highlighted in Org-mode files.
  1578. This is a list of symbols, each one of them leading to the
  1579. highlighting of a certain link type.
  1580. You can still open links that are not highlighted.
  1581. In principle, it does not hurt to turn on highlighting for all
  1582. link types. There may be a small gain when turning off unused
  1583. link types. The types are:
  1584. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1585. angle Links in angular brackets that may contain whitespace like
  1586. <bbdb:Carsten Dominik>.
  1587. plain Plain links in normal text, no whitespace, like http://google.com.
  1588. radio Text that is matched by a radio target, see manual for details.
  1589. tag Tag settings in a headline (link to tag search).
  1590. date Time stamps (link to calendar).
  1591. footnote Footnote labels.
  1592. If you set this variable during an Emacs session, use `org-mode-restart'
  1593. in the Org buffer so that the change takes effect."
  1594. :group 'org-link
  1595. :group 'org-appearance
  1596. :type '(set :greedy t
  1597. (const :tag "Double bracket links" bracket)
  1598. (const :tag "Angular bracket links" angle)
  1599. (const :tag "Plain text links" plain)
  1600. (const :tag "Radio target matches" radio)
  1601. (const :tag "Tags" tag)
  1602. (const :tag "Timestamps" date)
  1603. (const :tag "Footnotes" footnote)))
  1604. (defcustom org-make-link-description-function nil
  1605. "Function to use for generating link descriptions from links.
  1606. When nil, the link location will be used. This function must take
  1607. two parameters: the first one is the link, the second one is the
  1608. description generated by `org-insert-link'. The function should
  1609. return the description to use."
  1610. :group 'org-link
  1611. :type '(choice (const nil) (function)))
  1612. (defgroup org-link-store nil
  1613. "Options concerning storing links in Org-mode."
  1614. :tag "Org Store Link"
  1615. :group 'org-link)
  1616. (defcustom org-url-hexify-p t
  1617. "When non-nil, hexify URL when creating a link."
  1618. :type 'boolean
  1619. :version "24.3"
  1620. :group 'org-link-store)
  1621. (defcustom org-email-link-description-format "Email %c: %.30s"
  1622. "Format of the description part of a link to an email or usenet message.
  1623. The following %-escapes will be replaced by corresponding information:
  1624. %F full \"From\" field
  1625. %f name, taken from \"From\" field, address if no name
  1626. %T full \"To\" field
  1627. %t first name in \"To\" field, address if no name
  1628. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1629. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1630. %s subject
  1631. %d date
  1632. %m message-id.
  1633. You may use normal field width specification between the % and the letter.
  1634. This is for example useful to limit the length of the subject.
  1635. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1636. :group 'org-link-store
  1637. :type 'string)
  1638. (defcustom org-from-is-user-regexp
  1639. (let (r1 r2)
  1640. (when (and user-mail-address (not (string= user-mail-address "")))
  1641. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1642. (when (and user-full-name (not (string= user-full-name "")))
  1643. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1644. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1645. "Regexp matched against the \"From:\" header of an email or usenet message.
  1646. It should match if the message is from the user him/herself."
  1647. :group 'org-link-store
  1648. :type 'regexp)
  1649. (defcustom org-context-in-file-links t
  1650. "Non-nil means file links from `org-store-link' contain context.
  1651. A search string will be added to the file name with :: as separator and
  1652. used to find the context when the link is activated by the command
  1653. `org-open-at-point'. When this option is t, the entire active region
  1654. will be placed in the search string of the file link. If set to a
  1655. positive integer, only the first n lines of context will be stored.
  1656. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1657. negates this setting for the duration of the command."
  1658. :group 'org-link-store
  1659. :type '(choice boolean integer))
  1660. (defcustom org-keep-stored-link-after-insertion nil
  1661. "Non-nil means keep link in list for entire session.
  1662. The command `org-store-link' adds a link pointing to the current
  1663. location to an internal list. These links accumulate during a session.
  1664. The command `org-insert-link' can be used to insert links into any
  1665. Org-mode file (offering completion for all stored links). When this
  1666. option is nil, every link which has been inserted once using \\[org-insert-link]
  1667. will be removed from the list, to make completing the unused links
  1668. more efficient."
  1669. :group 'org-link-store
  1670. :type 'boolean)
  1671. (defgroup org-link-follow nil
  1672. "Options concerning following links in Org-mode."
  1673. :tag "Org Follow Link"
  1674. :group 'org-link)
  1675. (defcustom org-link-translation-function nil
  1676. "Function to translate links with different syntax to Org syntax.
  1677. This can be used to translate links created for example by the Planner
  1678. or emacs-wiki packages to Org syntax.
  1679. The function must accept two parameters, a TYPE containing the link
  1680. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1681. which is everything after the link protocol. It should return a cons
  1682. with possibly modified values of type and path.
  1683. Org contains a function for this, so if you set this variable to
  1684. `org-translate-link-from-planner', you should be able follow many
  1685. links created by planner."
  1686. :group 'org-link-follow
  1687. :type '(choice (const nil) (function)))
  1688. (defcustom org-follow-link-hook nil
  1689. "Hook that is run after a link has been followed."
  1690. :group 'org-link-follow
  1691. :type 'hook)
  1692. (defcustom org-tab-follows-link nil
  1693. "Non-nil means on links TAB will follow the link.
  1694. Needs to be set before org.el is loaded.
  1695. This really should not be used, it does not make sense, and the
  1696. implementation is bad."
  1697. :group 'org-link-follow
  1698. :type 'boolean)
  1699. (defcustom org-return-follows-link nil
  1700. "Non-nil means on links RET will follow the link.
  1701. In tables, the special behavior of RET has precedence."
  1702. :group 'org-link-follow
  1703. :type 'boolean)
  1704. (defcustom org-mouse-1-follows-link
  1705. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1706. "Non-nil means mouse-1 on a link will follow the link.
  1707. A longer mouse click will still set point. Does not work on XEmacs.
  1708. Needs to be set before org.el is loaded."
  1709. :group 'org-link-follow
  1710. :version "24.4"
  1711. :package-version '(Org . "8.3")
  1712. :type '(choice
  1713. (const :tag "A double click follows the link" double)
  1714. (const :tag "Unconditionally follow the link with mouse-1" t)
  1715. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1716. (defcustom org-mark-ring-length 4
  1717. "Number of different positions to be recorded in the ring.
  1718. Changing this requires a restart of Emacs to work correctly."
  1719. :group 'org-link-follow
  1720. :type 'integer)
  1721. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1722. "Non-nil means internal links in Org files must exactly match a headline.
  1723. When nil, the link search tries to match a phrase with all words
  1724. in the search text."
  1725. :group 'org-link-follow
  1726. :version "24.1"
  1727. :type '(choice
  1728. (const :tag "Use fuzzy text search" nil)
  1729. (const :tag "Match only exact headline" t)
  1730. (const :tag "Match exact headline or query to create it"
  1731. query-to-create)))
  1732. (defcustom org-link-frame-setup
  1733. '((vm . vm-visit-folder-other-frame)
  1734. (vm-imap . vm-visit-imap-folder-other-frame)
  1735. (gnus . org-gnus-no-new-news)
  1736. (file . find-file-other-window)
  1737. (wl . wl-other-frame))
  1738. "Setup the frame configuration for following links.
  1739. When following a link with Emacs, it may often be useful to display
  1740. this link in another window or frame. This variable can be used to
  1741. set this up for the different types of links.
  1742. For VM, use any of
  1743. `vm-visit-folder'
  1744. `vm-visit-folder-other-window'
  1745. `vm-visit-folder-other-frame'
  1746. For Gnus, use any of
  1747. `gnus'
  1748. `gnus-other-frame'
  1749. `org-gnus-no-new-news'
  1750. For FILE, use any of
  1751. `find-file'
  1752. `find-file-other-window'
  1753. `find-file-other-frame'
  1754. For Wanderlust use any of
  1755. `wl'
  1756. `wl-other-frame'
  1757. For the calendar, use the variable `calendar-setup'.
  1758. For BBDB, it is currently only possible to display the matches in
  1759. another window."
  1760. :group 'org-link-follow
  1761. :type '(list
  1762. (cons (const vm)
  1763. (choice
  1764. (const vm-visit-folder)
  1765. (const vm-visit-folder-other-window)
  1766. (const vm-visit-folder-other-frame)))
  1767. (cons (const vm-imap)
  1768. (choice
  1769. (const vm-visit-imap-folder)
  1770. (const vm-visit-imap-folder-other-window)
  1771. (const vm-visit-imap-folder-other-frame)))
  1772. (cons (const gnus)
  1773. (choice
  1774. (const gnus)
  1775. (const gnus-other-frame)
  1776. (const org-gnus-no-new-news)))
  1777. (cons (const file)
  1778. (choice
  1779. (const find-file)
  1780. (const find-file-other-window)
  1781. (const find-file-other-frame)))
  1782. (cons (const wl)
  1783. (choice
  1784. (const wl)
  1785. (const wl-other-frame)))))
  1786. (defcustom org-display-internal-link-with-indirect-buffer nil
  1787. "Non-nil means use indirect buffer to display infile links.
  1788. Activating internal links (from one location in a file to another location
  1789. in the same file) normally just jumps to the location. When the link is
  1790. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1791. is displayed in
  1792. another window. When this option is set, the other window actually displays
  1793. an indirect buffer clone of the current buffer, to avoid any visibility
  1794. changes to the current buffer."
  1795. :group 'org-link-follow
  1796. :type 'boolean)
  1797. (defcustom org-open-non-existing-files nil
  1798. "Non-nil means `org-open-file' will open non-existing files.
  1799. When nil, an error will be generated.
  1800. This variable applies only to external applications because they
  1801. might choke on non-existing files. If the link is to a file that
  1802. will be opened in Emacs, the variable is ignored."
  1803. :group 'org-link-follow
  1804. :type 'boolean)
  1805. (defcustom org-open-directory-means-index-dot-org nil
  1806. "Non-nil means a link to a directory really means to index.org.
  1807. When nil, following a directory link will run dired or open a finder/explorer
  1808. window on that directory."
  1809. :group 'org-link-follow
  1810. :type 'boolean)
  1811. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1812. "Non-nil means ask for confirmation before executing shell links.
  1813. Shell links can be dangerous: just think about a link
  1814. [[shell:rm -rf ~/*][Google Search]]
  1815. This link would show up in your Org-mode document as \"Google Search\",
  1816. but really it would remove your entire home directory.
  1817. Therefore we advise against setting this variable to nil.
  1818. Just change it to `y-or-n-p' if you want to confirm with a
  1819. single keystroke rather than having to type \"yes\"."
  1820. :group 'org-link-follow
  1821. :type '(choice
  1822. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1823. (const :tag "with y-or-n (faster)" y-or-n-p)
  1824. (const :tag "no confirmation (dangerous)" nil)))
  1825. (put 'org-confirm-shell-link-function
  1826. 'safe-local-variable
  1827. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1828. (defcustom org-confirm-shell-link-not-regexp ""
  1829. "A regexp to skip confirmation for shell links."
  1830. :group 'org-link-follow
  1831. :version "24.1"
  1832. :type 'regexp)
  1833. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1834. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1835. Elisp links can be dangerous: just think about a link
  1836. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1837. This link would show up in your Org-mode document as \"Google Search\",
  1838. but really it would remove your entire home directory.
  1839. Therefore we advise against setting this variable to nil.
  1840. Just change it to `y-or-n-p' if you want to confirm with a
  1841. single keystroke rather than having to type \"yes\"."
  1842. :group 'org-link-follow
  1843. :type '(choice
  1844. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1845. (const :tag "with y-or-n (faster)" y-or-n-p)
  1846. (const :tag "no confirmation (dangerous)" nil)))
  1847. (put 'org-confirm-shell-link-function
  1848. 'safe-local-variable
  1849. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1850. (defcustom org-confirm-elisp-link-not-regexp ""
  1851. "A regexp to skip confirmation for Elisp links."
  1852. :group 'org-link-follow
  1853. :version "24.1"
  1854. :type 'regexp)
  1855. (defconst org-file-apps-defaults-gnu
  1856. '((remote . emacs)
  1857. (system . mailcap)
  1858. (t . mailcap))
  1859. "Default file applications on a UNIX or GNU/Linux system.
  1860. See `org-file-apps'.")
  1861. (defconst org-file-apps-defaults-macosx
  1862. '((remote . emacs)
  1863. (t . "open %s")
  1864. (system . "open %s")
  1865. ("ps.gz" . "gv %s")
  1866. ("eps.gz" . "gv %s")
  1867. ("dvi" . "xdvi %s")
  1868. ("fig" . "xfig %s"))
  1869. "Default file applications on a MacOS X system.
  1870. The system \"open\" is known as a default, but we use X11 applications
  1871. for some files for which the OS does not have a good default.
  1872. See `org-file-apps'.")
  1873. (defconst org-file-apps-defaults-windowsnt
  1874. (list
  1875. '(remote . emacs)
  1876. (cons t
  1877. (list (if (featurep 'xemacs)
  1878. 'mswindows-shell-execute
  1879. 'w32-shell-execute)
  1880. "open" 'file))
  1881. (cons 'system
  1882. (list (if (featurep 'xemacs)
  1883. 'mswindows-shell-execute
  1884. 'w32-shell-execute)
  1885. "open" 'file)))
  1886. "Default file applications on a Windows NT system.
  1887. The system \"open\" is used for most files.
  1888. See `org-file-apps'.")
  1889. (defcustom org-file-apps
  1890. '((auto-mode . emacs)
  1891. ("\\.mm\\'" . default)
  1892. ("\\.x?html?\\'" . default)
  1893. ("\\.pdf\\'" . default))
  1894. "External applications for opening `file:path' items in a document.
  1895. Org-mode uses system defaults for different file types, but
  1896. you can use this variable to set the application for a given file
  1897. extension. The entries in this list are cons cells where the car identifies
  1898. files and the cdr the corresponding command. Possible values for the
  1899. file identifier are
  1900. \"string\" A string as a file identifier can be interpreted in different
  1901. ways, depending on its contents:
  1902. - Alphanumeric characters only:
  1903. Match links with this file extension.
  1904. Example: (\"pdf\" . \"evince %s\")
  1905. to open PDFs with evince.
  1906. - Regular expression: Match links where the
  1907. filename matches the regexp. If you want to
  1908. use groups here, use shy groups.
  1909. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1910. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1911. to open *.html and *.xhtml with firefox.
  1912. - Regular expression which contains (non-shy) groups:
  1913. Match links where the whole link, including \"::\", and
  1914. anything after that, matches the regexp.
  1915. In a custom command string, %1, %2, etc. are replaced with
  1916. the parts of the link that were matched by the groups.
  1917. For backwards compatibility, if a command string is given
  1918. that does not use any of the group matches, this case is
  1919. handled identically to the second one (i.e. match against
  1920. file name only).
  1921. In a custom lisp form, you can access the group matches with
  1922. (match-string n link).
  1923. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1924. to open [[file:document.pdf::5]] with evince at page 5.
  1925. `directory' Matches a directory
  1926. `remote' Matches a remote file, accessible through tramp or efs.
  1927. Remote files most likely should be visited through Emacs
  1928. because external applications cannot handle such paths.
  1929. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1930. so all files Emacs knows how to handle. Using this with
  1931. command `emacs' will open most files in Emacs. Beware that this
  1932. will also open html files inside Emacs, unless you add
  1933. (\"html\" . default) to the list as well.
  1934. t Default for files not matched by any of the other options.
  1935. `system' The system command to open files, like `open' on Windows
  1936. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1937. that will be selected if you call `C-c C-o' with a double
  1938. \\[universal-argument] \\[universal-argument] prefix.
  1939. Possible values for the command are:
  1940. `emacs' The file will be visited by the current Emacs process.
  1941. `default' Use the default application for this file type, which is the
  1942. association for t in the list, most likely in the system-specific
  1943. part.
  1944. This can be used to overrule an unwanted setting in the
  1945. system-specific variable.
  1946. `system' Use the system command for opening files, like \"open\".
  1947. This command is specified by the entry whose car is `system'.
  1948. Most likely, the system-specific version of this variable
  1949. does define this command, but you can overrule/replace it
  1950. here.
  1951. string A command to be executed by a shell; %s will be replaced
  1952. by the path to the file.
  1953. sexp A Lisp form which will be evaluated. The file path will
  1954. be available in the Lisp variable `file'.
  1955. For more examples, see the system specific constants
  1956. `org-file-apps-defaults-macosx'
  1957. `org-file-apps-defaults-windowsnt'
  1958. `org-file-apps-defaults-gnu'."
  1959. :group 'org-link-follow
  1960. :type '(repeat
  1961. (cons (choice :value ""
  1962. (string :tag "Extension")
  1963. (const :tag "System command to open files" system)
  1964. (const :tag "Default for unrecognized files" t)
  1965. (const :tag "Remote file" remote)
  1966. (const :tag "Links to a directory" directory)
  1967. (const :tag "Any files that have Emacs modes"
  1968. auto-mode))
  1969. (choice :value ""
  1970. (const :tag "Visit with Emacs" emacs)
  1971. (const :tag "Use default" default)
  1972. (const :tag "Use the system command" system)
  1973. (string :tag "Command")
  1974. (sexp :tag "Lisp form")))))
  1975. (defcustom org-doi-server-url "http://dx.doi.org/"
  1976. "The URL of the DOI server."
  1977. :type 'string
  1978. :version "24.3"
  1979. :group 'org-link-follow)
  1980. (defgroup org-refile nil
  1981. "Options concerning refiling entries in Org-mode."
  1982. :tag "Org Refile"
  1983. :group 'org)
  1984. (defcustom org-directory "~/org"
  1985. "Directory with org files.
  1986. This is just a default location to look for Org files. There is no need
  1987. at all to put your files into this directory. It is only used in the
  1988. following situations:
  1989. 1. When a capture template specifies a target file that is not an
  1990. absolute path. The path will then be interpreted relative to
  1991. `org-directory'
  1992. 2. When a capture note is filed away in an interactive way (when exiting the
  1993. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1994. with `org-directory' as the default path."
  1995. :group 'org-refile
  1996. :group 'org-capture
  1997. :type 'directory)
  1998. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1999. "Default target for storing notes.
  2000. Used as a fall back file for org-capture.el, for templates that
  2001. do not specify a target file."
  2002. :group 'org-refile
  2003. :group 'org-capture
  2004. :type 'file)
  2005. (defcustom org-goto-interface 'outline
  2006. "The default interface to be used for `org-goto'.
  2007. Allowed values are:
  2008. outline The interface shows an outline of the relevant file
  2009. and the correct heading is found by moving through
  2010. the outline or by searching with incremental search.
  2011. outline-path-completion Headlines in the current buffer are offered via
  2012. completion. This is the interface also used by
  2013. the refile command."
  2014. :group 'org-refile
  2015. :type '(choice
  2016. (const :tag "Outline" outline)
  2017. (const :tag "Outline-path-completion" outline-path-completion)))
  2018. (defcustom org-goto-max-level 5
  2019. "Maximum target level when running `org-goto' with refile interface."
  2020. :group 'org-refile
  2021. :type 'integer)
  2022. (defcustom org-reverse-note-order nil
  2023. "Non-nil means store new notes at the beginning of a file or entry.
  2024. When nil, new notes will be filed to the end of a file or entry.
  2025. This can also be a list with cons cells of regular expressions that
  2026. are matched against file names, and values."
  2027. :group 'org-capture
  2028. :group 'org-refile
  2029. :type '(choice
  2030. (const :tag "Reverse always" t)
  2031. (const :tag "Reverse never" nil)
  2032. (repeat :tag "By file name regexp"
  2033. (cons regexp boolean))))
  2034. (defcustom org-log-refile nil
  2035. "Information to record when a task is refiled.
  2036. Possible values are:
  2037. nil Don't add anything
  2038. time Add a time stamp to the task
  2039. note Prompt for a note and add it with template `org-log-note-headings'
  2040. This option can also be set with on a per-file-basis with
  2041. #+STARTUP: nologrefile
  2042. #+STARTUP: logrefile
  2043. #+STARTUP: lognoterefile
  2044. You can have local logging settings for a subtree by setting the LOGGING
  2045. property to one or more of these keywords.
  2046. When bulk-refiling from the agenda, the value `note' is forbidden and
  2047. will temporarily be changed to `time'."
  2048. :group 'org-refile
  2049. :group 'org-progress
  2050. :version "24.1"
  2051. :type '(choice
  2052. (const :tag "No logging" nil)
  2053. (const :tag "Record timestamp" time)
  2054. (const :tag "Record timestamp with note." note)))
  2055. (defcustom org-refile-targets nil
  2056. "Targets for refiling entries with \\[org-refile].
  2057. This is a list of cons cells. Each cell contains:
  2058. - a specification of the files to be considered, either a list of files,
  2059. or a symbol whose function or variable value will be used to retrieve
  2060. a file name or a list of file names. If you use `org-agenda-files' for
  2061. that, all agenda files will be scanned for targets. Nil means consider
  2062. headings in the current buffer.
  2063. - A specification of how to find candidate refile targets. This may be
  2064. any of:
  2065. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2066. This tag has to be present in all target headlines, inheritance will
  2067. not be considered.
  2068. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2069. todo keyword.
  2070. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2071. headlines that are refiling targets.
  2072. - a cons cell (:level . N). Any headline of level N is considered a target.
  2073. Note that, when `org-odd-levels-only' is set, level corresponds to
  2074. order in hierarchy, not to the number of stars.
  2075. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2076. Note that, when `org-odd-levels-only' is set, level corresponds to
  2077. order in hierarchy, not to the number of stars.
  2078. Each element of this list generates a set of possible targets.
  2079. The union of these sets is presented (with completion) to
  2080. the user by `org-refile'.
  2081. You can set the variable `org-refile-target-verify-function' to a function
  2082. to verify each headline found by the simple criteria above.
  2083. When this variable is nil, all top-level headlines in the current buffer
  2084. are used, equivalent to the value `((nil . (:level . 1))'."
  2085. :group 'org-refile
  2086. :type '(repeat
  2087. (cons
  2088. (choice :value org-agenda-files
  2089. (const :tag "All agenda files" org-agenda-files)
  2090. (const :tag "Current buffer" nil)
  2091. (function) (variable) (file))
  2092. (choice :tag "Identify target headline by"
  2093. (cons :tag "Specific tag" (const :value :tag) (string))
  2094. (cons :tag "TODO keyword" (const :value :todo) (string))
  2095. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2096. (cons :tag "Level number" (const :value :level) (integer))
  2097. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2098. (defcustom org-refile-target-verify-function nil
  2099. "Function to verify if the headline at point should be a refile target.
  2100. The function will be called without arguments, with point at the
  2101. beginning of the headline. It should return t and leave point
  2102. where it is if the headline is a valid target for refiling.
  2103. If the target should not be selected, the function must return nil.
  2104. In addition to this, it may move point to a place from where the search
  2105. should be continued. For example, the function may decide that the entire
  2106. subtree of the current entry should be excluded and move point to the end
  2107. of the subtree."
  2108. :group 'org-refile
  2109. :type '(choice
  2110. (const nil)
  2111. (function)))
  2112. (defcustom org-refile-use-cache nil
  2113. "Non-nil means cache refile targets to speed up the process.
  2114. The cache for a particular file will be updated automatically when
  2115. the buffer has been killed, or when any of the marker used for flagging
  2116. refile targets no longer points at a live buffer.
  2117. If you have added new entries to a buffer that might themselves be targets,
  2118. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2119. find that easier, `C-u C-u C-u C-c C-w'."
  2120. :group 'org-refile
  2121. :version "24.1"
  2122. :type 'boolean)
  2123. (defcustom org-refile-use-outline-path nil
  2124. "Non-nil means provide refile targets as paths.
  2125. So a level 3 headline will be available as level1/level2/level3.
  2126. When the value is `file', also include the file name (without directory)
  2127. into the path. In this case, you can also stop the completion after
  2128. the file name, to get entries inserted as top level in the file.
  2129. When `full-file-path', include the full file path."
  2130. :group 'org-refile
  2131. :type '(choice
  2132. (const :tag "Not" nil)
  2133. (const :tag "Yes" t)
  2134. (const :tag "Start with file name" file)
  2135. (const :tag "Start with full file path" full-file-path)))
  2136. (defcustom org-outline-path-complete-in-steps t
  2137. "Non-nil means complete the outline path in hierarchical steps.
  2138. When Org-mode uses the refile interface to select an outline path
  2139. \(see variable `org-refile-use-outline-path'), the completion of
  2140. the path can be done in a single go, or it can be done in steps down
  2141. the headline hierarchy. Going in steps is probably the best if you
  2142. do not use a special completion package like `ido' or `icicles'.
  2143. However, when using these packages, going in one step can be very
  2144. fast, while still showing the whole path to the entry."
  2145. :group 'org-refile
  2146. :type 'boolean)
  2147. (defcustom org-refile-allow-creating-parent-nodes nil
  2148. "Non-nil means allow to create new nodes as refile targets.
  2149. New nodes are then created by adding \"/new node name\" to the completion
  2150. of an existing node. When the value of this variable is `confirm',
  2151. new node creation must be confirmed by the user (recommended).
  2152. When nil, the completion must match an existing entry.
  2153. Note that, if the new heading is not seen by the criteria
  2154. listed in `org-refile-targets', multiple instances of the same
  2155. heading would be created by trying again to file under the new
  2156. heading."
  2157. :group 'org-refile
  2158. :type '(choice
  2159. (const :tag "Never" nil)
  2160. (const :tag "Always" t)
  2161. (const :tag "Prompt for confirmation" confirm)))
  2162. (defcustom org-refile-active-region-within-subtree nil
  2163. "Non-nil means also refile active region within a subtree.
  2164. By default `org-refile' doesn't allow refiling regions if they
  2165. don't contain a set of subtrees, but it might be convenient to
  2166. do so sometimes: in that case, the first line of the region is
  2167. converted to a headline before refiling."
  2168. :group 'org-refile
  2169. :version "24.1"
  2170. :type 'boolean)
  2171. (defgroup org-todo nil
  2172. "Options concerning TODO items in Org-mode."
  2173. :tag "Org TODO"
  2174. :group 'org)
  2175. (defgroup org-progress nil
  2176. "Options concerning Progress logging in Org-mode."
  2177. :tag "Org Progress"
  2178. :group 'org-time)
  2179. (defvar org-todo-interpretation-widgets
  2180. '((:tag "Sequence (cycling hits every state)" sequence)
  2181. (:tag "Type (cycling directly to DONE)" type))
  2182. "The available interpretation symbols for customizing `org-todo-keywords'.
  2183. Interested libraries should add to this list.")
  2184. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2185. "List of TODO entry keyword sequences and their interpretation.
  2186. \\<org-mode-map>This is a list of sequences.
  2187. Each sequence starts with a symbol, either `sequence' or `type',
  2188. indicating if the keywords should be interpreted as a sequence of
  2189. action steps, or as different types of TODO items. The first
  2190. keywords are states requiring action - these states will select a headline
  2191. for inclusion into the global TODO list Org-mode produces. If one of
  2192. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2193. signify that no further action is necessary. If \"|\" is not found,
  2194. the last keyword is treated as the only DONE state of the sequence.
  2195. The command \\[org-todo] cycles an entry through these states, and one
  2196. additional state where no keyword is present. For details about this
  2197. cycling, see the manual.
  2198. TODO keywords and interpretation can also be set on a per-file basis with
  2199. the special #+SEQ_TODO and #+TYP_TODO lines.
  2200. Each keyword can optionally specify a character for fast state selection
  2201. \(in combination with the variable `org-use-fast-todo-selection')
  2202. and specifiers for state change logging, using the same syntax that
  2203. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2204. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2205. indicates to record a time stamp each time this state is selected.
  2206. Each keyword may also specify if a timestamp or a note should be
  2207. recorded when entering or leaving the state, by adding additional
  2208. characters in the parenthesis after the keyword. This looks like this:
  2209. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2210. record only the time of the state change. With X and Y being either
  2211. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2212. Y when leaving the state if and only if the *target* state does not
  2213. define X. You may omit any of the fast-selection key or X or /Y,
  2214. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2215. For backward compatibility, this variable may also be just a list
  2216. of keywords. In this case the interpretation (sequence or type) will be
  2217. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2218. :group 'org-todo
  2219. :group 'org-keywords
  2220. :type '(choice
  2221. (repeat :tag "Old syntax, just keywords"
  2222. (string :tag "Keyword"))
  2223. (repeat :tag "New syntax"
  2224. (cons
  2225. (choice
  2226. :tag "Interpretation"
  2227. ;;Quick and dirty way to see
  2228. ;;`org-todo-interpretations'. This takes the
  2229. ;;place of item arguments
  2230. :convert-widget
  2231. (lambda (widget)
  2232. (widget-put widget
  2233. :args (mapcar
  2234. (lambda (x)
  2235. (widget-convert
  2236. (cons 'const x)))
  2237. org-todo-interpretation-widgets))
  2238. widget))
  2239. (repeat
  2240. (string :tag "Keyword"))))))
  2241. (defvar org-todo-keywords-1 nil
  2242. "All TODO and DONE keywords active in a buffer.")
  2243. (make-variable-buffer-local 'org-todo-keywords-1)
  2244. (defvar org-todo-keywords-for-agenda nil)
  2245. (defvar org-done-keywords-for-agenda nil)
  2246. (defvar org-todo-keyword-alist-for-agenda nil)
  2247. (defvar org-tag-alist-for-agenda nil
  2248. "Alist of all tags from all agenda files.")
  2249. (defvar org-tag-groups-alist-for-agenda nil
  2250. "Alist of all groups tags from all current agenda files.")
  2251. (defvar org-tag-groups-alist nil)
  2252. (make-variable-buffer-local 'org-tag-groups-alist)
  2253. (defvar org-agenda-contributing-files nil)
  2254. (defvar org-not-done-keywords nil)
  2255. (make-variable-buffer-local 'org-not-done-keywords)
  2256. (defvar org-done-keywords nil)
  2257. (make-variable-buffer-local 'org-done-keywords)
  2258. (defvar org-todo-heads nil)
  2259. (make-variable-buffer-local 'org-todo-heads)
  2260. (defvar org-todo-sets nil)
  2261. (make-variable-buffer-local 'org-todo-sets)
  2262. (defvar org-todo-log-states nil)
  2263. (make-variable-buffer-local 'org-todo-log-states)
  2264. (defvar org-todo-kwd-alist nil)
  2265. (make-variable-buffer-local 'org-todo-kwd-alist)
  2266. (defvar org-todo-key-alist nil)
  2267. (make-variable-buffer-local 'org-todo-key-alist)
  2268. (defvar org-todo-key-trigger nil)
  2269. (make-variable-buffer-local 'org-todo-key-trigger)
  2270. (defcustom org-todo-interpretation 'sequence
  2271. "Controls how TODO keywords are interpreted.
  2272. This variable is in principle obsolete and is only used for
  2273. backward compatibility, if the interpretation of todo keywords is
  2274. not given already in `org-todo-keywords'. See that variable for
  2275. more information."
  2276. :group 'org-todo
  2277. :group 'org-keywords
  2278. :type '(choice (const sequence)
  2279. (const type)))
  2280. (defcustom org-use-fast-todo-selection t
  2281. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2282. This variable describes if and under what circumstances the cycling
  2283. mechanism for TODO keywords will be replaced by a single-key, direct
  2284. selection scheme.
  2285. When nil, fast selection is never used.
  2286. When the symbol `prefix', it will be used when `org-todo' is called
  2287. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2288. `C-u t' in an agenda buffer.
  2289. When t, fast selection is used by default. In this case, the prefix
  2290. argument forces cycling instead.
  2291. In all cases, the special interface is only used if access keys have
  2292. actually been assigned by the user, i.e. if keywords in the configuration
  2293. are followed by a letter in parenthesis, like TODO(t)."
  2294. :group 'org-todo
  2295. :type '(choice
  2296. (const :tag "Never" nil)
  2297. (const :tag "By default" t)
  2298. (const :tag "Only with C-u C-c C-t" prefix)))
  2299. (defcustom org-provide-todo-statistics t
  2300. "Non-nil means update todo statistics after insert and toggle.
  2301. ALL-HEADLINES means update todo statistics by including headlines
  2302. with no TODO keyword as well, counting them as not done.
  2303. A list of TODO keywords means the same, but skip keywords that are
  2304. not in this list.
  2305. When set to a list of two lists, the first list contains keywords
  2306. to consider as TODO keywords, the second list contains keywords
  2307. to consider as DONE keywords.
  2308. When this is set, todo statistics is updated in the parent of the
  2309. current entry each time a todo state is changed."
  2310. :group 'org-todo
  2311. :type '(choice
  2312. (const :tag "Yes, only for TODO entries" t)
  2313. (const :tag "Yes, including all entries" all-headlines)
  2314. (repeat :tag "Yes, for TODOs in this list"
  2315. (string :tag "TODO keyword"))
  2316. (list :tag "Yes, for TODOs and DONEs in these lists"
  2317. (repeat (string :tag "TODO keyword"))
  2318. (repeat (string :tag "DONE keyword")))
  2319. (other :tag "No TODO statistics" nil)))
  2320. (defcustom org-hierarchical-todo-statistics t
  2321. "Non-nil means TODO statistics covers just direct children.
  2322. When nil, all entries in the subtree are considered.
  2323. This has only an effect if `org-provide-todo-statistics' is set.
  2324. To set this to nil for only a single subtree, use a COOKIE_DATA
  2325. property and include the word \"recursive\" into the value."
  2326. :group 'org-todo
  2327. :type 'boolean)
  2328. (defcustom org-after-todo-state-change-hook nil
  2329. "Hook which is run after the state of a TODO item was changed.
  2330. The new state (a string with a TODO keyword, or nil) is available in the
  2331. Lisp variable `org-state'."
  2332. :group 'org-todo
  2333. :type 'hook)
  2334. (defvar org-blocker-hook nil
  2335. "Hook for functions that are allowed to block a state change.
  2336. Functions in this hook should not modify the buffer.
  2337. Each function gets as its single argument a property list,
  2338. see `org-trigger-hook' for more information about this list.
  2339. If any of the functions in this hook returns nil, the state change
  2340. is blocked.")
  2341. (defvar org-trigger-hook nil
  2342. "Hook for functions that are triggered by a state change.
  2343. Each function gets as its single argument a property list with at
  2344. least the following elements:
  2345. (:type type-of-change :position pos-at-entry-start
  2346. :from old-state :to new-state)
  2347. Depending on the type, more properties may be present.
  2348. This mechanism is currently implemented for:
  2349. TODO state changes
  2350. ------------------
  2351. :type todo-state-change
  2352. :from previous state (keyword as a string), or nil, or a symbol
  2353. 'todo' or 'done', to indicate the general type of state.
  2354. :to new state, like in :from")
  2355. (defcustom org-enforce-todo-dependencies nil
  2356. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2357. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2358. be blocked if any prior sibling is not yet done.
  2359. Finally, if the parent is blocked because of ordered siblings of its own,
  2360. the child will also be blocked."
  2361. :set (lambda (var val)
  2362. (set var val)
  2363. (if val
  2364. (add-hook 'org-blocker-hook
  2365. 'org-block-todo-from-children-or-siblings-or-parent)
  2366. (remove-hook 'org-blocker-hook
  2367. 'org-block-todo-from-children-or-siblings-or-parent)))
  2368. :group 'org-todo
  2369. :type 'boolean)
  2370. (defcustom org-enforce-todo-checkbox-dependencies nil
  2371. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2372. When this is nil, checkboxes have no influence on switching TODO states.
  2373. When non-nil, you first need to check off all check boxes before the TODO
  2374. entry can be switched to DONE.
  2375. This variable needs to be set before org.el is loaded, and you need to
  2376. restart Emacs after a change to make the change effective. The only way
  2377. to change is while Emacs is running is through the customize interface."
  2378. :set (lambda (var val)
  2379. (set var val)
  2380. (if val
  2381. (add-hook 'org-blocker-hook
  2382. 'org-block-todo-from-checkboxes)
  2383. (remove-hook 'org-blocker-hook
  2384. 'org-block-todo-from-checkboxes)))
  2385. :group 'org-todo
  2386. :type 'boolean)
  2387. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2388. "Non-nil means inserting a TODO heading is treated as state change.
  2389. So when the command \\[org-insert-todo-heading] is used, state change
  2390. logging will apply if appropriate. When nil, the new TODO item will
  2391. be inserted directly, and no logging will take place."
  2392. :group 'org-todo
  2393. :type 'boolean)
  2394. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2395. "Non-nil means switching TODO states with S-cursor counts as state change.
  2396. This is the default behavior. However, setting this to nil allows a
  2397. convenient way to select a TODO state and bypass any logging associated
  2398. with that."
  2399. :group 'org-todo
  2400. :type 'boolean)
  2401. (defcustom org-todo-state-tags-triggers nil
  2402. "Tag changes that should be triggered by TODO state changes.
  2403. This is a list. Each entry is
  2404. (state-change (tag . flag) .......)
  2405. State-change can be a string with a state, and empty string to indicate the
  2406. state that has no TODO keyword, or it can be one of the symbols `todo'
  2407. or `done', meaning any not-done or done state, respectively."
  2408. :group 'org-todo
  2409. :group 'org-tags
  2410. :type '(repeat
  2411. (cons (choice :tag "When changing to"
  2412. (const :tag "Not-done state" todo)
  2413. (const :tag "Done state" done)
  2414. (string :tag "State"))
  2415. (repeat
  2416. (cons :tag "Tag action"
  2417. (string :tag "Tag")
  2418. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2419. (defcustom org-log-done nil
  2420. "Information to record when a task moves to the DONE state.
  2421. Possible values are:
  2422. nil Don't add anything, just change the keyword
  2423. time Add a time stamp to the task
  2424. note Prompt for a note and add it with template `org-log-note-headings'
  2425. This option can also be set with on a per-file-basis with
  2426. #+STARTUP: nologdone
  2427. #+STARTUP: logdone
  2428. #+STARTUP: lognotedone
  2429. You can have local logging settings for a subtree by setting the LOGGING
  2430. property to one or more of these keywords."
  2431. :group 'org-todo
  2432. :group 'org-progress
  2433. :type '(choice
  2434. (const :tag "No logging" nil)
  2435. (const :tag "Record CLOSED timestamp" time)
  2436. (const :tag "Record CLOSED timestamp with note." note)))
  2437. ;; Normalize old uses of org-log-done.
  2438. (cond
  2439. ((eq org-log-done t) (setq org-log-done 'time))
  2440. ((and (listp org-log-done) (memq 'done org-log-done))
  2441. (setq org-log-done 'note)))
  2442. (defcustom org-log-reschedule nil
  2443. "Information to record when the scheduling date of a tasks is modified.
  2444. Possible values are:
  2445. nil Don't add anything, just change the date
  2446. time Add a time stamp to the task
  2447. note Prompt for a note and add it with template `org-log-note-headings'
  2448. This option can also be set with on a per-file-basis with
  2449. #+STARTUP: nologreschedule
  2450. #+STARTUP: logreschedule
  2451. #+STARTUP: lognotereschedule"
  2452. :group 'org-todo
  2453. :group 'org-progress
  2454. :type '(choice
  2455. (const :tag "No logging" nil)
  2456. (const :tag "Record timestamp" time)
  2457. (const :tag "Record timestamp with note." note)))
  2458. (defcustom org-log-redeadline nil
  2459. "Information to record when the deadline date of a tasks is modified.
  2460. Possible values are:
  2461. nil Don't add anything, just change the date
  2462. time Add a time stamp to the task
  2463. note Prompt for a note and add it with template `org-log-note-headings'
  2464. This option can also be set with on a per-file-basis with
  2465. #+STARTUP: nologredeadline
  2466. #+STARTUP: logredeadline
  2467. #+STARTUP: lognoteredeadline
  2468. You can have local logging settings for a subtree by setting the LOGGING
  2469. property to one or more of these keywords."
  2470. :group 'org-todo
  2471. :group 'org-progress
  2472. :type '(choice
  2473. (const :tag "No logging" nil)
  2474. (const :tag "Record timestamp" time)
  2475. (const :tag "Record timestamp with note." note)))
  2476. (defcustom org-log-note-clock-out nil
  2477. "Non-nil means record a note when clocking out of an item.
  2478. This can also be configured on a per-file basis by adding one of
  2479. the following lines anywhere in the buffer:
  2480. #+STARTUP: lognoteclock-out
  2481. #+STARTUP: nolognoteclock-out"
  2482. :group 'org-todo
  2483. :group 'org-progress
  2484. :type 'boolean)
  2485. (defcustom org-log-done-with-time t
  2486. "Non-nil means the CLOSED time stamp will contain date and time.
  2487. When nil, only the date will be recorded."
  2488. :group 'org-progress
  2489. :type 'boolean)
  2490. (defcustom org-log-note-headings
  2491. '((done . "CLOSING NOTE %t")
  2492. (state . "State %-12s from %-12S %t")
  2493. (note . "Note taken on %t")
  2494. (reschedule . "Rescheduled from %S on %t")
  2495. (delschedule . "Not scheduled, was %S on %t")
  2496. (redeadline . "New deadline from %S on %t")
  2497. (deldeadline . "Removed deadline, was %S on %t")
  2498. (refile . "Refiled on %t")
  2499. (clock-out . ""))
  2500. "Headings for notes added to entries.
  2501. The value is an alist, with the car being a symbol indicating the
  2502. note context, and the cdr is the heading to be used. The heading
  2503. may also be the empty string. The following placeholders can be
  2504. used:
  2505. %t a time stamp.
  2506. %T an active time stamp instead the default inactive one
  2507. %d a short-format time stamp.
  2508. %D an active short-format time stamp.
  2509. %s the new TODO state or time stamp (inactive), in double quotes.
  2510. %S the old TODO state or time stamp (inactive), in double quotes.
  2511. %u the user name.
  2512. %U full user name.
  2513. In fact, it is not a good idea to change the `state' entry,
  2514. because Agenda Log mode depends on the format of these entries."
  2515. :group 'org-todo
  2516. :group 'org-progress
  2517. :type '(list :greedy t
  2518. (cons (const :tag "Heading when closing an item" done) string)
  2519. (cons (const :tag
  2520. "Heading when changing todo state (todo sequence only)"
  2521. state) string)
  2522. (cons (const :tag "Heading when just taking a note" note) string)
  2523. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2524. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2525. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2526. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2527. (cons (const :tag "Heading when refiling" refile) string)
  2528. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2529. (unless (assq 'note org-log-note-headings)
  2530. (push '(note . "%t") org-log-note-headings))
  2531. (defcustom org-log-into-drawer nil
  2532. "Non-nil means insert state change notes and time stamps into a drawer.
  2533. When nil, state changes notes will be inserted after the headline and
  2534. any scheduling and clock lines, but not inside a drawer.
  2535. The value of this variable should be the name of the drawer to use.
  2536. LOGBOOK is proposed as the default drawer for this purpose, you can
  2537. also set this to a string to define the drawer of your choice.
  2538. A value of t is also allowed, representing \"LOGBOOK\".
  2539. A value of t or nil can also be set with on a per-file-basis with
  2540. #+STARTUP: logdrawer
  2541. #+STARTUP: nologdrawer
  2542. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2543. will be ignored.
  2544. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2545. a subtree.
  2546. Do not check directly this variable in a Lisp program. Call
  2547. function `org-log-into-drawer' instead."
  2548. :group 'org-todo
  2549. :group 'org-progress
  2550. :type '(choice
  2551. (const :tag "Not into a drawer" nil)
  2552. (const :tag "LOGBOOK" t)
  2553. (string :tag "Other")))
  2554. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2555. (defun org-log-into-drawer ()
  2556. "Name of the log drawer, as a string, or nil.
  2557. This is the value of `org-log-into-drawer'. However, if the
  2558. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2559. be used instead of the default value."
  2560. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2561. (cond ((equal p "nil") nil)
  2562. ((equal p "t") "LOGBOOK")
  2563. ((stringp p) p)
  2564. (p "LOGBOOK")
  2565. ((stringp org-log-into-drawer) org-log-into-drawer)
  2566. (org-log-into-drawer "LOGBOOK"))))
  2567. (defcustom org-log-state-notes-insert-after-drawers nil
  2568. "Non-nil means insert state change notes after any drawers in entry.
  2569. Only the drawers that *immediately* follow the headline and the
  2570. deadline/scheduled line are skipped.
  2571. When nil, insert notes right after the heading and perhaps the line
  2572. with deadline/scheduling if present.
  2573. This variable will have no effect if `org-log-into-drawer' is
  2574. set."
  2575. :group 'org-todo
  2576. :group 'org-progress
  2577. :type 'boolean)
  2578. (defcustom org-log-states-order-reversed t
  2579. "Non-nil means the latest state note will be directly after heading.
  2580. When nil, the state change notes will be ordered according to time.
  2581. This option can also be set with on a per-file-basis with
  2582. #+STARTUP: logstatesreversed
  2583. #+STARTUP: nologstatesreversed"
  2584. :group 'org-todo
  2585. :group 'org-progress
  2586. :type 'boolean)
  2587. (defcustom org-todo-repeat-to-state nil
  2588. "The TODO state to which a repeater should return the repeating task.
  2589. By default this is the first task in a TODO sequence, or the previous state
  2590. in a TODO_TYP set. But you can specify another task here.
  2591. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2592. :group 'org-todo
  2593. :version "24.1"
  2594. :type '(choice (const :tag "Head of sequence" nil)
  2595. (string :tag "Specific state")))
  2596. (defcustom org-log-repeat 'time
  2597. "Non-nil means record moving through the DONE state when triggering repeat.
  2598. An auto-repeating task is immediately switched back to TODO when
  2599. marked DONE. If you are not logging state changes (by adding \"@\"
  2600. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2601. record a closing note, there will be no record of the task moving
  2602. through DONE. This variable forces taking a note anyway.
  2603. nil Don't force a record
  2604. time Record a time stamp
  2605. note Prompt for a note and add it with template `org-log-note-headings'
  2606. This option can also be set with on a per-file-basis with
  2607. #+STARTUP: nologrepeat
  2608. #+STARTUP: logrepeat
  2609. #+STARTUP: lognoterepeat
  2610. You can have local logging settings for a subtree by setting the LOGGING
  2611. property to one or more of these keywords."
  2612. :group 'org-todo
  2613. :group 'org-progress
  2614. :type '(choice
  2615. (const :tag "Don't force a record" nil)
  2616. (const :tag "Force recording the DONE state" time)
  2617. (const :tag "Force recording a note with the DONE state" note)))
  2618. (defgroup org-priorities nil
  2619. "Priorities in Org-mode."
  2620. :tag "Org Priorities"
  2621. :group 'org-todo)
  2622. (defcustom org-enable-priority-commands t
  2623. "Non-nil means priority commands are active.
  2624. When nil, these commands will be disabled, so that you never accidentally
  2625. set a priority."
  2626. :group 'org-priorities
  2627. :type 'boolean)
  2628. (defcustom org-highest-priority ?A
  2629. "The highest priority of TODO items. A character like ?A, ?B etc.
  2630. Must have a smaller ASCII number than `org-lowest-priority'."
  2631. :group 'org-priorities
  2632. :type 'character)
  2633. (defcustom org-lowest-priority ?C
  2634. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2635. Must have a larger ASCII number than `org-highest-priority'."
  2636. :group 'org-priorities
  2637. :type 'character)
  2638. (defcustom org-default-priority ?B
  2639. "The default priority of TODO items.
  2640. This is the priority an item gets if no explicit priority is given.
  2641. When starting to cycle on an empty priority the first step in the cycle
  2642. depends on `org-priority-start-cycle-with-default'. The resulting first
  2643. step priority must not exceed the range from `org-highest-priority' to
  2644. `org-lowest-priority' which means that `org-default-priority' has to be
  2645. in this range exclusive or inclusive the range boundaries. Else the
  2646. first step refuses to set the default and the second will fall back
  2647. to (depending on the command used) the highest or lowest priority."
  2648. :group 'org-priorities
  2649. :type 'character)
  2650. (defcustom org-priority-start-cycle-with-default t
  2651. "Non-nil means start with default priority when starting to cycle.
  2652. When this is nil, the first step in the cycle will be (depending on the
  2653. command used) one higher or lower than the default priority.
  2654. See also `org-default-priority'."
  2655. :group 'org-priorities
  2656. :type 'boolean)
  2657. (defcustom org-get-priority-function nil
  2658. "Function to extract the priority from a string.
  2659. The string is normally the headline. If this is nil Org computes the
  2660. priority from the priority cookie like [#A] in the headline. It returns
  2661. an integer, increasing by 1000 for each priority level.
  2662. The user can set a different function here, which should take a string
  2663. as an argument and return the numeric priority."
  2664. :group 'org-priorities
  2665. :version "24.1"
  2666. :type '(choice
  2667. (const nil)
  2668. (function)))
  2669. (defgroup org-time nil
  2670. "Options concerning time stamps and deadlines in Org-mode."
  2671. :tag "Org Time"
  2672. :group 'org)
  2673. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2674. "Number of minutes to round time stamps to.
  2675. These are two values, the first applies when first creating a time stamp.
  2676. The second applies when changing it with the commands `S-up' and `S-down'.
  2677. When changing the time stamp, this means that it will change in steps
  2678. of N minutes, as given by the second value.
  2679. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2680. numbers should be factors of 60, so for example 5, 10, 15.
  2681. When this is larger than 1, you can still force an exact time stamp by using
  2682. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2683. and by using a prefix arg to `S-up/down' to specify the exact number
  2684. of minutes to shift."
  2685. :group 'org-time
  2686. :get (lambda (var) ; Make sure both elements are there
  2687. (if (integerp (default-value var))
  2688. (list (default-value var) 5)
  2689. (default-value var)))
  2690. :type '(list
  2691. (integer :tag "when inserting times")
  2692. (integer :tag "when modifying times")))
  2693. ;; Normalize old customizations of this variable.
  2694. (when (integerp org-time-stamp-rounding-minutes)
  2695. (setq org-time-stamp-rounding-minutes
  2696. (list org-time-stamp-rounding-minutes
  2697. org-time-stamp-rounding-minutes)))
  2698. (defcustom org-display-custom-times nil
  2699. "Non-nil means overlay custom formats over all time stamps.
  2700. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2701. To turn this on on a per-file basis, insert anywhere in the file:
  2702. #+STARTUP: customtime"
  2703. :group 'org-time
  2704. :set 'set-default
  2705. :type 'sexp)
  2706. (make-variable-buffer-local 'org-display-custom-times)
  2707. (defcustom org-time-stamp-custom-formats
  2708. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2709. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2710. These are overlaid over the default ISO format if the variable
  2711. `org-display-custom-times' is set. Time like %H:%M should be at the
  2712. end of the second format. The custom formats are also honored by export
  2713. commands, if custom time display is turned on at the time of export."
  2714. :group 'org-time
  2715. :type 'sexp)
  2716. (defun org-time-stamp-format (&optional long inactive)
  2717. "Get the right format for a time string."
  2718. (let ((f (if long (cdr org-time-stamp-formats)
  2719. (car org-time-stamp-formats))))
  2720. (if inactive
  2721. (concat "[" (substring f 1 -1) "]")
  2722. f)))
  2723. (defcustom org-time-clocksum-format
  2724. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2725. "The format string used when creating CLOCKSUM lines.
  2726. This is also used when Org mode generates a time duration.
  2727. The value can be a single format string containing two
  2728. %-sequences, which will be filled with the number of hours and
  2729. minutes in that order.
  2730. Alternatively, the value can be a plist associating any of the
  2731. keys :years, :months, :weeks, :days, :hours or :minutes with
  2732. format strings. The time duration is formatted using only the
  2733. time components that are needed and concatenating the results.
  2734. If a time unit in absent, it falls back to the next smallest
  2735. unit.
  2736. The keys :require-years, :require-months, :require-days,
  2737. :require-weeks, :require-hours, :require-minutes are also
  2738. meaningful. A non-nil value for these keys indicates that the
  2739. corresponding time component should always be included, even if
  2740. its value is 0.
  2741. For example,
  2742. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2743. :require-minutes t)
  2744. means durations longer than a day will be expressed in days,
  2745. hours and minutes, and durations less than a day will always be
  2746. expressed in hours and minutes (even for durations less than an
  2747. hour).
  2748. The value
  2749. \(:days \"%dd\" :minutes \"%dm\")
  2750. means durations longer than a day will be expressed in days and
  2751. minutes, and durations less than a day will be expressed entirely
  2752. in minutes (even for durations longer than an hour)."
  2753. :group 'org-time
  2754. :group 'org-clock
  2755. :version "24.4"
  2756. :package-version '(Org . "8.0")
  2757. :type '(choice (string :tag "Format string")
  2758. (set :tag "Plist"
  2759. (group :inline t (const :tag "Years" :years)
  2760. (string :tag "Format string"))
  2761. (group :inline t
  2762. (const :tag "Always show years" :require-years)
  2763. (const t))
  2764. (group :inline t (const :tag "Months" :months)
  2765. (string :tag "Format string"))
  2766. (group :inline t
  2767. (const :tag "Always show months" :require-months)
  2768. (const t))
  2769. (group :inline t (const :tag "Weeks" :weeks)
  2770. (string :tag "Format string"))
  2771. (group :inline t
  2772. (const :tag "Always show weeks" :require-weeks)
  2773. (const t))
  2774. (group :inline t (const :tag "Days" :days)
  2775. (string :tag "Format string"))
  2776. (group :inline t
  2777. (const :tag "Always show days" :require-days)
  2778. (const t))
  2779. (group :inline t (const :tag "Hours" :hours)
  2780. (string :tag "Format string"))
  2781. (group :inline t
  2782. (const :tag "Always show hours" :require-hours)
  2783. (const t))
  2784. (group :inline t (const :tag "Minutes" :minutes)
  2785. (string :tag "Format string"))
  2786. (group :inline t
  2787. (const :tag "Always show minutes" :require-minutes)
  2788. (const t)))))
  2789. (defcustom org-time-clocksum-use-fractional nil
  2790. "When non-nil, \\[org-clock-display] uses fractional times.
  2791. See `org-time-clocksum-format' for more on time clock formats."
  2792. :group 'org-time
  2793. :group 'org-clock
  2794. :version "24.3"
  2795. :type 'boolean)
  2796. (defcustom org-time-clocksum-use-effort-durations nil
  2797. "When non-nil, \\[org-clock-display] uses effort durations.
  2798. E.g. by default, one day is considered to be a 8 hours effort,
  2799. so a task that has been clocked for 16 hours will be displayed
  2800. as during 2 days in the clock display or in the clocktable.
  2801. See `org-effort-durations' on how to set effort durations
  2802. and `org-time-clocksum-format' for more on time clock formats."
  2803. :group 'org-time
  2804. :group 'org-clock
  2805. :version "24.4"
  2806. :package-version '(Org . "8.0")
  2807. :type 'boolean)
  2808. (defcustom org-time-clocksum-fractional-format "%.2f"
  2809. "The format string used when creating CLOCKSUM lines,
  2810. or when Org mode generates a time duration, if
  2811. `org-time-clocksum-use-fractional' is enabled.
  2812. The value can be a single format string containing one
  2813. %-sequence, which will be filled with the number of hours as
  2814. a float.
  2815. Alternatively, the value can be a plist associating any of the
  2816. keys :years, :months, :weeks, :days, :hours or :minutes with
  2817. a format string. The time duration is formatted using the
  2818. largest time unit which gives a non-zero integer part. If all
  2819. specified formats have zero integer part, the smallest time unit
  2820. is used."
  2821. :group 'org-time
  2822. :type '(choice (string :tag "Format string")
  2823. (set (group :inline t (const :tag "Years" :years)
  2824. (string :tag "Format string"))
  2825. (group :inline t (const :tag "Months" :months)
  2826. (string :tag "Format string"))
  2827. (group :inline t (const :tag "Weeks" :weeks)
  2828. (string :tag "Format string"))
  2829. (group :inline t (const :tag "Days" :days)
  2830. (string :tag "Format string"))
  2831. (group :inline t (const :tag "Hours" :hours)
  2832. (string :tag "Format string"))
  2833. (group :inline t (const :tag "Minutes" :minutes)
  2834. (string :tag "Format string")))))
  2835. (defcustom org-deadline-warning-days 14
  2836. "Number of days before expiration during which a deadline becomes active.
  2837. This variable governs the display in sparse trees and in the agenda.
  2838. When 0 or negative, it means use this number (the absolute value of it)
  2839. even if a deadline has a different individual lead time specified.
  2840. Custom commands can set this variable in the options section."
  2841. :group 'org-time
  2842. :group 'org-agenda-daily/weekly
  2843. :type 'integer)
  2844. (defcustom org-scheduled-delay-days 0
  2845. "Number of days before a scheduled item becomes active.
  2846. This variable governs the display in sparse trees and in the agenda.
  2847. The default value (i.e. 0) means: don't delay scheduled item.
  2848. When negative, it means use this number (the absolute value of it)
  2849. even if a scheduled item has a different individual delay time
  2850. specified.
  2851. Custom commands can set this variable in the options section."
  2852. :group 'org-time
  2853. :group 'org-agenda-daily/weekly
  2854. :version "24.4"
  2855. :package-version '(Org . "8.0")
  2856. :type 'integer)
  2857. (defcustom org-read-date-prefer-future t
  2858. "Non-nil means assume future for incomplete date input from user.
  2859. This affects the following situations:
  2860. 1. The user gives a month but not a year.
  2861. For example, if it is April and you enter \"feb 2\", this will be read
  2862. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2863. 2. The user gives a day, but no month.
  2864. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2865. read this as the third of *next* month. However, if you enter \"17\",
  2866. it will be considered as *this* month.
  2867. If you set this variable to the symbol `time', then also the following
  2868. will work:
  2869. 3. If the user gives a time.
  2870. If the time is before now, it will be interpreted as tomorrow.
  2871. Currently none of this works for ISO week specifications.
  2872. When this option is nil, the current day, month and year will always be
  2873. used as defaults.
  2874. See also `org-agenda-jump-prefer-future'."
  2875. :group 'org-time
  2876. :type '(choice
  2877. (const :tag "Never" nil)
  2878. (const :tag "Check month and day" t)
  2879. (const :tag "Check month, day, and time" time)))
  2880. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2881. "Should the agenda jump command prefer the future for incomplete dates?
  2882. The default is to do the same as configured in `org-read-date-prefer-future'.
  2883. But you can also set a deviating value here.
  2884. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2885. :group 'org-agenda
  2886. :group 'org-time
  2887. :version "24.1"
  2888. :type '(choice
  2889. (const :tag "Use org-read-date-prefer-future"
  2890. org-read-date-prefer-future)
  2891. (const :tag "Never" nil)
  2892. (const :tag "Always" t)))
  2893. (defcustom org-read-date-force-compatible-dates t
  2894. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2895. Depending on the system Emacs is running on, certain dates cannot
  2896. be represented with the type used internally to represent time.
  2897. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2898. correctly. Some systems allow for earlier dates, some for later,
  2899. some for both. One way to find out it to insert any date into an
  2900. Org buffer, putting the cursor on the year and hitting S-up and
  2901. S-down to test the range.
  2902. When this variable is set to t, the date/time prompt will not let
  2903. you specify dates outside the 1970-2037 range, so it is certain that
  2904. these dates will work in whatever version of Emacs you are
  2905. running, and also that you can move a file from one Emacs implementation
  2906. to another. WHenever Org is forcing the year for you, it will display
  2907. a message and beep.
  2908. When this variable is nil, Org will check if the date is
  2909. representable in the specific Emacs implementation you are using.
  2910. If not, it will force a year, usually the current year, and beep
  2911. to remind you. Currently this setting is not recommended because
  2912. the likelihood that you will open your Org files in an Emacs that
  2913. has limited date range is not negligible.
  2914. A workaround for this problem is to use diary sexp dates for time
  2915. stamps outside of this range."
  2916. :group 'org-time
  2917. :version "24.1"
  2918. :type 'boolean)
  2919. (defcustom org-read-date-display-live t
  2920. "Non-nil means display current interpretation of date prompt live.
  2921. This display will be in an overlay, in the minibuffer."
  2922. :group 'org-time
  2923. :type 'boolean)
  2924. (defcustom org-read-date-popup-calendar t
  2925. "Non-nil means pop up a calendar when prompting for a date.
  2926. In the calendar, the date can be selected with mouse-1. However, the
  2927. minibuffer will also be active, and you can simply enter the date as well.
  2928. When nil, only the minibuffer will be available."
  2929. :group 'org-time
  2930. :type 'boolean)
  2931. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2932. 'org-read-date-popup-calendar)
  2933. (make-obsolete-variable
  2934. 'org-read-date-minibuffer-setup-hook
  2935. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2936. (defcustom org-read-date-minibuffer-setup-hook nil
  2937. "Hook to be used to set up keys for the date/time interface.
  2938. Add key definitions to `minibuffer-local-map', which will be a
  2939. temporary copy.
  2940. WARNING: This option is obsolete, you should use
  2941. `org-read-date-minibuffer-local-map' to set up keys."
  2942. :group 'org-time
  2943. :type 'hook)
  2944. (defcustom org-extend-today-until 0
  2945. "The hour when your day really ends. Must be an integer.
  2946. This has influence for the following applications:
  2947. - When switching the agenda to \"today\". It it is still earlier than
  2948. the time given here, the day recognized as TODAY is actually yesterday.
  2949. - When a date is read from the user and it is still before the time given
  2950. here, the current date and time will be assumed to be yesterday, 23:59.
  2951. Also, timestamps inserted in capture templates follow this rule.
  2952. IMPORTANT: This is a feature whose implementation is and likely will
  2953. remain incomplete. Really, it is only here because past midnight seems to
  2954. be the favorite working time of John Wiegley :-)"
  2955. :group 'org-time
  2956. :type 'integer)
  2957. (defcustom org-use-effective-time nil
  2958. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2959. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2960. \"effective time\" of any timestamps between midnight and 8am will be
  2961. 23:59 of the previous day."
  2962. :group 'org-time
  2963. :version "24.1"
  2964. :type 'boolean)
  2965. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2966. "When non-nil, use the last clock out time for `org-todo'.
  2967. Note that this option has precedence over the combined use of
  2968. `org-use-effective-time' and `org-extend-today-until'."
  2969. :group 'org-time
  2970. :version "24.4"
  2971. :package-version '(Org . "8.0")
  2972. :type 'boolean)
  2973. (defcustom org-edit-timestamp-down-means-later nil
  2974. "Non-nil means S-down will increase the time in a time stamp.
  2975. When nil, S-up will increase."
  2976. :group 'org-time
  2977. :type 'boolean)
  2978. (defcustom org-calendar-follow-timestamp-change t
  2979. "Non-nil means make the calendar window follow timestamp changes.
  2980. When a timestamp is modified and the calendar window is visible, it will be
  2981. moved to the new date."
  2982. :group 'org-time
  2983. :type 'boolean)
  2984. (defgroup org-tags nil
  2985. "Options concerning tags in Org-mode."
  2986. :tag "Org Tags"
  2987. :group 'org)
  2988. (defcustom org-tag-alist nil
  2989. "List of tags allowed in Org-mode files.
  2990. When this list is nil, Org-mode will base TAG input on what is already in the
  2991. buffer.
  2992. The value of this variable is an alist, the car of each entry must be a
  2993. keyword as a string, the cdr may be a character that is used to select
  2994. that tag through the fast-tag-selection interface.
  2995. See the manual for details."
  2996. :group 'org-tags
  2997. :type '(repeat
  2998. (choice
  2999. (cons (string :tag "Tag name")
  3000. (character :tag "Access char"))
  3001. (list :tag "Start radio group"
  3002. (const :startgroup)
  3003. (option (string :tag "Group description")))
  3004. (list :tag "Start tag group, non distinct"
  3005. (const :startgrouptag)
  3006. (option (string :tag "Group description")))
  3007. (list :tag "Group tags delimiter"
  3008. (const :grouptags))
  3009. (list :tag "End radio group"
  3010. (const :endgroup)
  3011. (option (string :tag "Group description")))
  3012. (list :tag "End tag group, non distinct"
  3013. (const :endgrouptag)
  3014. (option (string :tag "Group description")))
  3015. (const :tag "New line" (:newline)))))
  3016. (defcustom org-tag-persistent-alist nil
  3017. "List of tags that will always appear in all Org-mode files.
  3018. This is in addition to any in buffer settings or customizations
  3019. of `org-tag-alist'.
  3020. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3021. The value of this variable is an alist, the car of each entry must be a
  3022. keyword as a string, the cdr may be a character that is used to select
  3023. that tag through the fast-tag-selection interface.
  3024. See the manual for details.
  3025. To disable these tags on a per-file basis, insert anywhere in the file:
  3026. #+STARTUP: noptag"
  3027. :group 'org-tags
  3028. :type '(repeat
  3029. (choice
  3030. (cons (string :tag "Tag name")
  3031. (character :tag "Access char"))
  3032. (const :tag "Start radio group" (:startgroup))
  3033. (const :tag "Group tags delimiter" (:grouptags))
  3034. (const :tag "End radio group" (:endgroup))
  3035. (const :tag "New line" (:newline)))))
  3036. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3037. "If non-nil, always offer completion for all tags of all agenda files.
  3038. Instead of customizing this variable directly, you might want to
  3039. set it locally for capture buffers, because there no list of
  3040. tags in that file can be created dynamically (there are none).
  3041. (add-hook 'org-capture-mode-hook
  3042. (lambda ()
  3043. (set (make-local-variable
  3044. 'org-complete-tags-always-offer-all-agenda-tags)
  3045. t)))"
  3046. :group 'org-tags
  3047. :version "24.1"
  3048. :type 'boolean)
  3049. (defvar org-file-tags nil
  3050. "List of tags that can be inherited by all entries in the file.
  3051. The tags will be inherited if the variable `org-use-tag-inheritance'
  3052. says they should be.
  3053. This variable is populated from #+FILETAGS lines.")
  3054. (defcustom org-use-fast-tag-selection 'auto
  3055. "Non-nil means use fast tag selection scheme.
  3056. This is a special interface to select and deselect tags with single keys.
  3057. When nil, fast selection is never used.
  3058. When the symbol `auto', fast selection is used if and only if selection
  3059. characters for tags have been configured, either through the variable
  3060. `org-tag-alist' or through a #+TAGS line in the buffer.
  3061. When t, fast selection is always used and selection keys are assigned
  3062. automatically if necessary."
  3063. :group 'org-tags
  3064. :type '(choice
  3065. (const :tag "Always" t)
  3066. (const :tag "Never" nil)
  3067. (const :tag "When selection characters are configured" auto)))
  3068. (defcustom org-fast-tag-selection-single-key nil
  3069. "Non-nil means fast tag selection exits after first change.
  3070. When nil, you have to press RET to exit it.
  3071. During fast tag selection, you can toggle this flag with `C-c'.
  3072. This variable can also have the value `expert'. In this case, the window
  3073. displaying the tags menu is not even shown, until you press C-c again."
  3074. :group 'org-tags
  3075. :type '(choice
  3076. (const :tag "No" nil)
  3077. (const :tag "Yes" t)
  3078. (const :tag "Expert" expert)))
  3079. (defvar org-fast-tag-selection-include-todo nil
  3080. "Non-nil means fast tags selection interface will also offer TODO states.
  3081. This is an undocumented feature, you should not rely on it.")
  3082. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3083. "The column to which tags should be indented in a headline.
  3084. If this number is positive, it specifies the column. If it is negative,
  3085. it means that the tags should be flushright to that column. For example,
  3086. -80 works well for a normal 80 character screen.
  3087. When 0, place tags directly after headline text, with only one space in
  3088. between."
  3089. :group 'org-tags
  3090. :type 'integer)
  3091. (defcustom org-auto-align-tags t
  3092. "Non-nil keeps tags aligned when modifying headlines.
  3093. Some operations (i.e. demoting) change the length of a headline and
  3094. therefore shift the tags around. With this option turned on, after
  3095. each such operation the tags are again aligned to `org-tags-column'."
  3096. :group 'org-tags
  3097. :type 'boolean)
  3098. (defcustom org-use-tag-inheritance t
  3099. "Non-nil means tags in levels apply also for sublevels.
  3100. When nil, only the tags directly given in a specific line apply there.
  3101. This may also be a list of tags that should be inherited, or a regexp that
  3102. matches tags that should be inherited. Additional control is possible
  3103. with the variable `org-tags-exclude-from-inheritance' which gives an
  3104. explicit list of tags to be excluded from inheritance, even if the value of
  3105. `org-use-tag-inheritance' would select it for inheritance.
  3106. If this option is t, a match early-on in a tree can lead to a large
  3107. number of matches in the subtree when constructing the agenda or creating
  3108. a sparse tree. If you only want to see the first match in a tree during
  3109. a search, check out the variable `org-tags-match-list-sublevels'."
  3110. :group 'org-tags
  3111. :type '(choice
  3112. (const :tag "Not" nil)
  3113. (const :tag "Always" t)
  3114. (repeat :tag "Specific tags" (string :tag "Tag"))
  3115. (regexp :tag "Tags matched by regexp")))
  3116. (defcustom org-tags-exclude-from-inheritance nil
  3117. "List of tags that should never be inherited.
  3118. This is a way to exclude a few tags from inheritance. For way to do
  3119. the opposite, to actively allow inheritance for selected tags,
  3120. see the variable `org-use-tag-inheritance'."
  3121. :group 'org-tags
  3122. :type '(repeat (string :tag "Tag")))
  3123. (defun org-tag-inherit-p (tag)
  3124. "Check if TAG is one that should be inherited."
  3125. (cond
  3126. ((member tag org-tags-exclude-from-inheritance) nil)
  3127. ((eq org-use-tag-inheritance t) t)
  3128. ((not org-use-tag-inheritance) nil)
  3129. ((stringp org-use-tag-inheritance)
  3130. (string-match org-use-tag-inheritance tag))
  3131. ((listp org-use-tag-inheritance)
  3132. (member tag org-use-tag-inheritance))
  3133. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3134. (defcustom org-tags-match-list-sublevels t
  3135. "Non-nil means list also sublevels of headlines matching a search.
  3136. This variable applies to tags/property searches, and also to stuck
  3137. projects because this search is based on a tags match as well.
  3138. When set to the symbol `indented', sublevels are indented with
  3139. leading dots.
  3140. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3141. the sublevels of a headline matching a tag search often also match
  3142. the same search. Listing all of them can create very long lists.
  3143. Setting this variable to nil causes subtrees of a match to be skipped.
  3144. This variable is semi-obsolete and probably should always be true. It
  3145. is better to limit inheritance to certain tags using the variables
  3146. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3147. :group 'org-tags
  3148. :type '(choice
  3149. (const :tag "No, don't list them" nil)
  3150. (const :tag "Yes, do list them" t)
  3151. (const :tag "List them, indented with leading dots" indented)))
  3152. (defcustom org-tags-sort-function nil
  3153. "When set, tags are sorted using this function as a comparator."
  3154. :group 'org-tags
  3155. :type '(choice
  3156. (const :tag "No sorting" nil)
  3157. (const :tag "Alphabetical" string<)
  3158. (const :tag "Reverse alphabetical" string>)
  3159. (function :tag "Custom function" nil)))
  3160. (defvar org-tags-history nil
  3161. "History of minibuffer reads for tags.")
  3162. (defvar org-last-tags-completion-table nil
  3163. "The last used completion table for tags.")
  3164. (defvar org-after-tags-change-hook nil
  3165. "Hook that is run after the tags in a line have changed.")
  3166. (defgroup org-properties nil
  3167. "Options concerning properties in Org-mode."
  3168. :tag "Org Properties"
  3169. :group 'org)
  3170. (defcustom org-property-format "%-10s %s"
  3171. "How property key/value pairs should be formatted by `indent-line'.
  3172. When `indent-line' hits a property definition, it will format the line
  3173. according to this format, mainly to make sure that the values are
  3174. lined-up with respect to each other."
  3175. :group 'org-properties
  3176. :type 'string)
  3177. (defcustom org-properties-postprocess-alist nil
  3178. "Alist of properties and functions to adjust inserted values.
  3179. Elements of this alist must be of the form
  3180. ([string] [function])
  3181. where [string] must be a property name and [function] must be a
  3182. lambda expression: this lambda expression must take one argument,
  3183. the value to adjust, and return the new value as a string.
  3184. For example, this element will allow the property \"Remaining\"
  3185. to be updated wrt the relation between the \"Effort\" property
  3186. and the clock summary:
  3187. ((\"Remaining\" (lambda(value)
  3188. (let ((clocksum (org-clock-sum-current-item))
  3189. (effort (org-duration-string-to-minutes
  3190. (org-entry-get (point) \"Effort\"))))
  3191. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3192. :group 'org-properties
  3193. :version "24.1"
  3194. :type '(alist :key-type (string :tag "Property")
  3195. :value-type (function :tag "Function")))
  3196. (defcustom org-use-property-inheritance nil
  3197. "Non-nil means properties apply also for sublevels.
  3198. This setting is chiefly used during property searches. Turning it on can
  3199. cause significant overhead when doing a search, which is why it is not
  3200. on by default.
  3201. When nil, only the properties directly given in the current entry count.
  3202. When t, every property is inherited. The value may also be a list of
  3203. properties that should have inheritance, or a regular expression matching
  3204. properties that should be inherited.
  3205. However, note that some special properties use inheritance under special
  3206. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3207. and the properties ending in \"_ALL\" when they are used as descriptor
  3208. for valid values of a property.
  3209. Note for programmers:
  3210. When querying an entry with `org-entry-get', you can control if inheritance
  3211. should be used. By default, `org-entry-get' looks only at the local
  3212. properties. You can request inheritance by setting the inherit argument
  3213. to t (to force inheritance) or to `selective' (to respect the setting
  3214. in this variable)."
  3215. :group 'org-properties
  3216. :type '(choice
  3217. (const :tag "Not" nil)
  3218. (const :tag "Always" t)
  3219. (repeat :tag "Specific properties" (string :tag "Property"))
  3220. (regexp :tag "Properties matched by regexp")))
  3221. (defun org-property-inherit-p (property)
  3222. "Check if PROPERTY is one that should be inherited."
  3223. (cond
  3224. ((eq org-use-property-inheritance t) t)
  3225. ((not org-use-property-inheritance) nil)
  3226. ((stringp org-use-property-inheritance)
  3227. (string-match org-use-property-inheritance property))
  3228. ((listp org-use-property-inheritance)
  3229. (member property org-use-property-inheritance))
  3230. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3231. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3232. "The default column format, if no other format has been defined.
  3233. This variable can be set on the per-file basis by inserting a line
  3234. #+COLUMNS: %25ITEM ....."
  3235. :group 'org-properties
  3236. :type 'string)
  3237. (defcustom org-columns-ellipses ".."
  3238. "The ellipses to be used when a field in column view is truncated.
  3239. When this is the empty string, as many characters as possible are shown,
  3240. but then there will be no visual indication that the field has been truncated.
  3241. When this is a string of length N, the last N characters of a truncated
  3242. field are replaced by this string. If the column is narrower than the
  3243. ellipses string, only part of the ellipses string will be shown."
  3244. :group 'org-properties
  3245. :type 'string)
  3246. (defcustom org-columns-modify-value-for-display-function nil
  3247. "Function that modifies values for display in column view.
  3248. For example, it can be used to cut out a certain part from a time stamp.
  3249. The function must take 2 arguments:
  3250. column-title The title of the column (*not* the property name)
  3251. value The value that should be modified.
  3252. The function should return the value that should be displayed,
  3253. or nil if the normal value should be used."
  3254. :group 'org-properties
  3255. :type '(choice (const nil) (function)))
  3256. (defconst org-global-properties-fixed
  3257. '(("VISIBILITY_ALL" . "folded children content all")
  3258. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3259. "List of property/value pairs that can be inherited by any entry.
  3260. These are fixed values, for the preset properties. The user variable
  3261. that can be used to add to this list is `org-global-properties'.
  3262. The entries in this list are cons cells where the car is a property
  3263. name and cdr is a string with the value. If the value represents
  3264. multiple items like an \"_ALL\" property, separate the items by
  3265. spaces.")
  3266. (defcustom org-global-properties nil
  3267. "List of property/value pairs that can be inherited by any entry.
  3268. This list will be combined with the constant `org-global-properties-fixed'.
  3269. The entries in this list are cons cells where the car is a property
  3270. name and cdr is a string with the value.
  3271. You can set buffer-local values for the same purpose in the variable
  3272. `org-file-properties' this by adding lines like
  3273. #+PROPERTY: NAME VALUE"
  3274. :group 'org-properties
  3275. :type '(repeat
  3276. (cons (string :tag "Property")
  3277. (string :tag "Value"))))
  3278. (defvar org-file-properties nil
  3279. "List of property/value pairs that can be inherited by any entry.
  3280. Valid for the current buffer.
  3281. This variable is populated from #+PROPERTY lines.")
  3282. (make-variable-buffer-local 'org-file-properties)
  3283. (defgroup org-agenda nil
  3284. "Options concerning agenda views in Org-mode."
  3285. :tag "Org Agenda"
  3286. :group 'org)
  3287. (defvar org-category nil
  3288. "Variable used by org files to set a category for agenda display.
  3289. Such files should use a file variable to set it, for example
  3290. # -*- mode: org; org-category: \"ELisp\"
  3291. or contain a special line
  3292. #+CATEGORY: ELisp
  3293. If the file does not specify a category, then file's base name
  3294. is used instead.")
  3295. (make-variable-buffer-local 'org-category)
  3296. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3297. (defcustom org-agenda-files nil
  3298. "The files to be used for agenda display.
  3299. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3300. \\[org-remove-file]. You can also use customize to edit the list.
  3301. If an entry is a directory, all files in that directory that are matched by
  3302. `org-agenda-file-regexp' will be part of the file list.
  3303. If the value of the variable is not a list but a single file name, then
  3304. the list of agenda files is actually stored and maintained in that file, one
  3305. agenda file per line. In this file paths can be given relative to
  3306. `org-directory'. Tilde expansion and environment variable substitution
  3307. are also made."
  3308. :group 'org-agenda
  3309. :type '(choice
  3310. (repeat :tag "List of files and directories" file)
  3311. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3312. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3313. "Regular expression to match files for `org-agenda-files'.
  3314. If any element in the list in that variable contains a directory instead
  3315. of a normal file, all files in that directory that are matched by this
  3316. regular expression will be included."
  3317. :group 'org-agenda
  3318. :type 'regexp)
  3319. (defcustom org-agenda-text-search-extra-files nil
  3320. "List of extra files to be searched by text search commands.
  3321. These files will be searched in addition to the agenda files by the
  3322. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3323. Note that these files will only be searched for text search commands,
  3324. not for the other agenda views like todo lists, tag searches or the weekly
  3325. agenda. This variable is intended to list notes and possibly archive files
  3326. that should also be searched by these two commands.
  3327. In fact, if the first element in the list is the symbol `agenda-archives',
  3328. then all archive files of all agenda files will be added to the search
  3329. scope."
  3330. :group 'org-agenda
  3331. :type '(set :greedy t
  3332. (const :tag "Agenda Archives" agenda-archives)
  3333. (repeat :inline t (file))))
  3334. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3335. 'org-agenda-text-search-extra-files)
  3336. (defcustom org-agenda-skip-unavailable-files nil
  3337. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3338. A nil value means to remove them, after a query, from the list."
  3339. :group 'org-agenda
  3340. :type 'boolean)
  3341. (defcustom org-calendar-to-agenda-key [?c]
  3342. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3343. The command `org-calendar-goto-agenda' will be bound to this key. The
  3344. default is the character `c' because then `c' can be used to switch back and
  3345. forth between agenda and calendar."
  3346. :group 'org-agenda
  3347. :type 'sexp)
  3348. (defcustom org-calendar-insert-diary-entry-key [?i]
  3349. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3350. This option is irrelevant until `org-agenda-diary-file' has been configured
  3351. to point to an Org-mode file. When that is the case, the command
  3352. `org-agenda-diary-entry' will be bound to the key given here, by default
  3353. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3354. if you want to continue doing this, you need to change this to a different
  3355. key."
  3356. :group 'org-agenda
  3357. :type 'sexp)
  3358. (defcustom org-agenda-diary-file 'diary-file
  3359. "File to which to add new entries with the `i' key in agenda and calendar.
  3360. When this is the symbol `diary-file', the functionality in the Emacs
  3361. calendar will be used to add entries to the `diary-file'. But when this
  3362. points to a file, `org-agenda-diary-entry' will be used instead."
  3363. :group 'org-agenda
  3364. :type '(choice
  3365. (const :tag "The standard Emacs diary file" diary-file)
  3366. (file :tag "Special Org file diary entries")))
  3367. (eval-after-load "calendar"
  3368. '(progn
  3369. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3370. 'org-calendar-goto-agenda)
  3371. (add-hook 'calendar-mode-hook
  3372. (lambda ()
  3373. (unless (eq org-agenda-diary-file 'diary-file)
  3374. (define-key calendar-mode-map
  3375. org-calendar-insert-diary-entry-key
  3376. 'org-agenda-diary-entry))))))
  3377. (defgroup org-latex nil
  3378. "Options for embedding LaTeX code into Org-mode."
  3379. :tag "Org LaTeX"
  3380. :group 'org)
  3381. (defcustom org-format-latex-options
  3382. '(:foreground default :background default :scale 1.0
  3383. :html-foreground "Black" :html-background "Transparent"
  3384. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3385. "Options for creating images from LaTeX fragments.
  3386. This is a property list with the following properties:
  3387. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3388. `default' means use the foreground of the default face.
  3389. `auto' means use the foreground from the text face.
  3390. :background the background color, or \"Transparent\".
  3391. `default' means use the background of the default face.
  3392. `auto' means use the background from the text face.
  3393. :scale a scaling factor for the size of the images, to get more pixels
  3394. :html-foreground, :html-background, :html-scale
  3395. the same numbers for HTML export.
  3396. :matchers a list indicating which matchers should be used to
  3397. find LaTeX fragments. Valid members of this list are:
  3398. \"begin\" find environments
  3399. \"$1\" find single characters surrounded by $.$
  3400. \"$\" find math expressions surrounded by $...$
  3401. \"$$\" find math expressions surrounded by $$....$$
  3402. \"\\(\" find math expressions surrounded by \\(...\\)
  3403. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3404. :group 'org-latex
  3405. :type 'plist)
  3406. (defcustom org-format-latex-signal-error t
  3407. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3408. When nil, just push out a message."
  3409. :group 'org-latex
  3410. :version "24.1"
  3411. :type 'boolean)
  3412. (defcustom org-latex-to-mathml-jar-file nil
  3413. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3414. Use this to specify additional executable file say a jar file.
  3415. When using MathToWeb as the converter, specify the full-path to
  3416. your mathtoweb.jar file."
  3417. :group 'org-latex
  3418. :version "24.1"
  3419. :type '(choice
  3420. (const :tag "None" nil)
  3421. (file :tag "JAR file" :must-match t)))
  3422. (defcustom org-latex-to-mathml-convert-command nil
  3423. "Command to convert LaTeX fragments to MathML.
  3424. Replace format-specifiers in the command as noted below and use
  3425. `shell-command' to convert LaTeX to MathML.
  3426. %j: Executable file in fully expanded form as specified by
  3427. `org-latex-to-mathml-jar-file'.
  3428. %I: Input LaTeX file in fully expanded form.
  3429. %i: The latex fragment to be converted.
  3430. %o: Output MathML file.
  3431. This command is used by `org-create-math-formula'.
  3432. When using MathToWeb as the converter, set this option to
  3433. \"java -jar %j -unicode -force -df %o %I\".
  3434. When using LaTeXML set this option to
  3435. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3436. :group 'org-latex
  3437. :version "24.1"
  3438. :type '(choice
  3439. (const :tag "None" nil)
  3440. (string :tag "\nShell command")))
  3441. (defcustom org-latex-create-formula-image-program 'dvipng
  3442. "Program to convert LaTeX fragments with.
  3443. dvipng Process the LaTeX fragments to dvi file, then convert
  3444. dvi files to png files using dvipng.
  3445. This will also include processing of non-math environments.
  3446. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3447. to convert pdf files to png files"
  3448. :group 'org-latex
  3449. :version "24.1"
  3450. :type '(choice
  3451. (const :tag "dvipng" dvipng)
  3452. (const :tag "imagemagick" imagemagick)))
  3453. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3454. "Path to store latex preview images.
  3455. A relative path here creates many directories relative to the
  3456. processed org files paths. An absolute path puts all preview
  3457. images at the same place."
  3458. :group 'org-latex
  3459. :version "24.3"
  3460. :type 'string)
  3461. (defun org-format-latex-mathml-available-p ()
  3462. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3463. (save-match-data
  3464. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3465. org-latex-to-mathml-convert-command)
  3466. (let ((executable (car (split-string
  3467. org-latex-to-mathml-convert-command))))
  3468. (when (executable-find executable)
  3469. (if (string-match
  3470. "%j" org-latex-to-mathml-convert-command)
  3471. (file-readable-p org-latex-to-mathml-jar-file)
  3472. t))))))
  3473. (defcustom org-format-latex-header "\\documentclass{article}
  3474. \\usepackage[usenames]{color}
  3475. \[PACKAGES]
  3476. \[DEFAULT-PACKAGES]
  3477. \\pagestyle{empty} % do not remove
  3478. % The settings below are copied from fullpage.sty
  3479. \\setlength{\\textwidth}{\\paperwidth}
  3480. \\addtolength{\\textwidth}{-3cm}
  3481. \\setlength{\\oddsidemargin}{1.5cm}
  3482. \\addtolength{\\oddsidemargin}{-2.54cm}
  3483. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3484. \\setlength{\\textheight}{\\paperheight}
  3485. \\addtolength{\\textheight}{-\\headheight}
  3486. \\addtolength{\\textheight}{-\\headsep}
  3487. \\addtolength{\\textheight}{-\\footskip}
  3488. \\addtolength{\\textheight}{-3cm}
  3489. \\setlength{\\topmargin}{1.5cm}
  3490. \\addtolength{\\topmargin}{-2.54cm}"
  3491. "The document header used for processing LaTeX fragments.
  3492. It is imperative that this header make sure that no page number
  3493. appears on the page. The package defined in the variables
  3494. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3495. will either replace the placeholder \"[PACKAGES]\" in this
  3496. header, or they will be appended."
  3497. :group 'org-latex
  3498. :type 'string)
  3499. (defun org-set-packages-alist (var val)
  3500. "Set the packages alist and make sure it has 3 elements per entry."
  3501. (set var (mapcar (lambda (x)
  3502. (if (and (consp x) (= (length x) 2))
  3503. (list (car x) (nth 1 x) t)
  3504. x))
  3505. val)))
  3506. (defun org-get-packages-alist (var)
  3507. "Get the packages alist and make sure it has 3 elements per entry."
  3508. (mapcar (lambda (x)
  3509. (if (and (consp x) (= (length x) 2))
  3510. (list (car x) (nth 1 x) t)
  3511. x))
  3512. (default-value var)))
  3513. (defcustom org-latex-default-packages-alist
  3514. '(("AUTO" "inputenc" t)
  3515. ("T1" "fontenc" t)
  3516. ("" "fixltx2e" nil)
  3517. ("" "graphicx" t)
  3518. ("" "grffile" t)
  3519. ("" "longtable" nil)
  3520. ("" "wrapfig" nil)
  3521. ("" "rotating" nil)
  3522. ("normalem" "ulem" t)
  3523. ("" "amsmath" t)
  3524. ("" "textcomp" t)
  3525. ("" "amssymb" t)
  3526. ("" "capt-of" nil)
  3527. ("" "hyperref" nil))
  3528. "Alist of default packages to be inserted in the header.
  3529. Change this only if one of the packages here causes an
  3530. incompatibility with another package you are using.
  3531. The packages in this list are needed by one part or another of
  3532. Org mode to function properly:
  3533. - inputenc, fontenc: for basic font and character selection
  3534. - fixltx2e: Important patches of LaTeX itself
  3535. - graphicx: for including images
  3536. - grffile: allow periods and spaces in graphics file names
  3537. - longtable: For multipage tables
  3538. - wrapfig: for figure placement
  3539. - rotating: for sideways figures and tables
  3540. - ulem: for underline and strike-through
  3541. - amsmath: for subscript and superscript and math environments
  3542. - textcomp, amssymb: for various symbols used
  3543. for interpreting the entities in `org-entities'. You can skip
  3544. some of these packages if you don't use any of their symbols.
  3545. - capt-of: for captions outside of floats
  3546. - hyperref: for cross references
  3547. Therefore you should not modify this variable unless you know
  3548. what you are doing. The one reason to change it anyway is that
  3549. you might be loading some other package that conflicts with one
  3550. of the default packages. Each element is either a cell or
  3551. a string.
  3552. A cell is of the format
  3553. \(\"options\" \"package\" SNIPPET-FLAG)
  3554. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3555. when compiling LaTeX snippets into images for inclusion into
  3556. non-LaTeX output.
  3557. A string will be inserted as-is in the header of the document."
  3558. :group 'org-latex
  3559. :group 'org-export-latex
  3560. :set 'org-set-packages-alist
  3561. :get 'org-get-packages-alist
  3562. :version "25.1"
  3563. :package-version '(Org . "8.3")
  3564. :type '(repeat
  3565. (choice
  3566. (list :tag "options/package pair"
  3567. (string :tag "options")
  3568. (string :tag "package")
  3569. (boolean :tag "Snippet"))
  3570. (string :tag "A line of LaTeX"))))
  3571. (defcustom org-latex-packages-alist nil
  3572. "Alist of packages to be inserted in every LaTeX header.
  3573. These will be inserted after `org-latex-default-packages-alist'.
  3574. Each element is either a cell or a string.
  3575. A cell is of the format:
  3576. \(\"options\" \"package\" SNIPPET-FLAG)
  3577. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3578. needed when turning LaTeX snippets into images for inclusion into
  3579. non-LaTeX output.
  3580. A string will be inserted as-is in the header of the document.
  3581. Make sure that you only list packages here which:
  3582. - you want in every file;
  3583. - do not conflict with the setup in `org-format-latex-header';
  3584. - do not conflict with the default packages in
  3585. `org-latex-default-packages-alist'."
  3586. :group 'org-latex
  3587. :group 'org-export-latex
  3588. :set 'org-set-packages-alist
  3589. :get 'org-get-packages-alist
  3590. :type '(repeat
  3591. (choice
  3592. (list :tag "options/package pair"
  3593. (string :tag "options")
  3594. (string :tag "package")
  3595. (boolean :tag "Snippet"))
  3596. (string :tag "A line of LaTeX"))))
  3597. (defgroup org-appearance nil
  3598. "Settings for Org-mode appearance."
  3599. :tag "Org Appearance"
  3600. :group 'org)
  3601. (defcustom org-level-color-stars-only nil
  3602. "Non-nil means fontify only the stars in each headline.
  3603. When nil, the entire headline is fontified.
  3604. Changing it requires restart of `font-lock-mode' to become effective
  3605. also in regions already fontified."
  3606. :group 'org-appearance
  3607. :type 'boolean)
  3608. (defcustom org-hide-leading-stars nil
  3609. "Non-nil means hide the first N-1 stars in a headline.
  3610. This works by using the face `org-hide' for these stars. This
  3611. face is white for a light background, and black for a dark
  3612. background. You may have to customize the face `org-hide' to
  3613. make this work.
  3614. Changing it requires restart of `font-lock-mode' to become effective
  3615. also in regions already fontified.
  3616. You may also set this on a per-file basis by adding one of the following
  3617. lines to the buffer:
  3618. #+STARTUP: hidestars
  3619. #+STARTUP: showstars"
  3620. :group 'org-appearance
  3621. :type 'boolean)
  3622. (defcustom org-hidden-keywords nil
  3623. "List of symbols corresponding to keywords to be hidden the org buffer.
  3624. For example, a value '(title) for this list will make the document's title
  3625. appear in the buffer without the initial #+TITLE: keyword."
  3626. :group 'org-appearance
  3627. :version "24.1"
  3628. :type '(set (const :tag "#+AUTHOR" author)
  3629. (const :tag "#+DATE" date)
  3630. (const :tag "#+EMAIL" email)
  3631. (const :tag "#+TITLE" title)))
  3632. (defcustom org-custom-properties nil
  3633. "List of properties (as strings) with a special meaning.
  3634. The default use of these custom properties is to let the user
  3635. hide them with `org-toggle-custom-properties-visibility'."
  3636. :group 'org-properties
  3637. :group 'org-appearance
  3638. :version "24.3"
  3639. :type '(repeat (string :tag "Property Name")))
  3640. (defcustom org-fontify-done-headline nil
  3641. "Non-nil means change the face of a headline if it is marked DONE.
  3642. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3643. When this is non-nil, the headline after the keyword is set to the
  3644. `org-headline-done' as an additional indication."
  3645. :group 'org-appearance
  3646. :type 'boolean)
  3647. (defcustom org-fontify-emphasized-text t
  3648. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3649. Changing this variable requires a restart of Emacs to take effect."
  3650. :group 'org-appearance
  3651. :type 'boolean)
  3652. (defcustom org-fontify-whole-heading-line nil
  3653. "Non-nil means fontify the whole line for headings.
  3654. This is useful when setting a background color for the
  3655. org-level-* faces."
  3656. :group 'org-appearance
  3657. :type 'boolean)
  3658. (defcustom org-highlight-latex-and-related nil
  3659. "Non-nil means highlight LaTeX related syntax in the buffer.
  3660. When non nil, the value should be a list containing any of the
  3661. following symbols:
  3662. `latex' Highlight LaTeX snippets and environments.
  3663. `script' Highlight subscript and superscript.
  3664. `entities' Highlight entities."
  3665. :group 'org-appearance
  3666. :version "24.4"
  3667. :package-version '(Org . "8.0")
  3668. :type '(choice
  3669. (const :tag "No highlighting" nil)
  3670. (set :greedy t :tag "Highlight"
  3671. (const :tag "LaTeX snippets and environments" latex)
  3672. (const :tag "Subscript and superscript" script)
  3673. (const :tag "Entities" entities))))
  3674. (defcustom org-hide-emphasis-markers nil
  3675. "Non-nil mean font-lock should hide the emphasis marker characters."
  3676. :group 'org-appearance
  3677. :type 'boolean)
  3678. (defcustom org-hide-macro-markers nil
  3679. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3680. :group 'org-appearance
  3681. :type 'boolean)
  3682. (defcustom org-pretty-entities nil
  3683. "Non-nil means show entities as UTF8 characters.
  3684. When nil, the \\name form remains in the buffer."
  3685. :group 'org-appearance
  3686. :version "24.1"
  3687. :type 'boolean)
  3688. (defcustom org-pretty-entities-include-sub-superscripts t
  3689. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3690. :group 'org-appearance
  3691. :version "24.1"
  3692. :type 'boolean)
  3693. (defvar org-emph-re nil
  3694. "Regular expression for matching emphasis.
  3695. After a match, the match groups contain these elements:
  3696. 0 The match of the full regular expression, including the characters
  3697. before and after the proper match
  3698. 1 The character before the proper match, or empty at beginning of line
  3699. 2 The proper match, including the leading and trailing markers
  3700. 3 The leading marker like * or /, indicating the type of highlighting
  3701. 4 The text between the emphasis markers, not including the markers
  3702. 5 The character after the match, empty at the end of a line")
  3703. (defvar org-verbatim-re nil
  3704. "Regular expression for matching verbatim text.")
  3705. (defvar org-emphasis-regexp-components) ; defined just below
  3706. (defvar org-emphasis-alist) ; defined just below
  3707. (defun org-set-emph-re (var val)
  3708. "Set variable and compute the emphasis regular expression."
  3709. (set var val)
  3710. (when (and (boundp 'org-emphasis-alist)
  3711. (boundp 'org-emphasis-regexp-components)
  3712. org-emphasis-alist org-emphasis-regexp-components)
  3713. (let* ((e org-emphasis-regexp-components)
  3714. (pre (car e))
  3715. (post (nth 1 e))
  3716. (border (nth 2 e))
  3717. (body (nth 3 e))
  3718. (nl (nth 4 e))
  3719. (body1 (concat body "*?"))
  3720. (markers (mapconcat 'car org-emphasis-alist ""))
  3721. (vmarkers (mapconcat
  3722. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3723. org-emphasis-alist "")))
  3724. ;; make sure special characters appear at the right position in the class
  3725. (if (string-match "\\^" markers)
  3726. (setq markers (concat (replace-match "" t t markers) "^")))
  3727. (if (string-match "-" markers)
  3728. (setq markers (concat (replace-match "" t t markers) "-")))
  3729. (if (string-match "\\^" vmarkers)
  3730. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3731. (if (string-match "-" vmarkers)
  3732. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3733. (if (> nl 0)
  3734. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3735. (int-to-string nl) "\\}")))
  3736. ;; Make the regexp
  3737. (setq org-emph-re
  3738. (concat "\\([" pre "]\\|^\\)"
  3739. "\\("
  3740. "\\([" markers "]\\)"
  3741. "\\("
  3742. "[^" border "]\\|"
  3743. "[^" border "]"
  3744. body1
  3745. "[^" border "]"
  3746. "\\)"
  3747. "\\3\\)"
  3748. "\\([" post "]\\|$\\)"))
  3749. (setq org-verbatim-re
  3750. (concat "\\([" pre "]\\|^\\)"
  3751. "\\("
  3752. "\\([" vmarkers "]\\)"
  3753. "\\("
  3754. "[^" border "]\\|"
  3755. "[^" border "]"
  3756. body1
  3757. "[^" border "]"
  3758. "\\)"
  3759. "\\3\\)"
  3760. "\\([" post "]\\|$\\)")))))
  3761. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3762. ;; set this option proved cumbersome. See this message/thread:
  3763. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3764. (defvar org-emphasis-regexp-components
  3765. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3766. "Components used to build the regular expression for emphasis.
  3767. This is a list with five entries. Terminology: In an emphasis string
  3768. like \" *strong word* \", we call the initial space PREMATCH, the final
  3769. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3770. and \"trong wor\" is the body. The different components in this variable
  3771. specify what is allowed/forbidden in each part:
  3772. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3773. post Chars allowed as postmatch. End of line will be allowed too.
  3774. border The chars *forbidden* as border characters.
  3775. body-regexp A regexp like \".\" to match a body character. Don't use
  3776. non-shy groups here, and don't allow newline here.
  3777. newline The maximum number of newlines allowed in an emphasis exp.
  3778. You need to reload Org or to restart Emacs after customizing this.")
  3779. (defcustom org-emphasis-alist
  3780. `(("*" bold)
  3781. ("/" italic)
  3782. ("_" underline)
  3783. ("=" org-verbatim verbatim)
  3784. ("~" org-code verbatim)
  3785. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3786. "Alist of characters and faces to emphasize text.
  3787. Text starting and ending with a special character will be emphasized,
  3788. for example *bold*, _underlined_ and /italic/. This variable sets the
  3789. marker characters and the face to be used by font-lock for highlighting
  3790. in Org-mode Emacs buffers.
  3791. You need to reload Org or to restart Emacs after customizing this."
  3792. :group 'org-appearance
  3793. :set 'org-set-emph-re
  3794. :version "24.4"
  3795. :package-version '(Org . "8.0")
  3796. :type '(repeat
  3797. (list
  3798. (string :tag "Marker character")
  3799. (choice
  3800. (face :tag "Font-lock-face")
  3801. (plist :tag "Face property list"))
  3802. (option (const verbatim)))))
  3803. (defvar org-protecting-blocks
  3804. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3805. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3806. This is needed for font-lock setup.")
  3807. ;;; Miscellaneous options
  3808. (defgroup org-completion nil
  3809. "Completion in Org-mode."
  3810. :tag "Org Completion"
  3811. :group 'org)
  3812. (defcustom org-completion-use-ido nil
  3813. "Non-nil means use ido completion wherever possible.
  3814. Note that `ido-mode' must be active for this variable to be relevant.
  3815. If you decide to turn this variable on, you might well want to turn off
  3816. `org-outline-path-complete-in-steps'.
  3817. See also `org-completion-use-iswitchb'."
  3818. :group 'org-completion
  3819. :type 'boolean)
  3820. (defcustom org-completion-use-iswitchb nil
  3821. "Non-nil means use iswitchb completion wherever possible.
  3822. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3823. If you decide to turn this variable on, you might well want to turn off
  3824. `org-outline-path-complete-in-steps'.
  3825. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3826. :group 'org-completion
  3827. :type 'boolean)
  3828. (defcustom org-completion-fallback-command 'hippie-expand
  3829. "The expansion command called by \\[pcomplete] in normal context.
  3830. Normal means, no org-mode-specific context."
  3831. :group 'org-completion
  3832. :type 'function)
  3833. ;;; Functions and variables from their packages
  3834. ;; Declared here to avoid compiler warnings
  3835. ;; XEmacs only
  3836. (defvar outline-mode-menu-heading)
  3837. (defvar outline-mode-menu-show)
  3838. (defvar outline-mode-menu-hide)
  3839. (defvar zmacs-regions) ; XEmacs regions
  3840. ;; Emacs only
  3841. (defvar mark-active)
  3842. ;; Various packages
  3843. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3844. (declare-function calendar-forward-day "cal-move" (arg))
  3845. (declare-function calendar-goto-date "cal-move" (date))
  3846. (declare-function calendar-goto-today "cal-move" ())
  3847. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3848. (defvar calc-embedded-close-formula)
  3849. (defvar calc-embedded-open-formula)
  3850. (declare-function cdlatex-tab "ext:cdlatex" ())
  3851. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3852. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3853. (defvar font-lock-unfontify-region-function)
  3854. (declare-function iswitchb-read-buffer "iswitchb"
  3855. (prompt &optional default require-match start matches-set))
  3856. (defvar iswitchb-temp-buflist)
  3857. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3858. (defvar org-agenda-tags-todo-honor-ignore-options)
  3859. (declare-function org-agenda-skip "org-agenda" ())
  3860. (declare-function
  3861. org-agenda-format-item "org-agenda"
  3862. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3863. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3864. (declare-function org-agenda-change-all-lines "org-agenda"
  3865. (newhead hdmarker &optional fixface just-this))
  3866. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3867. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3868. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3869. (beg end))
  3870. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3871. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3872. "org-agenda" (&optional end))
  3873. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3874. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3875. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3876. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3877. (declare-function org-indent-mode "org-indent" (&optional arg))
  3878. (declare-function parse-time-string "parse-time" (string))
  3879. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3880. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3881. (defvar remember-data-file)
  3882. (defvar texmathp-why)
  3883. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3884. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3885. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3886. ;;;###autoload
  3887. (defun turn-on-orgtbl ()
  3888. "Unconditionally turn on `orgtbl-mode'."
  3889. (require 'org-table)
  3890. (orgtbl-mode 1))
  3891. (defun org-at-table-p (&optional table-type)
  3892. "Non-nil if the cursor is inside an Org table.
  3893. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3894. If `org-enable-table-editor' is nil, return nil unconditionally."
  3895. (and
  3896. org-enable-table-editor
  3897. (save-excursion
  3898. (beginning-of-line)
  3899. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3900. (or (not (derived-mode-p 'org-mode))
  3901. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3902. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3903. (defsubst org-table-p () (org-at-table-p))
  3904. (defun org-at-table.el-p ()
  3905. "Non-nil when point is at a table.el table."
  3906. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3907. (let ((element (org-element-at-point)))
  3908. (and (eq (org-element-type element) 'table)
  3909. (eq (org-element-property :type element) 'table.el)))))
  3910. (defun org-table-recognize-table.el ()
  3911. "If there is a table.el table nearby, recognize it and move into it."
  3912. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3913. (beginning-of-line)
  3914. (unless (or (looking-at org-table-dataline-regexp)
  3915. (not (looking-at org-table1-hline-regexp)))
  3916. (forward-line)
  3917. (when (looking-at org-table-any-border-regexp)
  3918. (forward-line -2)))
  3919. (if (re-search-forward "|" (org-table-end t) t)
  3920. (progn
  3921. (require 'table)
  3922. (if (table--at-cell-p (point)) t
  3923. (message "recognizing table.el table...")
  3924. (table-recognize-table)
  3925. (message "recognizing table.el table...done")))
  3926. (error "This should not happen"))))
  3927. (defun org-at-table-hline-p ()
  3928. "Non-nil when point is inside a hline in a table.
  3929. Assume point is already in a table. If `org-enable-table-editor'
  3930. is nil, return nil unconditionally."
  3931. (and org-enable-table-editor
  3932. (save-excursion
  3933. (beginning-of-line)
  3934. (looking-at org-table-hline-regexp))))
  3935. (defun org-table-map-tables (function &optional quietly)
  3936. "Apply FUNCTION to the start of all tables in the buffer."
  3937. (save-excursion
  3938. (save-restriction
  3939. (widen)
  3940. (goto-char (point-min))
  3941. (while (re-search-forward org-table-any-line-regexp nil t)
  3942. (unless quietly
  3943. (message "Mapping tables: %d%%"
  3944. (floor (* 100.0 (point)) (buffer-size))))
  3945. (beginning-of-line 1)
  3946. (when (and (looking-at org-table-line-regexp)
  3947. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3948. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3949. (save-excursion (funcall function))
  3950. (or (looking-at org-table-line-regexp)
  3951. (forward-char 1)))
  3952. (re-search-forward org-table-any-border-regexp nil 1))))
  3953. (unless quietly (message "Mapping tables: done")))
  3954. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3955. (declare-function org-clock-update-mode-line "org-clock" ())
  3956. (declare-function org-resolve-clocks "org-clock"
  3957. (&optional also-non-dangling-p prompt last-valid))
  3958. (defun org-at-TBLFM-p (&optional pos)
  3959. "Non-nil when point (or POS) is in #+TBLFM line."
  3960. (save-excursion
  3961. (goto-char (or pos (point)))
  3962. (beginning-of-line)
  3963. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3964. (eq (org-element-type (org-element-at-point)) 'table))))
  3965. (defvar org-clock-start-time)
  3966. (defvar org-clock-marker (make-marker)
  3967. "Marker recording the last clock-in.")
  3968. (defvar org-clock-hd-marker (make-marker)
  3969. "Marker recording the last clock-in, but the headline position.")
  3970. (defvar org-clock-heading ""
  3971. "The heading of the current clock entry.")
  3972. (defun org-clock-is-active ()
  3973. "Return the buffer where the clock is currently running.
  3974. Return nil if no clock is running."
  3975. (marker-buffer org-clock-marker))
  3976. (defun org-check-running-clock ()
  3977. "Check if the current buffer contains the running clock.
  3978. If yes, offer to stop it and to save the buffer with the changes."
  3979. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3980. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3981. (buffer-name))))
  3982. (org-clock-out)
  3983. (when (y-or-n-p "Save changed buffer?")
  3984. (save-buffer))))
  3985. (defun org-clocktable-try-shift (dir n)
  3986. "Check if this line starts a clock table, if yes, shift the time block."
  3987. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3988. (org-clocktable-shift dir n)))
  3989. ;;;###autoload
  3990. (defun org-clock-persistence-insinuate ()
  3991. "Set up hooks for clock persistence."
  3992. (require 'org-clock)
  3993. (add-hook 'org-mode-hook 'org-clock-load)
  3994. (add-hook 'kill-emacs-hook 'org-clock-save))
  3995. (defgroup org-archive nil
  3996. "Options concerning archiving in Org-mode."
  3997. :tag "Org Archive"
  3998. :group 'org-structure)
  3999. (defcustom org-archive-location "%s_archive::"
  4000. "The location where subtrees should be archived.
  4001. The value of this variable is a string, consisting of two parts,
  4002. separated by a double-colon. The first part is a filename and
  4003. the second part is a headline.
  4004. When the filename is omitted, archiving happens in the same file.
  4005. %s in the filename will be replaced by the current file
  4006. name (without the directory part). Archiving to a different file
  4007. is useful to keep archived entries from contributing to the
  4008. Org-mode Agenda.
  4009. The archived entries will be filed as subtrees of the specified
  4010. headline. When the headline is omitted, the subtrees are simply
  4011. filed away at the end of the file, as top-level entries. Also in
  4012. the heading you can use %s to represent the file name, this can be
  4013. useful when using the same archive for a number of different files.
  4014. Here are a few examples:
  4015. \"%s_archive::\"
  4016. If the current file is Projects.org, archive in file
  4017. Projects.org_archive, as top-level trees. This is the default.
  4018. \"::* Archived Tasks\"
  4019. Archive in the current file, under the top-level headline
  4020. \"* Archived Tasks\".
  4021. \"~/org/archive.org::\"
  4022. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4023. \"~/org/archive.org::* From %s\"
  4024. Archive in file ~/org/archive.org (absolute path), under headlines
  4025. \"From FILENAME\" where file name is the current file name.
  4026. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4027. The \"datetree/\" string is special, signifying to archive
  4028. items to the datetree. Items are placed in either the CLOSED
  4029. date of the item, or the current date if there is no CLOSED date.
  4030. The heading will be a subentry to the current date. There doesn't
  4031. need to be a heading, but there always needs to be a slash after
  4032. datetree. For example, to store archived items directly in the
  4033. datetree, use \"~/org/datetree.org::datetree/\".
  4034. \"basement::** Finished Tasks\"
  4035. Archive in file ./basement (relative path), as level 3 trees
  4036. below the level 2 heading \"** Finished Tasks\".
  4037. You may set this option on a per-file basis by adding to the buffer a
  4038. line like
  4039. #+ARCHIVE: basement::** Finished Tasks
  4040. You may also define it locally for a subtree by setting an ARCHIVE property
  4041. in the entry. If such a property is found in an entry, or anywhere up
  4042. the hierarchy, it will be used."
  4043. :group 'org-archive
  4044. :type 'string)
  4045. (defcustom org-agenda-skip-archived-trees t
  4046. "Non-nil means the agenda will skip any items located in archived trees.
  4047. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4048. variable is no longer recommended, you should leave it at the value t.
  4049. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4050. :group 'org-archive
  4051. :group 'org-agenda-skip
  4052. :type 'boolean)
  4053. (defcustom org-columns-skip-archived-trees t
  4054. "Non-nil means ignore archived trees when creating column view."
  4055. :group 'org-archive
  4056. :group 'org-properties
  4057. :type 'boolean)
  4058. (defcustom org-cycle-open-archived-trees nil
  4059. "Non-nil means `org-cycle' will open archived trees.
  4060. An archived tree is a tree marked with the tag ARCHIVE.
  4061. When nil, archived trees will stay folded. You can still open them with
  4062. normal outline commands like `show-all', but not with the cycling commands."
  4063. :group 'org-archive
  4064. :group 'org-cycle
  4065. :type 'boolean)
  4066. (defcustom org-sparse-tree-open-archived-trees nil
  4067. "Non-nil means sparse tree construction shows matches in archived trees.
  4068. When nil, matches in these trees are highlighted, but the trees are kept in
  4069. collapsed state."
  4070. :group 'org-archive
  4071. :group 'org-sparse-trees
  4072. :type 'boolean)
  4073. (defcustom org-sparse-tree-default-date-type nil
  4074. "The default date type when building a sparse tree.
  4075. When this is nil, a date is a scheduled or a deadline timestamp.
  4076. Otherwise, these types are allowed:
  4077. all: all timestamps
  4078. active: only active timestamps (<...>)
  4079. inactive: only inactive timestamps ([...])
  4080. scheduled: only scheduled timestamps
  4081. deadline: only deadline timestamps"
  4082. :type '(choice (const :tag "Scheduled or deadline" nil)
  4083. (const :tag "All timestamps" all)
  4084. (const :tag "Only active timestamps" active)
  4085. (const :tag "Only inactive timestamps" inactive)
  4086. (const :tag "Only scheduled timestamps" scheduled)
  4087. (const :tag "Only deadline timestamps" deadline)
  4088. (const :tag "Only closed timestamps" closed))
  4089. :version "25.1"
  4090. :package-version '(Org . "8.3")
  4091. :group 'org-sparse-trees)
  4092. (defun org-cycle-hide-archived-subtrees (state)
  4093. "Re-hide all archived subtrees after a visibility state change."
  4094. (when (and (not org-cycle-open-archived-trees)
  4095. (not (memq state '(overview folded))))
  4096. (save-excursion
  4097. (let* ((globalp (memq state '(contents all)))
  4098. (beg (if globalp (point-min) (point)))
  4099. (end (if globalp (point-max) (org-end-of-subtree t))))
  4100. (org-hide-archived-subtrees beg end)
  4101. (goto-char beg)
  4102. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4103. (message "%s" (substitute-command-keys
  4104. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4105. (defun org-force-cycle-archived ()
  4106. "Cycle subtree even if it is archived."
  4107. (interactive)
  4108. (setq this-command 'org-cycle)
  4109. (let ((org-cycle-open-archived-trees t))
  4110. (call-interactively 'org-cycle)))
  4111. (defun org-hide-archived-subtrees (beg end)
  4112. "Re-hide all archived subtrees after a visibility state change."
  4113. (org-with-wide-buffer
  4114. (let ((case-fold-search nil)
  4115. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4116. (goto-char beg)
  4117. ;; Include headline point is currently on.
  4118. (beginning-of-line)
  4119. (while (and (< (point) end) (re-search-forward re end t))
  4120. (when (member org-archive-tag (org-get-tags))
  4121. (org-flag-subtree t)
  4122. (org-end-of-subtree t))))))
  4123. (declare-function outline-end-of-heading "outline" ())
  4124. (declare-function outline-flag-region "outline" (from to flag))
  4125. (defun org-flag-subtree (flag)
  4126. (save-excursion
  4127. (org-back-to-heading t)
  4128. (outline-end-of-heading)
  4129. (outline-flag-region (point)
  4130. (progn (org-end-of-subtree t) (point))
  4131. flag)))
  4132. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4133. ;; Declare Column View Code
  4134. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4135. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4136. (declare-function org-columns-compute "org-colview" (property))
  4137. ;; Declare ID code
  4138. (declare-function org-id-store-link "org-id")
  4139. (declare-function org-id-locations-load "org-id")
  4140. (declare-function org-id-locations-save "org-id")
  4141. (defvar org-id-track-globally)
  4142. ;;; Variables for pre-computed regular expressions, all buffer local
  4143. (defvar org-todo-regexp nil
  4144. "Matches any of the TODO state keywords.")
  4145. (make-variable-buffer-local 'org-todo-regexp)
  4146. (defvar org-not-done-regexp nil
  4147. "Matches any of the TODO state keywords except the last one.")
  4148. (make-variable-buffer-local 'org-not-done-regexp)
  4149. (defvar org-not-done-heading-regexp nil
  4150. "Matches a TODO headline that is not done.")
  4151. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4152. (defvar org-todo-line-regexp nil
  4153. "Matches a headline and puts TODO state into group 2 if present.")
  4154. (make-variable-buffer-local 'org-todo-line-regexp)
  4155. (defvar org-complex-heading-regexp nil
  4156. "Matches a headline and puts everything into groups:
  4157. group 1: the stars
  4158. group 2: The todo keyword, maybe
  4159. group 3: Priority cookie
  4160. group 4: True headline
  4161. group 5: Tags")
  4162. (make-variable-buffer-local 'org-complex-heading-regexp)
  4163. (defvar org-complex-heading-regexp-format nil
  4164. "Printf format to make regexp to match an exact headline.
  4165. This regexp will match the headline of any node which has the
  4166. exact headline text that is put into the format, but may have any
  4167. TODO state, priority and tags.")
  4168. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4169. (defvar org-todo-line-tags-regexp nil
  4170. "Matches a headline and puts TODO state into group 2 if present.
  4171. Also put tags into group 4 if tags are present.")
  4172. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4173. (defconst org-plain-time-of-day-regexp
  4174. (concat
  4175. "\\(\\<[012]?[0-9]"
  4176. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4177. "\\(--?"
  4178. "\\(\\<[012]?[0-9]"
  4179. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4180. "\\)?")
  4181. "Regular expression to match a plain time or time range.
  4182. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4183. groups carry important information:
  4184. 0 the full match
  4185. 1 the first time, range or not
  4186. 8 the second time, if it is a range.")
  4187. (defconst org-plain-time-extension-regexp
  4188. (concat
  4189. "\\(\\<[012]?[0-9]"
  4190. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4191. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4192. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4193. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4194. groups carry important information:
  4195. 0 the full match
  4196. 7 hours of duration
  4197. 9 minutes of duration")
  4198. (defconst org-stamp-time-of-day-regexp
  4199. (concat
  4200. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4201. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4202. "\\(--?"
  4203. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4204. "Regular expression to match a timestamp time or time range.
  4205. After a match, the following groups carry important information:
  4206. 0 the full match
  4207. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4208. 2 the first time, range or not
  4209. 4 the second time, if it is a range.")
  4210. (defconst org-startup-options
  4211. '(("fold" org-startup-folded t)
  4212. ("overview" org-startup-folded t)
  4213. ("nofold" org-startup-folded nil)
  4214. ("showall" org-startup-folded nil)
  4215. ("showeverything" org-startup-folded showeverything)
  4216. ("content" org-startup-folded content)
  4217. ("indent" org-startup-indented t)
  4218. ("noindent" org-startup-indented nil)
  4219. ("hidestars" org-hide-leading-stars t)
  4220. ("showstars" org-hide-leading-stars nil)
  4221. ("odd" org-odd-levels-only t)
  4222. ("oddeven" org-odd-levels-only nil)
  4223. ("align" org-startup-align-all-tables t)
  4224. ("noalign" org-startup-align-all-tables nil)
  4225. ("inlineimages" org-startup-with-inline-images t)
  4226. ("noinlineimages" org-startup-with-inline-images nil)
  4227. ("latexpreview" org-startup-with-latex-preview t)
  4228. ("nolatexpreview" org-startup-with-latex-preview nil)
  4229. ("customtime" org-display-custom-times t)
  4230. ("logdone" org-log-done time)
  4231. ("lognotedone" org-log-done note)
  4232. ("nologdone" org-log-done nil)
  4233. ("lognoteclock-out" org-log-note-clock-out t)
  4234. ("nolognoteclock-out" org-log-note-clock-out nil)
  4235. ("logrepeat" org-log-repeat state)
  4236. ("lognoterepeat" org-log-repeat note)
  4237. ("logdrawer" org-log-into-drawer t)
  4238. ("nologdrawer" org-log-into-drawer nil)
  4239. ("logstatesreversed" org-log-states-order-reversed t)
  4240. ("nologstatesreversed" org-log-states-order-reversed nil)
  4241. ("nologrepeat" org-log-repeat nil)
  4242. ("logreschedule" org-log-reschedule time)
  4243. ("lognotereschedule" org-log-reschedule note)
  4244. ("nologreschedule" org-log-reschedule nil)
  4245. ("logredeadline" org-log-redeadline time)
  4246. ("lognoteredeadline" org-log-redeadline note)
  4247. ("nologredeadline" org-log-redeadline nil)
  4248. ("logrefile" org-log-refile time)
  4249. ("lognoterefile" org-log-refile note)
  4250. ("nologrefile" org-log-refile nil)
  4251. ("fninline" org-footnote-define-inline t)
  4252. ("nofninline" org-footnote-define-inline nil)
  4253. ("fnlocal" org-footnote-section nil)
  4254. ("fnauto" org-footnote-auto-label t)
  4255. ("fnprompt" org-footnote-auto-label nil)
  4256. ("fnconfirm" org-footnote-auto-label confirm)
  4257. ("fnplain" org-footnote-auto-label plain)
  4258. ("fnadjust" org-footnote-auto-adjust t)
  4259. ("nofnadjust" org-footnote-auto-adjust nil)
  4260. ("constcgs" constants-unit-system cgs)
  4261. ("constSI" constants-unit-system SI)
  4262. ("noptag" org-tag-persistent-alist nil)
  4263. ("hideblocks" org-hide-block-startup t)
  4264. ("nohideblocks" org-hide-block-startup nil)
  4265. ("beamer" org-startup-with-beamer-mode t)
  4266. ("entitiespretty" org-pretty-entities t)
  4267. ("entitiesplain" org-pretty-entities nil))
  4268. "Variable associated with STARTUP options for org-mode.
  4269. Each element is a list of three items: the startup options (as written
  4270. in the #+STARTUP line), the corresponding variable, and the value to set
  4271. this variable to if the option is found. An optional forth element PUSH
  4272. means to push this value onto the list in the variable.")
  4273. (defcustom org-group-tags t
  4274. "When non-nil (the default), use group tags.
  4275. This can be turned on/off through `org-toggle-tags-groups'."
  4276. :group 'org-tags
  4277. :group 'org-startup
  4278. :type 'boolean)
  4279. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4280. (defun org-toggle-tags-groups ()
  4281. "Toggle support for group tags.
  4282. Support for group tags is controlled by the option
  4283. `org-group-tags', which is non-nil by default."
  4284. (interactive)
  4285. (setq org-group-tags (not org-group-tags))
  4286. (cond ((and (derived-mode-p 'org-agenda-mode)
  4287. org-group-tags)
  4288. (org-agenda-redo))
  4289. ((derived-mode-p 'org-mode)
  4290. (let ((org-inhibit-startup t)) (org-mode))))
  4291. (message "Groups tags support has been turned %s"
  4292. (if org-group-tags "on" "off")))
  4293. (defun org-set-regexps-and-options (&optional tags-only)
  4294. "Precompute regular expressions used in the current buffer.
  4295. When optional argument TAGS-ONLY is non-nil, only compute tags
  4296. related expressions."
  4297. (when (derived-mode-p 'org-mode)
  4298. (let ((alist (org--setup-collect-keywords
  4299. (org-make-options-regexp
  4300. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4301. (and (not tags-only)
  4302. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4303. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4304. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4305. (org--setup-process-tags
  4306. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4307. (unless tags-only
  4308. ;; File properties.
  4309. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4310. ;; Archive location.
  4311. (let ((archive (cdr (assq 'archive alist))))
  4312. (when archive (org-set-local 'org-archive-location archive)))
  4313. ;; Category.
  4314. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4315. (when cat
  4316. (org-set-local 'org-category (intern cat))
  4317. (org-set-local 'org-file-properties
  4318. (org--update-property-plist
  4319. "CATEGORY" cat org-file-properties))))
  4320. ;; Columns.
  4321. (let ((column (cdr (assq 'columns alist))))
  4322. (when column (org-set-local 'org-columns-default-format column)))
  4323. ;; Constants.
  4324. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4325. ;; Link abbreviations.
  4326. (let ((links (cdr (assq 'link alist))))
  4327. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4328. ;; Priorities.
  4329. (let ((priorities (cdr (assq 'priorities alist))))
  4330. (when priorities
  4331. (org-set-local 'org-highest-priority (nth 0 priorities))
  4332. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4333. (org-set-local 'org-default-priority (nth 2 priorities))))
  4334. ;; Scripts.
  4335. (let ((scripts (assq 'scripts alist)))
  4336. (when scripts
  4337. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4338. ;; Startup options.
  4339. (let ((startup (cdr (assq 'startup alist))))
  4340. (dolist (option startup)
  4341. (let ((entry (assoc-string option org-startup-options t)))
  4342. (when entry
  4343. (let ((var (nth 1 entry))
  4344. (val (nth 2 entry)))
  4345. (if (not (nth 3 entry)) (org-set-local var val)
  4346. (unless (listp (symbol-value var))
  4347. (org-set-local var nil))
  4348. (add-to-list var val)))))))
  4349. ;; TODO keywords.
  4350. (org-set-local 'org-todo-kwd-alist nil)
  4351. (org-set-local 'org-todo-key-alist nil)
  4352. (org-set-local 'org-todo-key-trigger nil)
  4353. (org-set-local 'org-todo-keywords-1 nil)
  4354. (org-set-local 'org-done-keywords nil)
  4355. (org-set-local 'org-todo-heads nil)
  4356. (org-set-local 'org-todo-sets nil)
  4357. (org-set-local 'org-todo-log-states nil)
  4358. (let ((todo-sequences
  4359. (or (nreverse (cdr (assq 'todo alist)))
  4360. (let ((d (default-value 'org-todo-keywords)))
  4361. (if (not (stringp (car d))) d
  4362. ;; XXX: Backward compatibility code.
  4363. (list (cons org-todo-interpretation d)))))))
  4364. (dolist (sequence todo-sequences)
  4365. (let* ((sequence (or (run-hook-with-args-until-success
  4366. 'org-todo-setup-filter-hook sequence)
  4367. sequence))
  4368. (sequence-type (car sequence))
  4369. (keywords (cdr sequence))
  4370. (sep (member "|" keywords))
  4371. names alist)
  4372. (dolist (k (remove "|" keywords))
  4373. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4374. k)
  4375. (error "Invalid TODO keyword %s" k))
  4376. (let ((name (match-string 1 k))
  4377. (key (match-string 2 k))
  4378. (log (org-extract-log-state-settings k)))
  4379. (push name names)
  4380. (push (cons name (and key (string-to-char key))) alist)
  4381. (when log (push log org-todo-log-states))))
  4382. (let* ((names (nreverse names))
  4383. (done (if sep (org-remove-keyword-keys (cdr sep))
  4384. (last names)))
  4385. (head (car names))
  4386. (tail (list sequence-type head (car done) (org-last done))))
  4387. (add-to-list 'org-todo-heads head 'append)
  4388. (push names org-todo-sets)
  4389. (setq org-done-keywords (append org-done-keywords done nil))
  4390. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4391. (setq org-todo-key-alist
  4392. (append org-todo-key-alist
  4393. (and alist
  4394. (append '((:startgroup))
  4395. (nreverse alist)
  4396. '((:endgroup))))))
  4397. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4398. (setq org-todo-sets (nreverse org-todo-sets)
  4399. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4400. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4401. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4402. ;; Compute the regular expressions and other local variables.
  4403. ;; Using `org-outline-regexp-bol' would complicate them much,
  4404. ;; because of the fixed white space at the end of that string.
  4405. (if (not org-done-keywords)
  4406. (setq org-done-keywords
  4407. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4408. (setq org-not-done-keywords
  4409. (org-delete-all org-done-keywords
  4410. (copy-sequence org-todo-keywords-1))
  4411. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4412. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4413. org-not-done-heading-regexp
  4414. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4415. org-todo-line-regexp
  4416. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4417. org-complex-heading-regexp
  4418. (concat "^\\(\\*+\\)"
  4419. "\\(?: +" org-todo-regexp "\\)?"
  4420. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4421. "\\(?: +\\(.*?\\)\\)??"
  4422. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4423. "[ \t]*$")
  4424. org-complex-heading-regexp-format
  4425. (concat "^\\(\\*+\\)"
  4426. "\\(?: +" org-todo-regexp "\\)?"
  4427. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4428. "\\(?: +"
  4429. ;; Stats cookies can be stuck to body.
  4430. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4431. "\\(%s\\)"
  4432. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4433. "\\)"
  4434. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4435. "[ \t]*$")
  4436. org-todo-line-tags-regexp
  4437. (concat "^\\(\\*+\\)"
  4438. "\\(?: +" org-todo-regexp "\\)?"
  4439. "\\(?: +\\(.*?\\)\\)??"
  4440. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4441. "[ \t]*$"))
  4442. (org-compute-latex-and-related-regexp)))))
  4443. (defun org--setup-collect-keywords (regexp &optional files alist)
  4444. "Return setup keywords values as an alist.
  4445. REGEXP matches a subset of setup keywords. FILES is a list of
  4446. file names already visited. It is used to avoid circular setup
  4447. files. ALIST, when non-nil, is the alist computed so far.
  4448. Return value contains the following keys: `archive', `category',
  4449. `columns', `constants', `filetags', `link', `priorities',
  4450. `property', `scripts', `startup', `tags' and `todo'."
  4451. (org-with-wide-buffer
  4452. (goto-char (point-min))
  4453. (let ((case-fold-search t))
  4454. (while (re-search-forward regexp nil t)
  4455. (let ((element (org-element-at-point)))
  4456. (when (eq (org-element-type element) 'keyword)
  4457. (let ((key (org-element-property :key element))
  4458. (value (org-element-property :value element)))
  4459. (cond
  4460. ((equal key "ARCHIVE")
  4461. (when (org-string-nw-p value)
  4462. (push (cons 'archive value) alist)))
  4463. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4464. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4465. ((equal key "CONSTANTS")
  4466. (let* ((constants (assq 'constants alist))
  4467. (store (cdr constants)))
  4468. (dolist (pair (org-split-string value))
  4469. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4470. pair)
  4471. (let* ((name (match-string 1 pair))
  4472. (value (match-string 2 pair))
  4473. (old (assoc name store)))
  4474. (if old (setcdr old value)
  4475. (push (cons name value) store)))))
  4476. (if constants (setcdr constants store)
  4477. (push (cons 'constants store) alist))))
  4478. ((equal key "FILETAGS")
  4479. (when (org-string-nw-p value)
  4480. (let ((old (assq 'filetags alist))
  4481. (new (apply #'nconc
  4482. (mapcar (lambda (x) (org-split-string x ":"))
  4483. (org-split-string value)))))
  4484. (if old (setcdr old (append new (cdr old)))
  4485. (push (cons 'filetags new) alist)))))
  4486. ((equal key "LINK")
  4487. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4488. (let ((links (assq 'link alist))
  4489. (pair (cons (org-match-string-no-properties 1 value)
  4490. (org-match-string-no-properties 2 value))))
  4491. (if links (push pair (cdr links))
  4492. (push (list 'link pair) alist)))))
  4493. ((equal key "OPTIONS")
  4494. (when (and (org-string-nw-p value)
  4495. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4496. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4497. ((equal key "PRIORITIES")
  4498. (push (cons 'priorities
  4499. (let ((prio (org-split-string value)))
  4500. (if (< (length prio) 3) '(?A ?C ?B)
  4501. (mapcar #'string-to-char prio))))
  4502. alist))
  4503. ((equal key "PROPERTY")
  4504. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4505. (let* ((property (assq 'property alist))
  4506. (value (org--update-property-plist
  4507. (org-match-string-no-properties 1 value)
  4508. (org-match-string-no-properties 2 value)
  4509. (cdr property))))
  4510. (if property (setcdr property value)
  4511. (push (cons 'property value) alist)))))
  4512. ((equal key "STARTUP")
  4513. (let ((startup (assq 'startup alist)))
  4514. (if startup
  4515. (setcdr startup
  4516. (append (cdr startup) (org-split-string value)))
  4517. (push (cons 'startup (org-split-string value)) alist))))
  4518. ((equal key "TAGS")
  4519. (let ((tag-cell (assq 'tags alist)))
  4520. (if tag-cell
  4521. (setcdr tag-cell
  4522. (append (cdr tag-cell)
  4523. '("\\n")
  4524. (org-split-string value)))
  4525. (push (cons 'tags (org-split-string value)) alist))))
  4526. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4527. (let ((todo (assq 'todo alist))
  4528. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4529. (org-split-string value))))
  4530. (if todo (push value (cdr todo))
  4531. (push (list 'todo value) alist))))
  4532. ((equal key "SETUPFILE")
  4533. (unless buffer-read-only ; Do not check in Gnus messages.
  4534. (let ((f (and (org-string-nw-p value)
  4535. (expand-file-name
  4536. (org-remove-double-quotes value)))))
  4537. (when (and f (file-readable-p f) (not (member f files)))
  4538. (with-temp-buffer
  4539. (insert-file-contents f)
  4540. (setq alist
  4541. ;; Fake Org mode to benefit from cache
  4542. ;; without recurring needlessly.
  4543. (let ((major-mode 'org-mode))
  4544. (org--setup-collect-keywords
  4545. regexp (cons f files) alist)))))))))))))))
  4546. alist)
  4547. (defun org--setup-process-tags (tags filetags)
  4548. "Precompute variables used for tags.
  4549. TAGS is a list of tags and tag group symbols, as strings.
  4550. FILETAGS is a list of tags, as strings."
  4551. ;; Process the file tags.
  4552. (org-set-local 'org-file-tags
  4553. (mapcar #'org-add-prop-inherited filetags))
  4554. ;; Provide default tags if no local tags are found.
  4555. (when (and (not tags) org-tag-alist)
  4556. (setq tags
  4557. (mapcar (lambda (tag)
  4558. (case (car tag)
  4559. (:startgroup "{")
  4560. (:endgroup "}")
  4561. (:startgrouptag "[")
  4562. (:endgrouptag "]")
  4563. (:grouptags ":")
  4564. (:newline "\\n")
  4565. (otherwise (concat (car tag)
  4566. (and (characterp (cdr tag))
  4567. (format "(%c)" (cdr tag)))))))
  4568. org-tag-alist)))
  4569. ;; Process the tags.
  4570. (org-set-local 'org-tag-groups-alist nil)
  4571. (org-set-local 'org-tag-alist nil)
  4572. (let (group-flag)
  4573. (while tags
  4574. (let ((e (car tags)))
  4575. (setq tags (cdr tags))
  4576. (cond
  4577. ((equal e "{")
  4578. (push '(:startgroup) org-tag-alist)
  4579. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4580. ((equal e "}")
  4581. (push '(:endgroup) org-tag-alist)
  4582. (setq group-flag nil))
  4583. ((equal e "[")
  4584. (push '(:startgrouptag) org-tag-alist)
  4585. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4586. ((equal e "]")
  4587. (push '(:endgrouptag) org-tag-alist)
  4588. (setq group-flag nil))
  4589. ((equal e ":")
  4590. (push '(:grouptags) org-tag-alist)
  4591. (setq group-flag 'append))
  4592. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4593. ((string-match
  4594. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4595. "\\|{.+?}\\)" ; regular expression
  4596. "\\(?:(\\(.\\))\\)?\\'")) e)
  4597. (let ((tag (match-string 1 e))
  4598. (key (and (match-beginning 2)
  4599. (string-to-char (match-string 2 e)))))
  4600. (cond ((eq group-flag 'append)
  4601. (setcar org-tag-groups-alist
  4602. (append (car org-tag-groups-alist) (list tag))))
  4603. (group-flag (push (list tag) org-tag-groups-alist)))
  4604. ;; Push all tags in groups, no matter if they already exist.
  4605. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4606. (push (cons tag key) org-tag-alist))))))))
  4607. (setq org-tag-alist (nreverse org-tag-alist)))
  4608. (defun org-file-contents (file &optional noerror)
  4609. "Return the contents of FILE, as a string."
  4610. (if (and file (file-readable-p file))
  4611. (with-temp-buffer
  4612. (insert-file-contents file)
  4613. (buffer-string))
  4614. (funcall (if noerror 'message 'error)
  4615. "Cannot read file \"%s\"%s"
  4616. file
  4617. (let ((from (buffer-file-name (buffer-base-buffer))))
  4618. (if from (concat " (referenced in file \"" from "\")") "")))))
  4619. (defun org-extract-log-state-settings (x)
  4620. "Extract the log state setting from a TODO keyword string.
  4621. This will extract info from a string like \"WAIT(w@/!)\"."
  4622. (let (kw key log1 log2)
  4623. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4624. (setq kw (match-string 1 x)
  4625. key (and (match-end 2) (match-string 2 x))
  4626. log1 (and (match-end 3) (match-string 3 x))
  4627. log2 (and (match-end 4) (match-string 4 x)))
  4628. (and (or log1 log2)
  4629. (list kw
  4630. (and log1 (if (equal log1 "!") 'time 'note))
  4631. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4632. (defun org-remove-keyword-keys (list)
  4633. "Remove a pair of parenthesis at the end of each string in LIST."
  4634. (mapcar (lambda (x)
  4635. (if (string-match "(.*)$" x)
  4636. (substring x 0 (match-beginning 0))
  4637. x))
  4638. list))
  4639. (defun org-assign-fast-keys (alist)
  4640. "Assign fast keys to a keyword-key alist.
  4641. Respect keys that are already there."
  4642. (let (new e (alt ?0))
  4643. (while (setq e (pop alist))
  4644. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4645. (cdr e)) ;; Key already assigned.
  4646. (push e new)
  4647. (let ((clist (string-to-list (downcase (car e))))
  4648. (used (append new alist)))
  4649. (when (= (car clist) ?@)
  4650. (pop clist))
  4651. (while (and clist (rassoc (car clist) used))
  4652. (pop clist))
  4653. (unless clist
  4654. (while (rassoc alt used)
  4655. (incf alt)))
  4656. (push (cons (car e) (or (car clist) alt)) new))))
  4657. (nreverse new)))
  4658. ;;; Some variables used in various places
  4659. (defvar org-window-configuration nil
  4660. "Used in various places to store a window configuration.")
  4661. (defvar org-selected-window nil
  4662. "Used in various places to store a window configuration.")
  4663. (defvar org-finish-function nil
  4664. "Function to be called when `C-c C-c' is used.
  4665. This is for getting out of special buffers like capture.")
  4666. ;; FIXME: Occasionally check by commenting these, to make sure
  4667. ;; no other functions uses these, forgetting to let-bind them.
  4668. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4669. (defvar org-last-state)
  4670. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4671. ;; Defined somewhere in this file, but used before definition.
  4672. (defvar org-entities) ;; defined in org-entities.el
  4673. (defvar org-struct-menu)
  4674. (defvar org-org-menu)
  4675. (defvar org-tbl-menu)
  4676. ;;;; Define the Org-mode
  4677. ;; We use a before-change function to check if a table might need
  4678. ;; an update.
  4679. (defvar org-table-may-need-update t
  4680. "Indicates that a table might need an update.
  4681. This variable is set by `org-before-change-function'.
  4682. `org-table-align' sets it back to nil.")
  4683. (defun org-before-change-function (beg end)
  4684. "Every change indicates that a table might need an update."
  4685. (setq org-table-may-need-update t))
  4686. (defvar org-mode-map)
  4687. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4688. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4689. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4690. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4691. (defvar org-table-buffer-is-an nil)
  4692. (defvar bidi-paragraph-direction)
  4693. (defvar buffer-face-mode-face)
  4694. (require 'outline)
  4695. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4696. (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"))
  4697. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4698. ;; Other stuff we need.
  4699. (require 'time-date)
  4700. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4701. (require 'easymenu)
  4702. (autoload 'easy-menu-add "easymenu")
  4703. (require 'overlay)
  4704. ;; (require 'org-macs) moved higher up in the file before it is first used
  4705. (require 'org-entities)
  4706. ;; (require 'org-compat) moved higher up in the file before it is first used
  4707. (require 'org-faces)
  4708. (require 'org-list)
  4709. (require 'org-pcomplete)
  4710. (require 'org-src)
  4711. (require 'org-footnote)
  4712. (require 'org-macro)
  4713. ;; babel
  4714. (require 'ob)
  4715. ;;;###autoload
  4716. (define-derived-mode org-mode outline-mode "Org"
  4717. "Outline-based notes management and organizer, alias
  4718. \"Carsten's outline-mode for keeping track of everything.\"
  4719. Org-mode develops organizational tasks around a NOTES file which
  4720. contains information about projects as plain text. Org-mode is
  4721. implemented on top of outline-mode, which is ideal to keep the content
  4722. of large files well structured. It supports ToDo items, deadlines and
  4723. time stamps, which magically appear in the diary listing of the Emacs
  4724. calendar. Tables are easily created with a built-in table editor.
  4725. Plain text URL-like links connect to websites, emails (VM), Usenet
  4726. messages (Gnus), BBDB entries, and any files related to the project.
  4727. For printing and sharing of notes, an Org-mode file (or a part of it)
  4728. can be exported as a structured ASCII or HTML file.
  4729. The following commands are available:
  4730. \\{org-mode-map}"
  4731. ;; Get rid of Outline menus, they are not needed
  4732. ;; Need to do this here because define-derived-mode sets up
  4733. ;; the keymap so late. Still, it is a waste to call this each time
  4734. ;; we switch another buffer into org-mode.
  4735. (if (featurep 'xemacs)
  4736. (when (boundp 'outline-mode-menu-heading)
  4737. ;; Assume this is Greg's port, it uses easymenu
  4738. (easy-menu-remove outline-mode-menu-heading)
  4739. (easy-menu-remove outline-mode-menu-show)
  4740. (easy-menu-remove outline-mode-menu-hide))
  4741. (define-key org-mode-map [menu-bar headings] 'undefined)
  4742. (define-key org-mode-map [menu-bar hide] 'undefined)
  4743. (define-key org-mode-map [menu-bar show] 'undefined))
  4744. (org-load-modules-maybe)
  4745. (when (featurep 'xemacs)
  4746. (easy-menu-add org-org-menu)
  4747. (easy-menu-add org-tbl-menu))
  4748. (org-install-agenda-files-menu)
  4749. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4750. (add-to-invisibility-spec '(org-cwidth))
  4751. (add-to-invisibility-spec '(org-hide-block . t))
  4752. (when (featurep 'xemacs)
  4753. (org-set-local 'line-move-ignore-invisible t))
  4754. (org-set-local 'outline-regexp org-outline-regexp)
  4755. (org-set-local 'outline-level 'org-outline-level)
  4756. (setq bidi-paragraph-direction 'left-to-right)
  4757. (when (and org-ellipsis
  4758. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4759. (fboundp 'make-glyph-code))
  4760. (unless org-display-table
  4761. (setq org-display-table (make-display-table)))
  4762. (set-display-table-slot
  4763. org-display-table 4
  4764. (vconcat (mapcar
  4765. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4766. org-ellipsis)))
  4767. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4768. (setq buffer-display-table org-display-table))
  4769. (org-set-regexps-and-options)
  4770. (org-set-font-lock-defaults)
  4771. (when (and org-tag-faces (not org-tags-special-faces-re))
  4772. ;; tag faces set outside customize.... force initialization.
  4773. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4774. ;; Calc embedded
  4775. (org-set-local 'calc-embedded-open-mode "# ")
  4776. ;; Modify a few syntax entries
  4777. (modify-syntax-entry ?@ "w")
  4778. (modify-syntax-entry ?\" "\"")
  4779. (modify-syntax-entry ?\\ "_")
  4780. (modify-syntax-entry ?~ "_")
  4781. (if org-startup-truncated (setq truncate-lines t))
  4782. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4783. (org-set-local 'font-lock-unfontify-region-function
  4784. 'org-unfontify-region)
  4785. ;; Activate before-change-function
  4786. (org-set-local 'org-table-may-need-update t)
  4787. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4788. 'local)
  4789. ;; Check for running clock before killing a buffer
  4790. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4791. ;; Initialize macros templates.
  4792. (org-macro-initialize-templates)
  4793. ;; Initialize radio targets.
  4794. (org-update-radio-target-regexp)
  4795. ;; Indentation.
  4796. (org-set-local 'indent-line-function 'org-indent-line)
  4797. (org-set-local 'indent-region-function 'org-indent-region)
  4798. ;; Filling and auto-filling.
  4799. (org-setup-filling)
  4800. ;; Comments.
  4801. (org-setup-comments-handling)
  4802. ;; Initialize cache.
  4803. (org-element-cache-reset)
  4804. ;; Beginning/end of defun
  4805. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4806. (org-set-local 'end-of-defun-function
  4807. (lambda ()
  4808. (if (not (org-at-heading-p))
  4809. (org-forward-element)
  4810. (org-forward-element)
  4811. (forward-char -1))))
  4812. ;; Next error for sparse trees
  4813. (org-set-local 'next-error-function 'org-occur-next-match)
  4814. ;; Make sure dependence stuff works reliably, even for users who set it
  4815. ;; too late :-(
  4816. (if org-enforce-todo-dependencies
  4817. (add-hook 'org-blocker-hook
  4818. 'org-block-todo-from-children-or-siblings-or-parent)
  4819. (remove-hook 'org-blocker-hook
  4820. 'org-block-todo-from-children-or-siblings-or-parent))
  4821. (if org-enforce-todo-checkbox-dependencies
  4822. (add-hook 'org-blocker-hook
  4823. 'org-block-todo-from-checkboxes)
  4824. (remove-hook 'org-blocker-hook
  4825. 'org-block-todo-from-checkboxes))
  4826. ;; Align options lines
  4827. (org-set-local
  4828. 'align-mode-rules-list
  4829. '((org-in-buffer-settings
  4830. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4831. (modes . '(org-mode)))))
  4832. ;; Imenu
  4833. (org-set-local 'imenu-create-index-function
  4834. 'org-imenu-get-tree)
  4835. ;; Make isearch reveal context
  4836. (if (or (featurep 'xemacs)
  4837. (not (boundp 'outline-isearch-open-invisible-function)))
  4838. ;; Emacs 21 and XEmacs make use of the hook
  4839. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4840. ;; Emacs 22 deals with this through a special variable
  4841. (org-set-local 'outline-isearch-open-invisible-function
  4842. (lambda (&rest ignore) (org-show-context 'isearch))))
  4843. ;; Setup the pcomplete hooks
  4844. (set (make-local-variable 'pcomplete-command-completion-function)
  4845. 'org-pcomplete-initial)
  4846. (set (make-local-variable 'pcomplete-command-name-function)
  4847. 'org-command-at-point)
  4848. (set (make-local-variable 'pcomplete-default-completion-function)
  4849. 'ignore)
  4850. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4851. 'org-parse-arguments)
  4852. (set (make-local-variable 'pcomplete-termination-string) "")
  4853. (when (>= emacs-major-version 23)
  4854. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4855. ;; If empty file that did not turn on org-mode automatically, make it to.
  4856. (if (and org-insert-mode-line-in-empty-file
  4857. (org-called-interactively-p 'any)
  4858. (= (point-min) (point-max)))
  4859. (insert "# -*- mode: org -*-\n\n"))
  4860. (unless org-inhibit-startup
  4861. (org-unmodified
  4862. (and org-startup-with-beamer-mode (org-beamer-mode))
  4863. (when org-startup-align-all-tables
  4864. (org-table-map-tables 'org-table-align 'quietly))
  4865. (when org-startup-with-inline-images
  4866. (org-display-inline-images))
  4867. (when org-startup-with-latex-preview
  4868. (org-toggle-latex-fragment))
  4869. (unless org-inhibit-startup-visibility-stuff
  4870. (org-set-startup-visibility))
  4871. (org-refresh-effort-properties)))
  4872. ;; Try to set org-hide correctly
  4873. (let ((foreground (org-find-invisible-foreground)))
  4874. (if foreground
  4875. (set-face-foreground 'org-hide foreground))))
  4876. ;; Update `customize-package-emacs-version-alist'
  4877. (add-to-list 'customize-package-emacs-version-alist
  4878. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4879. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4880. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4881. (defvar org-mode-transpose-word-syntax-table
  4882. (let ((st (make-syntax-table text-mode-syntax-table)))
  4883. (mapc (lambda(c) (modify-syntax-entry
  4884. (string-to-char (car c)) "w p" st))
  4885. org-emphasis-alist)
  4886. st))
  4887. (when (fboundp 'abbrev-table-put)
  4888. (abbrev-table-put org-mode-abbrev-table
  4889. :parents (list text-mode-abbrev-table)))
  4890. (defun org-find-invisible-foreground ()
  4891. (let ((candidates (remove
  4892. "unspecified-bg"
  4893. (nconc
  4894. (list (face-background 'default)
  4895. (face-background 'org-default))
  4896. (mapcar
  4897. (lambda (alist)
  4898. (when (boundp alist)
  4899. (cdr (assoc 'background-color (symbol-value alist)))))
  4900. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4901. (list (face-foreground 'org-hide))))))
  4902. (car (remove nil candidates))))
  4903. (defun org-current-time (&optional rounding-minutes past)
  4904. "Current time, possibly rounded to ROUNDING-MINUTES.
  4905. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4906. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4907. the rounding returns a past time."
  4908. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4909. (car org-time-stamp-rounding-minutes)))
  4910. (time (decode-time)) res)
  4911. (if (< r 1)
  4912. (current-time)
  4913. (setq res
  4914. (apply 'encode-time
  4915. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4916. (nthcdr 2 time))))
  4917. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4918. (seconds-to-time (- (org-float-time res) (* r 60)))
  4919. res))))
  4920. (defun org-today ()
  4921. "Return today date, considering `org-extend-today-until'."
  4922. (time-to-days
  4923. (time-subtract (current-time)
  4924. (list 0 (* 3600 org-extend-today-until) 0))))
  4925. ;;;; Font-Lock stuff, including the activators
  4926. (defvar org-mouse-map (make-sparse-keymap))
  4927. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4928. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4929. (when org-mouse-1-follows-link
  4930. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4931. (when org-tab-follows-link
  4932. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4933. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4934. (require 'font-lock)
  4935. (defconst org-non-link-chars "]\t\n\r<>")
  4936. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4937. "file+sys" "news" "shell" "elisp" "doi" "message"
  4938. "help"))
  4939. (defvar org-link-types-re nil
  4940. "Matches a link that has a url-like prefix like \"http:\"")
  4941. (defvar org-link-re-with-space nil
  4942. "Matches a link with spaces, optional angular brackets around it.")
  4943. (defvar org-link-re-with-space2 nil
  4944. "Matches a link with spaces, optional angular brackets around it.")
  4945. (defvar org-link-re-with-space3 nil
  4946. "Matches a link with spaces, only for internal part in bracket links.")
  4947. (defvar org-angle-link-re nil
  4948. "Matches link with angular brackets, spaces are allowed.")
  4949. (defvar org-plain-link-re nil
  4950. "Matches plain link, without spaces.")
  4951. (defvar org-bracket-link-regexp nil
  4952. "Matches a link in double brackets.")
  4953. (defvar org-bracket-link-analytic-regexp nil
  4954. "Regular expression used to analyze links.
  4955. Here is what the match groups contain after a match:
  4956. 1: http:
  4957. 2: http
  4958. 3: path
  4959. 4: [desc]
  4960. 5: desc")
  4961. (defvar org-bracket-link-analytic-regexp++ nil
  4962. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4963. (defvar org-any-link-re nil
  4964. "Regular expression matching any link.")
  4965. (defconst org-match-sexp-depth 3
  4966. "Number of stacked braces for sub/superscript matching.")
  4967. (defun org-create-multibrace-regexp (left right n)
  4968. "Create a regular expression which will match a balanced sexp.
  4969. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4970. as single character strings.
  4971. The regexp returned will match the entire expression including the
  4972. delimiters. It will also define a single group which contains the
  4973. match except for the outermost delimiters. The maximum depth of
  4974. stacked delimiters is N. Escaping delimiters is not possible."
  4975. (let* ((nothing (concat "[^" left right "]*?"))
  4976. (or "\\|")
  4977. (re nothing)
  4978. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4979. (while (> n 1)
  4980. (setq n (1- n)
  4981. re (concat re or next)
  4982. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4983. (concat left "\\(" re "\\)" right)))
  4984. (defconst org-match-substring-regexp
  4985. (concat
  4986. "\\(\\S-\\)\\([_^]\\)\\("
  4987. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4988. "\\|"
  4989. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4990. "\\|"
  4991. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4992. "The regular expression matching a sub- or superscript.")
  4993. (defconst org-match-substring-with-braces-regexp
  4994. (concat
  4995. "\\(\\S-\\)\\([_^]\\)"
  4996. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4997. "The regular expression matching a sub- or superscript, forcing braces.")
  4998. (defun org-make-link-regexps ()
  4999. "Update the link regular expressions.
  5000. This should be called after the variable `org-link-types' has changed."
  5001. (let ((types-re (regexp-opt org-link-types t)))
  5002. (setq org-link-types-re
  5003. (concat "\\`" types-re ":")
  5004. org-link-re-with-space
  5005. (concat "<?" types-re ":"
  5006. "\\([^" org-non-link-chars " ]"
  5007. "[^" org-non-link-chars "]*"
  5008. "[^" org-non-link-chars " ]\\)>?")
  5009. org-link-re-with-space2
  5010. (concat "<?" types-re ":"
  5011. "\\([^" org-non-link-chars " ]"
  5012. "[^\t\n\r]*"
  5013. "[^" org-non-link-chars " ]\\)>?")
  5014. org-link-re-with-space3
  5015. (concat "<?" types-re ":"
  5016. "\\([^" org-non-link-chars " ]"
  5017. "[^\t\n\r]*\\)")
  5018. org-angle-link-re
  5019. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5020. types-re)
  5021. org-plain-link-re
  5022. (concat
  5023. "\\<" types-re ":"
  5024. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5025. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5026. org-bracket-link-regexp
  5027. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5028. org-bracket-link-analytic-regexp
  5029. (concat
  5030. "\\[\\["
  5031. "\\(" types-re ":\\)?"
  5032. "\\([^]]+\\)"
  5033. "\\]"
  5034. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5035. "\\]")
  5036. org-bracket-link-analytic-regexp++
  5037. (concat
  5038. "\\[\\["
  5039. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5040. "\\([^]]+\\)"
  5041. "\\]"
  5042. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5043. "\\]")
  5044. org-any-link-re
  5045. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5046. org-angle-link-re "\\)\\|\\("
  5047. org-plain-link-re "\\)"))))
  5048. (org-make-link-regexps)
  5049. (defvar org-emph-face nil)
  5050. (defun org-do-emphasis-faces (limit)
  5051. "Run through the buffer and emphasize strings."
  5052. (let (rtn a)
  5053. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5054. (let* ((border (char-after (match-beginning 3)))
  5055. (bre (regexp-quote (char-to-string border))))
  5056. (if (and (not (= border (char-after (match-beginning 4))))
  5057. (not (save-match-data
  5058. (string-match (concat bre ".*" bre)
  5059. (replace-regexp-in-string
  5060. "\n" " "
  5061. (substring (match-string 2) 1 -1))))))
  5062. (progn
  5063. (setq rtn t)
  5064. (setq a (assoc (match-string 3) org-emphasis-alist))
  5065. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5066. 'face
  5067. (nth 1 a))
  5068. (and (nth 2 a)
  5069. (org-remove-flyspell-overlays-in
  5070. (match-beginning 0) (match-end 0)))
  5071. (add-text-properties (match-beginning 2) (match-end 2)
  5072. '(font-lock-multiline t org-emphasis t))
  5073. (when org-hide-emphasis-markers
  5074. (add-text-properties (match-end 4) (match-beginning 5)
  5075. '(invisible org-link))
  5076. (add-text-properties (match-beginning 3) (match-end 3)
  5077. '(invisible org-link))))))
  5078. (goto-char (1+ (match-beginning 0))))
  5079. rtn))
  5080. (defun org-emphasize (&optional char)
  5081. "Insert or change an emphasis, i.e. a font like bold or italic.
  5082. If there is an active region, change that region to a new emphasis.
  5083. If there is no region, just insert the marker characters and position
  5084. the cursor between them.
  5085. CHAR should be the marker character. If it is a space, it means to
  5086. remove the emphasis of the selected region.
  5087. If CHAR is not given (for example in an interactive call) it will be
  5088. prompted for."
  5089. (interactive)
  5090. (let ((erc org-emphasis-regexp-components)
  5091. (prompt "")
  5092. (string "") beg end move c s)
  5093. (if (org-region-active-p)
  5094. (setq beg (region-beginning) end (region-end)
  5095. string (buffer-substring beg end))
  5096. (setq move t))
  5097. (unless char
  5098. (message "Emphasis marker or tag: [%s]"
  5099. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5100. (setq char (read-char-exclusive)))
  5101. (if (equal char ?\ )
  5102. (setq s "" move nil)
  5103. (unless (assoc (char-to-string char) org-emphasis-alist)
  5104. (user-error "No such emphasis marker: \"%c\"" char))
  5105. (setq s (char-to-string char)))
  5106. (while (and (> (length string) 1)
  5107. (equal (substring string 0 1) (substring string -1))
  5108. (assoc (substring string 0 1) org-emphasis-alist))
  5109. (setq string (substring string 1 -1)))
  5110. (setq string (concat s string s))
  5111. (if beg (delete-region beg end))
  5112. (unless (or (bolp)
  5113. (string-match (concat "[" (nth 0 erc) "\n]")
  5114. (char-to-string (char-before (point)))))
  5115. (insert " "))
  5116. (unless (or (eobp)
  5117. (string-match (concat "[" (nth 1 erc) "\n]")
  5118. (char-to-string (char-after (point)))))
  5119. (insert " ") (backward-char 1))
  5120. (insert string)
  5121. (and move (backward-char 1))))
  5122. (defconst org-nonsticky-props
  5123. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5124. (defsubst org-rear-nonsticky-at (pos)
  5125. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5126. (defun org-activate-plain-links (limit)
  5127. "Add link properties for plain links."
  5128. (let (f hl)
  5129. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5130. (not (member 'org-tag
  5131. (get-text-property (1- (match-beginning 0)) 'face)))
  5132. (not (org-in-src-block-p)))
  5133. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5134. (setq f (get-text-property (match-beginning 0) 'face))
  5135. (setq hl (org-match-string-no-properties 0))
  5136. (if (or (eq f 'org-tag)
  5137. (and (listp f) (memq 'org-tag f)))
  5138. nil
  5139. (add-text-properties (match-beginning 0) (match-end 0)
  5140. (list 'mouse-face 'highlight
  5141. 'face 'org-link
  5142. 'htmlize-link `(:uri ,hl)
  5143. 'keymap org-mouse-map))
  5144. (org-rear-nonsticky-at (match-end 0)))
  5145. t)))
  5146. (defun org-activate-code (limit)
  5147. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5148. (progn
  5149. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5150. (remove-text-properties (match-beginning 0) (match-end 0)
  5151. '(display t invisible t intangible t))
  5152. t)))
  5153. (defcustom org-src-fontify-natively t
  5154. "When non-nil, fontify code in code blocks."
  5155. :type 'boolean
  5156. :version "24.4"
  5157. :package-version '(Org . "8.3")
  5158. :group 'org-appearance
  5159. :group 'org-babel)
  5160. (defcustom org-allow-promoting-top-level-subtree nil
  5161. "When non-nil, allow promoting a top level subtree.
  5162. The leading star of the top level headline will be replaced
  5163. by a #."
  5164. :type 'boolean
  5165. :version "24.1"
  5166. :group 'org-appearance)
  5167. (defun org-fontify-meta-lines-and-blocks (limit)
  5168. (condition-case nil
  5169. (org-fontify-meta-lines-and-blocks-1 limit)
  5170. (error (message "org-mode fontification error"))))
  5171. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5172. "Fontify #+ lines and blocks."
  5173. (let ((case-fold-search t))
  5174. (if (re-search-forward
  5175. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5176. limit t)
  5177. (let ((beg (match-beginning 0))
  5178. (block-start (match-end 0))
  5179. (block-end nil)
  5180. (lang (match-string 7))
  5181. (beg1 (line-beginning-position 2))
  5182. (dc1 (downcase (match-string 2)))
  5183. (dc3 (downcase (match-string 3)))
  5184. end end1 quoting block-type ovl)
  5185. (cond
  5186. ((and (match-end 4) (equal dc3 "+begin"))
  5187. ;; Truly a block
  5188. (setq block-type (downcase (match-string 5))
  5189. quoting (member block-type org-protecting-blocks))
  5190. (when (re-search-forward
  5191. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5192. nil t) ;; on purpose, we look further than LIMIT
  5193. (setq end (min (point-max) (match-end 0))
  5194. end1 (min (point-max) (1- (match-beginning 0))))
  5195. (setq block-end (match-beginning 0))
  5196. (when quoting
  5197. (org-remove-flyspell-overlays-in beg1 end1)
  5198. (remove-text-properties beg end
  5199. '(display t invisible t intangible t)))
  5200. (add-text-properties
  5201. beg end '(font-lock-fontified t font-lock-multiline t))
  5202. (add-text-properties beg beg1 '(face org-meta-line))
  5203. (org-remove-flyspell-overlays-in beg beg1)
  5204. (add-text-properties ; For end_src
  5205. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5206. (org-remove-flyspell-overlays-in end1 end)
  5207. (cond
  5208. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5209. (org-src-font-lock-fontify-block lang block-start block-end)
  5210. (add-text-properties beg1 block-end '(src-block t)))
  5211. (quoting
  5212. (add-text-properties beg1 (min (point-max) (1+ end1))
  5213. '(face org-block))) ; end of source block
  5214. ((not org-fontify-quote-and-verse-blocks))
  5215. ((string= block-type "quote")
  5216. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5217. ((string= block-type "verse")
  5218. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5219. (add-text-properties beg beg1 '(face org-block-begin-line))
  5220. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5221. '(face org-block-end-line))
  5222. t))
  5223. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5224. (org-remove-flyspell-overlays-in
  5225. (match-beginning 0)
  5226. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5227. (add-text-properties
  5228. beg (match-end 3)
  5229. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5230. '(font-lock-fontified t invisible t)
  5231. '(font-lock-fontified t face org-document-info-keyword)))
  5232. (add-text-properties
  5233. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5234. (if (string-equal dc1 "+title:")
  5235. '(font-lock-fontified t face org-document-title)
  5236. '(font-lock-fontified t face org-document-info))))
  5237. ((equal dc1 "+caption:")
  5238. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5239. (remove-text-properties (match-beginning 0) (match-end 0)
  5240. '(display t invisible t intangible t))
  5241. (add-text-properties (match-beginning 1) (match-end 3)
  5242. '(font-lock-fontified t face org-meta-line))
  5243. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5244. '(font-lock-fontified t face org-block))
  5245. t)
  5246. ((member dc3 '(" " ""))
  5247. (org-remove-flyspell-overlays-in beg (match-end 0))
  5248. (add-text-properties
  5249. beg (match-end 0)
  5250. '(font-lock-fontified t face font-lock-comment-face)))
  5251. (t ;; just any other in-buffer setting, but not indented
  5252. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5253. (remove-text-properties (match-beginning 0) (match-end 0)
  5254. '(display t invisible t intangible t))
  5255. (add-text-properties beg (match-end 0)
  5256. '(font-lock-fontified t face org-meta-line))
  5257. t))))))
  5258. (defun org-fontify-drawers (limit)
  5259. "Fontify drawers."
  5260. (when (re-search-forward org-drawer-regexp limit t)
  5261. (add-text-properties
  5262. (match-beginning 0) (match-end 0)
  5263. '(font-lock-fontified t face org-special-keyword))
  5264. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5265. t))
  5266. (defun org-fontify-macros (limit)
  5267. "Fontify macros."
  5268. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5269. (add-text-properties
  5270. (match-beginning 0) (match-end 0)
  5271. '(font-lock-fontified t face org-macro))
  5272. (when org-hide-macro-markers
  5273. (add-text-properties (match-end 2) (match-beginning 2)
  5274. '(invisible t))
  5275. (add-text-properties (match-beginning 1) (match-end 1)
  5276. '(invisible t)))
  5277. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5278. t))
  5279. (defun org-activate-angle-links (limit)
  5280. "Add text properties for angle links."
  5281. (if (and (re-search-forward org-angle-link-re limit t)
  5282. (not (org-in-src-block-p)))
  5283. (progn
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. (add-text-properties (match-beginning 0) (match-end 0)
  5286. (list 'mouse-face 'highlight
  5287. 'keymap org-mouse-map
  5288. 'font-lock-multiline t))
  5289. (org-rear-nonsticky-at (match-end 0))
  5290. t)))
  5291. (defun org-activate-footnote-links (limit)
  5292. "Add text properties for footnotes."
  5293. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5294. (when fn
  5295. (let* ((beg (nth 1 fn))
  5296. (end (nth 2 fn))
  5297. (label (car fn))
  5298. (referencep (/= (line-beginning-position) beg)))
  5299. (when (and referencep (nth 3 fn))
  5300. (save-excursion
  5301. (goto-char beg)
  5302. (search-forward (or label "fn:"))
  5303. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5304. (add-text-properties beg end
  5305. (list 'mouse-face 'highlight
  5306. 'keymap org-mouse-map
  5307. 'help-echo
  5308. (if referencep "Footnote reference"
  5309. "Footnote definition")
  5310. 'font-lock-fontified t
  5311. 'font-lock-multiline t
  5312. 'face 'org-footnote))))))
  5313. (defun org-activate-bracket-links (limit)
  5314. "Add text properties for bracketed links."
  5315. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5316. (not (org-in-src-block-p)))
  5317. (let* ((hl (org-match-string-no-properties 1))
  5318. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5319. (ip (org-maybe-intangible
  5320. (list 'invisible 'org-link
  5321. 'keymap org-mouse-map 'mouse-face 'highlight
  5322. 'font-lock-multiline t 'help-echo help
  5323. 'htmlize-link `(:uri ,hl))))
  5324. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5325. 'font-lock-multiline t 'help-echo help
  5326. 'htmlize-link `(:uri ,hl))))
  5327. ;; We need to remove the invisible property here. Table narrowing
  5328. ;; may have made some of this invisible.
  5329. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5330. (remove-text-properties (match-beginning 0) (match-end 0)
  5331. '(invisible nil))
  5332. (if (match-end 3)
  5333. (progn
  5334. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5335. (org-rear-nonsticky-at (match-beginning 3))
  5336. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5337. (org-rear-nonsticky-at (match-end 3))
  5338. (add-text-properties (match-end 3) (match-end 0) ip)
  5339. (org-rear-nonsticky-at (match-end 0)))
  5340. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5341. (org-rear-nonsticky-at (match-beginning 1))
  5342. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5343. (org-rear-nonsticky-at (match-end 1))
  5344. (add-text-properties (match-end 1) (match-end 0) ip)
  5345. (org-rear-nonsticky-at (match-end 0)))
  5346. t)))
  5347. (defun org-activate-dates (limit)
  5348. "Add text properties for dates."
  5349. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5350. (not (equal (char-before (match-beginning 0)) 91)))
  5351. (progn
  5352. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5353. (add-text-properties (match-beginning 0) (match-end 0)
  5354. (list 'mouse-face 'highlight
  5355. 'keymap org-mouse-map))
  5356. (org-rear-nonsticky-at (match-end 0))
  5357. (when org-display-custom-times
  5358. (if (match-end 3)
  5359. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5360. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5361. t)))
  5362. (defvar org-target-link-regexp nil
  5363. "Regular expression matching radio targets in plain text.")
  5364. (make-variable-buffer-local 'org-target-link-regexp)
  5365. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5366. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5367. border border border))
  5368. "Regular expression matching a link target.")
  5369. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5370. "Regular expression matching a radio target.")
  5371. (defconst org-any-target-regexp
  5372. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5373. "Regular expression matching any target.")
  5374. (defun org-activate-target-links (limit)
  5375. "Add text properties for target matches."
  5376. (when org-target-link-regexp
  5377. (let ((case-fold-search t))
  5378. (if (re-search-forward org-target-link-regexp limit t)
  5379. (progn
  5380. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5381. (add-text-properties (match-beginning 1) (match-end 1)
  5382. (list 'mouse-face 'highlight
  5383. 'keymap org-mouse-map
  5384. 'help-echo "Radio target link"
  5385. 'org-linked-text t))
  5386. (org-rear-nonsticky-at (match-end 1))
  5387. t)))))
  5388. (defun org-update-radio-target-regexp ()
  5389. "Find all radio targets in this file and update the regular expression.
  5390. Also refresh fontification if needed."
  5391. (interactive)
  5392. (let ((old-regexp org-target-link-regexp)
  5393. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5394. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5395. (targets
  5396. (org-with-wide-buffer
  5397. (goto-char (point-min))
  5398. (let (rtn)
  5399. (while (re-search-forward org-radio-target-regexp nil t)
  5400. ;; Make sure point is really within the object.
  5401. (backward-char)
  5402. (let ((obj (org-element-context)))
  5403. (when (eq (org-element-type obj) 'radio-target)
  5404. (add-to-list 'rtn (org-element-property :value obj)))))
  5405. rtn))))
  5406. (setq org-target-link-regexp
  5407. (and targets
  5408. (concat before-re
  5409. (mapconcat
  5410. (lambda (x)
  5411. (replace-regexp-in-string
  5412. " +" "\\s-+" (regexp-quote x) t t))
  5413. targets
  5414. "\\|")
  5415. after-re)))
  5416. (unless (equal old-regexp org-target-link-regexp)
  5417. ;; Clean-up cache.
  5418. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5419. ((not org-target-link-regexp) old-regexp)
  5420. (t
  5421. (concat before-re
  5422. (mapconcat
  5423. (lambda (re)
  5424. (substring re (length before-re)
  5425. (- (length after-re))))
  5426. (list old-regexp org-target-link-regexp)
  5427. "\\|")
  5428. after-re)))))
  5429. (org-with-wide-buffer
  5430. (goto-char (point-min))
  5431. (while (re-search-forward regexp nil t)
  5432. (org-element-cache-refresh (match-beginning 1)))))
  5433. ;; Re fontify buffer.
  5434. (when (memq 'radio org-highlight-links)
  5435. (org-restart-font-lock)))))
  5436. (defun org-hide-wide-columns (limit)
  5437. (let (s e)
  5438. (setq s (text-property-any (point) (or limit (point-max))
  5439. 'org-cwidth t))
  5440. (when s
  5441. (setq e (next-single-property-change s 'org-cwidth))
  5442. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5443. (goto-char e)
  5444. t)))
  5445. (defvar org-latex-and-related-regexp nil
  5446. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5447. (defun org-compute-latex-and-related-regexp ()
  5448. "Compute regular expression for LaTeX, entities and sub/superscript.
  5449. Result depends on variable `org-highlight-latex-and-related'."
  5450. (org-set-local
  5451. 'org-latex-and-related-regexp
  5452. (let* ((re-sub
  5453. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5454. ((eq org-use-sub-superscripts '{})
  5455. (list org-match-substring-with-braces-regexp))
  5456. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5457. (re-latex
  5458. (when (memq 'latex org-highlight-latex-and-related)
  5459. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5460. (delq nil
  5461. (mapcar (lambda (x)
  5462. (and (member (car x) matchers) (nth 1 x)))
  5463. org-latex-regexps)))))
  5464. (re-entities
  5465. (when (memq 'entities org-highlight-latex-and-related)
  5466. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5467. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5468. (defun org-do-latex-and-related (limit)
  5469. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5470. LIMIT bounds the search for syntax to highlight. Stop at first
  5471. highlighted object, if any. Return t if some highlighting was
  5472. done, nil otherwise."
  5473. (when (org-string-nw-p org-latex-and-related-regexp)
  5474. (catch 'found
  5475. (while (re-search-forward org-latex-and-related-regexp limit t)
  5476. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5477. 'face))
  5478. '(org-code org-verbatim underline))
  5479. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5480. '(?_ ?^))
  5481. 1
  5482. 0)))
  5483. (font-lock-prepend-text-property
  5484. (+ offset (match-beginning 0)) (match-end 0)
  5485. 'face 'org-latex-and-related)
  5486. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5487. '(font-lock-multiline t)))
  5488. (throw 'found t)))
  5489. nil)))
  5490. (defun org-restart-font-lock ()
  5491. "Restart `font-lock-mode', to force refontification."
  5492. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5493. (font-lock-mode -1)
  5494. (font-lock-mode 1)))
  5495. (defun org-activate-tags (limit)
  5496. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5497. (progn
  5498. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5499. (add-text-properties (match-beginning 1) (match-end 1)
  5500. (list 'mouse-face 'highlight
  5501. 'keymap org-mouse-map))
  5502. (org-rear-nonsticky-at (match-end 1))
  5503. t)))
  5504. (defun org-outline-level ()
  5505. "Compute the outline level of the heading at point.
  5506. If this is called at a normal headline, the level is the number
  5507. of stars. Use `org-reduced-level' to remove the effect of
  5508. `org-odd-levels'. Unlike to `org-current-level', this function
  5509. takes into consideration inlinetasks."
  5510. (org-with-wide-buffer
  5511. (end-of-line)
  5512. (if (re-search-backward org-outline-regexp-bol nil t)
  5513. (1- (- (match-end 0) (match-beginning 0)))
  5514. 0)))
  5515. (defvar org-font-lock-keywords nil)
  5516. (defsubst org-re-property (property &optional literal allow-null value)
  5517. "Return a regexp matching a PROPERTY line.
  5518. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5519. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5520. non-nil, match properties even without a value.
  5521. Match group 3 is set to the value when it exists. If there is no
  5522. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5523. With optional argument VALUE, match only property lines with
  5524. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5525. unless LITERAL is non-nil."
  5526. (concat
  5527. "^\\(?4:[ \t]*\\)"
  5528. (format "\\(?1::\\(?2:%s\\):\\)"
  5529. (if literal property (regexp-quote property)))
  5530. (cond (value
  5531. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5532. (if literal value (regexp-quote value))))
  5533. (allow-null
  5534. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5535. (t
  5536. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5537. (defconst org-property-re
  5538. (org-re-property "\\S-+" 'literal t)
  5539. "Regular expression matching a property line.
  5540. There are four matching groups:
  5541. 1: :PROPKEY: including the leading and trailing colon,
  5542. 2: PROPKEY without the leading and trailing colon,
  5543. 3: PROPVAL without leading or trailing spaces,
  5544. 4: the indentation of the current line,
  5545. 5: trailing whitespace.")
  5546. (defvar org-font-lock-hook nil
  5547. "Functions to be called for special font lock stuff.")
  5548. (defvar org-font-lock-set-keywords-hook nil
  5549. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5550. This is called after `org-font-lock-extra-keywords' is defined, but before
  5551. it is installed to be used by font lock. This can be useful if something
  5552. needs to be inserted at a specific position in the font-lock sequence.")
  5553. (defun org-font-lock-hook (limit)
  5554. "Run `org-font-lock-hook' within LIMIT."
  5555. (run-hook-with-args 'org-font-lock-hook limit))
  5556. (defun org-set-font-lock-defaults ()
  5557. "Set font lock defaults for the current buffer."
  5558. (let* ((em org-fontify-emphasized-text)
  5559. (lk org-highlight-links)
  5560. (org-font-lock-extra-keywords
  5561. (list
  5562. ;; Call the hook
  5563. '(org-font-lock-hook)
  5564. ;; Headlines
  5565. `(,(if org-fontify-whole-heading-line
  5566. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5567. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5568. (1 (org-get-level-face 1))
  5569. (2 (org-get-level-face 2))
  5570. (3 (org-get-level-face 3)))
  5571. ;; Table lines
  5572. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5573. (1 'org-table t))
  5574. ;; Table internals
  5575. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5576. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5577. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5578. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5579. ;; Drawers
  5580. '(org-fontify-drawers)
  5581. ;; Properties
  5582. (list org-property-re
  5583. '(1 'org-special-keyword t)
  5584. '(3 'org-property-value t))
  5585. ;; Links
  5586. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5587. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5588. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5589. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5590. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5591. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5592. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5593. ;; Targets.
  5594. (list org-any-target-regexp '(0 'org-target t))
  5595. ;; Diary sexps.
  5596. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5597. ;; Macro
  5598. '(org-fontify-macros)
  5599. '(org-hide-wide-columns (0 nil append))
  5600. ;; TODO keyword
  5601. (list (format org-heading-keyword-regexp-format
  5602. org-todo-regexp)
  5603. '(2 (org-get-todo-face 2) t))
  5604. ;; DONE
  5605. (if org-fontify-done-headline
  5606. (list (format org-heading-keyword-regexp-format
  5607. (concat
  5608. "\\(?:"
  5609. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5610. "\\)"))
  5611. '(2 'org-headline-done t))
  5612. nil)
  5613. ;; Priorities
  5614. '(org-font-lock-add-priority-faces)
  5615. ;; Tags
  5616. '(org-font-lock-add-tag-faces)
  5617. ;; Tags groups
  5618. (if (and org-group-tags org-tag-groups-alist)
  5619. (list (concat org-outline-regexp-bol ".+\\(:"
  5620. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5621. ":\\).*$")
  5622. '(1 'org-tag-group prepend)))
  5623. ;; Special keywords
  5624. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5625. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5626. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5628. ;; Emphasis
  5629. (if em
  5630. (if (featurep 'xemacs)
  5631. '(org-do-emphasis-faces (0 nil append))
  5632. '(org-do-emphasis-faces)))
  5633. ;; Checkboxes
  5634. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5635. 1 'org-checkbox prepend)
  5636. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5637. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5638. (0 (org-get-checkbox-statistics-face) t)))
  5639. ;; Description list items
  5640. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5641. 1 'org-list-dt prepend)
  5642. ;; ARCHIVEd headings
  5643. (list (concat
  5644. org-outline-regexp-bol
  5645. "\\(.*:" org-archive-tag ":.*\\)")
  5646. '(1 'org-archived prepend))
  5647. ;; Specials
  5648. '(org-do-latex-and-related)
  5649. '(org-fontify-entities)
  5650. '(org-raise-scripts)
  5651. ;; Code
  5652. '(org-activate-code (1 'org-code t))
  5653. ;; COMMENT
  5654. (list (format
  5655. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5656. org-todo-regexp
  5657. org-comment-string)
  5658. '(9 'org-special-keyword t))
  5659. ;; Blocks and meta lines
  5660. '(org-fontify-meta-lines-and-blocks))))
  5661. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5662. (run-hooks 'org-font-lock-set-keywords-hook)
  5663. ;; Now set the full font-lock-keywords
  5664. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5665. (org-set-local 'font-lock-defaults
  5666. '(org-font-lock-keywords t nil nil backward-paragraph))
  5667. (kill-local-variable 'font-lock-keywords) nil))
  5668. (defun org-toggle-pretty-entities ()
  5669. "Toggle the composition display of entities as UTF8 characters."
  5670. (interactive)
  5671. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5672. (org-restart-font-lock)
  5673. (if org-pretty-entities
  5674. (message "Entities are now displayed as UTF8 characters")
  5675. (save-restriction
  5676. (widen)
  5677. (org-decompose-region (point-min) (point-max))
  5678. (message "Entities are now displayed as plain text"))))
  5679. (defvar org-custom-properties-overlays nil
  5680. "List of overlays used for custom properties.")
  5681. (make-variable-buffer-local 'org-custom-properties-overlays)
  5682. (defun org-toggle-custom-properties-visibility ()
  5683. "Display or hide properties in `org-custom-properties'."
  5684. (interactive)
  5685. (if org-custom-properties-overlays
  5686. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5687. (setq org-custom-properties-overlays nil))
  5688. (when org-custom-properties
  5689. (org-with-wide-buffer
  5690. (goto-char (point-min))
  5691. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5692. (while (re-search-forward regexp nil t)
  5693. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5694. (when (and end (< (point) end))
  5695. ;; Hide first custom property in current drawer.
  5696. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5697. (overlay-put o 'invisible t)
  5698. (overlay-put o 'org-custom-property t)
  5699. (push o org-custom-properties-overlays))
  5700. ;; Hide additional custom properties in the same drawer.
  5701. (while (re-search-forward regexp end t)
  5702. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5703. (overlay-put o 'invisible t)
  5704. (overlay-put o 'org-custom-property t)
  5705. (push o org-custom-properties-overlays)))))
  5706. ;; Each entry is limited to a single property drawer.
  5707. (outline-next-heading)))))))
  5708. (defun org-fontify-entities (limit)
  5709. "Find an entity to fontify."
  5710. (let (ee)
  5711. (when org-pretty-entities
  5712. (catch 'match
  5713. ;; "\_ "-family is left out on purpose. Only the first one,
  5714. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5715. ;; confusing when adding a second white space character.
  5716. (while (re-search-forward
  5717. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5718. limit t)
  5719. (if (and (not (org-at-comment-p))
  5720. (setq ee (org-entity-get (match-string 1)))
  5721. (= (length (nth 6 ee)) 1))
  5722. (let*
  5723. ((end (if (equal (match-string 2) "{}")
  5724. (match-end 2)
  5725. (match-end 1))))
  5726. (add-text-properties
  5727. (match-beginning 0) end
  5728. (list 'font-lock-fontified t))
  5729. (compose-region (match-beginning 0) end
  5730. (nth 6 ee) nil)
  5731. (backward-char 1)
  5732. (throw 'match t))))
  5733. nil))))
  5734. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5735. "Fontify string S like in Org-mode."
  5736. (with-temp-buffer
  5737. (insert s)
  5738. (let ((org-odd-levels-only odd-levels))
  5739. (org-mode)
  5740. (font-lock-ensure)
  5741. (buffer-string))))
  5742. (defvar org-m nil)
  5743. (defvar org-l nil)
  5744. (defvar org-f nil)
  5745. (defun org-get-level-face (n)
  5746. "Get the right face for match N in font-lock matching of headlines."
  5747. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5748. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5749. (if org-cycle-level-faces
  5750. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5751. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5752. (cond
  5753. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5754. ((eq n 2) org-f)
  5755. (t (if org-level-color-stars-only nil org-f))))
  5756. (defun org-face-from-face-or-color (context inherit face-or-color)
  5757. "Create a face list that inherits INHERIT, but sets the foreground color.
  5758. When FACE-OR-COLOR is not a string, just return it."
  5759. (if (stringp face-or-color)
  5760. (list :inherit inherit
  5761. (cdr (assoc context org-faces-easy-properties))
  5762. face-or-color)
  5763. face-or-color))
  5764. (defun org-get-todo-face (kwd)
  5765. "Get the right face for a TODO keyword KWD.
  5766. If KWD is a number, get the corresponding match group."
  5767. (if (numberp kwd) (setq kwd (match-string kwd)))
  5768. (or (org-face-from-face-or-color
  5769. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5770. (and (member kwd org-done-keywords) 'org-done)
  5771. 'org-todo))
  5772. (defun org-get-priority-face (priority)
  5773. "Get the right face for PRIORITY.
  5774. PRIORITY is a character."
  5775. (or (org-face-from-face-or-color
  5776. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5777. 'org-priority))
  5778. (defun org-get-tag-face (tag)
  5779. "Get the right face for TAG.
  5780. If TAG is a number, get the corresponding match group."
  5781. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5782. (or (org-face-from-face-or-color
  5783. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5784. 'org-tag)))
  5785. (defun org-font-lock-add-priority-faces (limit)
  5786. "Add the special priority faces."
  5787. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5788. (add-text-properties
  5789. (match-beginning 1) (match-end 1)
  5790. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5791. 'font-lock-fontified t))))
  5792. (defun org-font-lock-add-tag-faces (limit)
  5793. "Add the special tag faces."
  5794. (when (and org-tag-faces org-tags-special-faces-re)
  5795. (while (re-search-forward org-tags-special-faces-re limit t)
  5796. (add-text-properties (match-beginning 1) (match-end 1)
  5797. (list 'face (org-get-tag-face 1)
  5798. 'font-lock-fontified t))
  5799. (backward-char 1))))
  5800. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5801. "Remove fontification and activation overlays from links."
  5802. (font-lock-default-unfontify-region beg end)
  5803. (let* ((buffer-undo-list t)
  5804. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5805. (inhibit-modification-hooks t)
  5806. deactivate-mark buffer-file-name buffer-file-truename)
  5807. (org-decompose-region beg end)
  5808. (remove-text-properties beg end
  5809. '(mouse-face t keymap t org-linked-text t
  5810. invisible t intangible t
  5811. org-emphasis t))
  5812. (org-remove-font-lock-display-properties beg end)))
  5813. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5814. ((raise 0.3) (height 0.7))
  5815. ((raise -0.5))
  5816. ((raise 0.5)))
  5817. "Display properties for showing superscripts and subscripts.")
  5818. (defun org-remove-font-lock-display-properties (beg end)
  5819. "Remove specific display properties that have been added by font lock.
  5820. The will remove the raise properties that are used to show superscripts
  5821. and subscripts."
  5822. (let (next prop)
  5823. (while (< beg end)
  5824. (setq next (next-single-property-change beg 'display nil end)
  5825. prop (get-text-property beg 'display))
  5826. (if (member prop org-script-display)
  5827. (put-text-property beg next 'display nil))
  5828. (setq beg next))))
  5829. (defun org-raise-scripts (limit)
  5830. "Add raise properties to sub/superscripts."
  5831. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5832. (if (re-search-forward
  5833. (if (eq org-use-sub-superscripts t)
  5834. org-match-substring-regexp
  5835. org-match-substring-with-braces-regexp)
  5836. limit t)
  5837. (let* ((pos (point)) table-p comment-p
  5838. (mpos (match-beginning 3))
  5839. (emph-p (get-text-property mpos 'org-emphasis))
  5840. (link-p (get-text-property mpos 'mouse-face))
  5841. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5842. (goto-char (point-at-bol))
  5843. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5844. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5845. (goto-char pos)
  5846. ;; Handle a_b^c
  5847. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5848. (if (or comment-p emph-p link-p keyw-p)
  5849. t
  5850. (put-text-property (match-beginning 3) (match-end 0)
  5851. 'display
  5852. (if (equal (char-after (match-beginning 2)) ?^)
  5853. (nth (if table-p 3 1) org-script-display)
  5854. (nth (if table-p 2 0) org-script-display)))
  5855. (add-text-properties (match-beginning 2) (match-end 2)
  5856. (list 'invisible t
  5857. 'org-dwidth t 'org-dwidth-n 1))
  5858. (if (and (eq (char-after (match-beginning 3)) ?{)
  5859. (eq (char-before (match-end 3)) ?}))
  5860. (progn
  5861. (add-text-properties
  5862. (match-beginning 3) (1+ (match-beginning 3))
  5863. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5864. (add-text-properties
  5865. (1- (match-end 3)) (match-end 3)
  5866. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5867. t)))))
  5868. ;;;; Visibility cycling, including org-goto and indirect buffer
  5869. ;;; Cycling
  5870. (defvar org-cycle-global-status nil)
  5871. (make-variable-buffer-local 'org-cycle-global-status)
  5872. (put 'org-cycle-global-status 'org-state t)
  5873. (defvar org-cycle-subtree-status nil)
  5874. (make-variable-buffer-local 'org-cycle-subtree-status)
  5875. (put 'org-cycle-subtree-status 'org-state t)
  5876. (defvar org-inlinetask-min-level)
  5877. (defun org-unlogged-message (&rest args)
  5878. "Display a message, but avoid logging it in the *Messages* buffer."
  5879. (let ((message-log-max nil))
  5880. (apply 'message args)))
  5881. ;;;###autoload
  5882. (defun org-cycle (&optional arg)
  5883. "TAB-action and visibility cycling for Org-mode.
  5884. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5885. is outline visibility cycling, but it also invokes other actions
  5886. in special contexts.
  5887. - When this function is called with a prefix argument, rotate the entire
  5888. buffer through 3 states (global cycling)
  5889. 1. OVERVIEW: Show only top-level headlines.
  5890. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5891. 3. SHOW ALL: Show everything.
  5892. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5893. determined by the variable `org-startup-folded', and by any VISIBILITY
  5894. properties in the buffer.
  5895. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5896. including any drawers.
  5897. - When inside a table, re-align the table and move to the next field.
  5898. - When point is at the beginning of a headline, rotate the subtree started
  5899. by this line through 3 different states (local cycling)
  5900. 1. FOLDED: Only the main headline is shown.
  5901. 2. CHILDREN: The main headline and the direct children are shown.
  5902. From this state, you can move to one of the children
  5903. and zoom in further.
  5904. 3. SUBTREE: Show the entire subtree, including body text.
  5905. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5906. - When point is at the beginning of an empty headline and the variable
  5907. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5908. of the headline by demoting and promoting it to likely levels. This
  5909. speeds up creation document structure by pressing TAB once or several
  5910. times right after creating a new headline.
  5911. - When there is a numeric prefix, go up to a heading with level ARG, do
  5912. a `show-subtree' and return to the previous cursor position. If ARG
  5913. is negative, go up that many levels.
  5914. - When point is not at the beginning of a headline, execute the global
  5915. binding for TAB, which is re-indenting the line. See the option
  5916. `org-cycle-emulate-tab' for details.
  5917. - Special case: if point is at the beginning of the buffer and there is
  5918. no headline in line 1, this function will act as if called with prefix arg
  5919. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5920. But only if also the variable `org-cycle-global-at-bob' is t."
  5921. (interactive "P")
  5922. (org-load-modules-maybe)
  5923. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5924. (and org-cycle-level-after-item/entry-creation
  5925. (or (org-cycle-level)
  5926. (org-cycle-item-indentation))))
  5927. (let* ((limit-level
  5928. (or org-cycle-max-level
  5929. (and (boundp 'org-inlinetask-min-level)
  5930. org-inlinetask-min-level
  5931. (1- org-inlinetask-min-level))))
  5932. (nstars (and limit-level
  5933. (if org-odd-levels-only
  5934. (and limit-level (1- (* limit-level 2)))
  5935. limit-level)))
  5936. (org-outline-regexp
  5937. (if (not (derived-mode-p 'org-mode))
  5938. outline-regexp
  5939. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5940. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5941. (not (looking-at org-outline-regexp))))
  5942. (org-cycle-hook
  5943. (if bob-special
  5944. (delq 'org-optimize-window-after-visibility-change
  5945. (copy-sequence org-cycle-hook))
  5946. org-cycle-hook))
  5947. (pos (point)))
  5948. (if (or bob-special (equal arg '(4)))
  5949. ;; special case: use global cycling
  5950. (setq arg t))
  5951. (cond
  5952. ((equal arg '(16))
  5953. (setq last-command 'dummy)
  5954. (org-set-startup-visibility)
  5955. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5956. ((equal arg '(64))
  5957. (show-all)
  5958. (org-unlogged-message "Entire buffer visible, including drawers"))
  5959. ;; Try cdlatex TAB completion
  5960. ((org-try-cdlatex-tab))
  5961. ;; Table: enter it or move to the next field.
  5962. ((org-at-table-p 'any)
  5963. (if (org-at-table.el-p)
  5964. (message "Use C-c ' to edit table.el tables")
  5965. (if arg (org-table-edit-field t)
  5966. (org-table-justify-field-maybe)
  5967. (call-interactively 'org-table-next-field))))
  5968. ((run-hook-with-args-until-success
  5969. 'org-tab-after-check-for-table-hook))
  5970. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5971. ((eq arg t) (org-cycle-internal-global))
  5972. ;; Drawers: delegate to `org-flag-drawer'.
  5973. ((save-excursion
  5974. (beginning-of-line 1)
  5975. (looking-at org-drawer-regexp))
  5976. (org-flag-drawer ; toggle block visibility
  5977. (not (get-char-property (match-end 0) 'invisible))))
  5978. ;; Show-subtree, ARG levels up from here.
  5979. ((integerp arg)
  5980. (save-excursion
  5981. (org-back-to-heading)
  5982. (outline-up-heading (if (< arg 0) (- arg)
  5983. (- (funcall outline-level) arg)))
  5984. (org-show-subtree)))
  5985. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5986. ((and (featurep 'org-inlinetask)
  5987. (org-inlinetask-at-task-p)
  5988. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5989. (org-inlinetask-toggle-visibility))
  5990. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5991. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5992. (save-excursion (move-beginning-of-line 1)
  5993. (looking-at org-outline-regexp)))
  5994. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5995. (org-cycle-internal-local))
  5996. ;; From there: TAB emulation and template completion.
  5997. (buffer-read-only (org-back-to-heading))
  5998. ((run-hook-with-args-until-success
  5999. 'org-tab-after-check-for-cycling-hook))
  6000. ((org-try-structure-completion))
  6001. ((run-hook-with-args-until-success
  6002. 'org-tab-before-tab-emulation-hook))
  6003. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6004. (or (not (bolp))
  6005. (not (looking-at org-outline-regexp))))
  6006. (call-interactively (global-key-binding "\t")))
  6007. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6008. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6009. (or (and (eq org-cycle-emulate-tab 'white)
  6010. (= (match-end 0) (point-at-eol)))
  6011. (and (eq org-cycle-emulate-tab 'whitestart)
  6012. (>= (match-end 0) pos))))
  6013. t
  6014. (eq org-cycle-emulate-tab t))
  6015. (call-interactively (global-key-binding "\t")))
  6016. (t (save-excursion
  6017. (org-back-to-heading)
  6018. (org-cycle)))))))
  6019. (defun org-cycle-internal-global ()
  6020. "Do the global cycling action."
  6021. ;; Hack to avoid display of messages for .org attachments in Gnus
  6022. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6023. (cond
  6024. ((and (eq last-command this-command)
  6025. (eq org-cycle-global-status 'overview))
  6026. ;; We just created the overview - now do table of contents
  6027. ;; This can be slow in very large buffers, so indicate action
  6028. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6029. (unless ga (org-unlogged-message "CONTENTS..."))
  6030. (org-content)
  6031. (unless ga (org-unlogged-message "CONTENTS...done"))
  6032. (setq org-cycle-global-status 'contents)
  6033. (run-hook-with-args 'org-cycle-hook 'contents))
  6034. ((and (eq last-command this-command)
  6035. (eq org-cycle-global-status 'contents))
  6036. ;; We just showed the table of contents - now show everything
  6037. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6038. (show-all)
  6039. (unless ga (org-unlogged-message "SHOW ALL"))
  6040. (setq org-cycle-global-status 'all)
  6041. (run-hook-with-args 'org-cycle-hook 'all))
  6042. (t
  6043. ;; Default action: go to overview
  6044. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6045. (org-overview)
  6046. (unless ga (org-unlogged-message "OVERVIEW"))
  6047. (setq org-cycle-global-status 'overview)
  6048. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6049. (defvar org-called-with-limited-levels nil
  6050. "Non-nil when `org-with-limited-levels' is currently active.")
  6051. (defun org-cycle-internal-local ()
  6052. "Do the local cycling action."
  6053. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6054. ;; First, determine end of headline (EOH), end of subtree or item
  6055. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6056. (save-excursion
  6057. (if (org-at-item-p)
  6058. (progn
  6059. (beginning-of-line)
  6060. (setq struct (org-list-struct))
  6061. (setq eoh (point-at-eol))
  6062. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6063. (setq has-children (org-list-has-child-p (point) struct)))
  6064. (org-back-to-heading)
  6065. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6066. (setq eos (save-excursion (org-end-of-subtree t t)
  6067. (when (bolp) (backward-char)) (point)))
  6068. (setq has-children
  6069. (or (save-excursion
  6070. (let ((level (funcall outline-level)))
  6071. (outline-next-heading)
  6072. (and (org-at-heading-p t)
  6073. (> (funcall outline-level) level))))
  6074. (save-excursion
  6075. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6076. ;; Determine end invisible part of buffer (EOL)
  6077. (beginning-of-line 2)
  6078. ;; XEmacs doesn't have `next-single-char-property-change'
  6079. (if (featurep 'xemacs)
  6080. (while (and (not (eobp)) ;; this is like `next-line'
  6081. (get-char-property (1- (point)) 'invisible))
  6082. (beginning-of-line 2))
  6083. (while (and (not (eobp)) ;; this is like `next-line'
  6084. (get-char-property (1- (point)) 'invisible))
  6085. (goto-char (next-single-char-property-change (point) 'invisible))
  6086. (and (eolp) (beginning-of-line 2))))
  6087. (setq eol (point)))
  6088. ;; Find out what to do next and set `this-command'
  6089. (cond
  6090. ((= eos eoh)
  6091. ;; Nothing is hidden behind this heading
  6092. (unless (org-before-first-heading-p)
  6093. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6094. (org-unlogged-message "EMPTY ENTRY")
  6095. (setq org-cycle-subtree-status nil)
  6096. (save-excursion
  6097. (goto-char eos)
  6098. (outline-next-heading)
  6099. (if (outline-invisible-p) (org-flag-heading nil))))
  6100. ((and (or (>= eol eos)
  6101. (not (string-match "\\S-" (buffer-substring eol eos))))
  6102. (or has-children
  6103. (not (setq children-skipped
  6104. org-cycle-skip-children-state-if-no-children))))
  6105. ;; Entire subtree is hidden in one line: children view
  6106. (unless (org-before-first-heading-p)
  6107. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6108. (if (org-at-item-p)
  6109. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6110. (org-show-entry)
  6111. (org-with-limited-levels (org-show-children))
  6112. ;; FIXME: This slows down the func way too much.
  6113. ;; How keep drawers hidden in subtree anyway?
  6114. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6115. ;; (org-cycle-hide-drawers 'subtree))
  6116. ;; Fold every list in subtree to top-level items.
  6117. (when (eq org-cycle-include-plain-lists 'integrate)
  6118. (save-excursion
  6119. (org-back-to-heading)
  6120. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6121. (beginning-of-line 1)
  6122. (let* ((struct (org-list-struct))
  6123. (prevs (org-list-prevs-alist struct))
  6124. (end (org-list-get-bottom-point struct)))
  6125. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6126. (org-list-get-all-items (point) struct prevs))
  6127. (goto-char (if (< end eos) end eos)))))))
  6128. (org-unlogged-message "CHILDREN")
  6129. (save-excursion
  6130. (goto-char eos)
  6131. (outline-next-heading)
  6132. (if (outline-invisible-p) (org-flag-heading nil)))
  6133. (setq org-cycle-subtree-status 'children)
  6134. (unless (org-before-first-heading-p)
  6135. (run-hook-with-args 'org-cycle-hook 'children)))
  6136. ((or children-skipped
  6137. (and (eq last-command this-command)
  6138. (eq org-cycle-subtree-status 'children)))
  6139. ;; We just showed the children, or no children are there,
  6140. ;; now show everything.
  6141. (unless (org-before-first-heading-p)
  6142. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6143. (outline-flag-region eoh eos nil)
  6144. (org-unlogged-message
  6145. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6146. (setq org-cycle-subtree-status 'subtree)
  6147. (unless (org-before-first-heading-p)
  6148. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6149. (t
  6150. ;; Default action: hide the subtree.
  6151. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6152. (outline-flag-region eoh eos t)
  6153. (org-unlogged-message "FOLDED")
  6154. (setq org-cycle-subtree-status 'folded)
  6155. (unless (org-before-first-heading-p)
  6156. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6157. ;;;###autoload
  6158. (defun org-global-cycle (&optional arg)
  6159. "Cycle the global visibility. For details see `org-cycle'.
  6160. With \\[universal-argument] prefix arg, switch to startup visibility.
  6161. With a numeric prefix, show all headlines up to that level."
  6162. (interactive "P")
  6163. (let ((org-cycle-include-plain-lists
  6164. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6165. (cond
  6166. ((integerp arg)
  6167. (show-all)
  6168. (hide-sublevels arg)
  6169. (setq org-cycle-global-status 'contents))
  6170. ((equal arg '(4))
  6171. (org-set-startup-visibility)
  6172. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6173. (t
  6174. (org-cycle '(4))))))
  6175. (defun org-set-startup-visibility ()
  6176. "Set the visibility required by startup options and properties."
  6177. (cond
  6178. ((eq org-startup-folded t)
  6179. (org-overview))
  6180. ((eq org-startup-folded 'content)
  6181. (org-content))
  6182. ((or (eq org-startup-folded 'showeverything)
  6183. (eq org-startup-folded nil))
  6184. (show-all)))
  6185. (unless (eq org-startup-folded 'showeverything)
  6186. (if org-hide-block-startup (org-hide-block-all))
  6187. (org-set-visibility-according-to-property 'no-cleanup)
  6188. (org-cycle-hide-archived-subtrees 'all)
  6189. (org-cycle-hide-drawers 'all)
  6190. (org-cycle-show-empty-lines t)))
  6191. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6192. "Switch subtree visibilities according to :VISIBILITY: property."
  6193. (interactive)
  6194. (let (org-show-entry-below)
  6195. (org-with-wide-buffer
  6196. (goto-char (point-min))
  6197. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6198. (if (not (org-at-property-p)) (outline-next-heading)
  6199. (let ((state (match-string 3)))
  6200. (save-excursion
  6201. (org-back-to-heading t)
  6202. (hide-subtree)
  6203. (org-reveal)
  6204. (cond
  6205. ((equal state "folded")
  6206. (hide-subtree))
  6207. ((equal state "children")
  6208. (org-show-hidden-entry)
  6209. (org-show-children))
  6210. ((equal state "content")
  6211. (save-excursion
  6212. (save-restriction
  6213. (org-narrow-to-subtree)
  6214. (org-content))))
  6215. ((member state '("all" "showall"))
  6216. (show-subtree)))))))
  6217. (unless no-cleanup
  6218. (org-cycle-hide-archived-subtrees 'all)
  6219. (org-cycle-hide-drawers 'all)
  6220. (org-cycle-show-empty-lines 'all)))))
  6221. ;; This function uses outline-regexp instead of the more fundamental
  6222. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6223. ;; buffers, where outline-regexp is needed.
  6224. (defun org-overview ()
  6225. "Switch to overview mode, showing only top-level headlines.
  6226. This shows all headlines with a level equal or greater than the level
  6227. of the first headline in the buffer. This is important, because if the
  6228. first headline is not level one, then (hide-sublevels 1) gives confusing
  6229. results."
  6230. (interactive)
  6231. (save-excursion
  6232. (let ((level
  6233. (save-excursion
  6234. (goto-char (point-min))
  6235. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6236. (progn
  6237. (goto-char (match-beginning 0))
  6238. (funcall outline-level))))))
  6239. (and level (hide-sublevels level)))))
  6240. (defun org-content (&optional arg)
  6241. "Show all headlines in the buffer, like a table of contents.
  6242. With numerical argument N, show content up to level N."
  6243. (interactive "P")
  6244. (org-overview)
  6245. (save-excursion
  6246. ;; Visit all headings and show their offspring
  6247. (and (integerp arg) (org-overview))
  6248. (goto-char (point-max))
  6249. (catch 'exit
  6250. (while (and (progn (condition-case nil
  6251. (outline-previous-visible-heading 1)
  6252. (error (goto-char (point-min))))
  6253. t)
  6254. (looking-at org-outline-regexp))
  6255. (if (integerp arg)
  6256. (org-show-children (1- arg))
  6257. (show-branches))
  6258. (if (bobp) (throw 'exit nil))))))
  6259. (defun org-optimize-window-after-visibility-change (state)
  6260. "Adjust the window after a change in outline visibility.
  6261. This function is the default value of the hook `org-cycle-hook'."
  6262. (when (get-buffer-window (current-buffer))
  6263. (cond
  6264. ((eq state 'content) nil)
  6265. ((eq state 'all) nil)
  6266. ((eq state 'folded) nil)
  6267. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6268. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6269. (defun org-remove-empty-overlays-at (pos)
  6270. "Remove outline overlays that do not contain non-white stuff."
  6271. (mapc
  6272. (lambda (o)
  6273. (and (eq 'outline (overlay-get o 'invisible))
  6274. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6275. (overlay-end o))))
  6276. (delete-overlay o)))
  6277. (overlays-at pos)))
  6278. (defun org-clean-visibility-after-subtree-move ()
  6279. "Fix visibility issues after moving a subtree."
  6280. ;; First, find a reasonable region to look at:
  6281. ;; Start two siblings above, end three below
  6282. (let* ((beg (save-excursion
  6283. (and (org-get-last-sibling)
  6284. (org-get-last-sibling))
  6285. (point)))
  6286. (end (save-excursion
  6287. (and (org-get-next-sibling)
  6288. (org-get-next-sibling)
  6289. (org-get-next-sibling))
  6290. (if (org-at-heading-p)
  6291. (point-at-eol)
  6292. (point))))
  6293. (level (looking-at "\\*+"))
  6294. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6295. (save-excursion
  6296. (save-restriction
  6297. (narrow-to-region beg end)
  6298. (when re
  6299. ;; Properly fold already folded siblings
  6300. (goto-char (point-min))
  6301. (while (re-search-forward re nil t)
  6302. (if (and (not (outline-invisible-p))
  6303. (save-excursion
  6304. (goto-char (point-at-eol)) (outline-invisible-p)))
  6305. (hide-entry))))
  6306. (org-cycle-show-empty-lines 'overview)
  6307. (org-cycle-hide-drawers 'overview)))))
  6308. (defun org-cycle-show-empty-lines (state)
  6309. "Show empty lines above all visible headlines.
  6310. The region to be covered depends on STATE when called through
  6311. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6312. entire buffer covered. Note that an empty line is only shown if there
  6313. are at least `org-cycle-separator-lines' empty lines before the headline."
  6314. (when (/= org-cycle-separator-lines 0)
  6315. (save-excursion
  6316. (let* ((n (abs org-cycle-separator-lines))
  6317. (re (cond
  6318. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6319. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6320. (t (let ((ns (number-to-string (- n 2))))
  6321. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6322. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6323. beg end)
  6324. (cond
  6325. ((memq state '(overview contents t))
  6326. (setq beg (point-min) end (point-max)))
  6327. ((memq state '(children folded))
  6328. (setq beg (point)
  6329. end (progn (org-end-of-subtree t t)
  6330. (line-beginning-position 2)))))
  6331. (when beg
  6332. (goto-char beg)
  6333. (while (re-search-forward re end t)
  6334. (unless (get-char-property (match-end 1) 'invisible)
  6335. (let ((e (match-end 1))
  6336. (b (if (>= org-cycle-separator-lines 0)
  6337. (match-beginning 1)
  6338. (save-excursion
  6339. (goto-char (match-beginning 0))
  6340. (skip-chars-backward " \t\n")
  6341. (line-end-position)))))
  6342. (outline-flag-region b e nil))))))))
  6343. ;; Never hide empty lines at the end of the file.
  6344. (save-excursion
  6345. (goto-char (point-max))
  6346. (outline-previous-heading)
  6347. (outline-end-of-heading)
  6348. (if (and (looking-at "[ \t\n]+")
  6349. (= (match-end 0) (point-max)))
  6350. (outline-flag-region (point) (match-end 0) nil))))
  6351. (defun org-show-empty-lines-in-parent ()
  6352. "Move to the parent and re-show empty lines before visible headlines."
  6353. (save-excursion
  6354. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6355. (org-cycle-show-empty-lines context))))
  6356. (defun org-files-list ()
  6357. "Return `org-agenda-files' list, plus all open org-mode files.
  6358. This is useful for operations that need to scan all of a user's
  6359. open and agenda-wise Org files."
  6360. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6361. (dolist (buf (buffer-list))
  6362. (with-current-buffer buf
  6363. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6364. (let ((file (expand-file-name (buffer-file-name))))
  6365. (unless (member file files)
  6366. (push file files))))))
  6367. files))
  6368. (defsubst org-entry-beginning-position ()
  6369. "Return the beginning position of the current entry."
  6370. (save-excursion (outline-back-to-heading t) (point)))
  6371. (defsubst org-entry-end-position ()
  6372. "Return the end position of the current entry."
  6373. (save-excursion (outline-next-heading) (point)))
  6374. (defun org-cycle-hide-drawers (state &optional exceptions)
  6375. "Re-hide all drawers after a visibility state change.
  6376. When non-nil, optional argument EXCEPTIONS is a list of strings
  6377. specifying which drawers should not be hidden."
  6378. (when (and (derived-mode-p 'org-mode)
  6379. (not (memq state '(overview folded contents))))
  6380. (save-excursion
  6381. (let* ((globalp (memq state '(contents all)))
  6382. (beg (if globalp (point-min) (point)))
  6383. (end (if globalp (point-max)
  6384. (if (eq state 'children)
  6385. (save-excursion (outline-next-heading) (point))
  6386. (org-end-of-subtree t)))))
  6387. (goto-char beg)
  6388. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6389. (unless (member-ignore-case (match-string 1) exceptions)
  6390. (let ((drawer (org-element-at-point)))
  6391. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6392. (org-flag-drawer t drawer)
  6393. ;; Make sure to skip drawer entirely or we might flag
  6394. ;; it another time when matching its ending line with
  6395. ;; `org-drawer-regexp'.
  6396. (goto-char (org-element-property :end drawer))))))))))
  6397. (defun org-flag-drawer (flag &optional element)
  6398. "When FLAG is non-nil, hide the drawer we are at.
  6399. Otherwise make it visible. When optional argument ELEMENT is
  6400. a parsed drawer, as returned by `org-element-at-point', hide or
  6401. show that drawer instead."
  6402. (when (save-excursion
  6403. (beginning-of-line)
  6404. (org-looking-at-p org-drawer-regexp))
  6405. (let ((drawer (or element (org-element-at-point))))
  6406. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6407. (let ((post (org-element-property :post-affiliated drawer)))
  6408. (save-excursion
  6409. (outline-flag-region
  6410. (progn (goto-char post) (line-end-position))
  6411. (progn (goto-char (org-element-property :end drawer))
  6412. (skip-chars-backward " \r\t\n")
  6413. (line-end-position))
  6414. flag))
  6415. ;; When the drawer is hidden away, make sure point lies in
  6416. ;; a visible part of the buffer.
  6417. (when (and flag (> (line-beginning-position) post))
  6418. (goto-char post)))))))
  6419. (defun org-subtree-end-visible-p ()
  6420. "Is the end of the current subtree visible?"
  6421. (pos-visible-in-window-p
  6422. (save-excursion (org-end-of-subtree t) (point))))
  6423. (defun org-first-headline-recenter ()
  6424. "Move cursor to the first headline and recenter the headline."
  6425. (goto-char (point-min))
  6426. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6427. (set-window-start (selected-window) (point-at-bol))))
  6428. ;;; Saving and restoring visibility
  6429. (defun org-outline-overlay-data (&optional use-markers)
  6430. "Return a list of the locations of all outline overlays.
  6431. These are overlays with the `invisible' property value `outline'.
  6432. The return value is a list of cons cells, with start and stop
  6433. positions for each overlay.
  6434. If USE-MARKERS is set, return the positions as markers."
  6435. (let (beg end)
  6436. (save-excursion
  6437. (save-restriction
  6438. (widen)
  6439. (delq nil
  6440. (mapcar (lambda (o)
  6441. (when (eq (overlay-get o 'invisible) 'outline)
  6442. (setq beg (overlay-start o)
  6443. end (overlay-end o))
  6444. (and beg end (> end beg)
  6445. (if use-markers
  6446. (cons (copy-marker beg)
  6447. (copy-marker end t))
  6448. (cons beg end)))))
  6449. (overlays-in (point-min) (point-max))))))))
  6450. (defun org-set-outline-overlay-data (data)
  6451. "Create visibility overlays for all positions in DATA.
  6452. DATA should have been made by `org-outline-overlay-data'."
  6453. (let (o)
  6454. (save-excursion
  6455. (save-restriction
  6456. (widen)
  6457. (show-all)
  6458. (mapc (lambda (c)
  6459. (outline-flag-region (car c) (cdr c) t))
  6460. data)))))
  6461. ;;; Folding of blocks
  6462. (defvar org-hide-block-overlays nil
  6463. "Overlays hiding blocks.")
  6464. (make-variable-buffer-local 'org-hide-block-overlays)
  6465. (defun org-block-map (function &optional start end)
  6466. "Call FUNCTION at the head of all source blocks in the current buffer.
  6467. Optional arguments START and END can be used to limit the range."
  6468. (let ((start (or start (point-min)))
  6469. (end (or end (point-max))))
  6470. (save-excursion
  6471. (goto-char start)
  6472. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6473. (save-excursion
  6474. (save-match-data
  6475. (goto-char (match-beginning 0))
  6476. (funcall function)))))))
  6477. (defun org-hide-block-toggle-all ()
  6478. "Toggle the visibility of all blocks in the current buffer."
  6479. (org-block-map 'org-hide-block-toggle))
  6480. (defun org-hide-block-all ()
  6481. "Fold all blocks in the current buffer."
  6482. (interactive)
  6483. (org-show-block-all)
  6484. (org-block-map 'org-hide-block-toggle-maybe))
  6485. (defun org-show-block-all ()
  6486. "Unfold all blocks in the current buffer."
  6487. (interactive)
  6488. (mapc 'delete-overlay org-hide-block-overlays)
  6489. (setq org-hide-block-overlays nil))
  6490. (defun org-hide-block-toggle-maybe ()
  6491. "Toggle visibility of block at point.
  6492. Unlike to `org-hide-block-toggle', this function does not throw
  6493. an error. Return a non-nil value when toggling is successful."
  6494. (interactive)
  6495. (ignore-errors (org-hide-block-toggle)))
  6496. (defun org-hide-block-toggle (&optional force)
  6497. "Toggle the visibility of the current block.
  6498. When optional argument FORCE is `off', make block visible. If it
  6499. is non-nil, hide it unconditionally. Throw an error when not at
  6500. a block. Return a non-nil value when toggling is successful."
  6501. (interactive)
  6502. (let ((element (org-element-at-point)))
  6503. (unless (memq (org-element-type element)
  6504. '(center-block comment-block dynamic-block example-block
  6505. export-block quote-block special-block
  6506. src-block verse-block))
  6507. (user-error "Not at a block"))
  6508. (let* ((start (save-excursion
  6509. (goto-char (org-element-property :post-affiliated element))
  6510. (line-end-position)))
  6511. (end (save-excursion
  6512. (goto-char (org-element-property :end element))
  6513. (skip-chars-backward " \r\t\n")
  6514. (line-end-position)))
  6515. (overlays (overlays-at start)))
  6516. (cond
  6517. ;; Do nothing when not before or at the block opening line or
  6518. ;; at the block closing line.
  6519. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6520. ((and (not (eq force 'off))
  6521. (not (memq t (mapcar
  6522. (lambda (o)
  6523. (eq (overlay-get o 'invisible) 'org-hide-block))
  6524. overlays))))
  6525. (let ((ov (make-overlay start end)))
  6526. (overlay-put ov 'invisible 'org-hide-block)
  6527. ;; Make the block accessible to `isearch'.
  6528. (overlay-put
  6529. ov 'isearch-open-invisible
  6530. (lambda (ov)
  6531. (when (memq ov org-hide-block-overlays)
  6532. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6533. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6534. (delete-overlay ov))))
  6535. (push ov org-hide-block-overlays)
  6536. ;; When the block is hidden away, make sure point is left in
  6537. ;; a visible part of the buffer.
  6538. (when (> (line-beginning-position) start)
  6539. (goto-char start)
  6540. (beginning-of-line))
  6541. ;; Signal successful toggling.
  6542. t))
  6543. ((or (not force) (eq force 'off))
  6544. (dolist (ov overlays t)
  6545. (when (memq ov org-hide-block-overlays)
  6546. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6547. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6548. (delete-overlay ov))))))))
  6549. ;; org-tab-after-check-for-cycling-hook
  6550. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6551. ;; Remove overlays when changing major mode
  6552. (add-hook 'org-mode-hook
  6553. (lambda () (org-add-hook 'change-major-mode-hook
  6554. 'org-show-block-all 'append 'local)))
  6555. ;;; Org-goto
  6556. (defvar org-goto-window-configuration nil)
  6557. (defvar org-goto-marker nil)
  6558. (defvar org-goto-map)
  6559. (defun org-goto-map ()
  6560. "Set the keymap `org-goto'."
  6561. (setq org-goto-map
  6562. (let ((map (make-sparse-keymap)))
  6563. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6564. mouse-drag-region universal-argument org-occur)))
  6565. (dolist (cmd cmds)
  6566. (substitute-key-definition cmd cmd map global-map)))
  6567. (suppress-keymap map)
  6568. (org-defkey map "\C-m" 'org-goto-ret)
  6569. (org-defkey map [(return)] 'org-goto-ret)
  6570. (org-defkey map [(left)] 'org-goto-left)
  6571. (org-defkey map [(right)] 'org-goto-right)
  6572. (org-defkey map [(control ?g)] 'org-goto-quit)
  6573. (org-defkey map "\C-i" 'org-cycle)
  6574. (org-defkey map [(tab)] 'org-cycle)
  6575. (org-defkey map [(down)] 'outline-next-visible-heading)
  6576. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6577. (if org-goto-auto-isearch
  6578. (if (fboundp 'define-key-after)
  6579. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6580. nil)
  6581. (org-defkey map "q" 'org-goto-quit)
  6582. (org-defkey map "n" 'outline-next-visible-heading)
  6583. (org-defkey map "p" 'outline-previous-visible-heading)
  6584. (org-defkey map "f" 'outline-forward-same-level)
  6585. (org-defkey map "b" 'outline-backward-same-level)
  6586. (org-defkey map "u" 'outline-up-heading))
  6587. (org-defkey map "/" 'org-occur)
  6588. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6589. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6590. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6591. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6592. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6593. map)))
  6594. (defconst org-goto-help
  6595. "Browse buffer copy, to find location or copy text.%s
  6596. RET=jump to location C-g=quit and return to previous location
  6597. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6598. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6599. (defun org-goto (&optional alternative-interface)
  6600. "Look up a different location in the current file, keeping current visibility.
  6601. When you want look-up or go to a different location in a
  6602. document, the fastest way is often to fold the entire buffer and
  6603. then dive into the tree. This method has the disadvantage, that
  6604. the previous location will be folded, which may not be what you
  6605. want.
  6606. This command works around this by showing a copy of the current
  6607. buffer in an indirect buffer, in overview mode. You can dive
  6608. into the tree in that copy, use org-occur and incremental search
  6609. to find a location. When pressing RET or `Q', the command
  6610. returns to the original buffer in which the visibility is still
  6611. unchanged. After RET it will also jump to the location selected
  6612. in the indirect buffer and expose the headline hierarchy above.
  6613. With a prefix argument, use the alternative interface: e.g. if
  6614. `org-goto-interface' is 'outline use 'outline-path-completion."
  6615. (interactive "P")
  6616. (org-goto-map)
  6617. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6618. (org-refile-use-outline-path t)
  6619. (org-refile-target-verify-function nil)
  6620. (interface
  6621. (if (not alternative-interface)
  6622. org-goto-interface
  6623. (if (eq org-goto-interface 'outline)
  6624. 'outline-path-completion
  6625. 'outline)))
  6626. (org-goto-start-pos (point))
  6627. (selected-point
  6628. (if (eq interface 'outline)
  6629. (car (org-get-location (current-buffer) org-goto-help))
  6630. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6631. (org-refile-check-position pa)
  6632. (nth 3 pa)))))
  6633. (if selected-point
  6634. (progn
  6635. (org-mark-ring-push org-goto-start-pos)
  6636. (goto-char selected-point)
  6637. (if (or (outline-invisible-p) (org-invisible-p2))
  6638. (org-show-context 'org-goto)))
  6639. (message "Quit"))))
  6640. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6641. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6642. (defvar org-goto-local-auto-isearch-map) ; defined below
  6643. (defun org-get-location (buf help)
  6644. "Let the user select a location in the Org-mode buffer BUF.
  6645. This function uses a recursive edit. It returns the selected position
  6646. or nil."
  6647. (org-no-popups
  6648. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6649. (isearch-hide-immediately nil)
  6650. (isearch-search-fun-function
  6651. (lambda () 'org-goto-local-search-headings))
  6652. (org-goto-selected-point org-goto-exit-command))
  6653. (save-excursion
  6654. (save-window-excursion
  6655. (delete-other-windows)
  6656. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6657. (org-pop-to-buffer-same-window
  6658. (condition-case nil
  6659. (make-indirect-buffer (current-buffer) "*org-goto*")
  6660. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6661. (with-output-to-temp-buffer "*Org Help*"
  6662. (princ (format help (if org-goto-auto-isearch
  6663. " Just type for auto-isearch."
  6664. " n/p/f/b/u to navigate, q to quit."))))
  6665. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6666. (setq buffer-read-only nil)
  6667. (let ((org-startup-truncated t)
  6668. (org-startup-folded nil)
  6669. (org-startup-align-all-tables nil))
  6670. (org-mode)
  6671. (org-overview))
  6672. (setq buffer-read-only t)
  6673. (if (and (boundp 'org-goto-start-pos)
  6674. (integer-or-marker-p org-goto-start-pos))
  6675. (progn (goto-char org-goto-start-pos)
  6676. (when (outline-invisible-p)
  6677. (org-show-set-visibility 'lineage)))
  6678. (goto-char (point-min)))
  6679. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6680. (message "Select location and press RET")
  6681. (use-local-map org-goto-map)
  6682. (recursive-edit)))
  6683. (kill-buffer "*org-goto*")
  6684. (cons org-goto-selected-point org-goto-exit-command))))
  6685. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6686. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6687. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6688. (if (fboundp 'isearch-other-control-char)
  6689. (progn
  6690. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6691. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6692. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6693. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6694. (define-key org-goto-local-auto-isearch-map [return] nil))
  6695. (defun org-goto-local-search-headings (string bound noerror)
  6696. "Search and make sure that any matches are in headlines."
  6697. (catch 'return
  6698. (while (if isearch-forward
  6699. (search-forward string bound noerror)
  6700. (search-backward string bound noerror))
  6701. (when (save-match-data
  6702. (and (save-excursion
  6703. (beginning-of-line)
  6704. (looking-at org-complex-heading-regexp))
  6705. (or (not (match-beginning 5))
  6706. (< (point) (match-beginning 5)))))
  6707. (throw 'return (point))))))
  6708. (defun org-goto-local-auto-isearch ()
  6709. "Start isearch."
  6710. (interactive)
  6711. (goto-char (point-min))
  6712. (let ((keys (this-command-keys)))
  6713. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6714. (isearch-mode t)
  6715. (isearch-process-search-char (string-to-char keys)))))
  6716. (defun org-goto-ret (&optional arg)
  6717. "Finish `org-goto' by going to the new location."
  6718. (interactive "P")
  6719. (setq org-goto-selected-point (point)
  6720. org-goto-exit-command 'return)
  6721. (throw 'exit nil))
  6722. (defun org-goto-left ()
  6723. "Finish `org-goto' by going to the new location."
  6724. (interactive)
  6725. (if (org-at-heading-p)
  6726. (progn
  6727. (beginning-of-line 1)
  6728. (setq org-goto-selected-point (point)
  6729. org-goto-exit-command 'left)
  6730. (throw 'exit nil))
  6731. (user-error "Not on a heading")))
  6732. (defun org-goto-right ()
  6733. "Finish `org-goto' by going to the new location."
  6734. (interactive)
  6735. (if (org-at-heading-p)
  6736. (progn
  6737. (setq org-goto-selected-point (point)
  6738. org-goto-exit-command 'right)
  6739. (throw 'exit nil))
  6740. (user-error "Not on a heading")))
  6741. (defun org-goto-quit ()
  6742. "Finish `org-goto' without cursor motion."
  6743. (interactive)
  6744. (setq org-goto-selected-point nil)
  6745. (setq org-goto-exit-command 'quit)
  6746. (throw 'exit nil))
  6747. ;;; Indirect buffer display of subtrees
  6748. (defvar org-indirect-dedicated-frame nil
  6749. "This is the frame being used for indirect tree display.")
  6750. (defvar org-last-indirect-buffer nil)
  6751. (defun org-tree-to-indirect-buffer (&optional arg)
  6752. "Create indirect buffer and narrow it to current subtree.
  6753. With a numerical prefix ARG, go up to this level and then take that tree.
  6754. If ARG is negative, go up that many levels.
  6755. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6756. indirect buffer previously made with this command, to avoid proliferation of
  6757. indirect buffers. However, when you call the command with a \
  6758. \\[universal-argument] prefix, or
  6759. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6760. is kept so that you can work with several indirect buffers at the same time.
  6761. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6762. \\[universal-argument] prefix also
  6763. requests that a new frame be made for the new buffer, so that the dedicated
  6764. frame is not changed."
  6765. (interactive "P")
  6766. (let ((cbuf (current-buffer))
  6767. (cwin (selected-window))
  6768. (pos (point))
  6769. beg end level heading ibuf)
  6770. (save-excursion
  6771. (org-back-to-heading t)
  6772. (when (numberp arg)
  6773. (setq level (org-outline-level))
  6774. (if (< arg 0) (setq arg (+ level arg)))
  6775. (while (> (setq level (org-outline-level)) arg)
  6776. (org-up-heading-safe)))
  6777. (setq beg (point)
  6778. heading (org-get-heading))
  6779. (org-end-of-subtree t t)
  6780. (if (org-at-heading-p) (backward-char 1))
  6781. (setq end (point)))
  6782. (if (and (buffer-live-p org-last-indirect-buffer)
  6783. (not (eq org-indirect-buffer-display 'new-frame))
  6784. (not arg))
  6785. (kill-buffer org-last-indirect-buffer))
  6786. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6787. org-last-indirect-buffer ibuf)
  6788. (cond
  6789. ((or (eq org-indirect-buffer-display 'new-frame)
  6790. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6791. (select-frame (make-frame))
  6792. (delete-other-windows)
  6793. (org-pop-to-buffer-same-window ibuf)
  6794. (org-set-frame-title heading))
  6795. ((eq org-indirect-buffer-display 'dedicated-frame)
  6796. (raise-frame
  6797. (select-frame (or (and org-indirect-dedicated-frame
  6798. (frame-live-p org-indirect-dedicated-frame)
  6799. org-indirect-dedicated-frame)
  6800. (setq org-indirect-dedicated-frame (make-frame)))))
  6801. (delete-other-windows)
  6802. (org-pop-to-buffer-same-window ibuf)
  6803. (org-set-frame-title (concat "Indirect: " heading)))
  6804. ((eq org-indirect-buffer-display 'current-window)
  6805. (org-pop-to-buffer-same-window ibuf))
  6806. ((eq org-indirect-buffer-display 'other-window)
  6807. (pop-to-buffer ibuf))
  6808. (t (error "Invalid value")))
  6809. (if (featurep 'xemacs)
  6810. (save-excursion (org-mode) (turn-on-font-lock)))
  6811. (narrow-to-region beg end)
  6812. (show-all)
  6813. (goto-char pos)
  6814. (run-hook-with-args 'org-cycle-hook 'all)
  6815. (and (window-live-p cwin) (select-window cwin))))
  6816. (defun org-get-indirect-buffer (&optional buffer heading)
  6817. (setq buffer (or buffer (current-buffer)))
  6818. (let ((n 1) (base (buffer-name buffer)) bname)
  6819. (while (buffer-live-p
  6820. (get-buffer
  6821. (setq bname
  6822. (concat base "-"
  6823. (if heading (concat heading "-" (number-to-string n))
  6824. (number-to-string n))))))
  6825. (setq n (1+ n)))
  6826. (condition-case nil
  6827. (make-indirect-buffer buffer bname 'clone)
  6828. (error (make-indirect-buffer buffer bname)))))
  6829. (defun org-set-frame-title (title)
  6830. "Set the title of the current frame to the string TITLE."
  6831. ;; FIXME: how to name a single frame in XEmacs???
  6832. (unless (featurep 'xemacs)
  6833. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6834. ;;;; Structure editing
  6835. ;;; Inserting headlines
  6836. (defun org-previous-line-empty-p (&optional next)
  6837. "Is the previous line a blank line?
  6838. When NEXT is non-nil, check the next line instead."
  6839. (save-excursion
  6840. (and (not (bobp))
  6841. (or (beginning-of-line (if next 2 0)) t)
  6842. (save-match-data
  6843. (looking-at "[ \t]*$")))))
  6844. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6845. "Insert a new heading or an item with the same depth at point.
  6846. If point is at the beginning of a heading or a list item, insert
  6847. a new heading or a new item above the current one. If point is
  6848. at the beginning of a normal line, turn the line into a heading.
  6849. If point is in the middle of a headline or a list item, split the
  6850. headline or the item and create a new headline/item with the text
  6851. in the current line after point \(see `org-M-RET-may-split-line'
  6852. on how to modify this behavior).
  6853. With one universal prefix argument, set the user option
  6854. `org-insert-heading-respect-content' to t for the duration of
  6855. the command. This modifies the behavior described above in this
  6856. ways: on list items and at the beginning of normal lines, force
  6857. the insertion of a heading after the current subtree.
  6858. With two universal prefix arguments, insert the heading at the
  6859. end of the grandparent subtree. For example, if point is within
  6860. a 2nd-level heading, then it will insert a 2nd-level heading at
  6861. the end of the 1st-level parent heading.
  6862. If point is at the beginning of a headline, insert a sibling
  6863. before the current headline. If point is not at the beginning,
  6864. split the line and create a new headline with the text in the
  6865. current line after point \(see `org-M-RET-may-split-line' on how
  6866. to modify this behavior).
  6867. If point is at the beginning of a normal line, turn this line
  6868. into a heading.
  6869. When INVISIBLE-OK is set, stop at invisible headlines when going
  6870. back. This is important for non-interactive uses of the
  6871. command.
  6872. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6873. heading, unconditionally."
  6874. (interactive "P")
  6875. (if (org-called-interactively-p 'any) (org-reveal))
  6876. (let ((itemp (and (not top-level) (org-in-item-p)))
  6877. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6878. (respect-content (or org-insert-heading-respect-content
  6879. (equal arg '(4))))
  6880. (initial-content ""))
  6881. (cond
  6882. ((or (= (buffer-size) 0)
  6883. (and (not (save-excursion
  6884. (and (ignore-errors (org-back-to-heading invisible-ok))
  6885. (org-at-heading-p))))
  6886. (or arg (not itemp))))
  6887. ;; At beginning of buffer or so high up that only a heading
  6888. ;; makes sense.
  6889. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6890. ((not respect-content)
  6891. (unless may-split (end-of-line))
  6892. (insert "\n* "))
  6893. ((re-search-forward org-outline-regexp-bol nil t)
  6894. (beginning-of-line)
  6895. (insert "* \n")
  6896. (backward-char))
  6897. (t (goto-char (point-max))
  6898. (insert "\n* ")))
  6899. (run-hooks 'org-insert-heading-hook))
  6900. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6901. (t
  6902. ;; Maybe move at the end of the subtree
  6903. (when (equal arg '(16))
  6904. (org-up-heading-safe)
  6905. (org-end-of-subtree t))
  6906. ;; Insert a heading
  6907. (save-restriction
  6908. (widen)
  6909. (let* ((level nil)
  6910. (on-heading (org-at-heading-p))
  6911. (empty-line-p (if on-heading
  6912. (org-previous-line-empty-p)
  6913. ;; We will decide later
  6914. nil))
  6915. ;; Get a level string to fall back on.
  6916. (fix-level
  6917. (if (org-before-first-heading-p) "*"
  6918. (save-excursion
  6919. (org-back-to-heading t)
  6920. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6921. (looking-at org-outline-regexp)
  6922. (make-string (1- (length (match-string 0))) ?*))))
  6923. (stars
  6924. (save-excursion
  6925. (condition-case nil
  6926. (if top-level "* "
  6927. (org-back-to-heading invisible-ok)
  6928. (when (and (not on-heading)
  6929. (featurep 'org-inlinetask)
  6930. (integerp org-inlinetask-min-level)
  6931. (>= (length (match-string 0))
  6932. org-inlinetask-min-level))
  6933. ;; Find a heading level before the inline
  6934. ;; task.
  6935. (while (and (setq level (org-up-heading-safe))
  6936. (>= level org-inlinetask-min-level)))
  6937. (if (org-at-heading-p)
  6938. (org-back-to-heading invisible-ok)
  6939. (error "This should not happen")))
  6940. (unless (and (save-excursion
  6941. (save-match-data
  6942. (org-backward-heading-same-level
  6943. 1 invisible-ok))
  6944. (= (point) (match-beginning 0)))
  6945. (not (org-previous-line-empty-p t)))
  6946. (setq empty-line-p (or empty-line-p
  6947. (org-previous-line-empty-p))))
  6948. (match-string 0))
  6949. (error (or fix-level "* ")))))
  6950. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6951. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6952. pos hide-previous previous-pos)
  6953. ;; If we insert after content, move there and clean up
  6954. ;; whitespace.
  6955. (when (and respect-content
  6956. (not (org-looking-at-p org-outline-regexp-bol)))
  6957. (if (not (org-before-first-heading-p))
  6958. (org-end-of-subtree nil t)
  6959. (re-search-forward org-outline-regexp-bol)
  6960. (beginning-of-line 0))
  6961. (skip-chars-backward " \r\t\n")
  6962. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6963. (looking-at "[ \t]+") (replace-match ""))
  6964. (unless (eobp) (forward-char 1))
  6965. (when (looking-at "^\\*")
  6966. (unless (bobp) (backward-char 1))
  6967. (insert "\n")))
  6968. ;; If we are splitting, grab the text that should be moved
  6969. ;; to the new headline.
  6970. (when may-split
  6971. (if (org-on-heading-p)
  6972. ;; This is a heading: split intelligently (keeping
  6973. ;; tags).
  6974. (let ((pos (point)))
  6975. (beginning-of-line)
  6976. (unless (looking-at org-complex-heading-regexp)
  6977. (error "This should not happen"))
  6978. (when (and (match-beginning 4)
  6979. (> pos (match-beginning 4))
  6980. (< pos (match-end 4)))
  6981. (setq initial-content (buffer-substring pos (match-end 4)))
  6982. (goto-char pos)
  6983. (delete-region (point) (match-end 4))
  6984. (if (looking-at "[ \t]*$")
  6985. (replace-match "")
  6986. (insert (make-string (length initial-content) ?\s)))
  6987. (setq initial-content (org-trim initial-content)))
  6988. (goto-char pos))
  6989. ;; A normal line.
  6990. (setq initial-content
  6991. (org-trim
  6992. (delete-and-extract-region (point) (line-end-position))))))
  6993. ;; If we are at the beginning of the line, insert before it.
  6994. ;; Otherwise, after it.
  6995. (cond
  6996. ((and (bolp) (looking-at "[ \t]*$")))
  6997. ((bolp) (save-excursion (insert "\n")))
  6998. (t (end-of-line)
  6999. (insert "\n")))
  7000. ;; Insert the new heading
  7001. (insert stars)
  7002. (just-one-space)
  7003. (insert initial-content)
  7004. (unless (and blank (org-previous-line-empty-p))
  7005. (org-N-empty-lines-before-current (if blank 1 0)))
  7006. ;; Adjust visibility, which may be messed up if we removed
  7007. ;; blank lines while previous entry was hidden.
  7008. (let ((bol (line-beginning-position)))
  7009. (dolist (o (overlays-at (1- bol)))
  7010. (when (and (eq (overlay-get o 'invisible) 'outline)
  7011. (eq (overlay-end o) bol))
  7012. (move-overlay o (overlay-start o) (1- bol)))))
  7013. (run-hooks 'org-insert-heading-hook)))))))
  7014. (defun org-N-empty-lines-before-current (N)
  7015. "Make the number of empty lines before current exactly N.
  7016. So this will delete or add empty lines."
  7017. (save-excursion
  7018. (beginning-of-line)
  7019. (let ((p (point)))
  7020. (skip-chars-backward " \r\t\n")
  7021. (unless (bolp) (forward-line))
  7022. (delete-region (point) p))
  7023. (when (> N 0) (insert (make-string N ?\n)))))
  7024. (defun org-get-heading (&optional no-tags no-todo)
  7025. "Return the heading of the current entry, without the stars.
  7026. When NO-TAGS is non-nil, don't include tags.
  7027. When NO-TODO is non-nil, don't include TODO keywords."
  7028. (save-excursion
  7029. (org-back-to-heading t)
  7030. (cond
  7031. ((and no-tags no-todo)
  7032. (looking-at org-complex-heading-regexp)
  7033. (match-string 4))
  7034. (no-tags
  7035. (looking-at (concat org-outline-regexp
  7036. "\\(.*?\\)"
  7037. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7038. (match-string 1))
  7039. (no-todo
  7040. (looking-at org-todo-line-regexp)
  7041. (match-string 3))
  7042. (t (looking-at org-heading-regexp)
  7043. (match-string 2)))))
  7044. (defvar orgstruct-mode) ; defined below
  7045. (defun org-heading-components ()
  7046. "Return the components of the current heading.
  7047. This is a list with the following elements:
  7048. - the level as an integer
  7049. - the reduced level, different if `org-odd-levels-only' is set.
  7050. - the TODO keyword, or nil
  7051. - the priority character, like ?A, or nil if no priority is given
  7052. - the headline text itself, or the tags string if no headline text
  7053. - the tags string, or nil."
  7054. (save-excursion
  7055. (org-back-to-heading t)
  7056. (if (let (case-fold-search)
  7057. (looking-at
  7058. (if orgstruct-mode
  7059. org-heading-regexp
  7060. org-complex-heading-regexp)))
  7061. (if orgstruct-mode
  7062. (list (length (match-string 1))
  7063. (org-reduced-level (length (match-string 1)))
  7064. nil
  7065. nil
  7066. (match-string 2)
  7067. nil)
  7068. (list (length (match-string 1))
  7069. (org-reduced-level (length (match-string 1)))
  7070. (org-match-string-no-properties 2)
  7071. (and (match-end 3) (aref (match-string 3) 2))
  7072. (org-match-string-no-properties 4)
  7073. (org-match-string-no-properties 5))))))
  7074. (defun org-get-entry ()
  7075. "Get the entry text, after heading, entire subtree."
  7076. (save-excursion
  7077. (org-back-to-heading t)
  7078. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7079. (defun org-insert-heading-after-current ()
  7080. "Insert a new heading with same level as current, after current subtree."
  7081. (interactive)
  7082. (org-back-to-heading)
  7083. (org-insert-heading)
  7084. (org-move-subtree-down)
  7085. (end-of-line 1))
  7086. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7087. "Insert heading with `org-insert-heading-respect-content' set to t."
  7088. (interactive)
  7089. (org-insert-heading '(4) invisible-ok))
  7090. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7091. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7092. (interactive)
  7093. (org-insert-todo-heading force-state '(4)))
  7094. (defun org-insert-todo-heading (arg &optional force-heading)
  7095. "Insert a new heading with the same level and TODO state as current heading.
  7096. If the heading has no TODO state, or if the state is DONE, use the first
  7097. state (TODO by default). Also with one prefix arg, force first state. With
  7098. two prefix args, force inserting at the end of the parent subtree."
  7099. (interactive "P")
  7100. (when (or force-heading (not (org-insert-item 'checkbox)))
  7101. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7102. force-heading))
  7103. (save-excursion
  7104. (org-back-to-heading)
  7105. (outline-previous-heading)
  7106. (looking-at org-todo-line-regexp))
  7107. (let*
  7108. ((new-mark-x
  7109. (if (or (equal arg '(4))
  7110. (not (match-beginning 2))
  7111. (member (match-string 2) org-done-keywords))
  7112. (car org-todo-keywords-1)
  7113. (match-string 2)))
  7114. (new-mark
  7115. (or
  7116. (run-hook-with-args-until-success
  7117. 'org-todo-get-default-hook new-mark-x nil)
  7118. new-mark-x)))
  7119. (beginning-of-line 1)
  7120. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7121. (if org-treat-insert-todo-heading-as-state-change
  7122. (org-todo new-mark)
  7123. (insert new-mark " "))))
  7124. (when org-provide-todo-statistics
  7125. (org-update-parent-todo-statistics))))
  7126. (defun org-insert-subheading (arg)
  7127. "Insert a new subheading and demote it.
  7128. Works for outline headings and for plain lists alike."
  7129. (interactive "P")
  7130. (org-insert-heading arg)
  7131. (cond
  7132. ((org-at-heading-p) (org-do-demote))
  7133. ((org-at-item-p) (org-indent-item))))
  7134. (defun org-insert-todo-subheading (arg)
  7135. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7136. Works for outline headings and for plain lists alike."
  7137. (interactive "P")
  7138. (org-insert-todo-heading arg)
  7139. (cond
  7140. ((org-at-heading-p) (org-do-demote))
  7141. ((org-at-item-p) (org-indent-item))))
  7142. ;;; Promotion and Demotion
  7143. (defvar org-after-demote-entry-hook nil
  7144. "Hook run after an entry has been demoted.
  7145. The cursor will be at the beginning of the entry.
  7146. When a subtree is being demoted, the hook will be called for each node.")
  7147. (defvar org-after-promote-entry-hook nil
  7148. "Hook run after an entry has been promoted.
  7149. The cursor will be at the beginning of the entry.
  7150. When a subtree is being promoted, the hook will be called for each node.")
  7151. (defun org-promote-subtree ()
  7152. "Promote the entire subtree.
  7153. See also `org-promote'."
  7154. (interactive)
  7155. (save-excursion
  7156. (org-with-limited-levels (org-map-tree 'org-promote)))
  7157. (org-fix-position-after-promote))
  7158. (defun org-demote-subtree ()
  7159. "Demote the entire subtree. See `org-demote'.
  7160. See also `org-promote'."
  7161. (interactive)
  7162. (save-excursion
  7163. (org-with-limited-levels (org-map-tree 'org-demote)))
  7164. (org-fix-position-after-promote))
  7165. (defun org-do-promote ()
  7166. "Promote the current heading higher up the tree.
  7167. If the region is active in `transient-mark-mode', promote all headings
  7168. in the region."
  7169. (interactive)
  7170. (save-excursion
  7171. (if (org-region-active-p)
  7172. (org-map-region 'org-promote (region-beginning) (region-end))
  7173. (org-promote)))
  7174. (org-fix-position-after-promote))
  7175. (defun org-do-demote ()
  7176. "Demote the current heading lower down the tree.
  7177. If the region is active in `transient-mark-mode', demote all headings
  7178. in the region."
  7179. (interactive)
  7180. (save-excursion
  7181. (if (org-region-active-p)
  7182. (org-map-region 'org-demote (region-beginning) (region-end))
  7183. (org-demote)))
  7184. (org-fix-position-after-promote))
  7185. (defun org-fix-position-after-promote ()
  7186. "Make sure that after pro/demotion cursor position is right."
  7187. (let ((pos (point)))
  7188. (when (save-excursion
  7189. (beginning-of-line 1)
  7190. (looking-at org-todo-line-regexp)
  7191. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7192. (cond ((eobp) (insert " "))
  7193. ((eolp) (insert " "))
  7194. ((equal (char-after) ?\ ) (forward-char 1))))))
  7195. (defun org-current-level ()
  7196. "Return the level of the current entry, or nil if before the first headline.
  7197. The level is the number of stars at the beginning of the
  7198. headline. Use `org-reduced-level' to remove the effect of
  7199. `org-odd-levels'. Unlike to `org-outline-level', this function
  7200. ignores inlinetasks."
  7201. (let ((level (org-with-limited-levels (org-outline-level))))
  7202. (and (> level 0) level)))
  7203. (defun org-get-previous-line-level ()
  7204. "Return the outline depth of the last headline before the current line.
  7205. Returns 0 for the first headline in the buffer, and nil if before the
  7206. first headline."
  7207. (and (org-current-level)
  7208. (or (and (/= (line-beginning-position) (point-min))
  7209. (save-excursion (beginning-of-line 0) (org-current-level)))
  7210. 0)))
  7211. (defun org-reduced-level (l)
  7212. "Compute the effective level of a heading.
  7213. This takes into account the setting of `org-odd-levels-only'."
  7214. (cond
  7215. ((zerop l) 0)
  7216. (org-odd-levels-only (1+ (floor (/ l 2))))
  7217. (t l)))
  7218. (defun org-level-increment ()
  7219. "Return the number of stars that will be added or removed at a
  7220. time to headlines when structure editing, based on the value of
  7221. `org-odd-levels-only'."
  7222. (if org-odd-levels-only 2 1))
  7223. (defun org-get-valid-level (level &optional change)
  7224. "Rectify a level change under the influence of `org-odd-levels-only'
  7225. LEVEL is a current level, CHANGE is by how much the level should be
  7226. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7227. even level numbers will become the next higher odd number."
  7228. (if org-odd-levels-only
  7229. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7230. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7231. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7232. (max 1 (+ level (or change 0)))))
  7233. (if (boundp 'define-obsolete-function-alias)
  7234. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7235. (define-obsolete-function-alias 'org-get-legal-level
  7236. 'org-get-valid-level)
  7237. (define-obsolete-function-alias 'org-get-legal-level
  7238. 'org-get-valid-level "23.1")))
  7239. (defun org-promote ()
  7240. "Promote the current heading higher up the tree."
  7241. (org-with-wide-buffer
  7242. (org-back-to-heading t)
  7243. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7244. after-change-functions))
  7245. (level (save-match-data (funcall outline-level)))
  7246. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7247. (diff (abs (- level (length up-head) -1))))
  7248. (cond
  7249. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7250. (replace-match "# " nil t))
  7251. ((= level 1)
  7252. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7253. (t (replace-match up-head nil t)))
  7254. (unless (= level 1)
  7255. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7256. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7257. (run-hooks 'org-after-promote-entry-hook))))
  7258. (defun org-demote ()
  7259. "Demote the current heading lower down the tree."
  7260. (org-with-wide-buffer
  7261. (org-back-to-heading t)
  7262. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7263. after-change-functions))
  7264. (level (save-match-data (funcall outline-level)))
  7265. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7266. (diff (abs (- level (length down-head) -1))))
  7267. (replace-match down-head nil t)
  7268. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7269. (when org-adapt-indentation (org-fixup-indentation diff))
  7270. (run-hooks 'org-after-demote-entry-hook))))
  7271. (defun org-cycle-level ()
  7272. "Cycle the level of an empty headline through possible states.
  7273. This goes first to child, then to parent, level, then up the hierarchy.
  7274. After top level, it switches back to sibling level."
  7275. (interactive)
  7276. (let ((org-adapt-indentation nil))
  7277. (when (org-point-at-end-of-empty-headline)
  7278. (setq this-command 'org-cycle-level) ; Only needed for caching
  7279. (let ((cur-level (org-current-level))
  7280. (prev-level (org-get-previous-line-level)))
  7281. (cond
  7282. ;; If first headline in file, promote to top-level.
  7283. ((= prev-level 0)
  7284. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7285. do (org-do-promote)))
  7286. ;; If same level as prev, demote one.
  7287. ((= prev-level cur-level)
  7288. (org-do-demote))
  7289. ;; If parent is top-level, promote to top level if not already.
  7290. ((= prev-level 1)
  7291. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7292. do (org-do-promote)))
  7293. ;; If top-level, return to prev-level.
  7294. ((= cur-level 1)
  7295. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7296. do (org-do-demote)))
  7297. ;; If less than prev-level, promote one.
  7298. ((< cur-level prev-level)
  7299. (org-do-promote))
  7300. ;; If deeper than prev-level, promote until higher than
  7301. ;; prev-level.
  7302. ((> cur-level prev-level)
  7303. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7304. do (org-do-promote))))
  7305. t))))
  7306. (defun org-map-tree (fun)
  7307. "Call FUN for every heading underneath the current one."
  7308. (org-back-to-heading)
  7309. (let ((level (funcall outline-level)))
  7310. (save-excursion
  7311. (funcall fun)
  7312. (while (and (progn
  7313. (outline-next-heading)
  7314. (> (funcall outline-level) level))
  7315. (not (eobp)))
  7316. (funcall fun)))))
  7317. (defun org-map-region (fun beg end)
  7318. "Call FUN for every heading between BEG and END."
  7319. (let ((org-ignore-region t))
  7320. (save-excursion
  7321. (setq end (copy-marker end))
  7322. (goto-char beg)
  7323. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7324. (< (point) end))
  7325. (funcall fun))
  7326. (while (and (progn
  7327. (outline-next-heading)
  7328. (< (point) end))
  7329. (not (eobp)))
  7330. (funcall fun)))))
  7331. (defun org-fixup-indentation (diff)
  7332. "Change the indentation in the current entry by DIFF.
  7333. DIFF is an integer. Indentation is done according to the
  7334. following rules:
  7335. - Planning information and property drawers are always indented
  7336. according to the new level of the headline;
  7337. - Footnote definitions and their contents are ignored;
  7338. - Inlinetasks' boundaries are not shifted;
  7339. - Empty lines are ignored;
  7340. - Other lines' indentation are shifted by DIFF columns, unless
  7341. it would introduce a structural change in the document, in
  7342. which case no shifting is done at all.
  7343. Assume point is at a heading or an inlinetask beginning."
  7344. (org-with-wide-buffer
  7345. (narrow-to-region (line-beginning-position)
  7346. (save-excursion
  7347. (if (org-with-limited-levels (org-at-heading-p))
  7348. (org-with-limited-levels (outline-next-heading))
  7349. (org-inlinetask-goto-end))
  7350. (point)))
  7351. (forward-line)
  7352. ;; Indent properly planning info and property drawer.
  7353. (when (org-looking-at-p org-planning-line-re)
  7354. (org-indent-line)
  7355. (forward-line))
  7356. (when (looking-at org-property-drawer-re)
  7357. (goto-char (match-end 0))
  7358. (forward-line)
  7359. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7360. (catch 'no-shift
  7361. (when (zerop diff) (throw 'no-shift nil))
  7362. ;; If DIFF is negative, first check if a shift is possible at all
  7363. ;; (e.g., it doesn't break structure). This can only happen if
  7364. ;; some contents are not properly indented.
  7365. (let ((case-fold-search t))
  7366. (when (< diff 0)
  7367. (let ((diff (- diff))
  7368. (forbidden-re (concat org-outline-regexp
  7369. "\\|"
  7370. (substring org-footnote-definition-re 1))))
  7371. (save-excursion
  7372. (while (not (eobp))
  7373. (cond
  7374. ((org-looking-at-p "[ \t]*$") (forward-line))
  7375. ((and (org-looking-at-p org-footnote-definition-re)
  7376. (let ((e (org-element-at-point)))
  7377. (and (eq (org-element-type e) 'footnote-definition)
  7378. (goto-char (org-element-property :end e))))))
  7379. ((org-looking-at-p org-outline-regexp) (forward-line))
  7380. ;; Give up if shifting would move before column 0 or
  7381. ;; if it would introduce a headline or a footnote
  7382. ;; definition.
  7383. (t
  7384. (skip-chars-forward " \t")
  7385. (let ((ind (current-column)))
  7386. (when (or (< ind diff)
  7387. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7388. (throw 'no-shift nil)))
  7389. ;; Ignore contents of example blocks and source
  7390. ;; blocks if their indentation is meant to be
  7391. ;; preserved. Jump to block's closing line.
  7392. (beginning-of-line)
  7393. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7394. (let ((e (org-element-at-point)))
  7395. (and (memq (org-element-type e)
  7396. '(example-block src-block))
  7397. (or org-src-preserve-indentation
  7398. (org-element-property :preserve-indent e))
  7399. (goto-char (org-element-property :end e))
  7400. (progn (skip-chars-backward " \r\t\n")
  7401. (beginning-of-line)
  7402. t))))
  7403. (forward-line))))))))
  7404. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7405. ;; by DIFF. Also skip contents of source or example blocks
  7406. ;; when indentation is meant to be preserved.
  7407. (while (not (eobp))
  7408. (cond
  7409. ((and (org-looking-at-p org-footnote-definition-re)
  7410. (let ((e (org-element-at-point)))
  7411. (and (eq (org-element-type e) 'footnote-definition)
  7412. (goto-char (org-element-property :end e))))))
  7413. ((org-looking-at-p org-outline-regexp) (forward-line))
  7414. ((org-looking-at-p "[ \t]*$") (forward-line))
  7415. (t
  7416. (org-indent-line-to (+ (org-get-indentation) diff))
  7417. (beginning-of-line)
  7418. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7419. (let ((e (org-element-at-point)))
  7420. (and (memq (org-element-type e)
  7421. '(example-block src-block))
  7422. (or org-src-preserve-indentation
  7423. (org-element-property :preserve-indent e))
  7424. (goto-char (org-element-property :end e))
  7425. (progn (skip-chars-backward " \r\t\n")
  7426. (beginning-of-line)
  7427. t))))
  7428. (forward-line)))))))))
  7429. (defun org-convert-to-odd-levels ()
  7430. "Convert an org-mode file with all levels allowed to one with odd levels.
  7431. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7432. level 5 etc."
  7433. (interactive)
  7434. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7435. (let ((outline-level 'org-outline-level)
  7436. (org-odd-levels-only nil) n)
  7437. (save-excursion
  7438. (goto-char (point-min))
  7439. (while (re-search-forward "^\\*\\*+ " nil t)
  7440. (setq n (- (length (match-string 0)) 2))
  7441. (while (>= (setq n (1- n)) 0)
  7442. (org-demote))
  7443. (end-of-line 1))))))
  7444. (defun org-convert-to-oddeven-levels ()
  7445. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7446. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7447. file contains a section with an even level, conversion would
  7448. destroy the structure of the file. An error is signaled in this
  7449. case."
  7450. (interactive)
  7451. (goto-char (point-min))
  7452. ;; First check if there are no even levels
  7453. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7454. (org-show-set-visibility 'canonical)
  7455. (error "Not all levels are odd in this file. Conversion not possible"))
  7456. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7457. (let ((outline-regexp org-outline-regexp)
  7458. (outline-level 'org-outline-level)
  7459. (org-odd-levels-only nil) n)
  7460. (save-excursion
  7461. (goto-char (point-min))
  7462. (while (re-search-forward "^\\*\\*+ " nil t)
  7463. (setq n (/ (1- (length (match-string 0))) 2))
  7464. (while (>= (setq n (1- n)) 0)
  7465. (org-promote))
  7466. (end-of-line 1))))))
  7467. (defun org-tr-level (n)
  7468. "Make N odd if required."
  7469. (if org-odd-levels-only (1+ (/ n 2)) n))
  7470. ;;; Vertical tree motion, cutting and pasting of subtrees
  7471. (defun org-move-subtree-up (&optional arg)
  7472. "Move the current subtree up past ARG headlines of the same level."
  7473. (interactive "p")
  7474. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7475. (defun org-move-subtree-down (&optional arg)
  7476. "Move the current subtree down past ARG headlines of the same level."
  7477. (interactive "p")
  7478. (setq arg (prefix-numeric-value arg))
  7479. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7480. 'org-get-last-sibling))
  7481. (ins-point (make-marker))
  7482. (cnt (abs arg))
  7483. (col (current-column))
  7484. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7485. ;; Select the tree
  7486. (org-back-to-heading)
  7487. (setq beg0 (point))
  7488. (save-excursion
  7489. (setq ne-beg (org-back-over-empty-lines))
  7490. (setq beg (point)))
  7491. (save-match-data
  7492. (save-excursion (outline-end-of-heading)
  7493. (setq folded (outline-invisible-p)))
  7494. (progn (org-end-of-subtree nil t)
  7495. (unless (eobp) (backward-char))))
  7496. (outline-next-heading)
  7497. (setq ne-end (org-back-over-empty-lines))
  7498. (setq end (point))
  7499. (goto-char beg0)
  7500. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7501. ;; include less whitespace
  7502. (save-excursion
  7503. (goto-char beg)
  7504. (forward-line (- ne-beg ne-end))
  7505. (setq beg (point))))
  7506. ;; Find insertion point, with error handling
  7507. (while (> cnt 0)
  7508. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7509. (progn (goto-char beg0)
  7510. (user-error "Cannot move past superior level or buffer limit")))
  7511. (setq cnt (1- cnt)))
  7512. (if (> arg 0)
  7513. ;; Moving forward - still need to move over subtree
  7514. (progn (org-end-of-subtree t t)
  7515. (save-excursion
  7516. (org-back-over-empty-lines)
  7517. (or (bolp) (newline)))))
  7518. (setq ne-ins (org-back-over-empty-lines))
  7519. (move-marker ins-point (point))
  7520. (setq txt (buffer-substring beg end))
  7521. (org-save-markers-in-region beg end)
  7522. (delete-region beg end)
  7523. (org-remove-empty-overlays-at beg)
  7524. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7525. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7526. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7527. (let ((bbb (point)))
  7528. (insert-before-markers txt)
  7529. (org-reinstall-markers-in-region bbb)
  7530. (move-marker ins-point bbb))
  7531. (or (bolp) (insert "\n"))
  7532. (setq ins-end (point))
  7533. (goto-char ins-point)
  7534. (org-skip-whitespace)
  7535. (when (and (< arg 0)
  7536. (org-first-sibling-p)
  7537. (> ne-ins ne-beg))
  7538. ;; Move whitespace back to beginning
  7539. (save-excursion
  7540. (goto-char ins-end)
  7541. (let ((kill-whole-line t))
  7542. (kill-line (- ne-ins ne-beg)) (point)))
  7543. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7544. (move-marker ins-point nil)
  7545. (if folded
  7546. (hide-subtree)
  7547. (org-show-entry)
  7548. (org-show-children)
  7549. (org-cycle-hide-drawers 'children))
  7550. (org-clean-visibility-after-subtree-move)
  7551. ;; move back to the initial column we were at
  7552. (move-to-column col)))
  7553. (defvar org-subtree-clip ""
  7554. "Clipboard for cut and paste of subtrees.
  7555. This is actually only a copy of the kill, because we use the normal kill
  7556. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7557. (defvar org-subtree-clip-folded nil
  7558. "Was the last copied subtree folded?
  7559. This is used to fold the tree back after pasting.")
  7560. (defun org-cut-subtree (&optional n)
  7561. "Cut the current subtree into the clipboard.
  7562. With prefix arg N, cut this many sequential subtrees.
  7563. This is a short-hand for marking the subtree and then cutting it."
  7564. (interactive "p")
  7565. (org-copy-subtree n 'cut))
  7566. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7567. "Copy the current subtree it in the clipboard.
  7568. With prefix arg N, copy this many sequential subtrees.
  7569. This is a short-hand for marking the subtree and then copying it.
  7570. If CUT is non-nil, actually cut the subtree.
  7571. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7572. of some markers in the region, even if CUT is non-nil. This is
  7573. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7574. (interactive "p")
  7575. (let (beg end folded (beg0 (point)))
  7576. (if (org-called-interactively-p 'any)
  7577. (org-back-to-heading nil) ; take what looks like a subtree
  7578. (org-back-to-heading t)) ; take what is really there
  7579. (setq beg (point))
  7580. (skip-chars-forward " \t\r\n")
  7581. (save-match-data
  7582. (if nosubtrees
  7583. (outline-next-heading)
  7584. (save-excursion (outline-end-of-heading)
  7585. (setq folded (outline-invisible-p)))
  7586. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7587. (org-end-of-subtree t t)))
  7588. ;; Include the end of an inlinetask
  7589. (when (and (featurep 'org-inlinetask)
  7590. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7591. "END[ \t]*$")))
  7592. (end-of-line))
  7593. (setq end (point))
  7594. (goto-char beg0)
  7595. (when (> end beg)
  7596. (setq org-subtree-clip-folded folded)
  7597. (when (or cut force-store-markers)
  7598. (org-save-markers-in-region beg end))
  7599. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7600. (setq org-subtree-clip (current-kill 0))
  7601. (message "%s: Subtree(s) with %d characters"
  7602. (if cut "Cut" "Copied")
  7603. (length org-subtree-clip)))))
  7604. (defun org-paste-subtree (&optional level tree for-yank remove)
  7605. "Paste the clipboard as a subtree, with modification of headline level.
  7606. The entire subtree is promoted or demoted in order to match a new headline
  7607. level.
  7608. If the cursor is at the beginning of a headline, the same level as
  7609. that headline is used to paste the tree.
  7610. If not, the new level is derived from the *visible* headings
  7611. before and after the insertion point, and taken to be the inferior headline
  7612. level of the two. So if the previous visible heading is level 3 and the
  7613. next is level 4 (or vice versa), level 4 will be used for insertion.
  7614. This makes sure that the subtree remains an independent subtree and does
  7615. not swallow low level entries.
  7616. You can also force a different level, either by using a numeric prefix
  7617. argument, or by inserting the heading marker by hand. For example, if the
  7618. cursor is after \"*****\", then the tree will be shifted to level 5.
  7619. If optional TREE is given, use this text instead of the kill ring.
  7620. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7621. move back over whitespace before inserting, and move point to the end of
  7622. the inserted text when done.
  7623. When REMOVE is non-nil, remove the subtree from the clipboard."
  7624. (interactive "P")
  7625. (setq tree (or tree (and kill-ring (current-kill 0))))
  7626. (unless (org-kill-is-subtree-p tree)
  7627. (user-error "%s"
  7628. (substitute-command-keys
  7629. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7630. (org-with-limited-levels
  7631. (let* ((visp (not (outline-invisible-p)))
  7632. (txt tree)
  7633. (^re_ "\\(\\*+\\)[ \t]*")
  7634. (old-level (if (string-match org-outline-regexp-bol txt)
  7635. (- (match-end 0) (match-beginning 0) 1)
  7636. -1))
  7637. (force-level (cond (level (prefix-numeric-value level))
  7638. ((and (looking-at "[ \t]*$")
  7639. (string-match
  7640. "^\\*+$" (buffer-substring
  7641. (point-at-bol) (point))))
  7642. (- (match-end 0) (match-beginning 0)))
  7643. ((and (bolp)
  7644. (looking-at org-outline-regexp))
  7645. (- (match-end 0) (point) 1))))
  7646. (previous-level (save-excursion
  7647. (condition-case nil
  7648. (progn
  7649. (outline-previous-visible-heading 1)
  7650. (if (looking-at ^re_)
  7651. (- (match-end 0) (match-beginning 0) 1)
  7652. 1))
  7653. (error 1))))
  7654. (next-level (save-excursion
  7655. (condition-case nil
  7656. (progn
  7657. (or (looking-at org-outline-regexp)
  7658. (outline-next-visible-heading 1))
  7659. (if (looking-at ^re_)
  7660. (- (match-end 0) (match-beginning 0) 1)
  7661. 1))
  7662. (error 1))))
  7663. (new-level (or force-level (max previous-level next-level)))
  7664. (shift (if (or (= old-level -1)
  7665. (= new-level -1)
  7666. (= old-level new-level))
  7667. 0
  7668. (- new-level old-level)))
  7669. (delta (if (> shift 0) -1 1))
  7670. (func (if (> shift 0) 'org-demote 'org-promote))
  7671. (org-odd-levels-only nil)
  7672. beg end newend)
  7673. ;; Remove the forced level indicator
  7674. (if force-level
  7675. (delete-region (point-at-bol) (point)))
  7676. ;; Paste
  7677. (beginning-of-line (if (bolp) 1 2))
  7678. (setq beg (point))
  7679. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7680. (insert-before-markers txt)
  7681. (unless (string-match "\n\\'" txt) (insert "\n"))
  7682. (setq newend (point))
  7683. (org-reinstall-markers-in-region beg)
  7684. (setq end (point))
  7685. (goto-char beg)
  7686. (skip-chars-forward " \t\n\r")
  7687. (setq beg (point))
  7688. (if (and (outline-invisible-p) visp)
  7689. (save-excursion (outline-show-heading)))
  7690. ;; Shift if necessary
  7691. (unless (= shift 0)
  7692. (save-restriction
  7693. (narrow-to-region beg end)
  7694. (while (not (= shift 0))
  7695. (org-map-region func (point-min) (point-max))
  7696. (setq shift (+ delta shift)))
  7697. (goto-char (point-min))
  7698. (setq newend (point-max))))
  7699. (when (or (org-called-interactively-p 'interactive) for-yank)
  7700. (message "Clipboard pasted as level %d subtree" new-level))
  7701. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7702. kill-ring
  7703. (eq org-subtree-clip (current-kill 0))
  7704. org-subtree-clip-folded)
  7705. ;; The tree was folded before it was killed/copied
  7706. (hide-subtree))
  7707. (and for-yank (goto-char newend))
  7708. (and remove (setq kill-ring (cdr kill-ring))))))
  7709. (defun org-kill-is-subtree-p (&optional txt)
  7710. "Check if the current kill is an outline subtree, or a set of trees.
  7711. Returns nil if kill does not start with a headline, or if the first
  7712. headline level is not the largest headline level in the tree.
  7713. So this will actually accept several entries of equal levels as well,
  7714. which is OK for `org-paste-subtree'.
  7715. If optional TXT is given, check this string instead of the current kill."
  7716. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7717. (re (org-get-limited-outline-regexp))
  7718. (^re (concat "^" re))
  7719. (start-level (and kill
  7720. (string-match
  7721. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7722. kill)
  7723. (- (match-end 2) (match-beginning 2) 1)))
  7724. (start (1+ (or (match-beginning 2) -1))))
  7725. (if (not start-level)
  7726. (progn
  7727. nil) ;; does not even start with a heading
  7728. (catch 'exit
  7729. (while (setq start (string-match ^re kill (1+ start)))
  7730. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7731. (throw 'exit nil)))
  7732. t))))
  7733. (defvar org-markers-to-move nil
  7734. "Markers that should be moved with a cut-and-paste operation.
  7735. Those markers are stored together with their positions relative to
  7736. the start of the region.")
  7737. (defun org-save-markers-in-region (beg end)
  7738. "Check markers in region.
  7739. If these markers are between BEG and END, record their position relative
  7740. to BEG, so that after moving the block of text, we can put the markers back
  7741. into place.
  7742. This function gets called just before an entry or tree gets cut from the
  7743. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7744. called immediately, to move the markers with the entries."
  7745. (setq org-markers-to-move nil)
  7746. (when (featurep 'org-clock)
  7747. (org-clock-save-markers-for-cut-and-paste beg end))
  7748. (when (featurep 'org-agenda)
  7749. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7750. (defun org-check-and-save-marker (marker beg end)
  7751. "Check if MARKER is between BEG and END.
  7752. If yes, remember the marker and the distance to BEG."
  7753. (when (and (marker-buffer marker)
  7754. (equal (marker-buffer marker) (current-buffer)))
  7755. (if (and (>= marker beg) (< marker end))
  7756. (push (cons marker (- marker beg)) org-markers-to-move))))
  7757. (defun org-reinstall-markers-in-region (beg)
  7758. "Move all remembered markers to their position relative to BEG."
  7759. (mapc (lambda (x)
  7760. (move-marker (car x) (+ beg (cdr x))))
  7761. org-markers-to-move)
  7762. (setq org-markers-to-move nil))
  7763. (defun org-narrow-to-subtree ()
  7764. "Narrow buffer to the current subtree."
  7765. (interactive)
  7766. (save-excursion
  7767. (save-match-data
  7768. (org-with-limited-levels
  7769. (narrow-to-region
  7770. (progn (org-back-to-heading t) (point))
  7771. (progn (org-end-of-subtree t t)
  7772. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7773. (point)))))))
  7774. (defun org-narrow-to-block ()
  7775. "Narrow buffer to the current block."
  7776. (interactive)
  7777. (let* ((case-fold-search t)
  7778. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7779. "^[ \t]*#\\+end_.*")))
  7780. (if blockp
  7781. (narrow-to-region (car blockp) (cdr blockp))
  7782. (user-error "Not in a block"))))
  7783. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7784. "Clone the task (subtree) at point N times.
  7785. The clones will be inserted as siblings.
  7786. In interactive use, the user will be prompted for the number of
  7787. clones to be produced. If the entry has a timestamp, the user
  7788. will also be prompted for a time shift, which may be a repeater
  7789. as used in time stamps, for example `+3d'. To disable this,
  7790. you can call the function with a universal prefix argument.
  7791. When a valid repeater is given and the entry contains any time
  7792. stamps, the clones will become a sequence in time, with time
  7793. stamps in the subtree shifted for each clone produced. If SHIFT
  7794. is nil or the empty string, time stamps will be left alone. The
  7795. ID property of the original subtree is removed.
  7796. If the original subtree did contain time stamps with a repeater,
  7797. the following will happen:
  7798. - the repeater will be removed in each clone
  7799. - an additional clone will be produced, with the current, unshifted
  7800. date(s) in the entry.
  7801. - the original entry will be placed *after* all the clones, with
  7802. repeater intact.
  7803. - the start days in the repeater in the original entry will be shifted
  7804. to past the last clone.
  7805. In this way you can spell out a number of instances of a repeating task,
  7806. and still retain the repeater to cover future instances of the task.
  7807. As described above, N+1 clones are produced when the original
  7808. subtree has a repeater. Setting N to 0, then, can be used to
  7809. remove the repeater from a subtree and create a shifted clone
  7810. with the original repeater."
  7811. (interactive "nNumber of clones to produce: ")
  7812. (let ((shift
  7813. (or shift
  7814. (if (and (not (equal current-prefix-arg '(4)))
  7815. (save-excursion
  7816. (re-search-forward org-ts-regexp-both
  7817. (save-excursion
  7818. (org-end-of-subtree t)
  7819. (point)) t)))
  7820. (read-from-minibuffer
  7821. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7822. ""))) ;; No time shift
  7823. (n-no-remove -1)
  7824. (drawer-re org-drawer-regexp)
  7825. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7826. beg end template task idprop
  7827. shift-n shift-what doshift nmin nmax)
  7828. (unless (wholenump n)
  7829. (user-error "Invalid number of replications %s" n))
  7830. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7831. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7832. shift)))
  7833. (user-error "Invalid shift specification %s" shift))
  7834. (when doshift
  7835. (setq shift-n (string-to-number (match-string 1 shift))
  7836. shift-what (cdr (assoc (match-string 2 shift)
  7837. '(("d" . day) ("w" . week)
  7838. ("m" . month) ("y" . year))))))
  7839. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7840. (setq nmin 1 nmax n)
  7841. (org-back-to-heading t)
  7842. (setq beg (point))
  7843. (setq idprop (org-entry-get nil "ID"))
  7844. (org-end-of-subtree t t)
  7845. (or (bolp) (insert "\n"))
  7846. (setq end (point))
  7847. (setq template (buffer-substring beg end))
  7848. (when (and doshift
  7849. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7850. (delete-region beg end)
  7851. (setq end beg)
  7852. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7853. (goto-char end)
  7854. (loop for n from nmin to nmax do
  7855. ;; prepare clone
  7856. (with-temp-buffer
  7857. (insert template)
  7858. (org-mode)
  7859. (goto-char (point-min))
  7860. (org-show-subtree)
  7861. (and idprop (if org-clone-delete-id
  7862. (org-entry-delete nil "ID")
  7863. (org-id-get-create t)))
  7864. (unless (= n 0)
  7865. (while (re-search-forward org-clock-re nil t)
  7866. (kill-whole-line))
  7867. (goto-char (point-min))
  7868. (while (re-search-forward drawer-re nil t)
  7869. (org-remove-empty-drawer-at (point))))
  7870. (goto-char (point-min))
  7871. (when doshift
  7872. (while (re-search-forward org-ts-regexp-both nil t)
  7873. (org-timestamp-change (* n shift-n) shift-what))
  7874. (unless (= n n-no-remove)
  7875. (goto-char (point-min))
  7876. (while (re-search-forward org-ts-regexp nil t)
  7877. (save-excursion
  7878. (goto-char (match-beginning 0))
  7879. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7880. (delete-region (match-beginning 1) (match-end 1)))))))
  7881. (setq task (buffer-string)))
  7882. (insert task))
  7883. (goto-char beg)))
  7884. ;;; Outline Sorting
  7885. (defun org-sort (with-case)
  7886. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7887. Optional argument WITH-CASE means sort case-sensitively."
  7888. (interactive "P")
  7889. (cond
  7890. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7891. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7892. (t
  7893. (org-call-with-arg 'org-sort-entries with-case))))
  7894. (defun org-sort-remove-invisible (s)
  7895. "Remove invisible links from string S."
  7896. (remove-text-properties 0 (length s) org-rm-props s)
  7897. (while (string-match org-bracket-link-regexp s)
  7898. (setq s (replace-match (if (match-end 2)
  7899. (match-string 3 s)
  7900. (match-string 1 s))
  7901. t t s)))
  7902. (let ((st (format " %s " s)))
  7903. (while (string-match org-emph-re st)
  7904. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7905. (setq s (substring st 1 -1)))
  7906. s)
  7907. (defvar org-priority-regexp) ; defined later in the file
  7908. (defvar org-after-sorting-entries-or-items-hook nil
  7909. "Hook that is run after a bunch of entries or items have been sorted.
  7910. When children are sorted, the cursor is in the parent line when this
  7911. hook gets called. When a region or a plain list is sorted, the cursor
  7912. will be in the first entry of the sorted region/list.")
  7913. (defun org-sort-entries
  7914. (&optional with-case sorting-type getkey-func compare-func property)
  7915. "Sort entries on a certain level of an outline tree.
  7916. If there is an active region, the entries in the region are sorted.
  7917. Else, if the cursor is before the first entry, sort the top-level items.
  7918. Else, the children of the entry at point are sorted.
  7919. Sorting can be alphabetically, numerically, by date/time as given by
  7920. a time stamp, by a property, by priority order, or by a custom function.
  7921. The command prompts for the sorting type unless it has been given to the
  7922. function through the SORTING-TYPE argument, which needs to be a character,
  7923. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7924. the precise meaning of each character:
  7925. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7926. c By creation time, which is assumed to be the first inactive time stamp
  7927. at the beginning of a line.
  7928. d By deadline date/time.
  7929. k By clocking time.
  7930. n Numerically, by converting the beginning of the entry/item to a number.
  7931. o By order of TODO keywords.
  7932. p By priority according to the cookie.
  7933. r By the value of a property.
  7934. s By scheduled date/time.
  7935. t By date/time, either the first active time stamp in the entry, or, if
  7936. none exist, by the first inactive one.
  7937. Capital letters will reverse the sort order.
  7938. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7939. called with point at the beginning of the record. It must return either
  7940. a string or a number that should serve as the sorting key for that record.
  7941. Comparing entries ignores case by default. However, with an optional argument
  7942. WITH-CASE, the sorting considers case as well.
  7943. Sorting is done against the visible part of the headlines, it ignores hidden
  7944. links.
  7945. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7946. (interactive "P")
  7947. (let ((case-func (if with-case 'identity 'downcase))
  7948. (cmstr
  7949. ;; The clock marker is lost when using `sort-subr', let's
  7950. ;; store the clocking string.
  7951. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7952. (save-excursion
  7953. (goto-char org-clock-marker)
  7954. (buffer-substring-no-properties (line-beginning-position)
  7955. (point)))))
  7956. start beg end stars re re2
  7957. txt what tmp)
  7958. ;; Find beginning and end of region to sort
  7959. (cond
  7960. ((org-region-active-p)
  7961. ;; we will sort the region
  7962. (setq end (region-end)
  7963. what "region")
  7964. (goto-char (region-beginning))
  7965. (if (not (org-at-heading-p)) (outline-next-heading))
  7966. (setq start (point)))
  7967. ((or (org-at-heading-p)
  7968. (ignore-errors (progn (org-back-to-heading) t)))
  7969. ;; we will sort the children of the current headline
  7970. (org-back-to-heading)
  7971. (setq start (point)
  7972. end (progn (org-end-of-subtree t t)
  7973. (or (bolp) (insert "\n"))
  7974. (when (>= (org-back-over-empty-lines) 1)
  7975. (forward-line 1))
  7976. (point))
  7977. what "children")
  7978. (goto-char start)
  7979. (show-subtree)
  7980. (outline-next-heading))
  7981. (t
  7982. ;; we will sort the top-level entries in this file
  7983. (goto-char (point-min))
  7984. (or (org-at-heading-p) (outline-next-heading))
  7985. (setq start (point))
  7986. (goto-char (point-max))
  7987. (beginning-of-line 1)
  7988. (when (looking-at ".*?\\S-")
  7989. ;; File ends in a non-white line
  7990. (end-of-line 1)
  7991. (insert "\n"))
  7992. (setq end (point-max))
  7993. (setq what "top-level")
  7994. (goto-char start)
  7995. (show-all)))
  7996. (setq beg (point))
  7997. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7998. (looking-at "\\(\\*+\\)")
  7999. (setq stars (match-string 1)
  8000. re (concat "^" (regexp-quote stars) " +")
  8001. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8002. txt (buffer-substring beg end))
  8003. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8004. (if (and (not (equal stars "*")) (string-match re2 txt))
  8005. (user-error "Region to sort contains a level above the first entry"))
  8006. (unless sorting-type
  8007. (message
  8008. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8009. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8010. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8011. what)
  8012. (setq sorting-type (read-char-exclusive))
  8013. (unless getkey-func
  8014. (and (= (downcase sorting-type) ?f)
  8015. (setq getkey-func
  8016. (org-icompleting-read "Sort using function: "
  8017. obarray 'fboundp t nil nil))
  8018. (setq getkey-func (intern getkey-func))))
  8019. (and (= (downcase sorting-type) ?r)
  8020. (not property)
  8021. (setq property
  8022. (org-icompleting-read "Property: "
  8023. (mapcar 'list (org-buffer-property-keys t))
  8024. nil t))))
  8025. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8026. (message "Sorting entries...")
  8027. (save-restriction
  8028. (narrow-to-region start end)
  8029. (let ((dcst (downcase sorting-type))
  8030. (case-fold-search nil)
  8031. (now (current-time)))
  8032. (sort-subr
  8033. (/= dcst sorting-type)
  8034. ;; This function moves to the beginning character of the "record" to
  8035. ;; be sorted.
  8036. (lambda nil
  8037. (if (re-search-forward re nil t)
  8038. (goto-char (match-beginning 0))
  8039. (goto-char (point-max))))
  8040. ;; This function moves to the last character of the "record" being
  8041. ;; sorted.
  8042. (lambda nil
  8043. (save-match-data
  8044. (condition-case nil
  8045. (outline-forward-same-level 1)
  8046. (error
  8047. (goto-char (point-max))))))
  8048. ;; This function returns the value that gets sorted against.
  8049. (lambda nil
  8050. (cond
  8051. ((= dcst ?n)
  8052. (if (looking-at org-complex-heading-regexp)
  8053. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8054. nil))
  8055. ((= dcst ?a)
  8056. (if (looking-at org-complex-heading-regexp)
  8057. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8058. nil))
  8059. ((= dcst ?k)
  8060. (or (get-text-property (point) :org-clock-minutes) 0))
  8061. ((= dcst ?t)
  8062. (let ((end (save-excursion (outline-next-heading) (point))))
  8063. (if (or (re-search-forward org-ts-regexp end t)
  8064. (re-search-forward org-ts-regexp-both end t))
  8065. (org-time-string-to-seconds (match-string 0))
  8066. (org-float-time now))))
  8067. ((= dcst ?c)
  8068. (let ((end (save-excursion (outline-next-heading) (point))))
  8069. (if (re-search-forward
  8070. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8071. end t)
  8072. (org-time-string-to-seconds (match-string 0))
  8073. (org-float-time now))))
  8074. ((= dcst ?s)
  8075. (let ((end (save-excursion (outline-next-heading) (point))))
  8076. (if (re-search-forward org-scheduled-time-regexp end t)
  8077. (org-time-string-to-seconds (match-string 1))
  8078. (org-float-time now))))
  8079. ((= dcst ?d)
  8080. (let ((end (save-excursion (outline-next-heading) (point))))
  8081. (if (re-search-forward org-deadline-time-regexp end t)
  8082. (org-time-string-to-seconds (match-string 1))
  8083. (org-float-time now))))
  8084. ((= dcst ?p)
  8085. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8086. (string-to-char (match-string 2))
  8087. org-default-priority))
  8088. ((= dcst ?r)
  8089. (or (org-entry-get nil property) ""))
  8090. ((= dcst ?o)
  8091. (if (looking-at org-complex-heading-regexp)
  8092. (let* ((m (match-string 2))
  8093. (s (if (member m org-done-keywords) '- '+)))
  8094. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8095. ((= dcst ?f)
  8096. (if getkey-func
  8097. (progn
  8098. (setq tmp (funcall getkey-func))
  8099. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8100. tmp)
  8101. (error "Invalid key function `%s'" getkey-func)))
  8102. (t (error "Invalid sorting type `%c'" sorting-type))))
  8103. nil
  8104. (cond
  8105. ((= dcst ?a) 'string<)
  8106. ((= dcst ?f) compare-func)
  8107. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8108. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8109. ;; Reset the clock marker if needed
  8110. (when cmstr
  8111. (save-excursion
  8112. (goto-char start)
  8113. (search-forward cmstr nil t)
  8114. (move-marker org-clock-marker (point))))
  8115. (message "Sorting entries...done")))
  8116. ;;; The orgstruct minor mode
  8117. ;; Define a minor mode which can be used in other modes in order to
  8118. ;; integrate the org-mode structure editing commands.
  8119. ;; This is really a hack, because the org-mode structure commands use
  8120. ;; keys which normally belong to the major mode. Here is how it
  8121. ;; works: The minor mode defines all the keys necessary to operate the
  8122. ;; structure commands, but wraps the commands into a function which
  8123. ;; tests if the cursor is currently at a headline or a plain list
  8124. ;; item. If that is the case, the structure command is used,
  8125. ;; temporarily setting many Org-mode variables like regular
  8126. ;; expressions for filling etc. However, when any of those keys is
  8127. ;; used at a different location, function uses `key-binding' to look
  8128. ;; up if the key has an associated command in another currently active
  8129. ;; keymap (minor modes, major mode, global), and executes that
  8130. ;; command. There might be problems if any of the keys is otherwise
  8131. ;; used as a prefix key.
  8132. (defcustom orgstruct-heading-prefix-regexp ""
  8133. "Regexp that matches the custom prefix of Org headlines in
  8134. orgstruct(++)-mode."
  8135. :group 'org
  8136. :version "24.4"
  8137. :package-version '(Org . "8.3")
  8138. :type 'regexp)
  8139. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8140. (defcustom orgstruct-setup-hook nil
  8141. "Hook run after orgstruct-mode-map is filled."
  8142. :group 'org
  8143. :version "24.4"
  8144. :package-version '(Org . "8.0")
  8145. :type 'hook)
  8146. (defvar orgstruct-initialized nil)
  8147. (defvar org-local-vars nil
  8148. "List of local variables, for use by `orgstruct-mode'.")
  8149. ;;;###autoload
  8150. (define-minor-mode orgstruct-mode
  8151. "Toggle the minor mode `orgstruct-mode'.
  8152. This mode is for using Org-mode structure commands in other
  8153. modes. The following keys behave as if Org-mode were active, if
  8154. the cursor is on a headline, or on a plain list item (both as
  8155. defined by Org-mode)."
  8156. nil " OrgStruct" (make-sparse-keymap)
  8157. (funcall (if orgstruct-mode
  8158. 'add-to-invisibility-spec
  8159. 'remove-from-invisibility-spec)
  8160. '(outline . t))
  8161. (when orgstruct-mode
  8162. (org-load-modules-maybe)
  8163. (unless orgstruct-initialized
  8164. (orgstruct-setup)
  8165. (setq orgstruct-initialized t))))
  8166. ;;;###autoload
  8167. (defun turn-on-orgstruct ()
  8168. "Unconditionally turn on `orgstruct-mode'."
  8169. (orgstruct-mode 1))
  8170. (defvar org-fb-vars nil)
  8171. (make-variable-buffer-local 'org-fb-vars)
  8172. (defun orgstruct++-mode (&optional arg)
  8173. "Toggle `orgstruct-mode', the enhanced version of it.
  8174. In addition to setting orgstruct-mode, this also exports all
  8175. indentation and autofilling variables from org-mode into the
  8176. buffer. It will also recognize item context in multiline items."
  8177. (interactive "P")
  8178. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8179. (if (< arg 1)
  8180. (progn (orgstruct-mode -1)
  8181. (mapc (lambda(v)
  8182. (org-set-local (car v)
  8183. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8184. org-fb-vars))
  8185. (orgstruct-mode 1)
  8186. (setq org-fb-vars nil)
  8187. (unless org-local-vars
  8188. (setq org-local-vars (org-get-local-variables)))
  8189. (let (var val)
  8190. (mapc
  8191. (lambda (x)
  8192. (when (string-match
  8193. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8194. (symbol-name (car x)))
  8195. (setq var (car x) val (nth 1 x))
  8196. (push (list var `(quote ,(eval var))) org-fb-vars)
  8197. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8198. org-local-vars)
  8199. (org-set-local 'orgstruct-is-++ t))))
  8200. (defvar orgstruct-is-++ nil
  8201. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8202. (make-variable-buffer-local 'orgstruct-is-++)
  8203. ;;;###autoload
  8204. (defun turn-on-orgstruct++ ()
  8205. "Unconditionally turn on `orgstruct++-mode'."
  8206. (orgstruct++-mode 1))
  8207. (defun orgstruct-error ()
  8208. "Error when there is no default binding for a structure key."
  8209. (interactive)
  8210. (funcall (if (fboundp 'user-error)
  8211. 'user-error
  8212. 'error)
  8213. "This key has no function outside structure elements"))
  8214. (defun orgstruct-setup ()
  8215. "Setup orgstruct keymap."
  8216. (dolist (cell '((org-demote . t)
  8217. (org-metaleft . t)
  8218. (org-metaright . t)
  8219. (org-promote . t)
  8220. (org-shiftmetaleft . t)
  8221. (org-shiftmetaright . t)
  8222. org-backward-element
  8223. org-backward-heading-same-level
  8224. org-ctrl-c-ret
  8225. org-ctrl-c-minus
  8226. org-ctrl-c-star
  8227. org-cycle
  8228. org-forward-heading-same-level
  8229. org-insert-heading
  8230. org-insert-heading-respect-content
  8231. org-kill-note-or-show-branches
  8232. org-mark-subtree
  8233. org-meta-return
  8234. org-metadown
  8235. org-metaup
  8236. org-narrow-to-subtree
  8237. org-promote-subtree
  8238. org-reveal
  8239. org-shiftdown
  8240. org-shiftleft
  8241. org-shiftmetadown
  8242. org-shiftmetaup
  8243. org-shiftright
  8244. org-shifttab
  8245. org-shifttab
  8246. org-shiftup
  8247. org-show-children
  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. (let ((f (or (car-safe cell) cell))
  8257. (disable-when-heading-prefix (cdr-safe cell)))
  8258. (when (fboundp f)
  8259. (let ((new-bindings))
  8260. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8261. (where-is-internal f outline-mode-map)))
  8262. (push binding new-bindings)
  8263. ;; TODO use local-function-key-map
  8264. (dolist (rep '(("<tab>" . "TAB")
  8265. ("<return>" . "RET")
  8266. ("<escape>" . "ESC")
  8267. ("<delete>" . "DEL")))
  8268. (setq binding (read-kbd-macro
  8269. (let ((case-fold-search))
  8270. (replace-regexp-in-string
  8271. (regexp-quote (cdr rep))
  8272. (car rep)
  8273. (key-description binding)))))
  8274. (pushnew binding new-bindings :test 'equal)))
  8275. (dolist (binding new-bindings)
  8276. (let ((key (lookup-key orgstruct-mode-map binding)))
  8277. (when (or (not key) (numberp key))
  8278. (ignore-errors
  8279. (org-defkey orgstruct-mode-map
  8280. binding
  8281. (orgstruct-make-binding
  8282. f binding disable-when-heading-prefix))))))))))
  8283. (run-hooks 'orgstruct-setup-hook))
  8284. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8285. "Create a function for binding in the structure minor mode.
  8286. FUN is the command to call inside a table. KEY is the key that
  8287. should be checked in for a command to execute outside of tables.
  8288. Non-nil `disable-when-heading-prefix' means to disable the command
  8289. if `orgstruct-heading-prefix-regexp' is not empty."
  8290. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8291. (let ((nname name)
  8292. (i 0))
  8293. (while (fboundp (intern nname))
  8294. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8295. (setq name (intern nname)))
  8296. (eval
  8297. (let ((bindings '((org-heading-regexp
  8298. (concat "^"
  8299. orgstruct-heading-prefix-regexp
  8300. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8301. (org-outline-regexp
  8302. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8303. (org-outline-regexp-bol
  8304. (concat "^" org-outline-regexp))
  8305. (outline-regexp org-outline-regexp)
  8306. (outline-heading-end-regexp "\n")
  8307. (outline-level 'org-outline-level)
  8308. (outline-heading-alist))))
  8309. `(defun ,name (arg)
  8310. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8311. "Outside of structure, run the binding of `"
  8312. (key-description key) "'."
  8313. (when disable-when-heading-prefix
  8314. (concat
  8315. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8316. "back to the default binding due to limitations of Org's implementation of\n"
  8317. "`" (symbol-name fun) "'.")))
  8318. (interactive "p")
  8319. (let* ((disable
  8320. ,(and disable-when-heading-prefix
  8321. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8322. (fallback
  8323. (or disable
  8324. (not
  8325. (let* ,bindings
  8326. (org-context-p 'headline 'item
  8327. ,(when (memq fun
  8328. '(org-insert-heading
  8329. org-insert-heading-respect-content
  8330. org-meta-return))
  8331. '(when orgstruct-is-++
  8332. 'item-body))))))))
  8333. (if fallback
  8334. (let* ((orgstruct-mode)
  8335. (binding
  8336. (let ((key ,key))
  8337. (catch 'exit
  8338. (dolist
  8339. (rep
  8340. '(nil
  8341. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8342. ("<\\([^>]*\\)return>" . "\\1RET")
  8343. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8344. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8345. nil)
  8346. (when rep
  8347. (setq key (read-kbd-macro
  8348. (let ((case-fold-search))
  8349. (replace-regexp-in-string
  8350. (car rep)
  8351. (cdr rep)
  8352. (key-description key))))))
  8353. (when (key-binding key)
  8354. (throw 'exit (key-binding key))))))))
  8355. (if (keymapp binding)
  8356. (org-set-transient-map binding)
  8357. (let ((func (or binding
  8358. (unless disable
  8359. 'orgstruct-error))))
  8360. (when func
  8361. (call-interactively func)))))
  8362. (org-run-like-in-org-mode
  8363. (lambda ()
  8364. (interactive)
  8365. (let* ,bindings
  8366. (call-interactively ',fun)))))))))
  8367. name))
  8368. (defun org-contextualize-keys (alist contexts)
  8369. "Return valid elements in ALIST depending on CONTEXTS.
  8370. `org-agenda-custom-commands' or `org-capture-templates' are the
  8371. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8372. or `org-capture-templates-contexts' are the associated contexts
  8373. definitions."
  8374. (let ((contexts
  8375. ;; normalize contexts
  8376. (mapcar
  8377. (lambda(c) (cond ((listp (cadr c))
  8378. (list (car c) (car c) (cadr c)))
  8379. ((string= "" (cadr c))
  8380. (list (car c) (car c) (caddr c)))
  8381. (t c)))
  8382. contexts))
  8383. (a alist) r s)
  8384. ;; loop over all commands or templates
  8385. (dolist (c a)
  8386. (let (vrules repl)
  8387. (cond
  8388. ((not (assoc (car c) contexts))
  8389. (push c r))
  8390. ((and (assoc (car c) contexts)
  8391. (setq vrules (org-contextualize-validate-key
  8392. (car c) contexts)))
  8393. (mapc (lambda (vr)
  8394. (when (not (equal (car vr) (cadr vr)))
  8395. (setq repl vr)))
  8396. vrules)
  8397. (if (not repl) (push c r)
  8398. (push (cadr repl) s)
  8399. (push
  8400. (cons (car c)
  8401. (cdr (or (assoc (cadr repl) alist)
  8402. (error "Undefined key `%s' as contextual replacement for `%s'"
  8403. (cadr repl) (car c)))))
  8404. r))))))
  8405. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8406. (delq
  8407. nil
  8408. (delete-dups
  8409. (mapcar (lambda (x)
  8410. (let ((tpl (car x)))
  8411. (when (not (delq
  8412. nil
  8413. (mapcar (lambda (y)
  8414. (equal y tpl))
  8415. s)))
  8416. x)))
  8417. (reverse r))))))
  8418. (defun org-contextualize-validate-key (key contexts)
  8419. "Check CONTEXTS for agenda or capture KEY."
  8420. (let (rr res)
  8421. (dolist (r contexts)
  8422. (mapc
  8423. (lambda (rr)
  8424. (when
  8425. (and (equal key (car r))
  8426. (if (functionp rr) (funcall rr)
  8427. (or (and (eq (car rr) 'in-file)
  8428. (buffer-file-name)
  8429. (string-match (cdr rr) (buffer-file-name)))
  8430. (and (eq (car rr) 'in-mode)
  8431. (string-match (cdr rr) (symbol-name major-mode)))
  8432. (and (eq (car rr) 'in-buffer)
  8433. (string-match (cdr rr) (buffer-name)))
  8434. (when (and (eq (car rr) 'not-in-file)
  8435. (buffer-file-name))
  8436. (not (string-match (cdr rr) (buffer-file-name))))
  8437. (when (eq (car rr) 'not-in-mode)
  8438. (not (string-match (cdr rr) (symbol-name major-mode))))
  8439. (when (eq (car rr) 'not-in-buffer)
  8440. (not (string-match (cdr rr) (buffer-name)))))))
  8441. (push r res)))
  8442. (car (last r))))
  8443. (delete-dups (delq nil res))))
  8444. (defun org-context-p (&rest contexts)
  8445. "Check if local context is any of CONTEXTS.
  8446. Possible values in the list of contexts are `table', `headline', and `item'."
  8447. (let ((pos (point)))
  8448. (goto-char (point-at-bol))
  8449. (prog1 (or (and (memq 'table contexts)
  8450. (looking-at "[ \t]*|"))
  8451. (and (memq 'headline contexts)
  8452. (looking-at org-outline-regexp))
  8453. (and (memq 'item contexts)
  8454. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8455. (and (memq 'item-body contexts)
  8456. (org-in-item-p)))
  8457. (goto-char pos))))
  8458. (defun org-get-local-variables ()
  8459. "Return a list of all local variables in an Org mode buffer."
  8460. (let (varlist)
  8461. (with-current-buffer (get-buffer-create "*Org tmp*")
  8462. (erase-buffer)
  8463. (org-mode)
  8464. (setq varlist (buffer-local-variables)))
  8465. (kill-buffer "*Org tmp*")
  8466. (delq nil
  8467. (mapcar
  8468. (lambda (x)
  8469. (setq x
  8470. (if (symbolp x)
  8471. (list x)
  8472. (list (car x) (cdr x))))
  8473. (if (and (not (get (car x) 'org-state))
  8474. (string-match
  8475. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8476. (symbol-name (car x))))
  8477. x nil))
  8478. varlist))))
  8479. (defun org-clone-local-variables (from-buffer &optional regexp)
  8480. "Clone local variables from FROM-BUFFER.
  8481. Optional argument REGEXP selects variables to clone."
  8482. (mapc
  8483. (lambda (pair)
  8484. (and (symbolp (car pair))
  8485. (or (null regexp)
  8486. (string-match regexp (symbol-name (car pair))))
  8487. (set (make-local-variable (car pair))
  8488. (cdr pair))))
  8489. (buffer-local-variables from-buffer)))
  8490. ;;;###autoload
  8491. (defun org-run-like-in-org-mode (cmd)
  8492. "Run a command, pretending that the current buffer is in Org-mode.
  8493. This will temporarily bind local variables that are typically bound in
  8494. Org-mode to the values they have in Org-mode, and then interactively
  8495. call CMD."
  8496. (org-load-modules-maybe)
  8497. (unless org-local-vars
  8498. (setq org-local-vars (org-get-local-variables)))
  8499. (let (binds)
  8500. (dolist (var org-local-vars)
  8501. (when (or (not (boundp (car var)))
  8502. (eq (symbol-value (car var))
  8503. (default-value (car var))))
  8504. (push (list (car var) `(quote ,(cadr var))) binds)))
  8505. (eval `(let ,binds
  8506. (call-interactively (quote ,cmd))))))
  8507. (defun org-get-category (&optional pos force-refresh)
  8508. "Get the category applying to position POS."
  8509. (save-match-data
  8510. (if force-refresh (org-refresh-category-properties))
  8511. (let ((pos (or pos (point))))
  8512. (or (get-text-property pos 'org-category)
  8513. (progn (org-refresh-category-properties)
  8514. (get-text-property pos 'org-category))))))
  8515. ;;; Refresh properties
  8516. (defun org-refresh-properties (dprop tprop)
  8517. "Refresh buffer text properties.
  8518. DPROP is the drawer property and TPROP is either the
  8519. corresponding text property to set, or an alist with each element
  8520. being a text property (as a symbol) and a function to apply to
  8521. the value of the drawer property."
  8522. (let ((case-fold-search t)
  8523. (inhibit-read-only t))
  8524. (org-with-silent-modifications
  8525. (save-excursion
  8526. (save-restriction
  8527. (widen)
  8528. (goto-char (point-min))
  8529. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8530. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8531. (defun org-refresh-property (tprop p)
  8532. "Refresh the buffer text property TPROP from the drawer property P.
  8533. The refresh happens only for the current tree (not subtree)."
  8534. (unless (org-before-first-heading-p)
  8535. (save-excursion
  8536. (org-back-to-heading t)
  8537. (if (symbolp tprop)
  8538. ;; TPROP is a text property symbol
  8539. (put-text-property
  8540. (point) (or (outline-next-heading) (point-max)) tprop p)
  8541. ;; TPROP is an alist with (properties . function) elements
  8542. (dolist (al tprop)
  8543. (save-excursion
  8544. (put-text-property
  8545. (line-beginning-position) (or (outline-next-heading) (point-max))
  8546. (car al)
  8547. (funcall (cdr al) p))))))))
  8548. (defun org-refresh-category-properties ()
  8549. "Refresh category text properties in the buffer."
  8550. (let ((case-fold-search t)
  8551. (inhibit-read-only t)
  8552. (default-category
  8553. (cond ((null org-category)
  8554. (if buffer-file-name
  8555. (file-name-sans-extension
  8556. (file-name-nondirectory buffer-file-name))
  8557. "???"))
  8558. ((symbolp org-category) (symbol-name org-category))
  8559. (t org-category))))
  8560. (org-with-silent-modifications
  8561. (org-with-wide-buffer
  8562. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8563. ;; This is the default category for the buffer. If none is
  8564. ;; found, fall-back to `org-category' or buffer file name.
  8565. (put-text-property
  8566. (point-min) (point-max)
  8567. 'org-category
  8568. (catch 'buffer-category
  8569. (goto-char (point-max))
  8570. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8571. (let ((element (org-element-at-point)))
  8572. (when (eq (org-element-type element) 'keyword)
  8573. (throw 'buffer-category
  8574. (org-element-property :value element)))))
  8575. default-category))
  8576. ;; Set sub-tree specific categories.
  8577. (goto-char (point-min))
  8578. (let ((regexp (org-re-property "CATEGORY")))
  8579. (while (re-search-forward regexp nil t)
  8580. (let ((value (org-match-string-no-properties 3)))
  8581. (when (org-at-property-p)
  8582. (put-text-property
  8583. (save-excursion (org-back-to-heading t) (point))
  8584. (save-excursion (org-end-of-subtree t t) (point))
  8585. 'org-category
  8586. value)))))))))
  8587. (defun org-refresh-stats-properties ()
  8588. "Refresh stats text properties in the buffer."
  8589. (let (stats)
  8590. (org-with-silent-modifications
  8591. (save-excursion
  8592. (save-restriction
  8593. (widen)
  8594. (goto-char (point-min))
  8595. (while (re-search-forward
  8596. (concat org-outline-regexp-bol ".*"
  8597. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8598. nil t)
  8599. (setq stats (cond ((equal (match-string 3) "0") 0)
  8600. ((match-string 2)
  8601. (/ (* (string-to-number (match-string 2)) 100)
  8602. (string-to-number (match-string 3))))
  8603. (t (string-to-number (match-string 1)))))
  8604. (org-back-to-heading t)
  8605. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8606. 'org-stats stats)))))))
  8607. (defun org-refresh-effort-properties ()
  8608. "Refresh effort properties"
  8609. (org-refresh-properties
  8610. org-effort-property
  8611. '((effort . identity)
  8612. (effort-minutes . org-duration-string-to-minutes))))
  8613. ;;;; Link Stuff
  8614. ;;; Link abbreviations
  8615. (defun org-link-expand-abbrev (link)
  8616. "Apply replacements as defined in `org-link-abbrev-alist'."
  8617. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8618. (let* ((key (match-string 1 link))
  8619. (as (or (assoc key org-link-abbrev-alist-local)
  8620. (assoc key org-link-abbrev-alist)))
  8621. (tag (and (match-end 2) (match-string 3 link)))
  8622. rpl)
  8623. (if (not as)
  8624. link
  8625. (setq rpl (cdr as))
  8626. (cond
  8627. ((symbolp rpl) (funcall rpl tag))
  8628. ((string-match "%(\\([^)]+\\))" rpl)
  8629. (replace-match
  8630. (save-match-data
  8631. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8632. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8633. ((string-match "%h" rpl)
  8634. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8635. (t (concat rpl tag)))))
  8636. link))
  8637. ;;; Storing and inserting links
  8638. (defvar org-insert-link-history nil
  8639. "Minibuffer history for links inserted with `org-insert-link'.")
  8640. (defvar org-stored-links nil
  8641. "Contains the links stored with `org-store-link'.")
  8642. (defvar org-store-link-plist nil
  8643. "Plist with info about the most recently link created with `org-store-link'.")
  8644. (defvar org-link-protocols nil
  8645. "Link protocols added to Org-mode using `org-add-link-type'.")
  8646. (defvar org-store-link-functions nil
  8647. "List of functions that are called to create and store a link.
  8648. Each function will be called in turn until one returns a non-nil
  8649. value. Each function should check if it is responsible for creating
  8650. this link (for example by looking at the major mode).
  8651. If not, it must exit and return nil.
  8652. If yes, it should return a non-nil value after a calling
  8653. `org-store-link-props' with a list of properties and values.
  8654. Special properties are:
  8655. :type The link prefix, like \"http\". This must be given.
  8656. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8657. This is obligatory as well.
  8658. :description Optional default description for the second pair
  8659. of brackets in an Org-mode link. The user can still change
  8660. this when inserting this link into an Org-mode buffer.
  8661. In addition to these, any additional properties can be specified
  8662. and then used in capture templates.")
  8663. (defun org-add-link-type (type &optional follow export)
  8664. "Add TYPE to the list of `org-link-types'.
  8665. Re-compute all regular expressions depending on `org-link-types'
  8666. FOLLOW and EXPORT are two functions.
  8667. FOLLOW should take the link path as the single argument and do whatever
  8668. is necessary to follow the link, for example find a file or display
  8669. a mail message.
  8670. EXPORT should format the link path for export to one of the export formats.
  8671. It should be a function accepting three arguments:
  8672. path the path of the link, the text after the prefix (like \"http:\")
  8673. desc the description of the link, if any
  8674. format the export format, a symbol like `html' or `latex' or `ascii'.
  8675. The function may use the FORMAT information to return different values
  8676. depending on the format. The return value will be put literally into
  8677. the exported file. If the return value is nil, this means Org should
  8678. do what it normally does with links which do not have EXPORT defined.
  8679. Org mode has a built-in default for exporting links. If you are happy with
  8680. this default, there is no need to define an export function for the link
  8681. type. For a simple example of an export function, see `org-bbdb.el'."
  8682. (add-to-list 'org-link-types type t)
  8683. (org-make-link-regexps)
  8684. (org-element-update-syntax)
  8685. (if (assoc type org-link-protocols)
  8686. (setcdr (assoc type org-link-protocols) (list follow export))
  8687. (push (list type follow export) org-link-protocols)))
  8688. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8689. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8690. ;;;###autoload
  8691. (defun org-store-link (arg)
  8692. "\\<org-mode-map>Store an org-link to the current location.
  8693. This link is added to `org-stored-links' and can later be inserted
  8694. into an Org buffer with \\[org-insert-link].
  8695. For some link types, a prefix ARG is interpreted.
  8696. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8697. For file links, ARG negates `org-context-in-file-links'.
  8698. A double prefix ARG force skipping storing functions that are not
  8699. part of Org's core.
  8700. A triple prefix ARG force storing a link for each line in the
  8701. active region."
  8702. (interactive "P")
  8703. (org-load-modules-maybe)
  8704. (if (and (equal arg '(64)) (org-region-active-p))
  8705. (save-excursion
  8706. (let ((end (region-end)))
  8707. (goto-char (region-beginning))
  8708. (set-mark (point))
  8709. (while (< (point-at-eol) end)
  8710. (move-end-of-line 1) (activate-mark)
  8711. (let (current-prefix-arg)
  8712. (call-interactively 'org-store-link))
  8713. (move-beginning-of-line 2)
  8714. (set-mark (point)))))
  8715. (org-with-limited-levels
  8716. (setq org-store-link-plist nil)
  8717. (let (link cpltxt desc description search
  8718. txt custom-id agenda-link sfuns sfunsn)
  8719. (cond
  8720. ;; Store a link using an external link type
  8721. ((and (not (equal arg '(16)))
  8722. (setq sfuns
  8723. (delq
  8724. nil (mapcar (lambda (f)
  8725. (let (fs) (if (funcall f) (push f fs))))
  8726. org-store-link-functions))
  8727. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8728. (or (and (cdr sfuns)
  8729. (funcall (intern
  8730. (completing-read
  8731. "Which function for creating the link? "
  8732. sfunsn nil t (car sfunsn)))))
  8733. (funcall (caar sfuns)))
  8734. (setq link (plist-get org-store-link-plist :link)
  8735. desc (or (plist-get org-store-link-plist
  8736. :description)
  8737. link))))
  8738. ;; Store a link from a source code buffer.
  8739. ((org-src-edit-buffer-p)
  8740. (cond
  8741. ((save-excursion
  8742. (beginning-of-line)
  8743. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8744. "[ \t]*$")))
  8745. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8746. ((org-called-interactively-p 'any)
  8747. (let (label)
  8748. (while (or (not label)
  8749. (org-with-wide-buffer
  8750. (goto-char (point-min))
  8751. (re-search-forward
  8752. (regexp-quote (format org-coderef-label-format label))
  8753. nil t)))
  8754. (when label (message "Label exists already") (sit-for 2))
  8755. (setq label (read-string "Code line label: " label)))
  8756. (end-of-line)
  8757. (setq link (format org-coderef-label-format label))
  8758. (let ((gc (- 79 (length link))))
  8759. (if (< (current-column) gc) (org-move-to-column gc t)
  8760. (insert " ")))
  8761. (insert link)
  8762. (setq link (concat "(" label ")") desc nil)))
  8763. (t (setq link nil))))
  8764. ;; We are in the agenda, link to referenced location
  8765. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8766. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8767. (get-text-property (point) 'org-marker))))
  8768. (when m
  8769. (org-with-point-at m
  8770. (setq agenda-link
  8771. (if (org-called-interactively-p 'any)
  8772. (call-interactively 'org-store-link)
  8773. (org-store-link nil)))))))
  8774. ((eq major-mode 'calendar-mode)
  8775. (let ((cd (calendar-cursor-to-date)))
  8776. (setq link
  8777. (format-time-string
  8778. (car org-time-stamp-formats)
  8779. (apply 'encode-time
  8780. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8781. nil nil nil))))
  8782. (org-store-link-props :type "calendar" :date cd)))
  8783. ((eq major-mode 'help-mode)
  8784. (setq link (concat "help:" (save-excursion
  8785. (goto-char (point-min))
  8786. (looking-at "^[^ ]+")
  8787. (match-string 0))))
  8788. (org-store-link-props :type "help"))
  8789. ((eq major-mode 'w3-mode)
  8790. (setq cpltxt (if (and (buffer-name)
  8791. (not (string-match "Untitled" (buffer-name))))
  8792. (buffer-name)
  8793. (url-view-url t))
  8794. link (url-view-url t))
  8795. (org-store-link-props :type "w3" :url (url-view-url t)))
  8796. ((eq major-mode 'image-mode)
  8797. (setq cpltxt (concat "file:"
  8798. (abbreviate-file-name buffer-file-name))
  8799. link cpltxt)
  8800. (org-store-link-props :type "image" :file buffer-file-name))
  8801. ;; In dired, store a link to the file of the current line
  8802. ((derived-mode-p 'dired-mode)
  8803. (let ((file (dired-get-filename nil t)))
  8804. (setq file (if file
  8805. (abbreviate-file-name
  8806. (expand-file-name (dired-get-filename nil t)))
  8807. ;; otherwise, no file so use current directory.
  8808. default-directory))
  8809. (setq cpltxt (concat "file:" file)
  8810. link cpltxt)))
  8811. ((setq search (run-hook-with-args-until-success
  8812. 'org-create-file-search-functions))
  8813. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8814. "::" search))
  8815. (setq cpltxt (or description link)))
  8816. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8817. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8818. (cond
  8819. ;; Store a link using the target at point
  8820. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8821. (setq cpltxt
  8822. (concat "file:"
  8823. (abbreviate-file-name
  8824. (buffer-file-name (buffer-base-buffer)))
  8825. "::" (match-string 1))
  8826. link cpltxt))
  8827. ((and (featurep 'org-id)
  8828. (or (eq org-id-link-to-org-use-id t)
  8829. (and (org-called-interactively-p 'any)
  8830. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8831. (and (eq org-id-link-to-org-use-id
  8832. 'create-if-interactive-and-no-custom-id)
  8833. (not custom-id))))
  8834. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8835. ;; Store a link using the ID at point
  8836. (setq link (condition-case nil
  8837. (prog1 (org-id-store-link)
  8838. (setq desc (or (plist-get org-store-link-plist
  8839. :description)
  8840. "")))
  8841. (error
  8842. ;; Probably before first headline, link only to file
  8843. (concat "file:"
  8844. (abbreviate-file-name
  8845. (buffer-file-name (buffer-base-buffer))))))))
  8846. (t
  8847. ;; Just link to current headline
  8848. (setq cpltxt (concat "file:"
  8849. (abbreviate-file-name
  8850. (buffer-file-name (buffer-base-buffer)))))
  8851. ;; Add a context search string
  8852. (when (org-xor org-context-in-file-links arg)
  8853. (let* ((ee (org-element-at-point))
  8854. (et (org-element-type ee))
  8855. (ev (plist-get (cadr ee) :value))
  8856. (ek (plist-get (cadr ee) :key))
  8857. (eok (and (stringp ek) (string-match "name" ek))))
  8858. (setq txt (cond
  8859. ((org-at-heading-p) nil)
  8860. ((and (eq et 'keyword) eok) ev)
  8861. ((org-region-active-p)
  8862. (buffer-substring (region-beginning) (region-end)))))
  8863. (when (or (null txt) (string-match "\\S-" txt))
  8864. (setq cpltxt
  8865. (concat cpltxt "::"
  8866. (condition-case nil
  8867. (org-make-org-heading-search-string txt)
  8868. (error "")))
  8869. desc (or (and (eq et 'keyword) eok ev)
  8870. (nth 4 (ignore-errors (org-heading-components)))
  8871. "NONE")))))
  8872. (if (string-match "::\\'" cpltxt)
  8873. (setq cpltxt (substring cpltxt 0 -2)))
  8874. (setq link cpltxt))))
  8875. ((buffer-file-name (buffer-base-buffer))
  8876. ;; Just link to this file here.
  8877. (setq cpltxt (concat "file:"
  8878. (abbreviate-file-name
  8879. (buffer-file-name (buffer-base-buffer)))))
  8880. ;; Add a context string.
  8881. (when (org-xor org-context-in-file-links arg)
  8882. (setq txt (if (org-region-active-p)
  8883. (buffer-substring (region-beginning) (region-end))
  8884. (buffer-substring (point-at-bol) (point-at-eol))))
  8885. ;; Only use search option if there is some text.
  8886. (when (string-match "\\S-" txt)
  8887. (setq cpltxt
  8888. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8889. desc "NONE")))
  8890. (setq link cpltxt))
  8891. ((org-called-interactively-p 'interactive)
  8892. (user-error "No method for storing a link from this buffer"))
  8893. (t (setq link nil)))
  8894. ;; We're done setting link and desc, clean up
  8895. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8896. (setq link (or link cpltxt)
  8897. desc (or desc cpltxt))
  8898. (cond ((equal desc "NONE") (setq desc nil))
  8899. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8900. (let ((d0 (match-string 3 desc))
  8901. (p0 (match-string 5 desc)))
  8902. (setq desc
  8903. (replace-regexp-in-string
  8904. org-bracket-link-regexp
  8905. (concat (or p0 d0)
  8906. (if (equal (length (match-string 0 desc))
  8907. (length desc)) "*" "")) desc)))))
  8908. ;; Return the link
  8909. (if (not (and (or (org-called-interactively-p 'any)
  8910. executing-kbd-macro)
  8911. link))
  8912. (or agenda-link (and link (org-make-link-string link desc)))
  8913. (push (list link desc) org-stored-links)
  8914. (message "Stored: %s" (or desc link))
  8915. (when custom-id
  8916. (setq link (concat "file:" (abbreviate-file-name
  8917. (buffer-file-name)) "::#" custom-id))
  8918. (push (list link desc) org-stored-links))
  8919. (car org-stored-links))))))
  8920. (defun org-store-link-props (&rest plist)
  8921. "Store link properties, extract names and addresses."
  8922. (let (x adr)
  8923. (when (setq x (plist-get plist :from))
  8924. (setq adr (mail-extract-address-components x))
  8925. (setq plist (plist-put plist :fromname (car adr)))
  8926. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8927. (when (setq x (plist-get plist :to))
  8928. (setq adr (mail-extract-address-components x))
  8929. (setq plist (plist-put plist :toname (car adr)))
  8930. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8931. (let ((from (plist-get plist :from))
  8932. (to (plist-get plist :to)))
  8933. (when (and from to org-from-is-user-regexp)
  8934. (setq plist
  8935. (plist-put plist :fromto
  8936. (if (string-match org-from-is-user-regexp from)
  8937. (concat "to %t")
  8938. (concat "from %f"))))))
  8939. (setq org-store-link-plist plist))
  8940. (defun org-add-link-props (&rest plist)
  8941. "Add these properties to the link property list."
  8942. (let (key value)
  8943. (while plist
  8944. (setq key (pop plist) value (pop plist))
  8945. (setq org-store-link-plist
  8946. (plist-put org-store-link-plist key value)))))
  8947. (defun org-email-link-description (&optional fmt)
  8948. "Return the description part of an email link.
  8949. This takes information from `org-store-link-plist' and formats it
  8950. according to FMT (default from `org-email-link-description-format')."
  8951. (setq fmt (or fmt org-email-link-description-format))
  8952. (let* ((p org-store-link-plist)
  8953. (to (plist-get p :toaddress))
  8954. (from (plist-get p :fromaddress))
  8955. (table
  8956. (list
  8957. (cons "%c" (plist-get p :fromto))
  8958. (cons "%F" (plist-get p :from))
  8959. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8960. (cons "%T" (plist-get p :to))
  8961. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8962. (cons "%s" (plist-get p :subject))
  8963. (cons "%d" (plist-get p :date))
  8964. (cons "%m" (plist-get p :message-id)))))
  8965. (when (string-match "%c" fmt)
  8966. ;; Check if the user wrote this message
  8967. (if (and org-from-is-user-regexp from to
  8968. (save-match-data (string-match org-from-is-user-regexp from)))
  8969. (setq fmt (replace-match "to %t" t t fmt))
  8970. (setq fmt (replace-match "from %f" t t fmt))))
  8971. (org-replace-escapes fmt table)))
  8972. (defun org-make-org-heading-search-string (&optional string)
  8973. "Make search string for the current headline or STRING."
  8974. (let ((s (or string
  8975. (and (derived-mode-p 'org-mode)
  8976. (save-excursion
  8977. (org-back-to-heading t)
  8978. (org-element-property :raw-value (org-element-at-point))))))
  8979. (lines org-context-in-file-links))
  8980. (or string (setq s (concat "*" s))) ; Add * for headlines
  8981. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8982. (when (and string (integerp lines) (> lines 0))
  8983. (let ((slines (org-split-string s "\n")))
  8984. (when (< lines (length slines))
  8985. (setq s (mapconcat
  8986. 'identity
  8987. (reverse (nthcdr (- (length slines) lines)
  8988. (reverse slines))) "\n")))))
  8989. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8990. (defun org-make-link-string (link &optional description)
  8991. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8992. (unless (org-string-nw-p link) (error "Empty link"))
  8993. (let ((uri (cond ((string-match org-link-types-re link)
  8994. (concat (match-string 1 link)
  8995. (org-link-escape (substring link (match-end 1)))))
  8996. ;; For readability, url-encode internal links only
  8997. ;; when absolutely needed (i.e, when they contain
  8998. ;; square brackets). File links however, are
  8999. ;; encoded since, e.g., spaces are significant.
  9000. ((or (file-name-absolute-p link)
  9001. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9002. (org-link-escape link))
  9003. (t link)))
  9004. (description
  9005. (and (org-string-nw-p description)
  9006. ;; Remove brackets from description, as they are fatal.
  9007. (replace-regexp-in-string
  9008. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9009. (org-trim description)))))
  9010. (format "[[%s]%s]"
  9011. uri
  9012. (if description (format "[%s]" description) ""))))
  9013. (defconst org-link-escape-chars
  9014. ;;%20 %5B %5D %25
  9015. '(?\s ?\[ ?\] ?%)
  9016. "List of characters that should be escaped in a link when stored to Org.
  9017. This is the list that is used for internal purposes.")
  9018. (defconst org-link-escape-chars-browser
  9019. ;;%20 %22
  9020. '(?\s ?\")
  9021. "List of characters to be escaped before handing over to the browser.
  9022. If you consider using this constant then you probably want to use
  9023. the function `org-link-escape-browser' instead. See there why
  9024. this constant is a candidate to be removed once Org drops support
  9025. for Emacs 24.1 and 24.2.")
  9026. (defun org-link-escape (text &optional table merge)
  9027. "Return percent escaped representation of TEXT.
  9028. TEXT is a string with the text to escape.
  9029. Optional argument TABLE is a list with characters that should be
  9030. escaped. When nil, `org-link-escape-chars' is used.
  9031. If optional argument MERGE is set, merge TABLE into
  9032. `org-link-escape-chars'."
  9033. (let ((characters-to-encode
  9034. (cond ((null table) org-link-escape-chars)
  9035. (merge (append org-link-escape-chars table))
  9036. (t table))))
  9037. (mapconcat
  9038. (lambda (c)
  9039. (if (or (memq c characters-to-encode)
  9040. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9041. (mapconcat (lambda (e) (format "%%%.2X" e))
  9042. (or (encode-coding-char c 'utf-8)
  9043. (error "Unable to percent escape character: %c" c))
  9044. "")
  9045. (char-to-string c)))
  9046. text "")))
  9047. (defun org-link-escape-browser (text)
  9048. "Escape some characters before handing over to the browser.
  9049. This function is a candidate to be removed together with the
  9050. constant `org-link-escape-chars-browser' once Org drops support
  9051. for Emacs 24.1 and 24.2. All calls to this function will have to
  9052. be replaced with `url-encode-url' which is available since Emacs
  9053. 24.3.1."
  9054. ;; Example with the Org link
  9055. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9056. ;; to open the browser with +subject:"Release 8.2" filled into the
  9057. ;; query field: In this case the variable TEXT contains the
  9058. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9059. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9060. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9061. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9062. (if (fboundp 'url-encode-url)
  9063. (url-encode-url text)
  9064. (if (org-string-match-p
  9065. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9066. text)
  9067. (org-link-escape text org-link-escape-chars-browser)
  9068. text)))
  9069. (defun org-link-unescape (str)
  9070. "Unhex hexified Unicode parts in string STR.
  9071. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9072. reciprocal of `org-link-escape', which see."
  9073. (if (org-string-nw-p str)
  9074. (replace-regexp-in-string
  9075. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9076. str))
  9077. (defun org-link-unescape-compound (hex)
  9078. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9079. Note: this function also decodes single byte encodings like
  9080. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9081. (save-match-data
  9082. (let* ((bytes (cdr (split-string hex "%")))
  9083. (ret "")
  9084. (eat 0)
  9085. (sum 0))
  9086. (while bytes
  9087. (let* ((val (string-to-number (pop bytes) 16))
  9088. (shift-xor
  9089. (if (= 0 eat)
  9090. (cond
  9091. ((>= val 252) (cons 6 252))
  9092. ((>= val 248) (cons 5 248))
  9093. ((>= val 240) (cons 4 240))
  9094. ((>= val 224) (cons 3 224))
  9095. ((>= val 192) (cons 2 192))
  9096. (t (cons 0 0)))
  9097. (cons 6 128))))
  9098. (if (>= val 192) (setq eat (car shift-xor)))
  9099. (setq val (logxor val (cdr shift-xor)))
  9100. (setq sum (+ (lsh sum (car shift-xor)) val))
  9101. (if (> eat 0) (setq eat (- eat 1)))
  9102. (cond
  9103. ((= 0 eat) ;multi byte
  9104. (setq ret (concat ret (org-char-to-string sum)))
  9105. (setq sum 0))
  9106. ((not bytes) ; single byte(s)
  9107. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9108. ret)))
  9109. (defun org-link-unescape-single-byte-sequence (hex)
  9110. "Unhexify hex-encoded single byte character sequences."
  9111. (mapconcat (lambda (byte)
  9112. (char-to-string (string-to-number byte 16)))
  9113. (cdr (split-string hex "%")) ""))
  9114. (defun org-xor (a b)
  9115. "Exclusive or."
  9116. (if a (not b) b))
  9117. (defun org-fixup-message-id-for-http (s)
  9118. "Replace special characters in a message id, so it can be used in an http query."
  9119. (when (string-match "%" s)
  9120. (setq s (mapconcat (lambda (c)
  9121. (if (eq c ?%)
  9122. "%25"
  9123. (char-to-string c)))
  9124. s "")))
  9125. (while (string-match "<" s)
  9126. (setq s (replace-match "%3C" t t s)))
  9127. (while (string-match ">" s)
  9128. (setq s (replace-match "%3E" t t s)))
  9129. (while (string-match "@" s)
  9130. (setq s (replace-match "%40" t t s)))
  9131. s)
  9132. (defun org-link-prettify (link)
  9133. "Return a human-readable representation of LINK.
  9134. The car of LINK must be a raw link.
  9135. The cdr of LINK must be either a link description or nil."
  9136. (let ((desc (or (cadr link) "<no description>")))
  9137. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9138. "<" (car link) ">")))
  9139. ;;;###autoload
  9140. (defun org-insert-link-global ()
  9141. "Insert a link like Org-mode does.
  9142. This command can be called in any mode to insert a link in Org-mode syntax."
  9143. (interactive)
  9144. (org-load-modules-maybe)
  9145. (org-run-like-in-org-mode 'org-insert-link))
  9146. (defun org-insert-all-links (arg &optional pre post)
  9147. "Insert all links in `org-stored-links'.
  9148. When a universal prefix, do not delete the links from `org-stored-links'.
  9149. When `ARG' is a number, insert the last N link(s).
  9150. `PRE' and `POST' are optional arguments to define a string to
  9151. prepend or to append."
  9152. (interactive "P")
  9153. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9154. (links (copy-seq org-stored-links))
  9155. (pr (or pre "- "))
  9156. (po (or post "\n"))
  9157. (cnt 1) l)
  9158. (if (null org-stored-links)
  9159. (message "No link to insert")
  9160. (while (and (or (listp arg) (>= arg cnt))
  9161. (setq l (if (listp arg)
  9162. (pop links)
  9163. (pop org-stored-links))))
  9164. (setq cnt (1+ cnt))
  9165. (insert pr)
  9166. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9167. (insert po)))))
  9168. (defun org-insert-last-stored-link (arg)
  9169. "Insert the last link stored in `org-stored-links'."
  9170. (interactive "p")
  9171. (org-insert-all-links arg "" "\n"))
  9172. (defun org-link-fontify-links-to-this-file ()
  9173. "Fontify links to the current file in `org-stored-links'."
  9174. (let ((f (buffer-file-name)) a b)
  9175. (setq a (mapcar (lambda(l)
  9176. (let ((ll (car l)))
  9177. (when (and (string-match "^file:\\(.+\\)::" ll)
  9178. (equal f (expand-file-name (match-string 1 ll))))
  9179. ll)))
  9180. org-stored-links))
  9181. (when (featurep 'org-id)
  9182. (setq b (mapcar (lambda(l)
  9183. (let ((ll (car l)))
  9184. (when (and (string-match "^id:\\(.+\\)$" ll)
  9185. (equal f (expand-file-name
  9186. (or (org-id-find-id-file
  9187. (match-string 1 ll)) ""))))
  9188. ll)))
  9189. org-stored-links)))
  9190. (mapcar (lambda(l)
  9191. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9192. (delq nil (append a b)))))
  9193. (defvar org-link-links-in-this-file nil)
  9194. (defun org-insert-link (&optional complete-file link-location default-description)
  9195. "Insert a link. At the prompt, enter the link.
  9196. Completion can be used to insert any of the link protocol prefixes like
  9197. http or ftp in use.
  9198. The history can be used to select a link previously stored with
  9199. `org-store-link'. When the empty string is entered (i.e. if you just
  9200. press RET at the prompt), the link defaults to the most recently
  9201. stored link. As SPC triggers completion in the minibuffer, you need to
  9202. use M-SPC or C-q SPC to force the insertion of a space character.
  9203. You will also be prompted for a description, and if one is given, it will
  9204. be displayed in the buffer instead of the link.
  9205. If there is already a link at point, this command will allow you to edit link
  9206. and description parts.
  9207. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9208. be selected using completion. The path to the file will be relative to the
  9209. current directory if the file is in the current directory or a subdirectory.
  9210. Otherwise, the link will be the absolute path as completed in the minibuffer
  9211. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9212. option `org-link-file-path-type'.
  9213. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9214. the current directory or below.
  9215. With three \\[universal-argument] prefixes, negate the meaning of
  9216. `org-keep-stored-link-after-insertion'.
  9217. If `org-make-link-description-function' is non-nil, this function will be
  9218. called with the link target, and the result will be the default
  9219. link description.
  9220. If the LINK-LOCATION parameter is non-nil, this value will be
  9221. used as the link location instead of reading one interactively.
  9222. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9223. be used as the default description."
  9224. (interactive "P")
  9225. (let* ((wcf (current-window-configuration))
  9226. (origbuf (current-buffer))
  9227. (region (if (org-region-active-p)
  9228. (buffer-substring (region-beginning) (region-end))))
  9229. (remove (and region (list (region-beginning) (region-end))))
  9230. (desc region)
  9231. tmphist ; byte-compile incorrectly complains about this
  9232. (link link-location)
  9233. (abbrevs org-link-abbrev-alist-local)
  9234. entry file all-prefixes auto-desc)
  9235. (cond
  9236. (link-location) ; specified by arg, just use it.
  9237. ((org-in-regexp org-bracket-link-regexp 1)
  9238. ;; We do have a link at point, and we are going to edit it.
  9239. (setq remove (list (match-beginning 0) (match-end 0)))
  9240. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9241. (setq link (read-string "Link: "
  9242. (org-link-unescape
  9243. (org-match-string-no-properties 1)))))
  9244. ((or (org-in-regexp org-angle-link-re)
  9245. (org-in-regexp org-plain-link-re))
  9246. ;; Convert to bracket link
  9247. (setq remove (list (match-beginning 0) (match-end 0))
  9248. link (read-string "Link: "
  9249. (org-remove-angle-brackets (match-string 0)))))
  9250. ((member complete-file '((4) (16)))
  9251. ;; Completing read for file names.
  9252. (setq link (org-file-complete-link complete-file)))
  9253. (t
  9254. ;; Read link, with completion for stored links.
  9255. (org-link-fontify-links-to-this-file)
  9256. (org-switch-to-buffer-other-window "*Org Links*")
  9257. (with-current-buffer "*Org Links*"
  9258. (erase-buffer)
  9259. (insert "Insert a link.
  9260. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9261. (when org-stored-links
  9262. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9263. (insert (mapconcat 'org-link-prettify
  9264. (reverse org-stored-links) "\n")))
  9265. (goto-char (point-min)))
  9266. (let ((cw (selected-window)))
  9267. (select-window (get-buffer-window "*Org Links*" 'visible))
  9268. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9269. (unless (pos-visible-in-window-p (point-max))
  9270. (org-fit-window-to-buffer))
  9271. (and (window-live-p cw) (select-window cw)))
  9272. ;; Fake a link history, containing the stored links.
  9273. (setq tmphist (append (mapcar 'car org-stored-links)
  9274. org-insert-link-history))
  9275. (setq all-prefixes (append (mapcar 'car abbrevs)
  9276. (mapcar 'car org-link-abbrev-alist)
  9277. org-link-types))
  9278. (unwind-protect
  9279. (progn
  9280. (setq link
  9281. (org-completing-read
  9282. "Link: "
  9283. (append
  9284. (mapcar (lambda (x) (concat x ":"))
  9285. all-prefixes)
  9286. (mapcar 'car org-stored-links))
  9287. nil nil nil
  9288. 'tmphist
  9289. (caar org-stored-links)))
  9290. (if (not (string-match "\\S-" link))
  9291. (user-error "No link selected"))
  9292. (mapc (lambda(l)
  9293. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9294. org-stored-links)
  9295. (if (or (member link all-prefixes)
  9296. (and (equal ":" (substring link -1))
  9297. (member (substring link 0 -1) all-prefixes)
  9298. (setq link (substring link 0 -1))))
  9299. (setq link (with-current-buffer origbuf
  9300. (org-link-try-special-completion link)))))
  9301. (set-window-configuration wcf)
  9302. (kill-buffer "*Org Links*"))
  9303. (setq entry (assoc link org-stored-links))
  9304. (or entry (push link org-insert-link-history))
  9305. (setq desc (or desc (nth 1 entry)))))
  9306. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9307. (not org-keep-stored-link-after-insertion))
  9308. (setq org-stored-links (delq (assoc link org-stored-links)
  9309. org-stored-links)))
  9310. (if (and (string-match org-plain-link-re link)
  9311. (not (string-match org-ts-regexp link)))
  9312. ;; URL-like link, normalize the use of angular brackets.
  9313. (setq link (org-remove-angle-brackets link)))
  9314. ;; Check if we are linking to the current file with a search
  9315. ;; option If yes, simplify the link by using only the search
  9316. ;; option.
  9317. (when (and buffer-file-name
  9318. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9319. (let* ((path (match-string 1 link))
  9320. (case-fold-search nil)
  9321. (search (match-string 2 link)))
  9322. (save-match-data
  9323. (if (equal (file-truename buffer-file-name) (file-truename path))
  9324. ;; We are linking to this same file, with a search option
  9325. (setq link search)))))
  9326. ;; Check if we can/should use a relative path. If yes, simplify the link
  9327. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9328. (let* ((type (match-string 1 link))
  9329. (path (match-string 2 link))
  9330. (origpath path)
  9331. (case-fold-search nil))
  9332. (cond
  9333. ((or (eq org-link-file-path-type 'absolute)
  9334. (equal complete-file '(16)))
  9335. (setq path (abbreviate-file-name (expand-file-name path))))
  9336. ((eq org-link-file-path-type 'noabbrev)
  9337. (setq path (expand-file-name path)))
  9338. ((eq org-link-file-path-type 'relative)
  9339. (setq path (file-relative-name path)))
  9340. (t
  9341. (save-match-data
  9342. (if (string-match (concat "^" (regexp-quote
  9343. (expand-file-name
  9344. (file-name-as-directory
  9345. default-directory))))
  9346. (expand-file-name path))
  9347. ;; We are linking a file with relative path name.
  9348. (setq path (substring (expand-file-name path)
  9349. (match-end 0)))
  9350. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9351. (setq link (concat type path))
  9352. (if (equal desc origpath)
  9353. (setq desc path))))
  9354. (if org-make-link-description-function
  9355. (setq desc
  9356. (or (condition-case nil
  9357. (funcall org-make-link-description-function link desc)
  9358. (error (progn (message "Can't get link description from `%s'"
  9359. (symbol-name org-make-link-description-function))
  9360. (sit-for 2) nil)))
  9361. (read-string "Description: " default-description)))
  9362. (if default-description (setq desc default-description)
  9363. (setq desc (or (and auto-desc desc)
  9364. (read-string "Description: " desc)))))
  9365. (unless (string-match "\\S-" desc) (setq desc nil))
  9366. (if remove (apply 'delete-region remove))
  9367. (insert (org-make-link-string link desc))))
  9368. (defun org-link-try-special-completion (type)
  9369. "If there is completion support for link type TYPE, offer it."
  9370. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9371. (if (functionp fun)
  9372. (funcall fun)
  9373. (read-string "Link (no completion support): " (concat type ":")))))
  9374. (defun org-file-complete-link (&optional arg)
  9375. "Create a file link using completion."
  9376. (let ((file (org-iread-file-name "File: "))
  9377. (pwd (file-name-as-directory (expand-file-name ".")))
  9378. (pwd1 (file-name-as-directory (abbreviate-file-name
  9379. (expand-file-name ".")))))
  9380. (cond ((equal arg '(16))
  9381. (concat "file:"
  9382. (abbreviate-file-name (expand-file-name file))))
  9383. ((string-match
  9384. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9385. (concat "file:" (match-string 1 file)))
  9386. ((string-match
  9387. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9388. (expand-file-name file))
  9389. (concat "file:"
  9390. (match-string 1 (expand-file-name file))))
  9391. (t (concat "file:" file)))))
  9392. (defun org-iread-file-name (&rest args)
  9393. "Read-file-name using `ido-mode' speedup if available.
  9394. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9395. See `read-file-name' for a description of parameters."
  9396. (org-without-partial-completion
  9397. (if (and org-completion-use-ido
  9398. (fboundp 'ido-read-file-name)
  9399. (org-bound-and-true-p ido-mode)
  9400. (listp (nth 1 args)))
  9401. (let ((ido-enter-matching-directory nil))
  9402. (apply #'ido-read-file-name args))
  9403. (apply #'read-file-name args))))
  9404. (defun org-completing-read (&rest args)
  9405. "Completing-read with SPACE being a normal character."
  9406. (let ((enable-recursive-minibuffers t)
  9407. (minibuffer-local-completion-map
  9408. (copy-keymap minibuffer-local-completion-map)))
  9409. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9410. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9411. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9412. (apply 'org-icompleting-read args)))
  9413. (defun org-completing-read-no-i (&rest args)
  9414. (let (org-completion-use-ido org-completion-use-iswitchb)
  9415. (apply 'org-completing-read args)))
  9416. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9417. "Use iswitch as a completing-read replacement to choose from choices.
  9418. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9419. from."
  9420. (let* ((iswitchb-use-virtual-buffers nil)
  9421. (iswitchb-make-buflist-hook
  9422. (lambda ()
  9423. (setq iswitchb-temp-buflist choices))))
  9424. (iswitchb-read-buffer prompt)))
  9425. (defun org-icompleting-read (&rest args)
  9426. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9427. Should be called like `completing-read'."
  9428. (org-without-partial-completion
  9429. (if (not (listp (nth 1 args)))
  9430. ;; Ido only supports lists as the COLLECTION argument. Use
  9431. ;; default completion function when second argument is not
  9432. ;; a list.
  9433. (apply #'completing-read args)
  9434. (let ((ido-enter-matching-directory nil))
  9435. (apply (cond ((and org-completion-use-ido
  9436. (fboundp 'ido-completing-read)
  9437. (org-bound-and-true-p ido-mode))
  9438. #'ido-completing-read)
  9439. ((and org-completion-use-iswitchb
  9440. (org-bound-and-true-p iswitchb-mode))
  9441. #'org-iswitchb-completing-read)
  9442. (t #'completing-read))
  9443. args)))))
  9444. ;;; Opening/following a link
  9445. (defvar org-link-search-failed nil)
  9446. (defvar org-open-link-functions nil
  9447. "Hook for functions finding a plain text link.
  9448. These functions must take a single argument, the link content.
  9449. They will be called for links that look like [[link text][description]]
  9450. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9451. like a filename (e.g. \"./blue.png\").
  9452. These functions will be called *before* Org attempts to resolve the
  9453. link by doing text searches in the current buffer - so if you want a
  9454. link \"[[target]]\" to still find \"<<target>>\", your function should
  9455. handle this as a special case.
  9456. When the function does handle the link, it must return a non-nil value.
  9457. If it decides that it is not responsible for this link, it must return
  9458. nil to indicate that that Org-mode can continue with other options
  9459. like exact and fuzzy text search.")
  9460. (defun org-next-link (&optional search-backward)
  9461. "Move forward to the next link.
  9462. If the link is in hidden text, expose it."
  9463. (interactive "P")
  9464. (when (and org-link-search-failed (eq this-command last-command))
  9465. (goto-char (point-min))
  9466. (message "Link search wrapped back to beginning of buffer"))
  9467. (setq org-link-search-failed nil)
  9468. (let* ((pos (point))
  9469. (ct (org-context))
  9470. (a (assoc :link ct))
  9471. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9472. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9473. ((looking-at org-any-link-re)
  9474. ;; Don't stay stuck at link without an org-link face
  9475. (forward-char (if search-backward -1 1))))
  9476. (if (funcall srch-fun org-any-link-re nil t)
  9477. (progn
  9478. (goto-char (match-beginning 0))
  9479. (if (outline-invisible-p) (org-show-context)))
  9480. (goto-char pos)
  9481. (setq org-link-search-failed t)
  9482. (message "No further link found"))))
  9483. (defun org-previous-link ()
  9484. "Move backward to the previous link.
  9485. If the link is in hidden text, expose it."
  9486. (interactive)
  9487. (funcall 'org-next-link t))
  9488. (defun org-translate-link (s)
  9489. "Translate a link string if a translation function has been defined."
  9490. (with-temp-buffer
  9491. (insert (org-trim s))
  9492. (org-trim (org-element-interpret-data (org-element-context)))))
  9493. (defun org-translate-link-from-planner (type path)
  9494. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9495. This is still an experimental function, your mileage may vary."
  9496. (cond
  9497. ((member type '("http" "https" "news" "ftp"))
  9498. ;; standard Internet links are the same.
  9499. nil)
  9500. ((and (equal type "irc") (string-match "^//" path))
  9501. ;; Planner has two / at the beginning of an irc link, we have 1.
  9502. ;; We should have zero, actually....
  9503. (setq path (substring path 1)))
  9504. ((and (equal type "lisp") (string-match "^/" path))
  9505. ;; Planner has a slash, we do not.
  9506. (setq type "elisp" path (substring path 1)))
  9507. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9508. ;; A typical message link. Planner has the id after the final slash,
  9509. ;; we separate it with a hash mark
  9510. (setq path (concat (match-string 1 path) "#"
  9511. (org-remove-angle-brackets (match-string 2 path))))))
  9512. (cons type path))
  9513. (defun org-find-file-at-mouse (ev)
  9514. "Open file link or URL at mouse."
  9515. (interactive "e")
  9516. (mouse-set-point ev)
  9517. (org-open-at-point 'in-emacs))
  9518. (defun org-open-at-mouse (ev)
  9519. "Open file link or URL at mouse.
  9520. See the docstring of `org-open-file' for details."
  9521. (interactive "e")
  9522. (mouse-set-point ev)
  9523. (if (eq major-mode 'org-agenda-mode)
  9524. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9525. (org-open-at-point))
  9526. (defvar org-window-config-before-follow-link nil
  9527. "The window configuration before following a link.
  9528. This is saved in case the need arises to restore it.")
  9529. ;;;###autoload
  9530. (defun org-open-at-point-global ()
  9531. "Follow a link like Org-mode does.
  9532. This command can be called in any mode to follow a link that has
  9533. Org-mode syntax."
  9534. (interactive)
  9535. (org-run-like-in-org-mode 'org-open-at-point))
  9536. ;;;###autoload
  9537. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9538. "Open a link in the string S, as if it was in Org-mode."
  9539. (interactive "sLink: \nP")
  9540. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9541. (with-temp-buffer
  9542. (let ((org-inhibit-startup (not reference-buffer)))
  9543. (org-mode)
  9544. (insert s)
  9545. (goto-char (point-min))
  9546. (when reference-buffer
  9547. (setq org-link-abbrev-alist-local
  9548. (with-current-buffer reference-buffer
  9549. org-link-abbrev-alist-local)))
  9550. (org-open-at-point arg reference-buffer)))))
  9551. (defvar org-open-at-point-functions nil
  9552. "Hook that is run when following a link at point.
  9553. Functions in this hook must return t if they identify and follow
  9554. a link at point. If they don't find anything interesting at point,
  9555. they must return nil.")
  9556. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9557. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9558. (defun org-open-at-point (&optional arg reference-buffer)
  9559. "Open link, timestamp, footnote or tags at point.
  9560. When point is on a link, follow it. Normally, files will be
  9561. opened by an appropriate application. If the optional prefix
  9562. argument ARG is non-nil, Emacs will visit the file. With
  9563. a double prefix argument, try to open outside of Emacs, in the
  9564. application the system uses for this file type.
  9565. When point is on a timestamp, open the agenda at the day
  9566. specified.
  9567. When point is a footnote definition, move to the first reference
  9568. found. If it is on a reference, move to the associated
  9569. definition.
  9570. When point is on a headline, display a list of every link in the
  9571. entry, so it is possible to pick one, or all, of them. If point
  9572. is on a tag, call `org-tags-view' instead.
  9573. When optional argument REFERENCE-BUFFER is non-nil, it should
  9574. specify a buffer from where the link search should happen. This
  9575. is used internally by `org-open-link-from-string'.
  9576. On top of syntactically correct links, this function will open
  9577. the link at point in comments or comment blocks and the first
  9578. link in a property drawer line."
  9579. (interactive "P")
  9580. ;; On a code block, open block's results.
  9581. (unless (call-interactively 'org-babel-open-src-block-result)
  9582. (org-load-modules-maybe)
  9583. (setq org-window-config-before-follow-link (current-window-configuration))
  9584. (org-remove-occur-highlights nil nil t)
  9585. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9586. (let* ((context
  9587. ;; Only consider supported types, even if they are not
  9588. ;; the closest one.
  9589. (org-element-lineage
  9590. (org-element-context)
  9591. '(clock comment comment-block footnote-definition
  9592. footnote-reference headline inlinetask keyword link
  9593. node-property timestamp)
  9594. t))
  9595. (type (org-element-type context))
  9596. (value (org-element-property :value context)))
  9597. (cond
  9598. ((not context) (user-error "No link found"))
  9599. ;; Exception: open timestamps and links in properties
  9600. ;; drawers, keywords and comments.
  9601. ((memq type '(comment comment-block keyword node-property))
  9602. (cond ((org-in-regexp org-any-link-re)
  9603. (org-open-link-from-string (match-string-no-properties 0)))
  9604. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9605. (org-follow-timestamp-link))
  9606. (t (user-error "No link found"))))
  9607. ;; On a headline or an inlinetask, but not on a timestamp,
  9608. ;; a link, a footnote reference or on tags.
  9609. ((and (memq type '(headline inlinetask))
  9610. ;; Not on tags.
  9611. (progn (save-excursion (beginning-of-line)
  9612. (looking-at org-complex-heading-regexp))
  9613. (or (not (match-beginning 5))
  9614. (< (point) (match-beginning 5)))))
  9615. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9616. (links (car data))
  9617. (links-end (cdr data)))
  9618. (if links
  9619. (dolist (link (if (stringp links) (list links) links))
  9620. (search-forward link nil links-end)
  9621. (goto-char (match-beginning 0))
  9622. (org-open-at-point))
  9623. (require 'org-attach)
  9624. (org-attach-reveal 'if-exists))))
  9625. ;; On a clock line, make sure point is on the timestamp
  9626. ;; before opening it.
  9627. ((and (eq type 'clock)
  9628. value
  9629. (>= (point) (org-element-property :begin value))
  9630. (<= (point) (org-element-property :end value)))
  9631. (org-follow-timestamp-link))
  9632. ;; Do nothing on white spaces after an object, unless point
  9633. ;; is right after it.
  9634. ((> (point)
  9635. (save-excursion
  9636. (goto-char (org-element-property :end context))
  9637. (skip-chars-backward " \t")
  9638. (point)))
  9639. (user-error "No link found"))
  9640. ((eq type 'timestamp) (org-follow-timestamp-link))
  9641. ;; On tags within a headline or an inlinetask.
  9642. ((and (memq type '(headline inlinetask))
  9643. (progn (save-excursion (beginning-of-line)
  9644. (looking-at org-complex-heading-regexp))
  9645. (and (match-beginning 5)
  9646. (>= (point) (match-beginning 5)))))
  9647. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9648. ((eq type 'link)
  9649. ;; When link is located within the description of another
  9650. ;; link (e.g., an inline image), always open the parent
  9651. ;; link.
  9652. (let*((link (let ((up (org-element-property :parent context)))
  9653. (if (eq (org-element-type up) 'link) up context)))
  9654. (type (org-element-property :type link))
  9655. (path (org-link-unescape (org-element-property :path link))))
  9656. ;; Switch back to REFERENCE-BUFFER needed when called in
  9657. ;; a temporary buffer through `org-open-link-from-string'.
  9658. (with-current-buffer (or reference-buffer (current-buffer))
  9659. (cond
  9660. ((equal type "file")
  9661. (if (string-match "[*?{]" (file-name-nondirectory path))
  9662. (dired path)
  9663. ;; Look into `org-link-protocols' in order to find
  9664. ;; a DEDICATED-FUNCTION to open file. The function
  9665. ;; will be applied on raw link instead of parsed
  9666. ;; link due to the limitation in `org-add-link-type'
  9667. ;; ("open" function called with a single argument).
  9668. ;; If no such function is found, fallback to
  9669. ;; `org-open-file'.
  9670. ;;
  9671. ;; Note : "file+emacs" and "file+sys" types are
  9672. ;; hard-coded in order to escape the previous
  9673. ;; limitation.
  9674. (let* ((option (org-element-property :search-option link))
  9675. (app (org-element-property :application link))
  9676. (dedicated-function
  9677. (nth 1 (assoc app org-link-protocols))))
  9678. (if dedicated-function
  9679. (funcall dedicated-function
  9680. (concat path
  9681. (and option (concat "::" option))))
  9682. (apply #'org-open-file
  9683. path
  9684. (cond (arg)
  9685. ((equal app "emacs") 'emacs)
  9686. ((equal app "sys") 'system))
  9687. (cond ((not option) nil)
  9688. ((org-string-match-p "\\`[0-9]+\\'" option)
  9689. (list (string-to-number option)))
  9690. (t (list nil
  9691. (org-link-unescape option)))))))))
  9692. ((assoc type org-link-protocols)
  9693. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9694. ((equal type "help")
  9695. (let ((f-or-v (intern path)))
  9696. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9697. ((boundp f-or-v) (describe-variable f-or-v))
  9698. (t (error "Not a known function or variable")))))
  9699. ((member type '("http" "https" "ftp" "mailto" "news"))
  9700. (browse-url (org-link-escape-browser (concat type ":" path))))
  9701. ((equal type "doi")
  9702. (browse-url
  9703. (org-link-escape-browser (concat org-doi-server-url path))))
  9704. ((equal type "message") (browse-url (concat type ":" path)))
  9705. ((equal type "shell")
  9706. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9707. (cmd path))
  9708. (if (or (and (org-string-nw-p
  9709. org-confirm-shell-link-not-regexp)
  9710. (string-match
  9711. org-confirm-shell-link-not-regexp cmd))
  9712. (not org-confirm-shell-link-function)
  9713. (funcall org-confirm-shell-link-function
  9714. (format "Execute \"%s\" in shell? "
  9715. (org-add-props cmd nil
  9716. 'face 'org-warning))))
  9717. (progn
  9718. (message "Executing %s" cmd)
  9719. (shell-command cmd buf)
  9720. (when (featurep 'midnight)
  9721. (setq clean-buffer-list-kill-buffer-names
  9722. (cons (buffer-name buf)
  9723. clean-buffer-list-kill-buffer-names))))
  9724. (user-error "Abort"))))
  9725. ((equal type "elisp")
  9726. (let ((cmd path))
  9727. (if (or (and (org-string-nw-p
  9728. org-confirm-elisp-link-not-regexp)
  9729. (org-string-match-p
  9730. org-confirm-elisp-link-not-regexp cmd))
  9731. (not org-confirm-elisp-link-function)
  9732. (funcall org-confirm-elisp-link-function
  9733. (format "Execute \"%s\" as elisp? "
  9734. (org-add-props cmd nil
  9735. 'face 'org-warning))))
  9736. (message "%s => %s" cmd
  9737. (if (eq (string-to-char cmd) ?\()
  9738. (eval (read cmd))
  9739. (call-interactively (read cmd))))
  9740. (user-error "Abort"))))
  9741. ((equal type "id")
  9742. (require 'org-id)
  9743. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9744. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9745. (unless (run-hook-with-args-until-success
  9746. 'org-open-link-functions path)
  9747. (if (not arg) (org-mark-ring-push)
  9748. (switch-to-buffer-other-window
  9749. (org-get-buffer-for-internal-link (current-buffer))))
  9750. (let ((destination
  9751. (org-with-wide-buffer
  9752. (if (equal type "radio")
  9753. (org-search-radio-target
  9754. (org-element-property :path link))
  9755. (org-link-search
  9756. (if (member type '("custom-id" "coderef"))
  9757. (org-element-property :raw-link link)
  9758. path)
  9759. ;; Prevent fuzzy links from matching
  9760. ;; themselves.
  9761. (and (equal type "fuzzy")
  9762. (+ 2 (org-element-property :begin link)))))
  9763. (point))))
  9764. (unless (and (<= (point-min) destination)
  9765. (>= (point-max) destination))
  9766. (widen))
  9767. (goto-char destination))))
  9768. (t (browse-url-at-point))))))
  9769. ;; On a footnote reference or at a footnote definition's label.
  9770. ((or (eq type 'footnote-reference)
  9771. (and (eq type 'footnote-definition)
  9772. (save-excursion
  9773. ;; Do not validate action when point is on the
  9774. ;; spaces right after the footnote label, in
  9775. ;; order to be on par with behaviour on links.
  9776. (skip-chars-forward " \t")
  9777. (let ((begin
  9778. (org-element-property :contents-begin context)))
  9779. (if begin (< (point) begin)
  9780. (= (org-element-property :post-affiliated context)
  9781. (line-beginning-position)))))))
  9782. (org-footnote-action))
  9783. (t (user-error "No link found")))))
  9784. (run-hook-with-args 'org-follow-link-hook)))
  9785. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9786. "Offer links in the current entry and return the selected link.
  9787. If there is only one link, return it.
  9788. If NTH is an integer, return the NTH link found.
  9789. If ZERO is a string, check also this string for a link, and if
  9790. there is one, return it."
  9791. (with-current-buffer buffer
  9792. (save-excursion
  9793. (save-restriction
  9794. (widen)
  9795. (goto-char marker)
  9796. (let ((cnt ?0)
  9797. (in-emacs (if (integerp nth) nil nth))
  9798. have-zero end links link c)
  9799. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9800. (push (match-string 0 zero) links)
  9801. (setq cnt (1- cnt) have-zero t))
  9802. (save-excursion
  9803. (org-back-to-heading t)
  9804. (setq end (save-excursion (outline-next-heading) (point)))
  9805. (while (re-search-forward org-any-link-re end t)
  9806. (push (match-string 0) links))
  9807. (setq links (org-uniquify (reverse links))))
  9808. (cond
  9809. ((null links)
  9810. (message "No links"))
  9811. ((equal (length links) 1)
  9812. (setq link (car links)))
  9813. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9814. (setq link (nth (if have-zero nth (1- nth)) links)))
  9815. (t ; we have to select a link
  9816. (save-excursion
  9817. (save-window-excursion
  9818. (delete-other-windows)
  9819. (with-output-to-temp-buffer "*Select Link*"
  9820. (mapc (lambda (l)
  9821. (if (not (string-match org-bracket-link-regexp l))
  9822. (princ (format "[%c] %s\n" (incf cnt)
  9823. (org-remove-angle-brackets l)))
  9824. (if (match-end 3)
  9825. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9826. (match-string 3 l) (match-string 1 l)))
  9827. (princ (format "[%c] %s\n" (incf cnt)
  9828. (match-string 1 l))))))
  9829. links))
  9830. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9831. (message "Select link to open, RET to open all:")
  9832. (setq c (read-char-exclusive))
  9833. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9834. (when (equal c ?q) (user-error "Abort"))
  9835. (if (equal c ?\C-m)
  9836. (setq link links)
  9837. (setq nth (- c ?0))
  9838. (if have-zero (setq nth (1+ nth)))
  9839. (unless (and (integerp nth) (>= (length links) nth))
  9840. (user-error "Invalid link selection"))
  9841. (setq link (nth (1- nth) links)))))
  9842. (cons link end))))))
  9843. ;; TODO: These functions are deprecated since `org-open-at-point'
  9844. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9845. (defun org-open-file-with-system (path)
  9846. "Open file at PATH using the system way of opening it."
  9847. (org-open-file path 'system))
  9848. (defun org-open-file-with-emacs (path)
  9849. "Open file at PATH in Emacs."
  9850. (org-open-file path 'emacs))
  9851. ;;; File search
  9852. (defvar org-create-file-search-functions nil
  9853. "List of functions to construct the right search string for a file link.
  9854. These functions are called in turn with point at the location to
  9855. which the link should point.
  9856. A function in the hook should first test if it would like to
  9857. handle this file type, for example by checking the `major-mode'
  9858. or the file extension. If it decides not to handle this file, it
  9859. should just return nil to give other functions a chance. If it
  9860. does handle the file, it must return the search string to be used
  9861. when following the link. The search string will be part of the
  9862. file link, given after a double colon, and `org-open-at-point'
  9863. will automatically search for it. If special measures must be
  9864. taken to make the search successful, another function should be
  9865. added to the companion hook `org-execute-file-search-functions',
  9866. which see.
  9867. A function in this hook may also use `setq' to set the variable
  9868. `description' to provide a suggestion for the descriptive text to
  9869. be used for this link when it gets inserted into an Org-mode
  9870. buffer with \\[org-insert-link].")
  9871. (defvar org-execute-file-search-functions nil
  9872. "List of functions to execute a file search triggered by a link.
  9873. Functions added to this hook must accept a single argument, the
  9874. search string that was part of the file link, the part after the
  9875. double colon. The function must first check if it would like to
  9876. handle this search, for example by checking the `major-mode' or
  9877. the file extension. If it decides not to handle this search, it
  9878. should just return nil to give other functions a chance. If it
  9879. does handle the search, it must return a non-nil value to keep
  9880. other functions from trying.
  9881. Each function can access the current prefix argument through the
  9882. variable `current-prefix-arg'. Note that a single prefix is used
  9883. to force opening a link in Emacs, so it may be good to only use a
  9884. numeric or double prefix to guide the search function.
  9885. In case this is needed, a function in this hook can also restore
  9886. the window configuration before `org-open-at-point' was called using:
  9887. (set-window-configuration org-window-config-before-follow-link)")
  9888. (defun org-search-radio-target (target)
  9889. "Search a radio target matching TARGET in current buffer.
  9890. White spaces are not significant."
  9891. (let ((re (format "<<<%s>>>"
  9892. (mapconcat #'regexp-quote
  9893. (org-split-string target "[ \t\n]+")
  9894. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9895. (origin (point)))
  9896. (goto-char (point-min))
  9897. (catch :radio-match
  9898. (while (re-search-forward re nil t)
  9899. (backward-char)
  9900. (let ((object (org-element-context)))
  9901. (when (eq (org-element-type object) 'radio-target)
  9902. (goto-char (org-element-property :begin object))
  9903. (org-show-context 'link-search)
  9904. (throw :radio-match nil))))
  9905. (goto-char origin)
  9906. (user-error "No match for radio target: %s" target))))
  9907. (defun org-link-search (s &optional avoid-pos stealth)
  9908. "Search for a search string S.
  9909. If S starts with \"#\", it triggers a custom ID search.
  9910. If S is enclosed within parenthesis, it initiates a coderef
  9911. search.
  9912. If S is surrounded by forward slashes, it is interpreted as
  9913. a regular expression. In Org mode files, this will create an
  9914. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9915. to list matches. If the current buffer is in `dired-mode', grep
  9916. will be used to search in all files.
  9917. When AVOID-POS is given, ignore matches near that position.
  9918. When optional argument STEALTH is non-nil, do not modify
  9919. visibility around point, thus ignoring `org-show-context-detail'
  9920. variable.
  9921. Search is case-insensitive and ignores white spaces. Return type
  9922. of matched result, with is either `dedicated' or `fuzzy'."
  9923. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9924. (let* ((case-fold-search t)
  9925. (origin (point))
  9926. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9927. (words (org-split-string s "[ \t\n]+"))
  9928. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9929. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9930. type)
  9931. (cond
  9932. ;; Check if there are any special search functions.
  9933. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9934. ((eq (string-to-char s) ?#)
  9935. ;; Look for a custom ID S if S starts with "#".
  9936. (let* ((id (substring normalized 1))
  9937. (match (org-find-property "CUSTOM_ID" id)))
  9938. (if match (progn (goto-char match) (setf type 'dedicated))
  9939. (error "No match for custom ID: %s" id))))
  9940. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9941. ;; Look for coderef targets if S is enclosed within parenthesis.
  9942. (let ((coderef (match-string-no-properties 1 normalized))
  9943. (re (substring s-single-re 1 -1)))
  9944. (goto-char (point-min))
  9945. (catch :coderef-match
  9946. (while (re-search-forward re nil t)
  9947. (let ((element (org-element-at-point)))
  9948. (when (and (memq (org-element-type element)
  9949. '(example-block src-block))
  9950. ;; Build proper regexp according to current
  9951. ;; block's label format.
  9952. (let ((label-fmt
  9953. (regexp-quote
  9954. (or (org-element-property :label-fmt element)
  9955. org-coderef-label-format))))
  9956. (save-excursion
  9957. (beginning-of-line)
  9958. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9959. (format label-fmt coderef))))))
  9960. (setq type 'dedicated)
  9961. (goto-char (match-beginning 1))
  9962. (throw :coderef-match nil))))
  9963. (goto-char origin)
  9964. (error "No match for coderef: %s" coderef))))
  9965. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9966. ;; Look for a regular expression.
  9967. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9968. (match-string 1 s)))
  9969. ;; Fuzzy links.
  9970. (t
  9971. (let* ((starred (eq (string-to-char normalized) ?*))
  9972. (headline-search (and (derived-mode-p 'org-mode)
  9973. (or org-link-search-must-match-exact-headline
  9974. starred))))
  9975. (cond
  9976. ;; Look for targets, only if not in a headline search.
  9977. ((and (not starred)
  9978. (let ((target (format "<<%s>>" s-multi-re)))
  9979. (catch :target-match
  9980. (goto-char (point-min))
  9981. (while (re-search-forward target nil t)
  9982. (backward-char)
  9983. (let ((context (org-element-context)))
  9984. (when (eq (org-element-type context) 'target)
  9985. (setq type 'dedicated)
  9986. (goto-char (org-element-property :begin context))
  9987. (throw :target-match t))))
  9988. nil))))
  9989. ;; Look for elements named after S, only if not in a headline
  9990. ;; search.
  9991. ((and (not starred)
  9992. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9993. (catch :name-match
  9994. (goto-char (point-min))
  9995. (while (re-search-forward name nil t)
  9996. (let ((element (org-element-at-point)))
  9997. (when (equal (org-split-string
  9998. (org-element-property :name element)
  9999. "[ \t]+")
  10000. words)
  10001. (setq type 'dedicated)
  10002. (beginning-of-line)
  10003. (throw :name-match t))))
  10004. nil))))
  10005. ;; Regular text search. Prefer headlines in Org mode
  10006. ;; buffers.
  10007. ((and (derived-mode-p 'org-mode)
  10008. (let* ((wspace "[ \t]")
  10009. (wspaceopt (concat wspace "*"))
  10010. (cookie (concat "\\(?:"
  10011. wspaceopt
  10012. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10013. wspaceopt
  10014. "\\)"))
  10015. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10016. (re (concat
  10017. org-outline-regexp-bol
  10018. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10019. "\\(?:\\[#.\\][ \t]+\\)?"
  10020. "\\(?:" org-comment-string "[ \t]+\\)?"
  10021. sep "?"
  10022. (let ((title (mapconcat #'regexp-quote
  10023. words
  10024. sep)))
  10025. (if starred (substring title 1) title))
  10026. sep "?"
  10027. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10028. "[ \t]*$")))
  10029. (goto-char (point-min))
  10030. (re-search-forward re nil t)))
  10031. (goto-char (match-beginning 0))
  10032. (setq type 'dedicated))
  10033. ;; Offer to create non-existent headline depending on
  10034. ;; `org-link-search-must-match-exact-headline'.
  10035. ((and (derived-mode-p 'org-mode)
  10036. (not org-link-search-inhibit-query)
  10037. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10038. (yes-or-no-p "No match - create this as a new heading? "))
  10039. (goto-char (point-max))
  10040. (unless (bolp) (newline))
  10041. (org-insert-heading nil t t)
  10042. (insert s "\n")
  10043. (beginning-of-line 0))
  10044. ;; Only headlines are looked after. No need to process
  10045. ;; further: throw an error.
  10046. ((and (derived-mode-p 'org-mode)
  10047. (or starred org-link-search-must-match-exact-headline))
  10048. (goto-char origin)
  10049. (error "No match for fuzzy expression: %s" normalized))
  10050. ;; Regular text search.
  10051. ((catch :fuzzy-match
  10052. (goto-char (point-min))
  10053. (while (re-search-forward s-multi-re nil t)
  10054. ;; Skip match if it contains AVOID-POS or it is included
  10055. ;; in a link with a description but outside the
  10056. ;; description.
  10057. (unless (or (and avoid-pos
  10058. (<= (match-beginning 0) avoid-pos)
  10059. (> (match-end 0) avoid-pos))
  10060. (and (save-match-data
  10061. (org-in-regexp org-bracket-link-regexp))
  10062. (match-beginning 3)
  10063. (or (> (match-beginning 3) (point))
  10064. (<= (match-end 3) (point)))
  10065. (org-element-lineage
  10066. (save-match-data (org-element-context))
  10067. '(link) t)))
  10068. (goto-char (match-beginning 0))
  10069. (setq type 'fuzzy)
  10070. (throw :fuzzy-match t)))
  10071. nil))
  10072. ;; All failed. Throw an error.
  10073. (t (goto-char origin)
  10074. (error "No match for fuzzy expression: %s" normalized))))))
  10075. ;; Disclose surroundings of match, if appropriate.
  10076. (when (and (derived-mode-p 'org-mode) (not stealth))
  10077. (org-show-context 'link-search))
  10078. type))
  10079. (defun org-get-buffer-for-internal-link (buffer)
  10080. "Return a buffer to be used for displaying the link target of internal links."
  10081. (cond
  10082. ((not org-display-internal-link-with-indirect-buffer)
  10083. buffer)
  10084. ((string-match "(Clone)$" (buffer-name buffer))
  10085. (message "Buffer is already a clone, not making another one")
  10086. ;; we also do not modify visibility in this case
  10087. buffer)
  10088. (t ; make a new indirect buffer for displaying the link
  10089. (let* ((bn (buffer-name buffer))
  10090. (ibn (concat bn "(Clone)"))
  10091. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10092. (with-current-buffer ib (org-overview))
  10093. ib))))
  10094. (defun org-do-occur (regexp &optional cleanup)
  10095. "Call the Emacs command `occur'.
  10096. If CLEANUP is non-nil, remove the printout of the regular expression
  10097. in the *Occur* buffer. This is useful if the regex is long and not useful
  10098. to read."
  10099. (occur regexp)
  10100. (when cleanup
  10101. (let ((cwin (selected-window)) win beg end)
  10102. (when (setq win (get-buffer-window "*Occur*"))
  10103. (select-window win))
  10104. (goto-char (point-min))
  10105. (when (re-search-forward "match[a-z]+" nil t)
  10106. (setq beg (match-end 0))
  10107. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10108. (setq end (1- (match-beginning 0)))))
  10109. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10110. (goto-char (point-min))
  10111. (select-window cwin))))
  10112. ;;; The mark ring for links jumps
  10113. (defvar org-mark-ring nil
  10114. "Mark ring for positions before jumps in Org-mode.")
  10115. (defvar org-mark-ring-last-goto nil
  10116. "Last position in the mark ring used to go back.")
  10117. ;; Fill and close the ring
  10118. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10119. (loop for i from 1 to org-mark-ring-length do
  10120. (push (make-marker) org-mark-ring))
  10121. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10122. org-mark-ring)
  10123. (defun org-mark-ring-push (&optional pos buffer)
  10124. "Put the current position or POS into the mark ring and rotate it."
  10125. (interactive)
  10126. (setq pos (or pos (point)))
  10127. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10128. (move-marker (car org-mark-ring)
  10129. (or pos (point))
  10130. (or buffer (current-buffer)))
  10131. (message "%s"
  10132. (substitute-command-keys
  10133. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10134. (defun org-mark-ring-goto (&optional n)
  10135. "Jump to the previous position in the mark ring.
  10136. With prefix arg N, jump back that many stored positions. When
  10137. called several times in succession, walk through the entire ring.
  10138. Org-mode commands jumping to a different position in the current file,
  10139. or to another Org-mode file, automatically push the old position
  10140. onto the ring."
  10141. (interactive "p")
  10142. (let (p m)
  10143. (if (eq last-command this-command)
  10144. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10145. (setq p org-mark-ring))
  10146. (setq org-mark-ring-last-goto p)
  10147. (setq m (car p))
  10148. (org-pop-to-buffer-same-window (marker-buffer m))
  10149. (goto-char m)
  10150. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10151. (defun org-remove-angle-brackets (s)
  10152. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10153. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10154. s)
  10155. (defun org-add-angle-brackets (s)
  10156. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10157. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10158. s)
  10159. (defun org-remove-double-quotes (s)
  10160. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10161. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10162. s)
  10163. ;;; Following specific links
  10164. (defun org-follow-timestamp-link ()
  10165. "Open an agenda view for the time-stamp date/range at point."
  10166. (cond
  10167. ((org-at-date-range-p t)
  10168. (let ((org-agenda-start-on-weekday)
  10169. (t1 (match-string 1))
  10170. (t2 (match-string 2)) tt1 tt2)
  10171. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10172. tt2 (time-to-days (org-time-string-to-time t2)))
  10173. (let ((org-agenda-buffer-tmp-name
  10174. (format "*Org Agenda(a:%s)"
  10175. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10176. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10177. ((org-at-timestamp-p t)
  10178. (let ((org-agenda-buffer-tmp-name
  10179. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10180. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10181. (substring (match-string 1) 0 10)))
  10182. 1)))
  10183. (t (error "This should not happen"))))
  10184. ;;; Following file links
  10185. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10186. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10187. (declare-function mailcap-mime-info
  10188. "mailcap" (string &optional request no-decode))
  10189. (defvar org-wait nil)
  10190. (defun org-open-file (path &optional in-emacs line search)
  10191. "Open the file at PATH.
  10192. First, this expands any special file name abbreviations. Then the
  10193. configuration variable `org-file-apps' is checked if it contains an
  10194. entry for this file type, and if yes, the corresponding command is launched.
  10195. If no application is found, Emacs simply visits the file.
  10196. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10197. With a double \\[universal-argument] \\[universal-argument] \
  10198. prefix arg, Org tries to avoid opening in Emacs
  10199. and to use an external application to visit the file.
  10200. Optional LINE specifies a line to go to, optional SEARCH a string
  10201. to search for. If LINE or SEARCH is given, the file will be
  10202. opened in Emacs, unless an entry from org-file-apps that makes
  10203. use of groups in a regexp matches.
  10204. If you want to change the way frames are used when following a
  10205. link, please customize `org-link-frame-setup'.
  10206. If the file does not exist, an error is thrown."
  10207. (let* ((file (if (equal path "")
  10208. buffer-file-name
  10209. (substitute-in-file-name (expand-file-name path))))
  10210. (file-apps (append org-file-apps (org-default-apps)))
  10211. (apps (org-remove-if
  10212. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10213. (apps-dlink (org-remove-if-not
  10214. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10215. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10216. (dirp (if remp nil (file-directory-p file)))
  10217. (file (if (and dirp org-open-directory-means-index-dot-org)
  10218. (concat (file-name-as-directory file) "index.org")
  10219. file))
  10220. (a-m-a-p (assq 'auto-mode apps))
  10221. (dfile (downcase file))
  10222. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10223. (link (cond ((and (eq line nil)
  10224. (eq search nil))
  10225. file)
  10226. (line
  10227. (concat file "::" (number-to-string line)))
  10228. (search
  10229. (concat file "::" search))))
  10230. (dlink (downcase link))
  10231. (old-buffer (current-buffer))
  10232. (old-pos (point))
  10233. (old-mode major-mode)
  10234. ext cmd link-match-data)
  10235. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10236. (setq ext (match-string 1 dfile))
  10237. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10238. (setq ext (match-string 1 dfile))))
  10239. (cond
  10240. ((member in-emacs '((16) system))
  10241. (setq cmd (cdr (assoc 'system apps))))
  10242. (in-emacs (setq cmd 'emacs))
  10243. (t
  10244. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10245. (and dirp (cdr (assoc 'directory apps)))
  10246. ; first, try matching against apps-dlink
  10247. ; if we get a match here, store the match data for later
  10248. (let ((match (assoc-default dlink apps-dlink
  10249. 'string-match)))
  10250. (if match
  10251. (progn (setq link-match-data (match-data))
  10252. match)
  10253. (progn (setq in-emacs (or in-emacs line search))
  10254. nil))) ; if we have no match in apps-dlink,
  10255. ; always open the file in emacs if line or search
  10256. ; is given (for backwards compatibility)
  10257. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10258. 'string-match)
  10259. (cdr (assoc ext apps))
  10260. (cdr (assoc t apps))))))
  10261. (when (eq cmd 'system)
  10262. (setq cmd (cdr (assoc 'system apps))))
  10263. (when (eq cmd 'default)
  10264. (setq cmd (cdr (assoc t apps))))
  10265. (when (eq cmd 'mailcap)
  10266. (require 'mailcap)
  10267. (mailcap-parse-mailcaps)
  10268. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10269. (command (mailcap-mime-info mime-type)))
  10270. (if (stringp command)
  10271. (setq cmd command)
  10272. (setq cmd 'emacs))))
  10273. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10274. (not (file-exists-p file))
  10275. (not org-open-non-existing-files))
  10276. (user-error "No such file: %s" file))
  10277. (cond
  10278. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10279. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10280. (while (string-match "['\"]%s['\"]" cmd)
  10281. (setq cmd (replace-match "%s" t t cmd)))
  10282. (while (string-match "%s" cmd)
  10283. (setq cmd (replace-match
  10284. (save-match-data
  10285. (shell-quote-argument
  10286. (convert-standard-filename file)))
  10287. t t cmd)))
  10288. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10289. (save-match-data
  10290. (let ((match-index 1)
  10291. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10292. (set-match-data link-match-data)
  10293. (while (<= match-index number-of-groups)
  10294. (let ((regex (concat "%" (number-to-string match-index)))
  10295. (replace-with (match-string match-index dlink)))
  10296. (while (string-match regex cmd)
  10297. (setq cmd (replace-match replace-with t t cmd))))
  10298. (setq match-index (+ match-index 1)))))
  10299. (save-window-excursion
  10300. (message "Running %s...done" cmd)
  10301. (start-process-shell-command cmd nil cmd)
  10302. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10303. ((or (stringp cmd)
  10304. (eq cmd 'emacs))
  10305. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10306. (widen)
  10307. (if line (progn (org-goto-line line)
  10308. (if (derived-mode-p 'org-mode)
  10309. (org-reveal)))
  10310. (if search (org-link-search search))))
  10311. ((consp cmd)
  10312. (let ((file (convert-standard-filename file)))
  10313. (save-match-data
  10314. (set-match-data link-match-data)
  10315. (eval cmd))))
  10316. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10317. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10318. (or (not (equal old-buffer (current-buffer)))
  10319. (not (equal old-pos (point))))
  10320. (org-mark-ring-push old-pos old-buffer))))
  10321. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10322. "This function returns non-nil if `entry' uses a regular
  10323. expression which should be matched against the whole link by
  10324. org-open-file.
  10325. It assumes that is the case when the entry uses a regular
  10326. expression which has at least one grouping construct and the
  10327. action is either a lisp form or a command string containing
  10328. '%1', i.e. using at least one subexpression match as a
  10329. parameter."
  10330. (let ((selector (car entry))
  10331. (action (cdr entry)))
  10332. (if (stringp selector)
  10333. (and (> (regexp-opt-depth selector) 0)
  10334. (or (and (stringp action)
  10335. (string-match "%[0-9]" action))
  10336. (consp action)))
  10337. nil)))
  10338. (defun org-default-apps ()
  10339. "Return the default applications for this operating system."
  10340. (cond
  10341. ((eq system-type 'darwin)
  10342. org-file-apps-defaults-macosx)
  10343. ((eq system-type 'windows-nt)
  10344. org-file-apps-defaults-windowsnt)
  10345. (t org-file-apps-defaults-gnu)))
  10346. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10347. "Convert extensions to regular expressions in the cars of LIST.
  10348. Also, weed out any non-string entries, because the return value is used
  10349. only for regexp matching.
  10350. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10351. point to the symbol `emacs', indicating that the file should
  10352. be opened in Emacs."
  10353. (append
  10354. (delq nil
  10355. (mapcar (lambda (x)
  10356. (if (not (stringp (car x)))
  10357. nil
  10358. (if (string-match "\\W" (car x))
  10359. x
  10360. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10361. list))
  10362. (if add-auto-mode
  10363. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10364. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10365. (defun org-file-remote-p (file)
  10366. "Test whether FILE specifies a location on a remote system.
  10367. Return non-nil if the location is indeed remote.
  10368. For example, the filename \"/user@host:/foo\" specifies a location
  10369. on the system \"/user@host:\"."
  10370. (cond ((fboundp 'file-remote-p)
  10371. (file-remote-p file))
  10372. ((fboundp 'tramp-handle-file-remote-p)
  10373. (tramp-handle-file-remote-p file))
  10374. ((and (boundp 'ange-ftp-name-format)
  10375. (string-match (car ange-ftp-name-format) file))
  10376. t)))
  10377. ;;;; Refiling
  10378. (defun org-get-org-file ()
  10379. "Read a filename, with default directory `org-directory'."
  10380. (let ((default (or org-default-notes-file remember-data-file)))
  10381. (read-file-name (format "File name [%s]: " default)
  10382. (file-name-as-directory org-directory)
  10383. default)))
  10384. (defun org-notes-order-reversed-p ()
  10385. "Check if the current file should receive notes in reversed order."
  10386. (cond
  10387. ((not org-reverse-note-order) nil)
  10388. ((eq t org-reverse-note-order) t)
  10389. ((not (listp org-reverse-note-order)) nil)
  10390. (t (catch 'exit
  10391. (dolist (entry org-reverse-note-order)
  10392. (if (string-match (car entry) buffer-file-name)
  10393. (throw 'exit (cdr entry))))))))
  10394. (defvar org-refile-target-table nil
  10395. "The list of refile targets, created by `org-refile'.")
  10396. (defvar org-agenda-new-buffers nil
  10397. "Buffers created to visit agenda files.")
  10398. (defvar org-refile-cache nil
  10399. "Cache for refile targets.")
  10400. (defvar org-refile-markers nil
  10401. "All the markers used for caching refile locations.")
  10402. (defun org-refile-marker (pos)
  10403. "Get a new refile marker, but only if caching is in use."
  10404. (if (not org-refile-use-cache)
  10405. pos
  10406. (let ((m (make-marker)))
  10407. (move-marker m pos)
  10408. (push m org-refile-markers)
  10409. m)))
  10410. (defun org-refile-cache-clear ()
  10411. "Clear the refile cache and disable all the markers."
  10412. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10413. (setq org-refile-markers nil)
  10414. (setq org-refile-cache nil)
  10415. (message "Refile cache has been cleared"))
  10416. (defun org-refile-cache-check-set (set)
  10417. "Check if all the markers in the cache still have live buffers."
  10418. (let (marker)
  10419. (catch 'exit
  10420. (while (and set (setq marker (nth 3 (pop set))))
  10421. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10422. (when (and marker (null (marker-buffer marker)))
  10423. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10424. (sit-for 3)
  10425. (throw 'exit nil)))
  10426. t)))
  10427. (defun org-refile-cache-put (set &rest identifiers)
  10428. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10429. (let* ((key (sha1 (prin1-to-string identifiers)))
  10430. (entry (assoc key org-refile-cache)))
  10431. (if entry
  10432. (setcdr entry set)
  10433. (push (cons key set) org-refile-cache))))
  10434. (defun org-refile-cache-get (&rest identifiers)
  10435. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10436. (cond
  10437. ((not org-refile-cache) nil)
  10438. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10439. (t
  10440. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10441. org-refile-cache))))
  10442. (and set (org-refile-cache-check-set set) set)))))
  10443. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10444. "Produce a table with refile targets."
  10445. (let ((case-fold-search nil)
  10446. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10447. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10448. targets tgs txt re files desc descre fast-path-p level pos0)
  10449. (message "Getting targets...")
  10450. (with-current-buffer (or default-buffer (current-buffer))
  10451. (dolist (entry entries)
  10452. (setq files (car entry) desc (cdr entry))
  10453. (setq fast-path-p nil)
  10454. (cond
  10455. ((null files) (setq files (list (current-buffer))))
  10456. ((eq files 'org-agenda-files)
  10457. (setq files (org-agenda-files 'unrestricted)))
  10458. ((and (symbolp files) (fboundp files))
  10459. (setq files (funcall files)))
  10460. ((and (symbolp files) (boundp files))
  10461. (setq files (symbol-value files))))
  10462. (if (stringp files) (setq files (list files)))
  10463. (cond
  10464. ((eq (car desc) :tag)
  10465. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10466. ((eq (car desc) :todo)
  10467. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10468. ((eq (car desc) :regexp)
  10469. (setq descre (cdr desc)))
  10470. ((eq (car desc) :level)
  10471. (setq descre (concat "^\\*\\{" (number-to-string
  10472. (if org-odd-levels-only
  10473. (1- (* 2 (cdr desc)))
  10474. (cdr desc)))
  10475. "\\}[ \t]")))
  10476. ((eq (car desc) :maxlevel)
  10477. (setq fast-path-p t)
  10478. (setq descre (concat "^\\*\\{1," (number-to-string
  10479. (if org-odd-levels-only
  10480. (1- (* 2 (cdr desc)))
  10481. (cdr desc)))
  10482. "\\}[ \t]")))
  10483. (t (error "Bad refiling target description %s" desc)))
  10484. (dolist (f files)
  10485. (with-current-buffer
  10486. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10487. (or
  10488. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10489. (progn
  10490. (if (bufferp f) (setq f (buffer-file-name
  10491. (buffer-base-buffer f))))
  10492. (setq f (and f (expand-file-name f)))
  10493. (if (eq org-refile-use-outline-path 'file)
  10494. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10495. (save-excursion
  10496. (save-restriction
  10497. (widen)
  10498. (goto-char (point-min))
  10499. (while (re-search-forward descre nil t)
  10500. (goto-char (setq pos0 (point-at-bol)))
  10501. (catch 'next
  10502. (when org-refile-target-verify-function
  10503. (save-match-data
  10504. (or (funcall org-refile-target-verify-function)
  10505. (throw 'next t))))
  10506. (when (and (looking-at org-complex-heading-regexp)
  10507. (not (member (match-string 4) excluded-entries))
  10508. (match-string 4))
  10509. (setq level (org-reduced-level
  10510. (- (match-end 1) (match-beginning 1)))
  10511. txt (org-link-display-format (match-string 4))
  10512. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10513. re (format org-complex-heading-regexp-format
  10514. (regexp-quote (match-string 4))))
  10515. (when org-refile-use-outline-path
  10516. (setq txt (mapconcat
  10517. 'org-protect-slash
  10518. (append
  10519. (if (eq org-refile-use-outline-path
  10520. 'file)
  10521. (list (file-name-nondirectory
  10522. (buffer-file-name
  10523. (buffer-base-buffer))))
  10524. (if (eq org-refile-use-outline-path
  10525. 'full-file-path)
  10526. (list (buffer-file-name
  10527. (buffer-base-buffer)))))
  10528. (org-get-outline-path fast-path-p
  10529. level txt)
  10530. (list txt))
  10531. "/")))
  10532. (push (list txt f re (org-refile-marker (point)))
  10533. tgs)))
  10534. (when (= (point) pos0)
  10535. ;; verification function has not moved point
  10536. (goto-char (point-at-eol))))))))
  10537. (when org-refile-use-cache
  10538. (org-refile-cache-put tgs (buffer-file-name) descre))
  10539. (setq targets (append tgs targets))))))
  10540. (message "Getting targets...done")
  10541. (nreverse targets)))
  10542. (defun org-protect-slash (s)
  10543. (while (string-match "/" s)
  10544. (setq s (replace-match "\\" t t s)))
  10545. s)
  10546. (defvar org-olpa (make-vector 20 nil))
  10547. (defun org-get-outline-path (&optional fastp level heading)
  10548. "Return the outline path to the current entry, as a list.
  10549. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10550. routine which makes outline path derivations for an entire file,
  10551. avoiding backtracing. Refile target collection makes use of that."
  10552. (if fastp
  10553. (progn
  10554. (if (> level 19)
  10555. (error "Outline path failure, more than 19 levels"))
  10556. (loop for i from level upto 19 do
  10557. (aset org-olpa i nil))
  10558. (prog1
  10559. (delq nil (append org-olpa nil))
  10560. (aset org-olpa level heading)))
  10561. (let (rtn case-fold-search)
  10562. (save-excursion
  10563. (save-restriction
  10564. (widen)
  10565. (while (org-up-heading-safe)
  10566. (when (looking-at org-complex-heading-regexp)
  10567. (push (org-trim
  10568. (replace-regexp-in-string
  10569. ;; Remove statistical/checkboxes cookies
  10570. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10571. (org-match-string-no-properties 4)))
  10572. rtn)))
  10573. rtn)))))
  10574. (defun org-format-outline-path (path &optional width prefix separator)
  10575. "Format the outline path PATH for display.
  10576. WIDTH is the maximum number of characters that is available.
  10577. PREFIX is a prefix to be included in the returned string,
  10578. such as the file name.
  10579. SEPARATOR is inserted between the different parts of the path,
  10580. the default is \"/\"."
  10581. (setq width (or width 79))
  10582. (if prefix (setq width (- width (length prefix))))
  10583. (if (not path)
  10584. (or prefix "")
  10585. (let* ((nsteps (length path))
  10586. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10587. (maxwidth (if (<= total-width width)
  10588. 10000 ;; everything fits
  10589. ;; we need to shorten the level headings
  10590. (/ (- width nsteps) nsteps)))
  10591. (org-odd-levels-only nil)
  10592. (n 0)
  10593. (total (1+ (length prefix))))
  10594. (setq maxwidth (max maxwidth 10))
  10595. (concat prefix
  10596. (if prefix (or separator "/"))
  10597. (mapconcat
  10598. (lambda (h)
  10599. (setq n (1+ n))
  10600. (if (and (= n nsteps) (< maxwidth 10000))
  10601. (setq maxwidth (- total-width total)))
  10602. (if (< (length h) maxwidth)
  10603. (progn (setq total (+ total (length h) 1)) h)
  10604. (setq h (substring h 0 (- maxwidth 2))
  10605. total (+ total maxwidth 1))
  10606. (if (string-match "[ \t]+\\'" h)
  10607. (setq h (substring h 0 (match-beginning 0))))
  10608. (setq h (concat h "..")))
  10609. (org-add-props h nil 'face
  10610. (nth (% (1- n) org-n-level-faces)
  10611. org-level-faces))
  10612. h)
  10613. path (or separator "/"))))))
  10614. (defun org-display-outline-path (&optional file current separator just-return-string)
  10615. "Display the current outline path in the echo area.
  10616. If FILE is non-nil, prepend the output with the file name.
  10617. If CURRENT is non-nil, append the current heading to the output.
  10618. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10619. the different parts of the path and defaults to \"/\".
  10620. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10621. (interactive "P")
  10622. (let* (case-fold-search
  10623. (bfn (buffer-file-name (buffer-base-buffer)))
  10624. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10625. res)
  10626. (if current (setq path (append path
  10627. (save-excursion
  10628. (org-back-to-heading t)
  10629. (if (looking-at org-complex-heading-regexp)
  10630. (list (match-string 4)))))))
  10631. (setq res
  10632. (org-format-outline-path
  10633. path
  10634. (1- (frame-width))
  10635. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10636. separator))
  10637. (if just-return-string
  10638. (org-no-properties res)
  10639. (org-unlogged-message "%s" res))))
  10640. (defvar org-refile-history nil
  10641. "History for refiling operations.")
  10642. (defvar org-after-refile-insert-hook nil
  10643. "Hook run after `org-refile' has inserted its stuff at the new location.
  10644. Note that this is still *before* the stuff will be removed from
  10645. the *old* location.")
  10646. (defvar org-capture-last-stored-marker)
  10647. (defvar org-refile-keep nil
  10648. "Non-nil means `org-refile' will copy instead of refile.")
  10649. (defun org-copy ()
  10650. "Like `org-refile', but copy."
  10651. (interactive)
  10652. (let ((org-refile-keep t))
  10653. (funcall 'org-refile nil nil nil "Copy")))
  10654. (defun org-refile (&optional arg default-buffer rfloc msg)
  10655. "Move the entry or entries at point to another heading.
  10656. The list of target headings is compiled using the information in
  10657. `org-refile-targets', which see.
  10658. At the target location, the entry is filed as a subitem of the
  10659. target heading. Depending on `org-reverse-note-order', the new
  10660. subitem will either be the first or the last subitem.
  10661. If there is an active region, all entries in that region will be
  10662. refiled. However, the region must fulfill the requirement that
  10663. the first heading sets the top-level of the moved text.
  10664. With prefix arg ARG, the command will only visit the target
  10665. location and not actually move anything.
  10666. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10667. refiling operation has put the subtree.
  10668. With a numeric prefix argument of `2', refile to the running clock.
  10669. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10670. being set to t and copy to the target location, don't move it.
  10671. Beware that keeping refiled entries may result in duplicated ID
  10672. properties.
  10673. RFLOC can be a refile location obtained in a different way.
  10674. MSG is a string to replace \"Refile\" in the default prompt with
  10675. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10676. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10677. If you are using target caching (see `org-refile-use-cache'), you
  10678. have to clear the target cache in order to find new targets.
  10679. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10680. prefix argument (`C-u C-u C-u C-c C-w')."
  10681. (interactive "P")
  10682. (if (member arg '(0 (64)))
  10683. (org-refile-cache-clear)
  10684. (let* ((actionmsg (cond (msg msg)
  10685. ((equal arg 3) "Refile (and keep)")
  10686. (t "Refile")))
  10687. (cbuf (current-buffer))
  10688. (regionp (org-region-active-p))
  10689. (region-start (and regionp (region-beginning)))
  10690. (region-end (and regionp (region-end)))
  10691. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10692. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10693. pos it nbuf file re level reversed)
  10694. (setq last-command nil)
  10695. (when regionp
  10696. (goto-char region-start)
  10697. (or (bolp) (goto-char (point-at-bol)))
  10698. (setq region-start (point))
  10699. (unless (or (org-kill-is-subtree-p
  10700. (buffer-substring region-start region-end))
  10701. (prog1 org-refile-active-region-within-subtree
  10702. (let ((s (point-at-eol)))
  10703. (org-toggle-heading)
  10704. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10705. (user-error "The region is not a (sequence of) subtree(s)")))
  10706. (if (equal arg '(16))
  10707. (org-refile-goto-last-stored)
  10708. (when (or
  10709. (and (equal arg 2)
  10710. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10711. (prog1
  10712. (setq it (list (or org-clock-heading "running clock")
  10713. (buffer-file-name
  10714. (marker-buffer org-clock-hd-marker))
  10715. ""
  10716. (marker-position org-clock-hd-marker)))
  10717. (setq arg nil)))
  10718. (setq it (or rfloc
  10719. (let (heading-text)
  10720. (save-excursion
  10721. (unless (and arg (listp arg))
  10722. (org-back-to-heading t)
  10723. (setq heading-text
  10724. (replace-regexp-in-string
  10725. org-bracket-link-regexp
  10726. "\\3"
  10727. (nth 4 (org-heading-components)))))
  10728. (org-refile-get-location
  10729. (cond ((and arg (listp arg)) "Goto")
  10730. (regionp (concat actionmsg " region to"))
  10731. (t (concat actionmsg " subtree \""
  10732. heading-text "\" to")))
  10733. default-buffer
  10734. (and (not (equal '(4) arg))
  10735. org-refile-allow-creating-parent-nodes)
  10736. arg))))))
  10737. (setq file (nth 1 it)
  10738. re (nth 2 it)
  10739. pos (nth 3 it))
  10740. (if (and (not arg)
  10741. pos
  10742. (equal (buffer-file-name) file)
  10743. (if regionp
  10744. (and (>= pos region-start)
  10745. (<= pos region-end))
  10746. (and (>= pos (point))
  10747. (< pos (save-excursion
  10748. (org-end-of-subtree t t))))))
  10749. (error "Cannot refile to position inside the tree or region"))
  10750. (setq nbuf (or (find-buffer-visiting file)
  10751. (find-file-noselect file)))
  10752. (if (and arg (not (equal arg 3)))
  10753. (progn
  10754. (org-pop-to-buffer-same-window nbuf)
  10755. (goto-char pos)
  10756. (org-show-context 'org-goto))
  10757. (if regionp
  10758. (progn
  10759. (org-kill-new (buffer-substring region-start region-end))
  10760. (org-save-markers-in-region region-start region-end))
  10761. (org-copy-subtree 1 nil t))
  10762. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10763. (find-file-noselect file)))
  10764. (setq reversed (org-notes-order-reversed-p))
  10765. (save-excursion
  10766. (save-restriction
  10767. (widen)
  10768. (if pos
  10769. (progn
  10770. (goto-char pos)
  10771. (looking-at org-outline-regexp)
  10772. (setq level (org-get-valid-level (funcall outline-level) 1))
  10773. (goto-char
  10774. (if reversed
  10775. (or (outline-next-heading) (point-max))
  10776. (or (save-excursion (org-get-next-sibling))
  10777. (org-end-of-subtree t t)
  10778. (point-max)))))
  10779. (setq level 1)
  10780. (if (not reversed)
  10781. (goto-char (point-max))
  10782. (goto-char (point-min))
  10783. (or (outline-next-heading) (goto-char (point-max)))))
  10784. (if (not (bolp)) (newline))
  10785. (org-paste-subtree level nil nil t)
  10786. (when org-log-refile
  10787. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10788. (unless (eq org-log-refile 'note)
  10789. (save-excursion (org-add-log-note))))
  10790. (and org-auto-align-tags
  10791. (let ((org-loop-over-headlines-in-active-region nil))
  10792. (org-set-tags nil t)))
  10793. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10794. :last-refile)))
  10795. (when bookmark-name
  10796. (with-demoted-errors
  10797. (bookmark-set bookmark-name))))
  10798. ;; If we are refiling for capture, make sure that the
  10799. ;; last-capture pointers point here
  10800. (when (org-bound-and-true-p org-refile-for-capture)
  10801. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10802. :last-capture-marker)))
  10803. (when bookmark-name
  10804. (with-demoted-errors
  10805. (bookmark-set bookmark-name))))
  10806. (move-marker org-capture-last-stored-marker (point)))
  10807. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10808. (run-hooks 'org-after-refile-insert-hook))))
  10809. (unless org-refile-keep
  10810. (if regionp
  10811. (delete-region (point) (+ (point) (- region-end region-start)))
  10812. (delete-region
  10813. (and (org-back-to-heading t) (point))
  10814. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10815. (when (featurep 'org-inlinetask)
  10816. (org-inlinetask-remove-END-maybe))
  10817. (setq org-markers-to-move nil)
  10818. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10819. (defun org-refile-goto-last-stored ()
  10820. "Go to the location where the last refile was stored."
  10821. (interactive)
  10822. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10823. (message "This is the location of the last refile"))
  10824. (defun org-refile--get-location (refloc tbl)
  10825. "When user refile to REFLOC, find the associated target in TBL.
  10826. Also check `org-refile-target-table'."
  10827. (car (delq
  10828. nil
  10829. (mapcar
  10830. (lambda (r) (or (assoc r tbl)
  10831. (assoc r org-refile-target-table)))
  10832. (list (replace-regexp-in-string "/$" "" refloc)
  10833. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10834. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10835. no-exclude)
  10836. "Prompt the user for a refile location, using PROMPT.
  10837. PROMPT should not be suffixed with a colon and a space, because
  10838. this function appends the default value from
  10839. `org-refile-history' automatically, if that is not empty.
  10840. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10841. this is used for the GOTO interface."
  10842. (let ((org-refile-targets org-refile-targets)
  10843. (org-refile-use-outline-path org-refile-use-outline-path)
  10844. excluded-entries)
  10845. (when (and (derived-mode-p 'org-mode)
  10846. (not org-refile-use-cache)
  10847. (not no-exclude))
  10848. (org-map-tree
  10849. (lambda()
  10850. (setq excluded-entries
  10851. (append excluded-entries (list (org-get-heading t t)))))))
  10852. (setq org-refile-target-table
  10853. (org-refile-get-targets default-buffer excluded-entries)))
  10854. (unless org-refile-target-table
  10855. (user-error "No refile targets"))
  10856. (let* ((cbuf (current-buffer))
  10857. (partial-completion-mode nil)
  10858. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10859. (cfunc (if (and org-refile-use-outline-path
  10860. org-outline-path-complete-in-steps)
  10861. 'org-olpath-completing-read
  10862. 'org-icompleting-read))
  10863. (extra (if org-refile-use-outline-path "/" ""))
  10864. (cbnex (concat (buffer-name) extra))
  10865. (filename (and cfn (expand-file-name cfn)))
  10866. (tbl (mapcar
  10867. (lambda (x)
  10868. (if (and (not (member org-refile-use-outline-path
  10869. '(file full-file-path)))
  10870. (not (equal filename (nth 1 x))))
  10871. (cons (concat (car x) extra " ("
  10872. (file-name-nondirectory (nth 1 x)) ")")
  10873. (cdr x))
  10874. (cons (concat (car x) extra) (cdr x))))
  10875. org-refile-target-table))
  10876. (completion-ignore-case t)
  10877. cdef
  10878. (prompt (concat prompt
  10879. (or (and (car org-refile-history)
  10880. (concat " (default " (car org-refile-history) ")"))
  10881. (and (assoc cbnex tbl) (setq cdef cbnex)
  10882. (concat " (default " cbnex ")"))) ": "))
  10883. pa answ parent-target child parent old-hist)
  10884. (setq old-hist org-refile-history)
  10885. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10886. nil 'org-refile-history (or cdef (car org-refile-history))))
  10887. (if (setq pa (org-refile--get-location answ tbl))
  10888. (progn
  10889. (org-refile-check-position pa)
  10890. (when (or (not org-refile-history)
  10891. (not (eq old-hist org-refile-history))
  10892. (not (equal (car pa) (car org-refile-history))))
  10893. (setq org-refile-history
  10894. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10895. org-refile-history
  10896. (cdr org-refile-history))))
  10897. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10898. (pop org-refile-history)))
  10899. pa)
  10900. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10901. (progn
  10902. (setq parent (match-string 1 answ)
  10903. child (match-string 2 answ))
  10904. (setq parent-target (org-refile--get-location parent tbl))
  10905. (when (and parent-target
  10906. (or (eq new-nodes t)
  10907. (and (eq new-nodes 'confirm)
  10908. (y-or-n-p (format "Create new node \"%s\"? "
  10909. child)))))
  10910. (org-refile-new-child parent-target child)))
  10911. (user-error "Invalid target location")))))
  10912. (declare-function org-string-nw-p "org-macs" (s))
  10913. (defun org-refile-check-position (refile-pointer)
  10914. "Check if the refile pointer matches the headline to which it points."
  10915. (let* ((file (nth 1 refile-pointer))
  10916. (re (nth 2 refile-pointer))
  10917. (pos (nth 3 refile-pointer))
  10918. buffer)
  10919. (if (and (not (markerp pos)) (not file))
  10920. (user-error "Please indicate a target file in the refile path")
  10921. (when (org-string-nw-p re)
  10922. (setq buffer (if (markerp pos)
  10923. (marker-buffer pos)
  10924. (or (find-buffer-visiting file)
  10925. (find-file-noselect file))))
  10926. (with-current-buffer buffer
  10927. (save-excursion
  10928. (save-restriction
  10929. (widen)
  10930. (goto-char pos)
  10931. (beginning-of-line 1)
  10932. (unless (org-looking-at-p re)
  10933. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10934. (defun org-refile-new-child (parent-target child)
  10935. "Use refile target PARENT-TARGET to add new CHILD below it."
  10936. (unless parent-target
  10937. (error "Cannot find parent for new node"))
  10938. (let ((file (nth 1 parent-target))
  10939. (pos (nth 3 parent-target))
  10940. level)
  10941. (with-current-buffer (or (find-buffer-visiting file)
  10942. (find-file-noselect file))
  10943. (save-excursion
  10944. (save-restriction
  10945. (widen)
  10946. (if pos
  10947. (goto-char pos)
  10948. (goto-char (point-max))
  10949. (if (not (bolp)) (newline)))
  10950. (when (looking-at org-outline-regexp)
  10951. (setq level (funcall outline-level))
  10952. (org-end-of-subtree t t))
  10953. (org-back-over-empty-lines)
  10954. (insert "\n" (make-string
  10955. (if pos (org-get-valid-level level 1) 1) ?*)
  10956. " " child "\n")
  10957. (beginning-of-line 0)
  10958. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10959. (defun org-olpath-completing-read (prompt collection &rest args)
  10960. "Read an outline path like a file name."
  10961. (let ((thetable collection)
  10962. (org-completion-use-ido nil) ; does not work with ido.
  10963. (org-completion-use-iswitchb nil)) ; or iswitchb
  10964. (apply #'org-icompleting-read
  10965. prompt
  10966. (lambda (string predicate &optional flag)
  10967. (cond
  10968. ((eq flag nil) (try-completion string thetable))
  10969. ((eq flag t)
  10970. (let ((l (length string)))
  10971. (mapcar (lambda (x)
  10972. (let ((r (substring x l))
  10973. (f (if (string-match " ([^)]*)$" x)
  10974. (match-string 0 x)
  10975. "")))
  10976. (if (string-match "/" r)
  10977. (concat string (substring r 0 (match-end 0)) f)
  10978. x)))
  10979. (all-completions string thetable predicate))))
  10980. ;; Exact match?
  10981. ((eq flag 'lambda) (assoc string thetable))))
  10982. args)))
  10983. ;;;; Dynamic blocks
  10984. (defun org-find-dblock (name)
  10985. "Find the first dynamic block with name NAME in the buffer.
  10986. If not found, stay at current position and return nil."
  10987. (let ((case-fold-search t) pos)
  10988. (save-excursion
  10989. (goto-char (point-min))
  10990. (setq pos (and (re-search-forward
  10991. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10992. (match-beginning 0))))
  10993. (if pos (goto-char pos))
  10994. pos))
  10995. (defun org-create-dblock (plist)
  10996. "Create a dynamic block section, with parameters taken from PLIST.
  10997. PLIST must contain a :name entry which is used as the name of the block."
  10998. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10999. (end-of-line 1)
  11000. (newline))
  11001. (let ((col (current-column))
  11002. (name (plist-get plist :name)))
  11003. (insert "#+BEGIN: " name)
  11004. (while plist
  11005. (if (eq (car plist) :name)
  11006. (setq plist (cddr plist))
  11007. (insert " " (prin1-to-string (pop plist)))))
  11008. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11009. (beginning-of-line -2)))
  11010. (defun org-prepare-dblock ()
  11011. "Prepare dynamic block for refresh.
  11012. This empties the block, puts the cursor at the insert position and returns
  11013. the property list including an extra property :name with the block name."
  11014. (unless (looking-at org-dblock-start-re)
  11015. (user-error "Not at a dynamic block"))
  11016. (let* ((begdel (1+ (match-end 0)))
  11017. (name (org-no-properties (match-string 1)))
  11018. (params (append (list :name name)
  11019. (read (concat "(" (match-string 3) ")")))))
  11020. (save-excursion
  11021. (beginning-of-line 1)
  11022. (skip-chars-forward " \t")
  11023. (setq params (plist-put params :indentation-column (current-column))))
  11024. (unless (re-search-forward org-dblock-end-re nil t)
  11025. (error "Dynamic block not terminated"))
  11026. (setq params
  11027. (append params
  11028. (list :content (buffer-substring
  11029. begdel (match-beginning 0)))))
  11030. (delete-region begdel (match-beginning 0))
  11031. (goto-char begdel)
  11032. (open-line 1)
  11033. params))
  11034. (defun org-map-dblocks (&optional command)
  11035. "Apply COMMAND to all dynamic blocks in the current buffer.
  11036. If COMMAND is not given, use `org-update-dblock'."
  11037. (let ((cmd (or command 'org-update-dblock)))
  11038. (save-excursion
  11039. (goto-char (point-min))
  11040. (while (re-search-forward org-dblock-start-re nil t)
  11041. (goto-char (match-beginning 0))
  11042. (save-excursion
  11043. (condition-case nil
  11044. (funcall cmd)
  11045. (error (message "Error during update of dynamic block"))))
  11046. (unless (re-search-forward org-dblock-end-re nil t)
  11047. (error "Dynamic block not terminated"))))))
  11048. (defun org-dblock-update (&optional arg)
  11049. "User command for updating dynamic blocks.
  11050. Update the dynamic block at point. With prefix ARG, update all dynamic
  11051. blocks in the buffer."
  11052. (interactive "P")
  11053. (if arg
  11054. (org-update-all-dblocks)
  11055. (or (looking-at org-dblock-start-re)
  11056. (org-beginning-of-dblock))
  11057. (org-update-dblock)))
  11058. (defun org-update-dblock ()
  11059. "Update the dynamic block at point.
  11060. This means to empty the block, parse for parameters and then call
  11061. the correct writing function."
  11062. (interactive)
  11063. (save-excursion
  11064. (let* ((win (selected-window))
  11065. (pos (point))
  11066. (line (org-current-line))
  11067. (params (org-prepare-dblock))
  11068. (name (plist-get params :name))
  11069. (indent (plist-get params :indentation-column))
  11070. (cmd (intern (concat "org-dblock-write:" name))))
  11071. (message "Updating dynamic block `%s' at line %d..." name line)
  11072. (funcall cmd params)
  11073. (message "Updating dynamic block `%s' at line %d...done" name line)
  11074. (goto-char pos)
  11075. (when (and indent (> indent 0))
  11076. (setq indent (make-string indent ?\ ))
  11077. (save-excursion
  11078. (select-window win)
  11079. (org-beginning-of-dblock)
  11080. (forward-line 1)
  11081. (while (not (looking-at org-dblock-end-re))
  11082. (insert indent)
  11083. (beginning-of-line 2))
  11084. (when (looking-at org-dblock-end-re)
  11085. (and (looking-at "[ \t]+")
  11086. (replace-match ""))
  11087. (insert indent)))))))
  11088. (defun org-beginning-of-dblock ()
  11089. "Find the beginning of the dynamic block at point.
  11090. Error if there is no such block at point."
  11091. (let ((pos (point))
  11092. beg)
  11093. (end-of-line 1)
  11094. (if (and (re-search-backward org-dblock-start-re nil t)
  11095. (setq beg (match-beginning 0))
  11096. (re-search-forward org-dblock-end-re nil t)
  11097. (> (match-end 0) pos))
  11098. (goto-char beg)
  11099. (goto-char pos)
  11100. (error "Not in a dynamic block"))))
  11101. (defun org-update-all-dblocks ()
  11102. "Update all dynamic blocks in the buffer.
  11103. This function can be used in a hook."
  11104. (interactive)
  11105. (when (derived-mode-p 'org-mode)
  11106. (org-map-dblocks 'org-update-dblock)))
  11107. ;;;; Completion
  11108. (declare-function org-export-backend-name "org-export" (cl-x))
  11109. (declare-function org-export-backend-options "org-export" (cl-x))
  11110. (defun org-get-export-keywords ()
  11111. "Return a list of all currently understood export keywords.
  11112. Export keywords include options, block names, attributes and
  11113. keywords relative to each registered export back-end."
  11114. (let (keywords)
  11115. (dolist (backend
  11116. (org-bound-and-true-p org-export-registered-backends)
  11117. (delq nil keywords))
  11118. ;; Back-end name (for keywords, like #+LATEX:)
  11119. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11120. (dolist (option-entry (org-export-backend-options backend))
  11121. ;; Back-end options.
  11122. (push (nth 1 option-entry) keywords)))))
  11123. (defconst org-options-keywords
  11124. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11125. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11126. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11127. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11128. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11129. (defcustom org-structure-template-alist
  11130. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11131. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11132. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11133. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11134. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11135. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11136. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11137. ("L" "#+LaTeX: ")
  11138. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11139. ("H" "#+HTML: ")
  11140. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11141. ("A" "#+ASCII: ")
  11142. ("i" "#+INDEX: ?")
  11143. ("I" "#+INCLUDE: %file ?"))
  11144. "Structure completion elements.
  11145. This is a list of abbreviation keys and values. The value gets inserted
  11146. if you type `<' followed by the key and then press the completion key,
  11147. usually `TAB'. %file will be replaced by a file name after prompting
  11148. for the file using completion. The cursor will be placed at the position
  11149. of the `?' in the template.
  11150. There are two templates for each key, the first uses the original Org syntax,
  11151. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11152. the default when the /org-mtags.el/ module has been loaded. See also the
  11153. variable `org-mtags-prefer-muse-templates'."
  11154. :group 'org-completion
  11155. :type '(repeat
  11156. (list
  11157. (string :tag "Key")
  11158. (string :tag "Template")))
  11159. :version "25.1"
  11160. :package-version '(Org . "8.3"))
  11161. (defun org-try-structure-completion ()
  11162. "Try to complete a structure template before point.
  11163. This looks for strings like \"<e\" on an otherwise empty line and
  11164. expands them."
  11165. (let ((l (buffer-substring (point-at-bol) (point)))
  11166. a)
  11167. (when (and (looking-at "[ \t]*$")
  11168. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11169. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11170. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11171. (match-beginning 1)) a)
  11172. t)))
  11173. (defun org-complete-expand-structure-template (start cell)
  11174. "Expand a structure template."
  11175. (let ((rpl (nth 1 cell))
  11176. (ind ""))
  11177. (delete-region start (point))
  11178. (when (string-match "\\`[ \t]*#\\+" rpl)
  11179. (cond
  11180. ((bolp))
  11181. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11182. (setq ind (buffer-substring (point-at-bol) (point))))
  11183. (t (newline))))
  11184. (setq start (point))
  11185. (if (string-match "%file" rpl)
  11186. (setq rpl (replace-match
  11187. (concat
  11188. "\""
  11189. (save-match-data
  11190. (abbreviate-file-name (read-file-name "Include file: ")))
  11191. "\"")
  11192. t t rpl)))
  11193. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11194. (concat "\n" ind)))
  11195. (insert rpl)
  11196. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11197. ;;;; TODO, DEADLINE, Comments
  11198. (defun org-toggle-comment ()
  11199. "Change the COMMENT state of an entry."
  11200. (interactive)
  11201. (save-excursion
  11202. (org-back-to-heading)
  11203. (looking-at org-complex-heading-regexp)
  11204. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11205. (skip-chars-forward " \t")
  11206. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11207. (if (org-in-commented-heading-p t)
  11208. (delete-region (point)
  11209. (progn (search-forward " " (line-end-position) 'move)
  11210. (skip-chars-forward " \t")
  11211. (point)))
  11212. (insert org-comment-string)
  11213. (unless (eolp) (insert " ")))))
  11214. (defvar org-last-todo-state-is-todo nil
  11215. "This is non-nil when the last TODO state change led to a TODO state.
  11216. If the last change removed the TODO tag or switched to DONE, then
  11217. this is nil.")
  11218. (defvar org-setting-tags nil) ; dynamically skipped
  11219. (defvar org-todo-setup-filter-hook nil
  11220. "Hook for functions that pre-filter todo specs.
  11221. Each function takes a todo spec and returns either nil or the spec
  11222. transformed into canonical form." )
  11223. (defvar org-todo-get-default-hook nil
  11224. "Hook for functions that get a default item for todo.
  11225. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11226. nil or a string to be used for the todo mark." )
  11227. (defvar org-agenda-headline-snapshot-before-repeat)
  11228. (defun org-current-effective-time ()
  11229. "Return current time adjusted for `org-extend-today-until' variable."
  11230. (let* ((ct (org-current-time))
  11231. (dct (decode-time ct))
  11232. (ct1
  11233. (cond
  11234. (org-use-last-clock-out-time-as-effective-time
  11235. (or (org-clock-get-last-clock-out-time) ct))
  11236. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11237. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11238. (t ct))))
  11239. ct1))
  11240. (defun org-todo-yesterday (&optional arg)
  11241. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11242. (interactive "P")
  11243. (if (eq major-mode 'org-agenda-mode)
  11244. (apply 'org-agenda-todo-yesterday arg)
  11245. (let* ((org-use-effective-time t)
  11246. (hour (third (decode-time
  11247. (org-current-time))))
  11248. (org-extend-today-until (1+ hour)))
  11249. (org-todo arg))))
  11250. (defvar org-block-entry-blocking ""
  11251. "First entry preventing the TODO state change.")
  11252. (defun org-cancel-repeater ()
  11253. "Cancel a repeater by setting its numeric value to zero."
  11254. (interactive)
  11255. (save-excursion
  11256. (org-back-to-heading t)
  11257. (let ((bound1 (point))
  11258. (bound0 (save-excursion (outline-next-heading) (point))))
  11259. (when (re-search-forward
  11260. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11261. org-deadline-time-regexp "\\)\\|\\("
  11262. org-ts-regexp "\\)")
  11263. bound0 t)
  11264. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11265. (replace-match "0" t nil nil 1))))))
  11266. (defun org-todo (&optional arg)
  11267. "Change the TODO state of an item.
  11268. The state of an item is given by a keyword at the start of the heading,
  11269. like
  11270. *** TODO Write paper
  11271. *** DONE Call mom
  11272. The different keywords are specified in the variable `org-todo-keywords'.
  11273. By default the available states are \"TODO\" and \"DONE\".
  11274. So for this example: when the item starts with TODO, it is changed to DONE.
  11275. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11276. DONE are present, add TODO at the beginning of the heading.
  11277. With \\[universal-argument] prefix arg, use completion to determine the new \
  11278. state.
  11279. With numeric prefix arg, switch to that state.
  11280. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11281. keywords (nextset).
  11282. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11283. With a numeric prefix arg of 0, inhibit note taking for the change.
  11284. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11285. When called through ELisp, arg is also interpreted in the following way:
  11286. 'none -> empty state
  11287. \"\"(empty string) -> switch to empty state
  11288. 'done -> switch to DONE
  11289. 'nextset -> switch to the next set of keywords
  11290. 'previousset -> switch to the previous set of keywords
  11291. \"WAITING\" -> switch to the specified keyword, but only if it
  11292. really is a member of `org-todo-keywords'."
  11293. (interactive "P")
  11294. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11295. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11296. 'region-start-level 'region))
  11297. org-loop-over-headlines-in-active-region)
  11298. (org-map-entries
  11299. `(org-todo ,arg)
  11300. org-loop-over-headlines-in-active-region
  11301. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11302. (if (equal arg '(16)) (setq arg 'nextset))
  11303. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11304. (let ((org-blocker-hook org-blocker-hook)
  11305. commentp
  11306. case-fold-search)
  11307. (when (equal arg '(64))
  11308. (setq arg nil org-blocker-hook nil))
  11309. (when (and org-blocker-hook
  11310. (or org-inhibit-blocking
  11311. (org-entry-get nil "NOBLOCKING")))
  11312. (setq org-blocker-hook nil))
  11313. (save-excursion
  11314. (catch 'exit
  11315. (org-back-to-heading t)
  11316. (when (org-in-commented-heading-p t)
  11317. (org-toggle-comment)
  11318. (setq commentp t))
  11319. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11320. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11321. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11322. (let* ((match-data (match-data))
  11323. (startpos (point-at-bol))
  11324. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11325. (org-log-done org-log-done)
  11326. (org-log-repeat org-log-repeat)
  11327. (org-todo-log-states org-todo-log-states)
  11328. (org-inhibit-logging
  11329. (if (equal arg 0)
  11330. (progn (setq arg nil) 'note) org-inhibit-logging))
  11331. (this (match-string 1))
  11332. (hl-pos (match-beginning 0))
  11333. (head (org-get-todo-sequence-head this))
  11334. (ass (assoc head org-todo-kwd-alist))
  11335. (interpret (nth 1 ass))
  11336. (done-word (nth 3 ass))
  11337. (final-done-word (nth 4 ass))
  11338. (org-last-state (or this ""))
  11339. (completion-ignore-case t)
  11340. (member (member this org-todo-keywords-1))
  11341. (tail (cdr member))
  11342. (org-state (cond
  11343. ((and org-todo-key-trigger
  11344. (or (and (equal arg '(4))
  11345. (eq org-use-fast-todo-selection 'prefix))
  11346. (and (not arg) org-use-fast-todo-selection
  11347. (not (eq org-use-fast-todo-selection
  11348. 'prefix)))))
  11349. ;; Use fast selection
  11350. (org-fast-todo-selection))
  11351. ((and (equal arg '(4))
  11352. (or (not org-use-fast-todo-selection)
  11353. (not org-todo-key-trigger)))
  11354. ;; Read a state with completion
  11355. (org-icompleting-read
  11356. "State: " (mapcar 'list org-todo-keywords-1)
  11357. nil t))
  11358. ((eq arg 'right)
  11359. (if this
  11360. (if tail (car tail) nil)
  11361. (car org-todo-keywords-1)))
  11362. ((eq arg 'left)
  11363. (if (equal member org-todo-keywords-1)
  11364. nil
  11365. (if this
  11366. (nth (- (length org-todo-keywords-1)
  11367. (length tail) 2)
  11368. org-todo-keywords-1)
  11369. (org-last org-todo-keywords-1))))
  11370. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11371. (setq arg nil))) ; hack to fall back to cycling
  11372. (arg
  11373. ;; user or caller requests a specific state
  11374. (cond
  11375. ((equal arg "") nil)
  11376. ((eq arg 'none) nil)
  11377. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11378. ((eq arg 'nextset)
  11379. (or (car (cdr (member head org-todo-heads)))
  11380. (car org-todo-heads)))
  11381. ((eq arg 'previousset)
  11382. (let ((org-todo-heads (reverse org-todo-heads)))
  11383. (or (car (cdr (member head org-todo-heads)))
  11384. (car org-todo-heads))))
  11385. ((car (member arg org-todo-keywords-1)))
  11386. ((stringp arg)
  11387. (user-error "State `%s' not valid in this file" arg))
  11388. ((nth (1- (prefix-numeric-value arg))
  11389. org-todo-keywords-1))))
  11390. ((null member) (or head (car org-todo-keywords-1)))
  11391. ((equal this final-done-word) nil) ;; -> make empty
  11392. ((null tail) nil) ;; -> first entry
  11393. ((memq interpret '(type priority))
  11394. (if (eq this-command last-command)
  11395. (car tail)
  11396. (if (> (length tail) 0)
  11397. (or done-word (car org-done-keywords))
  11398. nil)))
  11399. (t
  11400. (car tail))))
  11401. (org-state (or
  11402. (run-hook-with-args-until-success
  11403. 'org-todo-get-default-hook org-state org-last-state)
  11404. org-state))
  11405. (next (if org-state (concat " " org-state " ") " "))
  11406. (change-plist (list :type 'todo-state-change :from this :to org-state
  11407. :position startpos))
  11408. dolog now-done-p)
  11409. (when org-blocker-hook
  11410. (setq org-last-todo-state-is-todo
  11411. (not (member this org-done-keywords)))
  11412. (unless (save-excursion
  11413. (save-match-data
  11414. (org-with-wide-buffer
  11415. (run-hook-with-args-until-failure
  11416. 'org-blocker-hook change-plist))))
  11417. (if (org-called-interactively-p 'interactive)
  11418. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11419. this org-state org-block-entry-blocking)
  11420. ;; fail silently
  11421. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11422. this org-state org-block-entry-blocking)
  11423. (throw 'exit nil))))
  11424. (store-match-data match-data)
  11425. (replace-match next t t)
  11426. (cond ((equal this org-state)
  11427. (message "TODO state was already %s" (org-trim next)))
  11428. ((pos-visible-in-window-p hl-pos)
  11429. (message "TODO state changed to %s" (org-trim next))))
  11430. (unless head
  11431. (setq head (org-get-todo-sequence-head org-state)
  11432. ass (assoc head org-todo-kwd-alist)
  11433. interpret (nth 1 ass)
  11434. done-word (nth 3 ass)
  11435. final-done-word (nth 4 ass)))
  11436. (when (memq arg '(nextset previousset))
  11437. (message "Keyword-Set %d/%d: %s"
  11438. (- (length org-todo-sets) -1
  11439. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11440. (length org-todo-sets)
  11441. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11442. (setq org-last-todo-state-is-todo
  11443. (not (member org-state org-done-keywords)))
  11444. (setq now-done-p (and (member org-state org-done-keywords)
  11445. (not (member this org-done-keywords))))
  11446. (and logging (org-local-logging logging))
  11447. (when (and (or org-todo-log-states org-log-done)
  11448. (not (eq org-inhibit-logging t))
  11449. (not (memq arg '(nextset previousset))))
  11450. ;; we need to look at recording a time and note
  11451. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11452. (nth 2 (assoc this org-todo-log-states))))
  11453. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11454. (setq dolog 'time))
  11455. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11456. (and org-state
  11457. (member org-state org-not-done-keywords)
  11458. (not (member this org-not-done-keywords))))
  11459. ;; This is now a todo state and was not one before
  11460. ;; If there was a CLOSED time stamp, get rid of it.
  11461. (org-add-planning-info nil nil 'closed))
  11462. (when (and now-done-p org-log-done)
  11463. ;; It is now done, and it was not done before
  11464. (org-add-planning-info 'closed (org-current-effective-time))
  11465. (if (and (not dolog) (eq 'note org-log-done))
  11466. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11467. (when (and org-state dolog)
  11468. ;; This is a non-nil state, and we need to log it
  11469. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11470. ;; Fixup tag positioning
  11471. (org-todo-trigger-tag-changes org-state)
  11472. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11473. (when org-provide-todo-statistics
  11474. (org-update-parent-todo-statistics))
  11475. (run-hooks 'org-after-todo-state-change-hook)
  11476. (if (and arg (not (member org-state org-done-keywords)))
  11477. (setq head (org-get-todo-sequence-head org-state)))
  11478. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11479. ;; Do we need to trigger a repeat?
  11480. (when now-done-p
  11481. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11482. ;; This is for the agenda, take a snapshot of the headline.
  11483. (save-match-data
  11484. (setq org-agenda-headline-snapshot-before-repeat
  11485. (org-get-heading))))
  11486. (org-auto-repeat-maybe org-state))
  11487. ;; Fixup cursor location if close to the keyword
  11488. (if (and (outline-on-heading-p)
  11489. (not (bolp))
  11490. (save-excursion (beginning-of-line 1)
  11491. (looking-at org-todo-line-regexp))
  11492. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11493. (progn
  11494. (goto-char (or (match-end 2) (match-end 1)))
  11495. (and (looking-at " ") (just-one-space))))
  11496. (when org-trigger-hook
  11497. (save-excursion
  11498. (run-hook-with-args 'org-trigger-hook change-plist)))
  11499. (when commentp (org-toggle-comment))))))))
  11500. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11501. "Block turning an entry into a TODO, using the hierarchy.
  11502. This checks whether the current task should be blocked from state
  11503. changes. Such blocking occurs when:
  11504. 1. The task has children which are not all in a completed state.
  11505. 2. A task has a parent with the property :ORDERED:, and there
  11506. are siblings prior to the current task with incomplete
  11507. status.
  11508. 3. The parent of the task is blocked because it has siblings that should
  11509. be done first, or is child of a block grandparent TODO entry."
  11510. (if (not org-enforce-todo-dependencies)
  11511. t ; if locally turned off don't block
  11512. (catch 'dont-block
  11513. ;; If this is not a todo state change, or if this entry is already DONE,
  11514. ;; do not block
  11515. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11516. (member (plist-get change-plist :from)
  11517. (cons 'done org-done-keywords))
  11518. (member (plist-get change-plist :to)
  11519. (cons 'todo org-not-done-keywords))
  11520. (not (plist-get change-plist :to)))
  11521. (throw 'dont-block t))
  11522. ;; If this task has children, and any are undone, it's blocked
  11523. (save-excursion
  11524. (org-back-to-heading t)
  11525. (let ((this-level (funcall outline-level)))
  11526. (outline-next-heading)
  11527. (let ((child-level (funcall outline-level)))
  11528. (while (and (not (eobp))
  11529. (> child-level this-level))
  11530. ;; this todo has children, check whether they are all
  11531. ;; completed
  11532. (if (and (not (org-entry-is-done-p))
  11533. (org-entry-is-todo-p))
  11534. (progn (setq org-block-entry-blocking (org-get-heading))
  11535. (throw 'dont-block nil)))
  11536. (outline-next-heading)
  11537. (setq child-level (funcall outline-level))))))
  11538. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11539. ;; any previous siblings are undone, it's blocked
  11540. (save-excursion
  11541. (org-back-to-heading t)
  11542. (let* ((pos (point))
  11543. (parent-pos (and (org-up-heading-safe) (point))))
  11544. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11545. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11546. (forward-line 1)
  11547. (re-search-forward org-not-done-heading-regexp pos t))
  11548. (setq org-block-entry-blocking (match-string 0))
  11549. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11550. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11551. (while t
  11552. (goto-char parent-pos)
  11553. (if (not (looking-at org-not-done-heading-regexp))
  11554. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11555. (setq pos (point))
  11556. (setq parent-pos (and (org-up-heading-safe) (point)))
  11557. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11558. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11559. (forward-line 1)
  11560. (re-search-forward org-not-done-heading-regexp pos t)
  11561. (setq org-block-entry-blocking (org-get-heading)))
  11562. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11563. (defcustom org-track-ordered-property-with-tag nil
  11564. "Should the ORDERED property also be shown as a tag?
  11565. The ORDERED property decides if an entry should require subtasks to be
  11566. completed in sequence. Since a property is not very visible, setting
  11567. this option means that toggling the ORDERED property with the command
  11568. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11569. not relevant for the behavior, but it makes things more visible.
  11570. Note that toggling the tag with tags commands will not change the property
  11571. and therefore not influence behavior!
  11572. This can be t, meaning the tag ORDERED should be used, It can also be a
  11573. string to select a different tag for this task."
  11574. :group 'org-todo
  11575. :type '(choice
  11576. (const :tag "No tracking" nil)
  11577. (const :tag "Track with ORDERED tag" t)
  11578. (string :tag "Use other tag")))
  11579. (defun org-toggle-ordered-property ()
  11580. "Toggle the ORDERED property of the current entry.
  11581. For better visibility, you can track the value of this property with a tag.
  11582. See variable `org-track-ordered-property-with-tag'."
  11583. (interactive)
  11584. (let* ((t1 org-track-ordered-property-with-tag)
  11585. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11586. (save-excursion
  11587. (org-back-to-heading)
  11588. (if (org-entry-get nil "ORDERED")
  11589. (progn
  11590. (org-delete-property "ORDERED")
  11591. (and tag (org-toggle-tag tag 'off))
  11592. (message "Subtasks can be completed in arbitrary order"))
  11593. (org-entry-put nil "ORDERED" "t")
  11594. (and tag (org-toggle-tag tag 'on))
  11595. (message "Subtasks must be completed in sequence")))))
  11596. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11597. (defun org-block-todo-from-checkboxes (change-plist)
  11598. "Block turning an entry into a TODO, using checkboxes.
  11599. This checks whether the current task should be blocked from state
  11600. changes because there are unchecked boxes in this entry."
  11601. (if (not org-enforce-todo-checkbox-dependencies)
  11602. t ; if locally turned off don't block
  11603. (catch 'dont-block
  11604. ;; If this is not a todo state change, or if this entry is already DONE,
  11605. ;; do not block
  11606. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11607. (member (plist-get change-plist :from)
  11608. (cons 'done org-done-keywords))
  11609. (member (plist-get change-plist :to)
  11610. (cons 'todo org-not-done-keywords))
  11611. (not (plist-get change-plist :to)))
  11612. (throw 'dont-block t))
  11613. ;; If this task has checkboxes that are not checked, it's blocked
  11614. (save-excursion
  11615. (org-back-to-heading t)
  11616. (let ((beg (point)) end)
  11617. (outline-next-heading)
  11618. (setq end (point))
  11619. (goto-char beg)
  11620. (if (org-list-search-forward
  11621. (concat (org-item-beginning-re)
  11622. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11623. "\\[[- ]\\]")
  11624. end t)
  11625. (progn
  11626. (if (boundp 'org-blocked-by-checkboxes)
  11627. (setq org-blocked-by-checkboxes t))
  11628. (throw 'dont-block nil)))))
  11629. t))) ; do not block
  11630. (defun org-entry-blocked-p ()
  11631. "Non-nil if entry at point is blocked."
  11632. (and (not (org-entry-get nil "NOBLOCKING"))
  11633. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11634. (not (run-hook-with-args-until-failure
  11635. 'org-blocker-hook
  11636. (list :type 'todo-state-change
  11637. :position (point)
  11638. :from 'todo
  11639. :to 'done)))))
  11640. (defun org-update-statistics-cookies (all)
  11641. "Update the statistics cookie, either from TODO or from checkboxes.
  11642. This should be called with the cursor in a line with a statistics cookie."
  11643. (interactive "P")
  11644. (if all
  11645. (progn
  11646. (org-update-checkbox-count 'all)
  11647. (org-map-entries 'org-update-parent-todo-statistics))
  11648. (if (not (org-at-heading-p))
  11649. (org-update-checkbox-count)
  11650. (let ((pos (point-marker))
  11651. end l1 l2)
  11652. (ignore-errors (org-back-to-heading t))
  11653. (if (not (org-at-heading-p))
  11654. (org-update-checkbox-count)
  11655. (setq l1 (org-outline-level))
  11656. (setq end (save-excursion
  11657. (outline-next-heading)
  11658. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11659. (point)))
  11660. (if (and (save-excursion
  11661. (re-search-forward
  11662. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11663. (not (save-excursion (re-search-forward
  11664. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11665. (org-update-checkbox-count)
  11666. (if (and l2 (> l2 l1))
  11667. (progn
  11668. (goto-char end)
  11669. (org-update-parent-todo-statistics))
  11670. (goto-char pos)
  11671. (beginning-of-line 1)
  11672. (while (re-search-forward
  11673. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11674. (point-at-eol) t)
  11675. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11676. (goto-char pos)
  11677. (move-marker pos nil)))))
  11678. (defvar org-entry-property-inherited-from) ;; defined below
  11679. (defun org-update-parent-todo-statistics ()
  11680. "Update any statistics cookie in the parent of the current headline.
  11681. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11682. the entire subtree and this will travel up the hierarchy and update
  11683. statistics everywhere."
  11684. (let* ((prop (save-excursion (org-up-heading-safe)
  11685. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11686. (recursive (or (not org-hierarchical-todo-statistics)
  11687. (and prop (string-match "\\<recursive\\>" prop))))
  11688. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11689. 0))
  11690. (first t)
  11691. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11692. level ltoggle l1 new ndel
  11693. (cnt-all 0) (cnt-done 0) is-percent kwd
  11694. checkbox-beg ov ovs ove cookie-present)
  11695. (catch 'exit
  11696. (save-excursion
  11697. (beginning-of-line 1)
  11698. (setq ltoggle (funcall outline-level))
  11699. ;; Three situations are to consider:
  11700. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11701. ;; to the top-level ancestor on the headline;
  11702. ;; 2. If parent has "recursive" property, repeat up to the
  11703. ;; headline setting that property, taking inheritance into
  11704. ;; account;
  11705. ;; 3. Else, move up to direct parent and proceed only once.
  11706. (while (and (setq level (org-up-heading-safe))
  11707. (or recursive first)
  11708. (>= (point) lim))
  11709. (setq first nil cookie-present nil)
  11710. (unless (and level
  11711. (not (string-match
  11712. "\\<checkbox\\>"
  11713. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11714. "")))))
  11715. (throw 'exit nil))
  11716. (while (re-search-forward box-re (point-at-eol) t)
  11717. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11718. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11719. (save-match-data
  11720. (unless (outline-next-heading) (throw 'exit nil))
  11721. (while (and (looking-at org-complex-heading-regexp)
  11722. (> (setq l1 (length (match-string 1))) level))
  11723. (setq kwd (and (or recursive (= l1 ltoggle))
  11724. (match-string 2)))
  11725. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11726. (and (eq org-provide-todo-statistics t)
  11727. (or (member kwd org-done-keywords)))
  11728. (and (listp org-provide-todo-statistics)
  11729. (stringp (car org-provide-todo-statistics))
  11730. (or (member kwd org-provide-todo-statistics)
  11731. (member kwd org-done-keywords)))
  11732. (and (listp org-provide-todo-statistics)
  11733. (listp (car org-provide-todo-statistics))
  11734. (or (member kwd (car org-provide-todo-statistics))
  11735. (and (member kwd org-done-keywords)
  11736. (member kwd (cadr org-provide-todo-statistics))))))
  11737. (setq cnt-all (1+ cnt-all))
  11738. (if (eq org-provide-todo-statistics t)
  11739. (and kwd (setq cnt-all (1+ cnt-all)))))
  11740. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11741. (member kwd org-done-keywords))
  11742. (and (listp org-provide-todo-statistics)
  11743. (listp (car org-provide-todo-statistics))
  11744. (member kwd org-done-keywords)
  11745. (member kwd (cadr org-provide-todo-statistics)))
  11746. (and (listp org-provide-todo-statistics)
  11747. (stringp (car org-provide-todo-statistics))
  11748. (member kwd org-done-keywords)))
  11749. (setq cnt-done (1+ cnt-done)))
  11750. (outline-next-heading)))
  11751. (setq new
  11752. (if is-percent
  11753. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11754. (max 1 cnt-all)))
  11755. (format "[%d/%d]" cnt-done cnt-all))
  11756. ndel (- (match-end 0) checkbox-beg))
  11757. ;; handle overlays when updating cookie from column view
  11758. (when (setq ov (car (overlays-at checkbox-beg)))
  11759. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11760. (delete-overlay ov))
  11761. (goto-char checkbox-beg)
  11762. (insert new)
  11763. (delete-region (point) (+ (point) ndel))
  11764. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11765. (when ov (move-overlay ov ovs ove)))
  11766. (when cookie-present
  11767. (run-hook-with-args 'org-after-todo-statistics-hook
  11768. cnt-done (- cnt-all cnt-done))))))
  11769. (run-hooks 'org-todo-statistics-hook)))
  11770. (defvar org-after-todo-statistics-hook nil
  11771. "Hook that is called after a TODO statistics cookie has been updated.
  11772. Each function is called with two arguments: the number of not-done entries
  11773. and the number of done entries.
  11774. For example, the following function, when added to this hook, will switch
  11775. an entry to DONE when all children are done, and back to TODO when new
  11776. entries are set to a TODO status. Note that this hook is only called
  11777. when there is a statistics cookie in the headline!
  11778. (defun org-summary-todo (n-done n-not-done)
  11779. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11780. (let (org-log-done org-log-states) ; turn off logging
  11781. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11782. ")
  11783. (defvar org-todo-statistics-hook nil
  11784. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11785. This hook runs even if there is no statistics cookie present, in which case
  11786. `org-after-todo-statistics-hook' would not run.")
  11787. (defun org-todo-trigger-tag-changes (state)
  11788. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11789. (let ((l org-todo-state-tags-triggers)
  11790. changes)
  11791. (when (or (not state) (equal state ""))
  11792. (setq changes (append changes (cdr (assoc "" l)))))
  11793. (when (and (stringp state) (> (length state) 0))
  11794. (setq changes (append changes (cdr (assoc state l)))))
  11795. (when (member state org-not-done-keywords)
  11796. (setq changes (append changes (cdr (assoc 'todo l)))))
  11797. (when (member state org-done-keywords)
  11798. (setq changes (append changes (cdr (assoc 'done l)))))
  11799. (dolist (c changes)
  11800. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11801. (defun org-local-logging (value)
  11802. "Get logging settings from a property VALUE."
  11803. ;; Directly set the variables, they are already local.
  11804. (setq org-log-done nil
  11805. org-log-repeat nil
  11806. org-todo-log-states nil)
  11807. (dolist (w (org-split-string value))
  11808. (let (a)
  11809. (cond
  11810. ((setq a (assoc w org-startup-options))
  11811. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11812. (set (nth 1 a) (nth 2 a))))
  11813. ((setq a (org-extract-log-state-settings w))
  11814. (and (member (car a) org-todo-keywords-1)
  11815. (push a org-todo-log-states)))))))
  11816. (defun org-get-todo-sequence-head (kwd)
  11817. "Return the head of the TODO sequence to which KWD belongs.
  11818. If KWD is not set, check if there is a text property remembering the
  11819. right sequence."
  11820. (let (p)
  11821. (cond
  11822. ((not kwd)
  11823. (or (get-text-property (point-at-bol) 'org-todo-head)
  11824. (progn
  11825. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11826. nil (point-at-eol)))
  11827. (get-text-property p 'org-todo-head))))
  11828. ((not (member kwd org-todo-keywords-1))
  11829. (car org-todo-keywords-1))
  11830. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11831. (defun org-fast-todo-selection ()
  11832. "Fast TODO keyword selection with single keys.
  11833. Returns the new TODO keyword, or nil if no state change should occur."
  11834. (let* ((fulltable org-todo-key-alist)
  11835. (done-keywords org-done-keywords) ;; needed for the faces.
  11836. (maxlen (apply 'max (mapcar
  11837. (lambda (x)
  11838. (if (stringp (car x)) (string-width (car x)) 0))
  11839. fulltable)))
  11840. (expert nil)
  11841. (fwidth (+ maxlen 3 1 3))
  11842. (ncol (/ (- (window-width) 4) fwidth))
  11843. tg cnt e c tbl
  11844. groups ingroup)
  11845. (save-excursion
  11846. (save-window-excursion
  11847. (if expert
  11848. (set-buffer (get-buffer-create " *Org todo*"))
  11849. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11850. (erase-buffer)
  11851. (org-set-local 'org-done-keywords done-keywords)
  11852. (setq tbl fulltable cnt 0)
  11853. (while (setq e (pop tbl))
  11854. (cond
  11855. ((equal e '(:startgroup))
  11856. (push '() groups) (setq ingroup t)
  11857. (when (not (= cnt 0))
  11858. (setq cnt 0)
  11859. (insert "\n"))
  11860. (insert "{ "))
  11861. ((equal e '(:endgroup))
  11862. (setq ingroup nil cnt 0)
  11863. (insert "}\n"))
  11864. ((equal e '(:newline))
  11865. (when (not (= cnt 0))
  11866. (setq cnt 0)
  11867. (insert "\n")
  11868. (setq e (car tbl))
  11869. (while (equal (car tbl) '(:newline))
  11870. (insert "\n")
  11871. (setq tbl (cdr tbl)))))
  11872. (t
  11873. (setq tg (car e) c (cdr e))
  11874. (if ingroup (push tg (car groups)))
  11875. (setq tg (org-add-props tg nil 'face
  11876. (org-get-todo-face tg)))
  11877. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11878. (insert "[" c "] " tg (make-string
  11879. (- fwidth 4 (length tg)) ?\ ))
  11880. (when (= (setq cnt (1+ cnt)) ncol)
  11881. (insert "\n")
  11882. (if ingroup (insert " "))
  11883. (setq cnt 0)))))
  11884. (insert "\n")
  11885. (goto-char (point-min))
  11886. (if (not expert) (org-fit-window-to-buffer))
  11887. (message "[a-z..]:Set [SPC]:clear")
  11888. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11889. (cond
  11890. ((or (= c ?\C-g)
  11891. (and (= c ?q) (not (rassoc c fulltable))))
  11892. (setq quit-flag t))
  11893. ((= c ?\ ) nil)
  11894. ((setq e (rassoc c fulltable) tg (car e))
  11895. tg)
  11896. (t (setq quit-flag t)))))))
  11897. (defun org-entry-is-todo-p ()
  11898. (member (org-get-todo-state) org-not-done-keywords))
  11899. (defun org-entry-is-done-p ()
  11900. (member (org-get-todo-state) org-done-keywords))
  11901. (defun org-get-todo-state ()
  11902. "Return the TODO keyword of the current subtree."
  11903. (save-excursion
  11904. (org-back-to-heading t)
  11905. (and (looking-at org-todo-line-regexp)
  11906. (match-end 2)
  11907. (match-string 2))))
  11908. (defun org-at-date-range-p (&optional inactive-ok)
  11909. "Non-nil if point is inside a date range.
  11910. When optional argument INACTIVE-OK is non-nil, also consider
  11911. inactive time ranges.
  11912. When this function returns a non-nil value, match data is set
  11913. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11914. on INACTIVE-OK."
  11915. (interactive)
  11916. (save-excursion
  11917. (catch 'exit
  11918. (let ((pos (point)))
  11919. (skip-chars-backward "^[<\r\n")
  11920. (skip-chars-backward "<[")
  11921. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11922. (>= (match-end 0) pos)
  11923. (throw 'exit t))
  11924. (skip-chars-backward "^<[\r\n")
  11925. (skip-chars-backward "<[")
  11926. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11927. (>= (match-end 0) pos)
  11928. (throw 'exit t)))
  11929. nil)))
  11930. (defun org-get-repeat (&optional tagline)
  11931. "Check if there is a deadline/schedule with repeater in this entry."
  11932. (save-match-data
  11933. (save-excursion
  11934. (org-back-to-heading t)
  11935. (and (re-search-forward (if tagline
  11936. (concat tagline "\\s-*" org-repeat-re)
  11937. org-repeat-re)
  11938. (org-entry-end-position) t)
  11939. (match-string-no-properties 1)))))
  11940. (defvar org-last-changed-timestamp)
  11941. (defvar org-last-inserted-timestamp)
  11942. (defvar org-log-post-message)
  11943. (defvar org-log-note-purpose)
  11944. (defvar org-log-note-how nil)
  11945. (defvar org-log-note-extra)
  11946. (defun org-auto-repeat-maybe (done-word)
  11947. "Check if the current headline contains a repeated deadline/schedule.
  11948. If yes, set TODO state back to what it was and change the base date
  11949. of repeating deadline/scheduled time stamps to new date.
  11950. This function is run automatically after each state change to a DONE state."
  11951. ;; last-state is dynamically scoped into this function
  11952. (let* ((repeat (org-get-repeat))
  11953. (aa (assoc org-last-state org-todo-kwd-alist))
  11954. (interpret (nth 1 aa))
  11955. (head (nth 2 aa))
  11956. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11957. (msg "Entry repeats: ")
  11958. (org-log-done nil)
  11959. (org-todo-log-states nil)
  11960. re type n what ts time to-state)
  11961. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11962. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11963. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11964. org-todo-repeat-to-state))
  11965. (unless (and to-state (member to-state org-todo-keywords-1))
  11966. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11967. (org-todo to-state)
  11968. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11969. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11970. (org-time-stamp-format t t))))
  11971. (when org-log-repeat
  11972. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11973. (memq 'org-add-log-note post-command-hook))
  11974. ;; OK, we are already setup for some record
  11975. (if (eq org-log-repeat 'note)
  11976. ;; make sure we take a note, not only a time stamp
  11977. (setq org-log-note-how 'note))
  11978. ;; Set up for taking a record
  11979. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11980. org-last-state
  11981. 'findpos org-log-repeat)))
  11982. (org-back-to-heading t)
  11983. (org-add-planning-info nil nil 'closed)
  11984. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11985. org-deadline-time-regexp "\\)\\|\\("
  11986. org-ts-regexp "\\)"))
  11987. (while (re-search-forward
  11988. re (save-excursion (outline-next-heading) (point)) t)
  11989. (setq type (if (match-end 1) org-scheduled-string
  11990. (if (match-end 3) org-deadline-string "Plain:"))
  11991. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11992. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11993. (setq n (string-to-number (match-string 2 ts))
  11994. what (match-string 3 ts))
  11995. (if (equal what "w") (setq n (* n 7) what "d"))
  11996. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11997. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11998. ;; Preparation, see if we need to modify the start date for the change
  11999. (when (match-end 1)
  12000. (setq time (save-match-data (org-time-string-to-time ts)))
  12001. (cond
  12002. ((equal (match-string 1 ts) ".")
  12003. ;; Shift starting date to today
  12004. (org-timestamp-change
  12005. (- (org-today) (time-to-days time))
  12006. 'day))
  12007. ((equal (match-string 1 ts) "+")
  12008. (let ((nshiftmax 10) (nshift 0))
  12009. (while (or (= nshift 0)
  12010. (<= (time-to-days time)
  12011. (time-to-days (current-time))))
  12012. (when (= (incf nshift) nshiftmax)
  12013. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12014. (user-error "Abort")))
  12015. (org-timestamp-change n (cdr (assoc what whata)))
  12016. (org-at-timestamp-p t)
  12017. (setq ts (match-string 1))
  12018. (setq time (save-match-data (org-time-string-to-time ts)))))
  12019. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12020. ;; rematch, so that we have everything in place for the real shift
  12021. (org-at-timestamp-p t)
  12022. (setq ts (match-string 1))
  12023. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12024. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12025. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12026. (setq org-log-post-message msg)
  12027. (message "%s" msg))))
  12028. (defun org-show-todo-tree (arg)
  12029. "Make a compact tree which shows all headlines marked with TODO.
  12030. The tree will show the lines where the regexp matches, and all higher
  12031. headlines above the match.
  12032. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12033. With a numeric prefix N, construct a sparse tree for the Nth element
  12034. of `org-todo-keywords-1'."
  12035. (interactive "P")
  12036. (let ((case-fold-search nil)
  12037. (kwd-re
  12038. (cond ((null arg) org-not-done-regexp)
  12039. ((equal arg '(4))
  12040. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12041. (mapcar 'list org-todo-keywords-1))))
  12042. (concat "\\("
  12043. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12044. "\\)\\>")))
  12045. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12046. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12047. org-todo-keywords-1)))
  12048. (t (user-error "Invalid prefix argument: %s" arg)))))
  12049. (message "%d TODO entries found"
  12050. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12051. (defun org-deadline (arg &optional time)
  12052. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12053. With one universal prefix argument, remove any deadline from the item.
  12054. With two universal prefix arguments, prompt for a warning delay.
  12055. With argument TIME, set the deadline at the corresponding date. TIME
  12056. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12057. (interactive "P")
  12058. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12059. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12060. 'region-start-level 'region))
  12061. org-loop-over-headlines-in-active-region)
  12062. (org-map-entries
  12063. `(org-deadline ',arg ,time)
  12064. org-loop-over-headlines-in-active-region
  12065. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12066. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12067. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12068. (repeater (and old-date
  12069. (string-match
  12070. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12071. old-date)
  12072. (match-string 1 old-date))))
  12073. (cond
  12074. ((equal arg '(4))
  12075. (when (and old-date org-log-redeadline)
  12076. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12077. org-log-redeadline))
  12078. (org-remove-timestamp-with-keyword org-deadline-string)
  12079. (message "Item no longer has a deadline."))
  12080. ((equal arg '(16))
  12081. (save-excursion
  12082. (org-back-to-heading t)
  12083. (if (re-search-forward
  12084. org-deadline-time-regexp
  12085. (save-excursion (outline-next-heading) (point)) t)
  12086. (let* ((rpl0 (match-string 1))
  12087. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12088. (replace-match
  12089. (concat org-deadline-string
  12090. " <" rpl
  12091. (format " -%dd"
  12092. (abs
  12093. (- (time-to-days
  12094. (save-match-data
  12095. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12096. (time-to-days old-date-time))))
  12097. ">") t t))
  12098. (user-error "No deadline information to update"))))
  12099. (t
  12100. (org-add-planning-info 'deadline time 'closed)
  12101. (when (and old-date
  12102. org-log-redeadline
  12103. (not (equal old-date org-last-inserted-timestamp)))
  12104. (org-add-log-setup
  12105. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12106. org-log-redeadline))
  12107. (when repeater
  12108. (save-excursion
  12109. (org-back-to-heading t)
  12110. (when (re-search-forward (concat org-deadline-string " "
  12111. org-last-inserted-timestamp)
  12112. (save-excursion
  12113. (outline-next-heading) (point)) t)
  12114. (goto-char (1- (match-end 0)))
  12115. (insert " " repeater)
  12116. (setq org-last-inserted-timestamp
  12117. (concat (substring org-last-inserted-timestamp 0 -1)
  12118. " " repeater
  12119. (substring org-last-inserted-timestamp -1))))))
  12120. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12121. (defun org-schedule (arg &optional time)
  12122. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12123. With one universal prefix argument, remove any scheduling date from the item.
  12124. With two universal prefix arguments, prompt for a delay cookie.
  12125. With argument TIME, scheduled at the corresponding date. TIME can
  12126. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12127. (interactive "P")
  12128. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12129. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12130. 'region-start-level 'region))
  12131. org-loop-over-headlines-in-active-region)
  12132. (org-map-entries
  12133. `(org-schedule ',arg ,time)
  12134. org-loop-over-headlines-in-active-region
  12135. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12136. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12137. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12138. (repeater (and old-date
  12139. (string-match
  12140. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12141. old-date)
  12142. (match-string 1 old-date))))
  12143. (cond
  12144. ((equal arg '(4))
  12145. (progn
  12146. (when (and old-date org-log-reschedule)
  12147. (org-add-log-setup 'delschedule nil old-date 'findpos
  12148. org-log-reschedule))
  12149. (org-remove-timestamp-with-keyword org-scheduled-string)
  12150. (message "Item is no longer scheduled.")))
  12151. ((equal arg '(16))
  12152. (save-excursion
  12153. (org-back-to-heading t)
  12154. (if (re-search-forward
  12155. org-scheduled-time-regexp
  12156. (save-excursion (outline-next-heading) (point)) t)
  12157. (let* ((rpl0 (match-string 1))
  12158. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12159. (replace-match
  12160. (concat org-scheduled-string
  12161. " <" rpl
  12162. (format " -%dd"
  12163. (abs
  12164. (- (time-to-days
  12165. (save-match-data
  12166. (org-read-date nil t nil "Delay until" old-date-time)))
  12167. (time-to-days old-date-time))))
  12168. ">") t t))
  12169. (user-error "No scheduled information to update"))))
  12170. (t
  12171. (org-add-planning-info 'scheduled time 'closed)
  12172. (when (and old-date
  12173. org-log-reschedule
  12174. (not (equal old-date org-last-inserted-timestamp)))
  12175. (org-add-log-setup
  12176. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12177. org-log-reschedule))
  12178. (when repeater
  12179. (save-excursion
  12180. (org-back-to-heading t)
  12181. (when (re-search-forward (concat org-scheduled-string " "
  12182. org-last-inserted-timestamp)
  12183. (save-excursion
  12184. (outline-next-heading) (point)) t)
  12185. (goto-char (1- (match-end 0)))
  12186. (insert " " repeater)
  12187. (setq org-last-inserted-timestamp
  12188. (concat (substring org-last-inserted-timestamp 0 -1)
  12189. " " repeater
  12190. (substring org-last-inserted-timestamp -1))))))
  12191. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12192. (defun org-get-scheduled-time (pom &optional inherit)
  12193. "Get the scheduled time as a time tuple, of a format suitable
  12194. for calling org-schedule with, or if there is no scheduling,
  12195. returns nil."
  12196. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12197. (when time
  12198. (apply 'encode-time (org-parse-time-string time)))))
  12199. (defun org-get-deadline-time (pom &optional inherit)
  12200. "Get the deadline as a time tuple, of a format suitable for
  12201. calling org-deadline with, or if there is no scheduling, returns
  12202. nil."
  12203. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12204. (when time
  12205. (apply 'encode-time (org-parse-time-string time)))))
  12206. (defun org-remove-timestamp-with-keyword (keyword)
  12207. "Remove all time stamps with KEYWORD in the current entry."
  12208. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12209. beg)
  12210. (save-excursion
  12211. (org-back-to-heading t)
  12212. (setq beg (point))
  12213. (outline-next-heading)
  12214. (while (re-search-backward re beg t)
  12215. (replace-match "")
  12216. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12217. (equal (char-before) ?\ ))
  12218. (backward-delete-char 1)
  12219. (if (string-match "^[ \t]*$" (buffer-substring
  12220. (point-at-bol) (point-at-eol)))
  12221. (delete-region (point-at-bol)
  12222. (min (point-max) (1+ (point-at-eol))))))))))
  12223. (defvar org-time-was-given) ; dynamically scoped parameter
  12224. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12225. (defun org-at-planning-p ()
  12226. "Non-nil when point is on a planning info line."
  12227. ;; This is as accurate and faster than `org-element-at-point' since
  12228. ;; planning info location is fixed in the section.
  12229. (org-with-wide-buffer
  12230. (beginning-of-line)
  12231. (and (org-looking-at-p org-planning-line-re)
  12232. (eq (point)
  12233. (ignore-errors
  12234. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12235. (org-back-to-heading t)
  12236. (org-with-limited-levels (org-back-to-heading t)))
  12237. (line-beginning-position 2))))))
  12238. (defun org-add-planning-info (what &optional time &rest remove)
  12239. "Insert new timestamp with keyword in the planning line.
  12240. WHAT indicates what kind of time stamp to add. It is a symbol
  12241. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12242. the time to use. If none is given, the user is prompted for
  12243. a date. REMOVE indicates what kind of entries to remove. An old
  12244. WHAT entry will also be removed."
  12245. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12246. (catch 'exit
  12247. (when (and (memq what '(scheduled deadline))
  12248. (or (not time)
  12249. (and (stringp time)
  12250. (string-match "^[-+]+[0-9]" time))))
  12251. ;; Try to get a default date/time from existing timestamp
  12252. (save-excursion
  12253. (org-back-to-heading t)
  12254. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12255. (when (re-search-forward (if (eq what 'scheduled)
  12256. org-scheduled-time-regexp
  12257. org-deadline-time-regexp)
  12258. end t)
  12259. (setq ts (match-string 1)
  12260. default-time (apply 'encode-time (org-parse-time-string ts))
  12261. default-input (and ts (org-get-compact-tod ts)))))))
  12262. (when what
  12263. (setq time
  12264. (if (stringp time)
  12265. ;; This is a string (relative or absolute), set
  12266. ;; proper date.
  12267. (apply #'encode-time
  12268. (org-read-date-analyze
  12269. time default-time (decode-time default-time)))
  12270. ;; If necessary, get the time from the user
  12271. (or time (org-read-date nil 'to-time nil nil
  12272. default-time default-input)))))
  12273. (org-with-wide-buffer
  12274. (org-back-to-heading t)
  12275. (forward-line)
  12276. (unless (bolp) (insert "\n"))
  12277. (cond ((org-looking-at-p org-planning-line-re)
  12278. ;; Move to current indentation.
  12279. (skip-chars-forward " \t")
  12280. ;; Check if we have to remove something.
  12281. (dolist (type (if what (cons what remove) remove))
  12282. (save-excursion
  12283. (when (re-search-forward
  12284. (concat
  12285. " *"
  12286. (case type
  12287. (closed org-closed-time-regexp)
  12288. (deadline org-deadline-time-regexp)
  12289. (scheduled org-scheduled-time-regexp)
  12290. (otherwise
  12291. (error "Invalid planning type: %s" type))))
  12292. (line-end-position) t)
  12293. (replace-match "")
  12294. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12295. (when (and (not what) (eq type 'closed))
  12296. (save-excursion
  12297. (beginning-of-line)
  12298. (when (looking-at "[ \t]*$")
  12299. (delete-region (point)
  12300. (line-beginning-position 2)))))))
  12301. ;; Remove leading white spaces.
  12302. (when (looking-at "[ \t]+") (replace-match ""))))
  12303. ((not what) (throw 'exit nil)) ; Nothing to do.
  12304. (t (insert-before-markers "\n")
  12305. (backward-char 1)
  12306. (when org-adapt-indentation
  12307. (org-indent-to-column (1+ (org-outline-level))))))
  12308. (when what
  12309. ;; Insert planning keyword.
  12310. (insert (case what
  12311. (closed org-closed-string)
  12312. (deadline org-deadline-string)
  12313. (scheduled org-scheduled-string)
  12314. (otherwise (error "Invalid planning type: %s" what)))
  12315. " ")
  12316. ;; Insert associated timestamp.
  12317. (let ((ts (org-insert-time-stamp
  12318. time
  12319. (or org-time-was-given
  12320. (and (eq what 'closed) org-log-done-with-time))
  12321. (eq what 'closed)
  12322. nil nil (list org-end-time-was-given))))
  12323. (unless (eolp) (insert " "))
  12324. ts))))))
  12325. (defvar org-log-note-marker (make-marker))
  12326. (defvar org-log-note-purpose nil)
  12327. (defvar org-log-note-state nil)
  12328. (defvar org-log-note-previous-state nil)
  12329. (defvar org-log-note-extra nil)
  12330. (defvar org-log-note-window-configuration nil)
  12331. (defvar org-log-note-return-to (make-marker))
  12332. (defvar org-log-note-effective-time nil
  12333. "Remembered current time so that dynamically scoped
  12334. `org-extend-today-until' affects tha timestamps in state change
  12335. log")
  12336. (defvar org-log-post-message nil
  12337. "Message to be displayed after a log note has been stored.
  12338. The auto-repeater uses this.")
  12339. (defun org-add-note ()
  12340. "Add a note to the current entry.
  12341. This is done in the same way as adding a state change note."
  12342. (interactive)
  12343. (org-add-log-setup 'note nil nil 'findpos nil))
  12344. (defun org-log-beginning (&optional create)
  12345. "Return expected start of log notes in current entry.
  12346. When optional argument CREATE is non-nil, the function creates
  12347. a drawer to store notes, if necessary. Returned position ignores
  12348. narrowing."
  12349. (org-with-wide-buffer
  12350. (org-end-of-meta-data)
  12351. (let ((end (if (org-at-heading-p) (point)
  12352. (save-excursion (outline-next-heading) (point))))
  12353. (drawer (org-log-into-drawer)))
  12354. (cond
  12355. (drawer
  12356. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12357. (case-fold-search t))
  12358. (catch 'exit
  12359. ;; Try to find existing drawer.
  12360. (while (re-search-forward regexp end t)
  12361. (let ((element (org-element-at-point)))
  12362. (when (eq (org-element-type element) 'drawer)
  12363. (let ((cend (org-element-property :contents-end element)))
  12364. (when (and (not org-log-states-order-reversed) cend)
  12365. (goto-char cend)))
  12366. (throw 'exit nil))))
  12367. ;; No drawer found. Create one, if permitted.
  12368. (when create
  12369. (unless (bolp) (insert "\n"))
  12370. (let ((beg (point)))
  12371. (insert ":" drawer ":\n:END:\n")
  12372. (org-indent-region beg (point)))
  12373. (end-of-line -1)))))
  12374. (org-log-state-notes-insert-after-drawers
  12375. (while (and (looking-at org-drawer-regexp)
  12376. (progn (goto-char (match-end 0))
  12377. (re-search-forward org-property-end-re end t)))
  12378. (forward-line)))))
  12379. (if (bolp) (point) (line-beginning-position 2))))
  12380. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12381. "Set up the post command hook to take a note.
  12382. If this is about to TODO state change, the new state is expected in STATE.
  12383. When FINDPOS is non-nil, find the correct position for the note in
  12384. the current entry. If not, assume that it can be inserted at point.
  12385. HOW is an indicator what kind of note should be created.
  12386. EXTRA is additional text that will be inserted into the notes buffer."
  12387. (org-with-wide-buffer
  12388. (when findpos
  12389. (goto-char (org-log-beginning t))
  12390. (unless org-log-states-order-reversed
  12391. (org-skip-over-state-notes)
  12392. (skip-chars-backward " \t\n\r")
  12393. (forward-line)))
  12394. (move-marker org-log-note-marker (point))
  12395. ;; Preserve position even if a property drawer is inserted in the
  12396. ;; process.
  12397. (set-marker-insertion-type org-log-note-marker t)
  12398. (setq org-log-note-purpose purpose
  12399. org-log-note-state state
  12400. org-log-note-previous-state prev-state
  12401. org-log-note-how how
  12402. org-log-note-extra extra
  12403. org-log-note-effective-time (org-current-effective-time))
  12404. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12405. (defun org-skip-over-state-notes ()
  12406. "Skip past the list of State notes in an entry."
  12407. (when (ignore-errors (goto-char (org-in-item-p)))
  12408. (let* ((struct (org-list-struct))
  12409. (prevs (org-list-prevs-alist struct))
  12410. (regexp
  12411. (concat "[ \t]*- +"
  12412. (replace-regexp-in-string
  12413. " +" " +"
  12414. (org-replace-escapes
  12415. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12416. `(("%d" . ,org-ts-regexp-inactive)
  12417. ("%D" . ,org-ts-regexp)
  12418. ("%s" . "\"\\S-+\"")
  12419. ("%S" . "\"\\S-+\"")
  12420. ("%t" . ,org-ts-regexp-inactive)
  12421. ("%T" . ,org-ts-regexp)
  12422. ("%u" . ".*?")
  12423. ("%U" . ".*?")))))))
  12424. (while (org-looking-at-p regexp)
  12425. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12426. (org-list-get-item-end (point) struct)))))))
  12427. (defun org-add-log-note (&optional purpose)
  12428. "Pop up a window for taking a note, and add this note later at point."
  12429. (remove-hook 'post-command-hook 'org-add-log-note)
  12430. (setq org-log-note-window-configuration (current-window-configuration))
  12431. (delete-other-windows)
  12432. (move-marker org-log-note-return-to (point))
  12433. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12434. (goto-char org-log-note-marker)
  12435. (org-switch-to-buffer-other-window "*Org Note*")
  12436. (erase-buffer)
  12437. (if (memq org-log-note-how '(time state))
  12438. (let (current-prefix-arg) (org-store-log-note))
  12439. (let ((org-inhibit-startup t)) (org-mode))
  12440. (insert (format "# Insert note for %s.
  12441. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12442. (cond
  12443. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12444. ((eq org-log-note-purpose 'done) "closed todo item")
  12445. ((eq org-log-note-purpose 'state)
  12446. (format "state change from \"%s\" to \"%s\""
  12447. (or org-log-note-previous-state "")
  12448. (or org-log-note-state "")))
  12449. ((eq org-log-note-purpose 'reschedule)
  12450. "rescheduling")
  12451. ((eq org-log-note-purpose 'delschedule)
  12452. "no longer scheduled")
  12453. ((eq org-log-note-purpose 'redeadline)
  12454. "changing deadline")
  12455. ((eq org-log-note-purpose 'deldeadline)
  12456. "removing deadline")
  12457. ((eq org-log-note-purpose 'refile)
  12458. "refiling")
  12459. ((eq org-log-note-purpose 'note)
  12460. "this entry")
  12461. (t (error "This should not happen")))))
  12462. (if org-log-note-extra (insert org-log-note-extra))
  12463. (org-set-local 'org-finish-function 'org-store-log-note)
  12464. (run-hooks 'org-log-buffer-setup-hook)))
  12465. (defvar org-note-abort nil) ; dynamically scoped
  12466. (defun org-store-log-note ()
  12467. "Finish taking a log note, and insert it to where it belongs."
  12468. (let ((txt (buffer-string)))
  12469. (kill-buffer (current-buffer))
  12470. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12471. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12472. (setq txt (replace-match "" t t txt)))
  12473. (if (string-match "\\s-+\\'" txt)
  12474. (setq txt (replace-match "" t t txt)))
  12475. (setq lines (org-split-string txt "\n"))
  12476. (when (and note (string-match "\\S-" note))
  12477. (setq note
  12478. (org-replace-escapes
  12479. note
  12480. (list (cons "%u" (user-login-name))
  12481. (cons "%U" user-full-name)
  12482. (cons "%t" (format-time-string
  12483. (org-time-stamp-format 'long 'inactive)
  12484. org-log-note-effective-time))
  12485. (cons "%T" (format-time-string
  12486. (org-time-stamp-format 'long nil)
  12487. org-log-note-effective-time))
  12488. (cons "%d" (format-time-string
  12489. (org-time-stamp-format nil 'inactive)
  12490. org-log-note-effective-time))
  12491. (cons "%D" (format-time-string
  12492. (org-time-stamp-format nil nil)
  12493. org-log-note-effective-time))
  12494. (cons "%s" (cond
  12495. ((not org-log-note-state) "")
  12496. ((org-string-match-p org-ts-regexp
  12497. org-log-note-state)
  12498. (format "\"[%s]\""
  12499. (substring org-log-note-state 1 -1)))
  12500. (t (format "\"%s\"" org-log-note-state))))
  12501. (cons "%S"
  12502. (cond
  12503. ((not org-log-note-previous-state) "")
  12504. ((org-string-match-p org-ts-regexp
  12505. org-log-note-previous-state)
  12506. (format "\"[%s]\""
  12507. (substring
  12508. org-log-note-previous-state 1 -1)))
  12509. (t (format "\"%s\""
  12510. org-log-note-previous-state)))))))
  12511. (when lines (setq note (concat note " \\\\")))
  12512. (push note lines))
  12513. (when (or current-prefix-arg org-note-abort)
  12514. (when (org-log-into-drawer)
  12515. (org-remove-empty-drawer-at org-log-note-marker))
  12516. (setq lines nil))
  12517. (when lines
  12518. (with-current-buffer (marker-buffer org-log-note-marker)
  12519. (org-with-wide-buffer
  12520. (goto-char org-log-note-marker)
  12521. (move-marker org-log-note-marker nil)
  12522. ;; Make sure point is at the beginning of an empty line.
  12523. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12524. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12525. ;; In an existing list, add a new item at the top level.
  12526. ;; Otherwise, indent line like a regular one.
  12527. (let ((itemp (org-in-item-p)))
  12528. (if itemp
  12529. (org-indent-line-to
  12530. (let ((struct (save-excursion
  12531. (goto-char itemp) (org-list-struct))))
  12532. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12533. (org-indent-line)))
  12534. (insert (org-list-bullet-string "-") (pop lines))
  12535. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12536. (dolist (line lines)
  12537. (insert "\n")
  12538. (org-indent-line-to ind)
  12539. (insert line)))
  12540. (message "Note stored")
  12541. (org-back-to-heading t)
  12542. (org-cycle-hide-drawers 'children))
  12543. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12544. ;; from within `org-add-log-note' because `buffer-undo-list'
  12545. ;; is then modified outside of `org-with-remote-undo'.
  12546. (when (eq this-command 'org-agenda-todo)
  12547. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12548. ;; Don't add undo information when called from `org-agenda-todo'
  12549. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12550. (set-window-configuration org-log-note-window-configuration)
  12551. (with-current-buffer (marker-buffer org-log-note-return-to)
  12552. (goto-char org-log-note-return-to))
  12553. (move-marker org-log-note-return-to nil)
  12554. (and org-log-post-message (message "%s" org-log-post-message))))
  12555. (defun org-remove-empty-drawer-at (pos)
  12556. "Remove an empty drawer at position POS.
  12557. POS may also be a marker."
  12558. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12559. (org-with-wide-buffer
  12560. (goto-char pos)
  12561. (let ((drawer (org-element-at-point)))
  12562. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12563. (not (org-element-property :contents-begin drawer)))
  12564. (delete-region (org-element-property :begin drawer)
  12565. (progn (goto-char (org-element-property :end drawer))
  12566. (skip-chars-backward " \r\t\n")
  12567. (forward-line)
  12568. (point))))))))
  12569. (defvar org-ts-type nil)
  12570. (defun org-sparse-tree (&optional arg type)
  12571. "Create a sparse tree, prompt for the details.
  12572. This command can create sparse trees. You first need to select the type
  12573. of match used to create the tree:
  12574. t Show all TODO entries.
  12575. T Show entries with a specific TODO keyword.
  12576. m Show entries selected by a tags/property match.
  12577. p Enter a property name and its value (both with completion on existing
  12578. names/values) and show entries with that property.
  12579. r Show entries matching a regular expression (`/' can be used as well).
  12580. b Show deadlines and scheduled items before a date.
  12581. a Show deadlines and scheduled items after a date.
  12582. d Show deadlines due within `org-deadline-warning-days'.
  12583. D Show deadlines and scheduled items between a date range."
  12584. (interactive "P")
  12585. (setq type (or type org-sparse-tree-default-date-type))
  12586. (setq org-ts-type type)
  12587. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12588. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12589. [c]ycle through date types: %s"
  12590. (case type
  12591. (all "all timestamps")
  12592. (scheduled "only scheduled")
  12593. (deadline "only deadline")
  12594. (active "only active timestamps")
  12595. (inactive "only inactive timestamps")
  12596. (closed "with a closed time-stamp")
  12597. (otherwise "scheduled/deadline")))
  12598. (let ((answer (read-char-exclusive)))
  12599. (case answer
  12600. (?c
  12601. (org-sparse-tree
  12602. arg
  12603. (cadr
  12604. (memq type '(nil all scheduled deadline active inactive closed)))))
  12605. (?d (call-interactively 'org-check-deadlines))
  12606. (?b (call-interactively 'org-check-before-date))
  12607. (?a (call-interactively 'org-check-after-date))
  12608. (?D (call-interactively 'org-check-dates-range))
  12609. (?t (call-interactively 'org-show-todo-tree))
  12610. (?T (org-show-todo-tree '(4)))
  12611. (?m (call-interactively 'org-match-sparse-tree))
  12612. ((?p ?P)
  12613. (let* ((kwd (org-icompleting-read
  12614. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12615. (value (org-icompleting-read
  12616. "Value: " (mapcar 'list (org-property-values kwd)))))
  12617. (unless (string-match "\\`{.*}\\'" value)
  12618. (setq value (concat "\"" value "\"")))
  12619. (org-match-sparse-tree arg (concat kwd "=" value))))
  12620. ((?r ?R ?/) (call-interactively 'org-occur))
  12621. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12622. (defvar org-occur-highlights nil
  12623. "List of overlays used for occur matches.")
  12624. (make-variable-buffer-local 'org-occur-highlights)
  12625. (defvar org-occur-parameters nil
  12626. "Parameters of the active org-occur calls.
  12627. This is a list, each call to org-occur pushes as cons cell,
  12628. containing the regular expression and the callback, onto the list.
  12629. The list can contain several entries if `org-occur' has been called
  12630. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12631. will only contain one set of parameters. When the highlights are
  12632. removed (for example with `C-c C-c', or with the next edit (depending
  12633. on `org-remove-highlights-with-change'), this variable is emptied
  12634. as well.")
  12635. (make-variable-buffer-local 'org-occur-parameters)
  12636. (defun org-occur (regexp &optional keep-previous callback)
  12637. "Make a compact tree which shows all matches of REGEXP.
  12638. The tree will show the lines where the regexp matches, and all higher
  12639. headlines above the match. It will also show the heading after the match,
  12640. to make sure editing the matching entry is easy.
  12641. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12642. call to `org-occur' will be kept, to allow stacking of calls to this
  12643. command.
  12644. If CALLBACK is non-nil, it is a function which is called to confirm
  12645. that the match should indeed be shown."
  12646. (interactive "sRegexp: \nP")
  12647. (when (equal regexp "")
  12648. (user-error "Regexp cannot be empty"))
  12649. (unless keep-previous
  12650. (org-remove-occur-highlights nil nil t))
  12651. (push (cons regexp callback) org-occur-parameters)
  12652. (let ((cnt 0))
  12653. (save-excursion
  12654. (goto-char (point-min))
  12655. (if (or (not keep-previous) ; do not want to keep
  12656. (not org-occur-highlights)) ; no previous matches
  12657. ;; hide everything
  12658. (org-overview))
  12659. (while (re-search-forward regexp nil t)
  12660. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12661. (when (or (not callback)
  12662. (save-match-data (funcall callback)))
  12663. (setq cnt (1+ cnt))
  12664. (when org-highlight-sparse-tree-matches
  12665. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12666. (org-show-context 'occur-tree))))
  12667. (when org-remove-highlights-with-change
  12668. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12669. nil 'local))
  12670. (unless org-sparse-tree-open-archived-trees
  12671. (org-hide-archived-subtrees (point-min) (point-max)))
  12672. (run-hooks 'org-occur-hook)
  12673. (if (org-called-interactively-p 'interactive)
  12674. (message "%d match(es) for regexp %s" cnt regexp))
  12675. cnt))
  12676. (defun org-occur-next-match (&optional n reset)
  12677. "Function for `next-error-function' to find sparse tree matches.
  12678. N is the number of matches to move, when negative move backwards.
  12679. RESET is entirely ignored - this function always goes back to the
  12680. starting point when no match is found."
  12681. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12682. (search-func (if (< n 0)
  12683. 'previous-single-char-property-change
  12684. 'next-single-char-property-change))
  12685. (n (abs n))
  12686. (pos (point))
  12687. p1)
  12688. (catch 'exit
  12689. (while (setq p1 (funcall search-func (point) 'org-type))
  12690. (when (equal p1 limit)
  12691. (goto-char pos)
  12692. (user-error "No more matches"))
  12693. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12694. (setq n (1- n))
  12695. (when (= n 0)
  12696. (goto-char p1)
  12697. (throw 'exit (point))))
  12698. (goto-char p1))
  12699. (goto-char p1)
  12700. (user-error "No more matches"))))
  12701. (defun org-show-context (&optional key)
  12702. "Make sure point and context are visible.
  12703. Optional argument KEY, when non-nil, is a symbol. See
  12704. `org-show-context-detail' for allowed values and how much is to
  12705. be shown."
  12706. (org-show-set-visibility
  12707. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12708. ((cdr (assq key org-show-context-detail)))
  12709. (t (cdr (assq 'default org-show-context-detail))))))
  12710. (defun org-show-set-visibility (detail)
  12711. "Set visibility around point according to DETAIL.
  12712. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12713. `tree', `canonical' or t. See `org-show-context-detail' for more
  12714. information."
  12715. (unless (org-before-first-heading-p)
  12716. ;; Show current heading and possibly its entry, following headline
  12717. ;; or all children.
  12718. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12719. (org-flag-heading nil)
  12720. (org-show-entry)
  12721. (org-with-limited-levels
  12722. (case detail
  12723. ((tree canonical t) (org-show-children))
  12724. ((nil minimal ancestors))
  12725. (t (save-excursion
  12726. (outline-next-heading)
  12727. (org-flag-heading nil))))))
  12728. ;; Show all siblings.
  12729. (when (eq detail 'lineage) (org-show-siblings))
  12730. ;; Show ancestors, possibly with their children.
  12731. (when (memq detail '(ancestors lineage tree canonical t))
  12732. (save-excursion
  12733. (while (org-up-heading-safe)
  12734. (org-flag-heading nil)
  12735. (when (memq detail '(canonical t)) (org-show-entry))
  12736. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12737. (defvar org-reveal-start-hook nil
  12738. "Hook run before revealing a location.")
  12739. (defun org-reveal (&optional siblings)
  12740. "Show current entry, hierarchy above it, and the following headline.
  12741. This can be used to show a consistent set of context around
  12742. locations exposed with `org-show-context'.
  12743. With optional argument SIBLINGS, on each level of the hierarchy all
  12744. siblings are shown. This repairs the tree structure to what it would
  12745. look like when opened with hierarchical calls to `org-cycle'.
  12746. With double optional argument \\[universal-argument] \\[universal-argument], \
  12747. go to the parent and show the
  12748. entire tree."
  12749. (interactive "P")
  12750. (run-hooks 'org-reveal-start-hook)
  12751. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12752. ((equal siblings '(16))
  12753. (save-excursion
  12754. (when (org-up-heading-safe)
  12755. (org-show-subtree)
  12756. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12757. (t (org-show-set-visibility 'lineage))))
  12758. (defun org-highlight-new-match (beg end)
  12759. "Highlight from BEG to END and mark the highlight is an occur headline."
  12760. (let ((ov (make-overlay beg end)))
  12761. (overlay-put ov 'face 'secondary-selection)
  12762. (overlay-put ov 'org-type 'org-occur)
  12763. (push ov org-occur-highlights)))
  12764. (defun org-remove-occur-highlights (&optional beg end noremove)
  12765. "Remove the occur highlights from the buffer.
  12766. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12767. from the `before-change-functions' in the current buffer."
  12768. (interactive)
  12769. (unless org-inhibit-highlight-removal
  12770. (mapc 'delete-overlay org-occur-highlights)
  12771. (setq org-occur-highlights nil)
  12772. (setq org-occur-parameters nil)
  12773. (unless noremove
  12774. (remove-hook 'before-change-functions
  12775. 'org-remove-occur-highlights 'local))))
  12776. ;;;; Priorities
  12777. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12778. "Regular expression matching the priority indicator.")
  12779. (defvar org-remove-priority-next-time nil)
  12780. (defun org-priority-up ()
  12781. "Increase the priority of the current item."
  12782. (interactive)
  12783. (org-priority 'up))
  12784. (defun org-priority-down ()
  12785. "Decrease the priority of the current item."
  12786. (interactive)
  12787. (org-priority 'down))
  12788. (defun org-priority (&optional action show)
  12789. "Change the priority of an item.
  12790. ACTION can be `set', `up', `down', or a character."
  12791. (interactive "P")
  12792. (if (equal action '(4))
  12793. (org-show-priority)
  12794. (unless org-enable-priority-commands
  12795. (user-error "Priority commands are disabled"))
  12796. (setq action (or action 'set))
  12797. (let (current new news have remove)
  12798. (save-excursion
  12799. (org-back-to-heading t)
  12800. (if (looking-at org-priority-regexp)
  12801. (setq current (string-to-char (match-string 2))
  12802. have t))
  12803. (cond
  12804. ((eq action 'remove)
  12805. (setq remove t new ?\ ))
  12806. ((or (eq action 'set)
  12807. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12808. (if (not (eq action 'set))
  12809. (setq new action)
  12810. (message "Priority %c-%c, SPC to remove: "
  12811. org-highest-priority org-lowest-priority)
  12812. (save-match-data
  12813. (setq new (read-char-exclusive))))
  12814. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12815. (= (upcase org-lowest-priority) org-lowest-priority))
  12816. (setq new (upcase new)))
  12817. (cond ((equal new ?\ ) (setq remove t))
  12818. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12819. (user-error "Priority must be between `%c' and `%c'"
  12820. org-highest-priority org-lowest-priority))))
  12821. ((eq action 'up)
  12822. (setq new (if have
  12823. (1- current) ; normal cycling
  12824. ;; last priority was empty
  12825. (if (eq last-command this-command)
  12826. org-lowest-priority ; wrap around empty to lowest
  12827. ;; default
  12828. (if org-priority-start-cycle-with-default
  12829. org-default-priority
  12830. (1- org-default-priority))))))
  12831. ((eq action 'down)
  12832. (setq new (if have
  12833. (1+ current) ; normal cycling
  12834. ;; last priority was empty
  12835. (if (eq last-command this-command)
  12836. org-highest-priority ; wrap around empty to highest
  12837. ;; default
  12838. (if org-priority-start-cycle-with-default
  12839. org-default-priority
  12840. (1+ org-default-priority))))))
  12841. (t (user-error "Invalid action")))
  12842. (if (or (< (upcase new) org-highest-priority)
  12843. (> (upcase new) org-lowest-priority))
  12844. (if (and (memq action '(up down))
  12845. (not have) (not (eq last-command this-command)))
  12846. ;; `new' is from default priority
  12847. (error
  12848. "The default can not be set, see `org-default-priority' why")
  12849. ;; normal cycling: `new' is beyond highest/lowest priority
  12850. ;; and is wrapped around to the empty priority
  12851. (setq remove t)))
  12852. (setq news (format "%c" new))
  12853. (if have
  12854. (if remove
  12855. (replace-match "" t t nil 1)
  12856. (replace-match news t t nil 2))
  12857. (if remove
  12858. (user-error "No priority cookie found in line")
  12859. (let ((case-fold-search nil))
  12860. (looking-at org-todo-line-regexp))
  12861. (if (match-end 2)
  12862. (progn
  12863. (goto-char (match-end 2))
  12864. (insert " [#" news "]"))
  12865. (goto-char (match-beginning 3))
  12866. (insert "[#" news "] "))))
  12867. (org-set-tags nil 'align))
  12868. (if remove
  12869. (message "Priority removed")
  12870. (message "Priority of current item set to %s" news)))))
  12871. (defun org-show-priority ()
  12872. "Show the priority of the current item.
  12873. This priority is composed of the main priority given with the [#A] cookies,
  12874. and by additional input from the age of a schedules or deadline entry."
  12875. (interactive)
  12876. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12877. (org-get-at-bol 'priority)
  12878. (save-excursion
  12879. (save-match-data
  12880. (beginning-of-line)
  12881. (and (looking-at org-heading-regexp)
  12882. (org-get-priority (match-string 0))))))))
  12883. (message "Priority is %d" (if pri pri -1000))))
  12884. (defun org-get-priority (s)
  12885. "Find priority cookie and return priority."
  12886. (save-match-data
  12887. (if (functionp org-get-priority-function)
  12888. (funcall org-get-priority-function)
  12889. (if (not (string-match org-priority-regexp s))
  12890. (* 1000 (- org-lowest-priority org-default-priority))
  12891. (* 1000 (- org-lowest-priority
  12892. (string-to-char (match-string 2 s))))))))
  12893. ;;;; Tags
  12894. (defvar org-agenda-archives-mode)
  12895. (defvar org-map-continue-from nil
  12896. "Position from where mapping should continue.
  12897. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12898. (defvar org-scanner-tags nil
  12899. "The current tag list while the tags scanner is running.")
  12900. (defvar org-trust-scanner-tags nil
  12901. "Should `org-get-tags-at' use the tags for the scanner.
  12902. This is for internal dynamical scoping only.
  12903. When this is non-nil, the function `org-get-tags-at' will return the value
  12904. of `org-scanner-tags' instead of building the list by itself. This
  12905. can lead to large speed-ups when the tags scanner is used in a file with
  12906. many entries, and when the list of tags is retrieved, for example to
  12907. obtain a list of properties. Building the tags list for each entry in such
  12908. a file becomes an N^2 operation - but with this variable set, it scales
  12909. as N.")
  12910. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12911. "Scan headline tags with inheritance and produce output ACTION.
  12912. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12913. or `agenda' to produce an entry list for an agenda view. It can also be
  12914. a Lisp form or a function that should be called at each matched headline, in
  12915. this case the return value is a list of all return values from these calls.
  12916. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12917. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12918. only lines with a not-done TODO keyword are included in the output.
  12919. This should be the same variable that was scoped into
  12920. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12921. START-LEVEL can be a string with asterisks, reducing the scope to
  12922. headlines matching this string."
  12923. (require 'org-agenda)
  12924. (let* ((re (concat "^"
  12925. (if start-level
  12926. ;; Get the correct level to match
  12927. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12928. org-outline-regexp)
  12929. " *\\(\\<\\("
  12930. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12931. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12932. (props (list 'face 'default
  12933. 'done-face 'org-agenda-done
  12934. 'undone-face 'default
  12935. 'mouse-face 'highlight
  12936. 'org-not-done-regexp org-not-done-regexp
  12937. 'org-todo-regexp org-todo-regexp
  12938. 'org-complex-heading-regexp org-complex-heading-regexp
  12939. 'help-echo
  12940. (format "mouse-2 or RET jump to org file %s"
  12941. (abbreviate-file-name
  12942. (or (buffer-file-name (buffer-base-buffer))
  12943. (buffer-name (buffer-base-buffer)))))))
  12944. (org-map-continue-from nil)
  12945. lspos tags tags-list
  12946. (tags-alist (list (cons 0 org-file-tags)))
  12947. (llast 0) rtn rtn1 level category i txt
  12948. todo marker entry priority
  12949. ts-date ts-date-type ts-date-pair)
  12950. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12951. (setq action (list 'lambda nil action)))
  12952. (save-excursion
  12953. (goto-char (point-min))
  12954. (when (eq action 'sparse-tree)
  12955. (org-overview)
  12956. (org-remove-occur-highlights))
  12957. (while (let (case-fold-search)
  12958. (re-search-forward re nil t))
  12959. (setq org-map-continue-from nil)
  12960. (catch :skip
  12961. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12962. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12963. (goto-char (setq lspos (match-beginning 0)))
  12964. (setq level (org-reduced-level (org-outline-level))
  12965. category (org-get-category))
  12966. (when (eq action 'agenda)
  12967. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12968. ts-date (car ts-date-pair)
  12969. ts-date-type (cdr ts-date-pair)))
  12970. (setq i llast llast level)
  12971. ;; remove tag lists from same and sublevels
  12972. (while (>= i level)
  12973. (when (setq entry (assoc i tags-alist))
  12974. (setq tags-alist (delete entry tags-alist)))
  12975. (setq i (1- i)))
  12976. ;; add the next tags
  12977. (when tags
  12978. (setq tags (org-split-string tags ":")
  12979. tags-alist
  12980. (cons (cons level tags) tags-alist)))
  12981. ;; compile tags for current headline
  12982. (setq tags-list
  12983. (if org-use-tag-inheritance
  12984. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12985. tags)
  12986. org-scanner-tags tags-list)
  12987. (when org-use-tag-inheritance
  12988. (setcdr (car tags-alist)
  12989. (mapcar (lambda (x)
  12990. (setq x (copy-sequence x))
  12991. (org-add-prop-inherited x))
  12992. (cdar tags-alist))))
  12993. (when (and tags org-use-tag-inheritance
  12994. (or (not (eq t org-use-tag-inheritance))
  12995. org-tags-exclude-from-inheritance))
  12996. ;; selective inheritance, remove uninherited ones
  12997. (setcdr (car tags-alist)
  12998. (org-remove-uninherited-tags (cdar tags-alist))))
  12999. (when (and
  13000. ;; eval matcher only when the todo condition is OK
  13001. (and (or (not todo-only) (member todo org-not-done-keywords))
  13002. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13003. (eval matcher)))
  13004. ;; Call the skipper, but return t if it does not skip,
  13005. ;; so that the `and' form continues evaluating
  13006. (progn
  13007. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13008. t)
  13009. ;; Check if timestamps are deselecting this entry
  13010. (or (not todo-only)
  13011. (and (member todo org-not-done-keywords)
  13012. (or (not org-agenda-tags-todo-honor-ignore-options)
  13013. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13014. ;; select this headline
  13015. (cond
  13016. ((eq action 'sparse-tree)
  13017. (and org-highlight-sparse-tree-matches
  13018. (org-get-heading) (match-end 0)
  13019. (org-highlight-new-match
  13020. (match-beginning 1) (match-end 1)))
  13021. (org-show-context 'tags-tree))
  13022. ((eq action 'agenda)
  13023. (setq txt (org-agenda-format-item
  13024. ""
  13025. (concat
  13026. (if (eq org-tags-match-list-sublevels 'indented)
  13027. (make-string (1- level) ?.) "")
  13028. (org-get-heading))
  13029. (make-string level ?\s)
  13030. category
  13031. tags-list)
  13032. priority (org-get-priority txt))
  13033. (goto-char lspos)
  13034. (setq marker (org-agenda-new-marker))
  13035. (org-add-props txt props
  13036. 'org-marker marker 'org-hd-marker marker 'org-category category
  13037. 'todo-state todo
  13038. 'ts-date ts-date
  13039. 'priority priority
  13040. 'type (concat "tagsmatch" ts-date-type))
  13041. (push txt rtn))
  13042. ((functionp action)
  13043. (setq org-map-continue-from nil)
  13044. (save-excursion
  13045. (setq rtn1 (funcall action))
  13046. (push rtn1 rtn)))
  13047. (t (user-error "Invalid action")))
  13048. ;; if we are to skip sublevels, jump to end of subtree
  13049. (unless org-tags-match-list-sublevels
  13050. (org-end-of-subtree t)
  13051. (backward-char 1))))
  13052. ;; Get the correct position from where to continue
  13053. (if org-map-continue-from
  13054. (goto-char org-map-continue-from)
  13055. (and (= (point) lspos) (end-of-line 1)))))
  13056. (when (and (eq action 'sparse-tree)
  13057. (not org-sparse-tree-open-archived-trees))
  13058. (org-hide-archived-subtrees (point-min) (point-max)))
  13059. (nreverse rtn)))
  13060. (defun org-remove-uninherited-tags (tags)
  13061. "Remove all tags that are not inherited from the list TAGS."
  13062. (cond
  13063. ((eq org-use-tag-inheritance t)
  13064. (if org-tags-exclude-from-inheritance
  13065. (org-delete-all org-tags-exclude-from-inheritance tags)
  13066. tags))
  13067. ((not org-use-tag-inheritance) nil)
  13068. ((stringp org-use-tag-inheritance)
  13069. (delq nil (mapcar
  13070. (lambda (x)
  13071. (if (and (string-match org-use-tag-inheritance x)
  13072. (not (member x org-tags-exclude-from-inheritance)))
  13073. x nil))
  13074. tags)))
  13075. ((listp org-use-tag-inheritance)
  13076. (delq nil (mapcar
  13077. (lambda (x)
  13078. (if (member x org-use-tag-inheritance) x nil))
  13079. tags)))))
  13080. (defun org-match-sparse-tree (&optional todo-only match)
  13081. "Create a sparse tree according to tags string MATCH.
  13082. MATCH can contain positive and negative selection of tags, like
  13083. \"+WORK+URGENT-WITHBOSS\".
  13084. If optional argument TODO-ONLY is non-nil, only select lines that are
  13085. also TODO lines."
  13086. (interactive "P")
  13087. (org-agenda-prepare-buffers (list (current-buffer)))
  13088. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13089. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13090. (defvar org-cached-props nil)
  13091. (defun org-cached-entry-get (pom property)
  13092. (if (or (eq t org-use-property-inheritance)
  13093. (and (stringp org-use-property-inheritance)
  13094. (let ((case-fold-search t))
  13095. (org-string-match-p org-use-property-inheritance property)))
  13096. (and (listp org-use-property-inheritance)
  13097. (member-ignore-case property org-use-property-inheritance)))
  13098. ;; Caching is not possible, check it directly.
  13099. (org-entry-get pom property 'inherit)
  13100. ;; Get all properties, so we can do complicated checks easily.
  13101. (cdr (assoc-string property
  13102. (or org-cached-props
  13103. (setq org-cached-props (org-entry-properties pom)))
  13104. t))))
  13105. (defun org-global-tags-completion-table (&optional files)
  13106. "Return the list of all tags in all agenda buffer/files.
  13107. Optional FILES argument is a list of files which can be used
  13108. instead of the agenda files."
  13109. (save-excursion
  13110. (org-uniquify
  13111. (delq nil
  13112. (apply 'append
  13113. (mapcar
  13114. (lambda (file)
  13115. (set-buffer (find-file-noselect file))
  13116. (append (org-get-buffer-tags)
  13117. (mapcar (lambda (x) (if (stringp (car-safe x))
  13118. (list (car-safe x)) nil))
  13119. org-tag-alist)))
  13120. (if (and files (car files))
  13121. files
  13122. (org-agenda-files))))))))
  13123. (defun org-make-tags-matcher (match)
  13124. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13125. The variable `todo-only' is scoped dynamically into this function.
  13126. It will be set to t if the matcher restricts matching to TODO entries,
  13127. otherwise will not be touched.
  13128. Returns a cons of the selection string MATCH and the constructed
  13129. lisp form implementing the matcher. The matcher is to be evaluated
  13130. at an Org entry, with point on the headline, and returns t if the
  13131. entry matches the selection string MATCH. The returned lisp form
  13132. references two variables with information about the entry, which
  13133. must be bound around the form's evaluation: todo, the TODO keyword
  13134. at the entry (or nil of none); and tags-list, the list of all tags
  13135. at the entry including inherited ones. Additionally, the category
  13136. of the entry (if any) must be specified as the text property
  13137. 'org-category on the headline.
  13138. See also `org-scan-tags'.
  13139. "
  13140. (declare (special todo-only))
  13141. (unless (boundp 'todo-only)
  13142. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13143. (unless match
  13144. ;; Get a new match request, with completion against the global
  13145. ;; tags table and the local tags in current buffer
  13146. (let ((org-last-tags-completion-table
  13147. (org-uniquify
  13148. (delq nil (append (org-get-buffer-tags)
  13149. (org-global-tags-completion-table))))))
  13150. (setq match (org-completing-read-no-i
  13151. "Match: " 'org-tags-completion-function nil nil nil
  13152. 'org-tags-history))))
  13153. ;; Parse the string and create a lisp form
  13154. (let ((match0 match)
  13155. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13156. minus tag mm
  13157. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13158. orterms term orlist re-p str-p level-p level-op time-p
  13159. prop-p pn pv po gv rest (start 0) (ss 0))
  13160. ;; Expand group tags
  13161. (setq match (org-tags-expand match))
  13162. ;; Check if there is a TODO part of this match, which would be the
  13163. ;; part after a "/". TO make sure that this slash is not part of
  13164. ;; a property value to be matched against, we also check that there
  13165. ;; is no " after that slash.
  13166. ;; First, find the last slash
  13167. (while (string-match "/+" match ss)
  13168. (setq start (match-beginning 0) ss (match-end 0)))
  13169. (if (and (string-match "/+" match start)
  13170. (not (save-match-data (string-match "\"" match start))))
  13171. ;; match contains also a todo-matching request
  13172. (progn
  13173. (setq tagsmatch (substring match 0 (match-beginning 0))
  13174. todomatch (substring match (match-end 0)))
  13175. (if (string-match "^!" todomatch)
  13176. (setq todo-only t todomatch (substring todomatch 1)))
  13177. (if (string-match "^\\s-*$" todomatch)
  13178. (setq todomatch nil)))
  13179. ;; only matching tags
  13180. (setq tagsmatch match todomatch nil))
  13181. ;; Make the tags matcher
  13182. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13183. (setq tagsmatcher t)
  13184. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13185. (while (setq term (pop orterms))
  13186. (while (and (equal (substring term -1) "\\") orterms)
  13187. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13188. (while (string-match re term)
  13189. (setq rest (substring term (match-end 0))
  13190. minus (and (match-end 1)
  13191. (equal (match-string 1 term) "-"))
  13192. tag (save-match-data (replace-regexp-in-string
  13193. "\\\\-" "-"
  13194. (match-string 2 term)))
  13195. re-p (equal (string-to-char tag) ?{)
  13196. level-p (match-end 4)
  13197. prop-p (match-end 5)
  13198. mm (cond
  13199. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13200. (level-p
  13201. (setq level-op (org-op-to-function (match-string 3 term)))
  13202. `(,level-op level ,(string-to-number
  13203. (match-string 4 term))))
  13204. (prop-p
  13205. (setq pn (match-string 5 term)
  13206. po (match-string 6 term)
  13207. pv (match-string 7 term)
  13208. re-p (equal (string-to-char pv) ?{)
  13209. str-p (equal (string-to-char pv) ?\")
  13210. time-p (save-match-data
  13211. (string-match "^\"[[<].*[]>]\"$" pv))
  13212. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13213. (if time-p (setq pv (org-matcher-time pv)))
  13214. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13215. (cond
  13216. ((equal pn "CATEGORY")
  13217. (setq gv '(get-text-property (point) 'org-category)))
  13218. ((equal pn "TODO")
  13219. (setq gv 'todo))
  13220. (t
  13221. (setq gv `(org-cached-entry-get nil ,pn))))
  13222. (if re-p
  13223. (if (eq po 'org<>)
  13224. `(not (string-match ,pv (or ,gv "")))
  13225. `(string-match ,pv (or ,gv "")))
  13226. (if str-p
  13227. `(,po (or ,gv "") ,pv)
  13228. `(,po (string-to-number (or ,gv ""))
  13229. ,(string-to-number pv) ))))
  13230. (t `(member ,tag tags-list)))
  13231. mm (if minus (list 'not mm) mm)
  13232. term rest)
  13233. (push mm tagsmatcher))
  13234. (push (if (> (length tagsmatcher) 1)
  13235. (cons 'and tagsmatcher)
  13236. (car tagsmatcher))
  13237. orlist)
  13238. (setq tagsmatcher nil))
  13239. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13240. (setq tagsmatcher
  13241. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13242. ;; Make the todo matcher
  13243. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13244. (setq todomatcher t)
  13245. (setq orterms (org-split-string todomatch "|") orlist nil)
  13246. (dolist (term orterms)
  13247. (while (string-match re term)
  13248. (setq minus (and (match-end 1)
  13249. (equal (match-string 1 term) "-"))
  13250. kwd (match-string 2 term)
  13251. re-p (equal (string-to-char kwd) ?{)
  13252. term (substring term (match-end 0))
  13253. mm (if re-p
  13254. `(string-match ,(substring kwd 1 -1) todo)
  13255. (list 'equal 'todo kwd))
  13256. mm (if minus (list 'not mm) mm))
  13257. (push mm todomatcher))
  13258. (push (if (> (length todomatcher) 1)
  13259. (cons 'and todomatcher)
  13260. (car todomatcher))
  13261. orlist)
  13262. (setq todomatcher nil))
  13263. (setq todomatcher (if (> (length orlist) 1)
  13264. (cons 'or orlist) (car orlist))))
  13265. ;; Return the string and lisp forms of the matcher
  13266. (setq matcher (if todomatcher
  13267. (list 'and tagsmatcher todomatcher)
  13268. tagsmatcher))
  13269. (when todo-only
  13270. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13271. matcher)))
  13272. (cons match0 matcher)))
  13273. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13274. "Expand group tags in MATCH.
  13275. This replaces every group tag in MATCH with a regexp tag search.
  13276. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13277. will be replaced like this:
  13278. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13279. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13280. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13281. Replacing by a regexp preserves the structure of the match.
  13282. E.g., this expansion
  13283. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13284. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13285. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13286. A group tag in MATCH can contain regular expressions of its own.
  13287. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13288. will be replaced like this:
  13289. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13290. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13291. assumed to be a single group tag, and the function will return
  13292. the list of tags in this group.
  13293. When DOWNCASE is non-nil, expand downcased TAGS."
  13294. (if org-group-tags
  13295. (let* ((case-fold-search t)
  13296. (stable org-mode-syntax-table)
  13297. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13298. (taggroups (if downcased
  13299. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13300. taggroups)
  13301. taggroups))
  13302. (taggroups-keys (mapcar #'car taggroups))
  13303. (return-match (if downcased (downcase match) match))
  13304. (count 0)
  13305. (work-already-expanded tags-already-expanded)
  13306. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13307. ;; @ and _ are allowed as word-components in tags.
  13308. (modify-syntax-entry ?@ "w" stable)
  13309. (modify-syntax-entry ?_ "w" stable)
  13310. ;; Temporarily replace regexp-expressions in the match-expression.
  13311. (while (string-match "{.+?}" return-match)
  13312. (incf count)
  13313. (push (match-string 0 return-match) regexps-in-match)
  13314. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13315. (while (and taggroups-keys
  13316. (with-syntax-table stable
  13317. (string-match
  13318. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13319. (regexp-opt taggroups-keys) "\\>\\)")
  13320. return-match)))
  13321. (let* ((dir (match-string 1 return-match))
  13322. (tag (match-string 2 return-match))
  13323. (tag (if downcased (downcase tag) tag)))
  13324. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13325. (member tag work-already-expanded))
  13326. (setq tags-in-group (assoc tag taggroups))
  13327. (push tag work-already-expanded)
  13328. ;; Recursively expand each tag in the group, if the tag hasn't
  13329. ;; already been expanded. Restore the match-data after all recursive calls.
  13330. (save-match-data
  13331. (let (tags-expanded)
  13332. (dolist (x (cdr tags-in-group))
  13333. (if (and (member x taggroups-keys)
  13334. (not (member x work-already-expanded)))
  13335. (setq tags-expanded
  13336. (delete-dups
  13337. (append
  13338. (org-tags-expand x t downcased
  13339. work-already-expanded)
  13340. tags-expanded)))
  13341. (setq tags-expanded
  13342. (append (list x) tags-expanded)))
  13343. (setq work-already-expanded
  13344. (delete-dups
  13345. (append tags-expanded
  13346. work-already-expanded))))
  13347. (setq tags-in-group
  13348. (delete-dups (cons (car tags-in-group)
  13349. tags-expanded)))))
  13350. ;; Filter tag-regexps from tags.
  13351. (setq regexp-in-group-escaped
  13352. (delq nil (mapcar (lambda (x)
  13353. (if (stringp x)
  13354. (and (equal "{" (substring x 0 1))
  13355. (equal "}" (substring x -1))
  13356. x)
  13357. x))
  13358. tags-in-group))
  13359. regexp-in-group
  13360. (mapcar (lambda (x)
  13361. (substring x 1 -1))
  13362. regexp-in-group-escaped)
  13363. tags-in-group
  13364. (delq nil (mapcar (lambda (x)
  13365. (if (stringp x)
  13366. (and (not (equal "{" (substring x 0 1)))
  13367. (not (equal "}" (substring x -1)))
  13368. x)
  13369. x))
  13370. tags-in-group)))
  13371. ;; If single-as-list, do no more in the while-loop.
  13372. (if (not single-as-list)
  13373. (progn
  13374. (when regexp-in-group
  13375. (setq regexp-in-group
  13376. (concat "\\|"
  13377. (mapconcat 'identity regexp-in-group
  13378. "\\|"))))
  13379. (setq tags-in-group
  13380. (concat dir
  13381. "{\\<"
  13382. (regexp-opt tags-in-group)
  13383. "\\>"
  13384. regexp-in-group
  13385. "}"))
  13386. (when (stringp tags-in-group)
  13387. (org-add-props tags-in-group '(grouptag t)))
  13388. (setq return-match
  13389. (replace-match tags-in-group t t return-match)))
  13390. (setq tags-in-group
  13391. (append regexp-in-group-escaped tags-in-group))))
  13392. (setq taggroups-keys (delete tag taggroups-keys))))
  13393. ;; Add the regular expressions back into the match-expression again.
  13394. (while regexps-in-match
  13395. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13396. (pop regexps-in-match)
  13397. return-match t t))
  13398. (decf count))
  13399. (if single-as-list
  13400. (if tags-in-group tags-in-group (list return-match))
  13401. return-match))
  13402. (if single-as-list
  13403. (list (if downcased (downcase match) match))
  13404. match)))
  13405. (defun org-op-to-function (op &optional stringp)
  13406. "Turn an operator into the appropriate function."
  13407. (setq op
  13408. (cond
  13409. ((equal op "<" ) '(< string< org-time<))
  13410. ((equal op ">" ) '(> org-string> org-time>))
  13411. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13412. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13413. ((member op '("=" "==")) '(= string= org-time=))
  13414. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13415. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13416. (defun org<> (a b) (not (= a b)))
  13417. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13418. (defun org-string>= (a b) (not (string< a b)))
  13419. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13420. (defun org-string<> (a b) (not (string= a b)))
  13421. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13422. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< 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) (org<> a b)))
  13427. (defun org-2ft (s)
  13428. "Convert S to a floating point time.
  13429. If S is already a number, just return it. If it is a string, parse
  13430. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13431. (cond
  13432. ((numberp s) s)
  13433. ((stringp s)
  13434. (condition-case nil
  13435. (float-time (apply 'encode-time (org-parse-time-string s)))
  13436. (error 0.)))
  13437. (t 0.)))
  13438. (defun org-time-today ()
  13439. "Time in seconds today at 0:00.
  13440. Returns the float number of seconds since the beginning of the
  13441. epoch to the beginning of today (00:00)."
  13442. (float-time (apply 'encode-time
  13443. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13444. (defun org-matcher-time (s)
  13445. "Interpret a time comparison value."
  13446. (save-match-data
  13447. (cond
  13448. ((string= s "<now>") (float-time))
  13449. ((string= s "<today>") (org-time-today))
  13450. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13451. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13452. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13453. (+ (org-time-today)
  13454. (* (string-to-number (match-string 1 s))
  13455. (cdr (assoc (match-string 2 s)
  13456. '(("d" . 86400.0) ("w" . 604800.0)
  13457. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13458. (t (org-2ft s)))))
  13459. (defun org-match-any-p (re list)
  13460. "Does re match any element of list?"
  13461. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13462. (delq nil list))
  13463. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13464. (defvar org-tags-overlay (make-overlay 1 1))
  13465. (org-detach-overlay org-tags-overlay)
  13466. (defun org-get-local-tags-at (&optional pos)
  13467. "Get a list of tags defined in the current headline."
  13468. (org-get-tags-at pos 'local))
  13469. (defun org-get-local-tags ()
  13470. "Get a list of tags defined in the current headline."
  13471. (org-get-tags-at nil 'local))
  13472. (defun org-get-tags-at (&optional pos local)
  13473. "Get a list of all headline tags applicable at POS.
  13474. POS defaults to point. If tags are inherited, the list contains
  13475. the targets in the same sequence as the headlines appear, i.e.
  13476. the tags of the current headline come last.
  13477. When LOCAL is non-nil, only return tags from the current headline,
  13478. ignore inherited ones."
  13479. (interactive)
  13480. (if (and org-trust-scanner-tags
  13481. (or (not pos) (equal pos (point)))
  13482. (not local))
  13483. org-scanner-tags
  13484. (let (tags ltags lastpos parent)
  13485. (save-excursion
  13486. (save-restriction
  13487. (widen)
  13488. (goto-char (or pos (point)))
  13489. (save-match-data
  13490. (catch 'done
  13491. (condition-case nil
  13492. (progn
  13493. (org-back-to-heading t)
  13494. (while (not (equal lastpos (point)))
  13495. (setq lastpos (point))
  13496. (when (looking-at
  13497. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13498. (setq ltags (org-split-string
  13499. (org-match-string-no-properties 1) ":"))
  13500. (when parent
  13501. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13502. (setq tags (append
  13503. (if parent
  13504. (org-remove-uninherited-tags ltags)
  13505. ltags)
  13506. tags)))
  13507. (or org-use-tag-inheritance (throw 'done t))
  13508. (if local (throw 'done t))
  13509. (or (org-up-heading-safe) (error nil))
  13510. (setq parent t)))
  13511. (error nil)))))
  13512. (if local
  13513. tags
  13514. (reverse (delete-dups
  13515. (reverse (append
  13516. (org-remove-uninherited-tags
  13517. org-file-tags)
  13518. tags)))))))))
  13519. (defun org-add-prop-inherited (s)
  13520. (add-text-properties 0 (length s) '(inherited t) s)
  13521. s)
  13522. (defun org-toggle-tag (tag &optional onoff)
  13523. "Toggle the tag TAG for the current line.
  13524. If ONOFF is `on' or `off', don't toggle but set to this state."
  13525. (let (res current)
  13526. (save-excursion
  13527. (org-back-to-heading t)
  13528. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13529. (point-at-eol) t)
  13530. (progn
  13531. (setq current (match-string 1))
  13532. (replace-match ""))
  13533. (setq current ""))
  13534. (setq current (nreverse (org-split-string current ":")))
  13535. (cond
  13536. ((eq onoff 'on)
  13537. (setq res t)
  13538. (or (member tag current) (push tag current)))
  13539. ((eq onoff 'off)
  13540. (or (not (member tag current)) (setq current (delete tag current))))
  13541. (t (if (member tag current)
  13542. (setq current (delete tag current))
  13543. (setq res t)
  13544. (push tag current))))
  13545. (end-of-line 1)
  13546. (if current
  13547. (progn
  13548. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13549. (org-set-tags nil t))
  13550. (delete-horizontal-space))
  13551. (run-hooks 'org-after-tags-change-hook))
  13552. res))
  13553. (defun org-align-tags-here (to-col)
  13554. ;; Assumes that this is a headline
  13555. "Align tags on the current headline to TO-COL."
  13556. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13557. (beginning-of-line 1)
  13558. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13559. (< pos (match-beginning 2)))
  13560. (progn
  13561. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13562. (goto-char (match-beginning 1))
  13563. (insert " ")
  13564. (delete-region (point) (1+ (match-beginning 2)))
  13565. (setq ncol (max (current-column)
  13566. (1+ col)
  13567. (if (> to-col 0)
  13568. to-col
  13569. (- (abs to-col) tags-l))))
  13570. (setq p (point))
  13571. (insert (make-string (- ncol (current-column)) ?\ ))
  13572. (setq ncol (current-column))
  13573. (when indent-tabs-mode (tabify p (point-at-eol)))
  13574. (org-move-to-column (min ncol col)))
  13575. (goto-char pos))))
  13576. (defun org-set-tags-command (&optional arg just-align)
  13577. "Call the set-tags command for the current entry."
  13578. (interactive "P")
  13579. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13580. (org-set-tags arg just-align)
  13581. (save-excursion
  13582. (unless (and (org-region-active-p)
  13583. org-loop-over-headlines-in-active-region)
  13584. (org-back-to-heading t))
  13585. (org-set-tags arg just-align))))
  13586. (defun org-set-tags-to (data)
  13587. "Set the tags of the current entry to DATA, replacing the current tags.
  13588. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13589. If DATA is nil or the empty string, any tags will be removed."
  13590. (interactive "sTags: ")
  13591. (setq data
  13592. (cond
  13593. ((eq data nil) "")
  13594. ((equal data "") "")
  13595. ((stringp data)
  13596. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13597. ":"))
  13598. ((listp data)
  13599. (concat ":" (mapconcat 'identity data ":") ":"))))
  13600. (when data
  13601. (save-excursion
  13602. (org-back-to-heading t)
  13603. (when (looking-at org-complex-heading-regexp)
  13604. (if (match-end 5)
  13605. (progn
  13606. (goto-char (match-beginning 5))
  13607. (insert data)
  13608. (delete-region (point) (point-at-eol))
  13609. (org-set-tags nil 'align))
  13610. (goto-char (point-at-eol))
  13611. (insert " " data)
  13612. (org-set-tags nil 'align)))
  13613. (beginning-of-line 1)
  13614. (if (looking-at ".*?\\([ \t]+\\)$")
  13615. (delete-region (match-beginning 1) (match-end 1))))))
  13616. (defun org-align-all-tags ()
  13617. "Align the tags i all headings."
  13618. (interactive)
  13619. (save-excursion
  13620. (or (ignore-errors (org-back-to-heading t))
  13621. (outline-next-heading))
  13622. (if (org-at-heading-p)
  13623. (org-set-tags t)
  13624. (message "No headings"))))
  13625. (defvar org-indent-indentation-per-level)
  13626. (defun org-set-tags (&optional arg just-align)
  13627. "Set the tags for the current headline.
  13628. With prefix ARG, realign all tags in headings in the current buffer.
  13629. When JUST-ALIGN is non-nil, only align tags."
  13630. (interactive "P")
  13631. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13632. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13633. 'region-start-level
  13634. 'region))
  13635. org-loop-over-headlines-in-active-region)
  13636. (org-map-entries
  13637. ;; We don't use ARG and JUST-ALIGN here because these args
  13638. ;; are not useful when looping over headlines.
  13639. #'org-set-tags
  13640. org-loop-over-headlines-in-active-region
  13641. cl
  13642. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13643. (let ((org-setting-tags t))
  13644. (if arg
  13645. (save-excursion
  13646. (goto-char (point-min))
  13647. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13648. (while (re-search-forward org-outline-regexp-bol nil t)
  13649. (org-set-tags nil t)
  13650. (end-of-line)))
  13651. (message "All tags realigned to column %d" org-tags-column))
  13652. (let* ((current (org-get-tags-string))
  13653. (col (current-column))
  13654. (tags
  13655. (if just-align current
  13656. ;; Get a new set of tags from the user.
  13657. (save-excursion
  13658. (let* ((table
  13659. (setq
  13660. org-last-tags-completion-table
  13661. (append
  13662. org-tag-persistent-alist
  13663. (or org-tag-alist (org-get-buffer-tags))
  13664. (and
  13665. org-complete-tags-always-offer-all-agenda-tags
  13666. (org-global-tags-completion-table
  13667. (org-agenda-files))))))
  13668. (current-tags (org-split-string current ":"))
  13669. (inherited-tags
  13670. (nreverse (nthcdr (length current-tags)
  13671. (nreverse (org-get-tags-at))))))
  13672. (replace-regexp-in-string
  13673. "\\([-+&]+\\|,\\)"
  13674. ":"
  13675. (if (or (eq t org-use-fast-tag-selection)
  13676. (and org-use-fast-tag-selection
  13677. (delq nil (mapcar #'cdr table))))
  13678. (org-fast-tag-selection
  13679. current-tags inherited-tags table
  13680. (and org-fast-tag-selection-include-todo
  13681. org-todo-key-alist))
  13682. (let ((org-add-colon-after-tag-completion
  13683. (< 1 (length table))))
  13684. (org-trim
  13685. (completing-read
  13686. "Tags: "
  13687. #'org-tags-completion-function
  13688. nil nil current 'org-tags-history))))))))))
  13689. (when org-tags-sort-function
  13690. (setq tags
  13691. (mapconcat
  13692. #'identity
  13693. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13694. org-tags-sort-function)
  13695. ":")))
  13696. (if (not (org-string-nw-p tags)) (setq tags "")
  13697. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13698. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13699. ;; Insert new tags at the correct column
  13700. (beginning-of-line)
  13701. (let ((level (if (looking-at org-outline-regexp)
  13702. (- (match-end 0) (point) 1)
  13703. 1)))
  13704. (cond
  13705. ((and (equal current "") (equal tags "")))
  13706. ((re-search-forward
  13707. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13708. (line-end-position)
  13709. t)
  13710. (if (equal tags "") (replace-match "" t t)
  13711. (goto-char (match-beginning 0))
  13712. (let* ((c0 (current-column))
  13713. ;; Compute offset for the case of org-indent-mode
  13714. ;; active.
  13715. (di (if (org-bound-and-true-p org-indent-mode)
  13716. (* (1- org-indent-indentation-per-level)
  13717. (1- level))
  13718. 0))
  13719. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13720. (tc (+ org-tags-column
  13721. (if (> org-tags-column 0) (- di) di)))
  13722. (c1 (max (1+ c0)
  13723. (if (> tc 0) tc
  13724. (- (- tc) (string-width tags)))))
  13725. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13726. (replace-match rpl t t)
  13727. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13728. (tabify p0 (point))))))
  13729. (t (error "Tags alignment failed"))))
  13730. (org-move-to-column col))
  13731. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13732. (defun org-change-tag-in-region (beg end tag off)
  13733. "Add or remove TAG for each entry in the region.
  13734. This works in the agenda, and also in an org-mode buffer."
  13735. (interactive
  13736. (list (region-beginning) (region-end)
  13737. (let ((org-last-tags-completion-table
  13738. (if (derived-mode-p 'org-mode)
  13739. (org-uniquify
  13740. (delq nil (append (org-get-buffer-tags)
  13741. (org-global-tags-completion-table))))
  13742. (org-global-tags-completion-table))))
  13743. (org-icompleting-read
  13744. "Tag: " 'org-tags-completion-function nil nil nil
  13745. 'org-tags-history))
  13746. (progn
  13747. (message "[s]et or [r]emove? ")
  13748. (equal (read-char-exclusive) ?r))))
  13749. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13750. (let ((agendap (equal major-mode 'org-agenda-mode))
  13751. l1 l2 m buf pos newhead (cnt 0))
  13752. (goto-char end)
  13753. (setq l2 (1- (org-current-line)))
  13754. (goto-char beg)
  13755. (setq l1 (org-current-line))
  13756. (loop for l from l1 to l2 do
  13757. (org-goto-line l)
  13758. (setq m (get-text-property (point) 'org-hd-marker))
  13759. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13760. (and agendap m))
  13761. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13762. pos (if agendap m (point)))
  13763. (with-current-buffer buf
  13764. (save-excursion
  13765. (save-restriction
  13766. (goto-char pos)
  13767. (setq cnt (1+ cnt))
  13768. (org-toggle-tag tag (if off 'off 'on))
  13769. (setq newhead (org-get-heading)))))
  13770. (and agendap (org-agenda-change-all-lines newhead m))))
  13771. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13772. (defun org-tags-completion-function (string predicate &optional flag)
  13773. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13774. (confirm (lambda (x) (stringp (car x)))))
  13775. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13776. (setq s1 (match-string 1 string)
  13777. s2 (match-string 2 string))
  13778. (setq s1 "" s2 string))
  13779. (cond
  13780. ((eq flag nil)
  13781. ;; try completion
  13782. (setq rtn (try-completion s2 ctable confirm))
  13783. (if (stringp rtn)
  13784. (setq rtn
  13785. (concat s1 s2 (substring rtn (length s2))
  13786. (if (and org-add-colon-after-tag-completion
  13787. (assoc rtn ctable))
  13788. ":" ""))))
  13789. rtn)
  13790. ((eq flag t)
  13791. ;; all-completions
  13792. (all-completions s2 ctable confirm))
  13793. ((eq flag 'lambda)
  13794. ;; exact match?
  13795. (assoc s2 ctable)))))
  13796. (defun org-fast-tag-insert (kwd tags face &optional end)
  13797. "Insert KDW, and the TAGS, the latter with face FACE.
  13798. Also insert END."
  13799. (insert (format "%-12s" (concat kwd ":"))
  13800. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13801. (or end "")))
  13802. (defun org-fast-tag-show-exit (flag)
  13803. (save-excursion
  13804. (org-goto-line 3)
  13805. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13806. (replace-match ""))
  13807. (when flag
  13808. (end-of-line 1)
  13809. (org-move-to-column (- (window-width) 19) t)
  13810. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13811. (defun org-set-current-tags-overlay (current prefix)
  13812. "Add an overlay to CURRENT tag with PREFIX."
  13813. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13814. (if (featurep 'xemacs)
  13815. (org-overlay-display org-tags-overlay (concat prefix s)
  13816. 'secondary-selection)
  13817. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13818. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13819. (defvar org-last-tag-selection-key nil)
  13820. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13821. "Fast tag selection with single keys.
  13822. CURRENT is the current list of tags in the headline, INHERITED is the
  13823. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13824. possibly with grouping information. TODO-TABLE is a similar table with
  13825. TODO keywords, should these have keys assigned to them.
  13826. If the keys are nil, a-z are automatically assigned.
  13827. Returns the new tags string, or nil to not change the current settings."
  13828. (let* ((fulltable (append table todo-table))
  13829. (maxlen (apply 'max (mapcar
  13830. (lambda (x)
  13831. (if (stringp (car x)) (string-width (car x)) 0))
  13832. fulltable)))
  13833. (buf (current-buffer))
  13834. (expert (eq org-fast-tag-selection-single-key 'expert))
  13835. (buffer-tags nil)
  13836. (fwidth (+ maxlen 3 1 3))
  13837. (ncol (/ (- (window-width) 4) fwidth))
  13838. (i-face 'org-done)
  13839. (c-face 'org-todo)
  13840. tg cnt e c char c1 c2 ntable tbl rtn
  13841. ov-start ov-end ov-prefix
  13842. (exit-after-next org-fast-tag-selection-single-key)
  13843. (done-keywords org-done-keywords)
  13844. groups ingroup intaggroup)
  13845. (save-excursion
  13846. (beginning-of-line 1)
  13847. (if (looking-at
  13848. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13849. (setq ov-start (match-beginning 1)
  13850. ov-end (match-end 1)
  13851. ov-prefix "")
  13852. (setq ov-start (1- (point-at-eol))
  13853. ov-end (1+ ov-start))
  13854. (skip-chars-forward "^\n\r")
  13855. (setq ov-prefix
  13856. (concat
  13857. (buffer-substring (1- (point)) (point))
  13858. (if (> (current-column) org-tags-column)
  13859. " "
  13860. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13861. (move-overlay org-tags-overlay ov-start ov-end)
  13862. (save-window-excursion
  13863. (if expert
  13864. (set-buffer (get-buffer-create " *Org tags*"))
  13865. (delete-other-windows)
  13866. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13867. (org-switch-to-buffer-other-window " *Org tags*"))
  13868. (erase-buffer)
  13869. (org-set-local 'org-done-keywords done-keywords)
  13870. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13871. (org-fast-tag-insert "Current" current c-face "\n\n")
  13872. (org-fast-tag-show-exit exit-after-next)
  13873. (org-set-current-tags-overlay current ov-prefix)
  13874. (setq tbl fulltable char ?a cnt 0)
  13875. (while (setq e (pop tbl))
  13876. (cond
  13877. ((eq (car e) :startgroup)
  13878. (push '() groups) (setq ingroup t)
  13879. (unless (zerop cnt)
  13880. (setq cnt 0)
  13881. (insert "\n"))
  13882. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13883. ((eq (car e) :endgroup)
  13884. (setq ingroup nil cnt 0)
  13885. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13886. ((eq (car e) :startgrouptag)
  13887. (setq intaggroup t)
  13888. (unless (zerop cnt)
  13889. (setq cnt 0)
  13890. (insert "\n"))
  13891. (insert "[ "))
  13892. ((eq (car e) :endgrouptag)
  13893. (setq intaggroup nil cnt 0)
  13894. (insert "]\n"))
  13895. ((equal e '(:newline))
  13896. (unless (zerop cnt)
  13897. (setq cnt 0)
  13898. (insert "\n")
  13899. (setq e (car tbl))
  13900. (while (equal (car tbl) '(:newline))
  13901. (insert "\n")
  13902. (setq tbl (cdr tbl)))))
  13903. ((equal e '(:grouptags)) (insert " : "))
  13904. (t
  13905. (setq tg (copy-sequence (car e)) c2 nil)
  13906. (if (cdr e)
  13907. (setq c (cdr e))
  13908. ;; automatically assign a character.
  13909. (setq c1 (string-to-char
  13910. (downcase (substring
  13911. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13912. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13913. (while (or (rassoc char ntable) (rassoc char table))
  13914. (setq char (1+ char)))
  13915. (setq c2 c1))
  13916. (setq c (or c2 char)))
  13917. (when ingroup (push tg (car groups)))
  13918. (setq tg (org-add-props tg nil 'face
  13919. (cond
  13920. ((not (assoc tg table))
  13921. (org-get-todo-face tg))
  13922. ((member tg current) c-face)
  13923. ((member tg inherited) i-face))))
  13924. (when (equal (caar tbl) :grouptags)
  13925. (org-add-props tg nil 'face 'org-tag-group))
  13926. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13927. (insert "[" c "] " tg (make-string
  13928. (- fwidth 4 (length tg)) ?\ ))
  13929. (push (cons tg c) ntable)
  13930. (when (= (incf cnt) ncol)
  13931. (insert "\n")
  13932. (when (or ingroup intaggroup) (insert " "))
  13933. (setq cnt 0)))))
  13934. (setq ntable (nreverse ntable))
  13935. (insert "\n")
  13936. (goto-char (point-min))
  13937. (unless expert (org-fit-window-to-buffer))
  13938. (setq rtn
  13939. (catch 'exit
  13940. (while t
  13941. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13942. (if (not groups) "no " "")
  13943. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13944. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13945. (setq org-last-tag-selection-key c)
  13946. (cond
  13947. ((= c ?\r) (throw 'exit t))
  13948. ((= c ?!)
  13949. (setq groups (not groups))
  13950. (goto-char (point-min))
  13951. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13952. ((= c ?\C-c)
  13953. (if (not expert)
  13954. (org-fast-tag-show-exit
  13955. (setq exit-after-next (not exit-after-next)))
  13956. (setq expert nil)
  13957. (delete-other-windows)
  13958. (set-window-buffer (split-window-vertically) " *Org tags*")
  13959. (org-switch-to-buffer-other-window " *Org tags*")
  13960. (org-fit-window-to-buffer)))
  13961. ((or (= c ?\C-g)
  13962. (and (= c ?q) (not (rassoc c ntable))))
  13963. (org-detach-overlay org-tags-overlay)
  13964. (setq quit-flag t))
  13965. ((= c ?\ )
  13966. (setq current nil)
  13967. (when exit-after-next (setq exit-after-next 'now)))
  13968. ((= c ?\t)
  13969. (condition-case nil
  13970. (setq tg (org-icompleting-read
  13971. "Tag: "
  13972. (or buffer-tags
  13973. (with-current-buffer buf
  13974. (setq buffer-tags
  13975. (org-get-buffer-tags))))))
  13976. (quit (setq tg "")))
  13977. (when (string-match "\\S-" tg)
  13978. (add-to-list 'buffer-tags (list tg))
  13979. (if (member tg current)
  13980. (setq current (delete tg current))
  13981. (push tg current)))
  13982. (when exit-after-next (setq exit-after-next 'now)))
  13983. ((setq e (rassoc c todo-table) tg (car e))
  13984. (with-current-buffer buf
  13985. (save-excursion (org-todo tg)))
  13986. (when exit-after-next (setq exit-after-next 'now)))
  13987. ((setq e (rassoc c ntable) tg (car e))
  13988. (if (member tg current)
  13989. (setq current (delete tg current))
  13990. (loop for g in groups do
  13991. (when (member tg g)
  13992. (dolist (x g) (setq current (delete x current)))))
  13993. (push tg current))
  13994. (when exit-after-next (setq exit-after-next 'now))))
  13995. ;; Create a sorted list
  13996. (setq current
  13997. (sort current
  13998. (lambda (a b)
  13999. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14000. (when (eq exit-after-next 'now) (throw 'exit t))
  14001. (goto-char (point-min))
  14002. (beginning-of-line 2)
  14003. (delete-region (point) (point-at-eol))
  14004. (org-fast-tag-insert "Current" current c-face)
  14005. (org-set-current-tags-overlay current ov-prefix)
  14006. (while (re-search-forward
  14007. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14008. (setq tg (match-string 1))
  14009. (add-text-properties
  14010. (match-beginning 1) (match-end 1)
  14011. (list 'face
  14012. (cond
  14013. ((member tg current) c-face)
  14014. ((member tg inherited) i-face)
  14015. (t (get-text-property (match-beginning 1) 'face))))))
  14016. (goto-char (point-min)))))
  14017. (org-detach-overlay org-tags-overlay)
  14018. (if rtn
  14019. (mapconcat 'identity current ":")
  14020. nil))))
  14021. (defun org-get-tags-string ()
  14022. "Get the TAGS string in the current headline."
  14023. (unless (org-at-heading-p t)
  14024. (user-error "Not on a heading"))
  14025. (save-excursion
  14026. (beginning-of-line 1)
  14027. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14028. (org-match-string-no-properties 1)
  14029. "")))
  14030. (defun org-get-tags ()
  14031. "Get the list of tags specified in the current headline."
  14032. (org-split-string (org-get-tags-string) ":"))
  14033. (defun org-get-buffer-tags ()
  14034. "Get a table of all tags used in the buffer, for completion."
  14035. (org-with-wide-buffer
  14036. (goto-char (point-min))
  14037. (let ((tag-re (concat org-outline-regexp-bol
  14038. "\\(?:.*?[ \t]\\)?"
  14039. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14040. tags)
  14041. (while (re-search-forward tag-re nil t)
  14042. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14043. (push tag tags)))
  14044. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14045. ;;;; The mapping API
  14046. (defun org-map-entries (func &optional match scope &rest skip)
  14047. "Call FUNC at each headline selected by MATCH in SCOPE.
  14048. FUNC is a function or a lisp form. The function will be called without
  14049. arguments, with the cursor positioned at the beginning of the headline.
  14050. The return values of all calls to the function will be collected and
  14051. returned as a list.
  14052. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14053. does not need to preserve point. After evaluation, the cursor will be
  14054. moved to the end of the line (presumably of the headline of the
  14055. processed entry) and search continues from there. Under some
  14056. circumstances, this may not produce the wanted results. For example,
  14057. if you have removed (e.g. archived) the current (sub)tree it could
  14058. mean that the next entry will be skipped entirely. In such cases, you
  14059. can specify the position from where search should continue by making
  14060. FUNC set the variable `org-map-continue-from' to the desired buffer
  14061. position.
  14062. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14063. Only headlines that are matched by this query will be considered during
  14064. the iteration. When MATCH is nil or t, all headlines will be
  14065. visited by the iteration.
  14066. SCOPE determines the scope of this command. It can be any of:
  14067. nil The current buffer, respecting the restriction if any
  14068. tree The subtree started with the entry at point
  14069. region The entries within the active region, if any
  14070. region-start-level
  14071. The entries within the active region, but only those at
  14072. the same level than the first one.
  14073. file The current buffer, without restriction
  14074. file-with-archives
  14075. The current buffer, and any archives associated with it
  14076. agenda All agenda files
  14077. agenda-with-archives
  14078. All agenda files with any archive files associated with them
  14079. \(file1 file2 ...)
  14080. If this is a list, all files in the list will be scanned
  14081. The remaining args are treated as settings for the skipping facilities of
  14082. the scanner. The following items can be given here:
  14083. archive skip trees with the archive tag
  14084. comment skip trees with the COMMENT keyword
  14085. function or Emacs Lisp form:
  14086. will be used as value for `org-agenda-skip-function', so
  14087. whenever the function returns a position, FUNC will not be
  14088. called for that entry and search will continue from the
  14089. position returned
  14090. If your function needs to retrieve the tags including inherited tags
  14091. at the *current* entry, you can use the value of the variable
  14092. `org-scanner-tags' which will be much faster than getting the value
  14093. with `org-get-tags-at'. If your function gets properties with
  14094. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14095. to t around the call to `org-entry-properties' to get the same speedup.
  14096. Note that if your function moves around to retrieve tags and properties at
  14097. a *different* entry, you cannot use these techniques."
  14098. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14099. (not (org-region-active-p)))
  14100. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14101. (org-agenda-skip-archived-trees (memq 'archive skip))
  14102. (org-agenda-skip-comment-trees (memq 'comment skip))
  14103. (org-agenda-skip-function
  14104. (car (org-delete-all '(comment archive) skip)))
  14105. (org-tags-match-list-sublevels t)
  14106. (start-level (eq scope 'region-start-level))
  14107. matcher file res
  14108. org-todo-keywords-for-agenda
  14109. org-done-keywords-for-agenda
  14110. org-todo-keyword-alist-for-agenda
  14111. org-tag-alist-for-agenda
  14112. todo-only)
  14113. (cond
  14114. ((eq match t) (setq matcher t))
  14115. ((eq match nil) (setq matcher t))
  14116. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14117. (save-excursion
  14118. (save-restriction
  14119. (cond ((eq scope 'tree)
  14120. (org-back-to-heading t)
  14121. (org-narrow-to-subtree)
  14122. (setq scope nil))
  14123. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14124. (org-region-active-p))
  14125. ;; If needed, set start-level to a string like "2"
  14126. (when start-level
  14127. (save-excursion
  14128. (goto-char (region-beginning))
  14129. (unless (org-at-heading-p) (outline-next-heading))
  14130. (setq start-level (org-current-level))))
  14131. (narrow-to-region (region-beginning)
  14132. (save-excursion
  14133. (goto-char (region-end))
  14134. (unless (and (bolp) (org-at-heading-p))
  14135. (outline-next-heading))
  14136. (point)))
  14137. (setq scope nil)))
  14138. (if (not scope)
  14139. (progn
  14140. (org-agenda-prepare-buffers
  14141. (and buffer-file-name (list buffer-file-name)))
  14142. (setq res (org-scan-tags func matcher todo-only start-level)))
  14143. ;; Get the right scope
  14144. (cond
  14145. ((and scope (listp scope) (symbolp (car scope)))
  14146. (setq scope (eval scope)))
  14147. ((eq scope 'agenda)
  14148. (setq scope (org-agenda-files t)))
  14149. ((eq scope 'agenda-with-archives)
  14150. (setq scope (org-agenda-files t))
  14151. (setq scope (org-add-archive-files scope)))
  14152. ((eq scope 'file)
  14153. (setq scope (and buffer-file-name (list buffer-file-name))))
  14154. ((eq scope 'file-with-archives)
  14155. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14156. (org-agenda-prepare-buffers scope)
  14157. (dolist (file scope)
  14158. (with-current-buffer (org-find-base-buffer-visiting file)
  14159. (save-excursion
  14160. (save-restriction
  14161. (widen)
  14162. (goto-char (point-min))
  14163. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14164. res)))
  14165. ;;; Properties API
  14166. (defconst org-special-properties
  14167. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14168. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14169. "The special properties valid in Org mode.
  14170. These are properties that are not defined in the property drawer,
  14171. but in some other way.")
  14172. (defconst org-default-properties
  14173. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14174. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14175. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14176. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14177. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14178. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14179. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14180. "Some properties that are used by Org mode for various purposes.
  14181. Being in this list makes sure that they are offered for completion.")
  14182. (defun org--update-property-plist (key val props)
  14183. "Associate KEY to VAL in alist PROPS.
  14184. Modifications are made by side-effect. Return new alist."
  14185. (let* ((appending (string= (substring key -1) "+"))
  14186. (key (if appending (substring key 0 -1) key))
  14187. (old (assoc-string key props t)))
  14188. (if (not old) (cons (cons key val) props)
  14189. (setcdr old (if appending (concat (cdr old) " " val) val))
  14190. props)))
  14191. (defun org-get-property-block (&optional beg force)
  14192. "Return the (beg . end) range of the body of the property drawer.
  14193. BEG is the beginning of the current subtree, or of the part
  14194. before the first headline. If it is not given, it will be found.
  14195. If the drawer does not exist, create it if FORCE is non-nil, or
  14196. return nil."
  14197. (org-with-wide-buffer
  14198. (when beg (goto-char beg))
  14199. (unless (org-before-first-heading-p)
  14200. (let ((beg (cond (beg)
  14201. ((or (not (featurep 'org-inlinetask))
  14202. (org-inlinetask-in-task-p))
  14203. (org-back-to-heading t))
  14204. (t (org-with-limited-levels (org-back-to-heading t))))))
  14205. (forward-line)
  14206. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14207. (cond ((looking-at org-property-drawer-re)
  14208. (forward-line)
  14209. (cons (point) (progn (goto-char (match-end 0))
  14210. (line-beginning-position))))
  14211. (force
  14212. (goto-char beg)
  14213. (org-insert-property-drawer)
  14214. (let ((pos (save-excursion (search-forward ":END:")
  14215. (line-beginning-position))))
  14216. (cons pos pos))))))))
  14217. (defun org-at-property-p ()
  14218. "Non-nil when point is inside a property drawer.
  14219. See `org-property-re' for match data, if applicable."
  14220. (save-excursion
  14221. (beginning-of-line)
  14222. (and (looking-at org-property-re)
  14223. (let ((property-drawer (save-match-data (org-get-property-block))))
  14224. (and property-drawer
  14225. (>= (point) (car property-drawer))
  14226. (< (point) (cdr property-drawer)))))))
  14227. (defun org-property-action ()
  14228. "Do an action on properties."
  14229. (interactive)
  14230. (unless (org-at-property-p) (user-error "Not at a property"))
  14231. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14232. (let ((c (read-char-exclusive)))
  14233. (case c
  14234. (?s (call-interactively #'org-set-property))
  14235. (?d (call-interactively #'org-delete-property))
  14236. (?D (call-interactively #'org-delete-property-globally))
  14237. (?c (call-interactively #'org-compute-property-at-point))
  14238. (otherwise (user-error "No such property action %c" c)))))
  14239. (defun org-inc-effort ()
  14240. "Increment the value of the effort property in the current entry."
  14241. (interactive)
  14242. (org-set-effort nil t))
  14243. (defvar org-clock-effort) ; Defined in org-clock.el.
  14244. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14245. (defun org-set-effort (&optional value increment)
  14246. "Set the effort property of the current entry.
  14247. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14248. 10th allowed value.
  14249. When INCREMENT is non-nil, set the property to the next allowed value."
  14250. (interactive "P")
  14251. (if (equal value 0) (setq value 10))
  14252. (let* ((completion-ignore-case t)
  14253. (prop org-effort-property)
  14254. (cur (org-entry-get nil prop))
  14255. (allowed (org-property-get-allowed-values nil prop 'table))
  14256. (existing (mapcar 'list (org-property-values prop)))
  14257. (heading (nth 4 (org-heading-components)))
  14258. rpl
  14259. (val (cond
  14260. ((stringp value) value)
  14261. ((and allowed (integerp value))
  14262. (or (car (nth (1- value) allowed))
  14263. (car (org-last allowed))))
  14264. ((and allowed increment)
  14265. (or (caadr (member (list cur) allowed))
  14266. (user-error "Allowed effort values are not set")))
  14267. (allowed
  14268. (message "Select 1-9,0, [RET%s]: %s"
  14269. (if cur (concat "=" cur) "")
  14270. (mapconcat 'car allowed " "))
  14271. (setq rpl (read-char-exclusive))
  14272. (if (equal rpl ?\r)
  14273. cur
  14274. (setq rpl (- rpl ?0))
  14275. (if (equal rpl 0) (setq rpl 10))
  14276. (if (and (> rpl 0) (<= rpl (length allowed)))
  14277. (car (nth (1- rpl) allowed))
  14278. (org-completing-read "Effort: " allowed nil))))
  14279. (t
  14280. (let (org-completion-use-ido org-completion-use-iswitchb)
  14281. (org-completing-read
  14282. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14283. (concat "[" cur "]") "")
  14284. ": ")
  14285. existing nil nil "" nil cur))))))
  14286. (unless (equal (org-entry-get nil prop) val)
  14287. (org-entry-put nil prop val))
  14288. (org-refresh-property
  14289. '((effort . identity)
  14290. (effort-minutes . org-duration-string-to-minutes))
  14291. val)
  14292. (when (string= heading org-clock-current-task)
  14293. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14294. (org-clock-update-mode-line))
  14295. (message "%s is now %s" prop val)))
  14296. (defun org-entry-properties (&optional pom which)
  14297. "Get all properties of the current entry.
  14298. When POM is a buffer position, get all properties from the entry
  14299. there instead.
  14300. This includes the TODO keyword, the tags, time strings for
  14301. deadline, scheduled, and clocking, and any additional properties
  14302. defined in the entry.
  14303. If WHICH is nil or `all', get all properties. If WHICH is
  14304. `special' or `standard', only get that subclass. If WHICH is
  14305. a string, only get that property.
  14306. Return value is an alist. Keys are properties, as upcased
  14307. strings."
  14308. (org-with-point-at pom
  14309. (when (and (derived-mode-p 'org-mode)
  14310. (ignore-errors (org-back-to-heading t)))
  14311. (catch 'exit
  14312. (let* ((beg (point))
  14313. (specific (and (stringp which) (upcase which)))
  14314. (which (cond ((not specific) which)
  14315. ((member specific org-special-properties) 'special)
  14316. (t 'standard)))
  14317. props)
  14318. ;; Get the special properties, like TODO and TAGS.
  14319. (when (memq which '(nil all special))
  14320. (when (or (not specific) (string= specific "CLOCKSUM"))
  14321. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14322. (when clocksum
  14323. (push (cons "CLOCKSUM"
  14324. (org-columns-number-to-string
  14325. (/ (float clocksum) 60.) 'add_times))
  14326. props)))
  14327. (when specific (throw 'exit props)))
  14328. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14329. (let ((clocksumt (get-text-property (point)
  14330. :org-clock-minutes-today)))
  14331. (when clocksumt
  14332. (push (cons "CLOCKSUM_T"
  14333. (org-columns-number-to-string
  14334. (/ (float clocksumt) 60.) 'add_times))
  14335. props)))
  14336. (when specific (throw 'exit props)))
  14337. (when (or (not specific) (string= specific "ITEM"))
  14338. (when (looking-at org-complex-heading-regexp)
  14339. (push (cons "ITEM"
  14340. (concat
  14341. (org-match-string-no-properties 1)
  14342. (let ((title (org-match-string-no-properties 4)))
  14343. (when (org-string-nw-p title)
  14344. (concat " " (org-remove-tabs title))))))
  14345. props))
  14346. (when specific (throw 'exit props)))
  14347. (when (or (not specific) (string= specific "TODO"))
  14348. (let ((case-fold-search nil))
  14349. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14350. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14351. (when specific (throw 'exit props)))
  14352. (when (or (not specific) (string= specific "PRIORITY"))
  14353. (push (cons "PRIORITY"
  14354. (if (looking-at org-priority-regexp)
  14355. (org-match-string-no-properties 2)
  14356. (char-to-string org-default-priority)))
  14357. props)
  14358. (when specific (throw 'exit props)))
  14359. (when (or (not specific) (string= specific "FILE"))
  14360. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14361. props)
  14362. (when specific (throw 'exit props)))
  14363. (when (or (not specific) (string= specific "TAGS"))
  14364. (let ((value (org-string-nw-p (org-get-tags-string))))
  14365. (when value (push (cons "TAGS" value) props)))
  14366. (when specific (throw 'exit props)))
  14367. (when (or (not specific) (string= specific "ALLTAGS"))
  14368. (let ((value (org-get-tags-at)))
  14369. (when value
  14370. (push (cons "ALLTAGS"
  14371. (format ":%s:" (mapconcat #'identity value ":")))
  14372. props)))
  14373. (when specific (throw 'exit props)))
  14374. (when (or (not specific) (string= specific "BLOCKED"))
  14375. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14376. (when specific (throw 'exit props)))
  14377. (when (or (not specific)
  14378. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14379. (forward-line)
  14380. (when (org-looking-at-p org-planning-line-re)
  14381. (end-of-line)
  14382. (let ((bol (line-beginning-position))
  14383. ;; Backward compatibility: time keywords used to
  14384. ;; be configurable (before 8.3). Make sure we
  14385. ;; get the correct keyword.
  14386. (key-assoc `(("CLOSED" . ,org-closed-string)
  14387. ("DEADLINE" . ,org-deadline-string)
  14388. ("SCHEDULED" . ,org-scheduled-string))))
  14389. (dolist (pair (if specific (list (assoc specific key-assoc))
  14390. key-assoc))
  14391. (save-excursion
  14392. (when (search-backward (cdr pair) bol t)
  14393. (goto-char (match-end 0))
  14394. (skip-chars-forward " \t")
  14395. (and (looking-at org-ts-regexp-both)
  14396. (push (cons (car pair)
  14397. (org-match-string-no-properties 0))
  14398. props)))))))
  14399. (when specific (throw 'exit props)))
  14400. (when (or (not specific)
  14401. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14402. (let ((find-ts
  14403. (lambda (end ts)
  14404. (let ((regexp (cond
  14405. ((string= specific "TIMESTAMP")
  14406. org-ts-regexp)
  14407. ((string= specific "TIMESTAMP_IA")
  14408. org-ts-regexp-inactive)
  14409. ((assoc "TIMESTAMP_IA" ts)
  14410. org-ts-regexp)
  14411. ((assoc "TIMESTAMP" ts)
  14412. org-ts-regexp-inactive)
  14413. (t org-ts-regexp-both))))
  14414. (catch 'next
  14415. (while (re-search-forward regexp end t)
  14416. (backward-char)
  14417. (let ((object (org-element-context)))
  14418. ;; Accept to match timestamps in node
  14419. ;; properties, too.
  14420. (when (memq (org-element-type object)
  14421. '(node-property timestamp))
  14422. (let ((type
  14423. (org-element-property :type object)))
  14424. (cond
  14425. ((and (memq type '(active active-range))
  14426. (not (equal specific "TIMESTAMP_IA")))
  14427. (unless (assoc "TIMESTAMP" ts)
  14428. (push (cons "TIMESTAMP"
  14429. (org-element-property
  14430. :raw-value object))
  14431. ts)
  14432. (when specific (throw 'exit ts))))
  14433. ((and (memq type '(inactive inactive-range))
  14434. (not (string= specific "TIMESTAMP")))
  14435. (unless (assoc "TIMESTAMP_IA" ts)
  14436. (push (cons "TIMESTAMP_IA"
  14437. (org-element-property
  14438. :raw-value object))
  14439. ts)
  14440. (when specific (throw 'exit ts))))))
  14441. ;; Both timestamp types are found,
  14442. ;; move to next part.
  14443. (when (= (length ts) 2) (throw 'next ts)))))
  14444. ts)))))
  14445. (goto-char beg)
  14446. ;; First look for timestamps within headline.
  14447. (let ((ts (funcall find-ts (line-end-position) nil)))
  14448. (if (= (length ts) 2) (setq props (nconc ts props))
  14449. (forward-line)
  14450. ;; Then find timestamps in the section, skipping
  14451. ;; planning line.
  14452. (when (org-looking-at-p org-planning-line-re)
  14453. (forward-line))
  14454. (let ((end (save-excursion (outline-next-heading))))
  14455. (setq props (nconc (funcall find-ts end ts) props))))))))
  14456. ;; Get the standard properties, like :PROP:.
  14457. (when (memq which '(nil all standard))
  14458. ;; If we are looking after a specific property, delegate
  14459. ;; to `org-entry-get', which is faster. However, make an
  14460. ;; exception for "CATEGORY", since it can be also set
  14461. ;; through keywords (i.e. #+CATEGORY).
  14462. (if (and specific (not (equal specific "CATEGORY")))
  14463. (let ((value (org-entry-get beg specific nil t)))
  14464. (throw 'exit (and value (list (cons specific value)))))
  14465. (let ((range (org-get-property-block beg)))
  14466. (when range
  14467. (let ((end (cdr range)) seen-base)
  14468. (goto-char (car range))
  14469. ;; Unlike to `org--update-property-plist', we
  14470. ;; handle the case where base values is found
  14471. ;; after its extension. We also forbid standard
  14472. ;; properties to be named as special properties.
  14473. (while (re-search-forward org-property-re end t)
  14474. (let* ((key (upcase (org-match-string-no-properties 2)))
  14475. (extendp (org-string-match-p "\\+\\'" key))
  14476. (key-base (if extendp (substring key 0 -1) key))
  14477. (value (org-match-string-no-properties 3)))
  14478. (cond
  14479. ((member-ignore-case key-base org-special-properties))
  14480. (extendp
  14481. (setq props
  14482. (org--update-property-plist key value props)))
  14483. ((member key seen-base))
  14484. (t (push key seen-base)
  14485. (let ((p (assoc-string key props t)))
  14486. (if p (setcdr p (concat value " " (cdr p)))
  14487. (push (cons key value) props))))))))))))
  14488. (unless (assoc "CATEGORY" props)
  14489. (push (cons "CATEGORY" (org-get-category beg)) props)
  14490. (when (string= specific "CATEGORY") (throw 'exit props)))
  14491. ;; Return value.
  14492. props)))))
  14493. (defun org-property--local-values (property literal-nil)
  14494. "Return value for PROPERTY in current entry.
  14495. Value is a list whose care is the base value for PROPERTY and cdr
  14496. a list of accumulated values. Return nil if neither is found in
  14497. the entry. Also return nil when PROPERTY is set to \"nil\",
  14498. unless LITERAL-NIL is non-nil."
  14499. (let ((range (org-get-property-block)))
  14500. (when range
  14501. (goto-char (car range))
  14502. (let* ((case-fold-search t)
  14503. (end (cdr range))
  14504. (value
  14505. ;; Base value.
  14506. (save-excursion
  14507. (let ((v (and (re-search-forward
  14508. (org-re-property property nil t) end t)
  14509. (org-match-string-no-properties 3))))
  14510. (list (if literal-nil v (org-not-nil v)))))))
  14511. ;; Find additional values.
  14512. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14513. (while (re-search-forward property+ end t)
  14514. (push (org-match-string-no-properties 3) value)))
  14515. ;; Return final values.
  14516. (and (not (equal value '(nil))) (nreverse value))))))
  14517. (defun org-entry-get (pom property &optional inherit literal-nil)
  14518. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14519. If INHERIT is non-nil and the entry does not have the property,
  14520. then also check higher levels of the hierarchy. If INHERIT is
  14521. the symbol `selective', use inheritance only if the setting in
  14522. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14523. If the property is present but empty, the return value is the
  14524. empty string. If the property is not present at all, nil is
  14525. returned. In any other case, return the value as a string.
  14526. Search is case-insensitive.
  14527. If LITERAL-NIL is set, return the string value \"nil\" as
  14528. a string, do not interpret it as the list atom nil. This is used
  14529. for inheritance when a \"nil\" value can supersede a non-nil
  14530. value higher up the hierarchy."
  14531. (org-with-point-at pom
  14532. (cond
  14533. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14534. ;; We need a special property. Use `org-entry-properties' to
  14535. ;; retrieve it, but specify the wanted property.
  14536. (cdr (assoc-string property (org-entry-properties nil property))))
  14537. ((and inherit
  14538. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14539. (org-entry-get-with-inheritance property literal-nil))
  14540. (t
  14541. (let* ((local (org-property--local-values property literal-nil))
  14542. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14543. (if literal-nil value (org-not-nil value)))))))
  14544. (defun org-property-or-variable-value (var &optional inherit)
  14545. "Check if there is a property fixing the value of VAR.
  14546. If yes, return this value. If not, return the current value of the variable."
  14547. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14548. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14549. (read prop)
  14550. (symbol-value var))))
  14551. (defun org-entry-delete (pom property)
  14552. "Delete PROPERTY from entry at point-or-marker POM.
  14553. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14554. non-nil when a property was removed."
  14555. (unless (member property org-special-properties)
  14556. (org-with-point-at pom
  14557. (let ((range (org-get-property-block)))
  14558. (when range
  14559. (let* ((begin (car range))
  14560. (origin (cdr range))
  14561. (end (copy-marker origin))
  14562. (re (org-re-property
  14563. (concat (regexp-quote property) "\\+?") t t)))
  14564. (goto-char begin)
  14565. (while (re-search-forward re end t)
  14566. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14567. ;; If drawer is empty, remove it altogether.
  14568. (when (= begin end)
  14569. (delete-region (line-beginning-position 0)
  14570. (line-beginning-position 2)))
  14571. ;; Return non-nil if some property was removed.
  14572. (prog1 (/= end origin) (set-marker end nil))))))))
  14573. ;; Multi-values properties are properties that contain multiple values
  14574. ;; These values are assumed to be single words, separated by whitespace.
  14575. (defun org-entry-add-to-multivalued-property (pom property value)
  14576. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14577. (let* ((old (org-entry-get pom property))
  14578. (values (and old (org-split-string old "[ \t]"))))
  14579. (setq value (org-entry-protect-space value))
  14580. (unless (member value values)
  14581. (setq values (append values (list value)))
  14582. (org-entry-put pom property
  14583. (mapconcat 'identity values " ")))))
  14584. (defun org-entry-remove-from-multivalued-property (pom property value)
  14585. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14586. (let* ((old (org-entry-get pom property))
  14587. (values (and old (org-split-string old "[ \t]"))))
  14588. (setq value (org-entry-protect-space value))
  14589. (when (member value values)
  14590. (setq values (delete value values))
  14591. (org-entry-put pom property
  14592. (mapconcat 'identity values " ")))))
  14593. (defun org-entry-member-in-multivalued-property (pom property value)
  14594. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14595. (let* ((old (org-entry-get pom property))
  14596. (values (and old (org-split-string old "[ \t]"))))
  14597. (setq value (org-entry-protect-space value))
  14598. (member value values)))
  14599. (defun org-entry-get-multivalued-property (pom property)
  14600. "Return a list of values in a multivalued property."
  14601. (let* ((value (org-entry-get pom property))
  14602. (values (and value (org-split-string value "[ \t]"))))
  14603. (mapcar 'org-entry-restore-space values)))
  14604. (defun org-entry-put-multivalued-property (pom property &rest values)
  14605. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14606. VALUES should be a list of strings. Spaces will be protected."
  14607. (org-entry-put pom property
  14608. (mapconcat 'org-entry-protect-space values " "))
  14609. (let* ((value (org-entry-get pom property))
  14610. (values (and value (org-split-string value "[ \t]"))))
  14611. (mapcar 'org-entry-restore-space values)))
  14612. (defun org-entry-protect-space (s)
  14613. "Protect spaces and newline in string S."
  14614. (while (string-match " " s)
  14615. (setq s (replace-match "%20" t t s)))
  14616. (while (string-match "\n" s)
  14617. (setq s (replace-match "%0A" t t s)))
  14618. s)
  14619. (defun org-entry-restore-space (s)
  14620. "Restore spaces and newline in string S."
  14621. (while (string-match "%20" s)
  14622. (setq s (replace-match " " t t s)))
  14623. (while (string-match "%0A" s)
  14624. (setq s (replace-match "\n" t t s)))
  14625. s)
  14626. (defvar org-entry-property-inherited-from (make-marker)
  14627. "Marker pointing to the entry from where a property was inherited.
  14628. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14629. location of the entry where the inheritance search matched. If there was
  14630. no match, the marker will point nowhere.
  14631. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14632. is set.")
  14633. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14634. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14635. The search will stop at the first ancestor which has the property defined.
  14636. If the value found is \"nil\", return nil to show that the property
  14637. should be considered as undefined (this is the meaning of nil here).
  14638. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14639. (move-marker org-entry-property-inherited-from nil)
  14640. (org-with-wide-buffer
  14641. (let (value)
  14642. (catch 'exit
  14643. (while t
  14644. (let ((v (org-property--local-values property literal-nil)))
  14645. (when v
  14646. (setq value
  14647. (concat (mapconcat #'identity (delq nil v) " ")
  14648. (and value " ")
  14649. value)))
  14650. (cond
  14651. ((car v)
  14652. (org-back-to-heading t)
  14653. (move-marker org-entry-property-inherited-from (point))
  14654. (throw 'exit nil))
  14655. ((org-up-heading-safe))
  14656. (t
  14657. (let ((global
  14658. (cdr (or (assoc-string property org-file-properties t)
  14659. (assoc-string property org-global-properties t)
  14660. (assoc-string property org-global-properties-fixed t)))))
  14661. (cond ((not global))
  14662. (value (setq value (concat global " " value)))
  14663. (t (setq value global))))
  14664. (throw 'exit nil))))))
  14665. (if literal-nil value (org-not-nil value)))))
  14666. (defvar org-property-changed-functions nil
  14667. "Hook called when the value of a property has changed.
  14668. Each hook function should accept two arguments, the name of the property
  14669. and the new value.")
  14670. (defun org-entry-put (pom property value)
  14671. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14672. If the value is nil, it is converted to the empty string. If
  14673. it is not a string, an error is raised.
  14674. PROPERTY can be any regular property (see
  14675. `org-special-properties'). It can also be \"TODO\",
  14676. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14677. For the last two properties, VALUE may have any of the special
  14678. values \"earlier\" and \"later\". The function then increases or
  14679. decreases scheduled or deadline date by one day."
  14680. (cond ((null value) (setq value ""))
  14681. ((not (stringp value)) (error "Properties values should be strings")))
  14682. (org-with-point-at pom
  14683. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14684. (org-back-to-heading t)
  14685. (org-with-limited-levels (org-back-to-heading t)))
  14686. (let ((beg (point)))
  14687. (cond
  14688. ((equal property "TODO")
  14689. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14690. ((not (member value org-todo-keywords-1))
  14691. (user-error "\"%s\" is not a valid TODO state" value)))
  14692. (org-todo value)
  14693. (org-set-tags nil 'align))
  14694. ((equal property "PRIORITY")
  14695. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14696. (org-set-tags nil 'align))
  14697. ((equal property "SCHEDULED")
  14698. (forward-line)
  14699. (if (and (org-looking-at-p org-planning-line-re)
  14700. (re-search-forward
  14701. org-scheduled-time-regexp (line-end-position) t))
  14702. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14703. ((string= value "later") (org-timestamp-change 1 'day))
  14704. ((string= value "") (org-schedule '(4)))
  14705. (t (org-schedule nil value)))
  14706. (if (member value '("earlier" "later" ""))
  14707. (call-interactively #'org-schedule)
  14708. (org-schedule nil value))))
  14709. ((equal property "DEADLINE")
  14710. (forward-line)
  14711. (if (and (org-looking-at-p org-planning-line-re)
  14712. (re-search-forward
  14713. org-deadline-time-regexp (line-end-position) t))
  14714. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14715. ((string= value "later") (org-timestamp-change 1 'day))
  14716. ((string= value "") (org-deadline '(4)))
  14717. (t (org-deadline nil value)))
  14718. (if (member value '("earlier" "later" ""))
  14719. (call-interactively #'org-deadline)
  14720. (org-deadline nil value))))
  14721. ((member property org-special-properties)
  14722. (error "The %s property cannot be set with `org-entry-put'" property))
  14723. (t
  14724. (let* ((range (org-get-property-block beg 'force))
  14725. (end (cdr range))
  14726. (case-fold-search t))
  14727. (goto-char (car range))
  14728. (if (re-search-forward (org-re-property property nil t) end t)
  14729. (progn (delete-region (match-beginning 0) (match-end 0))
  14730. (goto-char (match-beginning 0)))
  14731. (goto-char end)
  14732. (insert "\n")
  14733. (backward-char))
  14734. (insert ":" property ":")
  14735. (when value (insert " " value))
  14736. (org-indent-line)))))
  14737. (run-hook-with-args 'org-property-changed-functions property value)))
  14738. (defun org-buffer-property-keys (&optional specials defaults columns)
  14739. "Get all property keys in the current buffer.
  14740. When SPECIALS is non-nil, also list the special properties that
  14741. reflect things like tags and TODO state.
  14742. When DEFAULTS is non-nil, also include properties that has
  14743. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14744. DESCRIPTION, LOCATION, and LOGGING and others.
  14745. When COLUMNS in non-nil, also include property names given in
  14746. COLUMN formats in the current buffer."
  14747. (let ((case-fold-search t)
  14748. (props (append
  14749. (and specials org-special-properties)
  14750. (and defaults (cons org-effort-property org-default-properties))
  14751. nil)))
  14752. (org-with-wide-buffer
  14753. (goto-char (point-min))
  14754. (while (re-search-forward org-property-start-re nil t)
  14755. (let ((range (org-get-property-block)))
  14756. (catch 'skip
  14757. (unless range
  14758. (when (and (not (org-before-first-heading-p))
  14759. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14760. (line-beginning-position))))
  14761. (org-get-property-block nil t))
  14762. (throw 'skip nil))
  14763. (goto-char (car range))
  14764. (let ((begin (car range))
  14765. (end (cdr range)))
  14766. ;; Make sure that found property block is not located
  14767. ;; before current point, as it would generate an infloop.
  14768. ;; It can happen, for example, in the following
  14769. ;; situation:
  14770. ;;
  14771. ;; * Headline
  14772. ;; :PROPERTIES:
  14773. ;; ...
  14774. ;; :END:
  14775. ;; *************** Inlinetask
  14776. ;; #+BEGIN_EXAMPLE
  14777. ;; :PROPERTIES:
  14778. ;; #+END_EXAMPLE
  14779. ;;
  14780. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14781. (while (< (point) end)
  14782. (let ((p (progn (looking-at org-property-re)
  14783. (org-match-string-no-properties 2))))
  14784. ;; Only add true property name, not extension symbol.
  14785. (add-to-list 'props
  14786. (if (not (org-string-match-p "\\+\\'" p)) p
  14787. (substring p 0 -1))))
  14788. (forward-line))))
  14789. (outline-next-heading)))
  14790. (when columns
  14791. (goto-char (point-min))
  14792. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14793. (let ((element (org-element-at-point)))
  14794. (when (memq (org-element-type element) '(keyword node-property))
  14795. (let ((value (org-element-property :value element))
  14796. (start 0))
  14797. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14798. (setq start (match-end 0))
  14799. (let ((p (org-match-string-no-properties 1 value)))
  14800. (unless (member-ignore-case p org-special-properties)
  14801. (add-to-list 'props p))))))))))
  14802. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14803. (defun org-property-values (key)
  14804. "List all non-nil values of property KEY in current buffer."
  14805. (org-with-wide-buffer
  14806. (goto-char (point-min))
  14807. (let ((case-fold-search t)
  14808. (re (org-re-property key))
  14809. values)
  14810. (while (re-search-forward re nil t)
  14811. (add-to-list 'values (org-entry-get (point) key)))
  14812. values)))
  14813. (defun org-insert-property-drawer ()
  14814. "Insert a property drawer into the current entry."
  14815. (org-with-wide-buffer
  14816. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14817. (org-back-to-heading t)
  14818. (org-with-limited-levels (org-back-to-heading t)))
  14819. (forward-line)
  14820. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14821. (unless (org-looking-at-p org-property-drawer-re)
  14822. (let ((inhibit-read-only t))
  14823. (unless (bolp) (insert "\n"))
  14824. (let ((begin (point)))
  14825. (insert ":PROPERTIES:\n:END:\n")
  14826. (org-indent-region begin (point)))))))
  14827. (defun org-insert-drawer (&optional arg drawer)
  14828. "Insert a drawer at point.
  14829. When optional argument ARG is non-nil, insert a property drawer.
  14830. Optional argument DRAWER, when non-nil, is a string representing
  14831. drawer's name. Otherwise, the user is prompted for a name.
  14832. If a region is active, insert the drawer around that region
  14833. instead.
  14834. Point is left between drawer's boundaries."
  14835. (interactive "P")
  14836. (let* ((drawer (if arg "PROPERTIES"
  14837. (or drawer (read-from-minibuffer "Drawer: ")))))
  14838. (cond
  14839. ;; With C-u, fall back on `org-insert-property-drawer'
  14840. (arg (org-insert-property-drawer))
  14841. ;; Check validity of suggested drawer's name.
  14842. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14843. (user-error "Invalid drawer name"))
  14844. ;; With an active region, insert a drawer at point.
  14845. ((not (org-region-active-p))
  14846. (progn
  14847. (unless (bolp) (insert "\n"))
  14848. (insert (format ":%s:\n\n:END:\n" drawer))
  14849. (forward-line -2)))
  14850. ;; Otherwise, insert the drawer at point
  14851. (t
  14852. (let ((rbeg (region-beginning))
  14853. (rend (copy-marker (region-end))))
  14854. (unwind-protect
  14855. (progn
  14856. (goto-char rbeg)
  14857. (beginning-of-line)
  14858. (when (save-excursion
  14859. (re-search-forward org-outline-regexp-bol rend t))
  14860. (user-error "Drawers cannot contain headlines"))
  14861. ;; Position point at the beginning of the first
  14862. ;; non-blank line in region. Insert drawer's opening
  14863. ;; there, then indent it.
  14864. (org-skip-whitespace)
  14865. (beginning-of-line)
  14866. (insert ":" drawer ":\n")
  14867. (forward-line -1)
  14868. (indent-for-tab-command)
  14869. ;; Move point to the beginning of the first blank line
  14870. ;; after the last non-blank line in region. Insert
  14871. ;; drawer's closing, then indent it.
  14872. (goto-char rend)
  14873. (skip-chars-backward " \r\t\n")
  14874. (insert "\n:END:")
  14875. (deactivate-mark t)
  14876. (indent-for-tab-command)
  14877. (unless (eolp) (insert "\n")))
  14878. ;; Clear marker, whatever the outcome of insertion is.
  14879. (set-marker rend nil)))))))
  14880. (defvar org-property-set-functions-alist nil
  14881. "Property set function alist.
  14882. Each entry should have the following format:
  14883. (PROPERTY . READ-FUNCTION)
  14884. The read function will be called with the same argument as
  14885. `org-completing-read'.")
  14886. (defun org-set-property-function (property)
  14887. "Get the function that should be used to set PROPERTY.
  14888. This is computed according to `org-property-set-functions-alist'."
  14889. (or (cdr (assoc property org-property-set-functions-alist))
  14890. 'org-completing-read))
  14891. (defun org-read-property-value (property)
  14892. "Read PROPERTY value from user."
  14893. (let* ((completion-ignore-case t)
  14894. (allowed (org-property-get-allowed-values nil property 'table))
  14895. (cur (org-entry-get nil property))
  14896. (prompt (concat property " value"
  14897. (if (and cur (string-match "\\S-" cur))
  14898. (concat " [" cur "]") "") ": "))
  14899. (set-function (org-set-property-function property))
  14900. (val (if allowed
  14901. (funcall set-function prompt allowed nil
  14902. (not (get-text-property 0 'org-unrestricted
  14903. (caar allowed))))
  14904. (let (org-completion-use-ido org-completion-use-iswitchb)
  14905. (funcall set-function prompt
  14906. (mapcar 'list (org-property-values property))
  14907. nil nil "" nil cur)))))
  14908. (org-trim val)))
  14909. (defvar org-last-set-property nil)
  14910. (defvar org-last-set-property-value nil)
  14911. (defun org-read-property-name ()
  14912. "Read a property name."
  14913. (let ((completion-ignore-case t)
  14914. (default-prop (or (and (org-at-property-p)
  14915. (org-match-string-no-properties 2))
  14916. org-last-set-property)))
  14917. (org-completing-read
  14918. (concat "Property"
  14919. (if default-prop (concat " [" default-prop "]") "")
  14920. ": ")
  14921. (mapcar #'list (org-buffer-property-keys nil t t))
  14922. nil nil nil nil default-prop)))
  14923. (defun org-set-property-and-value (use-last)
  14924. "Allow to set [PROPERTY]: [value] direction from prompt.
  14925. When use-default, don't even ask, just use the last
  14926. \"[PROPERTY]: [value]\" string from the history."
  14927. (interactive "P")
  14928. (let* ((completion-ignore-case t)
  14929. (pv (or (and use-last org-last-set-property-value)
  14930. (org-completing-read
  14931. "Enter a \"[Property]: [value]\" pair: "
  14932. nil nil nil nil nil
  14933. org-last-set-property-value)))
  14934. prop val)
  14935. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14936. (setq prop (match-string 1 pv)
  14937. val (match-string 2 pv))
  14938. (org-set-property prop val))))
  14939. (defun org-set-property (property value)
  14940. "In the current entry, set PROPERTY to VALUE.
  14941. When called interactively, this will prompt for a property name, offering
  14942. completion on existing and default properties. And then it will prompt
  14943. for a value, offering completion either on allowed values (via an inherited
  14944. xxx_ALL property) or on existing values in other instances of this property
  14945. in the current file."
  14946. (interactive (list nil nil))
  14947. (let* ((property (or property (org-read-property-name)))
  14948. (value (or value (org-read-property-value property)))
  14949. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14950. (setq org-last-set-property property)
  14951. (setq org-last-set-property-value (concat property ": " value))
  14952. ;; Possibly postprocess the inserted value:
  14953. (when fn (setq value (funcall fn value)))
  14954. (unless (equal (org-entry-get nil property) value)
  14955. (org-entry-put nil property value))))
  14956. (defun org-find-property (property &optional value)
  14957. "Find first entry in buffer that sets PROPERTY.
  14958. When optional argument VALUE is non-nil, only consider an entry
  14959. if it contains PROPERTY set to this value. If PROPERTY should be
  14960. explicitly set to nil, use string \"nil\" for VALUE.
  14961. Return position where the entry begins, or nil if there is no
  14962. such entry. If narrowing is in effect, only search the visible
  14963. part of the buffer."
  14964. (save-excursion
  14965. (goto-char (point-min))
  14966. (let ((case-fold-search t)
  14967. (re (org-re-property property nil (not value) value)))
  14968. (catch 'exit
  14969. (while (re-search-forward re nil t)
  14970. (when (if value (org-at-property-p)
  14971. (org-entry-get (point) property nil t))
  14972. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14973. (defun org-delete-property (property)
  14974. "In the current entry, delete PROPERTY."
  14975. (interactive
  14976. (let* ((completion-ignore-case t)
  14977. (cat (org-entry-get (point) "CATEGORY"))
  14978. (props0 (org-entry-properties nil 'standard))
  14979. (props (if cat props0
  14980. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14981. (prop (if (< 1 (length props))
  14982. (org-icompleting-read "Property: " props nil t)
  14983. (caar props))))
  14984. (list prop)))
  14985. (if (not property)
  14986. (message "No property to delete in this entry")
  14987. (org-entry-delete nil property)
  14988. (message "Property \"%s\" deleted" property)))
  14989. (defun org-delete-property-globally (property)
  14990. "Remove PROPERTY globally, from all entries.
  14991. This function ignores narrowing, if any."
  14992. (interactive
  14993. (let* ((completion-ignore-case t)
  14994. (prop (org-icompleting-read
  14995. "Globally remove property: "
  14996. (mapcar #'list (org-buffer-property-keys)))))
  14997. (list prop)))
  14998. (org-with-wide-buffer
  14999. (goto-char (point-min))
  15000. (let ((count 0)
  15001. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15002. (while (re-search-forward re nil t)
  15003. (when (org-entry-delete (point) property) (incf count)))
  15004. (message "Property \"%s\" removed from %d entries" property count))))
  15005. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15006. (defun org-compute-property-at-point ()
  15007. "Compute the property at point.
  15008. This looks for an enclosing column format, extracts the operator and
  15009. then applies it to the property in the column format's scope."
  15010. (interactive)
  15011. (unless (org-at-property-p)
  15012. (user-error "Not at a property"))
  15013. (let ((prop (org-match-string-no-properties 2)))
  15014. (org-columns-get-format-and-top-level)
  15015. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15016. (user-error "No operator defined for property %s" prop))
  15017. (org-columns-compute prop)))
  15018. (defvar org-property-allowed-value-functions nil
  15019. "Hook for functions supplying allowed values for a specific property.
  15020. The functions must take a single argument, the name of the property, and
  15021. return a flat list of allowed values. If \":ETC\" is one of
  15022. the values, this means that these values are intended as defaults for
  15023. completion, but that other values should be allowed too.
  15024. The functions must return nil if they are not responsible for this
  15025. property.")
  15026. (defun org-property-get-allowed-values (pom property &optional table)
  15027. "Get allowed values for the property PROPERTY.
  15028. When TABLE is non-nil, return an alist that can directly be used for
  15029. completion."
  15030. (let (vals)
  15031. (cond
  15032. ((equal property "TODO")
  15033. (setq vals (org-with-point-at pom
  15034. (append org-todo-keywords-1 '("")))))
  15035. ((equal property "PRIORITY")
  15036. (let ((n org-lowest-priority))
  15037. (while (>= n org-highest-priority)
  15038. (push (char-to-string n) vals)
  15039. (setq n (1- n)))))
  15040. ((equal property "CATEGORY"))
  15041. ((member property org-special-properties))
  15042. ((setq vals (run-hook-with-args-until-success
  15043. 'org-property-allowed-value-functions property)))
  15044. (t
  15045. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15046. (when (and vals (string-match "\\S-" vals))
  15047. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15048. (setq vals (mapcar (lambda (x)
  15049. (cond ((stringp x) x)
  15050. ((numberp x) (number-to-string x))
  15051. ((symbolp x) (symbol-name x))
  15052. (t "???")))
  15053. vals)))))
  15054. (when (member ":ETC" vals)
  15055. (setq vals (remove ":ETC" vals))
  15056. (org-add-props (car vals) '(org-unrestricted t)))
  15057. (if table (mapcar 'list vals) vals)))
  15058. (defun org-property-previous-allowed-value (&optional previous)
  15059. "Switch to the next allowed value for this property."
  15060. (interactive)
  15061. (org-property-next-allowed-value t))
  15062. (defun org-property-next-allowed-value (&optional previous)
  15063. "Switch to the next allowed value for this property."
  15064. (interactive)
  15065. (unless (org-at-property-p)
  15066. (user-error "Not at a property"))
  15067. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15068. (key (match-string 2))
  15069. (value (match-string 3))
  15070. (allowed (or (org-property-get-allowed-values (point) key)
  15071. (and (member value '("[ ]" "[-]" "[X]"))
  15072. '("[ ]" "[X]"))))
  15073. (heading (save-match-data (nth 4 (org-heading-components))))
  15074. nval)
  15075. (unless allowed
  15076. (user-error "Allowed values for this property have not been defined"))
  15077. (if previous (setq allowed (reverse allowed)))
  15078. (if (member value allowed)
  15079. (setq nval (car (cdr (member value allowed)))))
  15080. (setq nval (or nval (car allowed)))
  15081. (if (equal nval value)
  15082. (user-error "Only one allowed value for this property"))
  15083. (org-at-property-p)
  15084. (replace-match (concat " :" key ": " nval) t t)
  15085. (org-indent-line)
  15086. (beginning-of-line 1)
  15087. (skip-chars-forward " \t")
  15088. (when (equal prop org-effort-property)
  15089. (org-refresh-property
  15090. '((effort . identity)
  15091. (effort-minutes . org-duration-string-to-minutes))
  15092. nval)
  15093. (when (string= org-clock-current-task heading)
  15094. (setq org-clock-effort nval)
  15095. (org-clock-update-mode-line)))
  15096. (run-hook-with-args 'org-property-changed-functions key nval)))
  15097. (defun org-find-olp (path &optional this-buffer)
  15098. "Return a marker pointing to the entry at outline path OLP.
  15099. If anything goes wrong, throw an error.
  15100. You can wrap this call to catch the error like this:
  15101. (condition-case msg
  15102. (org-mobile-locate-entry (match-string 4))
  15103. (error (nth 1 msg)))
  15104. The return value will then be either a string with the error message,
  15105. or a marker if everything is OK.
  15106. If THIS-BUFFER is set, the outline path does not contain a file,
  15107. only headings."
  15108. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15109. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15110. (level 1)
  15111. (lmin 1)
  15112. (lmax 1)
  15113. limit re end found pos heading cnt flevel)
  15114. (unless buffer (error "File not found :%s" file))
  15115. (with-current-buffer buffer
  15116. (save-excursion
  15117. (save-restriction
  15118. (widen)
  15119. (setq limit (point-max))
  15120. (goto-char (point-min))
  15121. (dolist (heading path)
  15122. (setq re (format org-complex-heading-regexp-format
  15123. (regexp-quote heading)))
  15124. (setq cnt 0 pos (point))
  15125. (while (re-search-forward re end t)
  15126. (setq level (- (match-end 1) (match-beginning 1)))
  15127. (if (and (>= level lmin) (<= level lmax))
  15128. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15129. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15130. lmax heading))
  15131. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15132. lmax heading))
  15133. (goto-char found)
  15134. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15135. (setq end (save-excursion (org-end-of-subtree t t))))
  15136. (when (org-at-heading-p)
  15137. (point-marker)))))))
  15138. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15139. "Find node HEADING in BUFFER.
  15140. Return a marker to the heading if it was found, or nil if not.
  15141. If POS-ONLY is set, return just the position instead of a marker.
  15142. The heading text must match exact, but it may have a TODO keyword,
  15143. a priority cookie and tags in the standard locations."
  15144. (with-current-buffer (or buffer (current-buffer))
  15145. (save-excursion
  15146. (save-restriction
  15147. (widen)
  15148. (goto-char (point-min))
  15149. (let (case-fold-search)
  15150. (if (re-search-forward
  15151. (format org-complex-heading-regexp-format
  15152. (regexp-quote heading)) nil t)
  15153. (if pos-only
  15154. (match-beginning 0)
  15155. (move-marker (make-marker) (match-beginning 0)))))))))
  15156. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15157. "Find Org node headline HEADING in all .org files in directory DIR.
  15158. When the target headline is found, return a marker to this location."
  15159. (let ((files (directory-files (or dir default-directory)
  15160. t "\\`[^.#].*\\.org\\'"))
  15161. visiting m buffer)
  15162. (catch 'found
  15163. (dolist (file files)
  15164. (message "trying %s" file)
  15165. (setq visiting (org-find-base-buffer-visiting file))
  15166. (setq buffer (or visiting (find-file-noselect file)))
  15167. (setq m (org-find-exact-headline-in-buffer
  15168. heading buffer))
  15169. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15170. (and m (throw 'found m))))))
  15171. (defun org-find-entry-with-id (ident)
  15172. "Locate the entry that contains the ID property with exact value IDENT.
  15173. IDENT can be a string, a symbol or a number, this function will search for
  15174. the string representation of it.
  15175. Return the position where this entry starts, or nil if there is no such entry."
  15176. (interactive "sID: ")
  15177. (let ((id (cond
  15178. ((stringp ident) ident)
  15179. ((symbolp ident) (symbol-name ident))
  15180. ((numberp ident) (number-to-string ident))
  15181. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15182. (org-with-wide-buffer (org-find-property "ID" id))))
  15183. ;;;; Timestamps
  15184. (defvar org-last-changed-timestamp nil)
  15185. (defvar org-last-inserted-timestamp nil
  15186. "The last time stamp inserted with `org-insert-time-stamp'.")
  15187. (defvar org-ts-what) ; dynamically scoped parameter
  15188. (defun org-time-stamp (arg &optional inactive)
  15189. "Prompt for a date/time and insert a time stamp.
  15190. If the user specifies a time like HH:MM or if this command is
  15191. called with at least one prefix argument, the time stamp contains
  15192. the date and the time. Otherwise, only the date is included.
  15193. All parts of a date not specified by the user are filled in from
  15194. the timestamp at point, if any, or the current date/time
  15195. otherwise.
  15196. If there is already a timestamp at the cursor, it is replaced.
  15197. With two universal prefix arguments, insert an active timestamp
  15198. with the current time without prompting the user.
  15199. When called from lisp, the timestamp is inactive if INACTIVE is
  15200. non-nil."
  15201. (interactive "P")
  15202. (let* ((ts (cond
  15203. ((org-at-date-range-p t)
  15204. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15205. ((org-at-timestamp-p t) (match-string 0))))
  15206. ;; Default time is either the timestamp at point or today.
  15207. ;; When entering a range, only the range start is considered.
  15208. (default-time (if (not ts) (current-time)
  15209. (apply #'encode-time (org-parse-time-string ts))))
  15210. (default-input (and ts (org-get-compact-tod ts)))
  15211. (repeater (and ts
  15212. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15213. (match-string 0 ts)))
  15214. org-time-was-given
  15215. org-end-time-was-given
  15216. (time
  15217. (and (if (equal arg '(16)) (current-time)
  15218. ;; Preserve `this-command' and `last-command'.
  15219. (let ((this-command this-command)
  15220. (last-command last-command))
  15221. (org-read-date
  15222. arg 'totime nil nil default-time default-input
  15223. inactive))))))
  15224. (cond
  15225. ((and ts
  15226. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15227. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15228. (insert "--")
  15229. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15230. (ts
  15231. ;; Make sure we're on a timestamp. When in the middle of a date
  15232. ;; range, move arbitrarily to range end.
  15233. (unless (org-at-timestamp-p t)
  15234. (skip-chars-forward "-")
  15235. (org-at-timestamp-p t))
  15236. (replace-match "")
  15237. (setq org-last-changed-timestamp
  15238. (org-insert-time-stamp
  15239. time (or org-time-was-given arg)
  15240. inactive nil nil (list org-end-time-was-given)))
  15241. (when repeater
  15242. (backward-char)
  15243. (insert " " repeater)
  15244. (setq org-last-changed-timestamp
  15245. (concat (substring org-last-inserted-timestamp 0 -1)
  15246. " " repeater ">")))
  15247. (message "Timestamp updated"))
  15248. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15249. (t (org-insert-time-stamp
  15250. time (or org-time-was-given arg) inactive nil nil
  15251. (list org-end-time-was-given))))))
  15252. ;; FIXME: can we use this for something else, like computing time differences?
  15253. (defun org-get-compact-tod (s)
  15254. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15255. (let* ((t1 (match-string 1 s))
  15256. (h1 (string-to-number (match-string 2 s)))
  15257. (m1 (string-to-number (match-string 3 s)))
  15258. (t2 (and (match-end 4) (match-string 5 s)))
  15259. (h2 (and t2 (string-to-number (match-string 6 s))))
  15260. (m2 (and t2 (string-to-number (match-string 7 s))))
  15261. dh dm)
  15262. (if (not t2)
  15263. t1
  15264. (setq dh (- h2 h1) dm (- m2 m1))
  15265. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15266. (concat t1 "+" (number-to-string dh)
  15267. (and (/= 0 dm) (format ":%02d" dm)))))))
  15268. (defun org-time-stamp-inactive (&optional arg)
  15269. "Insert an inactive time stamp.
  15270. An inactive time stamp is enclosed in square brackets instead of angle
  15271. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15272. does not link to the calendar and cannot be changed with the S-cursor keys.
  15273. So these are more for recording a certain time/date."
  15274. (interactive "P")
  15275. (org-time-stamp arg 'inactive))
  15276. (defvar org-date-ovl (make-overlay 1 1))
  15277. (overlay-put org-date-ovl 'face 'org-date-selected)
  15278. (org-detach-overlay org-date-ovl)
  15279. (defvar org-ans1) ; dynamically scoped parameter
  15280. (defvar org-ans2) ; dynamically scoped parameter
  15281. (defvar org-plain-time-of-day-regexp) ; defined below
  15282. (defvar org-overriding-default-time nil) ; dynamically scoped
  15283. (defvar org-read-date-overlay nil)
  15284. (defvar org-dcst nil) ; dynamically scoped
  15285. (defvar org-read-date-history nil)
  15286. (defvar org-read-date-final-answer nil)
  15287. (defvar org-read-date-analyze-futurep nil)
  15288. (defvar org-read-date-analyze-forced-year nil)
  15289. (defvar org-read-date-inactive)
  15290. (defvar org-read-date-minibuffer-local-map
  15291. (let* ((map (make-sparse-keymap)))
  15292. (set-keymap-parent map minibuffer-local-map)
  15293. (org-defkey map (kbd ".")
  15294. (lambda () (interactive)
  15295. ;; Are we at the beginning of the prompt?
  15296. (if (looking-back "^[^:]+: ")
  15297. (org-eval-in-calendar '(calendar-goto-today))
  15298. (insert "."))))
  15299. (org-defkey map (kbd "C-.")
  15300. (lambda () (interactive)
  15301. (org-eval-in-calendar '(calendar-goto-today))))
  15302. (org-defkey map [(meta shift left)]
  15303. (lambda () (interactive)
  15304. (org-eval-in-calendar '(calendar-backward-month 1))))
  15305. (org-defkey map [(meta shift right)]
  15306. (lambda () (interactive)
  15307. (org-eval-in-calendar '(calendar-forward-month 1))))
  15308. (org-defkey map [(meta shift up)]
  15309. (lambda () (interactive)
  15310. (org-eval-in-calendar '(calendar-backward-year 1))))
  15311. (org-defkey map [(meta shift down)]
  15312. (lambda () (interactive)
  15313. (org-eval-in-calendar '(calendar-forward-year 1))))
  15314. (org-defkey map [?\e (shift left)]
  15315. (lambda () (interactive)
  15316. (org-eval-in-calendar '(calendar-backward-month 1))))
  15317. (org-defkey map [?\e (shift right)]
  15318. (lambda () (interactive)
  15319. (org-eval-in-calendar '(calendar-forward-month 1))))
  15320. (org-defkey map [?\e (shift up)]
  15321. (lambda () (interactive)
  15322. (org-eval-in-calendar '(calendar-backward-year 1))))
  15323. (org-defkey map [?\e (shift down)]
  15324. (lambda () (interactive)
  15325. (org-eval-in-calendar '(calendar-forward-year 1))))
  15326. (org-defkey map [(shift up)]
  15327. (lambda () (interactive)
  15328. (org-eval-in-calendar '(calendar-backward-week 1))))
  15329. (org-defkey map [(shift down)]
  15330. (lambda () (interactive)
  15331. (org-eval-in-calendar '(calendar-forward-week 1))))
  15332. (org-defkey map [(shift left)]
  15333. (lambda () (interactive)
  15334. (org-eval-in-calendar '(calendar-backward-day 1))))
  15335. (org-defkey map [(shift right)]
  15336. (lambda () (interactive)
  15337. (org-eval-in-calendar '(calendar-forward-day 1))))
  15338. (org-defkey map "!"
  15339. (lambda () (interactive)
  15340. (org-eval-in-calendar '(diary-view-entries))
  15341. (message "")))
  15342. (org-defkey map ">"
  15343. (lambda () (interactive)
  15344. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15345. (org-defkey map "<"
  15346. (lambda () (interactive)
  15347. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15348. (org-defkey map "\C-v"
  15349. (lambda () (interactive)
  15350. (org-eval-in-calendar
  15351. '(calendar-scroll-left-three-months 1))))
  15352. (org-defkey map "\M-v"
  15353. (lambda () (interactive)
  15354. (org-eval-in-calendar
  15355. '(calendar-scroll-right-three-months 1))))
  15356. map)
  15357. "Keymap for minibuffer commands when using `org-read-date'.")
  15358. (defvar org-def)
  15359. (defvar org-defdecode)
  15360. (defvar org-with-time)
  15361. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15362. default-time default-input inactive)
  15363. "Read a date, possibly a time, and make things smooth for the user.
  15364. The prompt will suggest to enter an ISO date, but you can also enter anything
  15365. which will at least partially be understood by `parse-time-string'.
  15366. Unrecognized parts of the date will default to the current day, month, year,
  15367. hour and minute. If this command is called to replace a timestamp at point,
  15368. or to enter the second timestamp of a range, the default time is taken
  15369. from the existing stamp. Furthermore, the command prefers the future,
  15370. so if you are giving a date where the year is not given, and the day-month
  15371. combination is already past in the current year, it will assume you
  15372. mean next year. For details, see the manual. A few examples:
  15373. 3-2-5 --> 2003-02-05
  15374. feb 15 --> currentyear-02-15
  15375. 2/15 --> currentyear-02-15
  15376. sep 12 9 --> 2009-09-12
  15377. 12:45 --> today 12:45
  15378. 22 sept 0:34 --> currentyear-09-22 0:34
  15379. 12 --> currentyear-currentmonth-12
  15380. Fri --> nearest Friday after today
  15381. -Tue --> last Tuesday
  15382. etc.
  15383. Furthermore you can specify a relative date by giving, as the *first* thing
  15384. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15385. change in days weeks, months, years.
  15386. With a single plus or minus, the date is relative to today. With a double
  15387. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15388. +4d --> four days from today
  15389. +4 --> same as above
  15390. +2w --> two weeks from today
  15391. ++5 --> five days from default date
  15392. The function understands only English month and weekday abbreviations.
  15393. While prompting, a calendar is popped up - you can also select the
  15394. date with the mouse (button 1). The calendar shows a period of three
  15395. months. To scroll it to other months, use the keys `>' and `<'.
  15396. If you don't like the calendar, turn it off with
  15397. \(setq org-read-date-popup-calendar nil)
  15398. With optional argument TO-TIME, the date will immediately be converted
  15399. to an internal time.
  15400. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15401. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15402. still enter a time, and this function will inform the calling routine
  15403. about this change. The calling routine may then choose to change the
  15404. format used to insert the time stamp into the buffer to include the time.
  15405. With optional argument FROM-STRING, read from this string instead from
  15406. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15407. the time/date that is used for everything that is not specified by the
  15408. user."
  15409. (require 'parse-time)
  15410. (let* ((org-time-stamp-rounding-minutes
  15411. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15412. (org-dcst org-display-custom-times)
  15413. (ct (org-current-time))
  15414. (org-def (or org-overriding-default-time default-time ct))
  15415. (org-defdecode (decode-time org-def))
  15416. (dummy (progn
  15417. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15418. (setcar (nthcdr 2 org-defdecode) -1)
  15419. (setcar (nthcdr 1 org-defdecode) 59)
  15420. (setq org-def (apply 'encode-time org-defdecode)
  15421. org-defdecode (decode-time org-def)))))
  15422. (cur-frame (selected-frame))
  15423. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15424. (calendar-frame-setup nil)
  15425. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15426. (calendar-move-hook nil)
  15427. (calendar-view-diary-initially-flag nil)
  15428. (calendar-view-holidays-initially-flag nil)
  15429. (timestr (format-time-string
  15430. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15431. (prompt (concat (if prompt (concat prompt " ") "")
  15432. (format "Date+time [%s]: " timestr)))
  15433. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15434. (cond
  15435. (from-string (setq ans from-string))
  15436. (org-read-date-popup-calendar
  15437. (save-excursion
  15438. (save-window-excursion
  15439. (calendar)
  15440. (when (eq calendar-setup 'calendar-only)
  15441. (setq cal-frame
  15442. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15443. (select-frame cal-frame))
  15444. (org-eval-in-calendar '(setq cursor-type nil) t)
  15445. (unwind-protect
  15446. (progn
  15447. (calendar-forward-day (- (time-to-days org-def)
  15448. (calendar-absolute-from-gregorian
  15449. (calendar-current-date))))
  15450. (org-eval-in-calendar nil t)
  15451. (let* ((old-map (current-local-map))
  15452. (map (copy-keymap calendar-mode-map))
  15453. (minibuffer-local-map
  15454. (copy-keymap org-read-date-minibuffer-local-map)))
  15455. (org-defkey map (kbd "RET") 'org-calendar-select)
  15456. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15457. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15458. (unwind-protect
  15459. (progn
  15460. (use-local-map map)
  15461. (setq org-read-date-inactive inactive)
  15462. (add-hook 'post-command-hook 'org-read-date-display)
  15463. (setq org-ans0 (read-string prompt default-input
  15464. 'org-read-date-history nil))
  15465. ;; org-ans0: from prompt
  15466. ;; org-ans1: from mouse click
  15467. ;; org-ans2: from calendar motion
  15468. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15469. (remove-hook 'post-command-hook 'org-read-date-display)
  15470. (use-local-map old-map)
  15471. (when org-read-date-overlay
  15472. (delete-overlay org-read-date-overlay)
  15473. (setq org-read-date-overlay nil)))))
  15474. (bury-buffer "*Calendar*")
  15475. (when cal-frame
  15476. (delete-frame cal-frame)
  15477. (select-frame-set-input-focus cur-frame))))))
  15478. (t ; Naked prompt only
  15479. (unwind-protect
  15480. (setq ans (read-string prompt default-input
  15481. 'org-read-date-history timestr))
  15482. (when org-read-date-overlay
  15483. (delete-overlay org-read-date-overlay)
  15484. (setq org-read-date-overlay nil)))))
  15485. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15486. (when org-read-date-analyze-forced-year
  15487. (message "Year was forced into %s"
  15488. (if org-read-date-force-compatible-dates
  15489. "compatible range (1970-2037)"
  15490. "range representable on this machine"))
  15491. (ding))
  15492. ;; One round trip to get rid of 34th of August and stuff like that....
  15493. (setq final (decode-time (apply 'encode-time final)))
  15494. (setq org-read-date-final-answer ans)
  15495. (if to-time
  15496. (apply 'encode-time final)
  15497. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15498. (format "%04d-%02d-%02d %02d:%02d"
  15499. (nth 5 final) (nth 4 final) (nth 3 final)
  15500. (nth 2 final) (nth 1 final))
  15501. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15502. (defun org-read-date-display ()
  15503. "Display the current date prompt interpretation in the minibuffer."
  15504. (when org-read-date-display-live
  15505. (when org-read-date-overlay
  15506. (delete-overlay org-read-date-overlay))
  15507. (when (minibufferp (current-buffer))
  15508. (save-excursion
  15509. (end-of-line 1)
  15510. (while (not (equal (buffer-substring
  15511. (max (point-min) (- (point) 4)) (point))
  15512. " "))
  15513. (insert " ")))
  15514. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15515. " " (or org-ans1 org-ans2)))
  15516. (org-end-time-was-given nil)
  15517. (f (org-read-date-analyze ans org-def org-defdecode))
  15518. (fmts (if org-dcst
  15519. org-time-stamp-custom-formats
  15520. org-time-stamp-formats))
  15521. (fmt (if (or org-with-time
  15522. (and (boundp 'org-time-was-given) org-time-was-given))
  15523. (cdr fmts)
  15524. (car fmts)))
  15525. (txt (format-time-string fmt (apply 'encode-time f)))
  15526. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15527. (txt (concat "=> " txt)))
  15528. (when (and org-end-time-was-given
  15529. (string-match org-plain-time-of-day-regexp txt))
  15530. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15531. org-end-time-was-given
  15532. (substring txt (match-end 0)))))
  15533. (when org-read-date-analyze-futurep
  15534. (setq txt (concat txt " (=>F)")))
  15535. (setq org-read-date-overlay
  15536. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15537. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15538. (defun org-read-date-analyze (ans org-def org-defdecode)
  15539. "Analyze the combined answer of the date prompt."
  15540. ;; FIXME: cleanup and comment
  15541. ;; Pass `current-time' result to `decode-time' (instead of calling
  15542. ;; without arguments) so that only `current-time' has to be
  15543. ;; overriden in tests.
  15544. (let ((nowdecode (decode-time (current-time)))
  15545. delta deltan deltaw deltadef year month day
  15546. hour minute second wday pm h2 m2 tl wday1
  15547. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15548. (setq org-read-date-analyze-futurep nil
  15549. org-read-date-analyze-forced-year nil)
  15550. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15551. (setq ans "+0"))
  15552. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15553. (setq ans (replace-match "" t t ans)
  15554. deltan (car delta)
  15555. deltaw (nth 1 delta)
  15556. deltadef (nth 2 delta)))
  15557. ;; Check if there is an iso week date in there. If yes, store the
  15558. ;; info and postpone interpreting it until the rest of the parsing
  15559. ;; is done.
  15560. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15561. (setq iso-year (if (match-end 1)
  15562. (org-small-year-to-year
  15563. (string-to-number (match-string 1 ans))))
  15564. iso-weekday (if (match-end 3)
  15565. (string-to-number (match-string 3 ans)))
  15566. iso-week (string-to-number (match-string 2 ans)))
  15567. (setq ans (replace-match "" t t ans)))
  15568. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15569. (when (string-match
  15570. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15571. (setq year (if (match-end 2)
  15572. (string-to-number (match-string 2 ans))
  15573. (progn (setq kill-year t)
  15574. (string-to-number (format-time-string "%Y"))))
  15575. month (string-to-number (match-string 3 ans))
  15576. day (string-to-number (match-string 4 ans)))
  15577. (if (< year 100) (setq year (+ 2000 year)))
  15578. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15579. t nil ans)))
  15580. ;; Help matching dotted european dates
  15581. (when (string-match
  15582. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15583. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15584. (setq kill-year t)
  15585. (string-to-number (format-time-string "%Y")))
  15586. day (string-to-number (match-string 1 ans))
  15587. month (string-to-number (match-string 2 ans))
  15588. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15589. t nil ans)))
  15590. ;; Help matching american dates, like 5/30 or 5/30/7
  15591. (when (string-match
  15592. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15593. (setq year (if (match-end 4)
  15594. (string-to-number (match-string 4 ans))
  15595. (progn (setq kill-year t)
  15596. (string-to-number (format-time-string "%Y"))))
  15597. month (string-to-number (match-string 1 ans))
  15598. day (string-to-number (match-string 2 ans)))
  15599. (if (< year 100) (setq year (+ 2000 year)))
  15600. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15601. t nil ans)))
  15602. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15603. ;; If there is a time with am/pm, and *no* time without it, we convert
  15604. ;; so that matching will be successful.
  15605. (loop for i from 1 to 2 do ; twice, for end time as well
  15606. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15607. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15608. (setq hour (string-to-number (match-string 1 ans))
  15609. minute (if (match-end 3)
  15610. (string-to-number (match-string 3 ans))
  15611. 0)
  15612. pm (equal ?p
  15613. (string-to-char (downcase (match-string 4 ans)))))
  15614. (if (and (= hour 12) (not pm))
  15615. (setq hour 0)
  15616. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15617. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15618. t t ans))))
  15619. ;; Check if a time range is given as a duration
  15620. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15621. (setq hour (string-to-number (match-string 1 ans))
  15622. h2 (+ hour (string-to-number (match-string 3 ans)))
  15623. minute (string-to-number (match-string 2 ans))
  15624. m2 (+ minute (if (match-end 5) (string-to-number
  15625. (match-string 5 ans))0)))
  15626. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15627. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15628. t t ans)))
  15629. ;; Check if there is a time range
  15630. (when (boundp 'org-end-time-was-given)
  15631. (setq org-time-was-given nil)
  15632. (when (and (string-match org-plain-time-of-day-regexp ans)
  15633. (match-end 8))
  15634. (setq org-end-time-was-given (match-string 8 ans))
  15635. (setq ans (concat (substring ans 0 (match-beginning 7))
  15636. (substring ans (match-end 7))))))
  15637. (setq tl (parse-time-string ans)
  15638. day (or (nth 3 tl) (nth 3 org-defdecode))
  15639. month
  15640. (cond ((nth 4 tl))
  15641. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15642. ;; Day was specified. Make sure DAY+MONTH
  15643. ;; combination happens in the future.
  15644. ((nth 3 tl)
  15645. (setq futurep t)
  15646. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15647. (nth 4 nowdecode)))
  15648. (t (nth 4 org-defdecode)))
  15649. year
  15650. (cond ((and (not kill-year) (nth 5 tl)))
  15651. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15652. ;; Month was guessed in the future and is at least
  15653. ;; equal to NOWDECODE's. Fix year accordingly.
  15654. (futurep
  15655. (if (or (> month (nth 4 nowdecode))
  15656. (>= day (nth 3 nowdecode)))
  15657. (nth 5 nowdecode)
  15658. (1+ (nth 5 nowdecode))))
  15659. ;; Month was specified. Make sure MONTH+YEAR
  15660. ;; combination happens in the future.
  15661. ((nth 4 tl)
  15662. (setq futurep t)
  15663. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15664. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15665. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15666. (t (nth 5 nowdecode))))
  15667. (t (nth 5 org-defdecode)))
  15668. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15669. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15670. second (or (nth 0 tl) 0)
  15671. wday (nth 6 tl))
  15672. (when (and (eq org-read-date-prefer-future 'time)
  15673. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15674. (equal day (nth 3 nowdecode))
  15675. (equal month (nth 4 nowdecode))
  15676. (equal year (nth 5 nowdecode))
  15677. (nth 2 tl)
  15678. (or (< (nth 2 tl) (nth 2 nowdecode))
  15679. (and (= (nth 2 tl) (nth 2 nowdecode))
  15680. (nth 1 tl)
  15681. (< (nth 1 tl) (nth 1 nowdecode)))))
  15682. (setq day (1+ day)
  15683. futurep t))
  15684. ;; Special date definitions below
  15685. (cond
  15686. (iso-week
  15687. ;; There was an iso week
  15688. (require 'cal-iso)
  15689. (setq futurep nil)
  15690. (setq year (or iso-year year)
  15691. day (or iso-weekday wday 1)
  15692. wday nil ; to make sure that the trigger below does not match
  15693. iso-date (calendar-gregorian-from-absolute
  15694. (calendar-iso-to-absolute
  15695. (list iso-week day year))))
  15696. ; FIXME: Should we also push ISO weeks into the future?
  15697. ; (when (and org-read-date-prefer-future
  15698. ; (not iso-year)
  15699. ; (< (calendar-absolute-from-gregorian iso-date)
  15700. ; (time-to-days (current-time))))
  15701. ; (setq year (1+ year)
  15702. ; iso-date (calendar-gregorian-from-absolute
  15703. ; (calendar-iso-to-absolute
  15704. ; (list iso-week day year)))))
  15705. (setq month (car iso-date)
  15706. year (nth 2 iso-date)
  15707. day (nth 1 iso-date)))
  15708. (deltan
  15709. (setq futurep nil)
  15710. (unless deltadef
  15711. ;; Pass `current-time' result to `decode-time' (instead of
  15712. ;; calling without arguments) so that only `current-time' has
  15713. ;; to be overriden in tests.
  15714. (let ((now (decode-time (current-time))))
  15715. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15716. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15717. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15718. ((equal deltaw "m") (setq month (+ month deltan)))
  15719. ((equal deltaw "y") (setq year (+ year deltan)))))
  15720. ((and wday (not (nth 3 tl)))
  15721. ;; Weekday was given, but no day, so pick that day in the week
  15722. ;; on or after the derived date.
  15723. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15724. (unless (equal wday wday1)
  15725. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15726. (if (and (boundp 'org-time-was-given)
  15727. (nth 2 tl))
  15728. (setq org-time-was-given t))
  15729. (if (< year 100) (setq year (+ 2000 year)))
  15730. ;; Check of the date is representable
  15731. (if org-read-date-force-compatible-dates
  15732. (progn
  15733. (if (< year 1970)
  15734. (setq year 1970 org-read-date-analyze-forced-year t))
  15735. (if (> year 2037)
  15736. (setq year 2037 org-read-date-analyze-forced-year t)))
  15737. (condition-case nil
  15738. (ignore (encode-time second minute hour day month year))
  15739. (error
  15740. (setq year (nth 5 org-defdecode))
  15741. (setq org-read-date-analyze-forced-year t))))
  15742. (setq org-read-date-analyze-futurep futurep)
  15743. (list second minute hour day month year)))
  15744. (defvar parse-time-weekdays)
  15745. (defun org-read-date-get-relative (s today default)
  15746. "Check string S for special relative date string.
  15747. TODAY and DEFAULT are internal times, for today and for a default.
  15748. Return shift list (N what def-flag)
  15749. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15750. N is the number of WHATs to shift.
  15751. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15752. the DEFAULT date rather than TODAY."
  15753. (require 'parse-time)
  15754. (when (and
  15755. (string-match
  15756. (concat
  15757. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15758. "\\([0-9]+\\)?"
  15759. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15760. "\\([ \t]\\|$\\)") s)
  15761. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15762. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15763. (string-to-char (substring (match-string 1 s) -1))
  15764. ?+))
  15765. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15766. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15767. (what (if (match-end 3) (match-string 3 s) "d"))
  15768. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15769. (date (if rel default today))
  15770. (wday (nth 6 (decode-time date)))
  15771. delta)
  15772. (if wday1
  15773. (progn
  15774. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15775. (if (= delta 0) (setq delta 7))
  15776. (if (= dir ?-)
  15777. (progn
  15778. (setq delta (- delta 7))
  15779. (if (= delta 0) (setq delta -7))))
  15780. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15781. (list delta "d" rel))
  15782. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15783. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15784. "Turn a user-specified date into the internal representation.
  15785. The internal representation needed by the calendar is (month day year).
  15786. This is a wrapper to handle the brain-dead convention in calendar that
  15787. user function argument order change dependent on argument order."
  15788. (if (boundp 'calendar-date-style)
  15789. (cond
  15790. ((eq calendar-date-style 'american)
  15791. (list arg1 arg2 arg3))
  15792. ((eq calendar-date-style 'european)
  15793. (list arg2 arg1 arg3))
  15794. ((eq calendar-date-style 'iso)
  15795. (list arg2 arg3 arg1)))
  15796. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15797. (if (org-bound-and-true-p european-calendar-style)
  15798. (list arg2 arg1 arg3)
  15799. (list arg1 arg2 arg3)))))
  15800. (defun org-eval-in-calendar (form &optional keepdate)
  15801. "Eval FORM in the calendar window and return to current window.
  15802. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15803. otherwise stick to the current value of `org-ans2'."
  15804. (let ((sf (selected-frame))
  15805. (sw (selected-window)))
  15806. (select-window (get-buffer-window "*Calendar*" t))
  15807. (eval form)
  15808. (when (and (not keepdate) (calendar-cursor-to-date))
  15809. (let* ((date (calendar-cursor-to-date))
  15810. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15811. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15812. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15813. (select-window sw)
  15814. (org-select-frame-set-input-focus sf)))
  15815. (defun org-calendar-select ()
  15816. "Return to `org-read-date' with the date currently selected.
  15817. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15818. (interactive)
  15819. (when (calendar-cursor-to-date)
  15820. (let* ((date (calendar-cursor-to-date))
  15821. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15822. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15823. (if (active-minibuffer-window) (exit-minibuffer))))
  15824. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15825. "Insert a date stamp for the date given by the internal TIME.
  15826. See `format-time-string' for the format of TIME.
  15827. WITH-HM means use the stamp format that includes the time of the day.
  15828. INACTIVE means use square brackets instead of angular ones, so that the
  15829. stamp will not contribute to the agenda.
  15830. PRE and POST are optional strings to be inserted before and after the
  15831. stamp.
  15832. The command returns the inserted time stamp."
  15833. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15834. stamp)
  15835. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15836. (insert-before-markers (or pre ""))
  15837. (when (listp extra)
  15838. (setq extra (car extra))
  15839. (if (and (stringp extra)
  15840. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15841. (setq extra (format "-%02d:%02d"
  15842. (string-to-number (match-string 1 extra))
  15843. (string-to-number (match-string 2 extra))))
  15844. (setq extra nil)))
  15845. (when extra
  15846. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15847. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15848. (insert-before-markers (or post ""))
  15849. (setq org-last-inserted-timestamp stamp)))
  15850. (defun org-toggle-time-stamp-overlays ()
  15851. "Toggle the use of custom time stamp formats."
  15852. (interactive)
  15853. (setq org-display-custom-times (not org-display-custom-times))
  15854. (unless org-display-custom-times
  15855. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15856. (while (setq p (next-single-property-change p 'display))
  15857. (if (and (get-text-property p 'display)
  15858. (eq (get-text-property p 'face) 'org-date))
  15859. (remove-text-properties
  15860. p (setq p (next-single-property-change p 'display))
  15861. '(display t))))
  15862. (set-buffer-modified-p bmp)))
  15863. (if (featurep 'xemacs)
  15864. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15865. (org-restart-font-lock)
  15866. (setq org-table-may-need-update t)
  15867. (if org-display-custom-times
  15868. (message "Time stamps are overlaid with custom format")
  15869. (message "Time stamp overlays removed")))
  15870. (defun org-display-custom-time (beg end)
  15871. "Overlay modified time stamp format over timestamp between BEG and END."
  15872. (let* ((ts (buffer-substring beg end))
  15873. t1 w1 with-hm tf time str w2 (off 0))
  15874. (save-match-data
  15875. (setq t1 (org-parse-time-string ts t))
  15876. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15877. (setq off (- (match-end 0) (match-beginning 0)))))
  15878. (setq end (- end off))
  15879. (setq w1 (- end beg)
  15880. with-hm (and (nth 1 t1) (nth 2 t1))
  15881. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15882. time (org-fix-decoded-time t1)
  15883. str (org-add-props
  15884. (format-time-string
  15885. (substring tf 1 -1) (apply 'encode-time time))
  15886. nil 'mouse-face 'highlight)
  15887. w2 (length str))
  15888. (if (not (= w2 w1))
  15889. (add-text-properties (1+ beg) (+ 2 beg)
  15890. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15891. (if (featurep 'xemacs)
  15892. (progn
  15893. (put-text-property beg end 'invisible t)
  15894. (put-text-property beg end 'end-glyph (make-glyph str)))
  15895. (put-text-property beg end 'display str))))
  15896. (defun org-fix-decoded-time (time)
  15897. "Set 0 instead of nil for the first 6 elements of time.
  15898. Don't touch the rest."
  15899. (let ((n 0))
  15900. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15901. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15902. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15903. "Difference between TIMESTAMP-STRING and now in days.
  15904. If SECONDS is non-nil, return the difference in seconds."
  15905. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15906. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15907. (funcall fdiff (current-time)))))
  15908. (defun org-deadline-close (timestamp-string &optional ndays)
  15909. "Is the time in TIMESTAMP-STRING close to the current date?"
  15910. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15911. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15912. (not (org-entry-is-done-p))))
  15913. (defun org-get-wdays (ts &optional delay zero-delay)
  15914. "Get the deadline lead time appropriate for timestring TS.
  15915. When DELAY is non-nil, get the delay time for scheduled items
  15916. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15917. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15918. don't try to find the delay cookie in the scheduled timestamp."
  15919. (let ((tv (if delay org-scheduled-delay-days
  15920. org-deadline-warning-days)))
  15921. (cond
  15922. ((or (and delay (< tv 0))
  15923. (and delay zero-delay (<= tv 0))
  15924. (and (not delay) (<= tv 0)))
  15925. ;; Enforce this value no matter what
  15926. (- tv))
  15927. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15928. ;; lead time is specified.
  15929. (floor (* (string-to-number (match-string 1 ts))
  15930. (cdr (assoc (match-string 2 ts)
  15931. '(("d" . 1) ("w" . 7)
  15932. ("m" . 30.4) ("y" . 365.25)
  15933. ("h" . 0.041667)))))))
  15934. ;; go for the default.
  15935. (t tv))))
  15936. (defun org-calendar-select-mouse (ev)
  15937. "Return to `org-read-date' with the date currently selected.
  15938. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15939. (interactive "e")
  15940. (mouse-set-point ev)
  15941. (when (calendar-cursor-to-date)
  15942. (let* ((date (calendar-cursor-to-date))
  15943. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15944. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15945. (if (active-minibuffer-window) (exit-minibuffer))))
  15946. (defun org-check-deadlines (ndays)
  15947. "Check if there are any deadlines due or past due.
  15948. A deadline is considered due if it happens within `org-deadline-warning-days'
  15949. days from today's date. If the deadline appears in an entry marked DONE,
  15950. it is not shown. The prefix arg NDAYS can be used to test that many
  15951. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15952. (interactive "P")
  15953. (let* ((org-warn-days
  15954. (cond
  15955. ((equal ndays '(4)) 100000)
  15956. (ndays (prefix-numeric-value ndays))
  15957. (t (abs org-deadline-warning-days))))
  15958. (case-fold-search nil)
  15959. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15960. (callback
  15961. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15962. (message "%d deadlines past-due or due within %d days"
  15963. (org-occur regexp nil callback)
  15964. org-warn-days)))
  15965. (defsubst org-re-timestamp (type)
  15966. "Return a regexp for timestamp TYPE.
  15967. Allowed values for TYPE are:
  15968. all: all timestamps
  15969. active: only active timestamps (<...>)
  15970. inactive: only inactive timestamps ([...])
  15971. scheduled: only scheduled timestamps
  15972. deadline: only deadline timestamps
  15973. closed: only closed time-stamps
  15974. When TYPE is nil, fall back on returning a regexp that matches
  15975. both scheduled and deadline timestamps."
  15976. (case type
  15977. (all org-ts-regexp-both)
  15978. (active org-ts-regexp)
  15979. (inactive org-ts-regexp-inactive)
  15980. (scheduled org-scheduled-time-regexp)
  15981. (deadline org-deadline-time-regexp)
  15982. (closed org-closed-time-regexp)
  15983. (otherwise
  15984. (concat "\\<"
  15985. (regexp-opt (list org-deadline-string org-scheduled-string))
  15986. " *<\\([^>]+\\)>"))))
  15987. (defun org-check-before-date (date)
  15988. "Check if there are deadlines or scheduled entries before DATE."
  15989. (interactive (list (org-read-date)))
  15990. (let ((case-fold-search nil)
  15991. (regexp (org-re-timestamp org-ts-type))
  15992. (callback
  15993. `(lambda ()
  15994. (let ((match (match-string 1)))
  15995. (and ,(if (memq org-ts-type '(active inactive all))
  15996. '(eq (org-element-type (org-element-context)) 'timestamp)
  15997. '(org-at-planning-p))
  15998. (time-less-p
  15999. (org-time-string-to-time match)
  16000. (org-time-string-to-time date)))))))
  16001. (message "%d entries before %s"
  16002. (org-occur regexp nil callback) date)))
  16003. (defun org-check-after-date (date)
  16004. "Check if there are deadlines or scheduled entries after DATE."
  16005. (interactive (list (org-read-date)))
  16006. (let ((case-fold-search nil)
  16007. (regexp (org-re-timestamp org-ts-type))
  16008. (callback
  16009. `(lambda ()
  16010. (let ((match (match-string 1)))
  16011. (and ,(if (memq org-ts-type '(active inactive all))
  16012. '(eq (org-element-type (org-element-context)) 'timestamp)
  16013. '(org-at-planning-p))
  16014. (not (time-less-p
  16015. (org-time-string-to-time match)
  16016. (org-time-string-to-time date))))))))
  16017. (message "%d entries after %s"
  16018. (org-occur regexp nil callback) date)))
  16019. (defun org-check-dates-range (start-date end-date)
  16020. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16021. (interactive (list (org-read-date nil nil nil "Range starts")
  16022. (org-read-date nil nil nil "Range end")))
  16023. (let ((case-fold-search nil)
  16024. (regexp (org-re-timestamp org-ts-type))
  16025. (callback
  16026. `(lambda ()
  16027. (let ((match (match-string 1)))
  16028. (and
  16029. ,(if (memq org-ts-type '(active inactive all))
  16030. '(eq (org-element-type (org-element-context)) 'timestamp)
  16031. '(org-at-planning-p))
  16032. (not (time-less-p
  16033. (org-time-string-to-time match)
  16034. (org-time-string-to-time start-date)))
  16035. (time-less-p
  16036. (org-time-string-to-time match)
  16037. (org-time-string-to-time end-date)))))))
  16038. (message "%d entries between %s and %s"
  16039. (org-occur regexp nil callback) start-date end-date)))
  16040. (defun org-evaluate-time-range (&optional to-buffer)
  16041. "Evaluate a time range by computing the difference between start and end.
  16042. Normally the result is just printed in the echo area, but with prefix arg
  16043. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16044. If the time range is actually in a table, the result is inserted into the
  16045. next column.
  16046. For time difference computation, a year is assumed to be exactly 365
  16047. days in order to avoid rounding problems."
  16048. (interactive "P")
  16049. (or
  16050. (org-clock-update-time-maybe)
  16051. (save-excursion
  16052. (unless (org-at-date-range-p t)
  16053. (goto-char (point-at-bol))
  16054. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16055. (if (not (org-at-date-range-p t))
  16056. (user-error "Not at a time-stamp range, and none found in current line")))
  16057. (let* ((ts1 (match-string 1))
  16058. (ts2 (match-string 2))
  16059. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16060. (match-end (match-end 0))
  16061. (time1 (org-time-string-to-time ts1))
  16062. (time2 (org-time-string-to-time ts2))
  16063. (t1 (org-float-time time1))
  16064. (t2 (org-float-time time2))
  16065. (diff (abs (- t2 t1)))
  16066. (negative (< (- t2 t1) 0))
  16067. ;; (ys (floor (* 365 24 60 60)))
  16068. (ds (* 24 60 60))
  16069. (hs (* 60 60))
  16070. (fy "%dy %dd %02d:%02d")
  16071. (fy1 "%dy %dd")
  16072. (fd "%dd %02d:%02d")
  16073. (fd1 "%dd")
  16074. (fh "%02d:%02d")
  16075. y d h m align)
  16076. (if havetime
  16077. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16078. y 0
  16079. d (floor (/ diff ds)) diff (mod diff ds)
  16080. h (floor (/ diff hs)) diff (mod diff hs)
  16081. m (floor (/ diff 60)))
  16082. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16083. y 0
  16084. d (floor (+ (/ diff ds) 0.5))
  16085. h 0 m 0))
  16086. (if (not to-buffer)
  16087. (message "%s" (org-make-tdiff-string y d h m))
  16088. (if (org-at-table-p)
  16089. (progn
  16090. (goto-char match-end)
  16091. (setq align t)
  16092. (and (looking-at " *|") (goto-char (match-end 0))))
  16093. (goto-char match-end))
  16094. (if (looking-at
  16095. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16096. (replace-match ""))
  16097. (if negative (insert " -"))
  16098. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16099. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16100. (insert " " (format fh h m))))
  16101. (if align (org-table-align))
  16102. (message "Time difference inserted")))))
  16103. (defun org-make-tdiff-string (y d h m)
  16104. (let ((fmt "")
  16105. (l nil))
  16106. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16107. l (push y l)))
  16108. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16109. l (push d l)))
  16110. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16111. l (push h l)))
  16112. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16113. l (push m l)))
  16114. (apply 'format fmt (nreverse l))))
  16115. (defun org-time-string-to-time (s &optional buffer pos)
  16116. "Convert a timestamp string into internal time."
  16117. (condition-case errdata
  16118. (apply 'encode-time (org-parse-time-string s))
  16119. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16120. s (if (not (and buffer pos))
  16121. ""
  16122. (format " at %d in buffer `%s'" pos buffer))
  16123. (cdr errdata)))))
  16124. (defun org-time-string-to-seconds (s)
  16125. "Convert a timestamp string to a number of seconds."
  16126. (org-float-time (org-time-string-to-time s)))
  16127. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16128. "Convert a time stamp to an absolute day number.
  16129. If there is a specifier for a cyclic time stamp, get the closest
  16130. date to DAYNR.
  16131. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16132. The variable `date' is bound by the calendar when this is called."
  16133. (cond
  16134. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16135. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16136. daynr
  16137. (+ daynr 1000)))
  16138. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16139. (> (string-to-number (match-string 1 s)) 0))
  16140. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16141. (time-to-days (current-time))) (match-string 0 s)
  16142. prefer show-all))
  16143. (t (time-to-days
  16144. (condition-case errdata
  16145. (apply 'encode-time (org-parse-time-string s))
  16146. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16147. s (if (not (and buffer pos))
  16148. ""
  16149. (format " at %d in buffer `%s'" pos buffer))
  16150. (cdr errdata))))))))
  16151. (defun org-days-to-iso-week (days)
  16152. "Return the iso week number."
  16153. (require 'cal-iso)
  16154. (car (calendar-iso-from-absolute days)))
  16155. (defun org-small-year-to-year (year)
  16156. "Convert 2-digit years into 4-digit years.
  16157. YEAR is expanded into one of the 30 next years, if possible, or
  16158. into a past one. Any year larger than 99 is returned unchanged."
  16159. (if (>= year 100) year
  16160. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16161. (century (/ current 100))
  16162. (offset (- year (% current 100))))
  16163. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16164. ((> offset -70) (+ (* century 100) year))
  16165. (t (+ (* (1+ century) 100) year))))))
  16166. (defun org-time-from-absolute (d)
  16167. "Return the time corresponding to date D.
  16168. D may be an absolute day number, or a calendar-type list (month day year)."
  16169. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16170. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16171. (defun org-calendar-holiday ()
  16172. "List of holidays, for Diary display in Org-mode."
  16173. (require 'holidays)
  16174. (let ((hl (funcall
  16175. (if (fboundp 'calendar-check-holidays)
  16176. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16177. (if hl (mapconcat 'identity hl "; "))))
  16178. (defun org-diary-sexp-entry (sexp entry date)
  16179. "Process a SEXP diary ENTRY for DATE."
  16180. (require 'diary-lib)
  16181. (let ((result (if calendar-debug-sexp
  16182. (let ((stack-trace-on-error t))
  16183. (eval (car (read-from-string sexp))))
  16184. (condition-case nil
  16185. (eval (car (read-from-string sexp)))
  16186. (error
  16187. (beep)
  16188. (message "Bad sexp at line %d in %s: %s"
  16189. (org-current-line)
  16190. (buffer-file-name) sexp)
  16191. (sleep-for 2))))))
  16192. (cond ((stringp result) (split-string result "; "))
  16193. ((and (consp result)
  16194. (not (consp (cdr result)))
  16195. (stringp (cdr result))) (cdr result))
  16196. ((and (consp result)
  16197. (stringp (car result))) result)
  16198. (result entry))))
  16199. (defun org-diary-to-ical-string (frombuf)
  16200. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16201. This uses the icalendar.el library."
  16202. (let* ((tmpdir (if (featurep 'xemacs)
  16203. (temp-directory)
  16204. temporary-file-directory))
  16205. (tmpfile (make-temp-name
  16206. (expand-file-name "orgics" tmpdir)))
  16207. buf rtn b e)
  16208. (with-current-buffer frombuf
  16209. (icalendar-export-region (point-min) (point-max) tmpfile)
  16210. (setq buf (find-buffer-visiting tmpfile))
  16211. (set-buffer buf)
  16212. (goto-char (point-min))
  16213. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16214. (setq b (match-beginning 0)))
  16215. (goto-char (point-max))
  16216. (if (re-search-backward "^END:VEVENT" nil t)
  16217. (setq e (match-end 0)))
  16218. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16219. (kill-buffer buf)
  16220. (delete-file tmpfile)
  16221. rtn))
  16222. (defun org-closest-date (start current change prefer show-all)
  16223. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16224. When PREFER is `past', return a date that is either CURRENT or past.
  16225. When PREFER is `future', return a date that is either CURRENT or future.
  16226. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16227. ;; Make the proper lists from the dates
  16228. (catch 'exit
  16229. (let ((a1 '(("h" . hour)
  16230. ("d" . day)
  16231. ("w" . week)
  16232. ("m" . month)
  16233. ("y" . year)))
  16234. (shour (nth 2 (org-parse-time-string start)))
  16235. dn dw sday cday n1 n2 n0
  16236. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16237. (setq start (org-date-to-gregorian start)
  16238. current (org-date-to-gregorian
  16239. (if show-all
  16240. current
  16241. (time-to-days (current-time))))
  16242. sday (calendar-absolute-from-gregorian start)
  16243. cday (calendar-absolute-from-gregorian current))
  16244. (if (<= cday sday) (throw 'exit sday))
  16245. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16246. (setq dn (string-to-number (match-string 1 change))
  16247. dw (cdr (assoc (match-string 2 change) a1))))
  16248. (unless (and dn (> dn 0))
  16249. (user-error "Invalid change specifier: %s" change))
  16250. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16251. (cond
  16252. ((eq dw 'hour)
  16253. (let ((missing-hours
  16254. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16255. dn)))
  16256. (setq n1 (if (zerop missing-hours) cday
  16257. (- cday (1+ (floor (/ missing-hours 24)))))
  16258. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16259. ((eq dw 'day)
  16260. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16261. n2 (+ n1 dn)))
  16262. ((eq dw 'year)
  16263. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16264. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16265. (setq date1 (list m d y1)
  16266. n1 (calendar-absolute-from-gregorian date1)
  16267. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16268. n2 (calendar-absolute-from-gregorian date2)))
  16269. ((eq dw 'month)
  16270. ;; approx number of month between the two dates
  16271. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16272. ;; How often does dn fit in there?
  16273. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16274. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16275. m (+ m nm)
  16276. ny (floor (/ m 12))
  16277. y (+ y ny)
  16278. m (- m (* ny 12)))
  16279. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16280. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16281. (setq m2 (+ m dn) y2 y)
  16282. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16283. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16284. (while (<= n2 cday)
  16285. (setq n1 n2 m m2 y y2)
  16286. (setq m2 (+ m dn) y2 y)
  16287. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16288. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16289. ;; Make sure n1 is the earlier date
  16290. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16291. (if show-all
  16292. (cond
  16293. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16294. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16295. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16296. (cond
  16297. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16298. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16299. (t (if (= cday n1) n1 n2)))))))
  16300. (defun org-date-to-gregorian (date)
  16301. "Turn any specification of DATE into a Gregorian date for the calendar."
  16302. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16303. ((and (listp date) (= (length date) 3)) date)
  16304. ((stringp date)
  16305. (setq date (org-parse-time-string date))
  16306. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16307. ((listp date)
  16308. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16309. (defun org-parse-time-string (s &optional nodefault)
  16310. "Parse the standard Org-mode time string.
  16311. This should be a lot faster than the normal `parse-time-string'.
  16312. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16313. hour and minute fields will be nil if not given."
  16314. (cond ((string-match org-ts-regexp0 s)
  16315. (list 0
  16316. (if (or (match-beginning 8) (not nodefault))
  16317. (string-to-number (or (match-string 8 s) "0")))
  16318. (if (or (match-beginning 7) (not nodefault))
  16319. (string-to-number (or (match-string 7 s) "0")))
  16320. (string-to-number (match-string 4 s))
  16321. (string-to-number (match-string 3 s))
  16322. (string-to-number (match-string 2 s))
  16323. nil nil nil))
  16324. ((string-match "^<[^>]+>$" s)
  16325. (decode-time (seconds-to-time (org-matcher-time s))))
  16326. (t (error "Not a standard Org-mode time string: %s" s))))
  16327. (defun org-timestamp-up (&optional arg)
  16328. "Increase the date item at the cursor by one.
  16329. If the cursor is on the year, change the year. If it is on the month,
  16330. the day or the time, change that.
  16331. With prefix ARG, change by that many units."
  16332. (interactive "p")
  16333. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16334. (defun org-timestamp-down (&optional arg)
  16335. "Decrease the date item at the cursor by one.
  16336. If the cursor is on the year, change the year. If it is on the month,
  16337. the day or the time, change that.
  16338. With prefix ARG, change by that many units."
  16339. (interactive "p")
  16340. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16341. (defun org-timestamp-up-day (&optional arg)
  16342. "Increase the date in the time stamp by one day.
  16343. With prefix ARG, change that many days."
  16344. (interactive "p")
  16345. (if (and (not (org-at-timestamp-p t))
  16346. (org-at-heading-p))
  16347. (org-todo 'up)
  16348. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16349. (defun org-timestamp-down-day (&optional arg)
  16350. "Decrease the date in the time stamp by one day.
  16351. With prefix ARG, change that many days."
  16352. (interactive "p")
  16353. (if (and (not (org-at-timestamp-p t))
  16354. (org-at-heading-p))
  16355. (org-todo 'down)
  16356. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16357. (defun org-at-timestamp-p (&optional inactive-ok)
  16358. "Non-nil if point is inside a timestamp.
  16359. When optional argument INACTIVE-OK is non-nil, also consider
  16360. inactive timestamps.
  16361. When this function returns a non-nil value, match data is set
  16362. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16363. INACTIVE-OK."
  16364. (interactive)
  16365. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16366. (pos (point))
  16367. (ans (or (looking-at tsr)
  16368. (save-excursion
  16369. (skip-chars-backward "^[<\n\r\t")
  16370. (if (> (point) (point-min)) (backward-char 1))
  16371. (and (looking-at tsr)
  16372. (> (- (match-end 0) pos) -1))))))
  16373. (and ans
  16374. (boundp 'org-ts-what)
  16375. (setq org-ts-what
  16376. (cond
  16377. ((= pos (match-beginning 0)) 'bracket)
  16378. ;; Point is considered to be "on the bracket" whether
  16379. ;; it's really on it or right after it.
  16380. ((= pos (1- (match-end 0))) 'bracket)
  16381. ((= pos (match-end 0)) 'after)
  16382. ((org-pos-in-match-range pos 2) 'year)
  16383. ((org-pos-in-match-range pos 3) 'month)
  16384. ((org-pos-in-match-range pos 7) 'hour)
  16385. ((org-pos-in-match-range pos 8) 'minute)
  16386. ((or (org-pos-in-match-range pos 4)
  16387. (org-pos-in-match-range pos 5)) 'day)
  16388. ((and (> pos (or (match-end 8) (match-end 5)))
  16389. (< pos (match-end 0)))
  16390. (- pos (or (match-end 8) (match-end 5))))
  16391. (t 'day))))
  16392. ans))
  16393. (defun org-toggle-timestamp-type ()
  16394. "Toggle the type (<active> or [inactive]) of a time stamp."
  16395. (interactive)
  16396. (when (org-at-timestamp-p t)
  16397. (let ((beg (match-beginning 0)) (end (match-end 0))
  16398. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16399. (save-excursion
  16400. (goto-char beg)
  16401. (while (re-search-forward "[][<>]" end t)
  16402. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16403. t t)))
  16404. (message "Timestamp is now %sactive"
  16405. (if (equal (char-after beg) ?<) "" "in")))))
  16406. (defun org-at-clock-log-p nil
  16407. "Is the cursor on the clock log line?"
  16408. (save-excursion
  16409. (move-beginning-of-line 1)
  16410. (looking-at org-clock-line-re)))
  16411. (defvar org-clock-history) ; defined in org-clock.el
  16412. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16413. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16414. "Change the date in the time stamp at point.
  16415. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16416. `year', `minute', `second'. If WHAT is not given, the cursor position
  16417. in the timestamp determines what will be changed.
  16418. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16419. (let ((origin (point)) origin-cat
  16420. with-hm inactive
  16421. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16422. org-ts-what
  16423. extra rem
  16424. ts time time0 fixnext clrgx)
  16425. (if (not (org-at-timestamp-p t))
  16426. (user-error "Not at a timestamp"))
  16427. (if (and (not what) (eq org-ts-what 'bracket))
  16428. (org-toggle-timestamp-type)
  16429. ;; Point isn't on brackets. Remember the part of the time-stamp
  16430. ;; the point was in. Indeed, size of time-stamps may change,
  16431. ;; but point must be kept in the same category nonetheless.
  16432. (setq origin-cat org-ts-what)
  16433. (if (and (not what) (not (eq org-ts-what 'day))
  16434. org-display-custom-times
  16435. (get-text-property (point) 'display)
  16436. (not (get-text-property (1- (point)) 'display)))
  16437. (setq org-ts-what 'day))
  16438. (setq org-ts-what (or what org-ts-what)
  16439. inactive (= (char-after (match-beginning 0)) ?\[)
  16440. ts (match-string 0))
  16441. (replace-match "")
  16442. (when (string-match
  16443. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16444. ts)
  16445. (setq extra (match-string 1 ts))
  16446. (if suppress-tmp-delay
  16447. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16448. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16449. (setq with-hm t))
  16450. (setq time0 (org-parse-time-string ts))
  16451. (when (and updown
  16452. (eq org-ts-what 'minute)
  16453. (not current-prefix-arg))
  16454. ;; This looks like s-up and s-down. Change by one rounding step.
  16455. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16456. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16457. (setcar (cdr time0) (+ (nth 1 time0)
  16458. (if (> n 0) (- rem) (- dm rem))))))
  16459. (setq time
  16460. (encode-time (or (car time0) 0)
  16461. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16462. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16463. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16464. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16465. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16466. (nthcdr 6 time0)))
  16467. (when (and (member org-ts-what '(hour minute))
  16468. extra
  16469. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16470. (setq extra (org-modify-ts-extra
  16471. extra
  16472. (if (eq org-ts-what 'hour) 2 5)
  16473. n dm)))
  16474. (when (integerp org-ts-what)
  16475. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16476. (if (eq what 'calendar)
  16477. (let ((cal-date (org-get-date-from-calendar)))
  16478. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16479. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16480. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16481. (setcar time0 (or (car time0) 0))
  16482. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16483. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16484. (setq time (apply 'encode-time time0))))
  16485. ;; Insert the new time-stamp, and ensure point stays in the same
  16486. ;; category as before (i.e. not after the last position in that
  16487. ;; category).
  16488. (let ((pos (point)))
  16489. ;; Stay before inserted string. `save-excursion' is of no use.
  16490. (setq org-last-changed-timestamp
  16491. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16492. (goto-char pos))
  16493. (save-match-data
  16494. (looking-at org-ts-regexp3)
  16495. (goto-char (cond
  16496. ;; `day' category ends before `hour' if any, or at
  16497. ;; the end of the day name.
  16498. ((eq origin-cat 'day)
  16499. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16500. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16501. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16502. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16503. ;; `year' and `month' have both fixed size: point
  16504. ;; couldn't have moved into another part.
  16505. (t origin))))
  16506. ;; Update clock if on a CLOCK line.
  16507. (org-clock-update-time-maybe)
  16508. ;; Maybe adjust the closest clock in `org-clock-history'
  16509. (when org-clock-adjust-closest
  16510. (if (not (and (org-at-clock-log-p)
  16511. (< 1 (length (delq nil (mapcar 'marker-position
  16512. org-clock-history))))))
  16513. (message "No clock to adjust")
  16514. (cond ((save-excursion ; fix previous clock?
  16515. (re-search-backward org-ts-regexp0 nil t)
  16516. (org-looking-back (concat org-clock-string " \\[")))
  16517. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16518. ((save-excursion ; fix next clock?
  16519. (re-search-backward org-ts-regexp0 nil t)
  16520. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16521. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16522. (save-window-excursion
  16523. ;; Find closest clock to point, adjust the previous/next one in history
  16524. (let* ((p (save-excursion (org-back-to-heading t)))
  16525. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16526. (clfixnth
  16527. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16528. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16529. (if (not clfixpos)
  16530. (message "No clock to adjust")
  16531. (save-excursion
  16532. (org-goto-marker-or-bmk clfixpos)
  16533. (org-show-subtree)
  16534. (when (re-search-forward clrgx nil t)
  16535. (goto-char (match-beginning 1))
  16536. (let (org-clock-adjust-closest)
  16537. (org-timestamp-change n org-ts-what updown))
  16538. (message "Clock adjusted in %s for heading: %s"
  16539. (file-name-nondirectory (buffer-file-name))
  16540. (org-get-heading t t)))))))))
  16541. ;; Try to recenter the calendar window, if any.
  16542. (if (and org-calendar-follow-timestamp-change
  16543. (get-buffer-window "*Calendar*" t)
  16544. (memq org-ts-what '(day month year)))
  16545. (org-recenter-calendar (time-to-days time))))))
  16546. (defun org-modify-ts-extra (s pos n dm)
  16547. "Change the different parts of the lead-time and repeat fields in timestamp."
  16548. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16549. ng h m new rem)
  16550. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16551. (cond
  16552. ((or (org-pos-in-match-range pos 2)
  16553. (org-pos-in-match-range pos 3))
  16554. (setq m (string-to-number (match-string 3 s))
  16555. h (string-to-number (match-string 2 s)))
  16556. (if (org-pos-in-match-range pos 2)
  16557. (setq h (+ h n))
  16558. (setq n (* dm (org-no-warnings (signum n))))
  16559. (when (not (= 0 (setq rem (% m dm))))
  16560. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16561. (setq m (+ m n)))
  16562. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16563. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16564. (setq h (min 24 (max 0 h)))
  16565. (setq ng 1 new (format "-%02d:%02d" h m)))
  16566. ((org-pos-in-match-range pos 6)
  16567. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16568. ((org-pos-in-match-range pos 5)
  16569. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16570. ((org-pos-in-match-range pos 9)
  16571. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16572. ((org-pos-in-match-range pos 8)
  16573. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16574. (when ng
  16575. (setq s (concat
  16576. (substring s 0 (match-beginning ng))
  16577. new
  16578. (substring s (match-end ng))))))
  16579. s))
  16580. (defun org-recenter-calendar (date)
  16581. "If the calendar is visible, recenter it to DATE."
  16582. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16583. (when cwin
  16584. (let ((calendar-move-hook nil))
  16585. (with-selected-window cwin
  16586. (calendar-goto-date (if (listp date) date
  16587. (calendar-gregorian-from-absolute date))))))))
  16588. (defun org-goto-calendar (&optional arg)
  16589. "Go to the Emacs calendar at the current date.
  16590. If there is a time stamp in the current line, go to that date.
  16591. A prefix ARG can be used to force the current date."
  16592. (interactive "P")
  16593. (let ((tsr org-ts-regexp) diff
  16594. (calendar-move-hook nil)
  16595. (calendar-view-holidays-initially-flag nil)
  16596. (calendar-view-diary-initially-flag nil))
  16597. (if (or (org-at-timestamp-p)
  16598. (save-excursion
  16599. (beginning-of-line 1)
  16600. (looking-at (concat ".*" tsr))))
  16601. (let ((d1 (time-to-days (current-time)))
  16602. (d2 (time-to-days
  16603. (org-time-string-to-time (match-string 1)))))
  16604. (setq diff (- d2 d1))))
  16605. (calendar)
  16606. (calendar-goto-today)
  16607. (if (and diff (not arg)) (calendar-forward-day diff))))
  16608. (defun org-get-date-from-calendar ()
  16609. "Return a list (month day year) of date at point in calendar."
  16610. (with-current-buffer "*Calendar*"
  16611. (save-match-data
  16612. (calendar-cursor-to-date))))
  16613. (defun org-date-from-calendar ()
  16614. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16615. If there is already a time stamp at the cursor position, update it."
  16616. (interactive)
  16617. (if (org-at-timestamp-p t)
  16618. (org-timestamp-change 0 'calendar)
  16619. (let ((cal-date (org-get-date-from-calendar)))
  16620. (org-insert-time-stamp
  16621. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16622. (defcustom org-effort-durations
  16623. `(("min" . 1)
  16624. ("h" . 60)
  16625. ("d" . ,(* 60 8))
  16626. ("w" . ,(* 60 8 5))
  16627. ("m" . ,(* 60 8 5 4))
  16628. ("y" . ,(* 60 8 5 40)))
  16629. "Conversion factor to minutes for an effort modifier.
  16630. Each entry has the form (MODIFIER . MINUTES).
  16631. In an effort string, a number followed by MODIFIER is multiplied
  16632. by the specified number of MINUTES to obtain an effort in
  16633. minutes.
  16634. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16635. effort string \"2hours\" is equivalent to 120 minutes."
  16636. :group 'org-agenda
  16637. :version "25.1"
  16638. :package-version '(Org . "8.3")
  16639. :type '(alist :key-type (string :tag "Modifier")
  16640. :value-type (number :tag "Minutes")))
  16641. (defun org-minutes-to-clocksum-string (m)
  16642. "Format number of minutes as a clocksum string.
  16643. The format is determined by `org-time-clocksum-format',
  16644. `org-time-clocksum-use-fractional' and
  16645. `org-time-clocksum-fractional-format' and
  16646. `org-time-clocksum-use-effort-durations'."
  16647. (let ((clocksum "")
  16648. (m (round m)) ; Don't allow fractions of minutes
  16649. h d w mo y fmt n)
  16650. (setq h (if org-time-clocksum-use-effort-durations
  16651. (cdr (assoc "h" org-effort-durations)) 60)
  16652. d (if org-time-clocksum-use-effort-durations
  16653. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16654. w (if org-time-clocksum-use-effort-durations
  16655. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16656. mo (if org-time-clocksum-use-effort-durations
  16657. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16658. y (if org-time-clocksum-use-effort-durations
  16659. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16660. ;; fractional format
  16661. (if org-time-clocksum-use-fractional
  16662. (cond
  16663. ;; single format string
  16664. ((stringp org-time-clocksum-fractional-format)
  16665. (format org-time-clocksum-fractional-format (/ m (float h))))
  16666. ;; choice of fractional formats for different time units
  16667. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16668. (> (/ (truncate m) (* y d h)) 0))
  16669. (format fmt (/ m (* y d (float h)))))
  16670. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16671. (> (/ (truncate m) (* mo d h)) 0))
  16672. (format fmt (/ m (* mo d (float h)))))
  16673. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16674. (> (/ (truncate m) (* w d h)) 0))
  16675. (format fmt (/ m (* w d (float h)))))
  16676. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16677. (> (/ (truncate m) (* d h)) 0))
  16678. (format fmt (/ m (* d (float h)))))
  16679. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16680. (> (/ (truncate m) h) 0))
  16681. (format fmt (/ m (float h))))
  16682. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16683. (format fmt m))
  16684. ;; fall back to smallest time unit with a format
  16685. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16686. (format fmt (/ m (float h))))
  16687. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16688. (format fmt (/ m (* d (float h)))))
  16689. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16690. (format fmt (/ m (* w d (float h)))))
  16691. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16692. (format fmt (/ m (* mo d (float h)))))
  16693. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16694. (format fmt (/ m (* y d (float h))))))
  16695. ;; standard (non-fractional) format, with single format string
  16696. (if (stringp org-time-clocksum-format)
  16697. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16698. ;; separate formats components
  16699. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16700. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16701. (plist-get org-time-clocksum-format :require-years))
  16702. (setq clocksum (concat clocksum (format fmt n))
  16703. m (- m (* n y d h))))
  16704. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16705. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16706. (plist-get org-time-clocksum-format :require-months))
  16707. (setq clocksum (concat clocksum (format fmt n))
  16708. m (- m (* n mo d h))))
  16709. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16710. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16711. (plist-get org-time-clocksum-format :require-weeks))
  16712. (setq clocksum (concat clocksum (format fmt n))
  16713. m (- m (* n w d h))))
  16714. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16715. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16716. (plist-get org-time-clocksum-format :require-days))
  16717. (setq clocksum (concat clocksum (format fmt n))
  16718. m (- m (* n d h))))
  16719. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16720. (or (> (setq n (/ (truncate m) h)) 0)
  16721. (plist-get org-time-clocksum-format :require-hours))
  16722. (setq clocksum (concat clocksum (format fmt n))
  16723. m (- m (* n h))))
  16724. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16725. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16726. (setq clocksum (concat clocksum (format fmt m))))
  16727. ;; return formatted time duration
  16728. clocksum))))
  16729. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16730. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16731. "Org mode version 8.0")
  16732. (defun org-hours-to-clocksum-string (n)
  16733. (org-minutes-to-clocksum-string (* n 60)))
  16734. (defun org-hh:mm-string-to-minutes (s)
  16735. "Convert a string H:MM to a number of minutes.
  16736. If the string is just a number, interpret it as minutes.
  16737. In fact, the first hh:mm or number in the string will be taken,
  16738. there can be extra stuff in the string.
  16739. If no number is found, the return value is 0."
  16740. (cond
  16741. ((integerp s) s)
  16742. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16743. (+ (* (string-to-number (match-string 1 s)) 60)
  16744. (string-to-number (match-string 2 s))))
  16745. ((string-match "\\([0-9]+\\)" s)
  16746. (string-to-number (match-string 1 s)))
  16747. (t 0)))
  16748. (defcustom org-image-actual-width t
  16749. "Should we use the actual width of images when inlining them?
  16750. When set to t, always use the image width.
  16751. When set to a number, use imagemagick (when available) to set
  16752. the image's width to this value.
  16753. When set to a number in a list, try to get the width from any
  16754. #+ATTR.* keyword if it matches a width specification like
  16755. #+ATTR_HTML: :width 300px
  16756. and fall back on that number if none is found.
  16757. When set to nil, try to get the width from an #+ATTR.* keyword
  16758. and fall back on the original width if none is found.
  16759. This requires Emacs >= 24.1, build with imagemagick support."
  16760. :group 'org-appearance
  16761. :version "24.4"
  16762. :package-version '(Org . "8.0")
  16763. :type '(choice
  16764. (const :tag "Use the image width" t)
  16765. (integer :tag "Use a number of pixels")
  16766. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16767. (const :tag "Use #+ATTR* or don't resize" nil)))
  16768. (defcustom org-agenda-inhibit-startup nil
  16769. "Inhibit startup when preparing agenda buffers.
  16770. When this variable is t, the initialization of the Org agenda
  16771. buffers is inhibited: e.g. the visibility state is not set, the
  16772. tables are not re-aligned, etc."
  16773. :type 'boolean
  16774. :version "24.3"
  16775. :group 'org-agenda)
  16776. (define-obsolete-variable-alias
  16777. 'org-agenda-ignore-drawer-properties
  16778. 'org-agenda-ignore-properties "25.1")
  16779. (defcustom org-agenda-ignore-properties nil
  16780. "Avoid updating text properties when building the agenda.
  16781. Properties are used to prepare buffers for effort estimates,
  16782. appointments, statistics and subtree-local categories.
  16783. If you don't use these in the agenda, you can add them to this
  16784. list and agenda building will be a bit faster.
  16785. The value is a list, with zero or more of the symbols `effort', `appt',
  16786. `stats' or `category'."
  16787. :type '(set :greedy t
  16788. (const effort)
  16789. (const appt)
  16790. (const stats)
  16791. (const category))
  16792. :version "25.1"
  16793. :package-version '(Org . "8.3")
  16794. :group 'org-agenda)
  16795. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16796. "Convert a duration string S to minutes.
  16797. A bare number is interpreted as minutes, modifiers can be set by
  16798. customizing `org-effort-durations' (which see).
  16799. Entries containing a colon are interpreted as H:MM by
  16800. `org-hh:mm-string-to-minutes'."
  16801. (let ((result 0)
  16802. (re (concat "\\([0-9.]+\\) *\\("
  16803. (regexp-opt (mapcar 'car org-effort-durations))
  16804. "\\)")))
  16805. (while (string-match re s)
  16806. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16807. (string-to-number (match-string 1 s))))
  16808. (setq s (replace-match "" nil t s)))
  16809. (setq result (floor result))
  16810. (incf result (org-hh:mm-string-to-minutes s))
  16811. (if output-to-string (number-to-string result) result)))
  16812. ;;;; Files
  16813. (defun org-save-all-org-buffers ()
  16814. "Save all Org-mode buffers without user confirmation."
  16815. (interactive)
  16816. (message "Saving all Org-mode buffers...")
  16817. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16818. (when (featurep 'org-id) (org-id-locations-save))
  16819. (message "Saving all Org-mode buffers... done"))
  16820. (defun org-revert-all-org-buffers ()
  16821. "Revert all Org-mode buffers.
  16822. Prompt for confirmation when there are unsaved changes.
  16823. Be sure you know what you are doing before letting this function
  16824. overwrite your changes.
  16825. This function is useful in a setup where one tracks org files
  16826. with a version control system, to revert on one machine after pulling
  16827. changes from another. I believe the procedure must be like this:
  16828. 1. M-x org-save-all-org-buffers
  16829. 2. Pull changes from the other machine, resolve conflicts
  16830. 3. M-x org-revert-all-org-buffers"
  16831. (interactive)
  16832. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16833. (user-error "Abort"))
  16834. (save-excursion
  16835. (save-window-excursion
  16836. (mapc
  16837. (lambda (b)
  16838. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16839. (with-current-buffer b buffer-file-name))
  16840. (org-pop-to-buffer-same-window b)
  16841. (revert-buffer t 'no-confirm)))
  16842. (buffer-list))
  16843. (when (and (featurep 'org-id) org-id-track-globally)
  16844. (org-id-locations-load)))))
  16845. ;;;; Agenda files
  16846. ;;;###autoload
  16847. (defun org-switchb (&optional arg)
  16848. "Switch between Org buffers.
  16849. With one prefix argument, restrict available buffers to files.
  16850. With two prefix arguments, restrict available buffers to agenda files.
  16851. Defaults to `iswitchb' for buffer name completion.
  16852. Set `org-completion-use-ido' to make it use ido instead."
  16853. (interactive "P")
  16854. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16855. ((equal arg '(16)) (org-buffer-list 'agenda))
  16856. (t (org-buffer-list))))
  16857. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16858. (org-completion-use-ido org-completion-use-ido))
  16859. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16860. (setq org-completion-use-iswitchb t))
  16861. (org-pop-to-buffer-same-window
  16862. (org-icompleting-read "Org buffer: "
  16863. (mapcar 'list (mapcar 'buffer-name blist))
  16864. nil t))))
  16865. ;;; Define some older names previously used for this functionality
  16866. ;;;###autoload
  16867. (defalias 'org-ido-switchb 'org-switchb)
  16868. ;;;###autoload
  16869. (defalias 'org-iswitchb 'org-switchb)
  16870. (defun org-buffer-list (&optional predicate exclude-tmp)
  16871. "Return a list of Org buffers.
  16872. PREDICATE can be `export', `files' or `agenda'.
  16873. export restrict the list to Export buffers.
  16874. files restrict the list to buffers visiting Org files.
  16875. agenda restrict the list to buffers visiting agenda files.
  16876. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16877. (let* ((bfn nil)
  16878. (agenda-files (and (eq predicate 'agenda)
  16879. (mapcar 'file-truename (org-agenda-files t))))
  16880. (filter
  16881. (cond
  16882. ((eq predicate 'files)
  16883. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16884. ((eq predicate 'export)
  16885. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16886. ((eq predicate 'agenda)
  16887. (lambda (b)
  16888. (with-current-buffer b
  16889. (and (derived-mode-p 'org-mode)
  16890. (setq bfn (buffer-file-name b))
  16891. (member (file-truename bfn) agenda-files)))))
  16892. (t (lambda (b) (with-current-buffer b
  16893. (or (derived-mode-p 'org-mode)
  16894. (string-match "\*Org .*Export"
  16895. (buffer-name b)))))))))
  16896. (delq nil
  16897. (mapcar
  16898. (lambda(b)
  16899. (if (and (funcall filter b)
  16900. (or (not exclude-tmp)
  16901. (not (string-match "tmp" (buffer-name b)))))
  16902. b
  16903. nil))
  16904. (buffer-list)))))
  16905. (defun org-agenda-files (&optional unrestricted archives)
  16906. "Get the list of agenda files.
  16907. Optional UNRESTRICTED means return the full list even if a restriction
  16908. is currently in place.
  16909. When ARCHIVES is t, include all archive files that are really being
  16910. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16911. `org-agenda-archives-mode' is t."
  16912. (let ((files
  16913. (cond
  16914. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16915. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16916. ((listp org-agenda-files) org-agenda-files)
  16917. (t (error "Invalid value of `org-agenda-files'")))))
  16918. (setq files (apply 'append
  16919. (mapcar (lambda (f)
  16920. (if (file-directory-p f)
  16921. (directory-files
  16922. f t org-agenda-file-regexp)
  16923. (list f)))
  16924. files)))
  16925. (when org-agenda-skip-unavailable-files
  16926. (setq files (delq nil
  16927. (mapcar (function
  16928. (lambda (file)
  16929. (and (file-readable-p file) file)))
  16930. files))))
  16931. (when (or (eq archives t)
  16932. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16933. (setq files (org-add-archive-files files)))
  16934. files))
  16935. (defun org-agenda-file-p (&optional file)
  16936. "Return non-nil, if FILE is an agenda file.
  16937. If FILE is omitted, use the file associated with the current
  16938. buffer."
  16939. (let ((fname (or file (buffer-file-name))))
  16940. (and fname
  16941. (member (file-truename fname)
  16942. (mapcar #'file-truename (org-agenda-files t))))))
  16943. (defun org-edit-agenda-file-list ()
  16944. "Edit the list of agenda files.
  16945. Depending on setup, this either uses customize to edit the variable
  16946. `org-agenda-files', or it visits the file that is holding the list. In the
  16947. latter case, the buffer is set up in a way that saving it automatically kills
  16948. the buffer and restores the previous window configuration."
  16949. (interactive)
  16950. (if (stringp org-agenda-files)
  16951. (let ((cw (current-window-configuration)))
  16952. (find-file org-agenda-files)
  16953. (org-set-local 'org-window-configuration cw)
  16954. (org-add-hook 'after-save-hook
  16955. (lambda ()
  16956. (set-window-configuration
  16957. (prog1 org-window-configuration
  16958. (kill-buffer (current-buffer))))
  16959. (org-install-agenda-files-menu)
  16960. (message "New agenda file list installed"))
  16961. nil 'local)
  16962. (message "%s" (substitute-command-keys
  16963. "Edit list and finish with \\[save-buffer]")))
  16964. (customize-variable 'org-agenda-files)))
  16965. (defun org-store-new-agenda-file-list (list)
  16966. "Set new value for the agenda file list and save it correctly."
  16967. (if (stringp org-agenda-files)
  16968. (let ((fe (org-read-agenda-file-list t)) b u)
  16969. (while (setq b (find-buffer-visiting org-agenda-files))
  16970. (kill-buffer b))
  16971. (with-temp-file org-agenda-files
  16972. (insert
  16973. (mapconcat
  16974. (lambda (f) ;; Keep un-expanded entries.
  16975. (if (setq u (assoc f fe))
  16976. (cdr u)
  16977. f))
  16978. list "\n")
  16979. "\n")))
  16980. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16981. (org-insert-mode-line-in-empty-file nil))
  16982. (setq org-agenda-files list)
  16983. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16984. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16985. "Read the list of agenda files from a file.
  16986. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16987. filenames, used by `org-store-new-agenda-file-list' to write back
  16988. un-expanded file names."
  16989. (when (file-directory-p org-agenda-files)
  16990. (error "`org-agenda-files' cannot be a single directory"))
  16991. (when (stringp org-agenda-files)
  16992. (with-temp-buffer
  16993. (insert-file-contents org-agenda-files)
  16994. (mapcar
  16995. (lambda (f)
  16996. (let ((e (expand-file-name (substitute-in-file-name f)
  16997. org-directory)))
  16998. (if pair-with-expansion
  16999. (cons e f)
  17000. e)))
  17001. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17002. ;;;###autoload
  17003. (defun org-cycle-agenda-files ()
  17004. "Cycle through the files in `org-agenda-files'.
  17005. If the current buffer visits an agenda file, find the next one in the list.
  17006. If the current buffer does not, find the first agenda file."
  17007. (interactive)
  17008. (let* ((fs (or (org-agenda-files t)
  17009. (user-error "No agenda files")))
  17010. (files (copy-sequence fs))
  17011. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17012. file)
  17013. (when tcf
  17014. (while (and (setq file (pop files))
  17015. (not (equal (file-truename file) tcf)))))
  17016. (find-file (car (or files fs)))
  17017. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17018. (defun org-agenda-file-to-front (&optional to-end)
  17019. "Move/add the current file to the top of the agenda file list.
  17020. If the file is not present in the list, it is added to the front. If it is
  17021. present, it is moved there. With optional argument TO-END, add/move to the
  17022. end of the list."
  17023. (interactive "P")
  17024. (let ((org-agenda-skip-unavailable-files nil)
  17025. (file-alist (mapcar (lambda (x)
  17026. (cons (file-truename x) x))
  17027. (org-agenda-files t)))
  17028. (ctf (file-truename
  17029. (or buffer-file-name
  17030. (user-error "Please save the current buffer to a file"))))
  17031. x had)
  17032. (setq x (assoc ctf file-alist) had x)
  17033. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17034. (if to-end
  17035. (setq file-alist (append (delq x file-alist) (list x)))
  17036. (setq file-alist (cons x (delq x file-alist))))
  17037. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17038. (org-install-agenda-files-menu)
  17039. (message "File %s to %s of agenda file list"
  17040. (if had "moved" "added") (if to-end "end" "front"))))
  17041. (defun org-remove-file (&optional file)
  17042. "Remove current file from the list of files in variable `org-agenda-files'.
  17043. These are the files which are being checked for agenda entries.
  17044. Optional argument FILE means use this file instead of the current."
  17045. (interactive)
  17046. (let* ((org-agenda-skip-unavailable-files nil)
  17047. (file (or file buffer-file-name
  17048. (user-error "Current buffer does not visit a file")))
  17049. (true-file (file-truename file))
  17050. (afile (abbreviate-file-name file))
  17051. (files (delq nil (mapcar
  17052. (lambda (x)
  17053. (if (equal true-file
  17054. (file-truename x))
  17055. nil x))
  17056. (org-agenda-files t)))))
  17057. (if (not (= (length files) (length (org-agenda-files t))))
  17058. (progn
  17059. (org-store-new-agenda-file-list files)
  17060. (org-install-agenda-files-menu)
  17061. (message "Removed from Org Agenda list: %s" afile))
  17062. (message "File was not in list: %s (not removed)" afile))))
  17063. (defun org-file-menu-entry (file)
  17064. (vector file (list 'find-file file) t))
  17065. (defun org-check-agenda-file (file)
  17066. "Make sure FILE exists. If not, ask user what to do."
  17067. (when (not (file-exists-p file))
  17068. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17069. (abbreviate-file-name file))
  17070. (let ((r (downcase (read-char-exclusive))))
  17071. (cond
  17072. ((equal r ?r)
  17073. (org-remove-file file)
  17074. (throw 'nextfile t))
  17075. (t (user-error "Abort"))))))
  17076. (defun org-get-agenda-file-buffer (file)
  17077. "Get an agenda buffer visiting FILE.
  17078. If the buffer needs to be created, add it to the list of buffers
  17079. which might be released later."
  17080. (let ((buf (org-find-base-buffer-visiting file)))
  17081. (if buf
  17082. buf ; just return it
  17083. ;; Make a new buffer and remember it
  17084. (setq buf (find-file-noselect file))
  17085. (if buf (push buf org-agenda-new-buffers))
  17086. buf)))
  17087. (defun org-release-buffers (blist)
  17088. "Release all buffers in list, asking the user for confirmation when needed.
  17089. When a buffer is unmodified, it is just killed. When modified, it is saved
  17090. \(if the user agrees) and then killed."
  17091. (let (file)
  17092. (dolist (buf blist)
  17093. (setq file (buffer-file-name buf))
  17094. (when (and (buffer-modified-p buf)
  17095. file
  17096. (y-or-n-p (format "Save file %s? " file)))
  17097. (with-current-buffer buf (save-buffer)))
  17098. (kill-buffer buf))))
  17099. (defun org-agenda-prepare-buffers (files)
  17100. "Create buffers for all agenda files, protect archived trees and comments."
  17101. (interactive)
  17102. (let ((pa '(:org-archived t))
  17103. (pc '(:org-comment t))
  17104. (pall '(:org-archived t :org-comment t))
  17105. (inhibit-read-only t)
  17106. (org-inhibit-startup org-agenda-inhibit-startup)
  17107. (rea (concat ":" org-archive-tag ":"))
  17108. file re pos)
  17109. (setq org-tag-alist-for-agenda nil
  17110. org-tag-groups-alist-for-agenda nil)
  17111. (save-excursion
  17112. (save-restriction
  17113. (dolist (file files)
  17114. (catch 'nextfile
  17115. (if (bufferp file)
  17116. (set-buffer file)
  17117. (org-check-agenda-file file)
  17118. (set-buffer (org-get-agenda-file-buffer file)))
  17119. (widen)
  17120. (org-set-regexps-and-options 'tags-only)
  17121. (setq pos (point))
  17122. (or (memq 'category org-agenda-ignore-properties)
  17123. (org-refresh-category-properties))
  17124. (or (memq 'stats org-agenda-ignore-properties)
  17125. (org-refresh-stats-properties))
  17126. (or (memq 'effort org-agenda-ignore-properties)
  17127. (org-refresh-effort-properties))
  17128. (or (memq 'appt org-agenda-ignore-properties)
  17129. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17130. (setq org-todo-keywords-for-agenda
  17131. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17132. (setq org-done-keywords-for-agenda
  17133. (append org-done-keywords-for-agenda org-done-keywords))
  17134. (setq org-todo-keyword-alist-for-agenda
  17135. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17136. (setq org-tag-alist-for-agenda
  17137. (org-uniquify
  17138. (append org-tag-alist-for-agenda
  17139. org-tag-alist
  17140. org-tag-persistent-alist)))
  17141. (if org-group-tags
  17142. (setq org-tag-groups-alist-for-agenda
  17143. (org-uniquify-alist
  17144. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17145. (org-with-silent-modifications
  17146. (save-excursion
  17147. (remove-text-properties (point-min) (point-max) pall)
  17148. (when org-agenda-skip-archived-trees
  17149. (goto-char (point-min))
  17150. (while (re-search-forward rea nil t)
  17151. (if (org-at-heading-p t)
  17152. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17153. (goto-char (point-min))
  17154. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17155. (while (re-search-forward re nil t)
  17156. (when (save-match-data (org-in-commented-heading-p t))
  17157. (add-text-properties
  17158. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17159. (goto-char pos)))))
  17160. (setq org-todo-keywords-for-agenda
  17161. (org-uniquify org-todo-keywords-for-agenda))
  17162. (setq org-todo-keyword-alist-for-agenda
  17163. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17164. ;;;; CDLaTeX minor mode
  17165. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17166. "Keymap for the minor `org-cdlatex-mode'.")
  17167. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17168. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17169. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17170. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17171. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17172. (defvar org-cdlatex-texmathp-advice-is-done nil
  17173. "Flag remembering if we have applied the advice to texmathp already.")
  17174. (define-minor-mode org-cdlatex-mode
  17175. "Toggle the minor `org-cdlatex-mode'.
  17176. This mode supports entering LaTeX environment and math in LaTeX fragments
  17177. in Org-mode.
  17178. \\{org-cdlatex-mode-map}"
  17179. nil " OCDL" nil
  17180. (when org-cdlatex-mode
  17181. (require 'cdlatex)
  17182. (run-hooks 'cdlatex-mode-hook)
  17183. (cdlatex-compute-tables))
  17184. (unless org-cdlatex-texmathp-advice-is-done
  17185. (setq org-cdlatex-texmathp-advice-is-done t)
  17186. (defadvice texmathp (around org-math-always-on activate)
  17187. "Always return t in org-mode buffers.
  17188. This is because we want to insert math symbols without dollars even outside
  17189. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17190. an embedded LaTeX fragment, let texmathp do its job.
  17191. \\[org-cdlatex-mode-map]"
  17192. (interactive)
  17193. (let (p)
  17194. (cond
  17195. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17196. ((eq this-command 'cdlatex-math-symbol)
  17197. (setq ad-return-value t
  17198. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17199. (t
  17200. (let ((p (org-inside-LaTeX-fragment-p)))
  17201. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17202. (setq ad-return-value t
  17203. texmathp-why '("Org-mode embedded math" . 0))
  17204. (if p ad-do-it)))))))))
  17205. (defun turn-on-org-cdlatex ()
  17206. "Unconditionally turn on `org-cdlatex-mode'."
  17207. (org-cdlatex-mode 1))
  17208. (defun org-try-cdlatex-tab ()
  17209. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17210. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17211. - inside a LaTeX fragment, or
  17212. - after the first word in a line, where an abbreviation expansion could
  17213. insert a LaTeX environment."
  17214. (when org-cdlatex-mode
  17215. (cond
  17216. ;; Before any word on the line: No expansion possible.
  17217. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17218. ;; Just after first word on the line: Expand it. Make sure it
  17219. ;; cannot happen on headlines, though.
  17220. ((save-excursion
  17221. (skip-chars-backward "a-zA-Z0-9*")
  17222. (skip-chars-backward " \t")
  17223. (and (bolp) (not (org-at-heading-p))))
  17224. (cdlatex-tab) t)
  17225. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17226. (defun org-cdlatex-underscore-caret (&optional arg)
  17227. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17228. Revert to the normal definition outside of these fragments."
  17229. (interactive "P")
  17230. (if (org-inside-LaTeX-fragment-p)
  17231. (call-interactively 'cdlatex-sub-superscript)
  17232. (let (org-cdlatex-mode)
  17233. (call-interactively (key-binding (vector last-input-event))))))
  17234. (defun org-cdlatex-math-modify (&optional arg)
  17235. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17236. Revert to the normal definition outside of these fragments."
  17237. (interactive "P")
  17238. (if (org-inside-LaTeX-fragment-p)
  17239. (call-interactively 'cdlatex-math-modify)
  17240. (let (org-cdlatex-mode)
  17241. (call-interactively (key-binding (vector last-input-event))))))
  17242. (defun org-cdlatex-environment-indent (&optional environment item)
  17243. "Execute `cdlatex-environment' and indent the inserted environment.
  17244. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17245. The inserted environment is indented to current indentation
  17246. unless point is at the beginning of the line, in which the
  17247. environment remains unintended."
  17248. (interactive)
  17249. ;; cdlatex-environment always return nil. Therefore, capture output
  17250. ;; first and determine if an environment was selected.
  17251. (let* ((beg (point-marker))
  17252. (end (copy-marker (point) t))
  17253. (inserted (progn
  17254. (ignore-errors (cdlatex-environment environment item))
  17255. (< beg end)))
  17256. ;; Figure out how many lines to move forward after the
  17257. ;; environment has been inserted.
  17258. (lines (when inserted
  17259. (save-excursion
  17260. (- (loop while (< beg (point))
  17261. with x = 0
  17262. do (forward-line -1)
  17263. (incf x)
  17264. finally return x)
  17265. (if (progn (goto-char beg)
  17266. (and (progn (skip-chars-forward " \t") (eolp))
  17267. (progn (skip-chars-backward " \t") (bolp))))
  17268. 1 0)))))
  17269. (env (org-trim (delete-and-extract-region beg end))))
  17270. (when inserted
  17271. ;; Get indentation of next line unless at column 0.
  17272. (let ((ind (if (bolp) 0
  17273. (save-excursion
  17274. (org-return-indent)
  17275. (prog1 (org-get-indentation)
  17276. (when (progn (skip-chars-forward " \t") (eolp))
  17277. (delete-region beg (point)))))))
  17278. (bol (progn (skip-chars-backward " \t") (bolp))))
  17279. ;; Insert a newline before environment unless at column zero
  17280. ;; to "escape" the current line. Insert a newline if
  17281. ;; something is one the same line as \end{ENVIRONMENT}.
  17282. (insert
  17283. (concat (unless bol "\n") env
  17284. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17285. (unless (zerop ind)
  17286. (save-excursion
  17287. (goto-char beg)
  17288. (while (< (point) end)
  17289. (unless (eolp) (org-indent-line-to ind))
  17290. (forward-line))))
  17291. (goto-char beg)
  17292. (forward-line lines)
  17293. (org-indent-line-to ind)))
  17294. (set-marker beg nil)
  17295. (set-marker end nil)))
  17296. ;;;; LaTeX fragments
  17297. (defun org-inside-LaTeX-fragment-p ()
  17298. "Test if point is inside a LaTeX fragment.
  17299. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17300. sequence appearing also before point.
  17301. Even though the matchers for math are configurable, this function assumes
  17302. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17303. delimiters are skipped when they have been removed by customization.
  17304. The return value is nil, or a cons cell with the delimiter and the
  17305. position of this delimiter.
  17306. This function does a reasonably good job, but can locally be fooled by
  17307. for example currency specifications. For example it will assume being in
  17308. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17309. fragments that are properly closed, but during editing, we have to live
  17310. with the uncertainty caused by missing closing delimiters. This function
  17311. looks only before point, not after."
  17312. (catch 'exit
  17313. (let ((pos (point))
  17314. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17315. (lim (progn
  17316. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17317. (point)))
  17318. dd-on str (start 0) m re)
  17319. (goto-char pos)
  17320. (when dodollar
  17321. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17322. re (nth 1 (assoc "$" org-latex-regexps)))
  17323. (while (string-match re str start)
  17324. (cond
  17325. ((= (match-end 0) (length str))
  17326. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17327. ((= (match-end 0) (- (length str) 5))
  17328. (throw 'exit nil))
  17329. (t (setq start (match-end 0))))))
  17330. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17331. (goto-char pos)
  17332. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17333. (and (match-beginning 2) (throw 'exit nil))
  17334. ;; count $$
  17335. (while (re-search-backward "\\$\\$" lim t)
  17336. (setq dd-on (not dd-on)))
  17337. (goto-char pos)
  17338. (if dd-on (cons "$$" m))))))
  17339. (defun org-inside-latex-macro-p ()
  17340. "Is point inside a LaTeX macro or its arguments?"
  17341. (save-match-data
  17342. (org-in-regexp
  17343. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17344. (defvar org-latex-fragment-image-overlays nil
  17345. "List of overlays carrying the images of latex fragments.")
  17346. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17347. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17348. "Remove all overlays with LaTeX fragment images in current buffer.
  17349. When optional arguments BEG and END are non-nil, remove all
  17350. overlays between them instead. Return t when some overlays were
  17351. removed, nil otherwise."
  17352. (let (removedp)
  17353. (setq org-latex-fragment-image-overlays
  17354. (let ((beg (or beg (point-min)))
  17355. (end (or end (point-max))))
  17356. (org-remove-if
  17357. (lambda (o)
  17358. (and (>= (overlay-start o) beg)
  17359. (<= (overlay-end o) end)
  17360. (progn (delete-overlay o)
  17361. (or removedp (setq removedp t)))))
  17362. org-latex-fragment-image-overlays)))
  17363. removedp))
  17364. (define-obsolete-function-alias
  17365. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17366. (defun org-toggle-latex-fragment (&optional arg)
  17367. "Preview the LaTeX fragment at point, or all locally or globally.
  17368. If the cursor is on a LaTeX fragment, create the image and overlay
  17369. it over the source code, if there is none. Remove it otherwise.
  17370. If there is no fragment at point, display all fragments in the
  17371. current section.
  17372. With prefix ARG, preview or clear image for all fragments in the
  17373. current subtree or in the whole buffer when used before the first
  17374. headline. With a double prefix ARG \\[universal-argument] \
  17375. \\[universal-argument] preview or clear images
  17376. for all fragments in the buffer."
  17377. (interactive "P")
  17378. (unless (buffer-file-name (buffer-base-buffer))
  17379. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17380. (when (display-graphic-p)
  17381. (catch 'exit
  17382. (save-excursion
  17383. (let ((window-start (window-start)) msg)
  17384. (save-restriction
  17385. (cond
  17386. ((or (equal arg '(16))
  17387. (and (equal arg '(4))
  17388. (org-with-limited-levels (org-before-first-heading-p))))
  17389. (if (org-remove-latex-fragment-image-overlays)
  17390. (progn (message "LaTeX fragments images removed from buffer")
  17391. (throw 'exit nil))
  17392. (setq msg "Creating images for buffer...")))
  17393. ((equal arg '(4))
  17394. (org-with-limited-levels (org-back-to-heading t))
  17395. (let ((beg (point))
  17396. (end (progn (org-end-of-subtree t) (point))))
  17397. (if (org-remove-latex-fragment-image-overlays beg end)
  17398. (progn
  17399. (message "LaTeX fragment images removed from subtree")
  17400. (throw 'exit nil))
  17401. (setq msg "Creating images for subtree...")
  17402. (narrow-to-region beg end))))
  17403. ((let ((datum (org-element-context)))
  17404. (when (memq (org-element-type datum)
  17405. '(latex-environment latex-fragment))
  17406. (let* ((beg (org-element-property :begin datum))
  17407. (end (org-element-property :end datum)))
  17408. (if (org-remove-latex-fragment-image-overlays beg end)
  17409. (progn (message "LaTeX fragment image removed")
  17410. (throw 'exit nil))
  17411. (narrow-to-region beg end)
  17412. (setq msg "Creating image..."))))))
  17413. (t
  17414. (org-with-limited-levels
  17415. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17416. (outline-previous-heading)
  17417. (point)))
  17418. (end (progn (outline-next-heading) (point))))
  17419. (if (org-remove-latex-fragment-image-overlays beg end)
  17420. (progn
  17421. (message "LaTeX fragment images removed from section")
  17422. (throw 'exit nil))
  17423. (setq msg "Creating images for section...")
  17424. (narrow-to-region beg end))))))
  17425. (let ((file (buffer-file-name (buffer-base-buffer))))
  17426. (org-format-latex
  17427. (concat org-latex-preview-ltxpng-directory
  17428. (file-name-sans-extension (file-name-nondirectory file)))
  17429. ;; Emacs cannot overlay images from remote hosts.
  17430. ;; Create it in `temporary-file-directory' instead.
  17431. (if (file-remote-p file) temporary-file-directory
  17432. default-directory)
  17433. 'overlays msg 'forbuffer
  17434. org-latex-create-formula-image-program)))
  17435. ;; Work around a bug that doesn't restore window's start
  17436. ;; when widening back the buffer.
  17437. (set-window-start nil window-start)
  17438. (message (concat msg "done")))))))
  17439. (defun org-format-latex
  17440. (prefix &optional dir overlays msg forbuffer processing-type)
  17441. "Replace LaTeX fragments with links to an image, and produce images.
  17442. When optional argument OVERLAYS is non-nil, display the image on
  17443. top of the fragment instead of replacing it.
  17444. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17445. Some of the options can be changed using the variable
  17446. `org-format-latex-options', which see."
  17447. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17448. (unless (eq processing-type 'verbatim)
  17449. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17450. (cnt 0)
  17451. checkdir-flag)
  17452. (goto-char (point-min))
  17453. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17454. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17455. (overlay-recenter (point-max)))
  17456. (while (re-search-forward math-regexp nil t)
  17457. (unless (and overlays
  17458. (eq (get-char-property (point) 'org-overlay-type)
  17459. 'org-latex-overlay))
  17460. (let* ((context (org-element-context))
  17461. (type (org-element-type context)))
  17462. (when (memq type '(latex-environment latex-fragment))
  17463. (let ((block-type (eq type 'latex-environment))
  17464. (value (org-element-property :value context))
  17465. (beg (org-element-property :begin context))
  17466. (end (save-excursion
  17467. (goto-char (org-element-property :end context))
  17468. (skip-chars-backward " \r\t\n")
  17469. (point))))
  17470. (case processing-type
  17471. (mathjax
  17472. ;; Prepare for MathJax processing.
  17473. (if (eq (char-after beg) ?$)
  17474. (save-excursion
  17475. (delete-region beg end)
  17476. (insert "\\(" (substring value 1 -1) "\\)"))
  17477. (goto-char end)))
  17478. ((dvipng imagemagick)
  17479. ;; Process to an image.
  17480. (incf cnt)
  17481. (goto-char beg)
  17482. (let* ((face (face-at-point))
  17483. ;; Get the colors from the face at point.
  17484. (fg
  17485. (let ((color (plist-get org-format-latex-options
  17486. :foreground)))
  17487. (if (and forbuffer (eq color 'auto))
  17488. (face-attribute face :foreground nil 'default)
  17489. color)))
  17490. (bg
  17491. (let ((color (plist-get org-format-latex-options
  17492. :background)))
  17493. (if (and forbuffer (eq color 'auto))
  17494. (face-attribute face :background nil 'default)
  17495. color)))
  17496. (hash (sha1 (prin1-to-string
  17497. (list org-format-latex-header
  17498. org-latex-default-packages-alist
  17499. org-latex-packages-alist
  17500. org-format-latex-options
  17501. forbuffer value fg bg))))
  17502. (absprefix (expand-file-name prefix dir))
  17503. (linkfile (format "%s_%s.png" prefix hash))
  17504. (movefile (format "%s_%s.png" absprefix hash))
  17505. (sep (and block-type "\n\n"))
  17506. (link (concat sep "[[file:" linkfile "]]" sep))
  17507. (options
  17508. (org-combine-plists
  17509. org-format-latex-options
  17510. `(:foreground ,fg :background ,bg))))
  17511. (when msg (message msg cnt))
  17512. (unless checkdir-flag ; Ensure the directory exists.
  17513. (setq checkdir-flag t)
  17514. (let ((todir (file-name-directory absprefix)))
  17515. (unless (file-directory-p todir)
  17516. (make-directory todir t))))
  17517. (unless (file-exists-p movefile)
  17518. (org-create-formula-image
  17519. value movefile options forbuffer processing-type))
  17520. (if overlays
  17521. (progn
  17522. (dolist (o (overlays-in beg end))
  17523. (when (eq (overlay-get o 'org-overlay-type)
  17524. 'org-latex-overlay)
  17525. (delete-overlay o)))
  17526. (let ((ov (make-overlay beg end)))
  17527. (overlay-put ov
  17528. 'org-overlay-type
  17529. 'org-latex-overlay)
  17530. (overlay-put ov 'evaporate t)
  17531. (if (featurep 'xemacs)
  17532. (progn
  17533. (overlay-put ov 'invisible t)
  17534. (overlay-put
  17535. ov 'end-glyph
  17536. (make-glyph
  17537. (vector 'png :file movefile))))
  17538. (overlay-put
  17539. ov 'display
  17540. (list 'image
  17541. :type 'png
  17542. :file movefile
  17543. :ascent 'center)))
  17544. (push ov org-latex-fragment-image-overlays))
  17545. (goto-char end))
  17546. (delete-region beg end)
  17547. (insert
  17548. (org-add-props link
  17549. (list 'org-latex-src
  17550. (replace-regexp-in-string "\"" "" value)
  17551. 'org-latex-src-embed-type
  17552. (if block-type 'paragraph 'character)))))))
  17553. (mathml
  17554. ;; Process to MathML.
  17555. (unless (org-format-latex-mathml-available-p)
  17556. (user-error "LaTeX to MathML converter not configured"))
  17557. (incf cnt)
  17558. (when msg (message msg cnt))
  17559. (goto-char beg)
  17560. (delete-region beg end)
  17561. (insert (org-format-latex-as-mathml
  17562. value block-type prefix dir)))
  17563. (otherwise
  17564. (error "Unknown conversion type %s for LaTeX fragments"
  17565. processing-type)))))))))))
  17566. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17567. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17568. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17569. sucessful, return the portion between \"<math...> </math>\"
  17570. elements otherwise return nil. When MATHML-FILE is specified,
  17571. write the results in to that file. When invoked as an
  17572. interactive command, prompt for LATEX-FRAG, with initial value
  17573. set to the current active region and echo the results for user
  17574. inspection."
  17575. (interactive (list (let ((frag (when (org-region-active-p)
  17576. (buffer-substring-no-properties
  17577. (region-beginning) (region-end)))))
  17578. (read-string "LaTeX Fragment: " frag nil frag))))
  17579. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17580. (let* ((tmp-in-file (file-relative-name
  17581. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17582. (ignore (write-region latex-frag nil tmp-in-file))
  17583. (tmp-out-file (file-relative-name
  17584. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17585. (cmd (format-spec
  17586. org-latex-to-mathml-convert-command
  17587. `((?j . ,(and org-latex-to-mathml-jar-file
  17588. (shell-quote-argument
  17589. (expand-file-name
  17590. org-latex-to-mathml-jar-file))))
  17591. (?I . ,(shell-quote-argument tmp-in-file))
  17592. (?i . ,latex-frag)
  17593. (?o . ,(shell-quote-argument tmp-out-file)))))
  17594. mathml shell-command-output)
  17595. (when (org-called-interactively-p 'any)
  17596. (unless (org-format-latex-mathml-available-p)
  17597. (user-error "LaTeX to MathML converter not configured")))
  17598. (message "Running %s" cmd)
  17599. (setq shell-command-output (shell-command-to-string cmd))
  17600. (setq mathml
  17601. (when (file-readable-p tmp-out-file)
  17602. (with-current-buffer (find-file-noselect tmp-out-file t)
  17603. (goto-char (point-min))
  17604. (when (re-search-forward
  17605. (concat
  17606. (regexp-quote
  17607. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17608. "[^>]*?>"
  17609. "\\(.\\|\n\\)*"
  17610. "</math>")
  17611. nil t)
  17612. (prog1 (match-string 0) (kill-buffer))))))
  17613. (cond
  17614. (mathml
  17615. (setq mathml
  17616. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17617. (when mathml-file
  17618. (write-region mathml nil mathml-file))
  17619. (when (org-called-interactively-p 'any)
  17620. (message mathml)))
  17621. ((message "LaTeX to MathML conversion failed")
  17622. (message shell-command-output)))
  17623. (delete-file tmp-in-file)
  17624. (when (file-exists-p tmp-out-file)
  17625. (delete-file tmp-out-file))
  17626. mathml))
  17627. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17628. prefix &optional dir)
  17629. "Use `org-create-math-formula' but check local cache first."
  17630. (let* ((absprefix (expand-file-name prefix dir))
  17631. (print-length nil) (print-level nil)
  17632. (formula-id (concat
  17633. "formula-"
  17634. (sha1
  17635. (prin1-to-string
  17636. (list latex-frag
  17637. org-latex-to-mathml-convert-command)))))
  17638. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17639. (formula-cache-dir (file-name-directory formula-cache)))
  17640. (unless (file-directory-p formula-cache-dir)
  17641. (make-directory formula-cache-dir t))
  17642. (unless (file-exists-p formula-cache)
  17643. (org-create-math-formula latex-frag formula-cache))
  17644. (if (file-exists-p formula-cache)
  17645. ;; Successful conversion. Return the link to MathML file.
  17646. (org-add-props
  17647. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17648. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17649. 'org-latex-src-embed-type (if latex-frag-type
  17650. 'paragraph 'character)))
  17651. ;; Failed conversion. Return the LaTeX fragment verbatim
  17652. latex-frag)))
  17653. (defun org-create-formula-image (string tofile options buffer &optional type)
  17654. "Create an image from LaTeX source using dvipng or convert.
  17655. This function calls either `org-create-formula-image-with-dvipng'
  17656. or `org-create-formula-image-with-imagemagick' depending on the
  17657. value of `org-latex-create-formula-image-program' or on the value
  17658. of the optional TYPE variable.
  17659. Note: ultimately these two function should be combined as they
  17660. share a good deal of logic."
  17661. (org-check-external-command
  17662. "latex" "needed to convert LaTeX fragments to images")
  17663. (funcall
  17664. (case (or type org-latex-create-formula-image-program)
  17665. (dvipng
  17666. (org-check-external-command
  17667. "dvipng" "needed to convert LaTeX fragments to images")
  17668. #'org-create-formula-image-with-dvipng)
  17669. (imagemagick
  17670. (org-check-external-command
  17671. "convert" "you need to install imagemagick")
  17672. #'org-create-formula-image-with-imagemagick)
  17673. (t (error
  17674. "Invalid value of `org-latex-create-formula-image-program'")))
  17675. string tofile options buffer))
  17676. (declare-function org-export-get-backend "ox" (name))
  17677. (declare-function org-export--get-global-options "ox" (&optional backend))
  17678. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17679. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17680. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17681. (defun org-create-formula--latex-header ()
  17682. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17683. (let ((info (org-combine-plists (org-export--get-global-options
  17684. (org-export-get-backend 'latex))
  17685. (org-export--get-inbuffer-options
  17686. (org-export-get-backend 'latex)))))
  17687. (org-latex-guess-babel-language
  17688. (org-latex-guess-inputenc
  17689. (org-splice-latex-header
  17690. org-format-latex-header
  17691. org-latex-default-packages-alist
  17692. org-latex-packages-alist t
  17693. (plist-get info :latex-header)))
  17694. info)))
  17695. (defun org--get-display-dpi ()
  17696. "Get the DPI of the display.
  17697. Assumes that the display has the same pixel width in the
  17698. horizontal and vertical directions."
  17699. (if (display-graphic-p)
  17700. (round (/ (display-pixel-height)
  17701. (/ (display-mm-height) 25.4)))
  17702. (error "Attempt to calculate the dpi of a non-graphic display")))
  17703. ;; This function borrows from Ganesh Swami's latex2png.el
  17704. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17705. "This calls dvipng."
  17706. (require 'ox-latex)
  17707. (let* ((tmpdir (if (featurep 'xemacs)
  17708. (temp-directory)
  17709. temporary-file-directory))
  17710. (texfilebase (make-temp-name
  17711. (expand-file-name "orgtex" tmpdir)))
  17712. (texfile (concat texfilebase ".tex"))
  17713. (dvifile (concat texfilebase ".dvi"))
  17714. (pngfile (concat texfilebase ".png"))
  17715. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17716. ;; This assumes that the display has the same pixel width in
  17717. ;; the horizontal and vertical directions
  17718. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17719. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17720. "Black"))
  17721. (bg (or (plist-get options (if buffer :background :html-background))
  17722. "Transparent")))
  17723. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17724. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17725. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17726. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17727. (let ((latex-header (org-create-formula--latex-header)))
  17728. (with-temp-file texfile
  17729. (insert latex-header)
  17730. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17731. (let ((dir default-directory))
  17732. (ignore-errors
  17733. (cd tmpdir)
  17734. (call-process "latex" nil nil nil texfile))
  17735. (cd dir))
  17736. (if (not (file-exists-p dvifile))
  17737. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17738. (ignore-errors
  17739. (if (featurep 'xemacs)
  17740. (call-process "dvipng" nil nil nil
  17741. "-fg" fg "-bg" bg
  17742. "-T" "tight"
  17743. "-o" pngfile
  17744. dvifile)
  17745. (call-process "dvipng" nil nil nil
  17746. "-fg" fg "-bg" bg
  17747. "-D" dpi
  17748. ;;"-x" scale "-y" scale
  17749. "-T" "tight"
  17750. "-o" pngfile
  17751. dvifile)))
  17752. (if (not (file-exists-p pngfile))
  17753. (if org-format-latex-signal-error
  17754. (error "Failed to create png file from %s" texfile)
  17755. (message "Failed to create png file from %s" texfile)
  17756. nil)
  17757. ;; Use the requested file name and clean up
  17758. (copy-file pngfile tofile 'replace)
  17759. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17760. (if (file-exists-p (concat texfilebase e))
  17761. (delete-file (concat texfilebase e))))
  17762. pngfile))))
  17763. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17764. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17765. "This calls convert, which is included into imagemagick."
  17766. (require 'ox-latex)
  17767. (let* ((tmpdir (if (featurep 'xemacs)
  17768. (temp-directory)
  17769. temporary-file-directory))
  17770. (texfilebase (make-temp-name
  17771. (expand-file-name "orgtex" tmpdir)))
  17772. (texfile (concat texfilebase ".tex"))
  17773. (pdffile (concat texfilebase ".pdf"))
  17774. (pngfile (concat texfilebase ".png"))
  17775. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17776. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17777. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17778. "black"))
  17779. (bg (or (plist-get options (if buffer :background :html-background))
  17780. "white")))
  17781. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17782. (setq fg (org-latex-color-format fg)))
  17783. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17784. (setq bg (org-latex-color-format
  17785. (if (string= bg "Transparent") "white" bg))))
  17786. (let ((latex-header (org-create-formula--latex-header)))
  17787. (with-temp-file texfile
  17788. (insert latex-header)
  17789. (insert "\n\\begin{document}\n"
  17790. "\\definecolor{fg}{rgb}{" fg "}\n"
  17791. "\\definecolor{bg}{rgb}{" bg "}\n"
  17792. "\n\\pagecolor{bg}\n"
  17793. "\n{\\color{fg}\n"
  17794. string
  17795. "\n}\n"
  17796. "\n\\end{document}\n")))
  17797. (org-latex-compile texfile t)
  17798. (if (not (file-exists-p pdffile))
  17799. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17800. (ignore-errors
  17801. (if (featurep 'xemacs)
  17802. (call-process "convert" nil nil nil
  17803. "-density" "96"
  17804. "-trim"
  17805. "-antialias"
  17806. pdffile
  17807. "-quality" "100"
  17808. ;; "-sharpen" "0x1.0"
  17809. pngfile)
  17810. (call-process "convert" nil nil nil
  17811. "-density" dpi
  17812. "-trim"
  17813. "-antialias"
  17814. pdffile
  17815. "-quality" "100"
  17816. ;; "-sharpen" "0x1.0"
  17817. pngfile)))
  17818. (if (not (file-exists-p pngfile))
  17819. (if org-format-latex-signal-error
  17820. (error "Failed to create png file from %s" texfile)
  17821. (message "Failed to create png file from %s" texfile)
  17822. nil)
  17823. ;; Use the requested file name and clean up
  17824. (copy-file pngfile tofile 'replace)
  17825. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17826. (if (file-exists-p (concat texfilebase e))
  17827. (delete-file (concat texfilebase e))))
  17828. pngfile))))
  17829. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17830. "Fill a LaTeX header template TPL.
  17831. In the template, the following place holders will be recognized:
  17832. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17833. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17834. [PACKAGES] \\usepackage statements for PKG
  17835. [NO-PACKAGES] do not include PKG
  17836. [EXTRA] the string EXTRA
  17837. [NO-EXTRA] do not include EXTRA
  17838. For backward compatibility, if both the positive and the negative place
  17839. holder is missing, the positive one (without the \"NO-\") will be
  17840. assumed to be present at the end of the template.
  17841. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17842. EXTRA is a string.
  17843. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17844. (let (rpl (end ""))
  17845. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17846. (setq rpl (if (or (match-end 1) (not def-pkg))
  17847. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17848. tpl (replace-match rpl t t tpl))
  17849. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17850. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17851. (setq rpl (if (or (match-end 1) (not pkg))
  17852. "" (org-latex-packages-to-string pkg snippets-p t))
  17853. tpl (replace-match rpl t t tpl))
  17854. (if pkg (setq end
  17855. (concat end "\n"
  17856. (org-latex-packages-to-string pkg snippets-p)))))
  17857. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17858. (setq rpl (if (or (match-end 1) (not extra))
  17859. "" (concat extra "\n"))
  17860. tpl (replace-match rpl t t tpl))
  17861. (if (and extra (string-match "\\S-" extra))
  17862. (setq end (concat end "\n" extra))))
  17863. (if (string-match "\\S-" end)
  17864. (concat tpl "\n" end)
  17865. tpl)))
  17866. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17867. "Turn an alist of packages into a string with the \\usepackage macros."
  17868. (setq pkg (mapconcat (lambda(p)
  17869. (cond
  17870. ((stringp p) p)
  17871. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17872. (format "%% Package %s omitted" (cadr p)))
  17873. ((equal "" (car p))
  17874. (format "\\usepackage{%s}" (cadr p)))
  17875. (t
  17876. (format "\\usepackage[%s]{%s}"
  17877. (car p) (cadr p)))))
  17878. pkg
  17879. "\n"))
  17880. (if newline (concat pkg "\n") pkg))
  17881. (defun org-dvipng-color (attr)
  17882. "Return a RGB color specification for dvipng."
  17883. (apply 'format "rgb %s %s %s"
  17884. (mapcar 'org-normalize-color
  17885. (if (featurep 'xemacs)
  17886. (color-rgb-components
  17887. (face-property 'default
  17888. (cond ((eq attr :foreground) 'foreground)
  17889. ((eq attr :background) 'background))))
  17890. (color-values (face-attribute 'default attr nil))))))
  17891. (defun org-dvipng-color-format (color-name)
  17892. "Convert COLOR-NAME to a RGB color value for dvipng."
  17893. (apply 'format "rgb %s %s %s"
  17894. (mapcar 'org-normalize-color
  17895. (color-values color-name))))
  17896. (defun org-latex-color (attr)
  17897. "Return a RGB color for the LaTeX color package."
  17898. (apply 'format "%s,%s,%s"
  17899. (mapcar 'org-normalize-color
  17900. (if (featurep 'xemacs)
  17901. (color-rgb-components
  17902. (face-property 'default
  17903. (cond ((eq attr :foreground) 'foreground)
  17904. ((eq attr :background) 'background))))
  17905. (color-values (face-attribute 'default attr nil))))))
  17906. (defun org-latex-color-format (color-name)
  17907. "Convert COLOR-NAME to a RGB color value."
  17908. (apply 'format "%s,%s,%s"
  17909. (mapcar 'org-normalize-color
  17910. (color-values color-name))))
  17911. (defun org-normalize-color (value)
  17912. "Return string to be used as color value for an RGB component."
  17913. (format "%g" (/ value 65535.0)))
  17914. ;; Image display
  17915. (defvar org-inline-image-overlays nil)
  17916. (make-variable-buffer-local 'org-inline-image-overlays)
  17917. (defun org-toggle-inline-images (&optional include-linked)
  17918. "Toggle the display of inline images.
  17919. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17920. (interactive "P")
  17921. (if org-inline-image-overlays
  17922. (progn
  17923. (org-remove-inline-images)
  17924. (when (org-called-interactively-p 'interactive)
  17925. (message "Inline image display turned off")))
  17926. (org-display-inline-images include-linked)
  17927. (when (org-called-interactively-p 'interactive)
  17928. (message (if org-inline-image-overlays
  17929. (format "%d images displayed inline"
  17930. (length org-inline-image-overlays))
  17931. "No images to display inline")))))
  17932. (defun org-redisplay-inline-images ()
  17933. "Refresh the display of inline images."
  17934. (interactive)
  17935. (if (not org-inline-image-overlays)
  17936. (org-toggle-inline-images)
  17937. (org-toggle-inline-images)
  17938. (org-toggle-inline-images)))
  17939. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17940. "Display inline images.
  17941. An inline image is a link which follows either of these
  17942. conventions:
  17943. 1. Its path is a file with an extension matching return value
  17944. from `image-file-name-regexp' and it has no contents.
  17945. 2. Its description consists in a single link of the previous
  17946. type.
  17947. When optional argument INCLUDE-LINKED is non-nil, also links with
  17948. a text description part will be inlined. This can be nice for
  17949. a quick look at those images, but it does not reflect what
  17950. exported files will look like.
  17951. When optional argument REFRESH is non-nil, refresh existing
  17952. images between BEG and END. This will create new image displays
  17953. only if necessary. BEG and END default to the buffer
  17954. boundaries."
  17955. (interactive "P")
  17956. (when (display-graphic-p)
  17957. (unless refresh
  17958. (org-remove-inline-images)
  17959. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17960. (org-with-wide-buffer
  17961. (goto-char (or beg (point-min)))
  17962. (let ((case-fold-search t)
  17963. (file-extension-re (org-image-file-name-regexp)))
  17964. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17965. (let ((link (save-match-data (org-element-context))))
  17966. ;; Check if we're at an inline image.
  17967. (when (and (equal (org-element-property :type link) "file")
  17968. (or include-linked
  17969. (not (org-element-property :contents-begin link)))
  17970. (let ((parent (org-element-property :parent link)))
  17971. (or (not (eq (org-element-type parent) 'link))
  17972. (not (cdr (org-element-contents parent)))))
  17973. (org-string-match-p file-extension-re
  17974. (org-element-property :path link)))
  17975. (let ((file (expand-file-name
  17976. (org-link-unescape
  17977. (org-element-property :path link)))))
  17978. (when (file-exists-p file)
  17979. (let ((width
  17980. ;; Apply `org-image-actual-width' specifications.
  17981. (cond
  17982. ((not (image-type-available-p 'imagemagick)) nil)
  17983. ((eq org-image-actual-width t) nil)
  17984. ((listp org-image-actual-width)
  17985. (or
  17986. ;; First try to find a width among
  17987. ;; attributes associated to the paragraph
  17988. ;; containing link.
  17989. (let ((paragraph
  17990. (let ((e link))
  17991. (while (and (setq e (org-element-property
  17992. :parent e))
  17993. (not (eq (org-element-type e)
  17994. 'paragraph))))
  17995. e)))
  17996. (when paragraph
  17997. (save-excursion
  17998. (goto-char (org-element-property :begin paragraph))
  17999. (when
  18000. (re-search-forward
  18001. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18002. (org-element-property
  18003. :post-affiliated paragraph)
  18004. t)
  18005. (string-to-number (match-string 1))))))
  18006. ;; Otherwise, fall-back to provided number.
  18007. (car org-image-actual-width)))
  18008. ((numberp org-image-actual-width)
  18009. org-image-actual-width)))
  18010. (old (get-char-property-and-overlay
  18011. (org-element-property :begin link)
  18012. 'org-image-overlay)))
  18013. (if (and (car-safe old) refresh)
  18014. (image-refresh (overlay-get (cdr old) 'display))
  18015. (let ((image (create-image file
  18016. (and width 'imagemagick)
  18017. nil
  18018. :width width)))
  18019. (when image
  18020. (let* ((link
  18021. ;; If inline image is the description
  18022. ;; of another link, be sure to
  18023. ;; consider the latter as the one to
  18024. ;; apply the overlay on.
  18025. (let ((parent
  18026. (org-element-property :parent link)))
  18027. (if (eq (org-element-type parent) 'link)
  18028. parent
  18029. link)))
  18030. (ov (make-overlay
  18031. (org-element-property :begin link)
  18032. (progn
  18033. (goto-char
  18034. (org-element-property :end link))
  18035. (skip-chars-backward " \t")
  18036. (point)))))
  18037. (overlay-put ov 'display image)
  18038. (overlay-put ov 'face 'default)
  18039. (overlay-put ov 'org-image-overlay t)
  18040. (overlay-put
  18041. ov 'modification-hooks
  18042. (list 'org-display-inline-remove-overlay))
  18043. (push ov org-inline-image-overlays)))))))))))))))
  18044. (define-obsolete-function-alias
  18045. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18046. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18047. "Remove inline-display overlay if a corresponding region is modified."
  18048. (let ((inhibit-modification-hooks t))
  18049. (when (and ov after)
  18050. (delete ov org-inline-image-overlays)
  18051. (delete-overlay ov))))
  18052. (defun org-remove-inline-images ()
  18053. "Remove inline display of images."
  18054. (interactive)
  18055. (mapc 'delete-overlay org-inline-image-overlays)
  18056. (setq org-inline-image-overlays nil))
  18057. ;;;; Key bindings
  18058. ;; Outline functions from `outline-mode-prefix-map'
  18059. ;; that can be remapped in Org:
  18060. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18061. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18062. (define-key org-mode-map [remap outline-forward-same-level]
  18063. 'org-forward-heading-same-level)
  18064. (define-key org-mode-map [remap outline-backward-same-level]
  18065. 'org-backward-heading-same-level)
  18066. (define-key org-mode-map [remap show-branches]
  18067. 'org-kill-note-or-show-branches)
  18068. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18069. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18070. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18071. (define-key org-mode-map [remap outline-next-visible-heading]
  18072. 'org-next-visible-heading)
  18073. (define-key org-mode-map [remap outline-previous-visible-heading]
  18074. 'org-previous-visible-heading)
  18075. (define-key org-mode-map [remap show-children] 'org-show-children)
  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-up-heading' | `C-c C-u' | still same function |
  18086. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18087. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18088. ;; | `show-entry' | overridden | no replacement |
  18089. ;; | `show-branches' | `C-c C-k' | still same function |
  18090. ;; | `show-subtree' | overridden | visibility cycling |
  18091. ;; | `show-all' | overridden | no replacement |
  18092. ;; | `hide-subtree' | overridden | visibility cycling |
  18093. ;; | `hide-body' | overridden | no replacement |
  18094. ;; | `hide-entry' | overridden | visibility cycling |
  18095. ;; | `hide-leaves' | overridden | no replacement |
  18096. ;; | `hide-sublevels' | overridden | no replacement |
  18097. ;; | `hide-other' | overridden | no replacement |
  18098. ;; Make `C-c C-x' a prefix key
  18099. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18100. ;; TAB key with modifiers
  18101. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18102. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18103. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18104. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18105. ;; The following line is necessary under Suse GNU/Linux
  18106. (unless (featurep 'xemacs)
  18107. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18108. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18109. (define-key org-mode-map [backtab] 'org-shifttab)
  18110. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18111. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18112. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18113. ;; Cursor keys with modifiers
  18114. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18115. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18116. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18117. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18118. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18119. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18120. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18121. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18122. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18123. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18124. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18125. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18126. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18127. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18128. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18129. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18130. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18131. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18132. ;; Babel keys
  18133. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18134. (mapc (lambda (pair)
  18135. (define-key org-babel-map (car pair) (cdr pair)))
  18136. org-babel-key-bindings)
  18137. ;;; Extra keys for tty access.
  18138. ;; We only set them when really needed because otherwise the
  18139. ;; menus don't show the simple keys
  18140. (when (or org-use-extra-keys
  18141. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18142. (not window-system))
  18143. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18144. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18145. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18146. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18147. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18148. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18149. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18150. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18151. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18152. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18153. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18154. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18155. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18156. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18157. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18158. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18159. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18160. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18161. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18162. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18163. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18164. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18165. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18166. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18167. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18168. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18169. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18170. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18171. ;; All the other keys
  18172. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18173. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18174. (if (boundp 'narrow-map)
  18175. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18176. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18177. (if (boundp 'narrow-map)
  18178. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18179. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18180. (if (boundp 'narrow-map)
  18181. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18182. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18183. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18184. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18185. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18186. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18187. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18188. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18189. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18190. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18191. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18192. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18193. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18194. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18195. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18196. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18197. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18198. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18199. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18200. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18201. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18202. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18203. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18204. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18205. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18206. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18207. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18208. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18209. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18210. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18211. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18212. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18213. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18214. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18215. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18216. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18217. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18218. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18219. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18220. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18221. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18222. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18223. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18224. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18225. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18226. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18227. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18228. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18229. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18230. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18231. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18232. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18233. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18234. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18235. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18236. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18237. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18238. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18239. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18240. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18241. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18242. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18243. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18244. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18245. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18246. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18247. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18248. (org-defkey org-mode-map "\C-m" 'org-return)
  18249. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18250. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18251. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18252. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18253. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18254. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18255. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18256. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18257. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18258. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18259. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18260. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18261. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18262. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18263. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18264. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18265. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18266. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18267. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18268. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18269. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18270. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18271. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18272. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18273. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18274. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18275. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18290. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18292. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18293. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18294. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18295. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18296. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18297. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18298. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18299. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18300. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18301. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18302. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18303. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18304. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18305. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18306. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18307. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18308. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18309. (when (featurep 'xemacs)
  18310. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18311. (defconst org-speed-commands-default
  18312. '(
  18313. ("Outline Navigation")
  18314. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18315. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18316. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18317. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18318. ("F" . org-next-block)
  18319. ("B" . org-previous-block)
  18320. ("u" . (org-speed-move-safe 'outline-up-heading))
  18321. ("j" . org-goto)
  18322. ("g" . (org-refile t))
  18323. ("Outline Visibility")
  18324. ("c" . org-cycle)
  18325. ("C" . org-shifttab)
  18326. (" " . org-display-outline-path)
  18327. ("s" . org-narrow-to-subtree)
  18328. ("=" . org-columns)
  18329. ("Outline Structure Editing")
  18330. ("U" . org-metaup)
  18331. ("D" . org-metadown)
  18332. ("r" . org-metaright)
  18333. ("l" . org-metaleft)
  18334. ("R" . org-shiftmetaright)
  18335. ("L" . org-shiftmetaleft)
  18336. ("i" . (progn (forward-char 1) (call-interactively
  18337. 'org-insert-heading-respect-content)))
  18338. ("^" . org-sort)
  18339. ("w" . org-refile)
  18340. ("a" . org-archive-subtree-default-with-confirmation)
  18341. ("@" . org-mark-subtree)
  18342. ("#" . org-toggle-comment)
  18343. ("Clock Commands")
  18344. ("I" . org-clock-in)
  18345. ("O" . org-clock-out)
  18346. ("Meta Data Editing")
  18347. ("t" . org-todo)
  18348. ("," . (org-priority))
  18349. ("0" . (org-priority ?\ ))
  18350. ("1" . (org-priority ?A))
  18351. ("2" . (org-priority ?B))
  18352. ("3" . (org-priority ?C))
  18353. (":" . org-set-tags-command)
  18354. ("e" . org-set-effort)
  18355. ("E" . org-inc-effort)
  18356. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18357. (org-entry-put (point) "APPT_WARNTIME" m)))
  18358. ("Agenda Views etc")
  18359. ("v" . org-agenda)
  18360. ("/" . org-sparse-tree)
  18361. ("Misc")
  18362. ("o" . org-open-at-point)
  18363. ("?" . org-speed-command-help)
  18364. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18365. (">" . (org-agenda-remove-restriction-lock))
  18366. )
  18367. "The default speed commands.")
  18368. (defun org-print-speed-command (e)
  18369. (if (> (length (car e)) 1)
  18370. (progn
  18371. (princ "\n")
  18372. (princ (car e))
  18373. (princ "\n")
  18374. (princ (make-string (length (car e)) ?-))
  18375. (princ "\n"))
  18376. (princ (car e))
  18377. (princ " ")
  18378. (if (symbolp (cdr e))
  18379. (princ (symbol-name (cdr e)))
  18380. (prin1 (cdr e)))
  18381. (princ "\n")))
  18382. (defun org-speed-command-help ()
  18383. "Show the available speed commands."
  18384. (interactive)
  18385. (if (not org-use-speed-commands)
  18386. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18387. (with-output-to-temp-buffer "*Help*"
  18388. (princ "User-defined Speed commands\n===========================\n")
  18389. (mapc 'org-print-speed-command org-speed-commands-user)
  18390. (princ "\n")
  18391. (princ "Built-in Speed commands\n=======================\n")
  18392. (mapc 'org-print-speed-command org-speed-commands-default))
  18393. (with-current-buffer "*Help*"
  18394. (setq truncate-lines t))))
  18395. (defun org-speed-move-safe (cmd)
  18396. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18397. If not, return to the original position and throw an error."
  18398. (interactive)
  18399. (let ((pos (point)))
  18400. (call-interactively cmd)
  18401. (unless (and (bolp) (org-at-heading-p))
  18402. (goto-char pos)
  18403. (error "Boundary reached while executing %s" cmd))))
  18404. (defvar org-self-insert-command-undo-counter 0)
  18405. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18406. (defvar org-speed-command nil)
  18407. (define-obsolete-function-alias
  18408. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18409. (defun org-speed-command-activate (keys)
  18410. "Hook for activating single-letter speed commands.
  18411. `org-speed-commands-default' specifies a minimal command set.
  18412. Use `org-speed-commands-user' for further customization."
  18413. (when (or (and (bolp) (looking-at org-outline-regexp))
  18414. (and (functionp org-use-speed-commands)
  18415. (funcall org-use-speed-commands)))
  18416. (cdr (assoc keys (append org-speed-commands-user
  18417. org-speed-commands-default)))))
  18418. (define-obsolete-function-alias
  18419. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18420. (defun org-babel-speed-command-activate (keys)
  18421. "Hook for activating single-letter code block commands."
  18422. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18423. (cdr (assoc keys org-babel-key-bindings))))
  18424. (defcustom org-speed-command-hook
  18425. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18426. "Hook for activating speed commands at strategic locations.
  18427. Hook functions are called in sequence until a valid handler is
  18428. found.
  18429. Each hook takes a single argument, a user-pressed command key
  18430. which is also a `self-insert-command' from the global map.
  18431. Within the hook, examine the cursor position and the command key
  18432. and return nil or a valid handler as appropriate. Handler could
  18433. be one of an interactive command, a function, or a form.
  18434. Set `org-use-speed-commands' to non-nil value to enable this
  18435. hook. The default setting is `org-speed-command-activate'."
  18436. :group 'org-structure
  18437. :version "24.1"
  18438. :type 'hook)
  18439. (defun org-self-insert-command (N)
  18440. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18441. If the cursor is in a table looking at whitespace, the whitespace is
  18442. overwritten, and the table is not marked as requiring realignment."
  18443. (interactive "p")
  18444. (org-check-before-invisible-edit 'insert)
  18445. (cond
  18446. ((and org-use-speed-commands
  18447. (let ((kv (this-command-keys-vector)))
  18448. (setq org-speed-command
  18449. (run-hook-with-args-until-success
  18450. 'org-speed-command-hook
  18451. (make-string 1 (aref kv (1- (length kv))))))))
  18452. (cond
  18453. ((commandp org-speed-command)
  18454. (setq this-command org-speed-command)
  18455. (call-interactively org-speed-command))
  18456. ((functionp org-speed-command)
  18457. (funcall org-speed-command))
  18458. ((and org-speed-command (listp org-speed-command))
  18459. (eval org-speed-command))
  18460. (t (let (org-use-speed-commands)
  18461. (call-interactively 'org-self-insert-command)))))
  18462. ((and
  18463. (org-table-p)
  18464. (progn
  18465. ;; check if we blank the field, and if that triggers align
  18466. (and (featurep 'org-table) org-table-auto-blank-field
  18467. (memq last-command
  18468. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18469. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18470. ;; got extra space, this field does not determine column width
  18471. (let (org-table-may-need-update) (org-table-blank-field))
  18472. ;; no extra space, this field may determine column width
  18473. (org-table-blank-field)))
  18474. t)
  18475. (eq N 1)
  18476. (looking-at "[^|\n]* |"))
  18477. (let (org-table-may-need-update)
  18478. (goto-char (1- (match-end 0)))
  18479. (backward-delete-char 1)
  18480. (goto-char (match-beginning 0))
  18481. (self-insert-command N)))
  18482. (t
  18483. (setq org-table-may-need-update t)
  18484. (self-insert-command N)
  18485. (org-fix-tags-on-the-fly)
  18486. (if org-self-insert-cluster-for-undo
  18487. (if (not (eq last-command 'org-self-insert-command))
  18488. (setq org-self-insert-command-undo-counter 1)
  18489. (if (>= org-self-insert-command-undo-counter 20)
  18490. (setq org-self-insert-command-undo-counter 1)
  18491. (and (> org-self-insert-command-undo-counter 0)
  18492. buffer-undo-list (listp buffer-undo-list)
  18493. (not (cadr buffer-undo-list)) ; remove nil entry
  18494. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18495. (setq org-self-insert-command-undo-counter
  18496. (1+ org-self-insert-command-undo-counter))))))))
  18497. (defun org-check-before-invisible-edit (kind)
  18498. "Check is editing if kind KIND would be dangerous with invisible text around.
  18499. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18500. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18501. (if (and org-catch-invisible-edits
  18502. (or (not (boundp 'visible-mode)) (not visible-mode))
  18503. (or (get-char-property (point) 'invisible)
  18504. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18505. ;; OK, we need to take a closer look
  18506. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18507. (invisible-before-point (if (bobp) nil (get-char-property
  18508. (1- (point)) 'invisible)))
  18509. (border-and-ok-direction
  18510. (or
  18511. ;; Check if we are acting predictably before invisible text
  18512. (and invisible-at-point (not invisible-before-point)
  18513. (memq kind '(insert delete-backward)))
  18514. ;; Check if we are acting predictably after invisible text
  18515. ;; This works not well, and I have turned it off. It seems
  18516. ;; better to always show and stop after invisible text.
  18517. ;; (and (not invisible-at-point) invisible-before-point
  18518. ;; (memq kind '(insert delete)))
  18519. )))
  18520. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18521. (memq invisible-before-point '(outline org-hide-block t)))
  18522. (if (eq org-catch-invisible-edits 'error)
  18523. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18524. (if (and org-custom-properties-overlays
  18525. (y-or-n-p "Display invisible properties in this buffer? "))
  18526. (org-toggle-custom-properties-visibility)
  18527. ;; Make the area visible
  18528. (save-excursion
  18529. (if invisible-before-point
  18530. (goto-char (previous-single-char-property-change
  18531. (point) 'invisible)))
  18532. (show-subtree))
  18533. (cond
  18534. ((eq org-catch-invisible-edits 'show)
  18535. ;; That's it, we do the edit after showing
  18536. (message
  18537. "Unfolding invisible region around point before editing")
  18538. (sit-for 1))
  18539. ((and (eq org-catch-invisible-edits 'smart)
  18540. border-and-ok-direction)
  18541. (message "Unfolding invisible region around point before editing"))
  18542. (t
  18543. ;; Don't do the edit, make the user repeat it in full visibility
  18544. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18545. (defun org-fix-tags-on-the-fly ()
  18546. (when (and (equal (char-after (point-at-bol)) ?*)
  18547. (org-at-heading-p))
  18548. (org-align-tags-here org-tags-column)))
  18549. (defun org-delete-backward-char (N)
  18550. "Like `delete-backward-char', insert whitespace at field end in tables.
  18551. When deleting backwards, in tables this function will insert whitespace in
  18552. front of the next \"|\" separator, to keep the table aligned. The table will
  18553. still be marked for re-alignment if the field did fill the entire column,
  18554. because, in this case the deletion might narrow the column."
  18555. (interactive "p")
  18556. (save-match-data
  18557. (org-check-before-invisible-edit 'delete-backward)
  18558. (if (and (org-table-p)
  18559. (eq N 1)
  18560. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18561. (looking-at ".*?|"))
  18562. (let ((pos (point))
  18563. (noalign (looking-at "[^|\n\r]* |"))
  18564. (c org-table-may-need-update))
  18565. (backward-delete-char N)
  18566. (if (not overwrite-mode)
  18567. (progn
  18568. (skip-chars-forward "^|")
  18569. (insert " ")
  18570. (goto-char (1- pos))))
  18571. ;; noalign: if there were two spaces at the end, this field
  18572. ;; does not determine the width of the column.
  18573. (if noalign (setq org-table-may-need-update c)))
  18574. (backward-delete-char N)
  18575. (org-fix-tags-on-the-fly))))
  18576. (defun org-delete-char (N)
  18577. "Like `delete-char', but insert whitespace at field end in tables.
  18578. When deleting characters, in tables this function will insert whitespace in
  18579. front of the next \"|\" separator, to keep the table aligned. The table will
  18580. still be marked for re-alignment if the field did fill the entire column,
  18581. because, in this case the deletion might narrow the column."
  18582. (interactive "p")
  18583. (save-match-data
  18584. (org-check-before-invisible-edit 'delete)
  18585. (if (and (org-table-p)
  18586. (not (bolp))
  18587. (not (= (char-after) ?|))
  18588. (eq N 1))
  18589. (if (looking-at ".*?|")
  18590. (let ((pos (point))
  18591. (noalign (looking-at "[^|\n\r]* |"))
  18592. (c org-table-may-need-update))
  18593. (replace-match
  18594. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18595. (goto-char pos)
  18596. ;; noalign: if there were two spaces at the end, this field
  18597. ;; does not determine the width of the column.
  18598. (if noalign (setq org-table-may-need-update c)))
  18599. (delete-char N))
  18600. (delete-char N)
  18601. (org-fix-tags-on-the-fly))))
  18602. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18603. (put 'org-self-insert-command 'delete-selection
  18604. (lambda ()
  18605. (not (run-hook-with-args-until-success
  18606. 'self-insert-uses-region-functions))))
  18607. (put 'orgtbl-self-insert-command 'delete-selection
  18608. (lambda ()
  18609. (not (run-hook-with-args-until-success
  18610. 'self-insert-uses-region-functions))))
  18611. (put 'org-delete-char 'delete-selection 'supersede)
  18612. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18613. (put 'org-yank 'delete-selection 'yank)
  18614. ;; Make `flyspell-mode' delay after some commands
  18615. (put 'org-self-insert-command 'flyspell-delayed t)
  18616. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18617. (put 'org-delete-char 'flyspell-delayed t)
  18618. (put 'org-delete-backward-char 'flyspell-delayed t)
  18619. ;; Make pabbrev-mode expand after org-mode commands
  18620. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18621. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18622. (defun org-remap (map &rest commands)
  18623. "In MAP, remap the functions given in COMMANDS.
  18624. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18625. (let (new old)
  18626. (while commands
  18627. (setq old (pop commands) new (pop commands))
  18628. (if (fboundp 'command-remapping)
  18629. (org-defkey map (vector 'remap old) new)
  18630. (substitute-key-definition old new map global-map)))))
  18631. (defun org-transpose-words ()
  18632. "Transpose words for Org.
  18633. This uses the `org-mode-transpose-word-syntax-table' syntax
  18634. table, which interprets characters in `org-emphasis-alist' as
  18635. word constituents."
  18636. (interactive)
  18637. (with-syntax-table org-mode-transpose-word-syntax-table
  18638. (call-interactively 'transpose-words)))
  18639. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18640. (when (eq org-enable-table-editor 'optimized)
  18641. ;; If the user wants maximum table support, we need to hijack
  18642. ;; some standard editing functions
  18643. (org-remap org-mode-map
  18644. 'self-insert-command 'org-self-insert-command
  18645. 'delete-char 'org-delete-char
  18646. 'delete-backward-char 'org-delete-backward-char)
  18647. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18648. (defvar org-ctrl-c-ctrl-c-hook nil
  18649. "Hook for functions attaching themselves to `C-c C-c'.
  18650. This can be used to add additional functionality to the C-c C-c
  18651. key which executes context-dependent commands. This hook is run
  18652. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18653. run after the last test.
  18654. Each function will be called with no arguments. The function
  18655. must check if the context is appropriate for it to act. If yes,
  18656. it should do its thing and then return a non-nil value. If the
  18657. context is wrong, just do nothing and return nil.")
  18658. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18659. "Hook for functions attaching themselves to `C-c C-c'.
  18660. This can be used to add additional functionality to the C-c C-c
  18661. key which executes context-dependent commands. This hook is run
  18662. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18663. before the first test.
  18664. Each function will be called with no arguments. The function
  18665. must check if the context is appropriate for it to act. If yes,
  18666. it should do its thing and then return a non-nil value. If the
  18667. context is wrong, just do nothing and return nil.")
  18668. (defvar org-tab-first-hook nil
  18669. "Hook for functions to attach themselves to TAB.
  18670. See `org-ctrl-c-ctrl-c-hook' for more information.
  18671. This hook runs as the first action when TAB is pressed, even before
  18672. `org-cycle' messes around with the `outline-regexp' to cater for
  18673. inline tasks and plain list item folding.
  18674. If any function in this hook returns t, any other actions that
  18675. would have been caused by TAB (such as table field motion or visibility
  18676. cycling) will not occur.")
  18677. (defvar org-tab-after-check-for-table-hook nil
  18678. "Hook for functions to attach themselves to TAB.
  18679. See `org-ctrl-c-ctrl-c-hook' for more information.
  18680. This hook runs after it has been established that the cursor is not in a
  18681. table, but before checking if the cursor is in a headline or if global cycling
  18682. should be done.
  18683. If any function in this hook returns t, not other actions like visibility
  18684. cycling will be done.")
  18685. (defvar org-tab-after-check-for-cycling-hook nil
  18686. "Hook for functions to attach themselves to TAB.
  18687. See `org-ctrl-c-ctrl-c-hook' for more information.
  18688. This hook runs after it has been established that not table field motion and
  18689. not visibility should be done because of current context. This is probably
  18690. the place where a package like yasnippets can hook in.")
  18691. (defvar org-tab-before-tab-emulation-hook nil
  18692. "Hook for functions to attach themselves to TAB.
  18693. See `org-ctrl-c-ctrl-c-hook' for more information.
  18694. This hook runs after every other options for TAB have been exhausted, but
  18695. before indentation and \t insertion takes place.")
  18696. (defvar org-metaleft-hook nil
  18697. "Hook for functions attaching themselves to `M-left'.
  18698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18699. (defvar org-metaright-hook nil
  18700. "Hook for functions attaching themselves to `M-right'.
  18701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18702. (defvar org-metaup-hook nil
  18703. "Hook for functions attaching themselves to `M-up'.
  18704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18705. (defvar org-metadown-hook nil
  18706. "Hook for functions attaching themselves to `M-down'.
  18707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18708. (defvar org-shiftmetaleft-hook nil
  18709. "Hook for functions attaching themselves to `M-S-left'.
  18710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18711. (defvar org-shiftmetaright-hook nil
  18712. "Hook for functions attaching themselves to `M-S-right'.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18714. (defvar org-shiftmetaup-hook nil
  18715. "Hook for functions attaching themselves to `M-S-up'.
  18716. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18717. (defvar org-shiftmetadown-hook nil
  18718. "Hook for functions attaching themselves to `M-S-down'.
  18719. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18720. (defvar org-metareturn-hook nil
  18721. "Hook for functions attaching themselves to `M-RET'.
  18722. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18723. (defvar org-shiftup-hook nil
  18724. "Hook for functions attaching themselves to `S-up'.
  18725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18726. (defvar org-shiftup-final-hook nil
  18727. "Hook for functions attaching themselves to `S-up'.
  18728. This one runs after all other options except shift-select have been excluded.
  18729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18730. (defvar org-shiftdown-hook nil
  18731. "Hook for functions attaching themselves to `S-down'.
  18732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18733. (defvar org-shiftdown-final-hook nil
  18734. "Hook for functions attaching themselves to `S-down'.
  18735. This one runs after all other options except shift-select have been excluded.
  18736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18737. (defvar org-shiftleft-hook nil
  18738. "Hook for functions attaching themselves to `S-left'.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-shiftleft-final-hook nil
  18741. "Hook for functions attaching themselves to `S-left'.
  18742. This one runs after all other options except shift-select have been excluded.
  18743. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18744. (defvar org-shiftright-hook nil
  18745. "Hook for functions attaching themselves to `S-right'.
  18746. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18747. (defvar org-shiftright-final-hook nil
  18748. "Hook for functions attaching themselves to `S-right'.
  18749. This one runs after all other options except shift-select have been excluded.
  18750. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18751. (defun org-modifier-cursor-error ()
  18752. "Throw an error, a modified cursor command was applied in wrong context."
  18753. (user-error "This command is active in special context like tables, headlines or items"))
  18754. (defun org-shiftselect-error ()
  18755. "Throw an error because Shift-Cursor command was applied in wrong context."
  18756. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18757. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18758. (user-error "This command works only in special context like headlines or timestamps")))
  18759. (defun org-call-for-shift-select (cmd)
  18760. (let ((this-command-keys-shift-translated t))
  18761. (call-interactively cmd)))
  18762. (defun org-shifttab (&optional arg)
  18763. "Global visibility cycling or move to previous table field.
  18764. Call `org-table-previous-field' within a table.
  18765. When ARG is nil, cycle globally through visibility states.
  18766. When ARG is a numeric prefix, show contents of this level."
  18767. (interactive "P")
  18768. (cond
  18769. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18770. ((integerp arg)
  18771. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18772. (message "Content view to level: %d" arg)
  18773. (org-content (prefix-numeric-value arg2))
  18774. (org-cycle-show-empty-lines t)
  18775. (setq org-cycle-global-status 'overview)))
  18776. (t (call-interactively 'org-global-cycle))))
  18777. (defun org-shiftmetaleft ()
  18778. "Promote subtree or delete table column.
  18779. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18780. `org-table-delete-column', depending on context. See the
  18781. individual commands for more information."
  18782. (interactive)
  18783. (cond
  18784. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18785. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18786. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18787. ((if (not (org-region-active-p)) (org-at-item-p)
  18788. (save-excursion (goto-char (region-beginning))
  18789. (org-at-item-p)))
  18790. (call-interactively 'org-outdent-item-tree))
  18791. (t (org-modifier-cursor-error))))
  18792. (defun org-shiftmetaright ()
  18793. "Demote subtree or insert table column.
  18794. Calls `org-demote-subtree', `org-indent-item-tree', or
  18795. `org-table-insert-column', depending on context. See the
  18796. individual commands for more information."
  18797. (interactive)
  18798. (cond
  18799. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18800. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18801. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18802. ((if (not (org-region-active-p)) (org-at-item-p)
  18803. (save-excursion (goto-char (region-beginning))
  18804. (org-at-item-p)))
  18805. (call-interactively 'org-indent-item-tree))
  18806. (t (org-modifier-cursor-error))))
  18807. (defun org-shiftmetaup (&optional arg)
  18808. "Drag the line at point up.
  18809. In a table, kill the current row.
  18810. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18811. but also adjust the previous clocked item in the clock history.
  18812. Everywhere else, drag the line at point up."
  18813. (interactive "P")
  18814. (cond
  18815. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18816. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18817. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18818. (call-interactively 'org-timestamp-up)))
  18819. (t (call-interactively 'org-drag-line-backward))))
  18820. (defun org-shiftmetadown (&optional arg)
  18821. "Drag the line at point down.
  18822. In a table, insert an empty row at the current line.
  18823. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18824. but also adjust the previous clocked item in the clock history.
  18825. Everywhere else, drag the line at point down."
  18826. (interactive "P")
  18827. (cond
  18828. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18829. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18830. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18831. (call-interactively 'org-timestamp-down)))
  18832. (t (call-interactively 'org-drag-line-forward))))
  18833. (defsubst org-hidden-tree-error ()
  18834. (user-error
  18835. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18836. (defun org-metaleft (&optional arg)
  18837. "Promote heading, list item at point or move table column left.
  18838. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18839. depending on context. With no specific context, calls the Emacs
  18840. default `backward-word'. See the individual commands for more
  18841. information.
  18842. This function runs the hook `org-metaleft-hook' as a first step,
  18843. and returns at first non-nil value."
  18844. (interactive "P")
  18845. (cond
  18846. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18847. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18848. ((org-with-limited-levels
  18849. (or (org-at-heading-p)
  18850. (and (org-region-active-p)
  18851. (save-excursion
  18852. (goto-char (region-beginning))
  18853. (org-at-heading-p)))))
  18854. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18855. (call-interactively 'org-do-promote))
  18856. ;; At an inline task.
  18857. ((org-at-heading-p)
  18858. (call-interactively 'org-inlinetask-promote))
  18859. ((or (org-at-item-p)
  18860. (and (org-region-active-p)
  18861. (save-excursion
  18862. (goto-char (region-beginning))
  18863. (org-at-item-p))))
  18864. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18865. (call-interactively 'org-outdent-item))
  18866. (t (call-interactively 'backward-word))))
  18867. (defun org-metaright (&optional arg)
  18868. "Demote heading, list item at point or move table column right.
  18869. In front of a drawer or a block keyword, indent it correctly.
  18870. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18871. `org-indnet-drawer' or `org-indent-block' depending on context.
  18872. With no specific context, calls the Emacs default `forward-word'.
  18873. See the individual commands for more information.
  18874. This function runs the hook `org-metaright-hook' as a first step,
  18875. and returns at first non-nil value."
  18876. (interactive "P")
  18877. (cond
  18878. ((run-hook-with-args-until-success 'org-metaright-hook))
  18879. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18880. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18881. ((org-at-block-p) (call-interactively 'org-indent-block))
  18882. ((org-with-limited-levels
  18883. (or (org-at-heading-p)
  18884. (and (org-region-active-p)
  18885. (save-excursion
  18886. (goto-char (region-beginning))
  18887. (org-at-heading-p)))))
  18888. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18889. (call-interactively 'org-do-demote))
  18890. ;; At an inline task.
  18891. ((org-at-heading-p)
  18892. (call-interactively 'org-inlinetask-demote))
  18893. ((or (org-at-item-p)
  18894. (and (org-region-active-p)
  18895. (save-excursion
  18896. (goto-char (region-beginning))
  18897. (org-at-item-p))))
  18898. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18899. (call-interactively 'org-indent-item))
  18900. (t (call-interactively 'forward-word))))
  18901. (defun org-check-for-hidden (what)
  18902. "Check if there are hidden headlines/items in the current visual line.
  18903. WHAT can be either `headlines' or `items'. If the current line is
  18904. an outline or item heading and it has a folded subtree below it,
  18905. this function returns t, nil otherwise."
  18906. (let ((re (cond
  18907. ((eq what 'headlines) org-outline-regexp-bol)
  18908. ((eq what 'items) (org-item-beginning-re))
  18909. (t (error "This should not happen"))))
  18910. beg end)
  18911. (save-excursion
  18912. (catch 'exit
  18913. (unless (org-region-active-p)
  18914. (setq beg (point-at-bol))
  18915. (beginning-of-line 2)
  18916. (while (and (not (eobp)) ;; this is like `next-line'
  18917. (get-char-property (1- (point)) 'invisible))
  18918. (beginning-of-line 2))
  18919. (setq end (point))
  18920. (goto-char beg)
  18921. (goto-char (point-at-eol))
  18922. (setq end (max end (point)))
  18923. (while (re-search-forward re end t)
  18924. (if (get-char-property (match-beginning 0) 'invisible)
  18925. (throw 'exit t))))
  18926. nil))))
  18927. (defun org-metaup (&optional arg)
  18928. "Move subtree up or move table row up.
  18929. Calls `org-move-subtree-up' or `org-table-move-row' or
  18930. `org-move-item-up', depending on context. See the individual commands
  18931. for more information."
  18932. (interactive "P")
  18933. (cond
  18934. ((run-hook-with-args-until-success 'org-metaup-hook))
  18935. ((org-region-active-p)
  18936. (let* ((a (min (region-beginning) (region-end)))
  18937. (b (1- (max (region-beginning) (region-end))))
  18938. (c (save-excursion (goto-char a)
  18939. (move-beginning-of-line 0)))
  18940. (d (save-excursion (goto-char a)
  18941. (move-end-of-line 0) (point))))
  18942. (transpose-regions a b c d)
  18943. (goto-char c)))
  18944. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18945. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18946. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18947. (t (org-drag-element-backward))))
  18948. (defun org-metadown (&optional arg)
  18949. "Move subtree down or move table row down.
  18950. Calls `org-move-subtree-down' or `org-table-move-row' or
  18951. `org-move-item-down', depending on context. See the individual
  18952. commands for more information."
  18953. (interactive "P")
  18954. (cond
  18955. ((run-hook-with-args-until-success 'org-metadown-hook))
  18956. ((org-region-active-p)
  18957. (let* ((a (min (region-beginning) (region-end)))
  18958. (b (max (region-beginning) (region-end)))
  18959. (c (save-excursion (goto-char b)
  18960. (move-beginning-of-line 1)))
  18961. (d (save-excursion (goto-char b)
  18962. (move-end-of-line 1) (1+ (point)))))
  18963. (transpose-regions a b c d)
  18964. (goto-char d)))
  18965. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18966. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18967. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18968. (t (org-drag-element-forward))))
  18969. (defun org-shiftup (&optional arg)
  18970. "Increase item in timestamp or increase priority of current headline.
  18971. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18972. depending on context. See the individual commands for more information."
  18973. (interactive "P")
  18974. (cond
  18975. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18976. ((and org-support-shift-select (org-region-active-p))
  18977. (org-call-for-shift-select 'previous-line))
  18978. ((org-at-timestamp-p t)
  18979. (call-interactively (if org-edit-timestamp-down-means-later
  18980. 'org-timestamp-down 'org-timestamp-up)))
  18981. ((and (not (eq org-support-shift-select 'always))
  18982. org-enable-priority-commands
  18983. (org-at-heading-p))
  18984. (call-interactively 'org-priority-up))
  18985. ((and (not org-support-shift-select) (org-at-item-p))
  18986. (call-interactively 'org-previous-item))
  18987. ((org-clocktable-try-shift 'up arg))
  18988. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18989. (org-support-shift-select
  18990. (org-call-for-shift-select 'previous-line))
  18991. (t (org-shiftselect-error))))
  18992. (defun org-shiftdown (&optional arg)
  18993. "Decrease item in timestamp or decrease priority of current headline.
  18994. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18995. depending on context. See the individual commands for more information."
  18996. (interactive "P")
  18997. (cond
  18998. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18999. ((and org-support-shift-select (org-region-active-p))
  19000. (org-call-for-shift-select 'next-line))
  19001. ((org-at-timestamp-p t)
  19002. (call-interactively (if org-edit-timestamp-down-means-later
  19003. 'org-timestamp-up 'org-timestamp-down)))
  19004. ((and (not (eq org-support-shift-select 'always))
  19005. org-enable-priority-commands
  19006. (org-at-heading-p))
  19007. (call-interactively 'org-priority-down))
  19008. ((and (not org-support-shift-select) (org-at-item-p))
  19009. (call-interactively 'org-next-item))
  19010. ((org-clocktable-try-shift 'down arg))
  19011. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19012. (org-support-shift-select
  19013. (org-call-for-shift-select 'next-line))
  19014. (t (org-shiftselect-error))))
  19015. (defun org-shiftright (&optional arg)
  19016. "Cycle the thing at point or in the current line, depending on context.
  19017. Depending on context, this does one of the following:
  19018. - switch a timestamp at point one day into the future
  19019. - on a headline, switch to the next TODO keyword.
  19020. - on an item, switch entire list to the next bullet type
  19021. - on a property line, switch to the next allowed value
  19022. - on a clocktable definition line, move time block into the future"
  19023. (interactive "P")
  19024. (cond
  19025. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19026. ((and org-support-shift-select (org-region-active-p))
  19027. (org-call-for-shift-select 'forward-char))
  19028. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19029. ((and (not (eq org-support-shift-select 'always))
  19030. (org-at-heading-p))
  19031. (let ((org-inhibit-logging
  19032. (not org-treat-S-cursor-todo-selection-as-state-change))
  19033. (org-inhibit-blocking
  19034. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19035. (org-call-with-arg 'org-todo 'right)))
  19036. ((or (and org-support-shift-select
  19037. (not (eq org-support-shift-select 'always))
  19038. (org-at-item-bullet-p))
  19039. (and (not org-support-shift-select) (org-at-item-p)))
  19040. (org-call-with-arg 'org-cycle-list-bullet nil))
  19041. ((and (not (eq org-support-shift-select 'always))
  19042. (org-at-property-p))
  19043. (call-interactively 'org-property-next-allowed-value))
  19044. ((org-clocktable-try-shift 'right arg))
  19045. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19046. (org-support-shift-select
  19047. (org-call-for-shift-select 'forward-char))
  19048. (t (org-shiftselect-error))))
  19049. (defun org-shiftleft (&optional arg)
  19050. "Cycle the thing at point or in the current line, depending on context.
  19051. Depending on context, this does one of the following:
  19052. - switch a timestamp at point one day into the past
  19053. - on a headline, switch to the previous TODO keyword.
  19054. - on an item, switch entire list to the previous bullet type
  19055. - on a property line, switch to the previous allowed value
  19056. - on a clocktable definition line, move time block into the past"
  19057. (interactive "P")
  19058. (cond
  19059. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19060. ((and org-support-shift-select (org-region-active-p))
  19061. (org-call-for-shift-select 'backward-char))
  19062. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19063. ((and (not (eq org-support-shift-select 'always))
  19064. (org-at-heading-p))
  19065. (let ((org-inhibit-logging
  19066. (not org-treat-S-cursor-todo-selection-as-state-change))
  19067. (org-inhibit-blocking
  19068. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19069. (org-call-with-arg 'org-todo 'left)))
  19070. ((or (and org-support-shift-select
  19071. (not (eq org-support-shift-select 'always))
  19072. (org-at-item-bullet-p))
  19073. (and (not org-support-shift-select) (org-at-item-p)))
  19074. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19075. ((and (not (eq org-support-shift-select 'always))
  19076. (org-at-property-p))
  19077. (call-interactively 'org-property-previous-allowed-value))
  19078. ((org-clocktable-try-shift 'left arg))
  19079. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19080. (org-support-shift-select
  19081. (org-call-for-shift-select 'backward-char))
  19082. (t (org-shiftselect-error))))
  19083. (defun org-shiftcontrolright ()
  19084. "Switch to next TODO set."
  19085. (interactive)
  19086. (cond
  19087. ((and org-support-shift-select (org-region-active-p))
  19088. (org-call-for-shift-select 'forward-word))
  19089. ((and (not (eq org-support-shift-select 'always))
  19090. (org-at-heading-p))
  19091. (org-call-with-arg 'org-todo 'nextset))
  19092. (org-support-shift-select
  19093. (org-call-for-shift-select 'forward-word))
  19094. (t (org-shiftselect-error))))
  19095. (defun org-shiftcontrolleft ()
  19096. "Switch to previous TODO set."
  19097. (interactive)
  19098. (cond
  19099. ((and org-support-shift-select (org-region-active-p))
  19100. (org-call-for-shift-select 'backward-word))
  19101. ((and (not (eq org-support-shift-select 'always))
  19102. (org-at-heading-p))
  19103. (org-call-with-arg 'org-todo 'previousset))
  19104. (org-support-shift-select
  19105. (org-call-for-shift-select 'backward-word))
  19106. (t (org-shiftselect-error))))
  19107. (defun org-shiftcontrolup (&optional n)
  19108. "Change timestamps synchronously up in CLOCK log lines.
  19109. Optional argument N tells to change by that many units."
  19110. (interactive "P")
  19111. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19112. (let (org-support-shift-select)
  19113. (org-clock-timestamps-up n))
  19114. (user-error "Not at a clock log")))
  19115. (defun org-shiftcontroldown (&optional n)
  19116. "Change timestamps synchronously down in CLOCK log lines.
  19117. Optional argument N tells to change by that many units."
  19118. (interactive "P")
  19119. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19120. (let (org-support-shift-select)
  19121. (org-clock-timestamps-down n))
  19122. (user-error "Not at a clock log")))
  19123. (defun org-increase-number-at-point (&optional inc)
  19124. "Increment the number at point.
  19125. With an optional prefix numeric argument INC, increment using
  19126. this numeric value."
  19127. (interactive "p")
  19128. (if (not (number-at-point))
  19129. (user-error "Not on a number")
  19130. (unless inc (setq inc 1))
  19131. (let ((pos (point))
  19132. (beg (skip-chars-backward "-+^/*0-9eE."))
  19133. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19134. (setq nap (buffer-substring-no-properties
  19135. (+ pos beg) (+ pos beg end)))
  19136. (delete-region (+ pos beg) (+ pos beg end))
  19137. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19138. (when (org-at-table-p)
  19139. (org-table-align)
  19140. (org-table-end-of-field 1))))
  19141. (defun org-decrease-number-at-point (&optional inc)
  19142. "Decrement the number at point.
  19143. With an optional prefix numeric argument INC, decrement using
  19144. this numeric value."
  19145. (interactive "p")
  19146. (org-increase-number-at-point (- (or inc 1))))
  19147. (defun org-ctrl-c-ret ()
  19148. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19149. (interactive)
  19150. (cond
  19151. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19152. (t (call-interactively 'org-insert-heading))))
  19153. (defun org-find-visible ()
  19154. (let ((s (point)))
  19155. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19156. (get-char-property s 'invisible)))
  19157. s))
  19158. (defun org-find-invisible ()
  19159. (let ((s (point)))
  19160. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19161. (not (get-char-property s 'invisible))))
  19162. s))
  19163. (defun org-copy-visible (beg end)
  19164. "Copy the visible parts of the region."
  19165. (interactive "r")
  19166. (let (snippets s)
  19167. (save-excursion
  19168. (save-restriction
  19169. (narrow-to-region beg end)
  19170. (setq s (goto-char (point-min)))
  19171. (while (not (= (point) (point-max)))
  19172. (goto-char (org-find-invisible))
  19173. (push (buffer-substring s (point)) snippets)
  19174. (setq s (goto-char (org-find-visible))))))
  19175. (kill-new (apply 'concat (nreverse snippets)))))
  19176. (defun org-copy-special ()
  19177. "Copy region in table or copy current subtree.
  19178. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19179. See the individual commands for more information."
  19180. (interactive)
  19181. (call-interactively
  19182. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19183. (defun org-cut-special ()
  19184. "Cut region in table or cut current subtree.
  19185. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19186. See the individual commands for more information."
  19187. (interactive)
  19188. (call-interactively
  19189. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19190. (defun org-paste-special (arg)
  19191. "Paste rectangular region into table, or past subtree relative to level.
  19192. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19193. See the individual commands for more information."
  19194. (interactive "P")
  19195. (if (org-at-table-p)
  19196. (org-table-paste-rectangle)
  19197. (org-paste-subtree arg)))
  19198. (defsubst org-in-fixed-width-region-p ()
  19199. "Is point in a fixed-width region?"
  19200. (save-match-data
  19201. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19202. (defun org-edit-special (&optional arg)
  19203. "Call a special editor for the element at point.
  19204. When at a table, call the formula editor with `org-table-edit-formulas'.
  19205. When in a source code block, call `org-edit-src-code'.
  19206. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19207. When in an export block, call `org-edit-export-block'.
  19208. When at an #+INCLUDE keyword, visit the included file.
  19209. When at a footnote reference, call `org-edit-footnote-reference'
  19210. On a link, call `ffap' to visit the link at point.
  19211. Otherwise, return a user error."
  19212. (interactive "P")
  19213. (let ((element (org-element-at-point)))
  19214. (assert (not buffer-read-only) nil
  19215. "Buffer is read-only: %s" (buffer-name))
  19216. (case (org-element-type element)
  19217. (src-block
  19218. (if (not arg) (org-edit-src-code)
  19219. (let* ((info (org-babel-get-src-block-info))
  19220. (lang (nth 0 info))
  19221. (params (nth 2 info))
  19222. (session (cdr (assq :session params))))
  19223. (if (not session) (org-edit-src-code)
  19224. ;; At a src-block with a session and function called with
  19225. ;; an ARG: switch to the buffer related to the inferior
  19226. ;; process.
  19227. (switch-to-buffer
  19228. (funcall (intern (concat "org-babel-prep-session:" lang))
  19229. session params))))))
  19230. (keyword
  19231. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19232. (org-open-link-from-string
  19233. (format "[[%s]]"
  19234. (expand-file-name
  19235. (let ((value (org-element-property :value element)))
  19236. (cond ((not (org-string-nw-p value))
  19237. (user-error "No file to edit"))
  19238. ((string-match "\\`\"\\(.*?\\)\"" value)
  19239. (match-string 1 value))
  19240. ((string-match "\\`[^ \t\"]\\S-*" value)
  19241. (match-string 0 value))
  19242. (t (user-error "No valid file specified")))))))
  19243. (user-error "No special environment to edit here")))
  19244. (table
  19245. (if (eq (org-element-property :type element) 'table.el)
  19246. (org-edit-table.el)
  19247. (call-interactively 'org-table-edit-formulas)))
  19248. ;; Only Org tables contain `table-row' type elements.
  19249. (table-row (call-interactively 'org-table-edit-formulas))
  19250. (example-block (org-edit-src-code))
  19251. (export-block (org-edit-export-block))
  19252. (fixed-width (org-edit-fixed-width-region))
  19253. (otherwise
  19254. ;; No notable element at point. Though, we may be at a link or
  19255. ;; a footnote reference, which are objects. Thus, scan deeper.
  19256. (let ((context (org-element-context element)))
  19257. (case (org-element-type context)
  19258. (link (call-interactively #'ffap))
  19259. (footnote-reference (org-edit-footnote-reference))
  19260. (t (user-error "No special environment to edit here"))))))))
  19261. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19262. (defun org-ctrl-c-ctrl-c (&optional arg)
  19263. "Set tags in headline, or update according to changed information at point.
  19264. This command does many different things, depending on context:
  19265. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19266. this is what we do.
  19267. - If the cursor is on a statistics cookie, update it.
  19268. - If the cursor is in a headline, prompt for tags and insert them
  19269. into the current line, aligned to `org-tags-column'. When called
  19270. with prefix arg, realign all tags in the current buffer.
  19271. - If the cursor is in one of the special #+KEYWORD lines, this
  19272. triggers scanning the buffer for these lines and updating the
  19273. information.
  19274. - If the cursor is inside a table, realign the table. This command
  19275. works even if the automatic table editor has been turned off.
  19276. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19277. the entire table.
  19278. - If the cursor is at a footnote reference or definition, jump to
  19279. the corresponding definition or references, respectively.
  19280. - If the cursor is a the beginning of a dynamic block, update it.
  19281. - If the current buffer is a capture buffer, close note and file it.
  19282. - If the cursor is on a <<<target>>>, update radio targets and
  19283. corresponding links in this buffer.
  19284. - If the cursor is on a numbered item in a plain list, renumber the
  19285. ordered list.
  19286. - If the cursor is on a checkbox, toggle it.
  19287. - If the cursor is on a code block, evaluate it. The variable
  19288. `org-confirm-babel-evaluate' can be used to control prompting
  19289. before code block evaluation, by default every code block
  19290. evaluation requires confirmation. Code block evaluation can be
  19291. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19292. (interactive "P")
  19293. (cond
  19294. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19295. org-occur-highlights)
  19296. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19297. (org-remove-occur-highlights)
  19298. (message "Temporary highlights/overlays removed from current buffer"))
  19299. ((and (local-variable-p 'org-finish-function (current-buffer))
  19300. (fboundp org-finish-function))
  19301. (funcall org-finish-function))
  19302. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19303. (t
  19304. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19305. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19306. (user-error "C-c C-c can do nothing useful at this location"))
  19307. (let* ((context (org-element-context))
  19308. (type (org-element-type context)))
  19309. (case type
  19310. ;; When at a link, act according to the parent instead.
  19311. (link (setq context (org-element-property :parent context))
  19312. (setq type (org-element-type context)))
  19313. ;; Unsupported object types: refer to the first supported
  19314. ;; element or object containing it.
  19315. ((bold code entity export-snippet inline-babel-call inline-src-block
  19316. italic latex-fragment line-break macro strike-through subscript
  19317. superscript underline verbatim)
  19318. (setq context
  19319. (org-element-lineage
  19320. context '(radio-target paragraph verse-block table-cell)))))
  19321. ;; For convenience: at the first line of a paragraph on the
  19322. ;; same line as an item, apply function on that item instead.
  19323. (when (eq type 'paragraph)
  19324. (let ((parent (org-element-property :parent context)))
  19325. (when (and (eq (org-element-type parent) 'item)
  19326. (= (line-beginning-position)
  19327. (org-element-property :begin parent)))
  19328. (setq context parent type 'item))))
  19329. ;; Act according to type of element or object at point.
  19330. (case type
  19331. (clock (org-clock-update-time-maybe))
  19332. (dynamic-block
  19333. (save-excursion
  19334. (goto-char (org-element-property :post-affiliated context))
  19335. (org-update-dblock)))
  19336. (footnote-definition
  19337. (goto-char (org-element-property :post-affiliated context))
  19338. (call-interactively 'org-footnote-action))
  19339. (footnote-reference (call-interactively 'org-footnote-action))
  19340. ((headline inlinetask)
  19341. (save-excursion (goto-char (org-element-property :begin context))
  19342. (call-interactively 'org-set-tags)))
  19343. (item
  19344. ;; At an item: a double C-u set checkbox to "[-]"
  19345. ;; unconditionally, whereas a single one will toggle its
  19346. ;; presence. Without a universal argument, if the item
  19347. ;; has a checkbox, toggle it. Otherwise repair the list.
  19348. (let* ((box (org-element-property :checkbox context))
  19349. (struct (org-element-property :structure context))
  19350. (old-struct (copy-tree struct))
  19351. (parents (org-list-parents-alist struct))
  19352. (prevs (org-list-prevs-alist struct))
  19353. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19354. (org-list-set-checkbox
  19355. (org-element-property :begin context) struct
  19356. (cond ((equal arg '(16)) "[-]")
  19357. ((and (not box) (equal arg '(4))) "[ ]")
  19358. ((or (not box) (equal arg '(4))) nil)
  19359. ((eq box 'on) "[ ]")
  19360. (t "[X]")))
  19361. ;; Mimic `org-list-write-struct' but with grabbing
  19362. ;; a return value from `org-list-struct-fix-box'.
  19363. (org-list-struct-fix-ind struct parents 2)
  19364. (org-list-struct-fix-item-end struct)
  19365. (org-list-struct-fix-bul struct prevs)
  19366. (org-list-struct-fix-ind struct parents)
  19367. (let ((block-item
  19368. (org-list-struct-fix-box struct parents prevs orderedp)))
  19369. (if (and box (equal struct old-struct))
  19370. (if (equal arg '(16))
  19371. (message "Checkboxes already reset")
  19372. (user-error "Cannot toggle this checkbox: %s"
  19373. (if (eq box 'on)
  19374. "all subitems checked"
  19375. "unchecked subitems")))
  19376. (org-list-struct-apply-struct struct old-struct)
  19377. (org-update-checkbox-count-maybe))
  19378. (when block-item
  19379. (message "Checkboxes were removed due to empty box at line %d"
  19380. (org-current-line block-item))))))
  19381. (keyword
  19382. (let ((org-inhibit-startup-visibility-stuff t)
  19383. (org-startup-align-all-tables nil))
  19384. (when (boundp 'org-table-coordinate-overlays)
  19385. (mapc 'delete-overlay org-table-coordinate-overlays)
  19386. (setq org-table-coordinate-overlays nil))
  19387. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19388. (message "Local setup has been refreshed"))
  19389. (plain-list
  19390. ;; At a plain list, with a double C-u argument, set
  19391. ;; checkboxes of each item to "[-]", whereas a single one
  19392. ;; will toggle their presence according to the state of the
  19393. ;; first item in the list. Without an argument, repair the
  19394. ;; list.
  19395. (let* ((begin (org-element-property :contents-begin context))
  19396. (beginm (move-marker (make-marker) begin))
  19397. (struct (org-element-property :structure context))
  19398. (old-struct (copy-tree struct))
  19399. (first-box (save-excursion
  19400. (goto-char begin)
  19401. (looking-at org-list-full-item-re)
  19402. (match-string-no-properties 3)))
  19403. (new-box (cond ((equal arg '(16)) "[-]")
  19404. ((equal arg '(4)) (unless first-box "[ ]"))
  19405. ((equal first-box "[X]") "[ ]")
  19406. (t "[X]"))))
  19407. (cond
  19408. (arg
  19409. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19410. (org-list-get-all-items
  19411. begin struct (org-list-prevs-alist struct))))
  19412. ((and first-box (eq (point) begin))
  19413. ;; For convenience, when point is at bol on the first
  19414. ;; item of the list and no argument is provided, simply
  19415. ;; toggle checkbox of that item, if any.
  19416. (org-list-set-checkbox begin struct new-box)))
  19417. (org-list-write-struct
  19418. struct (org-list-parents-alist struct) old-struct)
  19419. (org-update-checkbox-count-maybe)
  19420. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19421. ((property-drawer node-property)
  19422. (call-interactively 'org-property-action))
  19423. ((radio-target target)
  19424. (call-interactively 'org-update-radio-target-regexp))
  19425. (statistics-cookie
  19426. (call-interactively 'org-update-statistics-cookies))
  19427. ((table table-cell table-row)
  19428. ;; At a table, recalculate every field and align it. Also
  19429. ;; send the table if necessary. If the table has
  19430. ;; a `table.el' type, just give up. At a table row or
  19431. ;; cell, maybe recalculate line but always align table.
  19432. (if (eq (org-element-property :type context) 'table.el)
  19433. (message "Use C-c ' to edit table.el tables")
  19434. (let ((org-enable-table-editor t))
  19435. (if (or (eq type 'table)
  19436. ;; Check if point is at a TBLFM line.
  19437. (and (eq type 'table-row)
  19438. (= (point) (org-element-property :end context))))
  19439. (save-excursion
  19440. (if (org-at-TBLFM-p)
  19441. (progn (require 'org-table)
  19442. (org-table-calc-current-TBLFM))
  19443. (goto-char (org-element-property :contents-begin context))
  19444. (org-call-with-arg 'org-table-recalculate (or arg t))
  19445. (orgtbl-send-table 'maybe)))
  19446. (org-table-maybe-eval-formula)
  19447. (cond (arg (call-interactively 'org-table-recalculate))
  19448. ((org-table-maybe-recalculate-line))
  19449. (t (org-table-align)))))))
  19450. (timestamp (org-timestamp-change 0 'day))
  19451. (otherwise
  19452. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19453. (user-error
  19454. "C-c C-c can do nothing useful at this location")))))))))
  19455. (defun org-mode-restart ()
  19456. (interactive)
  19457. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19458. (funcall major-mode)
  19459. (hack-local-variables)
  19460. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19461. (org-indent-mode -1)))
  19462. (message "%s restarted" major-mode))
  19463. (defun org-kill-note-or-show-branches ()
  19464. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19465. (interactive)
  19466. (if (not org-finish-function)
  19467. (progn
  19468. (hide-subtree)
  19469. (call-interactively 'show-branches))
  19470. (let ((org-note-abort t))
  19471. (funcall org-finish-function))))
  19472. (defun org-delete-indentation (&optional ARG)
  19473. "Join current line to previous and fix whitespace at join.
  19474. If previous line is a headline add to headline title. Otherwise
  19475. the function calls `delete-indentation'.
  19476. With argument, join this line to following line."
  19477. (interactive "*P")
  19478. (if (save-excursion
  19479. (if ARG (beginning-of-line)
  19480. (forward-line -1))
  19481. (looking-at org-complex-heading-regexp))
  19482. ;; At headline.
  19483. (let ((tags-column (when (match-beginning 5)
  19484. (save-excursion (goto-char (match-beginning 5))
  19485. (current-column))))
  19486. (string (concat " " (progn (when ARG (forward-line 1))
  19487. (org-trim (delete-and-extract-region
  19488. (line-beginning-position)
  19489. (line-end-position)))))))
  19490. (unless (bobp) (delete-region (point) (1- (point))))
  19491. (goto-char (or (match-end 4)
  19492. (match-beginning 5)
  19493. (match-end 0)))
  19494. (skip-chars-backward " \t")
  19495. (save-excursion (insert string))
  19496. ;; Adjust alignment of tags.
  19497. (when tags-column
  19498. (org-align-tags-here (if org-auto-align-tags
  19499. org-tags-column
  19500. tags-column))))
  19501. (delete-indentation ARG)))
  19502. (defun org-open-line (n)
  19503. "Insert a new row in tables, call `open-line' elsewhere.
  19504. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19505. (interactive "*p")
  19506. (cond
  19507. ((not org-special-ctrl-o)
  19508. (open-line n))
  19509. ((org-at-table-p)
  19510. (org-table-insert-row))
  19511. (t
  19512. (open-line n))))
  19513. (defun org-return (&optional indent)
  19514. "Goto next table row or insert a newline.
  19515. Calls `org-table-next-row' or `newline', depending on context.
  19516. When optional INDENT argument is non-nil, call
  19517. `newline-and-indent' instead of `newline'.
  19518. When `org-return-follows-link' is non-nil and point is on
  19519. a timestamp or a link, call `org-open-at-point'. However, it
  19520. will not happen if point is in a table or on a \"dead\"
  19521. object (e.g., within a comment). In these case, you need to use
  19522. `org-open-at-point' directly."
  19523. (interactive)
  19524. (let* ((context (if org-return-follows-link (org-element-context)
  19525. (org-element-at-point)))
  19526. (type (org-element-type context)))
  19527. (cond
  19528. ;; In a table, call `org-table-next-row'.
  19529. ((or (and (eq type 'table)
  19530. (>= (point) (org-element-property :contents-begin context))
  19531. (< (point) (org-element-property :contents-end context)))
  19532. (org-element-lineage context '(table-row table-cell) t))
  19533. (org-table-justify-field-maybe)
  19534. (call-interactively #'org-table-next-row))
  19535. ;; On a link or a timestamp but not on white spaces after it,
  19536. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19537. ((and org-return-follows-link
  19538. (memq type '(link timestamp))
  19539. (< (point)
  19540. (save-excursion (goto-char (org-element-property :end context))
  19541. (skip-chars-backward " \t")
  19542. (point))))
  19543. (call-interactively #'org-open-at-point))
  19544. ;; Insert newline in heading, but preserve tags.
  19545. ((and (not (bolp))
  19546. (save-excursion (beginning-of-line)
  19547. (looking-at org-complex-heading-regexp)))
  19548. ;; At headline. Split line. However, if point is on keyword,
  19549. ;; priority cookie or tags, do not break any of them: add
  19550. ;; a newline after the headline instead.
  19551. (let ((tags-column (and (match-beginning 5)
  19552. (save-excursion (goto-char (match-beginning 5))
  19553. (current-column))))
  19554. (string
  19555. (when (and (match-end 4)
  19556. (>= (point)
  19557. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19558. (<= (point) (match-end 4)))
  19559. (delete-and-extract-region (point) (match-end 4)))))
  19560. (when (and tags-column string) ; Adjust tag alignment.
  19561. (org-align-tags-here
  19562. (if org-auto-align-tags org-tags-column tags-column)))
  19563. (end-of-line)
  19564. (org-show-entry)
  19565. (if indent (newline-and-indent) (newline))
  19566. (when string (save-excursion (insert (org-trim string))))))
  19567. ;; In a list, make sure indenting keeps trailing text within.
  19568. ((and indent
  19569. (not (eolp))
  19570. (org-element-lineage context '(item)))
  19571. (let ((trailing-data
  19572. (delete-and-extract-region (point) (line-end-position))))
  19573. (newline-and-indent)
  19574. (save-excursion (insert trailing-data))))
  19575. (t (if indent (newline-and-indent) (newline))))))
  19576. (defun org-return-indent ()
  19577. "Goto next table row or insert a newline and indent.
  19578. Calls `org-table-next-row' or `newline-and-indent', depending on
  19579. context. See the individual commands for more information."
  19580. (interactive)
  19581. (org-return t))
  19582. (defun org-ctrl-c-star ()
  19583. "Compute table, or change heading status of lines.
  19584. Calls `org-table-recalculate' or `org-toggle-heading',
  19585. depending on context."
  19586. (interactive)
  19587. (cond
  19588. ((org-at-table-p)
  19589. (call-interactively 'org-table-recalculate))
  19590. (t
  19591. ;; Convert all lines in region to list items
  19592. (call-interactively 'org-toggle-heading))))
  19593. (defun org-ctrl-c-minus ()
  19594. "Insert separator line in table or modify bullet status of line.
  19595. Also turns a plain line or a region of lines into list items.
  19596. Calls `org-table-insert-hline', `org-toggle-item', or
  19597. `org-cycle-list-bullet', depending on context."
  19598. (interactive)
  19599. (cond
  19600. ((org-at-table-p)
  19601. (call-interactively 'org-table-insert-hline))
  19602. ((org-region-active-p)
  19603. (call-interactively 'org-toggle-item))
  19604. ((org-in-item-p)
  19605. (call-interactively 'org-cycle-list-bullet))
  19606. (t
  19607. (call-interactively 'org-toggle-item))))
  19608. (defun org-toggle-item (arg)
  19609. "Convert headings or normal lines to items, items to normal lines.
  19610. If there is no active region, only the current line is considered.
  19611. If the first non blank line in the region is a headline, convert
  19612. all headlines to items, shifting text accordingly.
  19613. If it is an item, convert all items to normal lines.
  19614. If it is normal text, change region into a list of items.
  19615. With a prefix argument ARG, change the region in a single item."
  19616. (interactive "P")
  19617. (let ((shift-text
  19618. (function
  19619. ;; Shift text in current section to IND, from point to END.
  19620. ;; The function leaves point to END line.
  19621. (lambda (ind end)
  19622. (let ((min-i 1000) (end (copy-marker end)))
  19623. ;; First determine the minimum indentation (MIN-I) of
  19624. ;; the text.
  19625. (save-excursion
  19626. (catch 'exit
  19627. (while (< (point) end)
  19628. (let ((i (org-get-indentation)))
  19629. (cond
  19630. ;; Skip blank lines and inline tasks.
  19631. ((looking-at "^[ \t]*$"))
  19632. ((looking-at org-outline-regexp-bol))
  19633. ;; We can't find less than 0 indentation.
  19634. ((zerop i) (throw 'exit (setq min-i 0)))
  19635. ((< i min-i) (setq min-i i))))
  19636. (forward-line))))
  19637. ;; Then indent each line so that a line indented to
  19638. ;; MIN-I becomes indented to IND. Ignore blank lines
  19639. ;; and inline tasks in the process.
  19640. (let ((delta (- ind min-i)))
  19641. (while (< (point) end)
  19642. (unless (or (looking-at "^[ \t]*$")
  19643. (looking-at org-outline-regexp-bol))
  19644. (org-indent-line-to (+ (org-get-indentation) delta)))
  19645. (forward-line)))))))
  19646. (skip-blanks
  19647. (function
  19648. ;; Return beginning of first non-blank line, starting from
  19649. ;; line at POS.
  19650. (lambda (pos)
  19651. (save-excursion
  19652. (goto-char pos)
  19653. (skip-chars-forward " \r\t\n")
  19654. (point-at-bol)))))
  19655. beg end)
  19656. ;; Determine boundaries of changes.
  19657. (if (org-region-active-p)
  19658. (setq beg (funcall skip-blanks (region-beginning))
  19659. end (copy-marker (region-end)))
  19660. (setq beg (funcall skip-blanks (point-at-bol))
  19661. end (copy-marker (point-at-eol))))
  19662. ;; Depending on the starting line, choose an action on the text
  19663. ;; between BEG and END.
  19664. (org-with-limited-levels
  19665. (save-excursion
  19666. (goto-char beg)
  19667. (cond
  19668. ;; Case 1. Start at an item: de-itemize. Note that it only
  19669. ;; happens when a region is active: `org-ctrl-c-minus'
  19670. ;; would call `org-cycle-list-bullet' otherwise.
  19671. ((org-at-item-p)
  19672. (while (< (point) end)
  19673. (when (org-at-item-p)
  19674. (skip-chars-forward " \t")
  19675. (delete-region (point) (match-end 0)))
  19676. (forward-line)))
  19677. ;; Case 2. Start at an heading: convert to items.
  19678. ((org-at-heading-p)
  19679. (let* ((bul (org-list-bullet-string "-"))
  19680. (bul-len (length bul))
  19681. (done (org-entry-is-done-p))
  19682. (todo (org-entry-is-todo-p))
  19683. ;; Indentation of the first heading. It should be
  19684. ;; relative to the indentation of its parent, if any.
  19685. (start-ind (save-excursion
  19686. (cond
  19687. ((not org-adapt-indentation) 0)
  19688. ((not (outline-previous-heading)) 0)
  19689. (t (length (match-string 0))))))
  19690. ;; Level of first heading. Further headings will be
  19691. ;; compared to it to determine hierarchy in the list.
  19692. (ref-level (org-reduced-level (org-outline-level))))
  19693. (when (or done todo) (org-todo ""))
  19694. (while (< (point) end)
  19695. (let* ((level (org-reduced-level (org-outline-level)))
  19696. (delta (max 0 (- level ref-level))))
  19697. ;; If current headline is less indented than the first
  19698. ;; one, set it as reference, in order to preserve
  19699. ;; subtrees.
  19700. (when (< level ref-level) (setq ref-level level))
  19701. (replace-match bul t t)
  19702. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19703. (when (or done todo)
  19704. (let* ((struct (org-list-struct))
  19705. (old (copy-tree struct)))
  19706. (org-list-set-checkbox (line-beginning-position)
  19707. struct
  19708. (if done "[X]" "[ ]"))
  19709. (org-list-write-struct struct
  19710. (org-list-parents-alist struct)
  19711. old)))
  19712. ;; Ensure all text down to END (or SECTION-END) belongs
  19713. ;; to the newly created item.
  19714. (let ((section-end (save-excursion
  19715. (or (outline-next-heading) (point)))))
  19716. (forward-line)
  19717. (funcall shift-text
  19718. (+ start-ind (* (1+ delta) bul-len))
  19719. (min end section-end)))))))
  19720. ;; Case 3. Normal line with ARG: make the first line of region
  19721. ;; an item, and shift indentation of others lines to
  19722. ;; set them as item's body.
  19723. (arg (let* ((bul (org-list-bullet-string "-"))
  19724. (bul-len (length bul))
  19725. (ref-ind (org-get-indentation)))
  19726. (skip-chars-forward " \t")
  19727. (insert bul)
  19728. (forward-line)
  19729. (while (< (point) end)
  19730. ;; Ensure that lines less indented than first one
  19731. ;; still get included in item body.
  19732. (funcall shift-text
  19733. (+ ref-ind bul-len)
  19734. (min end (save-excursion (or (outline-next-heading)
  19735. (point)))))
  19736. (forward-line))))
  19737. ;; Case 4. Normal line without ARG: turn each non-item line
  19738. ;; into an item.
  19739. (t
  19740. (while (< (point) end)
  19741. (unless (or (org-at-heading-p) (org-at-item-p))
  19742. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19743. (replace-match
  19744. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19745. (forward-line))))))))
  19746. (defun org-toggle-heading (&optional nstars)
  19747. "Convert headings to normal text, or items or text to headings.
  19748. If there is no active region, only convert the current line.
  19749. With a \\[universal-argument] prefix, convert the whole list at
  19750. point into heading.
  19751. In a region:
  19752. - If the first non blank line is a headline, remove the stars
  19753. from all headlines in the region.
  19754. - If it is a normal line, turn each and every normal line (i.e.,
  19755. not an heading or an item) in the region into headings. If you
  19756. want to convert only the first line of this region, use one
  19757. universal prefix argument.
  19758. - If it is a plain list item, turn all plain list items into headings.
  19759. When converting a line into a heading, the number of stars is chosen
  19760. such that the lines become children of the current entry. However,
  19761. when a numeric prefix argument is given, its value determines the
  19762. number of stars to add."
  19763. (interactive "P")
  19764. (let ((skip-blanks
  19765. (function
  19766. ;; Return beginning of first non-blank line, starting from
  19767. ;; line at POS.
  19768. (lambda (pos)
  19769. (save-excursion
  19770. (goto-char pos)
  19771. (while (org-at-comment-p) (forward-line))
  19772. (skip-chars-forward " \r\t\n")
  19773. (point-at-bol)))))
  19774. beg end toggled)
  19775. ;; Determine boundaries of changes. If a universal prefix has
  19776. ;; been given, put the list in a region. If region ends at a bol,
  19777. ;; do not consider the last line to be in the region.
  19778. (when (and current-prefix-arg (org-at-item-p))
  19779. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19780. (org-mark-element))
  19781. (if (org-region-active-p)
  19782. (setq beg (funcall skip-blanks (region-beginning))
  19783. end (copy-marker (save-excursion
  19784. (goto-char (region-end))
  19785. (if (bolp) (point) (point-at-eol)))))
  19786. (setq beg (funcall skip-blanks (point-at-bol))
  19787. end (copy-marker (point-at-eol))))
  19788. ;; Ensure inline tasks don't count as headings.
  19789. (org-with-limited-levels
  19790. (save-excursion
  19791. (goto-char beg)
  19792. (cond
  19793. ;; Case 1. Started at an heading: de-star headings.
  19794. ((org-at-heading-p)
  19795. (while (< (point) end)
  19796. (when (org-at-heading-p t)
  19797. (looking-at org-outline-regexp) (replace-match "")
  19798. (setq toggled t))
  19799. (forward-line)))
  19800. ;; Case 2. Started at an item: change items into headlines.
  19801. ;; One star will be added by `org-list-to-subtree'.
  19802. ((org-at-item-p)
  19803. (while (< (point) end)
  19804. (when (org-at-item-p)
  19805. ;; Pay attention to cases when region ends before list.
  19806. (let* ((struct (org-list-struct))
  19807. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19808. (save-restriction
  19809. (narrow-to-region (point) list-end)
  19810. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19811. (setq toggled t))
  19812. (forward-line)))
  19813. ;; Case 3. Started at normal text: make every line an heading,
  19814. ;; skipping headlines and items.
  19815. (t (let* ((stars
  19816. (make-string
  19817. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19818. (add-stars
  19819. (cond (nstars "") ; stars from prefix only
  19820. ((equal stars "") "*") ; before first heading
  19821. (org-odd-levels-only "**") ; inside heading, odd
  19822. (t "*"))) ; inside heading, oddeven
  19823. (rpl (concat stars add-stars " "))
  19824. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19825. (while (< (point) (if (equal nstars '(4)) lend end))
  19826. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19827. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19828. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19829. (forward-line)))))))
  19830. (unless toggled (message "Cannot toggle heading from here"))))
  19831. (defun org-meta-return (&optional _arg)
  19832. "Insert a new heading or wrap a region in a table.
  19833. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19834. on context. See the individual commands for more information."
  19835. (interactive)
  19836. (org-check-before-invisible-edit 'insert)
  19837. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19838. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19839. #'org-insert-heading))))
  19840. ;;; Menu entries
  19841. (defsubst org-in-subtree-not-table-p ()
  19842. "Are we in a subtree and not in a table?"
  19843. (and (not (org-before-first-heading-p))
  19844. (not (org-at-table-p))))
  19845. ;; Define the Org-mode menus
  19846. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19847. '("Tbl"
  19848. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19849. ["Next Field" org-cycle (org-at-table-p)]
  19850. ["Previous Field" org-shifttab (org-at-table-p)]
  19851. ["Next Row" org-return (org-at-table-p)]
  19852. "--"
  19853. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19854. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19855. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19856. "--"
  19857. ("Column"
  19858. ["Move Column Left" org-metaleft (org-at-table-p)]
  19859. ["Move Column Right" org-metaright (org-at-table-p)]
  19860. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19861. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19862. ("Row"
  19863. ["Move Row Up" org-metaup (org-at-table-p)]
  19864. ["Move Row Down" org-metadown (org-at-table-p)]
  19865. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19866. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19867. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19868. "--"
  19869. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19870. ("Rectangle"
  19871. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19872. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19873. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19874. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19875. "--"
  19876. ("Calculate"
  19877. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19878. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19879. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19880. "--"
  19881. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19882. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19883. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19884. "--"
  19885. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19886. "--"
  19887. ["Sum Column/Rectangle" org-table-sum
  19888. (or (org-at-table-p) (org-region-active-p))]
  19889. ["Which Column?" org-table-current-column (org-at-table-p)])
  19890. ["Debug Formulas"
  19891. org-table-toggle-formula-debugger
  19892. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19893. ["Show Col/Row Numbers"
  19894. org-table-toggle-coordinate-overlays
  19895. :style toggle
  19896. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19897. "--"
  19898. ["Create" org-table-create (and (not (org-at-table-p))
  19899. org-enable-table-editor)]
  19900. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19901. ["Import from File" org-table-import (not (org-at-table-p))]
  19902. ["Export to File" org-table-export (org-at-table-p)]
  19903. "--"
  19904. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19905. "--"
  19906. ("Plot"
  19907. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19908. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19909. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19910. '("Org"
  19911. ("Show/Hide"
  19912. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19913. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19914. ["Sparse Tree..." org-sparse-tree t]
  19915. ["Reveal Context" org-reveal t]
  19916. ["Show All" show-all t]
  19917. "--"
  19918. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19919. "--"
  19920. ["New Heading" org-insert-heading t]
  19921. ("Navigate Headings"
  19922. ["Up" outline-up-heading t]
  19923. ["Next" outline-next-visible-heading t]
  19924. ["Previous" outline-previous-visible-heading t]
  19925. ["Next Same Level" outline-forward-same-level t]
  19926. ["Previous Same Level" outline-backward-same-level t]
  19927. "--"
  19928. ["Jump" org-goto t])
  19929. ("Edit Structure"
  19930. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19931. "--"
  19932. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19933. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19934. "--"
  19935. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19936. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19937. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19938. "--"
  19939. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19940. "--"
  19941. ["Copy visible text" org-copy-visible t]
  19942. "--"
  19943. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19944. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19945. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19946. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19947. "--"
  19948. ["Sort Region/Children" org-sort t]
  19949. "--"
  19950. ["Convert to odd levels" org-convert-to-odd-levels t]
  19951. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19952. ("Editing"
  19953. ["Emphasis..." org-emphasize t]
  19954. ["Edit Source Example" org-edit-special t]
  19955. "--"
  19956. ["Footnote new/jump" org-footnote-action t]
  19957. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19958. ("Archive"
  19959. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19960. "--"
  19961. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19962. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19963. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19964. )
  19965. "--"
  19966. ("Hyperlinks"
  19967. ["Store Link (Global)" org-store-link t]
  19968. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19969. ["Insert Link" org-insert-link t]
  19970. ["Follow Link" org-open-at-point t]
  19971. "--"
  19972. ["Next link" org-next-link t]
  19973. ["Previous link" org-previous-link t]
  19974. "--"
  19975. ["Descriptive Links"
  19976. org-toggle-link-display
  19977. :style radio
  19978. :selected org-descriptive-links
  19979. ]
  19980. ["Literal Links"
  19981. org-toggle-link-display
  19982. :style radio
  19983. :selected (not org-descriptive-links)])
  19984. "--"
  19985. ("TODO Lists"
  19986. ["TODO/DONE/-" org-todo t]
  19987. ("Select keyword"
  19988. ["Next keyword" org-shiftright (org-at-heading-p)]
  19989. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19990. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19991. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19992. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19993. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19994. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19995. "--"
  19996. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19997. :selected org-enforce-todo-dependencies :style toggle :active t]
  19998. "Settings for tree at point"
  19999. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20000. :selected (org-entry-get nil "ORDERED")
  20001. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20002. ["Do Children parallel" org-toggle-ordered-property :style radio
  20003. :selected (not (org-entry-get nil "ORDERED"))
  20004. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20005. "--"
  20006. ["Set Priority" org-priority t]
  20007. ["Priority Up" org-shiftup t]
  20008. ["Priority Down" org-shiftdown t]
  20009. "--"
  20010. ["Get news from all feeds" org-feed-update-all t]
  20011. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20012. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20013. ("TAGS and Properties"
  20014. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20015. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20016. "--"
  20017. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20018. ["Column view of properties" org-columns t]
  20019. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20020. ("Dates and Scheduling"
  20021. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20022. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20023. ("Change Date"
  20024. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20025. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20026. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20027. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20028. ["Compute Time Range" org-evaluate-time-range t]
  20029. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20030. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20031. "--"
  20032. ["Custom time format" org-toggle-time-stamp-overlays
  20033. :style radio :selected org-display-custom-times]
  20034. "--"
  20035. ["Goto Calendar" org-goto-calendar t]
  20036. ["Date from Calendar" org-date-from-calendar t]
  20037. "--"
  20038. ["Start/Restart Timer" org-timer-start t]
  20039. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20040. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20041. ["Insert Timer String" org-timer t]
  20042. ["Insert Timer Item" org-timer-item t])
  20043. ("Logging work"
  20044. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20045. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20046. ["Clock out" org-clock-out t]
  20047. ["Clock cancel" org-clock-cancel t]
  20048. "--"
  20049. ["Mark as default task" org-clock-mark-default-task t]
  20050. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20051. ["Goto running clock" org-clock-goto t]
  20052. "--"
  20053. ["Display times" org-clock-display t]
  20054. ["Create clock table" org-clock-report t]
  20055. "--"
  20056. ["Record DONE time"
  20057. (progn (setq org-log-done (not org-log-done))
  20058. (message "Switching to %s will %s record a timestamp"
  20059. (car org-done-keywords)
  20060. (if org-log-done "automatically" "not")))
  20061. :style toggle :selected org-log-done])
  20062. "--"
  20063. ["Agenda Command..." org-agenda t]
  20064. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20065. ("File List for Agenda")
  20066. ("Special views current file"
  20067. ["TODO Tree" org-show-todo-tree t]
  20068. ["Check Deadlines" org-check-deadlines t]
  20069. ["Timeline" org-timeline t]
  20070. ["Tags/Property tree" org-match-sparse-tree t])
  20071. "--"
  20072. ["Export/Publish..." org-export-dispatch t]
  20073. ("LaTeX"
  20074. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20075. :selected org-cdlatex-mode]
  20076. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20077. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20078. ["Modify math symbol" org-cdlatex-math-modify
  20079. (org-inside-LaTeX-fragment-p)]
  20080. ["Insert citation" org-reftex-citation t]
  20081. "--"
  20082. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20083. "--"
  20084. ("MobileOrg"
  20085. ["Push Files and Views" org-mobile-push t]
  20086. ["Get Captured and Flagged" org-mobile-pull t]
  20087. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20088. "--"
  20089. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20090. "--"
  20091. ("Documentation"
  20092. ["Show Version" org-version t]
  20093. ["Info Documentation" org-info t])
  20094. ("Customize"
  20095. ["Browse Org Group" org-customize t]
  20096. "--"
  20097. ["Expand This Menu" org-create-customize-menu
  20098. (fboundp 'customize-menu-create)])
  20099. ["Send bug report" org-submit-bug-report t]
  20100. "--"
  20101. ("Refresh/Reload"
  20102. ["Refresh setup current buffer" org-mode-restart t]
  20103. ["Reload Org (after update)" org-reload t]
  20104. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20105. ))
  20106. (defun org-info (&optional node)
  20107. "Read documentation for Org-mode in the info system.
  20108. With optional NODE, go directly to that node."
  20109. (interactive)
  20110. (info (format "(org)%s" (or node ""))))
  20111. ;;;###autoload
  20112. (defun org-submit-bug-report ()
  20113. "Submit a bug report on Org-mode via mail.
  20114. Don't hesitate to report any problems or inaccurate documentation.
  20115. If you don't have setup sending mail from (X)Emacs, please copy the
  20116. output buffer into your mail program, as it gives us important
  20117. information about your Org-mode version and configuration."
  20118. (interactive)
  20119. (require 'reporter)
  20120. (defvar reporter-prompt-for-summary-p)
  20121. (org-load-modules-maybe)
  20122. (org-require-autoloaded-modules)
  20123. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20124. (reporter-submit-bug-report
  20125. "emacs-orgmode@gnu.org"
  20126. (org-version nil 'full)
  20127. (let (list)
  20128. (save-window-excursion
  20129. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20130. (delete-other-windows)
  20131. (erase-buffer)
  20132. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20133. We would like to add your full Org-mode and Outline configuration to the
  20134. bug report. This greatly simplifies the work of the maintainer and
  20135. other experts on the mailing list.
  20136. HOWEVER, some variables you have customized may contain private
  20137. information. The names of customers, colleagues, or friends, might
  20138. appear in the form of file names, tags, todo states, or search strings.
  20139. If you answer yes to the prompt, you might want to check and remove
  20140. such private information before sending the email.")
  20141. (add-text-properties (point-min) (point-max) '(face org-warning))
  20142. (when (yes-or-no-p "Include your Org-mode configuration ")
  20143. (mapatoms
  20144. (lambda (v)
  20145. (and (boundp v)
  20146. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20147. (or (and (symbol-value v)
  20148. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20149. (and
  20150. (get v 'custom-type) (get v 'standard-value)
  20151. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20152. (push v list)))))
  20153. (kill-buffer (get-buffer "*Warn about privacy*"))
  20154. list))
  20155. nil nil
  20156. "Remember to cover the basics, that is, what you expected to happen and
  20157. what in fact did happen. You don't know how to make a good report? See
  20158. http://orgmode.org/manual/Feedback.html#Feedback
  20159. Your bug report will be posted to the Org-mode mailing list.
  20160. ------------------------------------------------------------------------")
  20161. (save-excursion
  20162. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20163. (replace-match "\\1Bug: \\3 [\\2]")))))
  20164. (defun org-install-agenda-files-menu ()
  20165. (let ((bl (buffer-list)))
  20166. (save-excursion
  20167. (while bl
  20168. (set-buffer (pop bl))
  20169. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20170. (when (derived-mode-p 'org-mode)
  20171. (easy-menu-change
  20172. '("Org") "File List for Agenda"
  20173. (append
  20174. (list
  20175. ["Edit File List" (org-edit-agenda-file-list) t]
  20176. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20177. ["Remove Current File from List" org-remove-file t]
  20178. ["Cycle through agenda files" org-cycle-agenda-files t]
  20179. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20180. "--")
  20181. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20182. ;;;; Documentation
  20183. (defun org-require-autoloaded-modules ()
  20184. (interactive)
  20185. (mapc 'require
  20186. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20187. org-table org-timer)))
  20188. ;;;###autoload
  20189. (defun org-reload (&optional uncompiled)
  20190. "Reload all org lisp files.
  20191. With prefix arg UNCOMPILED, load the uncompiled versions."
  20192. (interactive "P")
  20193. (require 'loadhist)
  20194. (let* ((org-dir (org-find-library-dir "org"))
  20195. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20196. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20197. (remove-re (mapconcat 'identity
  20198. (mapcar (lambda (f) (concat "^" f "$"))
  20199. (list (if (featurep 'xemacs)
  20200. "org-colview"
  20201. "org-colview-xemacs")
  20202. "org" "org-loaddefs" "org-version"))
  20203. "\\|"))
  20204. (feats (delete-dups
  20205. (mapcar 'file-name-sans-extension
  20206. (mapcar 'file-name-nondirectory
  20207. (delq nil
  20208. (mapcar 'feature-file
  20209. features))))))
  20210. (lfeat (append
  20211. (sort
  20212. (setq feats
  20213. (delq nil (mapcar
  20214. (lambda (f)
  20215. (if (and (string-match feature-re f)
  20216. (not (string-match remove-re f)))
  20217. f nil))
  20218. feats)))
  20219. 'string-lessp)
  20220. (list "org-version" "org")))
  20221. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20222. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20223. load-uncore load-misses)
  20224. (setq load-misses
  20225. (delq 't
  20226. (mapcar (lambda (f)
  20227. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20228. (and (string= org-dir contrib-dir)
  20229. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20230. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20231. (add-to-list 'load-uncore f 'append)
  20232. 't)
  20233. f))
  20234. lfeat)))
  20235. (if load-uncore
  20236. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20237. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20238. (if load-misses
  20239. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20240. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20241. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20242. ;;;###autoload
  20243. (defun org-customize ()
  20244. "Call the customize function with org as argument."
  20245. (interactive)
  20246. (org-load-modules-maybe)
  20247. (org-require-autoloaded-modules)
  20248. (customize-browse 'org))
  20249. (defun org-create-customize-menu ()
  20250. "Create a full customization menu for Org-mode, insert it into the menu."
  20251. (interactive)
  20252. (org-load-modules-maybe)
  20253. (org-require-autoloaded-modules)
  20254. (if (fboundp 'customize-menu-create)
  20255. (progn
  20256. (easy-menu-change
  20257. '("Org") "Customize"
  20258. `(["Browse Org group" org-customize t]
  20259. "--"
  20260. ,(customize-menu-create 'org)
  20261. ["Set" Custom-set t]
  20262. ["Save" Custom-save t]
  20263. ["Reset to Current" Custom-reset-current t]
  20264. ["Reset to Saved" Custom-reset-saved t]
  20265. ["Reset to Standard Settings" Custom-reset-standard t]))
  20266. (message "\"Org\"-menu now contains full customization menu"))
  20267. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20268. ;;;; Miscellaneous stuff
  20269. ;;; Generally useful functions
  20270. (defsubst org-get-at-eol (property n)
  20271. "Get text property PROPERTY at the end of line less N characters."
  20272. (get-text-property (- (point-at-eol) n) property))
  20273. (defun org-find-text-property-in-string (prop s)
  20274. "Return the first non-nil value of property PROP in string S."
  20275. (or (get-text-property 0 prop s)
  20276. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20277. prop s)))
  20278. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20279. "Display the given MESSAGE as a warning."
  20280. (if (fboundp 'display-warning)
  20281. (display-warning 'org message
  20282. (if (featurep 'xemacs) 'warning :warning))
  20283. (let ((buf (get-buffer-create "*Org warnings*")))
  20284. (with-current-buffer buf
  20285. (goto-char (point-max))
  20286. (insert "Warning (Org): " message)
  20287. (unless (bolp)
  20288. (newline)))
  20289. (display-buffer buf)
  20290. (sit-for 0))))
  20291. (defun org-eval (form)
  20292. "Eval FORM and return result."
  20293. (condition-case error
  20294. (eval form)
  20295. (error (format "%%![Error: %s]" error))))
  20296. (defun org-in-clocktable-p ()
  20297. "Check if the cursor is in a clocktable."
  20298. (let ((pos (point)) start)
  20299. (save-excursion
  20300. (end-of-line 1)
  20301. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20302. (setq start (match-beginning 0))
  20303. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20304. (>= (match-end 0) pos)
  20305. start))))
  20306. (defun org-in-verbatim-emphasis ()
  20307. (save-match-data
  20308. (and (org-in-regexp org-emph-re 2)
  20309. (>= (point) (match-beginning 3))
  20310. (<= (point) (match-end 4))
  20311. (member (match-string 3) '("=" "~")))))
  20312. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20313. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20314. (if (and marker (marker-buffer marker)
  20315. (buffer-live-p (marker-buffer marker)))
  20316. (progn
  20317. (org-pop-to-buffer-same-window (marker-buffer marker))
  20318. (if (or (> marker (point-max)) (< marker (point-min)))
  20319. (widen))
  20320. (goto-char marker)
  20321. (org-show-context 'org-goto))
  20322. (if bookmark
  20323. (bookmark-jump bookmark)
  20324. (error "Cannot find location"))))
  20325. (defun org-quote-csv-field (s)
  20326. "Quote field for inclusion in CSV material."
  20327. (if (string-match "[\",]" s)
  20328. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20329. s))
  20330. (defun org-force-self-insert (N)
  20331. "Needed to enforce self-insert under remapping."
  20332. (interactive "p")
  20333. (self-insert-command N))
  20334. (defun org-string-width (s)
  20335. "Compute width of string, ignoring invisible characters.
  20336. This ignores character with invisibility property `org-link', and also
  20337. characters with property `org-cwidth', because these will become invisible
  20338. upon the next fontification round."
  20339. (let (b l)
  20340. (when (or (eq t buffer-invisibility-spec)
  20341. (assq 'org-link buffer-invisibility-spec))
  20342. (while (setq b (text-property-any 0 (length s)
  20343. 'invisible 'org-link s))
  20344. (setq s (concat (substring s 0 b)
  20345. (substring s (or (next-single-property-change
  20346. b 'invisible s)
  20347. (length s)))))))
  20348. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20349. (setq s (concat (substring s 0 b)
  20350. (substring s (or (next-single-property-change
  20351. b 'org-cwidth s)
  20352. (length s))))))
  20353. (setq l (string-width s) b -1)
  20354. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20355. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20356. l))
  20357. (defun org-shorten-string (s maxlength)
  20358. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20359. If the string is shorter or has length MAXLENGTH, just return the
  20360. original string. If it is longer, the functions finds a space in the
  20361. string, breaks this string off at that locations and adds three dots
  20362. as ellipsis. Including the ellipsis, the string will not be longer
  20363. than MAXLENGTH. If finding a good breaking point in the string does
  20364. not work, the string is just chopped off in the middle of a word
  20365. if necessary."
  20366. (if (<= (length s) maxlength)
  20367. s
  20368. (let* ((n (max (- maxlength 4) 1))
  20369. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20370. (if (string-match re s)
  20371. (concat (match-string 1 s) "...")
  20372. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20373. (defun org-get-indentation (&optional line)
  20374. "Get the indentation of the current line, interpreting tabs.
  20375. When LINE is given, assume it represents a line and compute its indentation."
  20376. (if line
  20377. (if (string-match "^ *" (org-remove-tabs line))
  20378. (match-end 0))
  20379. (save-excursion
  20380. (beginning-of-line 1)
  20381. (skip-chars-forward " \t")
  20382. (current-column))))
  20383. (defun org-get-string-indentation (s)
  20384. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20385. (let ((n -1) (i 0) (w tab-width) c)
  20386. (catch 'exit
  20387. (while (< (setq n (1+ n)) (length s))
  20388. (setq c (aref s n))
  20389. (cond ((= c ?\ ) (setq i (1+ i)))
  20390. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20391. (t (throw 'exit t)))))
  20392. i))
  20393. (defun org-remove-tabs (s &optional width)
  20394. "Replace tabulators in S with spaces.
  20395. Assumes that s is a single line, starting in column 0."
  20396. (setq width (or width tab-width))
  20397. (while (string-match "\t" s)
  20398. (setq s (replace-match
  20399. (make-string
  20400. (- (* width (/ (+ (match-beginning 0) width) width))
  20401. (match-beginning 0)) ?\ )
  20402. t t s)))
  20403. s)
  20404. (defun org-fix-indentation (line ind)
  20405. "Fix indentation in LINE.
  20406. IND is a cons cell with target and minimum indentation.
  20407. If the current indentation in LINE is smaller than the minimum,
  20408. leave it alone. If it is larger than ind, set it to the target."
  20409. (let* ((l (org-remove-tabs line))
  20410. (i (org-get-indentation l))
  20411. (i1 (car ind)) (i2 (cdr ind)))
  20412. (if (>= i i2) (setq l (substring line i2)))
  20413. (if (> i1 0)
  20414. (concat (make-string i1 ?\ ) l)
  20415. l)))
  20416. (defun org-remove-indentation (code &optional n)
  20417. "Remove the maximum common indentation from the lines in CODE.
  20418. N may optionally be the number of spaces to remove."
  20419. (with-temp-buffer
  20420. (insert code)
  20421. (org-do-remove-indentation n)
  20422. (buffer-string)))
  20423. (defun org-do-remove-indentation (&optional n)
  20424. "Remove the maximum common indentation from the buffer."
  20425. (untabify (point-min) (point-max))
  20426. (let ((min 10000) re)
  20427. (if n
  20428. (setq min n)
  20429. (goto-char (point-min))
  20430. (while (re-search-forward "^ *[^ \n]" nil t)
  20431. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20432. (unless (or (= min 0) (= min 10000))
  20433. (setq re (format "^ \\{%d\\}" min))
  20434. (goto-char (point-min))
  20435. (while (re-search-forward re nil t)
  20436. (replace-match "")
  20437. (end-of-line 1))
  20438. min)))
  20439. (defun org-fill-template (template alist)
  20440. "Find each %key of ALIST in TEMPLATE and replace it."
  20441. (let ((case-fold-search nil))
  20442. (dolist (entry (sort (copy-sequence alist)
  20443. (lambda (a b) (< (length (car a)) (length (car b))))))
  20444. (setq template
  20445. (replace-regexp-in-string
  20446. (concat "%" (regexp-quote (car entry)))
  20447. (or (cdr entry) "") template t t)))
  20448. template))
  20449. (defun org-base-buffer (buffer)
  20450. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20451. (if (not buffer)
  20452. buffer
  20453. (or (buffer-base-buffer buffer)
  20454. buffer)))
  20455. (defun org-wrap (string &optional width lines)
  20456. "Wrap string to either a number of lines, or a width in characters.
  20457. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20458. that costs. If there is a word longer than WIDTH, the text is actually
  20459. wrapped to the length of that word.
  20460. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20461. many lines, whatever width that takes.
  20462. The return value is a list of lines, without newlines at the end."
  20463. (let* ((words (org-split-string string "[ \t\n]+"))
  20464. (maxword (apply 'max (mapcar 'org-string-width words)))
  20465. w ll)
  20466. (cond (width
  20467. (org-do-wrap words (max maxword width)))
  20468. (lines
  20469. (setq w maxword)
  20470. (setq ll (org-do-wrap words maxword))
  20471. (if (<= (length ll) lines)
  20472. ll
  20473. (setq ll words)
  20474. (while (> (length ll) lines)
  20475. (setq w (1+ w))
  20476. (setq ll (org-do-wrap words w)))
  20477. ll))
  20478. (t (error "Cannot wrap this")))))
  20479. (defun org-do-wrap (words width)
  20480. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20481. (let (lines line)
  20482. (while words
  20483. (setq line (pop words))
  20484. (while (and words (< (+ (length line) (length (car words))) width))
  20485. (setq line (concat line " " (pop words))))
  20486. (setq lines (push line lines)))
  20487. (nreverse lines)))
  20488. (defun org-split-string (string &optional separators)
  20489. "Splits STRING into substrings at SEPARATORS.
  20490. SEPARATORS is a regular expression.
  20491. No empty strings are returned if there are matches at the beginning
  20492. and end of string."
  20493. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20494. (let ((start 0) notfirst list)
  20495. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20496. (if (and notfirst
  20497. (= start (match-beginning 0))
  20498. (< start (length string)))
  20499. (1+ start) start))
  20500. (< (match-beginning 0) (length string)))
  20501. (setq notfirst t)
  20502. (or (eq (match-beginning 0) 0)
  20503. (and (eq (match-beginning 0) (match-end 0))
  20504. (eq (match-beginning 0) start))
  20505. (push (substring string start (match-beginning 0)) list))
  20506. (setq start (match-end 0)))
  20507. (or (eq start (length string))
  20508. (push (substring string start) list))
  20509. (nreverse list)))
  20510. (defun org-quote-vert (s)
  20511. "Replace \"|\" with \"\\vert\"."
  20512. (while (string-match "|" s)
  20513. (setq s (replace-match "\\vert" t t s)))
  20514. s)
  20515. (defun org-uuidgen-p (s)
  20516. "Is S an ID created by UUIDGEN?"
  20517. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20518. (defun org-in-src-block-p (&optional inside)
  20519. "Whether point is in a code source block.
  20520. When INSIDE is non-nil, don't consider we are within a src block
  20521. when point is at #+BEGIN_SRC or #+END_SRC."
  20522. (let ((case-fold-search t) ov)
  20523. (or (and (eq (get-char-property (point) 'src-block) t))
  20524. (and (not inside)
  20525. (save-match-data
  20526. (save-excursion
  20527. (beginning-of-line)
  20528. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20529. (defun org-context ()
  20530. "Return a list of contexts of the current cursor position.
  20531. If several contexts apply, all are returned.
  20532. Each context entry is a list with a symbol naming the context, and
  20533. two positions indicating start and end of the context. Possible
  20534. contexts are:
  20535. :headline anywhere in a headline
  20536. :headline-stars on the leading stars in a headline
  20537. :todo-keyword on a TODO keyword (including DONE) in a headline
  20538. :tags on the TAGS in a headline
  20539. :priority on the priority cookie in a headline
  20540. :item on the first line of a plain list item
  20541. :item-bullet on the bullet/number of a plain list item
  20542. :checkbox on the checkbox in a plain list item
  20543. :table in an org-mode table
  20544. :table-special on a special filed in a table
  20545. :table-table in a table.el table
  20546. :clocktable in a clocktable
  20547. :src-block in a source block
  20548. :link on a hyperlink
  20549. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20550. :target on a <<target>>
  20551. :radio-target on a <<<radio-target>>>
  20552. :latex-fragment on a LaTeX fragment
  20553. :latex-preview on a LaTeX fragment with overlaid preview image
  20554. This function expects the position to be visible because it uses font-lock
  20555. faces as a help to recognize the following contexts: :table-special, :link,
  20556. and :keyword."
  20557. (let* ((f (get-text-property (point) 'face))
  20558. (faces (if (listp f) f (list f)))
  20559. (case-fold-search t)
  20560. (p (point)) clist o)
  20561. ;; First the large context
  20562. (cond
  20563. ((org-at-heading-p t)
  20564. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20565. (when (progn
  20566. (beginning-of-line 1)
  20567. (looking-at org-todo-line-tags-regexp))
  20568. (push (org-point-in-group p 1 :headline-stars) clist)
  20569. (push (org-point-in-group p 2 :todo-keyword) clist)
  20570. (push (org-point-in-group p 4 :tags) clist))
  20571. (goto-char p)
  20572. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20573. (if (looking-at "\\[#[A-Z0-9]\\]")
  20574. (push (org-point-in-group p 0 :priority) clist)))
  20575. ((org-at-item-p)
  20576. (push (org-point-in-group p 2 :item-bullet) clist)
  20577. (push (list :item (point-at-bol)
  20578. (save-excursion (org-end-of-item) (point)))
  20579. clist)
  20580. (and (org-at-item-checkbox-p)
  20581. (push (org-point-in-group p 0 :checkbox) clist)))
  20582. ((org-at-table-p)
  20583. (push (list :table (org-table-begin) (org-table-end)) clist)
  20584. (if (memq 'org-formula faces)
  20585. (push (list :table-special
  20586. (previous-single-property-change p 'face)
  20587. (next-single-property-change p 'face)) clist)))
  20588. ((org-at-table-p 'any)
  20589. (push (list :table-table) clist)))
  20590. (goto-char p)
  20591. (let ((case-fold-search t))
  20592. ;; New the "medium" contexts: clocktables, source blocks
  20593. (cond ((org-in-clocktable-p)
  20594. (push (list :clocktable
  20595. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20596. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20597. (match-beginning 1))
  20598. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20599. (match-end 0))) clist))
  20600. ((org-in-src-block-p)
  20601. (push (list :src-block
  20602. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20603. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20604. (match-beginning 1))
  20605. (and (search-forward "#+END_SRC" nil t)
  20606. (match-beginning 0))) clist))))
  20607. (goto-char p)
  20608. ;; Now the small context
  20609. (cond
  20610. ((org-at-timestamp-p)
  20611. (push (org-point-in-group p 0 :timestamp) clist))
  20612. ((memq 'org-link faces)
  20613. (push (list :link
  20614. (previous-single-property-change p 'face)
  20615. (next-single-property-change p 'face)) clist))
  20616. ((memq 'org-special-keyword faces)
  20617. (push (list :keyword
  20618. (previous-single-property-change p 'face)
  20619. (next-single-property-change p 'face)) clist))
  20620. ((org-at-target-p)
  20621. (push (org-point-in-group p 0 :target) clist)
  20622. (goto-char (1- (match-beginning 0)))
  20623. (if (looking-at org-radio-target-regexp)
  20624. (push (org-point-in-group p 0 :radio-target) clist))
  20625. (goto-char p))
  20626. ((setq o (car (delq nil
  20627. (mapcar
  20628. (lambda (x)
  20629. (if (memq x org-latex-fragment-image-overlays) x))
  20630. (overlays-at (point))))))
  20631. (push (list :latex-fragment
  20632. (overlay-start o) (overlay-end o)) clist)
  20633. (push (list :latex-preview
  20634. (overlay-start o) (overlay-end o)) clist))
  20635. ((org-inside-LaTeX-fragment-p)
  20636. ;; FIXME: positions wrong.
  20637. (push (list :latex-fragment (point) (point)) clist)))
  20638. (setq clist (nreverse (delq nil clist)))
  20639. clist))
  20640. (defun org-in-regexp (re &optional nlines visually)
  20641. "Check if point is inside a match of RE.
  20642. Normally only the current line is checked, but you can include
  20643. NLINES extra lines after point into the search. If VISUALLY is
  20644. set, require that the cursor is not after the match but really
  20645. on, so that the block visually is on the match."
  20646. (catch 'exit
  20647. (let ((pos (point))
  20648. (eol (point-at-eol (+ 1 (or nlines 0))))
  20649. (inc (if visually 1 0)))
  20650. (save-excursion
  20651. (beginning-of-line (- 1 (or nlines 0)))
  20652. (while (re-search-forward re eol t)
  20653. (if (and (<= (match-beginning 0) pos)
  20654. (>= (+ inc (match-end 0)) pos))
  20655. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20656. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20657. "Org mode 8.3")
  20658. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20659. "Non-nil when point is between matches of START-RE and END-RE.
  20660. Also return a non-nil value when point is on one of the matches.
  20661. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20662. buffer positions. Default values are the positions of headlines
  20663. surrounding the point.
  20664. The functions returns a cons cell whose car (resp. cdr) is the
  20665. position before START-RE (resp. after END-RE)."
  20666. (save-match-data
  20667. (let ((pos (point))
  20668. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20669. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20670. beg end)
  20671. (save-excursion
  20672. ;; Point is on a block when on START-RE or if START-RE can be
  20673. ;; found before it...
  20674. (and (or (org-in-regexp start-re)
  20675. (re-search-backward start-re limit-up t))
  20676. (setq beg (match-beginning 0))
  20677. ;; ... and END-RE after it...
  20678. (goto-char (match-end 0))
  20679. (re-search-forward end-re limit-down t)
  20680. (> (setq end (match-end 0)) pos)
  20681. ;; ... without another START-RE in-between.
  20682. (goto-char (match-beginning 0))
  20683. (not (re-search-backward start-re (1+ beg) t))
  20684. ;; Return value.
  20685. (cons beg end))))))
  20686. (defun org-in-block-p (names)
  20687. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20688. NAMES is a list of strings containing names of blocks.
  20689. Return first block name matched, or nil. Beware that in case of
  20690. nested blocks, the returned name may not belong to the closest
  20691. block from point."
  20692. (save-match-data
  20693. (catch 'exit
  20694. (let ((case-fold-search t)
  20695. (lim-up (save-excursion (outline-previous-heading)))
  20696. (lim-down (save-excursion (outline-next-heading))))
  20697. (mapc (lambda (name)
  20698. (let ((n (regexp-quote name)))
  20699. (when (org-between-regexps-p
  20700. (concat "^[ \t]*#\\+begin_" n)
  20701. (concat "^[ \t]*#\\+end_" n)
  20702. lim-up lim-down)
  20703. (throw 'exit n))))
  20704. names))
  20705. nil)))
  20706. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20707. "Call `multi-occur' with buffers for all agenda files."
  20708. (interactive "sOrg-files matching: ")
  20709. (let* ((files (org-agenda-files))
  20710. (tnames (mapcar #'file-truename files))
  20711. (extra org-agenda-text-search-extra-files))
  20712. (when (eq (car extra) 'agenda-archives)
  20713. (setq extra (cdr extra))
  20714. (setq files (org-add-archive-files files)))
  20715. (dolist (f extra)
  20716. (unless (member (file-truename f) tnames)
  20717. (unless (member f files) (setq files (append files (list f))))
  20718. (setq tnames (append tnames (list (file-truename f))))))
  20719. (multi-occur
  20720. (mapcar (lambda (x)
  20721. (with-current-buffer
  20722. ;; FIXME: Why not just (find-file-noselect x)?
  20723. ;; Is it to avoid the "revert buffer" prompt?
  20724. (or (get-file-buffer x) (find-file-noselect x))
  20725. (widen)
  20726. (current-buffer)))
  20727. files)
  20728. regexp)))
  20729. (if (boundp 'occur-mode-find-occurrence-hook)
  20730. ;; Emacs 23
  20731. (add-hook 'occur-mode-find-occurrence-hook
  20732. (lambda ()
  20733. (when (derived-mode-p 'org-mode)
  20734. (org-reveal))))
  20735. ;; Emacs 22
  20736. (defadvice occur-mode-goto-occurrence
  20737. (after org-occur-reveal activate)
  20738. (and (derived-mode-p 'org-mode) (org-reveal)))
  20739. (defadvice occur-mode-goto-occurrence-other-window
  20740. (after org-occur-reveal activate)
  20741. (and (derived-mode-p 'org-mode) (org-reveal)))
  20742. (defadvice occur-mode-display-occurrence
  20743. (after org-occur-reveal activate)
  20744. (when (derived-mode-p 'org-mode)
  20745. (let ((pos (occur-mode-find-occurrence)))
  20746. (with-current-buffer (marker-buffer pos)
  20747. (save-excursion
  20748. (goto-char pos)
  20749. (org-reveal)))))))
  20750. (defun org-occur-link-in-agenda-files ()
  20751. "Create a link and search for it in the agendas.
  20752. The link is not stored in `org-stored-links', it is just created
  20753. for the search purpose."
  20754. (interactive)
  20755. (let ((link (condition-case nil
  20756. (org-store-link nil)
  20757. (error "Unable to create a link to here"))))
  20758. (org-occur-in-agenda-files (regexp-quote link))))
  20759. (defun org-reverse-string (string)
  20760. "Return the reverse of STRING."
  20761. (apply 'string (reverse (string-to-list string))))
  20762. ;; defsubst org-uniquify must be defined before first use
  20763. (defun org-uniquify-alist (alist)
  20764. "Merge elements of ALIST with the same key.
  20765. For example, in this alist:
  20766. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20767. => '((a 1 3) (b 2))
  20768. merge (a 1) and (a 3) into (a 1 3).
  20769. The function returns the new ALIST."
  20770. (let (rtn)
  20771. (mapc
  20772. (lambda (e)
  20773. (let (n)
  20774. (if (not (assoc (car e) rtn))
  20775. (push e rtn)
  20776. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20777. (setq rtn (assq-delete-all (car e) rtn))
  20778. (push n rtn))))
  20779. alist)
  20780. rtn))
  20781. (defun org-delete-all (elts list)
  20782. "Remove all elements in ELTS from LIST."
  20783. (while elts
  20784. (setq list (delete (pop elts) list)))
  20785. list)
  20786. (defun org-count (cl-item cl-seq)
  20787. "Count the number of occurrences of ITEM in SEQ.
  20788. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20789. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20790. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20791. (while (< cl-start cl-end)
  20792. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20793. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20794. (setq cl-start (1+ cl-start)))
  20795. cl-count))
  20796. (defun org-remove-if (predicate seq)
  20797. "Remove everything from SEQ that fulfills PREDICATE."
  20798. (let (res e)
  20799. (while seq
  20800. (setq e (pop seq))
  20801. (if (not (funcall predicate e)) (push e res)))
  20802. (nreverse res)))
  20803. (defun org-remove-if-not (predicate seq)
  20804. "Remove everything from SEQ that does not fulfill PREDICATE."
  20805. (let (res e)
  20806. (while seq
  20807. (setq e (pop seq))
  20808. (if (funcall predicate e) (push e res)))
  20809. (nreverse res)))
  20810. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20811. "Reduce two-argument FUNCTION across SEQ.
  20812. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20813. \":initial-value\" removed."
  20814. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20815. (cadr (memq :initial-value cl-keys)))
  20816. (cl-seq (pop cl-seq))
  20817. (t (funcall cl-func)))))
  20818. (while cl-seq
  20819. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20820. cl-accum))
  20821. (defun org-every (pred seq)
  20822. "Return true if PREDICATE is true of every element of SEQ.
  20823. Adapted from `every' in cl.el."
  20824. (catch 'org-every
  20825. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20826. t))
  20827. (defun org-some (pred seq)
  20828. "Return true if PREDICATE is true of any element of SEQ.
  20829. Adapted from `some' in cl.el."
  20830. (catch 'org-some
  20831. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20832. nil))
  20833. (defun org-back-over-empty-lines ()
  20834. "Move backwards over whitespace, to the beginning of the first empty line.
  20835. Returns the number of empty lines passed."
  20836. (let ((pos (point)))
  20837. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20838. (skip-chars-backward " \t\n\r")
  20839. (unless (eobp)
  20840. (forward-line -1)))
  20841. (beginning-of-line 2)
  20842. (goto-char (min (point) pos))
  20843. (count-lines (point) pos)))
  20844. (defun org-skip-whitespace ()
  20845. (skip-chars-forward " \t\n\r"))
  20846. (defun org-point-in-group (point group &optional context)
  20847. "Check if POINT is in match-group GROUP.
  20848. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20849. match. If the match group does not exist or point is not inside it,
  20850. return nil."
  20851. (and (match-beginning group)
  20852. (>= point (match-beginning group))
  20853. (<= point (match-end group))
  20854. (if context
  20855. (list context (match-beginning group) (match-end group))
  20856. t)))
  20857. (defun org-switch-to-buffer-other-window (&rest args)
  20858. "Switch to buffer in a second window on the current frame.
  20859. In particular, do not allow pop-up frames.
  20860. Returns the newly created buffer."
  20861. (org-no-popups
  20862. (apply 'switch-to-buffer-other-window args)))
  20863. (defun org-combine-plists (&rest plists)
  20864. "Create a single property list from all plists in PLISTS.
  20865. The process starts by copying the first list, and then setting properties
  20866. from the other lists. Settings in the last list are the most significant
  20867. ones and overrule settings in the other lists."
  20868. (let ((rtn (copy-sequence (pop plists)))
  20869. p v ls)
  20870. (while plists
  20871. (setq ls (pop plists))
  20872. (while ls
  20873. (setq p (pop ls) v (pop ls))
  20874. (setq rtn (plist-put rtn p v))))
  20875. rtn))
  20876. (defun org-replace-escapes (string table)
  20877. "Replace %-escapes in STRING with values in TABLE.
  20878. TABLE is an association list with keys like \"%a\" and string values.
  20879. The sequences in STRING may contain normal field width and padding information,
  20880. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20881. so values can contain further %-escapes if they are define later in TABLE."
  20882. (let ((tbl (copy-alist table))
  20883. (case-fold-search nil)
  20884. (pchg 0)
  20885. e re rpl)
  20886. (dolist (e tbl)
  20887. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20888. (when (and (cdr e) (string-match re (cdr e)))
  20889. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20890. (safe "SREF"))
  20891. (add-text-properties 0 3 (list 'sref sref) safe)
  20892. (setcdr e (replace-match safe t t (cdr e)))))
  20893. (while (string-match re string)
  20894. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20895. (cdr e)))
  20896. (setq string (replace-match rpl t t string))))
  20897. (while (setq pchg (next-property-change pchg string))
  20898. (let ((sref (get-text-property pchg 'sref string)))
  20899. (when (and sref (string-match "SREF" string pchg))
  20900. (setq string (replace-match sref t t string)))))
  20901. string))
  20902. (defun org-sublist (list start end)
  20903. "Return a section of LIST, from START to END.
  20904. Counting starts at 1."
  20905. (let (rtn (c start))
  20906. (setq list (nthcdr (1- start) list))
  20907. (while (and list (<= c end))
  20908. (push (pop list) rtn)
  20909. (setq c (1+ c)))
  20910. (nreverse rtn)))
  20911. (defun org-find-base-buffer-visiting (file)
  20912. "Like `find-buffer-visiting' but always return the base buffer and
  20913. not an indirect buffer."
  20914. (let ((buf (or (get-file-buffer file)
  20915. (find-buffer-visiting file))))
  20916. (if buf
  20917. (or (buffer-base-buffer buf) buf)
  20918. nil)))
  20919. (defun org-image-file-name-regexp (&optional extensions)
  20920. "Return regexp matching the file names of images.
  20921. If EXTENSIONS is given, only match these."
  20922. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20923. (image-file-name-regexp)
  20924. (let ((image-file-name-extensions
  20925. (or extensions
  20926. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20927. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20928. (concat "\\."
  20929. (regexp-opt (nconc (mapcar 'upcase
  20930. image-file-name-extensions)
  20931. image-file-name-extensions)
  20932. t)
  20933. "\\'"))))
  20934. (defun org-file-image-p (file &optional extensions)
  20935. "Return non-nil if FILE is an image."
  20936. (save-match-data
  20937. (string-match (org-image-file-name-regexp extensions) file)))
  20938. (defun org-get-cursor-date (&optional with-time)
  20939. "Return the date at cursor in as a time.
  20940. This works in the calendar and in the agenda, anywhere else it just
  20941. returns the current time.
  20942. If WITH-TIME is non-nil, returns the time of the event at point (in
  20943. the agenda) or the current time of the day."
  20944. (let (date day defd tp hod mod)
  20945. (when with-time
  20946. (setq tp (get-text-property (point) 'time))
  20947. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20948. (setq hod (string-to-number (match-string 1 tp))
  20949. mod (string-to-number (match-string 2 tp))))
  20950. (or tp (let ((now (decode-time)))
  20951. (setq hod (nth 2 now)
  20952. mod (nth 1 now)))))
  20953. (cond
  20954. ((eq major-mode 'calendar-mode)
  20955. (setq date (calendar-cursor-to-date)
  20956. defd (encode-time 0 (or mod 0) (or hod 0)
  20957. (nth 1 date) (nth 0 date) (nth 2 date))))
  20958. ((eq major-mode 'org-agenda-mode)
  20959. (setq day (get-text-property (point) 'day))
  20960. (if day
  20961. (setq date (calendar-gregorian-from-absolute day)
  20962. defd (encode-time 0 (or mod 0) (or hod 0)
  20963. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20964. (or defd (current-time))))
  20965. (defun org-mark-subtree (&optional up)
  20966. "Mark the current subtree.
  20967. This puts point at the start of the current subtree, and mark at
  20968. the end. If a numeric prefix UP is given, move up into the
  20969. hierarchy of headlines by UP levels before marking the subtree."
  20970. (interactive "P")
  20971. (org-with-limited-levels
  20972. (cond ((org-at-heading-p) (beginning-of-line))
  20973. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20974. (t (outline-previous-visible-heading 1))))
  20975. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20976. (if (org-called-interactively-p 'any)
  20977. (call-interactively 'org-mark-element)
  20978. (org-mark-element)))
  20979. ;;; Indentation
  20980. (defun org--get-expected-indentation (element contentsp)
  20981. "Expected indentation column for current line, according to ELEMENT.
  20982. ELEMENT is an element containing point. CONTENTSP is non-nil
  20983. when indentation is to be computed according to contents of
  20984. ELEMENT."
  20985. (let ((type (org-element-type element))
  20986. (start (org-element-property :begin element))
  20987. (post-affiliated (org-element-property :post-affiliated element)))
  20988. (org-with-wide-buffer
  20989. (cond
  20990. (contentsp
  20991. (case type
  20992. ((diary-sexp footnote-definition) 0)
  20993. ((headline inlinetask nil)
  20994. (if (not org-adapt-indentation) 0
  20995. (let ((level (org-current-level)))
  20996. (if level (1+ level) 0))))
  20997. ((item plain-list) (org-list-item-body-column post-affiliated))
  20998. (t
  20999. (goto-char start)
  21000. (org-get-indentation))))
  21001. ((memq type '(headline inlinetask nil))
  21002. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21003. (org--get-expected-indentation element t)
  21004. 0))
  21005. ((memq type '(diary-sexp footnote-definition)) 0)
  21006. ;; First paragraph of a footnote definition or an item.
  21007. ;; Indent like parent.
  21008. ((< (line-beginning-position) start)
  21009. (org--get-expected-indentation
  21010. (org-element-property :parent element) t))
  21011. ;; At first line: indent according to previous sibling, if any,
  21012. ;; ignoring footnote definitions and inline tasks, or parent's
  21013. ;; contents.
  21014. ((= (line-beginning-position) start)
  21015. (catch 'exit
  21016. (while t
  21017. (if (= (point-min) start) (throw 'exit 0)
  21018. (goto-char (1- start))
  21019. (let* ((previous (org-element-at-point))
  21020. (parent previous))
  21021. (while (and parent (<= (org-element-property :end parent) start))
  21022. (setq previous parent
  21023. parent (org-element-property :parent parent)))
  21024. (cond
  21025. ((not previous) (throw 'exit 0))
  21026. ((> (org-element-property :end previous) start)
  21027. (throw 'exit (org--get-expected-indentation previous t)))
  21028. ((memq (org-element-type previous)
  21029. '(footnote-definition inlinetask))
  21030. (setq start (org-element-property :begin previous)))
  21031. (t (goto-char (org-element-property :begin previous))
  21032. (throw 'exit
  21033. (if (bolp) (org-get-indentation)
  21034. ;; At first paragraph in an item or
  21035. ;; a footnote definition.
  21036. (org--get-expected-indentation
  21037. (org-element-property :parent previous) t))))))))))
  21038. ;; Otherwise, move to the first non-blank line above.
  21039. (t
  21040. (beginning-of-line)
  21041. (let ((pos (point)))
  21042. (skip-chars-backward " \r\t\n")
  21043. (cond
  21044. ;; Two blank lines end a footnote definition or a plain
  21045. ;; list. When we indent an empty line after them, the
  21046. ;; containing list or footnote definition is over, so it
  21047. ;; qualifies as a previous sibling. Therefore, we indent
  21048. ;; like its first line.
  21049. ((and (memq type '(footnote-definition plain-list))
  21050. (> (count-lines (point) pos) 2))
  21051. (goto-char start)
  21052. (org-get-indentation))
  21053. ;; Line above is the first one of a paragraph at the
  21054. ;; beginning of an item or a footnote definition. Indent
  21055. ;; like parent.
  21056. ((< (line-beginning-position) start)
  21057. (org--get-expected-indentation
  21058. (org-element-property :parent element) t))
  21059. ;; Line above is the beginning of an element, i.e., point
  21060. ;; was originally on the blank lines between element's start
  21061. ;; and contents.
  21062. ((= (line-beginning-position) post-affiliated)
  21063. (org--get-expected-indentation element t))
  21064. ;; POS is after contents in a greater element. Indent like
  21065. ;; the beginning of the element.
  21066. ;;
  21067. ;; As a special case, if point is at the end of a footnote
  21068. ;; definition or an item, indent like the very last element
  21069. ;; within.
  21070. ((and (not (eq type 'paragraph))
  21071. (let ((cend (org-element-property :contents-end element)))
  21072. (and cend (<= cend pos))))
  21073. (if (memq type '(footnote-definition item plain-list))
  21074. (let ((last (org-element-at-point)))
  21075. (org--get-expected-indentation
  21076. last
  21077. (memq (org-element-type last)
  21078. '(footnote-definition item plain-list))))
  21079. (goto-char start)
  21080. (org-get-indentation)))
  21081. ;; In any other case, indent like the current line.
  21082. (t (org-get-indentation)))))))))
  21083. (defun org--align-node-property ()
  21084. "Align node property at point.
  21085. Alignment is done according to `org-property-format', which see."
  21086. (when (save-excursion
  21087. (beginning-of-line)
  21088. (looking-at org-property-re))
  21089. (replace-match
  21090. (concat (match-string 4)
  21091. (org-trim
  21092. (format org-property-format (match-string 1) (match-string 3))))
  21093. t t)))
  21094. (defun org-indent-line ()
  21095. "Indent line depending on context.
  21096. Indentation is done according to the following rules:
  21097. - Footnote definitions, diary sexps, headlines and inline tasks
  21098. have to start at column 0.
  21099. - On the very first line of an element, consider, in order, the
  21100. next rules until one matches:
  21101. 1. If there's a sibling element before, ignoring footnote
  21102. definitions and inline tasks, indent like its first line.
  21103. 2. If element has a parent, indent like its contents. More
  21104. precisely, if parent is an item, indent after the
  21105. description part, if any, or the bullet (see
  21106. `org-list-description-max-indent'). Else, indent like
  21107. parent's first line.
  21108. 3. Otherwise, indent relatively to current level, if
  21109. `org-adapt-indentation' is non-nil, or to left margin.
  21110. - On a blank line at the end of an element, indent according to
  21111. the type of the element. More precisely
  21112. 1. If element is a plain list, an item, or a footnote
  21113. definition, indent like the very last element within.
  21114. 2. If element is a paragraph, indent like its last non blank
  21115. line.
  21116. 3. Otherwise, indent like its very first line.
  21117. - In the code part of a source block, use language major mode
  21118. to indent current line if `org-src-tab-acts-natively' is
  21119. non-nil. If it is nil, do nothing.
  21120. - Otherwise, indent like the first non-blank line above.
  21121. The function doesn't indent an item as it could break the whole
  21122. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21123. \\[org-shiftmetaright].
  21124. Also align node properties according to `org-property-format'."
  21125. (interactive)
  21126. (cond
  21127. (orgstruct-is-++
  21128. (let ((indent-line-function
  21129. (cadadr (assq 'indent-line-function org-fb-vars))))
  21130. (indent-according-to-mode)))
  21131. ((org-at-heading-p) 'noindent)
  21132. (t
  21133. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21134. (type (org-element-type element)))
  21135. (cond ((and (memq type '(plain-list item))
  21136. (= (line-beginning-position)
  21137. (org-element-property :post-affiliated element)))
  21138. 'noindent)
  21139. ((and (eq type 'src-block)
  21140. org-src-tab-acts-natively
  21141. (> (line-beginning-position)
  21142. (org-element-property :post-affiliated element))
  21143. (< (line-beginning-position)
  21144. (org-with-wide-buffer
  21145. (goto-char (org-element-property :end element))
  21146. (skip-chars-backward " \r\t\n")
  21147. (line-beginning-position))))
  21148. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21149. (t
  21150. (let ((column (org--get-expected-indentation element nil)))
  21151. ;; Preserve current column.
  21152. (if (<= (current-column) (current-indentation))
  21153. (org-indent-line-to column)
  21154. (save-excursion (org-indent-line-to column))))
  21155. ;; Align node property. Also preserve current column.
  21156. (when (eq type 'node-property)
  21157. (let ((column (current-column)))
  21158. (org--align-node-property)
  21159. (org-move-to-column column)))))))))
  21160. (defun org-indent-region (start end)
  21161. "Indent each non-blank line in the region.
  21162. Called from a program, START and END specify the region to
  21163. indent. The function will not indent contents of example blocks,
  21164. verse blocks and export blocks as leading white spaces are
  21165. assumed to be significant there."
  21166. (interactive "r")
  21167. (save-excursion
  21168. (goto-char start)
  21169. (skip-chars-forward " \r\t\n")
  21170. (unless (eobp) (beginning-of-line))
  21171. (let ((indent-to
  21172. (lambda (ind pos)
  21173. ;; Set IND as indentation for all lines between point and
  21174. ;; POS or END, whichever comes first. Blank lines are
  21175. ;; ignored. Leave point after POS once done.
  21176. (let ((limit (copy-marker (min end pos))))
  21177. (while (< (point) limit)
  21178. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21179. (forward-line))
  21180. (set-marker limit nil))))
  21181. (end (copy-marker end)))
  21182. (while (< (point) end)
  21183. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21184. (let* ((element (org-element-at-point))
  21185. (type (org-element-type element))
  21186. (element-end (copy-marker (org-element-property :end element)))
  21187. (ind (org--get-expected-indentation element nil)))
  21188. (cond
  21189. ((or (memq type '(paragraph table table-row))
  21190. (not (or (org-element-property :contents-begin element)
  21191. (memq type
  21192. '(example-block export-block src-block)))))
  21193. ;; Elements here are indented as a single block. Also
  21194. ;; align node properties.
  21195. (when (eq type 'node-property)
  21196. (org--align-node-property)
  21197. (beginning-of-line))
  21198. (funcall indent-to ind element-end))
  21199. (t
  21200. ;; Elements in this category consist of three parts:
  21201. ;; before the contents, the contents, and after the
  21202. ;; contents. The contents are treated specially,
  21203. ;; according to the element type, or not indented at
  21204. ;; all. Other parts are indented as a single block.
  21205. (let* ((post (copy-marker
  21206. (org-element-property :post-affiliated element)))
  21207. (cbeg
  21208. (copy-marker
  21209. (cond
  21210. ((not (org-element-property :contents-begin element))
  21211. ;; Fake contents for source blocks.
  21212. (org-with-wide-buffer
  21213. (goto-char post)
  21214. (forward-line)
  21215. (point)))
  21216. ((memq type '(footnote-definition item plain-list))
  21217. ;; Contents in these elements could start on
  21218. ;; the same line as the beginning of the
  21219. ;; element. Make sure we start indenting
  21220. ;; from the second line.
  21221. (org-with-wide-buffer
  21222. (goto-char post)
  21223. (end-of-line)
  21224. (skip-chars-forward " \r\t\n")
  21225. (if (eobp) (point) (line-beginning-position))))
  21226. (t (org-element-property :contents-begin element)))))
  21227. (cend (copy-marker
  21228. (or (org-element-property :contents-end element)
  21229. ;; Fake contents for source blocks.
  21230. (org-with-wide-buffer
  21231. (goto-char element-end)
  21232. (skip-chars-backward " \r\t\n")
  21233. (line-beginning-position)))
  21234. t)))
  21235. ;; Do not change items indentation individually as it
  21236. ;; might break the list as a whole. On the other
  21237. ;; hand, when at a plain list, indent it as a whole.
  21238. (cond ((eq type 'plain-list)
  21239. (let ((offset (- ind (org-get-indentation))))
  21240. (unless (zerop offset)
  21241. (indent-rigidly (org-element-property :begin element)
  21242. (org-element-property :end element)
  21243. offset))
  21244. (goto-char cbeg)))
  21245. ((eq type 'item) (goto-char cbeg))
  21246. (t (funcall indent-to ind cbeg)))
  21247. (when (< (point) end)
  21248. (case type
  21249. ((example-block export-block verse-block))
  21250. (src-block
  21251. ;; In a source block, indent source code
  21252. ;; according to language major mode, but only if
  21253. ;; `org-src-tab-acts-natively' is non-nil.
  21254. (when (and (< (point) end) org-src-tab-acts-natively)
  21255. (ignore-errors
  21256. (org-babel-do-in-edit-buffer
  21257. (indent-region (point-min) (point-max))))))
  21258. (t (org-indent-region (point) (min cend end))))
  21259. (goto-char (min cend end))
  21260. (when (< (point) end) (funcall indent-to ind element-end)))
  21261. (set-marker post nil)
  21262. (set-marker cbeg nil)
  21263. (set-marker cend nil))))
  21264. (set-marker element-end nil))))
  21265. (set-marker end nil))))
  21266. (defun org-indent-drawer ()
  21267. "Indent the drawer at point."
  21268. (interactive)
  21269. (unless (save-excursion
  21270. (beginning-of-line)
  21271. (org-looking-at-p org-drawer-regexp))
  21272. (user-error "Not at a drawer"))
  21273. (let ((element (org-element-at-point)))
  21274. (unless (memq (org-element-type element) '(drawer property-drawer))
  21275. (user-error "Not at a drawer"))
  21276. (org-with-wide-buffer
  21277. (org-indent-region (org-element-property :begin element)
  21278. (org-element-property :end element))))
  21279. (message "Drawer at point indented"))
  21280. (defun org-indent-block ()
  21281. "Indent the block at point."
  21282. (interactive)
  21283. (unless (save-excursion
  21284. (beginning-of-line)
  21285. (let ((case-fold-search t))
  21286. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21287. (user-error "Not at a block"))
  21288. (let ((element (org-element-at-point)))
  21289. (unless (memq (org-element-type element)
  21290. '(comment-block center-block dynamic-block example-block
  21291. export-block quote-block special-block
  21292. src-block verse-block))
  21293. (user-error "Not at a block"))
  21294. (org-with-wide-buffer
  21295. (org-indent-region (org-element-property :begin element)
  21296. (org-element-property :end element))))
  21297. (message "Block at point indented"))
  21298. ;;; Filling
  21299. ;; We use our own fill-paragraph and auto-fill functions.
  21300. ;; `org-fill-paragraph' relies on adaptive filling and context
  21301. ;; checking. Appropriate `fill-prefix' is computed with
  21302. ;; `org-adaptive-fill-function'.
  21303. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21304. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21305. ;; `org-adaptive-fill-function' value. Internally,
  21306. ;; `org-comment-line-break-function' breaks the line.
  21307. ;; `org-setup-filling' installs filling and auto-filling related
  21308. ;; variables during `org-mode' initialization.
  21309. (defvar org-element-paragraph-separate) ; org-element.el
  21310. (defun org-setup-filling ()
  21311. (require 'org-element)
  21312. ;; Prevent auto-fill from inserting unwanted new items.
  21313. (when (boundp 'fill-nobreak-predicate)
  21314. (org-set-local
  21315. 'fill-nobreak-predicate
  21316. (org-uniquify
  21317. (append fill-nobreak-predicate
  21318. '(org-fill-line-break-nobreak-p
  21319. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21320. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21321. (org-set-local 'paragraph-start paragraph-ending)
  21322. (org-set-local 'paragraph-separate paragraph-ending))
  21323. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21324. (org-set-local 'auto-fill-inhibit-regexp nil)
  21325. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21326. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21327. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21328. (defun org-fill-line-break-nobreak-p ()
  21329. "Non-nil when a new line at point would create an Org line break."
  21330. (save-excursion
  21331. (skip-chars-backward "[ \t]")
  21332. (skip-chars-backward "\\\\")
  21333. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21334. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21335. "Non-nil when a new line at point would split a timestamp."
  21336. (and (org-at-timestamp-p t)
  21337. (not (looking-at org-ts-regexp-both))))
  21338. (declare-function message-in-body-p "message" ())
  21339. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21340. (defun org-adaptive-fill-function ()
  21341. "Compute a fill prefix for the current line.
  21342. Return fill prefix, as a string, or nil if current line isn't
  21343. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21344. matches in paragraphs or comments, use it."
  21345. (catch 'exit
  21346. (when (derived-mode-p 'message-mode)
  21347. (save-excursion
  21348. (beginning-of-line)
  21349. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21350. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21351. ((looking-at message-cite-prefix-regexp)
  21352. (throw 'exit (match-string-no-properties 0)))
  21353. ((looking-at org-outline-regexp)
  21354. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21355. (org-with-wide-buffer
  21356. (unless (org-at-heading-p)
  21357. (let* ((p (line-beginning-position))
  21358. (element (save-excursion
  21359. (beginning-of-line)
  21360. (org-element-at-point)))
  21361. (type (org-element-type element))
  21362. (post-affiliated (org-element-property :post-affiliated element)))
  21363. (unless (< p post-affiliated)
  21364. (case type
  21365. (comment
  21366. (save-excursion
  21367. (beginning-of-line)
  21368. (looking-at "[ \t]*")
  21369. (concat (match-string 0) "# ")))
  21370. (footnote-definition "")
  21371. ((item plain-list)
  21372. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21373. (paragraph
  21374. ;; Fill prefix is usually the same as the current line,
  21375. ;; unless the paragraph is at the beginning of an item.
  21376. (let ((parent (org-element-property :parent element)))
  21377. (save-excursion
  21378. (beginning-of-line)
  21379. (cond ((eq (org-element-type parent) 'item)
  21380. (make-string (org-list-item-body-column
  21381. (org-element-property :begin parent))
  21382. ?\s))
  21383. ((and adaptive-fill-regexp
  21384. ;; Locally disable
  21385. ;; `adaptive-fill-function' to let
  21386. ;; `fill-context-prefix' handle
  21387. ;; `adaptive-fill-regexp' variable.
  21388. (let (adaptive-fill-function)
  21389. (fill-context-prefix
  21390. post-affiliated
  21391. (org-element-property :end element)))))
  21392. ((looking-at "[ \t]+") (match-string 0))
  21393. (t "")))))
  21394. (comment-block
  21395. ;; Only fill contents if P is within block boundaries.
  21396. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21397. (forward-line)
  21398. (point)))
  21399. (cend (save-excursion
  21400. (goto-char (org-element-property :end element))
  21401. (skip-chars-backward " \r\t\n")
  21402. (line-beginning-position))))
  21403. (when (and (>= p cbeg) (< p cend))
  21404. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21405. (match-string 0)
  21406. "")))))))))))
  21407. (declare-function message-goto-body "message" ())
  21408. (defvar message-cite-prefix-regexp) ; From message.el
  21409. (defun org-fill-paragraph (&optional justify)
  21410. "Fill element at point, when applicable.
  21411. This function only applies to comment blocks, comments, example
  21412. blocks and paragraphs. Also, as a special case, re-align table
  21413. when point is at one.
  21414. If JUSTIFY is non-nil (interactively, with prefix argument),
  21415. justify as well. If `sentence-end-double-space' is non-nil, then
  21416. period followed by one space does not end a sentence, so don't
  21417. break a line there. The variable `fill-column' controls the
  21418. width for filling.
  21419. For convenience, when point is at a plain list, an item or
  21420. a footnote definition, try to fill the first paragraph within."
  21421. (interactive)
  21422. (if (and (derived-mode-p 'message-mode)
  21423. (or (not (message-in-body-p))
  21424. (save-excursion (move-beginning-of-line 1)
  21425. (looking-at message-cite-prefix-regexp))))
  21426. ;; First ensure filling is correct in message-mode.
  21427. (let ((fill-paragraph-function
  21428. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21429. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21430. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21431. (paragraph-separate
  21432. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21433. (fill-paragraph nil))
  21434. (with-syntax-table org-mode-transpose-word-syntax-table
  21435. ;; Move to end of line in order to get the first paragraph
  21436. ;; within a plain list or a footnote definition.
  21437. (let ((element (save-excursion
  21438. (end-of-line)
  21439. (or (ignore-errors (org-element-at-point))
  21440. (user-error "An element cannot be parsed line %d"
  21441. (line-number-at-pos (point)))))))
  21442. ;; First check if point is in a blank line at the beginning of
  21443. ;; the buffer. In that case, ignore filling.
  21444. (case (org-element-type element)
  21445. ;; Use major mode filling function is src blocks.
  21446. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21447. ;; Align Org tables, leave table.el tables as-is.
  21448. (table-row (org-table-align) t)
  21449. (table
  21450. (when (eq (org-element-property :type element) 'org)
  21451. (save-excursion
  21452. (goto-char (org-element-property :post-affiliated element))
  21453. (org-table-align)))
  21454. t)
  21455. (paragraph
  21456. ;; Paragraphs may contain `line-break' type objects.
  21457. (let ((beg (max (point-min)
  21458. (org-element-property :contents-begin element)))
  21459. (end (min (point-max)
  21460. (org-element-property :contents-end element))))
  21461. ;; Do nothing if point is at an affiliated keyword.
  21462. (if (< (line-end-position) beg) t
  21463. (when (derived-mode-p 'message-mode)
  21464. ;; In `message-mode', do not fill following citation
  21465. ;; in current paragraph nor text before message body.
  21466. (let ((body-start (save-excursion (message-goto-body))))
  21467. (when body-start (setq beg (max body-start beg))))
  21468. (when (save-excursion
  21469. (re-search-forward
  21470. (concat "^" message-cite-prefix-regexp) end t))
  21471. (setq end (match-beginning 0))))
  21472. ;; Fill paragraph, taking line breaks into account.
  21473. (save-excursion
  21474. (goto-char beg)
  21475. (let ((cuts (list beg)))
  21476. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21477. (when (eq 'line-break
  21478. (org-element-type
  21479. (save-excursion (backward-char)
  21480. (org-element-context))))
  21481. (push (point) cuts)))
  21482. (dolist (c (delq end cuts))
  21483. (fill-region-as-paragraph c end justify)
  21484. (setq end c))))
  21485. t)))
  21486. ;; Contents of `comment-block' type elements should be
  21487. ;; filled as plain text, but only if point is within block
  21488. ;; markers.
  21489. (comment-block
  21490. (let* ((case-fold-search t)
  21491. (beg (save-excursion
  21492. (goto-char (org-element-property :begin element))
  21493. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21494. (forward-line)
  21495. (point)))
  21496. (end (save-excursion
  21497. (goto-char (org-element-property :end element))
  21498. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21499. (line-beginning-position))))
  21500. (if (or (< (point) beg) (> (point) end)) t
  21501. (fill-region-as-paragraph
  21502. (save-excursion (end-of-line)
  21503. (re-search-backward "^[ \t]*$" beg 'move)
  21504. (line-beginning-position))
  21505. (save-excursion (beginning-of-line)
  21506. (re-search-forward "^[ \t]*$" end 'move)
  21507. (line-beginning-position))
  21508. justify))))
  21509. ;; Fill comments.
  21510. (comment
  21511. (let ((begin (org-element-property :post-affiliated element))
  21512. (end (org-element-property :end element)))
  21513. (when (and (>= (point) begin) (<= (point) end))
  21514. (let ((begin (save-excursion
  21515. (end-of-line)
  21516. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21517. (progn (forward-line) (point))
  21518. begin)))
  21519. (end (save-excursion
  21520. (end-of-line)
  21521. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21522. (1- (line-beginning-position))
  21523. (skip-chars-backward " \r\t\n")
  21524. (line-end-position)))))
  21525. ;; Do not fill comments when at a blank line.
  21526. (when (> end begin)
  21527. (let ((fill-prefix
  21528. (save-excursion
  21529. (beginning-of-line)
  21530. (looking-at "[ \t]*#")
  21531. (let ((comment-prefix (match-string 0)))
  21532. (goto-char (match-end 0))
  21533. (if (looking-at adaptive-fill-regexp)
  21534. (concat comment-prefix (match-string 0))
  21535. (concat comment-prefix " "))))))
  21536. (save-excursion
  21537. (fill-region-as-paragraph begin end justify))))))
  21538. t))
  21539. ;; Ignore every other element.
  21540. (otherwise t))))))
  21541. (defun org-auto-fill-function ()
  21542. "Auto-fill function."
  21543. ;; Check if auto-filling is meaningful.
  21544. (let ((fc (current-fill-column)))
  21545. (when (and fc (> (current-column) fc))
  21546. (let* ((fill-prefix (org-adaptive-fill-function))
  21547. ;; Enforce empty fill prefix, if required. Otherwise, it
  21548. ;; will be computed again.
  21549. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21550. (when fill-prefix (do-auto-fill))))))
  21551. (defun org-comment-line-break-function (&optional soft)
  21552. "Break line at point and indent, continuing comment if within one.
  21553. The inserted newline is marked hard if variable
  21554. `use-hard-newlines' is true, unless optional argument SOFT is
  21555. non-nil."
  21556. (if soft (insert-and-inherit ?\n) (newline 1))
  21557. (save-excursion (forward-char -1) (delete-horizontal-space))
  21558. (delete-horizontal-space)
  21559. (indent-to-left-margin)
  21560. (insert-before-markers-and-inherit fill-prefix))
  21561. ;;; Fixed Width Areas
  21562. (defun org-toggle-fixed-width ()
  21563. "Toggle fixed-width markup.
  21564. Add or remove fixed-width markup on current line, whenever it
  21565. makes sense. Return an error otherwise.
  21566. If a region is active and if it contains only fixed-width areas
  21567. or blank lines, remove all fixed-width markup in it. If the
  21568. region contains anything else, convert all non-fixed-width lines
  21569. to fixed-width ones.
  21570. Blank lines at the end of the region are ignored unless the
  21571. region only contains such lines."
  21572. (interactive)
  21573. (if (not (org-region-active-p))
  21574. ;; No region:
  21575. ;;
  21576. ;; Remove fixed width marker only in a fixed-with element.
  21577. ;;
  21578. ;; Add fixed width maker in paragraphs, in blank lines after
  21579. ;; elements or at the beginning of a headline or an inlinetask,
  21580. ;; and before any one-line elements (e.g., a clock).
  21581. (progn
  21582. (beginning-of-line)
  21583. (let* ((element (org-element-at-point))
  21584. (type (org-element-type element)))
  21585. (cond
  21586. ((and (eq type 'fixed-width)
  21587. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21588. (replace-match
  21589. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21590. ((and (memq type '(babel-call clock comment diary-sexp headline
  21591. horizontal-rule keyword paragraph
  21592. planning))
  21593. (<= (org-element-property :post-affiliated element) (point)))
  21594. (skip-chars-forward " \t")
  21595. (insert ": "))
  21596. ((and (org-looking-at-p "[ \t]*$")
  21597. (or (eq type 'inlinetask)
  21598. (save-excursion
  21599. (skip-chars-forward " \r\t\n")
  21600. (<= (org-element-property :end element) (point)))))
  21601. (delete-region (point) (line-end-position))
  21602. (org-indent-line)
  21603. (insert ": "))
  21604. (t (user-error "Cannot insert a fixed-width line here")))))
  21605. ;; Region active.
  21606. (let* ((begin (save-excursion
  21607. (goto-char (region-beginning))
  21608. (line-beginning-position)))
  21609. (end (copy-marker
  21610. (save-excursion
  21611. (goto-char (region-end))
  21612. (unless (eolp) (beginning-of-line))
  21613. (if (save-excursion (re-search-backward "\\S-" begin t))
  21614. (progn (skip-chars-backward " \r\t\n") (point))
  21615. (point)))))
  21616. (all-fixed-width-p
  21617. (catch 'not-all-p
  21618. (save-excursion
  21619. (goto-char begin)
  21620. (skip-chars-forward " \r\t\n")
  21621. (when (eobp) (throw 'not-all-p nil))
  21622. (while (< (point) end)
  21623. (let ((element (org-element-at-point)))
  21624. (if (eq (org-element-type element) 'fixed-width)
  21625. (goto-char (org-element-property :end element))
  21626. (throw 'not-all-p nil))))
  21627. t))))
  21628. (if all-fixed-width-p
  21629. (save-excursion
  21630. (goto-char begin)
  21631. (while (< (point) end)
  21632. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21633. (replace-match
  21634. "" nil nil nil
  21635. (if (= (line-end-position) (match-end 0)) 0 1)))
  21636. (forward-line)))
  21637. (let ((min-ind (point-max)))
  21638. ;; Find minimum indentation across all lines.
  21639. (save-excursion
  21640. (goto-char begin)
  21641. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21642. (setq min-ind 0)
  21643. (catch 'zerop
  21644. (while (< (point) end)
  21645. (unless (org-looking-at-p "[ \t]*$")
  21646. (let ((ind (org-get-indentation)))
  21647. (setq min-ind (min min-ind ind))
  21648. (when (zerop ind) (throw 'zerop t))))
  21649. (forward-line)))))
  21650. ;; Loop over all lines and add fixed-width markup everywhere
  21651. ;; but in fixed-width lines.
  21652. (save-excursion
  21653. (goto-char begin)
  21654. (while (< (point) end)
  21655. (cond
  21656. ((org-at-heading-p)
  21657. (insert ": ")
  21658. (forward-line)
  21659. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21660. (insert ":")
  21661. (forward-line)))
  21662. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21663. (let* ((element (org-element-at-point))
  21664. (element-end (org-element-property :end element)))
  21665. (if (eq (org-element-type element) 'fixed-width)
  21666. (progn (goto-char element-end)
  21667. (skip-chars-backward " \r\t\n")
  21668. (forward-line))
  21669. (let ((limit (min end element-end)))
  21670. (while (< (point) limit)
  21671. (org-move-to-column min-ind t)
  21672. (insert ": ")
  21673. (forward-line))))))
  21674. (t
  21675. (org-move-to-column min-ind t)
  21676. (insert ": ")
  21677. (forward-line)))))))
  21678. (set-marker end nil))))
  21679. ;;; Comments
  21680. ;; Org comments syntax is quite complex. It requires the entire line
  21681. ;; to be just a comment. Also, even with the right syntax at the
  21682. ;; beginning of line, some some elements (i.e. verse-block or
  21683. ;; example-block) don't accept comments. Usual Emacs comment commands
  21684. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21685. ;; Org still relies on `comment-dwim', but cannot trust
  21686. ;; `comment-only-p'. So, `comment-region-function' and
  21687. ;; `uncomment-region-function' both point
  21688. ;; to`org-comment-or-uncomment-region'. Eventually,
  21689. ;; `org-insert-comment' takes care of insertion of comments at the
  21690. ;; beginning of line.
  21691. ;; `org-setup-comments-handling' install comments related variables
  21692. ;; during `org-mode' initialization.
  21693. (defun org-setup-comments-handling ()
  21694. (interactive)
  21695. (org-set-local 'comment-use-syntax nil)
  21696. (org-set-local 'comment-start "# ")
  21697. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21698. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21699. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21700. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21701. (defun org-insert-comment ()
  21702. "Insert an empty comment above current line.
  21703. If the line is empty, insert comment at its beginning. When
  21704. point is within a source block, comment according to the related
  21705. major mode."
  21706. (if (let ((element (org-element-at-point)))
  21707. (and (eq (org-element-type element) 'src-block)
  21708. (< (save-excursion
  21709. (goto-char (org-element-property :post-affiliated element))
  21710. (line-end-position))
  21711. (point))
  21712. (> (save-excursion
  21713. (goto-char (org-element-property :end element))
  21714. (skip-chars-backward " \r\t\n")
  21715. (line-beginning-position))
  21716. (point))))
  21717. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21718. (beginning-of-line)
  21719. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21720. (open-line 1))
  21721. (org-indent-line)
  21722. (insert "# ")))
  21723. (defvar comment-empty-lines) ; From newcomment.el.
  21724. (defun org-comment-or-uncomment-region (beg end &rest _)
  21725. "Comment or uncomment each non-blank line in the region.
  21726. Uncomment each non-blank line between BEG and END if it only
  21727. contains commented lines. Otherwise, comment them. If region is
  21728. strictly within a source block, use appropriate comment syntax."
  21729. (if (let ((element (org-element-at-point)))
  21730. (and (eq (org-element-type element) 'src-block)
  21731. (< (save-excursion
  21732. (goto-char (org-element-property :post-affiliated element))
  21733. (line-end-position))
  21734. beg)
  21735. (>= (save-excursion
  21736. (goto-char (org-element-property :end element))
  21737. (skip-chars-backward " \r\t\n")
  21738. (line-beginning-position))
  21739. end)))
  21740. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21741. (save-restriction
  21742. ;; Restrict region
  21743. (narrow-to-region (save-excursion (goto-char beg)
  21744. (skip-chars-forward " \r\t\n" end)
  21745. (line-beginning-position))
  21746. (save-excursion (goto-char end)
  21747. (skip-chars-backward " \r\t\n" beg)
  21748. (line-end-position)))
  21749. (let ((uncommentp
  21750. ;; UNCOMMENTP is non-nil when every non blank line between
  21751. ;; BEG and END is a comment.
  21752. (save-excursion
  21753. (goto-char (point-min))
  21754. (while (and (not (eobp))
  21755. (let ((element (org-element-at-point)))
  21756. (and (eq (org-element-type element) 'comment)
  21757. (goto-char (min (point-max)
  21758. (org-element-property
  21759. :end element)))))))
  21760. (eobp))))
  21761. (if uncommentp
  21762. ;; Only blank lines and comments in region: uncomment it.
  21763. (save-excursion
  21764. (goto-char (point-min))
  21765. (while (not (eobp))
  21766. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21767. (replace-match "" nil nil nil 1))
  21768. (forward-line)))
  21769. ;; Comment each line in region.
  21770. (let ((min-indent (point-max)))
  21771. ;; First find the minimum indentation across all lines.
  21772. (save-excursion
  21773. (goto-char (point-min))
  21774. (while (and (not (eobp)) (not (zerop min-indent)))
  21775. (unless (looking-at "[ \t]*$")
  21776. (setq min-indent (min min-indent (current-indentation))))
  21777. (forward-line)))
  21778. ;; Then loop over all lines.
  21779. (save-excursion
  21780. (goto-char (point-min))
  21781. (while (not (eobp))
  21782. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21783. ;; Don't get fooled by invisible text (e.g. link path)
  21784. ;; when moving to column MIN-INDENT.
  21785. (let ((buffer-invisibility-spec nil))
  21786. (org-move-to-column min-indent t))
  21787. (insert comment-start))
  21788. (forward-line)))))))))
  21789. (defun org-comment-dwim (arg)
  21790. "Call `comment-dwim' within a source edit buffer if needed."
  21791. (interactive "*P")
  21792. (if (org-in-src-block-p)
  21793. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21794. (call-interactively 'comment-dwim)))
  21795. ;;; Timestamps API
  21796. ;; This section contains tools to operate on timestamp objects, as
  21797. ;; returned by, e.g. `org-element-context'.
  21798. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21799. "Encode TIMESTAMP object into Emacs internal time.
  21800. Use end of date range or time range when END is non-nil."
  21801. (apply #'encode-time
  21802. (cons 0
  21803. (mapcar
  21804. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21805. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21806. '(:minute-start :hour-start :day-start :month-start
  21807. :year-start))))))
  21808. (defun org-timestamp-has-time-p (timestamp)
  21809. "Non-nil when TIMESTAMP has a time specified."
  21810. (org-element-property :hour-start timestamp))
  21811. (defun org-timestamp-format (timestamp format &optional end utc)
  21812. "Format a TIMESTAMP object into a string.
  21813. FORMAT is a format specifier to be passed to
  21814. `format-time-string'.
  21815. When optional argument END is non-nil, use end of date-range or
  21816. time-range, if possible.
  21817. When optional argument UTC is non-nil, time will be expressed as
  21818. Universal Time."
  21819. (format-time-string
  21820. format (org-timestamp--to-internal-time timestamp end) utc))
  21821. (defun org-timestamp-split-range (timestamp &optional end)
  21822. "Extract a TIMESTAMP object from a date or time range.
  21823. END, when non-nil, means extract the end of the range.
  21824. Otherwise, extract its start.
  21825. Return a new timestamp object."
  21826. (let ((type (org-element-property :type timestamp)))
  21827. (if (memq type '(active inactive diary)) timestamp
  21828. (let ((split-ts (org-element-copy timestamp)))
  21829. ;; Set new type.
  21830. (org-element-put-property
  21831. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21832. ;; Copy start properties over end properties if END is
  21833. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21834. (let ((p-alist '((:minute-start . :minute-end)
  21835. (:hour-start . :hour-end)
  21836. (:day-start . :day-end)
  21837. (:month-start . :month-end)
  21838. (:year-start . :year-end))))
  21839. (dolist (p-cell p-alist)
  21840. (org-element-put-property
  21841. split-ts
  21842. (funcall (if end #'car #'cdr) p-cell)
  21843. (org-element-property
  21844. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21845. ;; Eventually refresh `:raw-value'.
  21846. (org-element-put-property split-ts :raw-value nil)
  21847. (org-element-put-property
  21848. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21849. (defun org-timestamp-translate (timestamp &optional boundary)
  21850. "Translate TIMESTAMP object to custom format.
  21851. Format string is defined in `org-time-stamp-custom-formats',
  21852. which see.
  21853. When optional argument BOUNDARY is non-nil, it is either the
  21854. symbol `start' or `end'. In this case, only translate the
  21855. starting or ending part of TIMESTAMP if it is a date or time
  21856. range. Otherwise, translate both parts.
  21857. Return timestamp as-is if `org-display-custom-times' is nil or if
  21858. it has a `diary' type."
  21859. (let ((type (org-element-property :type timestamp)))
  21860. (if (or (not org-display-custom-times) (eq type 'diary))
  21861. (org-element-interpret-data timestamp)
  21862. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21863. org-time-stamp-custom-formats)))
  21864. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21865. (concat (org-timestamp-format timestamp fmt)
  21866. "--"
  21867. (org-timestamp-format timestamp fmt t))
  21868. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21869. ;;; Other stuff.
  21870. (defvar reftex-docstruct-symbol)
  21871. (defvar reftex-cite-format)
  21872. (defvar org--rds)
  21873. (defun org-reftex-citation ()
  21874. "Use reftex-citation to insert a citation into the buffer.
  21875. This looks for a line like
  21876. #+BIBLIOGRAPHY: foo plain option:-d
  21877. and derives from it that foo.bib is the bibliography file relevant
  21878. for this document. It then installs the necessary environment for RefTeX
  21879. to work in this buffer and calls `reftex-citation' to insert a citation
  21880. into the buffer.
  21881. Export of such citations to both LaTeX and HTML is handled by the contributed
  21882. package ox-bibtex by Taru Karttunen."
  21883. (interactive)
  21884. (let ((reftex-docstruct-symbol 'org--rds)
  21885. (reftex-cite-format "\\cite{%l}")
  21886. org--rds bib)
  21887. (save-excursion
  21888. (save-restriction
  21889. (widen)
  21890. (let ((case-fold-search t)
  21891. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21892. (if (not (save-excursion
  21893. (or (re-search-forward re nil t)
  21894. (re-search-backward re nil t))))
  21895. (user-error "No bibliography defined in file")
  21896. (setq bib (concat (match-string 1) ".bib")
  21897. org--rds (list (list 'bib bib)))))))
  21898. (call-interactively 'reftex-citation)))
  21899. ;;;; Functions extending outline functionality
  21900. (defun org-beginning-of-line (&optional arg)
  21901. "Go to the beginning of the current line. If that is invisible, continue
  21902. to a visible line beginning. This makes the function of C-a more intuitive.
  21903. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21904. first attempt, and only move to after the tags when the cursor is already
  21905. beyond the end of the headline."
  21906. (interactive "P")
  21907. (let ((pos (point))
  21908. (special (if (consp org-special-ctrl-a/e)
  21909. (car org-special-ctrl-a/e)
  21910. org-special-ctrl-a/e))
  21911. deactivate-mark refpos)
  21912. (if (org-bound-and-true-p visual-line-mode)
  21913. (beginning-of-visual-line 1)
  21914. (beginning-of-line 1))
  21915. (if (and arg (fboundp 'move-beginning-of-line))
  21916. (call-interactively 'move-beginning-of-line)
  21917. (if (bobp)
  21918. nil
  21919. (backward-char 1)
  21920. (if (org-truely-invisible-p)
  21921. (while (and (not (bobp)) (org-truely-invisible-p))
  21922. (backward-char 1)
  21923. (beginning-of-line 1))
  21924. (forward-char 1))))
  21925. (when special
  21926. (cond
  21927. ((and (looking-at org-complex-heading-regexp)
  21928. (eq (char-after (match-end 1)) ?\s))
  21929. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21930. (point-at-eol)))
  21931. (goto-char
  21932. (if (eq special t)
  21933. (cond ((> pos refpos) refpos)
  21934. ((= pos (point)) refpos)
  21935. (t (point)))
  21936. (cond ((> pos (point)) (point))
  21937. ((not (eq last-command this-command)) (point))
  21938. (t refpos)))))
  21939. ((org-at-item-p)
  21940. ;; Being at an item and not looking at an the item means point
  21941. ;; was previously moved to beginning of a visual line, which
  21942. ;; doesn't contain the item. Therefore, do nothing special,
  21943. ;; just stay here.
  21944. (when (looking-at org-list-full-item-re)
  21945. ;; Set special position at first white space character after
  21946. ;; bullet, and check-box, if any.
  21947. (let ((after-bullet
  21948. (let ((box (match-end 3)))
  21949. (if (not box) (match-end 1)
  21950. (let ((after (char-after box)))
  21951. (if (and after (= after ? )) (1+ box) box))))))
  21952. ;; Special case: Move point to special position when
  21953. ;; currently after it or at beginning of line.
  21954. (if (eq special t)
  21955. (when (or (> pos after-bullet) (= (point) pos))
  21956. (goto-char after-bullet))
  21957. ;; Reversed case: Move point to special position when
  21958. ;; point was already at beginning of line and command is
  21959. ;; repeated.
  21960. (when (and (= (point) pos) (eq last-command this-command))
  21961. (goto-char after-bullet))))))))
  21962. (org-no-warnings
  21963. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21964. (setq disable-point-adjustment
  21965. (or (not (invisible-p (point)))
  21966. (not (invisible-p (max (point-min) (1- (point))))))))
  21967. (defun org-end-of-line (&optional arg)
  21968. "Go to the end of the line.
  21969. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21970. tags on the first attempt, and only move to after the tags when
  21971. the cursor is already beyond the end of the headline."
  21972. (interactive "P")
  21973. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21974. org-special-ctrl-a/e))
  21975. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21976. 'end-of-visual-line)
  21977. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21978. (t 'end-of-line)))
  21979. deactivate-mark)
  21980. (if (or (not special) arg) (call-interactively move-fun)
  21981. (let* ((element (save-excursion (beginning-of-line)
  21982. (org-element-at-point)))
  21983. (type (org-element-type element)))
  21984. (cond
  21985. ((memq type '(headline inlinetask))
  21986. (let ((pos (point)))
  21987. (beginning-of-line 1)
  21988. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21989. (if (eq special t)
  21990. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21991. (goto-char (match-beginning 1))
  21992. (goto-char (match-end 0)))
  21993. (if (or (< pos (match-end 0))
  21994. (not (eq this-command last-command)))
  21995. (goto-char (match-end 0))
  21996. (goto-char (match-beginning 1))))
  21997. (call-interactively move-fun))))
  21998. ((outline-invisible-p (line-end-position))
  21999. ;; If element is hidden, `move-end-of-line' would put point
  22000. ;; after it. Use `end-of-line' to stay on current line.
  22001. (call-interactively 'end-of-line))
  22002. (t (call-interactively move-fun)))))
  22003. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22004. (setq disable-point-adjustment
  22005. (or (not (invisible-p (point)))
  22006. (not (invisible-p (max (point-min) (1- (point))))))))
  22007. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22008. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22009. (defun org-backward-sentence (&optional _arg)
  22010. "Go to beginning of sentence, or beginning of table field.
  22011. This will call `backward-sentence' or `org-table-beginning-of-field',
  22012. depending on context."
  22013. (interactive)
  22014. (let* ((element (org-element-at-point))
  22015. (contents-begin (org-element-property :contents-begin element))
  22016. (table (org-element-lineage element '(table) t)))
  22017. (if (and table
  22018. (> (point) contents-begin)
  22019. (<= (point) (org-element-property :contents-end table)))
  22020. (call-interactively #'org-table-beginning-of-field)
  22021. (save-restriction
  22022. (when (and contents-begin
  22023. (< (point-min) contents-begin)
  22024. (> (point) contents-begin))
  22025. (narrow-to-region contents-begin
  22026. (org-element-property :contents-end element)))
  22027. (call-interactively #'backward-sentence)))))
  22028. (defun org-forward-sentence (&optional _arg)
  22029. "Go to end of sentence, or end of table field.
  22030. This will call `forward-sentence' or `org-table-end-of-field',
  22031. depending on context."
  22032. (interactive)
  22033. (let* ((element (org-element-at-point))
  22034. (contents-end (org-element-property :contents-end element))
  22035. (table (org-element-lineage element '(table) t)))
  22036. (if (and table
  22037. (>= (point) (org-element-property :contents-begin table))
  22038. (< (point) contents-end))
  22039. (call-interactively #'org-table-end-of-field)
  22040. (save-restriction
  22041. (when (and contents-end
  22042. (> (point-max) contents-end)
  22043. ;; Skip blank lines between elements.
  22044. (< (org-element-property :end element)
  22045. (save-excursion (goto-char contents-end)
  22046. (skip-chars-forward " \r\t\n"))))
  22047. (narrow-to-region (org-element-property :contents-begin element)
  22048. contents-end))
  22049. (call-interactively #'forward-sentence)))))
  22050. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22051. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22052. (defun org-kill-line (&optional _arg)
  22053. "Kill line, to tags or end of line."
  22054. (interactive)
  22055. (cond
  22056. ((or (not org-special-ctrl-k)
  22057. (bolp)
  22058. (not (org-at-heading-p)))
  22059. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22060. org-ctrl-k-protect-subtree)
  22061. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22062. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22063. (user-error "C-k aborted as it would kill a hidden subtree")))
  22064. (call-interactively
  22065. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22066. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22067. (kill-region (point) (match-beginning 1))
  22068. (org-set-tags nil t))
  22069. (t (kill-region (point) (point-at-eol)))))
  22070. (define-key org-mode-map "\C-k" 'org-kill-line)
  22071. (defun org-yank (&optional arg)
  22072. "Yank. If the kill is a subtree, treat it specially.
  22073. This command will look at the current kill and check if is a single
  22074. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22075. cursor is at the beginning of a line or after the stars of a currently
  22076. empty headline, then the yank is handled specially. How exactly depends
  22077. on the value of the following variables, both set by default.
  22078. `org-yank-folded-subtrees'
  22079. When set, the subtree(s) will be folded after insertion, but only
  22080. if doing so would now swallow text after the yanked text.
  22081. `org-yank-adjusted-subtrees'
  22082. When set, the subtree will be promoted or demoted in order to
  22083. fit into the local outline tree structure, which means that the
  22084. level will be adjusted so that it becomes the smaller one of the
  22085. two *visible* surrounding headings.
  22086. Any prefix to this command will cause `yank' to be called directly with
  22087. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22088. will just
  22089. plainly yank the text as it is.
  22090. \[1] The test checks if the first non-white line is a heading
  22091. and if there are no other headings with fewer stars."
  22092. (interactive "P")
  22093. (org-yank-generic 'yank arg))
  22094. (defun org-yank-generic (command arg)
  22095. "Perform some yank-like command.
  22096. This function implements the behavior described in the `org-yank'
  22097. documentation. However, it has been generalized to work for any
  22098. interactive command with similar behavior."
  22099. ;; pretend to be command COMMAND
  22100. (setq this-command command)
  22101. (if arg
  22102. (call-interactively command)
  22103. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22104. (and (org-kill-is-subtree-p)
  22105. (or (bolp)
  22106. (and (looking-at "[ \t]*$")
  22107. (string-match
  22108. "\\`\\*+\\'"
  22109. (buffer-substring (point-at-bol) (point)))))))
  22110. swallowp)
  22111. (cond
  22112. ((and subtreep org-yank-folded-subtrees)
  22113. (let ((beg (point))
  22114. end)
  22115. (if (and subtreep org-yank-adjusted-subtrees)
  22116. (org-paste-subtree nil nil 'for-yank)
  22117. (call-interactively command))
  22118. (setq end (point))
  22119. (goto-char beg)
  22120. (when (and (bolp) subtreep
  22121. (not (setq swallowp
  22122. (org-yank-folding-would-swallow-text beg end))))
  22123. (org-with-limited-levels
  22124. (or (looking-at org-outline-regexp)
  22125. (re-search-forward org-outline-regexp-bol end t))
  22126. (while (and (< (point) end) (looking-at org-outline-regexp))
  22127. (hide-subtree)
  22128. (org-cycle-show-empty-lines 'folded)
  22129. (condition-case nil
  22130. (outline-forward-same-level 1)
  22131. (error (goto-char end))))))
  22132. (when swallowp
  22133. (message
  22134. "Inserted text not folded because that would swallow text"))
  22135. (goto-char end)
  22136. (skip-chars-forward " \t\n\r")
  22137. (beginning-of-line 1)
  22138. (push-mark beg 'nomsg)))
  22139. ((and subtreep org-yank-adjusted-subtrees)
  22140. (let ((beg (point-at-bol)))
  22141. (org-paste-subtree nil nil 'for-yank)
  22142. (push-mark beg 'nomsg)))
  22143. (t
  22144. (call-interactively command))))))
  22145. (defun org-yank-folding-would-swallow-text (beg end)
  22146. "Would hide-subtree at BEG swallow any text after END?"
  22147. (let (level)
  22148. (org-with-limited-levels
  22149. (save-excursion
  22150. (goto-char beg)
  22151. (when (or (looking-at org-outline-regexp)
  22152. (re-search-forward org-outline-regexp-bol end t))
  22153. (setq level (org-outline-level)))
  22154. (goto-char end)
  22155. (skip-chars-forward " \t\r\n\v\f")
  22156. (if (or (eobp)
  22157. (and (bolp) (looking-at org-outline-regexp)
  22158. (<= (org-outline-level) level)))
  22159. nil ; Nothing would be swallowed
  22160. t))))) ; something would swallow
  22161. (define-key org-mode-map "\C-y" 'org-yank)
  22162. (defun org-truely-invisible-p ()
  22163. "Check if point is at a character currently not visible.
  22164. This version does not only check the character property, but also
  22165. `visible-mode'."
  22166. ;; Early versions of noutline don't have `outline-invisible-p'.
  22167. (if (org-bound-and-true-p visible-mode)
  22168. nil
  22169. (outline-invisible-p)))
  22170. (defun org-invisible-p2 ()
  22171. "Check if point is at a character currently not visible."
  22172. (save-excursion
  22173. (if (and (eolp) (not (bobp))) (backward-char 1))
  22174. ;; Early versions of noutline don't have `outline-invisible-p'.
  22175. (outline-invisible-p)))
  22176. (defun org-back-to-heading (&optional invisible-ok)
  22177. "Call `outline-back-to-heading', but provide a better error message."
  22178. (condition-case nil
  22179. (outline-back-to-heading invisible-ok)
  22180. (error (error "Before first headline at position %d in buffer %s"
  22181. (point) (current-buffer)))))
  22182. (defun org-before-first-heading-p ()
  22183. "Before first heading?"
  22184. (save-excursion
  22185. (end-of-line)
  22186. (null (re-search-backward org-outline-regexp-bol nil t))))
  22187. (defun org-at-heading-p (&optional ignored)
  22188. (outline-on-heading-p t))
  22189. ;; Compatibility alias with Org versions < 7.8.03
  22190. (defalias 'org-on-heading-p 'org-at-heading-p)
  22191. (defun org-in-commented-heading-p (&optional no-inheritance)
  22192. "Non-nil if point is under a commented heading.
  22193. This function also checks ancestors of the current headline,
  22194. unless optional argument NO-INHERITANCE is non-nil."
  22195. (cond
  22196. ((org-before-first-heading-p) nil)
  22197. ((let ((headline (nth 4 (org-heading-components))))
  22198. (and headline
  22199. (let ((case-fold-search nil))
  22200. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22201. headline)))))
  22202. (no-inheritance nil)
  22203. (t
  22204. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22205. (defun org-at-comment-p nil
  22206. "Is cursor in a commented line?"
  22207. (save-excursion
  22208. (save-match-data
  22209. (beginning-of-line)
  22210. (looking-at "^[ \t]*# "))))
  22211. (defun org-at-drawer-p nil
  22212. "Is cursor at a drawer keyword?"
  22213. (save-excursion
  22214. (move-beginning-of-line 1)
  22215. (looking-at org-drawer-regexp)))
  22216. (defun org-at-block-p nil
  22217. "Is cursor at a block keyword?"
  22218. (save-excursion
  22219. (move-beginning-of-line 1)
  22220. (looking-at org-block-regexp)))
  22221. (defun org-point-at-end-of-empty-headline ()
  22222. "If point is at the end of an empty headline, return t, else nil.
  22223. If the heading only contains a TODO keyword, it is still still considered
  22224. empty."
  22225. (and (looking-at "[ \t]*$")
  22226. (when org-todo-line-regexp
  22227. (save-excursion
  22228. (beginning-of-line 1)
  22229. (let ((case-fold-search nil))
  22230. (looking-at org-todo-line-regexp)
  22231. (string= (match-string 3) ""))))))
  22232. (defun org-at-heading-or-item-p ()
  22233. (or (org-at-heading-p) (org-at-item-p)))
  22234. (defun org-at-target-p ()
  22235. (or (org-in-regexp org-radio-target-regexp)
  22236. (org-in-regexp org-target-regexp)))
  22237. ;; Compatibility alias with Org versions < 7.8.03
  22238. (defalias 'org-on-target-p 'org-at-target-p)
  22239. (defun org-up-heading-all (arg)
  22240. "Move to the heading line of which the present line is a subheading.
  22241. This function considers both visible and invisible heading lines.
  22242. With argument, move up ARG levels."
  22243. (if (fboundp 'outline-up-heading-all)
  22244. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22245. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22246. (defun org-up-heading-safe ()
  22247. "Move to the heading line of which the present line is a subheading.
  22248. This version will not throw an error. It will return the level of the
  22249. headline found, or nil if no higher level is found.
  22250. Also, this function will be a lot faster than `outline-up-heading',
  22251. because it relies on stars being the outline starters. This can really
  22252. make a significant difference in outlines with very many siblings."
  22253. (when (ignore-errors (org-back-to-heading t))
  22254. (let ((level-up (1- (funcall outline-level))))
  22255. (and (> level-up 0)
  22256. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22257. (funcall outline-level)))))
  22258. (defun org-first-sibling-p ()
  22259. "Is this heading the first child of its parents?"
  22260. (interactive)
  22261. (let ((re org-outline-regexp-bol)
  22262. level l)
  22263. (unless (org-at-heading-p t)
  22264. (user-error "Not at a heading"))
  22265. (setq level (funcall outline-level))
  22266. (save-excursion
  22267. (if (not (re-search-backward re nil t))
  22268. t
  22269. (setq l (funcall outline-level))
  22270. (< l level)))))
  22271. (defun org-goto-sibling (&optional previous)
  22272. "Goto the next sibling, even if it is invisible.
  22273. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22274. when a sibling was found. When none is found, return nil and don't
  22275. move point."
  22276. (let ((fun (if previous 're-search-backward 're-search-forward))
  22277. (pos (point))
  22278. (re org-outline-regexp-bol)
  22279. level l)
  22280. (when (ignore-errors (org-back-to-heading t))
  22281. (setq level (funcall outline-level))
  22282. (catch 'exit
  22283. (or previous (forward-char 1))
  22284. (while (funcall fun re nil t)
  22285. (setq l (funcall outline-level))
  22286. (when (< l level) (goto-char pos) (throw 'exit nil))
  22287. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22288. (goto-char pos)
  22289. nil))))
  22290. (defun org-show-siblings ()
  22291. "Show all siblings of the current headline."
  22292. (save-excursion
  22293. (while (org-goto-sibling) (org-flag-heading nil)))
  22294. (save-excursion
  22295. (while (org-goto-sibling 'previous)
  22296. (org-flag-heading nil))))
  22297. (defun org-goto-first-child ()
  22298. "Goto the first child, even if it is invisible.
  22299. Return t when a child was found. Otherwise don't move point and
  22300. return nil."
  22301. (let (level (pos (point)) (re org-outline-regexp-bol))
  22302. (when (ignore-errors (org-back-to-heading t))
  22303. (setq level (outline-level))
  22304. (forward-char 1)
  22305. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22306. (progn (goto-char (match-beginning 0)) t)
  22307. (goto-char pos) nil))))
  22308. (defun org-show-hidden-entry ()
  22309. "Show an entry where even the heading is hidden."
  22310. (save-excursion
  22311. (org-show-entry)))
  22312. (defun org-flag-heading (flag &optional entry)
  22313. "Flag the current heading. FLAG non-nil means make invisible.
  22314. When ENTRY is non-nil, show the entire entry."
  22315. (save-excursion
  22316. (org-back-to-heading t)
  22317. ;; Check if we should show the entire entry
  22318. (if entry
  22319. (progn
  22320. (org-show-entry)
  22321. (save-excursion
  22322. (and (outline-next-heading)
  22323. (org-flag-heading nil))))
  22324. (outline-flag-region (max (point-min) (1- (point)))
  22325. (save-excursion (outline-end-of-heading) (point))
  22326. flag))))
  22327. (defun org-get-next-sibling ()
  22328. "Move to next heading of the same level, and return point.
  22329. If there is no such heading, return nil.
  22330. This is like outline-next-sibling, but invisible headings are ok."
  22331. (let ((level (funcall outline-level)))
  22332. (outline-next-heading)
  22333. (while (and (not (eobp)) (> (funcall outline-level) level))
  22334. (outline-next-heading))
  22335. (if (or (eobp) (< (funcall outline-level) level))
  22336. nil
  22337. (point))))
  22338. (defun org-get-last-sibling ()
  22339. "Move to previous heading of the same level, and return point.
  22340. If there is no such heading, return nil."
  22341. (let ((opoint (point))
  22342. (level (funcall outline-level)))
  22343. (outline-previous-heading)
  22344. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22345. (while (and (> (funcall outline-level) level)
  22346. (not (bobp)))
  22347. (outline-previous-heading))
  22348. (if (< (funcall outline-level) level)
  22349. nil
  22350. (point)))))
  22351. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22352. "Goto to the end of a subtree."
  22353. ;; This contains an exact copy of the original function, but it uses
  22354. ;; `org-back-to-heading', to make it work also in invisible
  22355. ;; trees. And is uses an invisible-ok argument.
  22356. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22357. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22358. ;; with many children and grandchildren etc, this can be much faster
  22359. ;; than the outline version.
  22360. (org-back-to-heading invisible-ok)
  22361. (let ((first t)
  22362. (level (funcall outline-level)))
  22363. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22364. ;; A true heading (not a plain list item), in Org-mode
  22365. ;; This means we can easily find the end by looking
  22366. ;; only for the right number of stars. Using a regexp to do
  22367. ;; this is so much faster than using a Lisp loop.
  22368. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22369. (forward-char 1)
  22370. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22371. ;; something else, do it the slow way
  22372. (while (and (not (eobp))
  22373. (or first (> (funcall outline-level) level)))
  22374. (setq first nil)
  22375. (outline-next-heading)))
  22376. (unless to-heading
  22377. (if (memq (preceding-char) '(?\n ?\^M))
  22378. (progn
  22379. ;; Go to end of line before heading
  22380. (forward-char -1)
  22381. (if (memq (preceding-char) '(?\n ?\^M))
  22382. ;; leave blank line before heading
  22383. (forward-char -1))))))
  22384. (point))
  22385. (defun org-end-of-meta-data (&optional full)
  22386. "Skip planning line and properties drawer in current entry.
  22387. When optional argument FULL is non-nil, also skip empty lines,
  22388. clocking lines and regular drawers at the beginning of the
  22389. entry."
  22390. (org-back-to-heading t)
  22391. (forward-line)
  22392. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22393. (when (looking-at org-property-drawer-re)
  22394. (goto-char (match-end 0))
  22395. (forward-line))
  22396. (when (and full (not (org-at-heading-p)))
  22397. (catch 'exit
  22398. (let ((end (save-excursion (outline-next-heading) (point)))
  22399. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22400. (while (not (eobp))
  22401. (cond ((org-looking-at-p org-drawer-regexp)
  22402. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22403. (forward-line)
  22404. (throw 'exit t)))
  22405. ((org-looking-at-p re) (forward-line))
  22406. (t (throw 'exit t))))))))
  22407. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22408. "Move forward to the ARG'th subheading at same level as this one.
  22409. Stop at the first and last subheadings of a superior heading.
  22410. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22411. non-nil it will also look at invisible ones."
  22412. (interactive "p")
  22413. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22414. (if (and arg (< arg 0))
  22415. (goto-char (point-min))
  22416. (outline-next-heading))
  22417. (org-at-heading-p)
  22418. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22419. (f (if (and arg (< arg 0))
  22420. 're-search-backward
  22421. 're-search-forward))
  22422. (count (if arg (abs arg) 1))
  22423. (result (point)))
  22424. (while (and (prog1 (> count 0)
  22425. (forward-char (if (and arg (< arg 0)) -1 1)))
  22426. (funcall f org-outline-regexp-bol nil 'move))
  22427. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22428. (cond ((< l level) (setq count 0))
  22429. ((and (= l level)
  22430. (or invisible-ok
  22431. (progn
  22432. (goto-char (line-beginning-position))
  22433. (not (outline-invisible-p)))))
  22434. (setq count (1- count))
  22435. (when (eq l level)
  22436. (setq result (point)))))))
  22437. (goto-char result))
  22438. (beginning-of-line 1)))
  22439. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22440. "Move backward to the ARG'th subheading at same level as this one.
  22441. Stop at the first and last subheadings of a superior heading."
  22442. (interactive "p")
  22443. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22444. (defun org-next-visible-heading (arg)
  22445. "Move to the next visible heading.
  22446. This function wraps `outline-next-visible-heading' with
  22447. `org-with-limited-levels' in order to skip over inline tasks and
  22448. respect customization of `org-odd-levels-only'."
  22449. (interactive "p")
  22450. (org-with-limited-levels
  22451. (outline-next-visible-heading arg)))
  22452. (defun org-previous-visible-heading (arg)
  22453. "Move to the next visible heading.
  22454. This function wraps `outline-previous-visible-heading' with
  22455. `org-with-limited-levels' in order to skip over inline tasks and
  22456. respect customization of `org-odd-levels-only'."
  22457. (interactive "p")
  22458. (org-with-limited-levels
  22459. (outline-previous-visible-heading arg)))
  22460. (defun org-next-block (arg &optional backward block-regexp)
  22461. "Jump to the next block.
  22462. With a prefix argument ARG, jump forward ARG many blocks.
  22463. When BACKWARD is non-nil, jump to the previous block.
  22464. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22465. Match data is set according to this regexp when the function
  22466. returns.
  22467. Return point at beginning of the opening line of found block.
  22468. Throw an error if no block is found."
  22469. (interactive "p")
  22470. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22471. (case-fold-search t)
  22472. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22473. (count (or arg 1))
  22474. (origin (point))
  22475. last-element)
  22476. (if backward (beginning-of-line) (end-of-line))
  22477. (while (and (> count 0) (funcall search-fn re nil t))
  22478. (let ((element (save-excursion
  22479. (goto-char (match-beginning 0))
  22480. (save-match-data (org-element-at-point)))))
  22481. (when (and (memq (org-element-type element)
  22482. '(center-block comment-block dynamic-block
  22483. example-block export-block quote-block
  22484. special-block src-block verse-block))
  22485. (<= (match-beginning 0)
  22486. (org-element-property :post-affiliated element)))
  22487. (setq last-element element)
  22488. (decf count))))
  22489. (if (= count 0)
  22490. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22491. (save-match-data (org-show-context)))
  22492. (goto-char origin)
  22493. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22494. (defun org-previous-block (arg &optional block-regexp)
  22495. "Jump to the previous block.
  22496. With a prefix argument ARG, jump backward ARG many source blocks.
  22497. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22498. (interactive "p")
  22499. (org-next-block arg t block-regexp))
  22500. (defun org-forward-paragraph ()
  22501. "Move forward to beginning of next paragraph or equivalent.
  22502. The function moves point to the beginning of the next visible
  22503. structural element, which can be a paragraph, a table, a list
  22504. item, etc. It also provides some special moves for convenience:
  22505. - On an affiliated keyword, jump to the beginning of the
  22506. relative element.
  22507. - On an item or a footnote definition, move to the second
  22508. element inside, if any.
  22509. - On a table or a property drawer, jump after it.
  22510. - On a verse or source block, stop after blank lines."
  22511. (interactive)
  22512. (when (eobp) (user-error "Cannot move further down"))
  22513. (let* ((deactivate-mark nil)
  22514. (element (org-element-at-point))
  22515. (type (org-element-type element))
  22516. (post-affiliated (org-element-property :post-affiliated element))
  22517. (contents-begin (org-element-property :contents-begin element))
  22518. (contents-end (org-element-property :contents-end element))
  22519. (end (let ((end (org-element-property :end element)) (parent element))
  22520. (while (and (setq parent (org-element-property :parent parent))
  22521. (= (org-element-property :contents-end parent) end))
  22522. (setq end (org-element-property :end parent)))
  22523. end)))
  22524. (cond ((not element)
  22525. (skip-chars-forward " \r\t\n")
  22526. (or (eobp) (beginning-of-line)))
  22527. ;; On affiliated keywords, move to element's beginning.
  22528. ((< (point) post-affiliated)
  22529. (goto-char post-affiliated))
  22530. ;; At a table row, move to the end of the table. Similarly,
  22531. ;; at a node property, move to the end of the property
  22532. ;; drawer.
  22533. ((memq type '(node-property table-row))
  22534. (goto-char (org-element-property
  22535. :end (org-element-property :parent element))))
  22536. ((memq type '(property-drawer table)) (goto-char end))
  22537. ;; Consider blank lines as separators in verse and source
  22538. ;; blocks to ease editing.
  22539. ((memq type '(src-block verse-block))
  22540. (when (eq type 'src-block)
  22541. (setq contents-end
  22542. (save-excursion (goto-char end)
  22543. (skip-chars-backward " \r\t\n")
  22544. (line-beginning-position))))
  22545. (beginning-of-line)
  22546. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22547. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22548. (goto-char end)
  22549. (skip-chars-forward " \r\t\n")
  22550. (if (= (point) contents-end) (goto-char end)
  22551. (beginning-of-line))))
  22552. ;; With no contents, just skip element.
  22553. ((not contents-begin) (goto-char end))
  22554. ;; If contents are invisible, skip the element altogether.
  22555. ((outline-invisible-p (line-end-position))
  22556. (case type
  22557. (headline
  22558. (org-with-limited-levels (outline-next-visible-heading 1)))
  22559. ;; At a plain list, make sure we move to the next item
  22560. ;; instead of skipping the whole list.
  22561. (plain-list (forward-char)
  22562. (org-forward-paragraph))
  22563. (otherwise (goto-char end))))
  22564. ((>= (point) contents-end) (goto-char end))
  22565. ((>= (point) contents-begin)
  22566. ;; This can only happen on paragraphs and plain lists.
  22567. (case type
  22568. (paragraph (goto-char end))
  22569. ;; At a plain list, try to move to second element in
  22570. ;; first item, if possible.
  22571. (plain-list (end-of-line)
  22572. (org-forward-paragraph))))
  22573. ;; When contents start on the middle of a line (e.g. in
  22574. ;; items and footnote definitions), try to reach first
  22575. ;; element starting after current line.
  22576. ((> (line-end-position) contents-begin)
  22577. (end-of-line)
  22578. (org-forward-paragraph))
  22579. (t (goto-char contents-begin)))))
  22580. (defun org-backward-paragraph ()
  22581. "Move backward to start of previous paragraph or equivalent.
  22582. The function moves point to the beginning of the current
  22583. structural element, which can be a paragraph, a table, a list
  22584. item, etc., or to the beginning of the previous visible one if
  22585. point is already there. It also provides some special moves for
  22586. convenience:
  22587. - On an affiliated keyword, jump to the first one.
  22588. - On a table or a property drawer, move to its beginning.
  22589. - On a verse or source block, stop before blank lines."
  22590. (interactive)
  22591. (when (bobp) (user-error "Cannot move further up"))
  22592. (let* ((deactivate-mark nil)
  22593. (element (org-element-at-point))
  22594. (type (org-element-type element))
  22595. (contents-begin (org-element-property :contents-begin element))
  22596. (contents-end (org-element-property :contents-end element))
  22597. (post-affiliated (org-element-property :post-affiliated element))
  22598. (begin (org-element-property :begin element)))
  22599. (cond
  22600. ((not element) (goto-char (point-min)))
  22601. ((= (point) begin)
  22602. (backward-char)
  22603. (org-backward-paragraph))
  22604. ((<= (point) post-affiliated) (goto-char begin))
  22605. ((memq type '(node-property table-row))
  22606. (goto-char (org-element-property
  22607. :post-affiliated (org-element-property :parent element))))
  22608. ((memq type '(property-drawer table)) (goto-char begin))
  22609. ((memq type '(src-block verse-block))
  22610. (when (eq type 'src-block)
  22611. (setq contents-begin
  22612. (save-excursion (goto-char begin) (forward-line) (point))))
  22613. (if (= (point) contents-begin) (goto-char post-affiliated)
  22614. ;; Inside a verse block, see blank lines as paragraph
  22615. ;; separators.
  22616. (let ((origin (point)))
  22617. (skip-chars-backward " \r\t\n" contents-begin)
  22618. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22619. (skip-chars-forward " \r\t\n" origin)
  22620. (if (= (point) origin) (goto-char contents-begin)
  22621. (beginning-of-line))))))
  22622. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22623. ((eq type 'paragraph)
  22624. (goto-char contents-begin)
  22625. ;; When at first paragraph in an item or a footnote definition,
  22626. ;; move directly to beginning of line.
  22627. (let ((parent-contents
  22628. (org-element-property
  22629. :contents-begin (org-element-property :parent element))))
  22630. (when (and parent-contents (= parent-contents contents-begin))
  22631. (beginning-of-line))))
  22632. ;; At the end of a greater element, move to the beginning of the
  22633. ;; last element within.
  22634. ((>= (point) contents-end)
  22635. (goto-char (1- contents-end))
  22636. (org-backward-paragraph))
  22637. (t (goto-char (or post-affiliated begin))))
  22638. ;; Ensure we never leave point invisible.
  22639. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22640. (defun org-forward-element ()
  22641. "Move forward by one element.
  22642. Move to the next element at the same level, when possible."
  22643. (interactive)
  22644. (cond ((eobp) (user-error "Cannot move further down"))
  22645. ((org-with-limited-levels (org-at-heading-p))
  22646. (let ((origin (point)))
  22647. (goto-char (org-end-of-subtree nil t))
  22648. (unless (org-with-limited-levels (org-at-heading-p))
  22649. (goto-char origin)
  22650. (user-error "Cannot move further down"))))
  22651. (t
  22652. (let* ((elem (org-element-at-point))
  22653. (end (org-element-property :end elem))
  22654. (parent (org-element-property :parent elem)))
  22655. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22656. (goto-char (org-element-property :end parent)))
  22657. ((integer-or-marker-p end) (goto-char end))
  22658. (t (message "No element at point")))))))
  22659. (defun org-backward-element ()
  22660. "Move backward by one element.
  22661. Move to the previous element at the same level, when possible."
  22662. (interactive)
  22663. (cond ((bobp) (user-error "Cannot move further up"))
  22664. ((org-with-limited-levels (org-at-heading-p))
  22665. ;; At a headline, move to the previous one, if any, or stay
  22666. ;; here.
  22667. (let ((origin (point)))
  22668. (org-with-limited-levels (org-backward-heading-same-level 1))
  22669. ;; When current headline has no sibling above, move to its
  22670. ;; parent.
  22671. (when (= (point) origin)
  22672. (or (org-with-limited-levels (org-up-heading-safe))
  22673. (progn (goto-char origin)
  22674. (user-error "Cannot move further up"))))))
  22675. (t
  22676. (let* ((elem (org-element-at-point))
  22677. (beg (org-element-property :begin elem)))
  22678. (cond
  22679. ;; Move to beginning of current element if point isn't
  22680. ;; there already.
  22681. ((null beg) (message "No element at point"))
  22682. ((/= (point) beg) (goto-char beg))
  22683. (t (goto-char beg)
  22684. (skip-chars-backward " \r\t\n")
  22685. (unless (bobp)
  22686. (let ((prev (org-element-at-point)))
  22687. (goto-char (org-element-property :begin prev))
  22688. (while (and (setq prev (org-element-property :parent prev))
  22689. (<= (org-element-property :end prev) beg))
  22690. (goto-char (org-element-property :begin prev)))))))))))
  22691. (defun org-up-element ()
  22692. "Move to upper element."
  22693. (interactive)
  22694. (if (org-with-limited-levels (org-at-heading-p))
  22695. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22696. (let* ((elem (org-element-at-point))
  22697. (parent (org-element-property :parent elem)))
  22698. (if parent (goto-char (org-element-property :begin parent))
  22699. (if (org-with-limited-levels (org-before-first-heading-p))
  22700. (user-error "No surrounding element")
  22701. (org-with-limited-levels (org-back-to-heading)))))))
  22702. (defvar org-element-greater-elements)
  22703. (defun org-down-element ()
  22704. "Move to inner element."
  22705. (interactive)
  22706. (let ((element (org-element-at-point)))
  22707. (cond
  22708. ((memq (org-element-type element) '(plain-list table))
  22709. (goto-char (org-element-property :contents-begin element))
  22710. (forward-char))
  22711. ((memq (org-element-type element) org-element-greater-elements)
  22712. ;; If contents are hidden, first disclose them.
  22713. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22714. (goto-char (or (org-element-property :contents-begin element)
  22715. (user-error "No content for this element"))))
  22716. (t (user-error "No inner element")))))
  22717. (defun org-drag-element-backward ()
  22718. "Move backward element at point."
  22719. (interactive)
  22720. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22721. (let* ((elem (org-element-at-point))
  22722. (prev-elem
  22723. (save-excursion
  22724. (goto-char (org-element-property :begin elem))
  22725. (skip-chars-backward " \r\t\n")
  22726. (unless (bobp)
  22727. (let* ((beg (org-element-property :begin elem))
  22728. (prev (org-element-at-point))
  22729. (up prev))
  22730. (while (and (setq up (org-element-property :parent up))
  22731. (<= (org-element-property :end up) beg))
  22732. (setq prev up))
  22733. prev)))))
  22734. ;; Error out if no previous element or previous element is
  22735. ;; a parent of the current one.
  22736. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22737. (user-error "Cannot drag element backward")
  22738. (let ((pos (point)))
  22739. (org-element-swap-A-B prev-elem elem)
  22740. (goto-char (+ (org-element-property :begin prev-elem)
  22741. (- pos (org-element-property :begin elem)))))))))
  22742. (defun org-drag-element-forward ()
  22743. "Move forward element at point."
  22744. (interactive)
  22745. (let* ((pos (point))
  22746. (elem (org-element-at-point)))
  22747. (when (= (point-max) (org-element-property :end elem))
  22748. (user-error "Cannot drag element forward"))
  22749. (goto-char (org-element-property :end elem))
  22750. (let ((next-elem (org-element-at-point)))
  22751. (when (or (org-element-nested-p elem next-elem)
  22752. (and (eq (org-element-type next-elem) 'headline)
  22753. (not (eq (org-element-type elem) 'headline))))
  22754. (goto-char pos)
  22755. (user-error "Cannot drag element forward"))
  22756. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22757. ;; body's length (without final blanks) and by the length of
  22758. ;; blanks between ELEM and NEXT-ELEM.
  22759. (let ((size-next (- (save-excursion
  22760. (goto-char (org-element-property :end next-elem))
  22761. (skip-chars-backward " \r\t\n")
  22762. (forward-line)
  22763. ;; Small correction if buffer doesn't end
  22764. ;; with a newline character.
  22765. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22766. (org-element-property :begin next-elem)))
  22767. (size-blank (- (org-element-property :end elem)
  22768. (save-excursion
  22769. (goto-char (org-element-property :end elem))
  22770. (skip-chars-backward " \r\t\n")
  22771. (forward-line)
  22772. (point)))))
  22773. (org-element-swap-A-B elem next-elem)
  22774. (goto-char (+ pos size-next size-blank))))))
  22775. (defun org-drag-line-forward (arg)
  22776. "Drag the line at point ARG lines forward."
  22777. (interactive "p")
  22778. (dotimes (n (abs arg))
  22779. (let ((c (current-column)))
  22780. (if (< 0 arg)
  22781. (progn
  22782. (beginning-of-line 2)
  22783. (transpose-lines 1)
  22784. (beginning-of-line 0))
  22785. (transpose-lines 1)
  22786. (beginning-of-line -1))
  22787. (org-move-to-column c))))
  22788. (defun org-drag-line-backward (arg)
  22789. "Drag the line at point ARG lines backward."
  22790. (interactive "p")
  22791. (org-drag-line-forward (- arg)))
  22792. (defun org-mark-element ()
  22793. "Put point at beginning of this element, mark at end.
  22794. Interactively, if this command is repeated or (in Transient Mark
  22795. mode) if the mark is active, it marks the next element after the
  22796. ones already marked."
  22797. (interactive)
  22798. (let (deactivate-mark)
  22799. (if (and (org-called-interactively-p 'any)
  22800. (or (and (eq last-command this-command) (mark t))
  22801. (and transient-mark-mode mark-active)))
  22802. (set-mark
  22803. (save-excursion
  22804. (goto-char (mark))
  22805. (goto-char (org-element-property :end (org-element-at-point)))))
  22806. (let ((element (org-element-at-point)))
  22807. (end-of-line)
  22808. (push-mark (org-element-property :end element) t t)
  22809. (goto-char (org-element-property :begin element))))))
  22810. (defun org-narrow-to-element ()
  22811. "Narrow buffer to current element."
  22812. (interactive)
  22813. (let ((elem (org-element-at-point)))
  22814. (cond
  22815. ((eq (car elem) 'headline)
  22816. (narrow-to-region
  22817. (org-element-property :begin elem)
  22818. (org-element-property :end elem)))
  22819. ((memq (car elem) org-element-greater-elements)
  22820. (narrow-to-region
  22821. (org-element-property :contents-begin elem)
  22822. (org-element-property :contents-end elem)))
  22823. (t
  22824. (narrow-to-region
  22825. (org-element-property :begin elem)
  22826. (org-element-property :end elem))))))
  22827. (defun org-transpose-element ()
  22828. "Transpose current and previous elements, keeping blank lines between.
  22829. Point is moved after both elements."
  22830. (interactive)
  22831. (org-skip-whitespace)
  22832. (let ((end (org-element-property :end (org-element-at-point))))
  22833. (org-drag-element-backward)
  22834. (goto-char end)))
  22835. (defun org-unindent-buffer ()
  22836. "Un-indent the visible part of the buffer.
  22837. Relative indentation (between items, inside blocks, etc.) isn't
  22838. modified."
  22839. (interactive)
  22840. (unless (eq major-mode 'org-mode)
  22841. (user-error "Cannot un-indent a buffer not in Org mode"))
  22842. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22843. unindent-tree ; For byte-compiler.
  22844. (unindent-tree
  22845. (function
  22846. (lambda (contents)
  22847. (mapc
  22848. (lambda (element)
  22849. (if (memq (org-element-type element) '(headline section))
  22850. (funcall unindent-tree (org-element-contents element))
  22851. (save-excursion
  22852. (save-restriction
  22853. (narrow-to-region
  22854. (org-element-property :begin element)
  22855. (org-element-property :end element))
  22856. (org-do-remove-indentation)))))
  22857. (reverse contents))))))
  22858. (funcall unindent-tree (org-element-contents parse-tree))))
  22859. (defun org-show-children (&optional level)
  22860. "Show all direct subheadings of this heading.
  22861. Prefix arg LEVEL is how many levels below the current level should be shown.
  22862. Default is enough to cause the following heading to appear."
  22863. (save-excursion
  22864. (org-back-to-heading t)
  22865. (let* ((current-level (funcall outline-level))
  22866. (max-level (org-get-valid-level
  22867. current-level (if level (prefix-numeric-value level) 1)))
  22868. (end (save-excursion (org-end-of-subtree t t)))
  22869. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22870. (past-first-child nil)
  22871. ;; Make sure to skip inlinetasks.
  22872. (re (format regexp-fmt
  22873. current-level
  22874. (cond
  22875. ((not (featurep 'org-inlinetask)) "")
  22876. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22877. 3))
  22878. (t (1- org-inlinetask-min-level))))))
  22879. ;; Display parent heading.
  22880. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22881. (forward-line)
  22882. ;; Display children. First child may be deeper than expected
  22883. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22884. ;; MAX-LEVEL accordingly.
  22885. (while (re-search-forward re end t)
  22886. (unless past-first-child
  22887. (setq re (format regexp-fmt
  22888. current-level
  22889. (max (funcall outline-level) max-level)))
  22890. (setq past-first-child t))
  22891. (outline-flag-region (line-end-position 0) (line-end-position) nil)))))
  22892. (defun org-show-subtree ()
  22893. "Show everything after this heading at deeper levels."
  22894. (interactive)
  22895. (outline-flag-region
  22896. (point)
  22897. (save-excursion
  22898. (org-end-of-subtree t t))
  22899. nil))
  22900. (defun org-show-entry ()
  22901. "Show the body directly following this heading.
  22902. Show the heading too, if it is currently invisible."
  22903. (interactive)
  22904. (save-excursion
  22905. (ignore-errors
  22906. (org-back-to-heading t)
  22907. (outline-flag-region
  22908. (max (point-min) (1- (point)))
  22909. (save-excursion
  22910. (if (re-search-forward
  22911. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22912. (match-beginning 1)
  22913. (point-max)))
  22914. nil)
  22915. (org-cycle-hide-drawers 'children))))
  22916. (defun org-make-options-regexp (kwds &optional extra)
  22917. "Make a regular expression for keyword lines.
  22918. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22919. when non-nil, is a regexp matching keywords names."
  22920. (concat "^[ \t]*#\\+\\("
  22921. (regexp-opt kwds)
  22922. (and extra (concat (and kwds "\\|") extra))
  22923. "\\):[ \t]*\\(.*\\)"))
  22924. ;; Make isearch reveal the necessary context
  22925. (defun org-isearch-end ()
  22926. "Reveal context after isearch exits."
  22927. (when isearch-success ; only if search was successful
  22928. (if (featurep 'xemacs)
  22929. ;; Under XEmacs, the hook is run in the correct place,
  22930. ;; we directly show the context.
  22931. (org-show-context 'isearch)
  22932. ;; In Emacs the hook runs *before* restoring the overlays.
  22933. ;; So we have to use a one-time post-command-hook to do this.
  22934. ;; (Emacs 22 has a special variable, see function `org-mode')
  22935. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22936. isearch-mode-end-hook-quit)
  22937. ;; Only when the isearch was not quitted.
  22938. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22939. 'append 'local)))))
  22940. (defun org-isearch-post-command ()
  22941. "Remove self from hook, and show context."
  22942. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22943. (org-show-context 'isearch))
  22944. ;;;; Integration with and fixes for other packages
  22945. ;;; Imenu support
  22946. (defvar org-imenu-markers nil
  22947. "All markers currently used by Imenu.")
  22948. (make-variable-buffer-local 'org-imenu-markers)
  22949. (defun org-imenu-new-marker (&optional pos)
  22950. "Return a new marker for use by Imenu, and remember the marker."
  22951. (let ((m (make-marker)))
  22952. (move-marker m (or pos (point)))
  22953. (push m org-imenu-markers)
  22954. m))
  22955. (defun org-imenu-get-tree ()
  22956. "Produce the index for Imenu."
  22957. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22958. (setq org-imenu-markers nil)
  22959. (let* ((n org-imenu-depth)
  22960. (re (concat "^" (org-get-limited-outline-regexp)))
  22961. (subs (make-vector (1+ n) nil))
  22962. (last-level 0)
  22963. m level head0 head)
  22964. (save-excursion
  22965. (save-restriction
  22966. (widen)
  22967. (goto-char (point-max))
  22968. (while (re-search-backward re nil t)
  22969. (setq level (org-reduced-level (funcall outline-level)))
  22970. (when (and (<= level n)
  22971. (looking-at org-complex-heading-regexp)
  22972. (setq head0 (org-match-string-no-properties 4)))
  22973. (setq head (org-link-display-format head0)
  22974. m (org-imenu-new-marker))
  22975. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22976. (if (>= level last-level)
  22977. (push (cons head m) (aref subs level))
  22978. (push (cons head (aref subs (1+ level))) (aref subs level))
  22979. (loop for i from (1+ level) to n do (aset subs i nil)))
  22980. (setq last-level level)))))
  22981. (aref subs 1)))
  22982. (eval-after-load "imenu"
  22983. '(progn
  22984. (add-hook 'imenu-after-jump-hook
  22985. (lambda ()
  22986. (if (derived-mode-p 'org-mode)
  22987. (org-show-context 'org-goto))))))
  22988. (defun org-link-display-format (link)
  22989. "Replace a link with its the description.
  22990. If there is no description, use the link target."
  22991. (save-match-data
  22992. (if (string-match org-bracket-link-analytic-regexp link)
  22993. (replace-match (if (match-end 5)
  22994. (match-string 5 link)
  22995. (concat (match-string 1 link)
  22996. (match-string 3 link)))
  22997. nil t link)
  22998. link)))
  22999. (defun org-toggle-link-display ()
  23000. "Toggle the literal or descriptive display of links."
  23001. (interactive)
  23002. (if org-descriptive-links
  23003. (progn (org-remove-from-invisibility-spec '(org-link))
  23004. (org-restart-font-lock)
  23005. (setq org-descriptive-links nil))
  23006. (progn (add-to-invisibility-spec '(org-link))
  23007. (org-restart-font-lock)
  23008. (setq org-descriptive-links t))))
  23009. ;; Speedbar support
  23010. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23011. "Overlay marking the agenda restriction line in speedbar.")
  23012. (overlay-put org-speedbar-restriction-lock-overlay
  23013. 'face 'org-agenda-restriction-lock)
  23014. (overlay-put org-speedbar-restriction-lock-overlay
  23015. 'help-echo "Agendas are currently limited to this item.")
  23016. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23017. (defun org-speedbar-set-agenda-restriction ()
  23018. "Restrict future agenda commands to the location at point in speedbar.
  23019. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23020. (interactive)
  23021. (require 'org-agenda)
  23022. (let (p m tp np dir txt)
  23023. (cond
  23024. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23025. 'org-imenu t))
  23026. (setq m (get-text-property p 'org-imenu-marker))
  23027. (with-current-buffer (marker-buffer m)
  23028. (goto-char m)
  23029. (org-agenda-set-restriction-lock 'subtree)))
  23030. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23031. 'speedbar-function 'speedbar-find-file))
  23032. (setq tp (previous-single-property-change
  23033. (1+ p) 'speedbar-function)
  23034. np (next-single-property-change
  23035. tp 'speedbar-function)
  23036. dir (speedbar-line-directory)
  23037. txt (buffer-substring-no-properties (or tp (point-min))
  23038. (or np (point-max))))
  23039. (with-current-buffer (find-file-noselect
  23040. (let ((default-directory dir))
  23041. (expand-file-name txt)))
  23042. (unless (derived-mode-p 'org-mode)
  23043. (user-error "Cannot restrict to non-Org-mode file"))
  23044. (org-agenda-set-restriction-lock 'file)))
  23045. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23046. (move-overlay org-speedbar-restriction-lock-overlay
  23047. (point-at-bol) (point-at-eol))
  23048. (setq current-prefix-arg nil)
  23049. (org-agenda-maybe-redo)))
  23050. (defvar speedbar-file-key-map)
  23051. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23052. (eval-after-load "speedbar"
  23053. '(progn
  23054. (speedbar-add-supported-extension ".org")
  23055. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23056. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23057. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23058. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23059. (add-hook 'speedbar-visiting-tag-hook
  23060. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23061. ;;; Fixes and Hacks for problems with other packages
  23062. (defun org--flyspell-object-check-p (element)
  23063. "Non-nil when Flyspell can check object at point.
  23064. ELEMENT is the element at point."
  23065. (let ((object (save-excursion
  23066. (when (org-looking-at-p "\\>") (backward-char))
  23067. (org-element-context element))))
  23068. (case (org-element-type object)
  23069. ;; Prevent checks in links due to keybinding conflict with
  23070. ;; Flyspell.
  23071. ((code entity export-snippet inline-babel-call
  23072. inline-src-block line-break latex-fragment link macro
  23073. statistics-cookie target timestamp verbatim)
  23074. nil)
  23075. (footnote-reference
  23076. ;; Only in inline footnotes, within the definition.
  23077. (and (eq (org-element-property :type object) 'inline)
  23078. (< (save-excursion
  23079. (goto-char (org-element-property :begin object))
  23080. (search-forward ":" nil t 2))
  23081. (point))))
  23082. (otherwise t))))
  23083. (defun org-mode-flyspell-verify ()
  23084. "Function used for `flyspell-generic-check-word-predicate'."
  23085. (if (org-at-heading-p)
  23086. ;; At a headline or an inlinetask, check title only. This is
  23087. ;; faster than relying on `org-element-at-point'.
  23088. (and (save-excursion (beginning-of-line)
  23089. (and (let ((case-fold-search t))
  23090. (not (looking-at "\\*+ END[ \t]*$")))
  23091. (looking-at org-complex-heading-regexp)))
  23092. (match-beginning 4)
  23093. (>= (point) (match-beginning 4))
  23094. (or (not (match-beginning 5))
  23095. (< (point) (match-beginning 5))))
  23096. (let* ((element (org-element-at-point))
  23097. (post-affiliated (org-element-property :post-affiliated element)))
  23098. (cond
  23099. ;; Ignore checks in all affiliated keywords but captions.
  23100. ((< (point) post-affiliated)
  23101. (and (save-excursion
  23102. (beginning-of-line)
  23103. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23104. (> (point) (match-end 0))
  23105. (org--flyspell-object-check-p element)))
  23106. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23107. ((let ((log (org-log-into-drawer)))
  23108. (and log
  23109. (let ((drawer (org-element-lineage element '(drawer))))
  23110. (and drawer
  23111. (eq (compare-strings
  23112. log nil nil
  23113. (org-element-property :drawer-name drawer) nil nil t)
  23114. t)))))
  23115. nil)
  23116. (t
  23117. (case (org-element-type element)
  23118. ((comment quote-section) t)
  23119. (comment-block
  23120. ;; Allow checks between block markers, not on them.
  23121. (and (> (line-beginning-position) post-affiliated)
  23122. (save-excursion
  23123. (end-of-line)
  23124. (skip-chars-forward " \r\t\n")
  23125. (< (point) (org-element-property :end element)))))
  23126. ;; Arbitrary list of keywords where checks are meaningful.
  23127. ;; Make sure point is on the value part of the element.
  23128. (keyword
  23129. (and (member (org-element-property :key element)
  23130. '("DESCRIPTION" "TITLE"))
  23131. (save-excursion
  23132. (search-backward ":" (line-beginning-position) t))))
  23133. ;; Check is globally allowed in paragraphs verse blocks and
  23134. ;; table rows (after affiliated keywords) but some objects
  23135. ;; must not be affected.
  23136. ((paragraph table-row verse-block)
  23137. (let ((cbeg (org-element-property :contents-begin element))
  23138. (cend (org-element-property :contents-end element)))
  23139. (and cbeg (>= (point) cbeg) (< (point) cend)
  23140. (org--flyspell-object-check-p element))))))))))
  23141. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23142. (defun org-remove-flyspell-overlays-in (beg end)
  23143. "Remove flyspell overlays in region."
  23144. (and (org-bound-and-true-p flyspell-mode)
  23145. (fboundp 'flyspell-delete-region-overlays)
  23146. (flyspell-delete-region-overlays beg end)))
  23147. (defvar flyspell-delayed-commands)
  23148. (eval-after-load "flyspell"
  23149. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23150. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23151. (eval-after-load "bookmark"
  23152. '(if (boundp 'bookmark-after-jump-hook)
  23153. ;; We can use the hook
  23154. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23155. ;; Hook not available, use advice
  23156. (defadvice bookmark-jump (after org-make-visible activate)
  23157. "Make the position visible."
  23158. (org-bookmark-jump-unhide))))
  23159. ;; Make sure saveplace shows the location if it was hidden
  23160. (eval-after-load "saveplace"
  23161. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23162. "Make the position visible."
  23163. (org-bookmark-jump-unhide)))
  23164. ;; Make sure ecb shows the location if it was hidden
  23165. (eval-after-load "ecb"
  23166. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23167. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23168. (if (derived-mode-p 'org-mode)
  23169. (org-show-context))))
  23170. (defun org-bookmark-jump-unhide ()
  23171. "Unhide the current position, to show the bookmark location."
  23172. (and (derived-mode-p 'org-mode)
  23173. (or (outline-invisible-p)
  23174. (save-excursion (goto-char (max (point-min) (1- (point))))
  23175. (outline-invisible-p)))
  23176. (org-show-context 'bookmark-jump)))
  23177. (defun org-mark-jump-unhide ()
  23178. "Make the point visible with `org-show-context' after jumping to the mark."
  23179. (when (and (derived-mode-p 'org-mode)
  23180. (outline-invisible-p))
  23181. (org-show-context 'mark-goto)))
  23182. (eval-after-load "simple"
  23183. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23184. "Make the point visible with `org-show-context'."
  23185. (org-mark-jump-unhide)))
  23186. (eval-after-load "simple"
  23187. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23188. "Make the point visible with `org-show-context'."
  23189. (org-mark-jump-unhide)))
  23190. (eval-after-load "simple"
  23191. '(defadvice pop-global-mark (after org-make-visible activate)
  23192. "Make the point visible with `org-show-context'."
  23193. (org-mark-jump-unhide)))
  23194. ;; Make session.el ignore our circular variable
  23195. (defvar session-globals-exclude)
  23196. (eval-after-load "session"
  23197. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23198. ;;;; Finish up
  23199. (provide 'org)
  23200. (run-hooks 'org-load-hook)
  23201. ;;; org.el ends here